<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Tristan.Blog</title>
      <link>https://ravit.ch</link>
      <description></description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://ravit.ch/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 26 Jul 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Piping data to Emacs</title>
          <pubDate>Sun, 26 Jul 2026 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2026-07-26-piping-data-to-emacs/</link>
          <guid>https://ravit.ch/blog/2026-07-26-piping-data-to-emacs/</guid>
          <description>&lt;p&gt;About once per month I find myself wanting to pipe some data into Emacs and being annoyed that I can&#x27;t.  I built &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;pipemacs&quot;&gt;Pipemacs&lt;&#x2F;a&gt; to let me do it.&lt;&#x2F;p&gt;
&lt;p&gt;Somewhat surprisingly, Emacs does not natively support piping data into it via standard input (e.g., as you might do from a shell).  The &lt;em&gt;Emacs Way&lt;&#x2F;em&gt; is to just run all commands via &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;manual&#x2F;html_node&#x2F;eshell&#x2F;&quot;&gt;Eshell&lt;&#x2F;a&gt;.  I don&#x27;t live in Emacs quite enough for that workflow to be natural.  My most common use case is just running normal commands but wanting to browse the output as it is produced (i.e., streaming) in something more capable than &lt;code&gt;less&lt;&#x2F;code&gt;.  I mostly used Vim for this, but was always annoyed that Emacs didn&#x27;t make it easier.&lt;&#x2F;p&gt;
&lt;p&gt;I am not the first person to want this.  The closest tool I found was &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;agzam&#x2F;mxp&quot;&gt;mxp&lt;&#x2F;a&gt;, which works by 1) starting a local TCP server in emacs, 2) populating a buffer with the data streamed to the TCP server, and 3) streaming data to it using the &lt;code&gt;&#x2F;dev&#x2F;tcp&lt;&#x2F;code&gt; Bash built-in.  This is flexible and works well, but mxp will only send data to a running Emacs instance using &lt;code&gt;emacsclient&lt;&#x2F;code&gt;.  I don&#x27;t run Emacs in server mode, so that wasn&#x27;t quite what I wanted: I run many separate Emacs instances and almost always want to stream command output to a new instance.  I thought about extending mxp to be able to spawn new instances, but wanted to try a slightly simpler approach, which turned into Pipemacs.&lt;&#x2F;p&gt;
&lt;p&gt;Pipemacs supports sending data to a new Emacs instance or an existing Emacs server using &lt;code&gt;emacsclient&lt;&#x2F;code&gt;.  It also uses Emacs support for TCP connections, but hosts the server in Pipemacs instead of Emacs (i.e., Emacs becomes the client instead).  This results in running less code (and simpler code) in Emacs.  It also avoids having to use string substitution to set the port in the elisp script that mxp uses.&lt;&#x2F;p&gt;
&lt;p&gt;While I was working on Pipemacs I wondered why mxp used TCP connections instead of something like a named pipe.  I don&#x27;t know the author&#x27;s full rationale, but it quickly became apparent that the TCP connection approach is at the very least easier.  Surprisingly, Emacs does not expose many functions for incrementally reading from (or writing to) files.  The best interface it provides for incremental I&#x2F;O is through its process abstraction, where there is a special process type for TCP connections.  This interface lets you provide a callback (a &lt;em&gt;process filter&lt;&#x2F;em&gt;) to handle received input and gives you the &lt;code&gt;process-send-string&lt;&#x2F;code&gt; function to send data out of Emacs.&lt;&#x2F;p&gt;
&lt;p&gt;As I was writing Pipemacs it occurred to me that while sending data to Emacs over standard input was useful, I could also send the modified buffer contents back to the Pipemacs shim (&lt;code&gt;pm&lt;&#x2F;code&gt;) to be echoed back to standard output.  This lets the caller put Pipemacs into the middle of shell pipelines to either inspect or modify data:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;color:#657b83;&quot;&gt;&lt;code&gt;&lt;span&gt;foo | pm --writeback | bar
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;m not sure if this is very useful or a good idea yet, but it was too easy to implement to not just include it.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Parser combinators in Rust</title>
          <pubDate>Sun, 12 Mar 2023 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2023-03-12-parser-combinators-in-rust/</link>
          <guid>https://ravit.ch/blog/2023-03-12-parser-combinators-in-rust/</guid>
          <description>&lt;p&gt;I have been working on a side project in Rust recently and thought I would write up some of the interesting things I&#x27;ve encountered while building something substantial and learning Rust at the same time.&lt;&#x2F;p&gt;
&lt;p&gt;Early in the project, I had a need to some strings into structured types.  Coming from Haskell, my first thought was to use parser combinators; however, I was open to other options if there was something better or more conventional in the Rust ecosystem.  It is probably worth a separate post to explore the parsing ecosystem, but long story short: none of the solutions really stood out to me.  In the realm of parser combinators, the most prominent solution seems to be &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;nom&quot;&gt;nom&lt;&#x2F;a&gt;, but it really did not click for me and seemed to have poor composition tools, which is a problem for a parser combinator library.  I ended up using &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;combine&quot;&gt;combine&lt;&#x2F;a&gt;, which is more like what I would expect from a parser combinator library.&lt;&#x2F;p&gt;
&lt;p&gt;Consider this definition of types for a language:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    PrimInteger,
&lt;&#x2F;span&gt;&lt;span&gt;    PrimBoolean,
&lt;&#x2F;span&gt;&lt;span&gt;    PrimString,
&lt;&#x2F;span&gt;&lt;span&gt;    List(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;Type&amp;gt;),
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first three constructors are primitive types, while the last type is a recursive type that can contain other types.  For example, &lt;code&gt;List&amp;lt;List&amp;lt;PrimInteger&amp;gt;&amp;gt;&lt;&#x2F;code&gt; is allowed.  I&#x27;ll walk through my journey trying to parse this simple type.  First, start with some imports for the &lt;code&gt;combine&lt;&#x2F;code&gt; library:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;combine::parser::char::string;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;combine::{between, choice, parser};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;combine::{ParseError, Parser, Stream};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, this combinator parses the basic types:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;basic_type_parser&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    Input: Stream&amp;lt;Token = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Error: ParseError&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Token, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Range, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Position&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;choice!&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|_| Type::PrimInteger),
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;boolean&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|_| Type::PrimBoolean),
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|_| Type::PrimString)
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I want to make a few observations before continuing.  The basic combinator implementation with the &lt;code&gt;choice!&lt;&#x2F;code&gt; macro and simple &lt;code&gt;string&lt;&#x2F;code&gt; parsing combinator are exactly what I expect from a parser combinator library.  I miss the clean &lt;code&gt;Applicative&lt;&#x2F;code&gt; interface from Haskell&#x27;s parser combinator libraries, but the &lt;code&gt;map&lt;&#x2F;code&gt; interface in &lt;code&gt;combine&lt;&#x2F;code&gt; is fine.  I like types, but the type signature is unwieldy and bordering on unreasonable; the required definitions in the &lt;code&gt;where&lt;&#x2F;code&gt; clause are especially egregious.&lt;&#x2F;p&gt;
&lt;p&gt;Next, the following two parser combinators build on the primitive combinator above to parse the rest of the type language:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; A parser for the list type (note that List&amp;lt;List&amp;lt;X&amp;gt;&amp;gt; is allowed, so this can
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; be recursive)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;aggregate_type_parser&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    Input: Stream&amp;lt;Token = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Error: ParseError&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Token, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Range, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Position&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;choice!&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;        (
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;between&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;type_parser&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;        ).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|(_, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ty&lt;&#x2F;span&gt;&lt;span&gt;)| Type::List(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;::new(ty)))
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; A top-level parser to parse any type
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;type_parser&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    Input: Stream&amp;lt;Token = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Error: ParseError&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Token, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Range, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Position&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;basic_type_parser&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;aggregate_type_parser&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, this does not compile. The compiler gives the following error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;color:#657b83;&quot;&gt;&lt;code&gt;&lt;span&gt;error[E0720]: cannot resolve opaque type
&lt;&#x2F;span&gt;&lt;span&gt;   --&amp;gt; src&#x2F;main.rs:27:38
&lt;&#x2F;span&gt;&lt;span&gt;    |
&lt;&#x2F;span&gt;&lt;span&gt;27  |   fn aggregate_type_parser&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ recursive opaque type
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;33  | &#x2F;         (
&lt;&#x2F;span&gt;&lt;span&gt;34  | |             string(&amp;quot;List&amp;quot;),
&lt;&#x2F;span&gt;&lt;span&gt;35  | |             between(string(&amp;quot;&amp;lt;&amp;quot;), string(&amp;quot;&amp;gt;&amp;quot;), type_parser())
&lt;&#x2F;span&gt;&lt;span&gt;36  | |         )
&lt;&#x2F;span&gt;&lt;span&gt;37  | |             .map(|(_, ty)| Type::List(Box::new(ty)))
&lt;&#x2F;span&gt;&lt;span&gt;    | |                                                    -
&lt;&#x2F;span&gt;&lt;span&gt;    | |____________________________________________________|
&lt;&#x2F;span&gt;&lt;span&gt;    | |____________________________________________________returning here with type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;    | |____________________________________________________returning here with type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;    | |____________________________________________________returning here with type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                      returning here with type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;41  | | fn type_parser&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    | |                            --------------------------------- returning this opaque type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;    |
&lt;&#x2F;span&gt;&lt;span&gt;   ::: &#x2F;home&#x2F;tristan&#x2F;.cargo&#x2F;registry&#x2F;src&#x2F;github.com-1ecc6299db9ec823&#x2F;combine-4.6.6&#x2F;src&#x2F;parser&#x2F;char.rs:260:46
&lt;&#x2F;span&gt;&lt;span&gt;    |
&lt;&#x2F;span&gt;&lt;span&gt;260 |   pub fn string&amp;lt;&amp;#39;a, Input&amp;gt;(s: &amp;amp;&amp;#39;static str) -&amp;gt; impl Parser&amp;lt;Input, Output = &amp;amp;&amp;#39;a str&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    |                                                ------------------------------------ returning this opaque type `combine::parser::combinator::Map&amp;lt;(impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, Between&amp;lt;Input, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = &amp;amp;str&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;), [closure@src&#x2F;main.rs:37:18: 37:27]&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;error[E0720]: cannot resolve opaque type
&lt;&#x2F;span&gt;&lt;span&gt;  --&amp;gt; src&#x2F;main.rs:41:28
&lt;&#x2F;span&gt;&lt;span&gt;   |
&lt;&#x2F;span&gt;&lt;span&gt;13 | fn basic_type_parser&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |                                  --------------------------------- returning this opaque type `Or&amp;lt;impl Parser&amp;lt;Input, Output = Type&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;27 | fn aggregate_type_parser&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |                                      --------------------------------- returning this opaque type `Or&amp;lt;impl Parser&amp;lt;Input, Output = Type&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;41 | fn type_parser&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ recursive opaque type
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;46 |     basic_type_parser().or(aggregate_type_parser())
&lt;&#x2F;span&gt;&lt;span&gt;   |     -----------------------------------------------
&lt;&#x2F;span&gt;&lt;span&gt;   |     |
&lt;&#x2F;span&gt;&lt;span&gt;   |     returning here with type `Or&amp;lt;impl Parser&amp;lt;Input, Output = Type&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;`
&lt;&#x2F;span&gt;&lt;span&gt;   |     returning here with type `Or&amp;lt;impl Parser&amp;lt;Input, Output = Type&amp;gt;, impl Parser&amp;lt;Input, Output = Type&amp;gt;&amp;gt;`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This set of errors is overwhelming, but has a single cause.  The first error message indicates that &lt;code&gt;impl Parser&amp;lt;Input, Output = Type&amp;gt;&lt;&#x2F;code&gt; (the return type of the aggregate type parser) is a &amp;quot;recursive opaque type&amp;quot;.  It sure is.  The &lt;code&gt;impl Trait&lt;&#x2F;code&gt; feature is a way to tell the compiler that the function will return some type that implements the named trait and ask the compiler to infer the concrete type.  My understanding is that the Rust compiler uses a constraint solver to do so, but will not attempt to solve &lt;code&gt;impl Trait&lt;&#x2F;code&gt; declarations for functions that occur in recursive cycles.  That is a problem for parser combinators, where recursion is essential.&lt;&#x2F;p&gt;
&lt;p&gt;It seems like it should be possible to break the recursive cycle by writing the type by hand to save the compiler from having to solve it.  It is not clear what that type should really be; I suspect that it is challenging to write.  It turns out that the right answer is to rewrite the functions as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;type_parser&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;Input&amp;gt;() -&amp;gt; impl Parser&amp;lt;Input, Output = Type&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    Input: Stream&amp;lt;Token = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Error: ParseError&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Token, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Range, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Input::&lt;&#x2F;span&gt;&lt;span&gt;Position&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;type_parser_&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parser! &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;type_parser_&lt;&#x2F;span&gt;&lt;span&gt;[Input]()(Input) -&amp;gt; Type
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where &lt;&#x2F;span&gt;&lt;span&gt;[ Input: Stream&amp;lt;Token = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; ]
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;basic_type_parser&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;aggregate_type_parser&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;parser!&lt;&#x2F;code&gt; macro provided by the &lt;code&gt;combine&lt;&#x2F;code&gt; library makes the type explicit, accomplishing that goal&lt;sup&gt;&lt;a href=&quot;#sidenote-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-1&quot;&gt;
    &lt;sup&gt;1&lt;&#x2F;sup&gt;
    &lt;small&gt;
        I was not able to find this explicitly called out in the documentation.  I found it by reading &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Marwes&#x2F;combine&#x2F;blob&#x2F;3f59f9d9173ffc8d017a7eed7dc52c128ba5159c&#x2F;benches&#x2F;json.rs#L162&quot;&gt;an example&lt;&#x2F;a&gt; in the GitHub repository.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
.  While this works, it is unfortunate that it has to be this way.  There are at least two major factors at play here:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The design of the &lt;code&gt;combine&lt;&#x2F;code&gt; library makes this problem inevitable&lt;&#x2F;li&gt;
&lt;li&gt;The implementation of the Rust language feels incomplete&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The &lt;code&gt;combine&lt;&#x2F;code&gt; library made the choice for one style of ergonomic type signature, which relies on the &lt;code&gt;impl Trait&lt;&#x2F;code&gt; pattern.  I do not know what all of the alternatives look like, so it is difficult to evaluate that choice right now.&lt;&#x2F;p&gt;
&lt;p&gt;The Rust language chooses not to require or support inference of &lt;code&gt;impl Trait&lt;&#x2F;code&gt; types in the presence of recursion.  That does not seem like an inherent limitation to the model, as general constraint solvers have no trouble with recursion.  It seems like this is omitted because the implementation cannot reliably handle it.  I have not looked for sources on that, but I would be shocked if it was computationally infeasible to support.&lt;&#x2F;p&gt;
&lt;p&gt;For now, the &lt;code&gt;parser!&lt;&#x2F;code&gt; macro is an acceptable workaround.  I hope the Rust language evolves to be more general and reduce the number of sharp edges in the type system.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Releasing haggle-0.2</title>
          <pubDate>Sun, 08 May 2022 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2022-05-08-releasing-haggle-0-2/</link>
          <guid>https://ravit.ch/blog/2022-05-08-releasing-haggle-0-2/</guid>
          <description>&lt;p&gt;I just pushed a new release of &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;haggle-0.2&quot;&gt;haggle&lt;&#x2F;a&gt; to Hackage today. The haggle library implements a number of different representations of graph data structures with different capability and efficiency tradeoffs. It is inspired by both &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;fgl&quot;&gt;fgl&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.boost.org&#x2F;doc&#x2F;libs&#x2F;1_79_0&#x2F;libs&#x2F;graph&#x2F;doc&#x2F;index.html&quot;&gt;boost::graph&lt;&#x2F;a&gt;, with the goal of enabling users to choose the right graph representation for their needs, while avoiding the partial functions that are used in fgl.&lt;&#x2F;p&gt;
&lt;p&gt;This release largely adds some instances contributed by users (including some &lt;code&gt;Unbox&lt;&#x2F;code&gt; instances).  The one small API change is to finally expose a function, &lt;code&gt;vertexId&lt;&#x2F;code&gt;, to extract an &lt;code&gt;Int&lt;&#x2F;code&gt; from a &lt;code&gt;Vertex&lt;&#x2F;code&gt;.  One of haggle&#x27;s original goals was to keep the &lt;code&gt;Vertex&lt;&#x2F;code&gt; type opaque to ensure that users could not create invalid vertices in graphs.  However, it became apparent that some part of the structure would need to be exposed, at least in a read-only capacity:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Interfacing with external libraries (e.g., to render graphs using &lt;a href=&quot;https:&#x2F;&#x2F;graphviz.org&#x2F;&quot;&gt;GraphViz&lt;&#x2F;a&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Implementing many graph algorithms efficiently usually requires an index of vertices that benefits from a raw &lt;code&gt;Int&lt;&#x2F;code&gt; representation&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;While I had haggle paged in, I also took the opportunity to clean up some technical debt, fix compilation with some older versions of GHC, and migrate to Github Actions for CI (from Travis CI).  I used the excellent &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;haskell-CI&#x2F;haskell-ci&quot;&gt;haskell-ci&lt;&#x2F;a&gt; tool to auto-generate the CI configuration, which worked on the first try.  It was a great experience, so I expect to use it going forward on all of my simpler packages.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Introducing build-bom</title>
          <pubDate>Sat, 18 Sep 2021 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2021-09-18-introducing-build-bom/</link>
          <guid>https://ravit.ch/blog/2021-09-18-introducing-build-bom/</guid>
          <description>&lt;h1 id=&quot;tl-dr&quot;&gt;TL;DR&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;build-bom&quot;&gt;build-bom&lt;&#x2F;a&gt; tool makes it easy to obtain LLVM IR for C&#x2F;C++ programs without requiring any modifications to build systems by using the low-level debugger primitive &lt;code&gt;ptrace&lt;&#x2F;code&gt;. This is something you may want if you build tools to analyze C&#x2F;C++ programs. A typical use of &lt;code&gt;build-bom&lt;&#x2F;code&gt; looks like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;.&#x2F;configure
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;build-bom&lt;&#x2F;span&gt;&lt;span&gt; generate-bitcode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt; make
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;build-bom&lt;&#x2F;span&gt;&lt;span&gt; extract-bitcode &#x2F;path&#x2F;to&#x2F;binary&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt; --output&lt;&#x2F;span&gt;&lt;span&gt; binary.bc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;background&quot;&gt;Background&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;what-is-llvm-ir&quot;&gt;What is LLVM IR?&lt;&#x2F;h2&gt;
&lt;p&gt;LLVM IR is the intermediate representation of code used by LLVM. The IR is a form of three address code that can be thought of as an abstract load&#x2F;store machine with unlimited virtual registers. It is both straightforward to generate from many imperative languages and amenable to the analyses and transformations necessary for an optimizing compiler.&lt;&#x2F;p&gt;
&lt;p&gt;Note that this is often referred to as LLVM Bitcode. To be precise, the bitcode is the format of a data stream that is used to represent LLVM IR. The distinction is slightly academic, as the LLVM bitcode format is not used for anything besides LLVM IR. I will refer to it as LLVM IR in this post.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-llvm-ir&quot;&gt;Why LLVM IR?&lt;&#x2F;h2&gt;
&lt;p&gt;There are, presumably, many things one can do with programs represented in LLVM IR. I will focus on the needs of program analysis (e.g., static analysis and verification) tools, which analyze programs to learn things about them.  By virtue of maintaining &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Static_single_assignment_form&quot;&gt;Static Single Assignment (SSA) form&lt;&#x2F;a&gt;, it is especially amenable to static analysis. Common program analysis tasks include finding bugs or proving that programs satisfy some specification (e.g., proving the absence of memory errors).&lt;&#x2F;p&gt;
&lt;p&gt;While analyzing C and C++ source directly would be ideal&lt;sup&gt;&lt;a href=&quot;#sidenote-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-1&quot;&gt;
    &lt;sup&gt;1&lt;&#x2F;sup&gt;
    &lt;small&gt;
        It would be worth devoting an entire post to some of the nuanced differences between source-level analysis and analysis of LLVM IR.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
, it is very difficult in practice due to the complexity of parsing and semantically analyzing C (and particularly C++). By analyzing programs represented in LLVM IR instead&lt;sup&gt;&lt;a href=&quot;#sidenote-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-2&quot;&gt;
    &lt;sup&gt;2&lt;&#x2F;sup&gt;
    &lt;small&gt;
        One might ask: why LLVM IR over one of the nearly countless other compiler IRs out there? The LLVM project has a thriving community with many contributors, as well as one of the most mature C&#x2F;C++ frontends available. It has also maintained a reasonably high degree of compatibility over the years (at least at the LLVM IR level). There are also libraries in many different programming languages for reading and creating LLVM IR.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
, tools are able to reuse the parser and semantic analysis of the Clang compiler, which has the benefit of many thousands of person hours of engineering. Beyond simplifying parsing, the number of constructs in LLVM IR (i.e., the number of instructions) is significantly smaller than the number of syntactic constructs in C. This simpler representation, which still precisely captures the semantics of the program, in turn simplifies the implementation of program analysis tools.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;where-does-llvm-ir-come-from&quot;&gt;Where does LLVM IR Come From?&lt;&#x2F;h2&gt;
&lt;p&gt;To turn a C source file into LLVM IR, one usually uses &lt;code&gt;clang&lt;&#x2F;code&gt; with the &lt;code&gt;-emit-llvm&lt;&#x2F;code&gt; argument:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;clang&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt; -emit-llvm -c&lt;&#x2F;span&gt;&lt;span&gt; foo.c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span&gt; foo.bc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That works well for a single source file, but creating LLVM IR for an entire program is more complicated. In fact, it is as complicated as the build system for the program, which can be arbitrarily complicated. In simple cases, one could modify the build system to generate LLVM IR instead of object files and then combine them into a single larger LLVM IR file using &lt;code&gt;llvm-link&lt;&#x2F;code&gt;. Ideally, there would be a tool to make the process easier.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;challenging-build-features&quot;&gt;Challenging Build Features&lt;&#x2F;h2&gt;
&lt;p&gt;Before looking at possible implementations of a tool to generate LLVM IR for a C&#x2F;C++ codebase, it is helpful to think about the constraints that such a tool should satisfy. These constraints are embodied by challenging features of build systems that appear in the wild.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;There is no standard build tooling for C&#x2F;C++.&lt;&#x2F;strong&gt; Since there is no standard build system, every C&#x2F;C++ project invents its own build system. While there are a number of common build systems (e.g., autotools and &lt;code&gt;cmake&lt;&#x2F;code&gt;), every instance of these build systems has its own unique quirks. &lt;em&gt;A general LLVM IR generator cannot assume anything about the build system.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems can invoke build intermediate executable artifacts.&lt;&#x2F;strong&gt; This case is not common, but arises for projects that build an intermediate code generator that generates additional source files. These intermediate binaries are necessary and must work, or the build will fail. &lt;em&gt;A general LLVM IR generator must generate executable artifacts faithfully.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems can delete intermediate files and directories.&lt;&#x2F;strong&gt; Some build systems, particularly &lt;code&gt;cmake&lt;&#x2F;code&gt;, aggressively discard intermediate files and directories. &lt;em&gt;A general LLVM IR generator must not leave LLVM IR in the build tree.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems can be configured via environment variables (except when they cannot)&lt;&#x2F;strong&gt; Some build systems, like &lt;code&gt;cmake&lt;&#x2F;code&gt;, can be configured through environment variables (e.g., &lt;code&gt;CC&lt;&#x2F;code&gt;). However, others reset environment variables either to achieve hermetic builds (or incidentally). &lt;em&gt;A general LLVM IR generator cannot rely on being able to pass environment variables to all stages of a build process.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems can invoke other build systems at build time.&lt;&#x2F;strong&gt; While recursive make is a famous and often maligned example, the reality of build systems in the wild can be even worse. For example, there are a number of build systems based on autotools that invoke other autotools build systems (i.e., &lt;code&gt;configure&lt;&#x2F;code&gt; scripts) at build time&lt;sup&gt;&lt;a href=&quot;#sidenote-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-3&quot;&gt;
    &lt;sup&gt;3&lt;&#x2F;sup&gt;
    &lt;small&gt;
        I remember encountering this when attempting to generate LLVM IR for gdb, which contains a few sub-libraries with their own configure scripts.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
.  It is usually very difficult to control the build flags passed to those recursively-invoked build systems, as they are not designed for customization. &lt;em&gt;A general LLVM IR generator cannot require passing arguments to the build system.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Configure scripts are very clever.&lt;&#x2F;strong&gt; Not all &lt;code&gt;configure&lt;&#x2F;code&gt; scripts (or variants for other build systems) are overly clever; however, some go to extraordinary lengths to fingerprint the system compiler. &lt;em&gt;A general LLVM IR generator cannot fool a configure script into believing it is the system compiler without actually being the system compiler.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems can hard-code absolute paths to tools.&lt;&#x2F;strong&gt; While it is not common, the configure stage of a build system may pick up absolute paths to build tools, which can become difficult to modify after configuration time. &lt;em&gt;A general LLVM IR generator cannot rely on substituting build tools post-configuration.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build systems may be externally-orchestrated.&lt;&#x2F;strong&gt; Some projects with complex dependencies or complex run-time setup requirements&lt;sup&gt;&lt;a href=&quot;#sidenote-4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-4&quot;&gt;
    &lt;sup&gt;4&lt;&#x2F;sup&gt;
    &lt;small&gt;
        The build system for the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nasa&#x2F;fprime&quot;&gt;NASA F&#x27; (F prime) system&lt;&#x2F;a&gt; is orchestrated by a number of python scripts that aggressively regenerate the build system after any changes. This is convenient for development, but is a pain when attempting to modify the build system temporarily.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
 provide orchestration scripts. Those scripts can aggressively reconfigure the build system, making it difficult to make spot modifications to substitute non-standard build tools. &lt;em&gt;A general LLVM IR generator cannot rely on being able to modify the build system.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Build system binaries can be statically linked.&lt;&#x2F;strong&gt; Build systems written in Golang or running on a fully statically linked distribution are more difficult to interpose upon. &lt;em&gt;A general LLVM IR generator cannot rely on &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; hooks.&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;related-tools&quot;&gt;Related Tools&lt;&#x2F;h2&gt;
&lt;p&gt;With some background on the requirements imposed on an LLVM IR generator by the challenges of real build systems, it is useful to look at the other tools in the ecosystem that try to solve this problem.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;clang-compilation-database&quot;&gt;Clang Compilation Database&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;clang&lt;&#x2F;code&gt; compiler natively supports maintaining a &lt;em&gt;compilation database&lt;&#x2F;em&gt;, which records all of the commands used in a build. See the &lt;code&gt;-MJ&lt;&#x2F;code&gt; command line option and the &lt;a href=&quot;https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;JSONCompilationDatabase.html&quot;&gt;Compilation Database documentation&lt;&#x2F;a&gt; for details. Some build systems, like &lt;code&gt;cmake&lt;&#x2F;code&gt;, provide native support for generating a compilation database. For well-behaved builds, one can easily write a script to replay the compilation database and generate LLVM IR for all of the input files.&lt;&#x2F;p&gt;
&lt;p&gt;This approach is elegant, but only works for build systems that are already compatible with &lt;code&gt;clang&lt;&#x2F;code&gt; &lt;em&gt;and&lt;&#x2F;em&gt; that do not dynamically generate&#x2F;delete source files. Dynamic source generation can be problematic because entries in the database may not exist after a build completes, so replaying the build accurately can be impossible.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;clang-link-time-optimization-lto&quot;&gt;Clang Link Time Optimization (LTO)&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;clang&lt;&#x2F;code&gt; compiler also now supports Link Time Optimization (LTO), which enables whole-program optimization by causing &lt;code&gt;clang&lt;&#x2F;code&gt; to populate object files with LLVM IR rather than native machine code. It then performs whole-program optimization at link time over a combined whole-program LLVM IR file. This is exactly what we want in principle, but in practice can be tricky to work with.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Supporting LTO requires the build system to be modified to support LTO, which can be a non-trivial effort.&lt;&#x2F;li&gt;
&lt;li&gt;Users would need to manually collect all of the LLVM IR they want to analyze, which can be difficult if a build system is set up to build multiple independent binaries or libraries that should not have their constituent object files combined for analysis.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;wllvm&quot;&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;whole-program-llvm&quot;&gt;wllvm&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;I started writing the &lt;code&gt;wllvm&lt;&#x2F;code&gt; tool in 2011 to solve the problem of generating LLVM IR for entire programs and libraries&lt;sup&gt;&lt;a href=&quot;#sidenote-5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-5&quot;&gt;
    &lt;sup&gt;5&lt;&#x2F;sup&gt;
    &lt;small&gt;
        While I wrote &lt;code&gt;wllvm&lt;&#x2F;code&gt;, it has been graciously maintained for the last few years by &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ianamason&quot;&gt;Ian Mason&lt;&#x2F;a&gt;, as most of my work has not involved LLVM until recently.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
. &lt;code&gt;wllvm&lt;&#x2F;code&gt; is a set of Python scripts that mimic a compiler driver, which attempts to be a drop-in replacement for &lt;code&gt;gcc&lt;&#x2F;code&gt;. Typically, one uses &lt;code&gt;wllvm&lt;&#x2F;code&gt; by configuring the build system to use &lt;code&gt;wllvm&lt;&#x2F;code&gt; as an alternative compiler (e.g., via the &lt;code&gt;CC&lt;&#x2F;code&gt; environment variable for autotools builds or &lt;code&gt;cmake&lt;&#x2F;code&gt; builds).&lt;&#x2F;p&gt;
&lt;p&gt;When invoked, the &lt;code&gt;wllvm&lt;&#x2F;code&gt; script compiles its input twice: first by invoking the underlying compiler with all of the requested flags and then again by using &lt;code&gt;clang&lt;&#x2F;code&gt; to generate LLVM IR&lt;sup&gt;&lt;a href=&quot;#sidenote-6&quot;&gt;6&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-6&quot;&gt;
    &lt;sup&gt;6&lt;&#x2F;sup&gt;
    &lt;small&gt;
        Interpreting compiler command line arguments is one of the major sources of complexity in &lt;code&gt;wllvm&lt;&#x2F;code&gt;. The GCC family of compilers support a large number of complex command line arguments. &lt;code&gt;wllvm&lt;&#x2F;code&gt; needs to be able to parse command lines to pick out the names of input files and output files so that it can recompile the inputs and attach metadata to the generated object files. To do this, &lt;code&gt;wllvm&lt;&#x2F;code&gt; attempts to implement a command line argument parser compatible with GCC.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
. The script then writes the path&lt;sup&gt;&lt;a href=&quot;#sidenote-7&quot;&gt;7&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-7&quot;&gt;
    &lt;sup&gt;7&lt;&#x2F;sup&gt;
    &lt;small&gt;
        &lt;code&gt;wllvm&lt;&#x2F;code&gt; saves the &lt;em&gt;path&lt;&#x2F;em&gt; of the LLVM IR in object files. Originally, it saved the contents of the LLVM IR into the special ELF section directly. This is more convenient and ensures that LLVM IR never gets lost. However, it was occasionally problematic on 32 bit systems when building LLVM IR with debug information. The maximum size for an ELF section on a 32 bit platform is 4GB, which could be exceeded in debug builds for large programs.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
 to the generated LLVM IR file into a special ELF section of the object file (&lt;code&gt;.llvm_bc&lt;&#x2F;code&gt;). When the object files for a binary (shared library or executable) are linked together, the linker concatenates the contents of all of the sections with the same name, which collects all of the LLVM IR file paths into a single section. &lt;code&gt;wllvm&lt;&#x2F;code&gt; comes with a helper script, &lt;code&gt;extract-bc&lt;&#x2F;code&gt;, to extract the LLVM IR file paths, collect all of the corresponding files on disk, and link them into a single monolithic LLVM IR file using &lt;code&gt;llvm-link&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In order to support build systems that delete intermediate files or directories, &lt;code&gt;wllvm&lt;&#x2F;code&gt; supports saving generated LLVM IR into a separate directory tree that the build system does not touch. &lt;code&gt;wllvm&lt;&#x2F;code&gt; supports generating LLVM IR using either &lt;code&gt;clang&lt;&#x2F;code&gt; or the Dragonegg plugin, which is a (since abandoned) GCC plugin that supports generating LLVM IR. The Dragonegg codepath was important in the early days of the LLVM project, when the &lt;code&gt;clang&lt;&#x2F;code&gt; frontend was less mature and was unable to compile some common codebases. These days, &lt;code&gt;clang&lt;&#x2F;code&gt; can handle nearly every somewhat modern codebase.&lt;&#x2F;p&gt;
&lt;p&gt;I had a great deal of success with &lt;code&gt;wllvm&lt;&#x2F;code&gt;. The cases where it fails tend to involve complex multi-stage build systems where when:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The build system invokes autoconf scripts in ways that make it impossible to specify an alternative compiler&lt;&#x2F;li&gt;
&lt;li&gt;The build system is too clever and detects that &lt;code&gt;wllvm&lt;&#x2F;code&gt; is an unrecognized compiler and refuses to build&lt;&#x2F;li&gt;
&lt;li&gt;The build system makes it difficult to impossible to specify an alternative compiler&lt;&#x2F;li&gt;
&lt;li&gt;The build system makes it difficult to impossible to replace the detected compiler after &lt;code&gt;configure&lt;&#x2F;code&gt; runs (e.g., because it refers to build tools with absolute paths)&lt;&#x2F;li&gt;
&lt;li&gt;The build system pipes input files to the compiler&lt;sup&gt;&lt;a href=&quot;#sidenote-8&quot;&gt;8&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-8&quot;&gt;
    &lt;sup&gt;8&lt;&#x2F;sup&gt;
    &lt;small&gt;
        Piped inputs are problematic because &lt;code&gt;wllvm&lt;&#x2F;code&gt; needs to compile them twice. The compilation to a normal object file drains the contents of the pipe, leaving an empty pipe for the second compilation with &lt;code&gt;clang&lt;&#x2F;code&gt;.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Note that none of these cases are particularly common (i.e., &lt;code&gt;wllvm&lt;&#x2F;code&gt; is very effective), but the rare difficult build systems are incredibly frustrating.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;gllvm&quot;&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;SRI-CSL&#x2F;gllvm&quot;&gt;gllvm&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;gllvm&lt;&#x2F;code&gt; tool is substantially similar to &lt;code&gt;wllvm&lt;&#x2F;code&gt;. It was written by &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ianamason&quot;&gt;Ian Mason&lt;&#x2F;a&gt;, who is also the primary maintainer of &lt;code&gt;wllvm&lt;&#x2F;code&gt;. &lt;code&gt;gllvm&lt;&#x2F;code&gt; operates in essentially the same manner as &lt;code&gt;wllvm&lt;&#x2F;code&gt;. In contrast, &lt;code&gt;gllvm&lt;&#x2F;code&gt; is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Written in Golang&lt;&#x2F;li&gt;
&lt;li&gt;Easier to maintain&lt;&#x2F;li&gt;
&lt;li&gt;More actively maintained&lt;&#x2F;li&gt;
&lt;li&gt;Easier to distribute&lt;sup&gt;&lt;a href=&quot;#sidenote-9&quot;&gt;9&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-9&quot;&gt;
    &lt;sup&gt;9&lt;&#x2F;sup&gt;
    &lt;small&gt;
        &lt;code&gt;gllvm&lt;&#x2F;code&gt; is easier to distribute because it uses libraries to manipulate ELF and Mach-O files, rather than command line tools (which &lt;code&gt;wllvm&lt;&#x2F;code&gt; assumes are installed and available).
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Faster&lt;sup&gt;&lt;a href=&quot;#sidenote-10&quot;&gt;10&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-10&quot;&gt;
    &lt;sup&gt;10&lt;&#x2F;sup&gt;
    &lt;small&gt;
        &lt;code&gt;gllvm&lt;&#x2F;code&gt; is faster than &lt;code&gt;wllvm&lt;&#x2F;code&gt; because it builds the object file and LLVM IR in parallel.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The only disadvantage to using &lt;code&gt;gllvm&lt;&#x2F;code&gt; is that it does not support the Dragonegg plugin, which is much less significant in 2021. My personal feeling is that &lt;code&gt;wllvm&lt;&#x2F;code&gt; should probably be deprecated in favor of &lt;code&gt;gllvm&lt;&#x2F;code&gt; at this point.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;blight&quot;&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;trailofbits&#x2F;blight&quot;&gt;blight&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;blight&lt;&#x2F;code&gt; system is a set of scripts that provide a convenient interface for executing actions (hooks) before and after commands are invoked. It provides hooks by exporting environment variables that many build systems respect (e.g., &lt;code&gt;CC&lt;&#x2F;code&gt;, &lt;code&gt;CXX&lt;&#x2F;code&gt;, and &lt;code&gt;LD&lt;&#x2F;code&gt;). Each of the scripts that implement pre- and post-hooks can be configured by other environment variables.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;blight&lt;&#x2F;code&gt; aims to make adding custom pre- and post-hooks as simple as possible, making it a more general tool than &lt;code&gt;wllvm&lt;&#x2F;code&gt; and &lt;code&gt;gllvm&lt;&#x2F;code&gt;. It can be used to generate LLVM IR by hooking &lt;code&gt;CC&lt;&#x2F;code&gt; and &lt;code&gt;CXX&lt;&#x2F;code&gt; (i.e., the C and C++ compilers) and using a post-hook to invoke &lt;code&gt;clang&lt;&#x2F;code&gt; to generate LLVM IR.&lt;&#x2F;p&gt;
&lt;p&gt;As &lt;code&gt;blight&lt;&#x2F;code&gt; relies on manipulating the environment, both through build system configuration and &lt;code&gt;PATH&lt;&#x2F;code&gt; manipulation, it can fail in the same ways that &lt;code&gt;wllvm&lt;&#x2F;code&gt; and &lt;code&gt;gllvm&lt;&#x2F;code&gt; can.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bear&quot;&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rizsotto&#x2F;Bear&quot;&gt;Bear&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Bear&lt;&#x2F;code&gt; tool does not generate LLVM IR directly; instead, it generates a Clang compilation database from nearly arbitrary builds for later processing (e.g., generating LLVM IR). It operates somewhat differently from the others. It uses an &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt;&lt;sup&gt;&lt;a href=&quot;#sidenote-11&quot;&gt;11&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-11&quot;&gt;
    &lt;sup&gt;11&lt;&#x2F;sup&gt;
    &lt;small&gt;
        &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; is an environment variable that can be used to inject a shared library into the address space of a process on Linux and many other UNIX-like systems. If &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; contains the path to a shared library, the dynamic loader will arrange the address space such that symbols defined in the shared library &lt;em&gt;override&lt;&#x2F;em&gt; any dynamic symbol table entries with the same name. This means that &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; only works for dynamically-linked binaries and is only able to override functions with appropriate linkage (e.g., static functions cannot be hooked).
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
 hook to observe build systems and catch invocations of compilers and records them in a compilation database. Specifically, it observes calls to the &lt;code&gt;exec&lt;&#x2F;code&gt; family of functions and records their arguments.&lt;&#x2F;p&gt;
&lt;p&gt;Several caveats apply:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Relying on &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; means that statically linked build tools are not supported; in a nod to this limitation, &lt;code&gt;Bear&lt;&#x2F;code&gt; has a fallback mode based on compiler wrapper scripts&lt;&#x2F;li&gt;
&lt;li&gt;Builds cannot always be replayed from an artifact like a compilation database&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;designing-build-bom&quot;&gt;Designing build-bom&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;build-bom&lt;&#x2F;code&gt; tool is inspired by the tools described above, but attempts to learn from and improve upon them. The goal of &lt;code&gt;build-bom&lt;&#x2F;code&gt; is to generate LLVM IR for arbitrarily complex projects without requiring any modifications at all to their build systems. &lt;code&gt;build-bom&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observes&lt;&#x2F;strong&gt; build processes and,&lt;&#x2F;li&gt;
&lt;li&gt;For each source file &lt;em&gt;f&lt;&#x2F;em&gt; compiled with a recognized compiler, &lt;strong&gt;rebuilds&lt;&#x2F;strong&gt; &lt;em&gt;f&lt;&#x2F;em&gt; using &lt;code&gt;clang&lt;&#x2F;code&gt; to generate LLVM IR.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Much like &lt;code&gt;wllvm&lt;&#x2F;code&gt;, it parses command line arguments to recognize sources and targets so that it can attach LLVM IR to object files in a dedicated ELF section. It provides a sub-command to extract LLVM IR after builds complete.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;initial-design&quot;&gt;Initial Design&lt;&#x2F;h2&gt;
&lt;p&gt;In the original design of &lt;code&gt;build-bom&lt;&#x2F;code&gt; I aspired to a two stage process much like &lt;code&gt;Bear&lt;&#x2F;code&gt;, where the tool would record builds and replay them to generate LLVM IR and also enable post-build analysis of dependencies (e.g., constructing a Software Bill of Materials). In my mind, the distinguishing feature of &lt;code&gt;build-bom&lt;&#x2F;code&gt; was that it would use &lt;code&gt;ptrace&lt;&#x2F;code&gt; to observe uses of the &lt;code&gt;execve&lt;&#x2F;code&gt; system call (i.e., trace spawned processes) instead of using &lt;code&gt;LD_PRELOAD&lt;&#x2F;code&gt; hooks. By using the lower-level facilities provided by the kernel rather than the dynamic loader, it would be more robust and work for statically-linked build system and for build systems that alter the environment.&lt;&#x2F;p&gt;
&lt;p&gt;During the initial testing of this design, it became apparent that replaying arbitrary builds is not practical. A few of the observed failures included:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Build systems that move files cannot be replayed.&lt;&#x2F;strong&gt; While it may seem like recording a file move (and all other file operations) should solve this problem, most file move operations do not correspond to a single system call&lt;sup&gt;&lt;a href=&quot;#sidenote-12&quot;&gt;12&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-12&quot;&gt;
    &lt;sup&gt;12&lt;&#x2F;sup&gt;
    &lt;small&gt;
        While file move operations within a single filesystem can be performed with the &lt;code&gt;rename&lt;&#x2F;code&gt; system call, it does not work across filesystems. Most programs that need to move files use a mix of I&#x2F;O calls and integrity checks provided by their programming language standard library.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
. This means that recording and replaying file move operations reliably is not really possible.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;The effects of shell scripts invoked by build systems are difficult to record.&lt;&#x2F;strong&gt; The executables invoked by a shell script can be observed and recorded using &lt;code&gt;ptrace&lt;&#x2F;code&gt;. However, shell actions (e.g., file redirections and pipes) cannot be easily replayed. This means that files &lt;em&gt;created&lt;&#x2F;em&gt; by shell scripts in the build process cannot be recreated while replaying the build; if the build system removed them, they are gone.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In principle, replaying every &lt;code&gt;write&lt;&#x2F;code&gt; system call---along with its arguments---should be sufficient to reconstruct any files. Tracking operations at this granularity seems problematically complex, and could ultimately require recording a huge range of system calls.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;actual-design&quot;&gt;Actual Design&lt;&#x2F;h2&gt;
&lt;p&gt;Rather than recording traces of build events, &lt;code&gt;build-bom&lt;&#x2F;code&gt; is now a hybrid of the &lt;code&gt;wllvm&lt;&#x2F;code&gt; and Bear approaches. It uses &lt;code&gt;ptrace&lt;&#x2F;code&gt; to attach to the build process (e.g., &lt;code&gt;make&lt;&#x2F;code&gt; or &lt;code&gt;ninja&lt;&#x2F;code&gt;) and generates LLVM IR with &lt;code&gt;clang&lt;&#x2F;code&gt; as it observes &lt;code&gt;execve&lt;&#x2F;code&gt; system calls that spawn recognized compilers. Like &lt;code&gt;wllvm&lt;&#x2F;code&gt;, it parses compiler command line arguments to determine source files and targets, enabling it to both generate LLVM IR for source files and attach it to the corresponding target (i.e., object file) created by the original compilation command.&lt;&#x2F;p&gt;
&lt;p&gt;By attaching to the build process (and all child processes) with &lt;code&gt;ptrace&lt;&#x2F;code&gt;, &lt;code&gt;build-bom&lt;&#x2F;code&gt; is able to invoke &lt;code&gt;clang&lt;&#x2F;code&gt;&lt;sup&gt;&lt;a href=&quot;#sidenote-13&quot;&gt;13&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-13&quot;&gt;
    &lt;sup&gt;13&lt;&#x2F;sup&gt;
    &lt;small&gt;
        Note that &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not support generating LLVM IR with the Dragonegg plugin.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
 to generate LLVM IR in the narrow window between the time the original compilation command finishes, but before it finishes terminating. This sequencing of events prevents race conditions by ensuring that the build system cannot move, rename, or remove files we need (e.g, renaming generated object files).&lt;&#x2F;p&gt;
&lt;p&gt;In contrast to &lt;code&gt;wllvm&lt;&#x2F;code&gt;, &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not invent a custom format for storing LLVM IR in an ELF section. Instead, the contents of the distinguished ELF section is a tar file. When &lt;code&gt;build-bom&lt;&#x2F;code&gt; generates LLVM IR for a source file, it wraps the LLVM IR in a tar file that it injects into the distinguished ELF section using &lt;code&gt;objcopy&lt;&#x2F;code&gt;. When the system linker combines the LLVM IR ELF sections, it concatenates their contents. It turns out that concatenating tar files produces a valid tar file with the combined contents of the original tar files. This small change means that extracting LLVM IR after the build completes is somewhat simpler, requiring just:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Extracting the contents of the tar file into a temporary directory&lt;&#x2F;li&gt;
&lt;li&gt;Linking together all of the individual LLVM bitcode files using &lt;code&gt;llvm-link&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;interesting-implementation-details&quot;&gt;Interesting Implementation Details&lt;&#x2F;h3&gt;
&lt;p&gt;Implementing &lt;code&gt;build-bom&lt;&#x2F;code&gt; was an interesting experience, as it shared some similarities with implementing a debugger.&lt;&#x2F;p&gt;
&lt;p&gt;The system call tracing in &lt;code&gt;build-bom&lt;&#x2F;code&gt; uses the &lt;code&gt;ptrace&lt;&#x2F;code&gt; system call on Linux&lt;sup&gt;&lt;a href=&quot;#sidenote-14&quot;&gt;14&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-14&quot;&gt;
    &lt;sup&gt;14&lt;&#x2F;sup&gt;
    &lt;small&gt;
        The same approach should work on the BSDs (but likely not MacOS).
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
 through the excellent &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ranweiler&#x2F;pete&quot;&gt;pete&lt;&#x2F;a&gt; library. In order to use &lt;code&gt;ptrace&lt;&#x2F;code&gt;, at least in the way that &lt;code&gt;build-bom&lt;&#x2F;code&gt; requires:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The process to be traced (i.e., the build process) needs to invoke &lt;code&gt;ptrace(PTRACE_TRACEME, ...)&lt;&#x2F;code&gt; to halt the tracee and allow the tracer to attach&lt;&#x2F;li&gt;
&lt;li&gt;The tracer (i.e., &lt;code&gt;build-bom&lt;&#x2F;code&gt;) needs to invoke &lt;code&gt;ptrace(PTRACE_ATTACH, ...)&lt;&#x2F;code&gt; to attach to the tracee&lt;&#x2F;li&gt;
&lt;li&gt;The tracer repeatedly invokes &lt;code&gt;ptrace(PTRACE_SYSCALL, ...)&lt;&#x2F;code&gt; to resume the tracee, but halt at the next system call&lt;&#x2F;li&gt;
&lt;li&gt;Each time the tracer stops the tracee at a system call, it can inspect the tracee state to determine what system call was invoked and what arguments were passed to it&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;While &lt;code&gt;ptrace&lt;&#x2F;code&gt; supports reading memory from the tracee using &lt;code&gt;ptrace(PTRACE_PEEKDATA, ...)&lt;&#x2F;code&gt;, it only supports reading a pointer-sized value at a time&lt;sup&gt;&lt;a href=&quot;#sidenote-15&quot;&gt;15&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-15&quot;&gt;
    &lt;sup&gt;15&lt;&#x2F;sup&gt;
    &lt;small&gt;
        The BSD implementations of &lt;code&gt;ptrace&lt;&#x2F;code&gt; support an additional command, &lt;code&gt;PT_IO&lt;&#x2F;code&gt;, which supports reading (or writing) arbitrary amounts of memory from the traced process.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
. This is too inefficient to read large amounts of data. &lt;code&gt;build-bom&lt;&#x2F;code&gt; uses the typical workaround of performing bulk reads by reading from &lt;code&gt;&#x2F;proc&#x2F;&amp;lt;pid&amp;gt;&#x2F;mem&lt;&#x2F;code&gt; with helper functions from the pete library.&lt;&#x2F;p&gt;
&lt;p&gt;Many build systems pass relative path names to build tools, rather than absolute paths. This is entirely reasonable from the point of view of the build system, but it does pose a slight challenge for &lt;code&gt;build-bom&lt;&#x2F;code&gt;. Throughout a build, a build tool will often change its working directory, which means that the working directory of the build tool and &lt;code&gt;build-bom&lt;&#x2F;code&gt; get out of sync. When the build system uses relative paths, &lt;code&gt;build-bom&lt;&#x2F;code&gt; must correct them in order to be able to generate LLVM IR through recompilation. While &lt;code&gt;build-bom&lt;&#x2F;code&gt; could change working directories with the build system, this would become complicated and inefficient with parallel builds. Instead, it normalizes relative paths into absolute paths when reading arguments passed to system calls.&lt;&#x2F;p&gt;
&lt;p&gt;Interpreting paths passed as arguments to system calls turned out to be a surprisingly tricky proposition. On Linux, paths are &lt;code&gt;NUL&lt;&#x2F;code&gt; terminated strings with &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; as a separator. There are no constraints on the encoding for paths beyond that, nor are there indicators as to what the encoding of any particular path is. The rust &lt;code&gt;String&lt;&#x2F;code&gt; type is explicitly a UTF-8 encoded string. This means that paths cannot be treated as rust strings; instead, &lt;code&gt;build-bom&lt;&#x2F;code&gt; represents them using the raw &lt;code&gt;OsString&lt;&#x2F;code&gt; type, which have no particular encoding for their contents. Semantically, this is entirely correct; however, it is a bit painful when trying to parse, manipulate, and print paths in &lt;code&gt;build-bom&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;evaluation-against-requirements&quot;&gt;Evaluation Against Requirements&lt;&#x2F;h2&gt;
&lt;p&gt;Earlier in this post, I outlined a list of challenging build features that a general LLVM IR generation tool should be able to handle. Since &lt;code&gt;build-bom&lt;&#x2F;code&gt; attempts to be a general LLVM IR generation tool, let&#x27;s evaluate how many it addresses.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; is build system agnostic&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; unobtrusively generates both the original executable build artifacts and LLVM IR, enabling intermediate build artifacts to be executed&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; generates LLVM IR for each source file at the same time the original object file is generated (recall, in the very narrow window between when the compiler completes but before it exists), ensuring that intermediate files are accessible&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not require any control over environment variables to work, nor does it require build systems to respect any environment variables&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; traces commands run in an entire process &#x2F;tree&#x2F;, which transparently includes child build processes&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not interpose in configuration processes or ever modify the arguments passed to the compiler&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; observes build commands, rather than interposing via scripts, so absolute paths in build systems (e.g., for compilers) are not problematic&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not need to directly trace or even be aware of the actual build process; running any orchestration scripts under &lt;code&gt;build-bom&lt;&#x2F;code&gt; will indirectly trace the build process&lt;&#x2F;li&gt;
&lt;li&gt;✓ &lt;code&gt;build-bom&lt;&#x2F;code&gt; traces system calls with kernel support (i.e., through &lt;code&gt;ptrace&lt;&#x2F;code&gt;), and so does not rely on the dynamic loader&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;design-consequences&quot;&gt;Design Consequences&lt;&#x2F;h2&gt;
&lt;p&gt;Relying on &lt;code&gt;ptrace&lt;&#x2F;code&gt; to observe system calls issued by the build system serializes all of them through &lt;code&gt;build-bom&lt;&#x2F;code&gt;. This significantly reduces the parallelism available in the build system (though processes still execute in parallel when not waiting in system calls).&lt;&#x2F;p&gt;
&lt;p&gt;The reliance on &lt;code&gt;ptrace&lt;&#x2F;code&gt; also ties &lt;code&gt;build-bom&lt;&#x2F;code&gt; to UNIX-like systems. This is not a hard limitation, as MacOS and Windows both support other mechanisms that would enable the same functionality. A &lt;code&gt;build-bom&lt;&#x2F;code&gt; backend can be implemented for any system with kernel support for debugging.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;While MacOS has the &lt;code&gt;ptrace&lt;&#x2F;code&gt; system call, it is not fully-featured; MacOS debuggers require additional Mach system calls&lt;&#x2F;li&gt;
&lt;li&gt;Windows has analogous system calls&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Since &lt;code&gt;build-bom&lt;&#x2F;code&gt; is essentially a debugger, other debuggers cannot attach to any of the processes being traced.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;build-bom&lt;&#x2F;code&gt; approach is less well-suited to builds where build commands are actually executed by a separate daemon process. Thus, using &lt;code&gt;build-bom&lt;&#x2F;code&gt; with systems like &lt;code&gt;distcc&lt;&#x2F;code&gt; and &lt;code&gt;bazel&lt;&#x2F;code&gt; is tricky. Avoiding &lt;code&gt;distcc&lt;&#x2F;code&gt; to generate LLVM IR is fairly easy. In the case of a build based on &lt;code&gt;bazel&lt;&#x2F;code&gt; (or similar systems), the server process would need to be traced.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;future-directions&quot;&gt;Future Directions&lt;&#x2F;h1&gt;
&lt;p&gt;There are a few major features that I plan to add to &lt;code&gt;build-bom&lt;&#x2F;code&gt; as they become needed:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Support for response files&lt;&#x2F;strong&gt; Response files are a feature provided by both &lt;code&gt;gcc&lt;&#x2F;code&gt; and &lt;code&gt;clang&lt;&#x2F;code&gt; to make it possible to pass in large numbers of arguments, especially on Windows&lt;sup&gt;&lt;a href=&quot;#sidenote-16&quot;&gt;16&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-16&quot;&gt;
    &lt;sup&gt;16&lt;&#x2F;sup&gt;
    &lt;small&gt;
        Operating systems typically limit the number of command line arguments, as well as the total storage occupied by command line arguments. See the definition of &lt;code&gt;ARG_MAX&lt;&#x2F;code&gt; on POSIX-like systems, which is typically a few megabytes on Linux. The limit is considerably lower on Windows (&lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-createprocessw&quot;&gt;about 32kb&lt;&#x2F;a&gt;), which is usually the motivation for supporting response files.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
. A response file simply records each argument to a process on its own line; &lt;code&gt;build-bom&lt;&#x2F;code&gt; would need to parse response files to determine the inputs and outputs of compilation commands.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Support piped inputs&lt;&#x2F;strong&gt; Currently &lt;code&gt;build-bom&lt;&#x2F;code&gt; does not work correctly with file inputs that are provided to the compiler via pipe. The original compilation command drains the pipe, leaving &lt;code&gt;build-bom&lt;&#x2F;code&gt; with no input file contents to pass to &lt;code&gt;clang&lt;&#x2F;code&gt; to generate bitcode. To fix this, &lt;code&gt;build-bom&lt;&#x2F;code&gt; will need to inspect input files and, if they are pipes, replace the pipe file with a temporary file on disk with the contents of the pipe&lt;sup&gt;&lt;a href=&quot;#sidenote-17&quot;&gt;17&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;span class=&quot;sidenote&quot; id=&quot;sidenote-17&quot;&gt;
    &lt;sup&gt;17&lt;&#x2F;sup&gt;
    &lt;small&gt;
        Note that this requires modifying the command line arguments in the build process to point to the new temporary file. This is somewhat complicated, as it requires finding space in the build process to store the new path without clobbering any existing data. It remains to be seen how this can best be handled.
    &lt;&#x2F;small&gt;
&lt;&#x2F;span&gt;
.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Support architectures besides x86_64&lt;&#x2F;strong&gt; &lt;code&gt;build-bom&lt;&#x2F;code&gt; contains a hard-coded table mapping system call numbers to names to be matched on. Unfortunately, system call numbers differ across architectures on Linux. Adding support for new architectures is straightforward.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Support ELF modification via library&lt;&#x2F;strong&gt; To attach LLVM IR to an object file, &lt;code&gt;build-bom&lt;&#x2F;code&gt; calls the &lt;code&gt;objcopy&lt;&#x2F;code&gt; binary, which it assumes is installed on the system. Using a library to read and write ELF files would reduce the run-time dependency footprint.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Support for Windows and MacOS&lt;&#x2F;strong&gt; Extending support to Windows and MacOS requires understanding the necessary debugging system calls, but is not conceptually difficult.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Support for arbitrary build command interposition&lt;&#x2F;strong&gt; Currently, &lt;code&gt;build-bom&lt;&#x2F;code&gt; runs extra commands to generate LLVM IR. It is a small delta to being able to arbitrarily modify build commands. I envision a &lt;code&gt;sed&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;awk&lt;&#x2F;code&gt;-like interface for matching and rewriting command line arguments. Potential uses include, but are not limited to:
&lt;ul&gt;
&lt;li&gt;Forcing the generation of debugging information&lt;&#x2F;li&gt;
&lt;li&gt;Forcing certain optimization levels&lt;&#x2F;li&gt;
&lt;li&gt;Enabling Address Sanitizer or Thread Sanitizer&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;build-bom&lt;&#x2F;code&gt; tool simplifies generating LLVM IR for C and C++ codebases. Compared to previous tools in this space, &lt;code&gt;build-bom&lt;&#x2F;code&gt; supports a wider range of build system features without requiring any modification to the build system in order to run. The major insight of &lt;code&gt;build-bom&lt;&#x2F;code&gt; is to use &lt;code&gt;ptrace&lt;&#x2F;code&gt;, the system call that provides debugging services on Linux, to observe build processes and generate LLVM IR just-in-time, avoiding many of the complexities inherent to other approaches.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Avy Interactive</title>
          <pubDate>Sun, 20 Dec 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2020-12-20-avy-interactive/</link>
          <guid>https://ravit.ch/blog/2020-12-20-avy-interactive/</guid>
          <description>&lt;p&gt;The excellent &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;abo-abo&#x2F;avy&quot;&gt;avy&lt;&#x2F;a&gt; package for emacs provides an efficient mechanism for positioning the cursor in the current window.  The avy workflow is:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Active avy via one of several possible functions&lt;&#x2F;li&gt;
&lt;li&gt;Type a few characters corresponding to the location on screen you want to navigate to&lt;&#x2F;li&gt;
&lt;li&gt;Avy will highlight each occurrence of those characters with navigation hints&lt;&#x2F;li&gt;
&lt;li&gt;Type the one or two letters corresponding to the desired destination&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Avy typically enables navigation to any position on screen in four or five keystrokes.  There are variants to highlight candidates based on a single character (&lt;code&gt;avy-goto-char&lt;&#x2F;code&gt;), two characters (&lt;code&gt;avy-goto-char-2&lt;&#x2F;code&gt;), a character at the beginning of a word (&lt;code&gt;avy-goto-word-1&lt;&#x2F;code&gt;), or an arbitrary number of characters entered within a short time limit (&lt;code&gt;avy-goto-char-timer&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;I find that the one and two character variants produce too many matches for me to easily visually identify my target. As a result, I have been using &lt;code&gt;avy-goto-char-timer&lt;&#x2F;code&gt;, which allows you to enter any number of characters; after a timeout following the final character, avy activates and highlights matches for selection.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;avy-goto-char-timer&lt;&#x2F;code&gt; function, unfortunately, does not provide a means for correcting typos.  This minor annoyance has kept me from using avy as much as I would like for navigation within a buffer.  I finally decided to fix it by introducing my own variant:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;defun &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;tr&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;avy-prompt (s)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Prompt for an avy target and activate avy with it.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  (interactive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;MAvy: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  (avy-process (avy--regex-candidates s)))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function (which I bind to &lt;code&gt;C-]&lt;&#x2F;code&gt;) simply uses the minibuffer to prompt for a string and activates avy on that string. Since the string is entered in the minibuffer, all normal editing commands work on it.  A future variant could potentially use &lt;code&gt;completing-read&lt;&#x2F;code&gt; to provide a filtered list of candidates (e.g., via &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;raxod502&#x2F;selectrum&quot;&gt;selectrum&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Less Funny Type Errors Issue 2</title>
          <pubDate>Tue, 19 May 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2020-05-19-less-funny-type-errors-2/</link>
          <guid>https://ravit.ch/blog/2020-05-19-less-funny-type-errors-2/</guid>
          <description>&lt;p&gt;This is part two of the presumably unending type errors series.  The previous entry involved an amusing type error.  This time the error is a bit less amusing:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;color:#657b83;&quot;&gt;&lt;code&gt;&lt;span&gt;ghc: panic! (the &amp;#39;impossible&amp;#39; happened)
&lt;&#x2F;span&gt;&lt;span&gt;  (GHC version 8.8.3 for x86_64-unknown-linux):
&lt;&#x2F;span&gt;&lt;span&gt;        exprToType
&lt;&#x2F;span&gt;&lt;span&gt;  Call stack:
&lt;&#x2F;span&gt;&lt;span&gt;      CallStack (from HasCallStack):
&lt;&#x2F;span&gt;&lt;span&gt;        callStackDoc, called at compiler&#x2F;utils&#x2F;Outputable.hs:1159:37 in ghc:Outputable
&lt;&#x2F;span&gt;&lt;span&gt;        pprPanic, called at compiler&#x2F;coreSyn&#x2F;CoreSyn.hs:1997:28 in ghc:CoreSyn
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Please report this as a GHC bug:  https:&#x2F;&#x2F;www.haskell.org&#x2F;ghc&#x2F;reportabug
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, there was no additional context to help narrow down the problem.  The file in question has a few hundred lines of code.  My starting set of observations was:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The code compiles and works on GHC 8.6.5 (so can&#x27;t be too bad)&lt;&#x2F;li&gt;
&lt;li&gt;It used to compile and work under GHC 8.8.3 (so there were no compile errors)&lt;&#x2F;li&gt;
&lt;li&gt;There were some warnings (but they didn&#x27;t seem particularly relevant)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I had vague memories of seeing this error before in another context and moving code around until the error went away.  I don&#x27;t remember any details besides that approach to debugging being unsatisfying.  Ultimately, I took a guess that:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;GHC was attempting to print a warning related to inferred types&lt;&#x2F;li&gt;
&lt;li&gt;GHC has recently started warning about type family applications appearing as kinds&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;There was one function in the file in question that had an explicitly quantified kind variable meeting those conditions.  Adding an explicit (dependent) kind annotation to that variable caused the crash to go away.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Funny Type Errors Issue 1</title>
          <pubDate>Sun, 19 Apr 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2020-04-19-funny-type-errors-1/</link>
          <guid>https://ravit.ch/blog/2020-04-19-funny-type-errors-1/</guid>
          <description>&lt;p&gt;I find that I run into a large number of what I would consider Funny Type Errors while using GHC.  This is the first post in what will presumably be a long series of posts on the topic.&lt;&#x2F;p&gt;
&lt;p&gt;Today I was working on trying to improve the performance of a codebase that uses a large number of the type system extensions provided by modern GHC including &lt;code&gt;-XGADTs&lt;&#x2F;code&gt; and &lt;code&gt;-XTypeInType&lt;&#x2F;code&gt;.  Seeing strange type errors in this code is expected on a daily basis.  However, the code I was working in today was not doing anything &lt;em&gt;too&lt;&#x2F;em&gt; unusual.  A reduced example of the surprising code is:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; GADTs #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; KindSignatures #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; TypeInType #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;OPTIONS_GHC&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; -Wall -Wcompat #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Functor.Const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;C
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import           &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Kind &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; (a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;) (b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; a tp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; b tp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; a b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;MonoStruct&lt;&#x2F;span&gt;&lt;span&gt; { item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Const Int&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Const Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;             , otherData &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;             }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getFirst &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;getFirst ms &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; a _b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; item ms
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;getConst a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct
&lt;&#x2F;span&gt;&lt;span&gt;update ms f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; ms { item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Const&lt;&#x2F;span&gt;&lt;span&gt; f) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Const&lt;&#x2F;span&gt;&lt;span&gt; f) }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Naively, I expected this to compile.  Instead, GHC (8.6.5 and 8.8.3) report the errors:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;color:#657b83;&quot;&gt;&lt;code&gt;&lt;span&gt;❯ ghci poly.hs
&lt;&#x2F;span&gt;&lt;span&gt;GHCi, version 8.8.3: https:&#x2F;&#x2F;www.haskell.org&#x2F;ghc&#x2F;  :? for help
&lt;&#x2F;span&gt;&lt;span&gt;[1 of 1] Compiling Main             ( poly.hs, interpreted )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;poly.hs:18:19: error:
&lt;&#x2F;span&gt;&lt;span&gt;    • Cannot use record selector ‘item’ as a function due to escaped type variables
&lt;&#x2F;span&gt;&lt;span&gt;      Probable fix: use pattern-matching syntax instead
&lt;&#x2F;span&gt;&lt;span&gt;    • In the expression: item ms
&lt;&#x2F;span&gt;&lt;span&gt;      In a pattern binding: Pair a _b = item ms
&lt;&#x2F;span&gt;&lt;span&gt;      In the expression: let Pair a _b = item ms in C.getConst a
&lt;&#x2F;span&gt;&lt;span&gt;   |
&lt;&#x2F;span&gt;&lt;span&gt;18 |   let Pair a _b = item ms
&lt;&#x2F;span&gt;&lt;span&gt;   |                   ^^^^
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;poly.hs:22:15: error:
&lt;&#x2F;span&gt;&lt;span&gt;    • Record update for insufficiently polymorphic field:
&lt;&#x2F;span&gt;&lt;span&gt;        item :: Pair (C.Const Int) (C.Const Int)
&lt;&#x2F;span&gt;&lt;span&gt;    • In the expression: ms {item = Pair (C.Const f) (C.Const f)}
&lt;&#x2F;span&gt;&lt;span&gt;      In an equation for ‘update’:
&lt;&#x2F;span&gt;&lt;span&gt;          update ms f = ms {item = Pair (C.Const f) (C.Const f)}
&lt;&#x2F;span&gt;&lt;span&gt;   |
&lt;&#x2F;span&gt;&lt;span&gt;22 | update ms f = ms { item = Pair (C.Const f) (C.Const f) }
&lt;&#x2F;span&gt;&lt;span&gt;   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is very strange: the ~MonoStruct~ type does not contain any polymorphic fields.  In fact, I called it &lt;code&gt;MonoStruct&lt;&#x2F;code&gt; in this example because it looks entirely monomorphic.  To dig a little deeper, we can ask &lt;code&gt;ghci&lt;&#x2F;code&gt; for some additional information:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;gt; :&lt;&#x2F;span&gt;&lt;span&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;fprint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;explicit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;foralls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;gt; :&lt;&#x2F;span&gt;&lt;span&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;fprint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;explicit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;kinds
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;gt; :&lt;&#x2F;span&gt;&lt;span&gt;load poly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;hs
&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;of &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Compiling Main&lt;&#x2F;span&gt;&lt;span&gt;             ( poly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;hs, interpreted )
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span&gt;, one &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;loaded&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MonoStruct
&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MonoStruct
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;forall&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt;}.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MonoStruct&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Pair &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)}
&lt;&#x2F;span&gt;&lt;span&gt;  	-- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Defined &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;at&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;poly&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#ec9489;color:#657b83;&quot;&gt;hs&lt;&#x2F;span&gt;&lt;span&gt;:11:1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This shows us what is really happening.  Note the definition of &lt;code&gt;Pair&lt;&#x2F;code&gt;: it takes two data items that are indexed by a poly-kinded type parameter (&lt;code&gt;k&lt;&#x2F;code&gt;).  This has an unfortunate interaction with the use of ~Const~ as the data elements, as ~Const~ is also kind polymorphic.  This means that while the type of the &lt;code&gt;Pair&lt;&#x2F;code&gt; in &lt;code&gt;MonoStruct&lt;&#x2F;code&gt; looks monomorphic, the &lt;em&gt;kind&lt;&#x2F;em&gt; of the pair could change on update, which causes the error we see from &lt;code&gt;ghci&lt;&#x2F;code&gt;.  As a concrete example, we could populate our &lt;code&gt;Pair&lt;&#x2F;code&gt; with &lt;code&gt;Const Int Int&lt;&#x2F;code&gt; or &lt;code&gt;Const Int &#x27;[Natural]&lt;&#x2F;code&gt;.  Specializing the type put into the &lt;code&gt;Pair&lt;&#x2F;code&gt; removes the unwanted polymorphism:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; DataKinds #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; GADTs #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; KindSignatures #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; TypeInType #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;OPTIONS_GHC&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; -Wall -Wcompat #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import           &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Kind &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Tag &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;A &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ConstInt&lt;&#x2F;span&gt;&lt;span&gt; (k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Tag&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ConstInt&lt;&#x2F;span&gt;&lt;span&gt; { getConstInt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; (a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;) (b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; k &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; a tp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; b tp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; a b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;MonoStruct&lt;&#x2F;span&gt;&lt;span&gt; { item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair ConstInt ConstInt
&lt;&#x2F;span&gt;&lt;span&gt;             }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;getFirst &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;getFirst ms &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; item ms &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; a _b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; getConstInt a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;update &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MonoStruct
&lt;&#x2F;span&gt;&lt;span&gt;update ms f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; ms { item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;Pair&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ConstInt&lt;&#x2F;span&gt;&lt;span&gt; f) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ConstInt&lt;&#x2F;span&gt;&lt;span&gt; f) }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</description>
      </item>
      <item>
          <title>Metablog 2019</title>
          <pubDate>Wed, 20 Feb 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2019-02-20-metablog-2019/</link>
          <guid>https://ravit.ch/blog/2019-02-20-metablog-2019/</guid>
          <description>&lt;p&gt;Instead of writing about something productive, I am going to again write about writing.  Or at least the infrastructure for writing.  First, I decided to switch comments from Disqus to &lt;a href=&quot;https:&#x2F;&#x2F;utteranc.es&#x2F;&quot;&gt;utteranc.es&lt;&#x2F;a&gt;, which is a clever approach to hosting comments for statically-generated sites.  For each post, the script creates a GitHub issue and adds each comment to the issue.  There are no ads, and it is all around more civilized.  Moreover, I appreciate the simplicity of the model.  Not that I expect an excessive comment load.&lt;&#x2F;p&gt;
&lt;p&gt;Next, I updated the version of the &lt;a href=&quot;https:&#x2F;&#x2F;jaspervdj.be&#x2F;hakyll&#x2F;&quot;&gt;Hakyll&lt;&#x2F;a&gt; static site generator from something very old to the latest as of the time of writing.  I am very pleased to report that it did not require any code changes.  I can only aspire to that level of stability in my own code.  Despite not requiring any code changes, I took the opportunity to change one line of code to enable support for writing in Emacs org-mode instead of markdown.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Emacs Startup Optimization</title>
          <pubDate>Mon, 12 Sep 2016 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2016-09-12-emacs-startup-optimization/</link>
          <guid>https://ravit.ch/blog/2016-09-12-emacs-startup-optimization/</guid>
          <description>&lt;p&gt;Recently, I had the urge to optimize the startup time of my emacs
configuration.  I managed to reduce my startup time from three seconds
to under half a second.  It was a fun exercise even though I don&#x27;t
really start emacs that often.  Even that minor reduction makes
starting a new emacs instance (e.g., from mutt or in a terminal) much
more pleasant.  I could use the server functionality in emacs (along
with &lt;code&gt;emacsclient&lt;&#x2F;code&gt;) to always connect to a single persistent instance,
but I like the isolation of separate processes.  I&#x27;ve had the server
crash before bringing down all of my sessions, which is fairly
annoying.  Most of the startup time savings came from systematically
employing lazy package loading through John Wiegley&#x27;s excellent
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jwiegley&#x2F;use-package&quot;&gt;use-package&lt;&#x2F;a&gt;.  Besides
making it easy to properly load packages lazily, use-package also
handles package installation and configuration.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;use-package&lt;&#x2F;code&gt; encourages a configuration style that isolates the
configuration for each package in a &lt;code&gt;use-package&lt;&#x2F;code&gt; form.  Moreover, the
configuration of each package is split into initialization steps taken
at emacs startup time (via the &lt;code&gt;:init&lt;&#x2F;code&gt; section) and configuration
steps executed after the package is loaded (via the &lt;code&gt;:config&lt;&#x2F;code&gt;
section).  Packages can also be loaded lazily, which significantly
improves startup time.  By default, &lt;code&gt;use-package&lt;&#x2F;code&gt; forms that bind keys or
are associated with a specific file type are loaded lazily.&lt;&#x2F;p&gt;
&lt;p&gt;After doing a bit of profiling with
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jschaf&#x2F;esup&quot;&gt;esup&lt;&#x2F;a&gt;, which is an emacs startup
profiler, I found a few surprising initialization steps that were
taking up most of my startup time.  The most expensive parts are
difficult to optimize further: theme loading, font setting in GUI
mode, and initialization of the &lt;code&gt;package&lt;&#x2F;code&gt; library.  Aside from that,
&lt;code&gt;cc-mode&lt;&#x2F;code&gt; was one of the most expensive things to load; moving more of
the setup from &lt;code&gt;:init&lt;&#x2F;code&gt; to &lt;code&gt;:config&lt;&#x2F;code&gt; took care of that.&lt;&#x2F;p&gt;
&lt;p&gt;There was at least one interesting and unexpected consequence of my
&lt;code&gt;use-package&lt;&#x2F;code&gt; configuration that I did not initially appreciate.  I
decided to set&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;setq use-package&lt;&#x2F;span&gt;&lt;span&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;always&lt;&#x2F;span&gt;&lt;span&gt;-ensure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This option tells &lt;code&gt;use-package&lt;&#x2F;code&gt; to automatically download and install
packages that are not already installed.  This is convenient to
bootstrap a new emacs installation or to bring an existing one up to
date with the latest changes to your emacs configuration.  It has side
effect of breaking when the package being set up is not in one of the
emacs package repositories (i.e., local packages).  This came up while
setting up a major mode for
&lt;a href=&quot;https:&#x2F;&#x2F;ccl.northwestern.edu&#x2F;netlogo&#x2F;&quot;&gt;netlogo&lt;&#x2F;a&gt;.  It eventually
occurred to me that I could tell &lt;code&gt;use-package&lt;&#x2F;code&gt; to &lt;em&gt;not&lt;&#x2F;em&gt; try to install
this package by setting &lt;code&gt;:ensure nil&lt;&#x2F;code&gt; in the &lt;code&gt;use-package&lt;&#x2F;code&gt; form:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use-package&lt;&#x2F;span&gt;&lt;span&gt; netlogo-mode
&lt;&#x2F;span&gt;&lt;span&gt;  :ensure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;nil
&lt;&#x2F;span&gt;&lt;span&gt;  :&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;load&lt;&#x2F;span&gt;&lt;span&gt;-path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;~&#x2F;.config&#x2F;local&#x2F;emacs.d&#x2F;lisp&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  :mode (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.nls$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.nlogo$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; . netlogo-mode))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the process, I cleaned out old configurations I didn&#x27;t need anymore and
enabled a few new packages including:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;abo-abo&#x2F;swiper&quot;&gt;swiper&lt;&#x2F;a&gt; (for sophisticated finding and replacing)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;abo-abo&#x2F;avy&quot;&gt;avy&lt;&#x2F;a&gt; (for quick navigation)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;abo-abo&#x2F;avy&quot;&gt;switch-window&lt;&#x2F;a&gt; (for faster movement between windows)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It was fun to refresh my entire configuration.  I should probably do
that more often.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Emacs and Terminal Pasting</title>
          <pubDate>Sat, 03 Sep 2016 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2016-09-03-emacs-and-terminal-pasting/</link>
          <guid>https://ravit.ch/blog/2016-09-03-emacs-and-terminal-pasting/</guid>
          <description>&lt;p&gt;Over the last few weeks, I&#x27;ve found myself having to copy and paste
large chunks of text into emacs running in a terminal.  This leads to
some annoyance, as each character pasted triggers a keystroke.  In
particular, every newline triggers indentation via
&lt;code&gt;newline-and-indent&lt;&#x2F;code&gt;.  This is very annoying, as the indentation
usually gets a bit messy and turns into an ugly staircase of text.
Vim has a solution to this problem via a command &lt;code&gt;:set paste&lt;&#x2F;code&gt;, which
turns off indentation while pasting.  More generally, most terminals
support what is known as
&lt;a href=&quot;https:&#x2F;&#x2F;cirw.in&#x2F;blog&#x2F;bracketed-paste&quot;&gt;bracketed paste&lt;&#x2F;a&gt; to enter a
large string of text as one virtual keystroke.  I recently discovered
the emacs equivalent: bracketed-paste mode on
&lt;a href=&quot;http:&#x2F;&#x2F;melpa.org&#x2F;#&#x2F;bracketed-paste&quot;&gt;melpa&lt;&#x2F;a&gt;.  Now a long standing
annoyance is fixed with a simple:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;bracketed-paste)
&lt;&#x2F;span&gt;&lt;span&gt;(bracketed-paste-enable)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</description>
      </item>
      <item>
          <title>Linux Messaging Clients</title>
          <pubDate>Mon, 09 Nov 2015 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2015-11-09-linux-messaging/</link>
          <guid>https://ravit.ch/blog/2015-11-09-linux-messaging/</guid>
          <description>&lt;p&gt;I recently found myself looking at Linux instant messaging clients
again.  Sometimes I feel like some kind of Luddite for still using a
desktop instant messaging client, but I just cannot bring myself to
use a web application.  I have used pidgin for as long as I can
remember, but I have been looking for an alternative with better
support for primarily keyboard interaction.  My biggest complaints
about pidgin center around odd focus issues that sometimes seem to
require a mouse to fix.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, none of the GUI clients seem to offer decent
keybindings.  That leaves console clients.  I have tried finch, which
isthe console frontend to libpurple (which is itself the library that
provides all of the interesting functionality in pidgin).  finch comes
with an interesting console window manager built on top of ncurses
called gnt.  I found the default UI and keybindings to be maddening,
even more than most normal GUI applications.  That defeated the
purpose of switching.&lt;&#x2F;p&gt;
&lt;p&gt;gnt has a surprising feature: it supports alternative window manager
layouts through plugins.  I tried to use the one that mimics an
irssi-style client.  This interface superficially looked like irssi,
but the interaction model was still annoying and required awkward
focus changing commands.  Moreover, it interacted badly with terminal
resize events.  Whenever I would move the terminal to another monitor
with xmonad, the subsequent resize event would jumble the window
positions.  This caused awkward overlaps, which ruined the irssi-like
illusion that the window manager plugin was trying to present.  It
turns out that the plugin really just carefully arranges windows to
mimic the irssi interface, but only on application startup.  It would
not reflow the layout in response to changes.&lt;&#x2F;p&gt;
&lt;p&gt;For now, I have settled on &lt;a href=&quot;http:&#x2F;&#x2F;profanity.im&quot;&gt;profanity&lt;&#x2F;a&gt;.
Profanity is a console client that also mimics the irssi interface.
It is significantly more successful at doing so than finch.  The only
downside to profanity is that it only supports a single account.  It
is not a huge burden to use tmux to manage a few instances for
different accounts, though I would not object to multi-account
support.  It is worth noting that profanity only supports XMPP, which
is fine for my purposes.  It seems to support all of the XMPP
extensions that I need (and then some), including multi-user chats.  I
had to spend a bit of time configuring things properly, but I
eventually managed to get it working well with tmux and my xmonad
configuration.  Most of this configuration was related to
notifications.  I use X window urgent hints to show activity
notifications in my xmonad bar
(&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;taffybar&quot;&gt;taffybar&lt;&#x2F;a&gt;).  Profanity easily
supports this mode of operation with the
&lt;a href=&quot;http:&#x2F;&#x2F;profanity.im&#x2F;reference.html#flash&quot;&gt;&#x2F;flash&lt;&#x2F;a&gt; configuration
option.  There was a bit of an issue with running it in a tmux
session, though; tmux would set the urgent hint when there was &lt;em&gt;any&lt;&#x2F;em&gt;
console activity at all.  Annoyingly, this included &lt;em&gt;clock&lt;&#x2F;em&gt; updates in
the profanity status bar.  I had to disable that with the
&lt;a href=&quot;http:&#x2F;&#x2F;profanity.im&#x2F;reference.html#time&quot;&gt;&#x2F;time&lt;&#x2F;a&gt; configuration option,
which was unfortunately a new addition as of 0.4.7.  Of course, the
version shipped with Ubuntu 15.10 is 0.4.6.&lt;&#x2F;p&gt;
&lt;p&gt;Overall, I am pleased with profanity so far.  We shall see if I can
stand it in a week.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Unusual Emacs keybindings</title>
          <pubDate>Fri, 13 Feb 2015 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2015-02-13-unusual-emacs-keybindings/</link>
          <guid>https://ravit.ch/blog/2015-02-13-unusual-emacs-keybindings/</guid>
          <description>&lt;p&gt;I recently ran into a strange problem with running emacs.  A few days ago, my delete, page up, and page down keys stopped working, but only when emacs was run in the terminal.  Whenever I hit one of those keys, emacs would complain that the region was empty.  This was baffling for a while.  Searching the internet turned up nothing, so I had to assume that it was something I had done.  One further symptom manifest: when I used my &lt;code&gt;M-[&lt;&#x2F;code&gt; keybinding, the bound function would execute and it inserted &lt;code&gt;3~&lt;&#x2F;code&gt; into the buffer.  That was unusual.&lt;&#x2F;p&gt;
&lt;p&gt;It turns out that binding &lt;code&gt;M-[&lt;&#x2F;code&gt; to anything is a bad idea because Meta and Escape are the same in emacs.  It also turns out that &lt;code&gt;Escape-[&lt;&#x2F;code&gt; is the prefix for a few keycodes, including delete, page up, and page down.  My delete key was being intercepted and invoking my keybinding.  When the associated function was successful, the rest of the delete keycode was inserted into the buffer.&lt;&#x2F;p&gt;
&lt;p&gt;Lesson learned: do not bind &lt;code&gt;M-[&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>TAGS for Haskell</title>
          <pubDate>Sun, 08 Feb 2015 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2015-02-08-tags-for-haskell/</link>
          <guid>https://ravit.ch/blog/2015-02-08-tags-for-haskell/</guid>
          <description>&lt;p&gt;I periodically try to use TAGS to navigate round my code in emacs.
When it works, it is very convenient.  I have not been using them
lately, partly because generating the tags and keeping them up-to-date
has always been a bit fiddly.  In the past, I have tried to get emacs
to automatically regenerate my tags when I save changes to a file.  I
have had solutions that work to a greater or lesser extent, but they
are always a bit unsatisfying.  Inevitably, I end up having to write
an extra shell script to generate the tags.  Usually, I need a script
because I often work in projects that span more than one repository or
cabal project.  The simple solutions I have seen for tag management
all assume that all of your code lives under one project root.&lt;&#x2F;p&gt;
&lt;p&gt;This time, given that I will probably require some kind of shell
script anyway, I decided to try a more UNIX-y approach.  My tag
generation script now uses &lt;code&gt;inotifywait&lt;&#x2F;code&gt; (from
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rvoicilas&#x2F;inotify-tools&#x2F;wiki&quot;&gt;inotify-tools&lt;&#x2F;a&gt;) to
watch for filesystem changes and rebuild my tags when necessary:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DIRS&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;*&#x2F;src&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;EVENTS&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;-e close_write -e move&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;inotifywait &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;EVENTS&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt; -mr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DIRS&lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;| while read&lt;&#x2F;span&gt;&lt;span&gt; event &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;; do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;hasktags&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt; -e -o&lt;&#x2F;span&gt;&lt;span&gt; TAGS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;DIRS&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;done;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This says: whenever a file under &lt;code&gt;src&lt;&#x2F;code&gt; in my repository is closed
(after some writes) or moved, run
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;MarcWeber&#x2F;hasktags&quot;&gt;hasktags&lt;&#x2F;a&gt; to rebuild the TAGS
file.  So far, it works remarkably well and emacs does not need to
know anything about the process.  &lt;code&gt;inotifywait&lt;&#x2F;code&gt; only works on Linux; I
am still looking for a corresponding utility for OS X.&lt;&#x2F;p&gt;
&lt;p&gt;While this approach produces a nice TAGS file, I still have a small
problem with the &lt;code&gt;find-tag&lt;&#x2F;code&gt; function in emacs.  This function lets you
jump to the tag for the identifier under the cursor.  The default
function does not know anything about the qualified module imports
typical of Haskell code, and includes the module qualifier in the
suggested tag name.  This does not work, because the TAGS file does
not record the qualifier (and it cannot do so, since you can choose a
different qualifier each time you import the module).  You can always
type in whatever name you want when using &lt;code&gt;find-tag&lt;&#x2F;code&gt;, but having the
default chosen by &lt;code&gt;find-tag&lt;&#x2F;code&gt; would be ideal (and faster).  To get
around this, I stole and adapted a bit of code from clojure-mode that
addresses this very same problem:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;lisp&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-lisp &quot;&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;defun &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;find-tag-without-module &lt;&#x2F;span&gt;&lt;span&gt;(next-p)
&lt;&#x2F;span&gt;&lt;span&gt;  (interactive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;P&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt;-tag (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;last &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;string &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;symbol-name &lt;&#x2F;span&gt;&lt;span&gt;(symbol-at-point)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)))
&lt;&#x2F;span&gt;&lt;span&gt;            next-p))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eval&lt;&#x2F;span&gt;&lt;span&gt;-after-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;load &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;haskell-mode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;#39;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;progn
&lt;&#x2F;span&gt;&lt;span&gt;     (define-key haskell-mode-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map &lt;&#x2F;span&gt;&lt;span&gt;(kbd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;M-.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;) &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt;-tag-without-module)))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So far, everything is working well.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Arrays in Haskell</title>
          <pubDate>Sat, 24 Jan 2015 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2015-01-24-arrays-in-haskell/</link>
          <guid>https://ravit.ch/blog/2015-01-24-arrays-in-haskell/</guid>
          <description>&lt;p&gt;Relative to many other languages, the humble array is underrepresented
in Haskell.  That may follow from the difficulty of fitting arrays
into pure code; an entire array to update a single element rarely
makes sense.  There are some immutable data structures that
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;persistent-vector&quot;&gt;look like arrays&lt;&#x2F;a&gt;,
but are not actually.  Mutable arrays (in &lt;code&gt;ST&lt;&#x2F;code&gt; or &lt;code&gt;IO&lt;&#x2F;code&gt;) make more
sense, so I want to write up a few notes about them.&lt;&#x2F;p&gt;
&lt;p&gt;Haskell has two general purpose array libraries:
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;array&quot;&gt;array&lt;&#x2F;a&gt; and
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;vector&quot;&gt;vector&lt;&#x2F;a&gt;.  There are also
some special-purpose libraries like
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;repa&quot;&gt;repa&lt;&#x2F;a&gt;, which enables
efficient (and implicitly parallel) numeric computations.  The vector
package provides a flexible and efficient array abstraction, with
support for fusing many operations.  In contrast, the array package
has a reputation as archaic and slightly odd; certainly, the API
specified in the Haskell report is strange when compared with the API
provided by vector, which more closely mirrors the APIs provided by
containers and the other data structure libraries in the Haskell
ecosystem.  That is a fair criticism.  However, if you do not need the
features in vector, array is still worth a look.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;why-array&quot;&gt;Why Array&lt;&#x2F;h1&gt;
&lt;p&gt;If you just need contiguous (and possibly &lt;em&gt;unboxed&lt;&#x2F;em&gt;) storage of values
with &lt;code&gt;Int&lt;&#x2F;code&gt; (or &lt;code&gt;Int&lt;&#x2F;code&gt;-like) keys with support for O(1) reads and
writes, array is still a very good choice.  For this use case, array
even has a significant advantage over vector: the index into the array
can be any type implementing the &lt;code&gt;Ix&lt;&#x2F;code&gt; typeclass (for “indexable”).
This means that you can define extra types (perhaps as a &lt;code&gt;newtype&lt;&#x2F;code&gt;
around &lt;code&gt;Int&lt;&#x2F;code&gt;) as array indexes.  I have found this to be immensely
useful, as it prevents me from accidentally indexing an array with the
wrong &lt;code&gt;Int&lt;&#x2F;code&gt; that I had lying around.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;unboxed-data&quot;&gt;Unboxed data&lt;&#x2F;h1&gt;
&lt;p&gt;Safe indexing and O(1) updates are undeniably useful.  However, one of
the real strengths of arrays (and vectors) is that they support
&lt;em&gt;unboxed&lt;&#x2F;em&gt; data.  In a boxed array, elements are stored via pointer.
In an unboxed array, the elements are stored directly in the allocated
array.  This has a few nice implications:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;it removes a layer of pointer indirection,&lt;&#x2F;li&gt;
&lt;li&gt;the elements are all stored contiguously, and&lt;&#x2F;li&gt;
&lt;li&gt;the garbage collector does not need to scan the elements.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In short, if you can store your data in an unboxed format, it is
usually worth it.  There is one restriction: you cannot store
unevaluated thunks in an unboxed container.  Since that extra layer of
indirection has been removed, all of the elements are always fully
evaluated.  In many cases, this is actually also a good thing.&lt;&#x2F;p&gt;
&lt;p&gt;Both array and vector have a default set of instances allowing them to
store most of the unboxable types in base.  But what if we want to
store our own types in unboxed arrays?  The answer turns out to be a
little complicated.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;unboxed-mutable-vectors&quot;&gt;Unboxed mutable vectors&lt;&#x2F;h2&gt;
&lt;p&gt;The vector package has a few classes that need to
be implemented for your type &lt;code&gt;T&lt;&#x2F;code&gt; (in the case of mutable vectors):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Data.Vector.Generic.Mutable.MVector Data.Vector.Unboxed.Mutable.MVector T&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Unbox T&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It really is not obvious how to implement them by hand.  You need to
root around in the implementation of vector to figure it out.
Luckily, these instances can be derived with
&lt;code&gt;GeneralizedNewtypeDeriving&lt;&#x2F;code&gt;.  Or, they could, until ghc 7.8 made
&lt;code&gt;GeneralizedNewtypeDeriving&lt;&#x2F;code&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;ghc.haskell.org&#x2F;trac&#x2F;ghc&#x2F;ticket&#x2F;9112&quot;&gt;safe&lt;&#x2F;a&gt;.  If you are
willing to use Template Haskell, the
&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;vector-th-unbox&quot;&gt;vector-th-unbox&lt;&#x2F;a&gt;
package can generate the necessary instances.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;unboxed-mutable-arrays&quot;&gt;Unboxed mutable arrays&lt;&#x2F;h2&gt;
&lt;p&gt;The story for array is not much simpler.  I could not find much
documentation on this issue, so hopefully this post will shed some
light on these darker corners of the library ecosystem.  In order to
store a type &lt;code&gt;T&lt;&#x2F;code&gt; in an unboxed mutable array, you only need one
instance (at least for the &lt;code&gt;IO&lt;&#x2F;code&gt; variant):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MArray IOUArray T IO&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is a bit problematic.  First, it does not really fit the form we
would expect for a (&lt;code&gt;GeneralizedNewtypeDeriving&lt;&#x2F;code&gt;) deriving clause.
Normally, we would expect the &lt;code&gt;T&lt;&#x2F;code&gt; to come last, but &lt;code&gt;MArray&lt;&#x2F;code&gt; is just
not defined that way.  That is okay – we can work around that with
&lt;code&gt;StandaloneDeriving&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; GeneralizedNewtypeDeriving #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; MultiParamTypeClasses #-}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;LANGUAGE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; StandaloneDeriving #-}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GHC.IO &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;IO&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;(..)&lt;&#x2F;span&gt;&lt;span&gt; )
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Array.IO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IOA
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;T &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;T Int
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;deriving instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MArray IOA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;IOUArray T IO
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have to import the constructor for &lt;code&gt;IO&lt;&#x2F;code&gt; these days, or GHC
complains.  It turns out that GHC still complains:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;color:#657b83;&quot;&gt;&lt;code&gt;&lt;span&gt;    Can&amp;#39;t make a derived instance of ‘IOA.MArray IOA.IOUArray T IO’
&lt;&#x2F;span&gt;&lt;span&gt;      (even with cunning newtype deriving):
&lt;&#x2F;span&gt;&lt;span&gt;      cannot eta-reduce the representation type enough
&lt;&#x2F;span&gt;&lt;span&gt;    In the stand-alone deriving instance for
&lt;&#x2F;span&gt;&lt;span&gt;      ‘IOA.MArray IOA.IOUArray T IO’
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I confess that I do not really know what that means, aside from it
just not working.  Perhaps GHC does not know what parameter it is
supposed to be deriving for.  In any case, that leaves us with one
option: we get to write the instance ourselves.  Doing so is
unpleasant, but at least it is fairly obvious from the existing
instances defined in array.  For our type &lt;code&gt;T&lt;&#x2F;code&gt;, which is a &lt;code&gt;newtype&lt;&#x2F;code&gt;
around &lt;code&gt;Int&lt;&#x2F;code&gt;, the instance looks like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GHC.Base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;B
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;GHC.ST &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;(..)&lt;&#x2F;span&gt;&lt;span&gt; )
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Control.Monad.ST &lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;stToIO&lt;&#x2F;span&gt;&lt;span&gt; )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Array.IO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IOA
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Array.IO.Internals &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;IOA
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Array.MArray &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;MA
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Data.Array.Base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;BA
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MArray&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;STUArray s&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ST s&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; getBounds #-}
&lt;&#x2F;span&gt;&lt;span&gt;  getBounds (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;STUArray&lt;&#x2F;span&gt;&lt;span&gt; l u _ _) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; return (l, u)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; getNumElements #-}
&lt;&#x2F;span&gt;&lt;span&gt;  getNumElements (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;STUArray&lt;&#x2F;span&gt;&lt;span&gt; _ _ n _) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; return n
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; unsafeRead #-}
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeRead (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;STUArray&lt;&#x2F;span&gt;&lt;span&gt; _ _ _ marr#) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;# i#) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ST &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ \&lt;&#x2F;span&gt;&lt;span&gt;s1# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;readIntArray# marr# i# s1# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;      (# s2#, e# #) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (# s2#, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;# e#) #)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; unsafeWrite #-}
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeWrite (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;STUArray&lt;&#x2F;span&gt;&lt;span&gt; _ _ _ marr#) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;# i#) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;# e#)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;ST &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ \&lt;&#x2F;span&gt;&lt;span&gt;s1# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;writeIntArray# marr# i# e# s1# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;      s2# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (# s2#, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt; #)
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeNewArray_ (l, u) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unsafeNewArraySTUArray_ (l, u) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;wORD_SCALE
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;MArray IOA&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;IOUArray T IO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; getBounds #-}
&lt;&#x2F;span&gt;&lt;span&gt;  getBounds (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOUArray&lt;&#x2F;span&gt;&lt;span&gt; arr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; stToIO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;getBounds arr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; getNumElements #-}
&lt;&#x2F;span&gt;&lt;span&gt;  getNumElements (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOUArray&lt;&#x2F;span&gt;&lt;span&gt; arr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; stToIO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;getNumElements arr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; unsafeRead #-}
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeRead (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOUArray&lt;&#x2F;span&gt;&lt;span&gt; arr) i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; stToIO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unsafeRead arr i
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;{-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;INLINE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt; unsafeWrite #-}
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeWrite (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOUArray&lt;&#x2F;span&gt;&lt;span&gt; arr) i e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; stToIO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unsafeWrite arr i e
&lt;&#x2F;span&gt;&lt;span&gt;  unsafeNewArray_ bounds &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; stToIO (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;IOUArray &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;lt;$&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;BA&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unsafeNewArray_ bounds)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Entirely unpleasant, but worth the effort.  There are a few important
things to note about these instances:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You need to replace calls to &lt;code&gt;readIntArray&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;writeIntArray&lt;&#x2F;code&gt; and the
&lt;code&gt;I#&lt;&#x2F;code&gt; constructors with variants appropriate for your underlying
type.  If you have a type that is not a &lt;code&gt;newtype&lt;&#x2F;code&gt; around some
existing type, you will need to write your own variants.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;wORD_SCALE&lt;&#x2F;code&gt; function is defined in Data.Array.Base, along with
a few others.  You also need to choose the appropriate scale (which
is basically the stride in the array) for your type.  Again, you may
need to define your own for custom types.&lt;&#x2F;li&gt;
&lt;li&gt;While it may look like a function you would never call, you still
need to implement &lt;code&gt;unsafeNewArray_&lt;&#x2F;code&gt;.  The default implementation of
&lt;code&gt;newArray&lt;&#x2F;code&gt;, which you probably do not want to implement, calls it.
The default implementation of &lt;code&gt;unsafeNewArray_&lt;&#x2F;code&gt; fills the array
elements with a call to error.  This is fine for boxed arrays, but
immediately bottoms out when it is evaluated to be put into an
unboxed array.  The call to &lt;code&gt;unsafeNewArraySTUArray_&lt;&#x2F;code&gt; just allocates
some memory and leaves its contents undefined in the traditional C
sense.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The substantive definition is for &lt;code&gt;ST&lt;&#x2F;code&gt;, with a trivial wrapper lifting
it into &lt;code&gt;IO&lt;&#x2F;code&gt;.  You probably want to keep all of the &lt;code&gt;INLINE&lt;&#x2F;code&gt; pragmas,
as most of this code will get compiled away nicely.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;Unboxed data can be a bit of a pain to work with in Haskell
(especially with the recent changes to &lt;code&gt;GeneralizedNewtypeDeriving&lt;&#x2F;code&gt;),
but it is often worth it when performance is important.  I have tried
to document the steps required to unbox custom types with the array
library.  I also argue that array is still a useful library, despite
its quirky API.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Using travis-ci</title>
          <pubDate>Tue, 30 Dec 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-12-30-using-travis-ci/</link>
          <guid>https://ravit.ch/blog/2014-12-30-using-travis-ci/</guid>
          <description>&lt;p&gt;For quite some time, I have been meaning to set up &lt;a href=&quot;https:&#x2F;&#x2F;travis-ci.org&#x2F;&quot;&gt;travis-ci&lt;&#x2F;a&gt; for some of my github projects.  To get the ball rolling, I chose my projects with the simplest build systems and dependencies.  As promised, it was very much a painless experience.  I added my trivial &lt;code&gt;.travis.yml&lt;&#x2F;code&gt; files specifying Haskell as my language and asking for a few versions of ghc; everything else was automatic.  I had to make a few tweaks here and there to update some old test suites that I had not run in a while.  That alone was worth the time.&lt;&#x2F;p&gt;
&lt;p&gt;One project was (is) more interesting.  I have a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;haggle&quot;&gt;highly experimental graph library&lt;&#x2F;a&gt; that I have been mulling over for a while.  It currently has very limited tests and is more in the design stage.  I set it up to build on travis and, oddly enough, it built fine on ghc 7.6, but failed on 7.4 and 7.8.  This was surprising.  I had developed the library on ghc 7.6, but I did not use any particularly advanced compiler features.  It is a bit embarrassing, but I think it only compiled when I wrote it due to a bug in the ghc 7.6 type checker (possibly related to partially-applied type synonyms).  I will have to shuffle things around a bit to make it work properly.  This easy multiple compiler version testing is probably the killer feature of travis-ci for me – I usually only develop on a single ghc version and did not catch this problem at all.&lt;&#x2F;p&gt;
&lt;p&gt;Annoyingly, I have a test suite that runs fine locally but fails on the travis nodes.  I am sure I will work it out eventually.  I also have to sit down and make some more sophisticated &lt;code&gt;.travis.yml&lt;&#x2F;code&gt; files for my projects with non-trivial dependencies.  Of course, I also need to add test suites to a few projects.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>whole-program-llvm Improvements</title>
          <pubDate>Mon, 29 Dec 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-12-29-whole-program-llvm-improvements/</link>
          <guid>https://ravit.ch/blog/2014-12-29-whole-program-llvm-improvements/</guid>
          <description>&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;whole-program-llvm&quot;&gt;&lt;code&gt;whole-program-llvm&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a set of scripts that I started to compile programs and libraries into single LLVM bitcode files.  The scripts stand in for a compiler (either clang or gcc) and compile each file in a program or library twice: once as the build system intended and once as LLVM bitcode.  Another script, &lt;code&gt;extract-bc&lt;&#x2F;code&gt; can then be run over the final binary to link the LLVM bitcode files into a single large bitcode file.  This is very useful for performing whole program analysis (hence the name of the scripts).&lt;&#x2F;p&gt;
&lt;p&gt;Recently, contributions by Ian Mason and Bruno Dutertre made &lt;code&gt;whole-program-llvm&lt;&#x2F;code&gt; work on OS X.  This has been requested by users many times, and I just never had the time to figure out the details.  &lt;code&gt;whole-program-llvm&lt;&#x2F;code&gt; uses some details of ELF executables on Linux to do its work.  In particular, it stores generated bitcode files inside of ELF object files as a special section during compilation.  It takes this slightly odd approach to be robust against strange build systems.  Some build systems move intermediate object files around and delete temporary directories, among other seemingly odd things.  Keeping the bitcode files attached to actual build artifacts (i.e., object files) means that we will end up with all of the bitcode relevant to the executable being built.  When all of the object files are linked together, the system linker combines all of the ELF sections of the same name from each input object file.  This lets us pull out the contents of a single ELF section from the final binary and just link everything together.  Unfortunately, relying on ELF details does not work on OS X, which uses Mach-O binaries instead.  Ian and Bruno added code paths to the scripts to use the equivalent Mach-O tools on OS X (mostly fancy invocations of &lt;code&gt;otool&lt;&#x2F;code&gt;, I believe).&lt;&#x2F;p&gt;
&lt;p&gt;Their contributions also made the scripts robust enough to be able to compile the entire FreeBSD base system and kernel.  If you ever wanted to run a static analysis over an entire OS, this is your chance.  Robustness is an issue with these scripts because they must intercept compiler arguments and tweak them a bit, but, more importantly, &lt;em&gt;recognize&lt;&#x2F;em&gt; a few special arguments.  Part of this is recognizing input file names to know what file to generate bitcode from.  To do that, the scripts need to know which command line flags take arguments that need to be ignored.  Unfortunately, gcc-compatible compilers (like clang) support a great many flags.  Not all of these flags are documented.  Furthermore, these compilers are somewhat robust against nonsensical combinations of flags.  While that is useful for users, it makes writing a compatible driver program difficult, since the documentation does not tell the whole story about what flags are allowed together and their precise meaning.  This comes up frequently with autotools and libtool-based build systems, which seem to throw together random combinations of compiler flags until they get working binary.  Ian and Bruno taught &lt;code&gt;whole-program-llvm&lt;&#x2F;code&gt; about enough of the compiler flags found in the wild to compile all of FreeBSD base.&lt;&#x2F;p&gt;
&lt;p&gt;I hope people continue to find these scripts useful – now on OS X!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Down with Type 3 Fonts</title>
          <pubDate>Tue, 25 Nov 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-11-25-down-with-type3-fonts/</link>
          <guid>https://ravit.ch/blog/2014-11-25-down-with-type3-fonts/</guid>
          <description>&lt;p&gt;I recently found myself needing to excise all of the type 3 fonts from
a TeX document.  I believe that type 3 fonts are actually just
embedded postscript, which is less than ideal for a nice PDF.
Obviously, I would never intentionally include a type 3 font, so some
other tool in my toolchain must have been adding them.  Eventually, I
figured out that they were sneaking in through some matplotlib graphs.
I found a nice &lt;a href=&quot;http:&#x2F;&#x2F;www.phyletica.com&#x2F;?p=308&quot;&gt;description&lt;&#x2F;a&gt; of the
problem.  The solution I chose was to tell matplotlib to use
TeX-compatible output:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#fdf6e3;color:#657b83;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;matplotlib
&lt;&#x2F;span&gt;&lt;span&gt;matplotlib.rcParams&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;text.usetex&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This fixed my output: all of my graphs now have proper type 1 fonts.
This particular solution had one interesting side effect: all of the
labels in the graphs were interpreted by TeX.  This meant that some
characters had to be escaped and that I was able to use some nice TeX
amenities in labels.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Linux Video Drivers</title>
          <pubDate>Sun, 21 Sep 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-09-21-linux-video-drivers/</link>
          <guid>https://ravit.ch/blog/2014-09-21-linux-video-drivers/</guid>
          <description>&lt;p&gt;Today I ended up reading one of many articles decrying the current state of OpenGL.  I don&#x27;t remember which one.  I will be interested to see where OpenGL does from here given its competition.  The whole topic got me curious to see what version of OpenGL my video drivers actually supported, so of course I checked with &lt;code&gt;glxinfo&lt;&#x2F;code&gt;.  Much to my surprise, I was running the open source &lt;a href=&quot;http:&#x2F;&#x2F;nouveau.freedesktop.org&#x2F;wiki&#x2F;&quot;&gt;nouveau&lt;&#x2F;a&gt; driver.  I never thought about it while I was installing Ubuntu, and I guess this was the default.&lt;&#x2F;p&gt;
&lt;p&gt;I have to say that I am very impressed by how far nouveau has come.  The last time I read about it, it barely worked.  I have been using it for a few weeks now and I had not noticed a single problem.  I even fired up Steam to test things out and a random sample of one game (Skulls of the Shogun) worked with no problems.  I have no reason right now to switch to the official closed-source driver, so I will run with nouveau until a problem pops up.  Good work, whoever has been working on it!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Releasing datalog-0.2.0.2</title>
          <pubDate>Thu, 18 Sep 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-09-18-datalog-release/</link>
          <guid>https://ravit.ch/blog/2014-09-18-datalog-release/</guid>
          <description>&lt;p&gt;I just released version 0.2.0.2 of my datalog library to
&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;datalog-0.2.0.2&quot;&gt;hackage&lt;&#x2F;a&gt;.
Datalog is a declarative query language similar in spirit to Prolog,
though less expressive.  If you have a problem that can be expressed
as
&lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Transitive_closure&quot;&gt;dynamic transitive closure&lt;&#x2F;a&gt;,
Datalog is one way to express your problem.  I believe Datalog is
equivalent to SQL with recursive queries.  My implementation is
currently fairly basic; I hope to add some more sophistication in the
near future.  The primary feature of my implementation is that it is
usable as a library: you can add facts to a database and issue queries
through just a few simple library calls.  The facts in the database
can be any Haskell type that is hashable and comparable for equality
-- basically anything you could reasonably want to query.&lt;&#x2F;p&gt;
&lt;p&gt;This latest release has one new feature and an important bug fix.  The
feature is a new REPL implementation using
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;haskeline&quot;&gt;haskeline&lt;&#x2F;a&gt;, a very
nice implementation of readline.  The REPL is not ideal as a
high-performance interface to the solver, but it is useful for
exploratory query design.&lt;&#x2F;p&gt;
&lt;p&gt;The bug fixed in this release caused the solver to return some
incorrect answers.  Specifically, it would return the empty set when
there were really some answers.  The root cause of the problem was a
bug in how queries with negation were handled.  In Datalog programs
with no negations in any rules, a very simple evaluation strategy
works well.  When a Datalog program includes negated clauses,
evaluation order begins to matter a great deal.  The simplest way to
deal with negation is to
&lt;a href=&quot;http:&#x2F;&#x2F;www.cs.uni-paderborn.de&#x2F;fileadmin&#x2F;Informatik&#x2F;AG-Boettcher&#x2F;Lehre&#x2F;WS_07_08&#x2F;dbis1&#x2F;stanford&#x2F;dbis1k4-ddb-JU-datalog_stratified-negation.pdf&quot;&gt;stratify&lt;&#x2F;a&gt;
the rules in the Datalog program.  This stratifies, or partitions, the
Datalog program into strongly-connected components (based on
dependencies between relations) and evaluates the strata (partitions)
bottom-up.  Embarrassingly enough, I was evaluating the strata
top-down before this bug fix release.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;edit&quot;&gt;Edit&lt;&#x2F;h2&gt;
&lt;p&gt;I misstated the exact nature of the bug fixed in the latest release.
Strata were not being evaluated in the wrong order.  Instead, stratum
&lt;em&gt;numbers&lt;&#x2F;em&gt; were being assigned in the wrong order.  Most of the time,
the numbers were correct and thus rules were evaluated in the correct
order.  However, in some cases, rules in the highest stratum would
actually be assigned to the lowest.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Taffybar 0.4.3</title>
          <pubDate>Fri, 12 Sep 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-09-12-taffybar-0-4-3/</link>
          <guid>https://ravit.ch/blog/2014-09-12-taffybar-0-4-3/</guid>
          <description>&lt;p&gt;Recently, I cut a long overdue release of Taffybar, my desktop bar for xmonad (or any window manager, really).  The changes are in the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;taffybar&#x2F;blob&#x2F;v0.4.3&#x2F;CHANGELOG.md&quot;&gt;changelog&lt;&#x2F;a&gt;.  A few highlights are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Better support for resizing screens&lt;&#x2F;li&gt;
&lt;li&gt;A few new widgets&lt;&#x2F;li&gt;
&lt;li&gt;A completely new pager built on EWMH&lt;&#x2F;li&gt;
&lt;li&gt;Vastly improved power usage&lt;&#x2F;li&gt;
&lt;li&gt;Fixed an annoying bug where time zone changes are not picked up&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;More recent fixes deal with breakage from the network&#x2F;network-uri split.  Now on a Hackage near you.&lt;&#x2F;p&gt;
&lt;p&gt;I have no specific plans for interesting new development at this point, but I am happy to take suggestions (or, better yet, patches).  I have been mulling over some ideas for a better way to present freedesktop notifications for a while, but I do not have a concrete plan yet.  The biggest outstanding bug I know of relates to handling of xrandr events.  Right now, removing a monitor with a taffybar on it makes you lose your bar, which is fairly annoying.  I need to figure out what my options are on that front.  I am sure that there are some xrandr events that one can listen for.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A Change of OS</title>
          <pubDate>Thu, 11 Sep 2014 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2014-09-11-new-os-install/</link>
          <guid>https://ravit.ch/blog/2014-09-11-new-os-install/</guid>
          <description>&lt;p&gt;I have been running Debian unstable on my machines for years reasonably happy.  Living on the bleeding edge is slightly exciting.  Having maximally up-to-date versions of programs is also useful.  The cost of running Debian unstable is implied by the name: sometimes things break.  I have been fine with that over the years - I knew what I was getting in to.  So the occasional update messes up a symlink for glibc.  No big deal.  At least, that is how I used to feel.&lt;&#x2F;p&gt;
&lt;p&gt;I feel like I might be getting old, since I seem to be losing patience with that sort of thing.  Most recently, seemingly during the transition to systemd, a few things suddenly broke.  There has been enough churn in recent years that there are a number of aspects of modern Linux distributions that I just do not understand everything anymore and have no idea how to fix these issues.  A few things that popped up off the top of my head:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;I could not mount USB drives with my normal user account anymore&lt;&#x2F;li&gt;
&lt;li&gt;NetworkManager simply laughed at me and failed silently when I attempted to connect to VPNs&lt;&#x2F;li&gt;
&lt;li&gt;gdm would start, but would not show a login screen&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The last one was slightly annoying.  I suspect that I was supposed to be doing something special when I logged in to get some magical new session permission system working, perhaps courtesy of systemd.  I have no idea, and searching for answers to these problems is difficult.  I should say, searching for these issues is very easy, but all of the results are for the era around 2006-2008 when these issues were a daily occurrence.  At this point, I decided that I did not really have the patience to figure out what was wrong.  I took the easy way out.&lt;&#x2F;p&gt;
&lt;p&gt;Ubuntu seems to have a lot of momentum these days, and it does have a reputation for just working.  That is, Just Working.  I figured that I would give it a shot.  My system could not get more broken, right?  I grabbed a Kubuntu image and let the installer do its thing.  The first thing I noticed was that the installer was very pleasant to use compared to the curses-based Debian installer.  In the grand scheme of things, that is not important at all.  While you are installing, though, it is a nice touch that you notice (at least if you are coming from a less well-polished distribution).  The installation went off without a hitch and I logged in.  I have to say - everything does Just Work, as promised.  I set up a custom X session and used my normal &lt;code&gt;.xsession&lt;&#x2F;code&gt; file to start xmonad.  Deep down in the system, some magic seems to happen with some basic KDE infrastructure tied into the PAM login modules.  I am not complaining - this is exactly the stuff I never want to think about again.&lt;&#x2F;p&gt;
&lt;p&gt;Updates will be less frequent, but that is for the best.  A normal distribution release cycle will protect me from myself.  Part of my dislike of normal release cycles stems from too much time spent with certain enterprise Linux distributions that only provide zombified versions of important programs (e.g., compiler toolchains).  Compiling your own version of GTK+ so that you can run Chrome is several kinds of not very much fun.  Ubuntu has much more reasonable versions of everything I need.  For the most part, libraries seem to be very stable and applications are new enough.  The shorter release cycle, as compared to an enterprise distribution, will also help.  It also seems that a new Ubuntu release is imminent, so we will see shortly how the upgrade process goes.  I have not tried a distribution upgrade since the Mandrake days - I have to assume it is much more pleasant now.  In any case, it probably has to be better than accidentally losing glibc to sid.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>(Programming) Language Interoperability</title>
          <pubDate>Fri, 27 Dec 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2013-12-27-language-interoperability/</link>
          <guid>https://ravit.ch/blog/2013-12-27-language-interoperability/</guid>
          <description>&lt;p&gt;My advisor recently pointed me to
&lt;a href=&quot;http:&#x2F;&#x2F;queue.acm.org&#x2F;detail.cfm?id=2543971&quot;&gt;an article&lt;&#x2F;a&gt; in the ACM
Queue about a familiar topic: interoperability between programming
languages.  My research for my thesis was very much in this area, and
I am happy to see others thinking about the topic.  I have not been
able to find a whole lot of related work.  Broadly speaking, I agree
with the sentiment of the article and differ on a few minor points.
For example, I don&#x27;t think that mutability is a significant barrier to
language interoperability.  Certainly, I would like to see immutable
data become the norm in every language, but that is almost an
orthogonal concern.  Even in Haskell, mutable data is easily handled
within the IO monad.  Existing Haskell language interop tools can deal
with this sensibly.  Expecting every library in every language to be
designed with purely functional languages in mind is unrealistic and
not productive.  In fact, sometimes mutation is the right tool for the
job, and in some of those cases, a Haskell programmer might &lt;em&gt;want&lt;&#x2F;em&gt; to
drop down into a more low-level language (e.g., rust) to more easily
handle their mutable data.  Of course, having mutability highlighted
in the type system of a language interoperability solution would be
highly desirable.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of focusing on the article itself, I want to spend some time
musing about a point raised in some of the comments: using C as a
least-common denominator for gluing languages together.  At the
moment, this is certainly the best option for language
interoperability.  However, I think a distinction should be made in
this context between using C the language and C the calling
convention.  The value of C in this domain is not related to the
language itself.  In fact, as the article and some of the comments
point out, it is in some ways a difficult language to interoperate
with due to its memory management, simplistic data model, and the
ad-hoc nature of its error handling.  That said, the underlying C
calling convention is, if not simple, consistent for each platform.
Nearly every language knows how to issue calls with the C calling
convention.  Libraries like libffi make this easy to provide for any
language implementation.&lt;&#x2F;p&gt;
&lt;p&gt;Granted, the C calling convention is not very expressive.  It is
sufficient if you only need to pass pointers and machine-sized data
types across language boundaries.  If you require deeper integration,
and in particular data interoperability, the C calling convention
falls short for most languages.  This is not insurmountable.  Only
agreement stands in the way of adopting more expressive calling
convention.  Even better if this common calling convention is a proper
superset of the C calling convention.  If the details of the calling
convention were abstracted behind a library interface that various
language implementations could use, it could even be evolved in a
backwards compatible way to improve performance over time as more work
is done in the area.  Perhaps, as we have the &amp;quot;C&amp;quot; calling convention,
this could be the &amp;quot;X&amp;quot; calling convention.&lt;&#x2F;p&gt;
&lt;p&gt;Ahead-of-time compiled languages may need to explicitly export certain
functions under the &amp;quot;X&amp;quot; calling convention, much like a C++ function
can be declared &lt;code&gt;extern &amp;quot;C&amp;quot;&lt;&#x2F;code&gt;.  If called from within their native
language, the compiler could either use the &lt;code&gt;extern &amp;quot;X&amp;quot;&lt;&#x2F;code&gt; version of
the function directly or possibly generate both the externalized
version of the function and a &amp;quot;native&amp;quot; version of the function.
Just-in-time compiled languages with a compiler available at run-time,
like Java, could simply generate &lt;code&gt;extern &amp;quot;X&amp;quot;&lt;&#x2F;code&gt; variants of functions
on-demand.&lt;&#x2F;p&gt;
&lt;p&gt;A useful cross-language calling convention would also need to expose
some amount of introspection data so that callers can determine what
functions the library provides.  This is one area where Microsoft&#x27;s
COM works well from the caller&#x27;s perspective.  A language
implementation aware of the &amp;quot;X&amp;quot; calling convention could automatically
generate this information.  A dynamic language run-time supporting the
calling convention could allow the introspection data to be queried at
run-time.  Static languages would require a separate tool to load the
(versioned) introspection data so that static type-safe call stubs
could be created.  This is roughly the client experience provided by
gobject-introspection.  In that case, however, the library interface
descriptions are largely hand-generated.  Keeping the compiler in the
loop would simplify the process and reduce the number of errors.&lt;&#x2F;p&gt;
&lt;p&gt;I should note that it is unlikely that all features of every language
could be exposed through a common interface.  Languages have many
differences, and some differences between languages may be
incompatible.  That is fine.  Languages have differences, and we
attempt to use the language with the most helpful quirks for any
particular problem.  A language interoperability layer that simplifies
data transfer and exposes most functionality would still be a vast
improvement over the status quo.  We have this problem already with C
and C++.  A C++ function being made callable from C must obey a number
of restrictions.  This is still useful.  It would be an interesting
research question to figure out what subset of features is reasonable,
and how language-specific extension could be handled.&lt;&#x2F;p&gt;
&lt;p&gt;There are many practical considerations for an &amp;quot;X&amp;quot; calling convention.
Error handling is important for any library.  While many languages use
exceptions to report errors, others do not.  Worse, nearly no
implementations of exceptions are compatible.  Any &amp;quot;X&amp;quot; calling
convention would need to standardize error handling.  One simple way
would be to require an implementation generating an &amp;quot;X&amp;quot;-callable
function to wrap it in a catch-all handler and return information
about any exceptional conditions via a standard mechanism (e.g., an
extra return value).  Deeper integration is, of course, possible, but
a simple solution may be best.  It requires little of language
implementors and does not require significant performance sacrifices
for the common case of intra-language calls.&lt;&#x2F;p&gt;
&lt;p&gt;Another significant problem in language interoperability is resource
management.  Objects allocated by the run-time system of one language
(malloc in C, the JVM in Java) must be managed by that run-time
system.  The &amp;quot;X&amp;quot; calling convention could expose resource management
hooks.  For example, a Python program calling a Java library would
receive objects allocated by the JVM; the calling convention could
expose pointers to the appropriate functions to call when the JVM
object is no longer reachable from Python.  The object would be pinned
in the JVM until Python un-pinned it.  Then it could be garbage
collected as it would normally in a Java-only environment.&lt;&#x2F;p&gt;
&lt;p&gt;These are just a few assorted thoughts.  Clearly, there would be many
details to work out.  These ideas are complementary to my earlier
work.  Instead of performing a deep analysis to make idiomatic
bindings, the additional features of this hypothetical calling
convention allow enough information to be exposed to require a bit
less analysis.  Furthermore, the extra information in this
hypothetical calling convention would make it easier to issue
cross-language calls into languages besides C.  The C calling
convention is our current lingua franca: we can do better, and doing
better would make true polyglot programming simpler.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>PLDI 2013 Trip Report</title>
          <pubDate>Thu, 05 Dec 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2013-12-05-pldi-report/</link>
          <guid>https://ravit.ch/blog/2013-12-05-pldi-report/</guid>
          <description>&lt;p&gt;As you can see, this post is late.  Very late.  PLDI was in June.  At
least I posted this in the right year.  I had some trip notes but never got around to turning them into a post.&lt;&#x2F;p&gt;
&lt;p&gt;I was presenting a
&lt;a href=&quot;http:&#x2F;&#x2F;pages.cs.wisc.edu&#x2F;~travitch&#x2F;ismm-2013&#x2F;&quot;&gt;paper&lt;&#x2F;a&gt; about a static
analysis to infer memory ownership properties for C libraries at ISMM
this year, co-located with PLDI.  Overall, I enjoyed the conference.
Presenting is always fun, as is putting faces to all of the names you
read about.  I just want to cover a few papers that stood out for me
(in roughly chronological order):&lt;&#x2F;p&gt;
&lt;p&gt;One of my favorite papers was presented early: &lt;a href=&quot;http:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?id=2462165&quot;&gt;Scalable Variable and
Data Type Detection in a Binary Rewriter&lt;&#x2F;a&gt;.  This work takes
(stripped) x86 binaries and converts them into the LLVM IR with
reasonable precision.  As the title hints, they recover many complex
types, which is both impressive and necessary for many tools.  My own
work is on the LLVM IR and having a tool like this would be very
useful to let me apply my work to a wider range of inputs.  Not having
perfect accuracy would probably be fine for many of my purposes, so
this was an especially exciting piece of work for me.&lt;&#x2F;p&gt;
&lt;p&gt;Being mostly a software person, I learned a bit about different types of hardware, specifically &lt;a href=&quot;http:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?doid=2462156.2462171&quot;&gt;memory that deteriorates&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;On the last day of PLDI, I was actually torn between the two
post-lunch sessions.  I actually wanted to see all of the
presentations from both sessions (one session was about Monads and
FRP, while the other was about alias analysis).  Being a mostly
functional programmer, I decided to go to the Monads and FRP session.
I couldn&#x27;t digest the whole thing during the presentation, but &lt;a href=&quot;http:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?id=2491979&quot;&gt;Monadic
Abstract Interpreters&lt;&#x2F;a&gt;
seemed very elegant.  I&#x27;d like to spend some more time on that paper
soon, actually.  That said, I was more immediately excited about
Asynchronous Functional Reactive Programming for GUIs, otherwise known
as &lt;a href=&quot;http:&#x2F;&#x2F;elm-lang.org&#x2F;&quot;&gt;Elm&lt;&#x2F;a&gt;.  I&#x27;ve been vaguely familiar with FRP
for a while, but this was a good presentation introducing it and
actually demonstrating how to use it.  I find that most of the
discussions of FRP assume you are already an expert, rendering them
somewhat unapproachable.  I&#x27;m not directly interested in Elm itself,
but I would very much like to try out an FRP library or two in
Haskell.  A side project I am working on now should afford me the
opportunity soon.&lt;&#x2F;p&gt;
&lt;p&gt;On the ISMM.  I am not really an expert on memory management, but I am
interested in the topic, obviously.  I found myself nodding along with
the keynote, which basically argued that sequential consistency (i.e.,
what most programmers who are not computer architects or compiler
writers expect) does not need to be prohibitively expensive.  Maybe we
will actually see sequential consistency in the wild one day.&lt;&#x2F;p&gt;
&lt;p&gt;My favorite paper in ISMM (besides mine, of course) was Control Theory
for Principled Heap Sizing.  I know nothing about control theory and I
have never implemented a garbage collector myself (though, like
everyone, I have all sorts of opinions about what makes a good garbage
collector).  I am not really qualified to evaluate the approach or
results in the paper, but I really liked the connection between a
well-known problem in Computer Science and a field of mathematics that
I have never really seen before.  It makes a lot of sense to look to
other fields for inspiration, especially since the programming
languages and compiler communities do not often focus on this type of
modeling.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Blog Software Update</title>
          <pubDate>Mon, 02 Sep 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2013-09-02-blog-software-update/</link>
          <guid>https://ravit.ch/blog/2013-09-02-blog-software-update/</guid>
          <description>&lt;p&gt;This blog is built on &lt;a href=&quot;http:&#x2F;&#x2F;jaspervdj.be&#x2F;hakyll&#x2F;&quot;&gt;hakyll&lt;&#x2F;a&gt;, a static
site generator written in Haskell.  I have been running on Hakyll 3.x
for quite some time now, but Hakyll 4 was released in January.  I kept
telling myself that I would update - I even had the port mostly
working a few months ago.  I never got around to finishing it, though.
Until today, that is.  There should be no visible changes if I did
everything right, but the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;travitch&#x2F;blog&quot;&gt;code&lt;&#x2F;a&gt; is
much cleaner.  It was essentially a rewrite, so it was a good
opportunity to make some style fixes.  I had to cheat and work off of
another
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nickcharlton&#x2F;nickcharlton.net&#x2F;blob&#x2F;master&#x2F;site.hs&quot;&gt;example&lt;&#x2F;a&gt;
of Hakyll usage, but it all worked out in the end.&lt;&#x2F;p&gt;
&lt;p&gt;The biggest change from Hakyll 3 to Hakyll 4 was a transition from an
&lt;a href=&quot;http:&#x2F;&#x2F;www.haskell.org&#x2F;arrows&#x2F;&quot;&gt;arrow&lt;&#x2F;a&gt;-based API to a monadic API.
Hakyll was my first exposure to arrows, and I use some small parts of
the arrow API in my own code.  I think the monadic API is a bit
simpler, but have no strong opinions.  It does require fewer odd
combinators in day-to-day use.  I like the concept of arrows just
fine, but I haven&#x27;t used them enough to always remember what each
combinator does off-hand.  I think the biggest change that affected me
is that Hakyll 4.1 uses pandoc 1.10, which just makes package
maintenance easier (most other things are updating to the newer
pandoc, too).  There also seem to be some fancy new features in the
template system in Hakyll 4.3 that I am not yet using.  I might try
out the new pagination and teaser functionality.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A Student No More</title>
          <pubDate>Thu, 29 Aug 2013 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://ravit.ch/blog/2013-08-29-a-student-no-more/</link>
          <guid>https://ravit.ch/blog/2013-08-29-a-student-no-more/</guid>
          <description>&lt;p&gt;After 22 (or was it 23?) years of formal education, I am no longer a
student.  I defended my dissertation on August 9 and deposited it on
August 20.  I will post a copy of my dissertation online shortly.&lt;&#x2F;p&gt;
&lt;p&gt;It feels good to finally be done.&lt;&#x2F;p&gt;
&lt;p&gt;I start work at &lt;a href=&quot;http:&#x2F;&#x2F;corp.galois.com&#x2F;&quot;&gt;Galois&lt;&#x2F;a&gt; shortly.  Until
then, I am just learning how to get around Portland and hopefully
riding some bicycles.  I am also going to spend some time catching up
on some of my open source projects.  There are several outstanding
bugs that I ignored while I was finishing my dissertation.  I might
even make some releases of some software while I am at it.&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
