<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>strongly-typed-thoughts.net blog</title><link>https://strongly-typed-thoughts.net/blog</link><description></description><lastBuildDate>Tue, 23 Sep 2025 18:00:00 GMT</lastBuildDate><item><title>Luminance – what was that alignment stuff already?</title><link>https://strongly-typed-thoughts.net/blog/luminance_32bit</link><description><![CDATA[<p>Yesterday, I released a new article about how I implement vertex arrays in
luminance. In that article, I told you that the memory was packed with
alignment set to <strong>1</strong>.</p>
<p>Well, I’ve changed my mind. Some people pointed out that the good thing to do
for most GPU is to align on 32-bit. That is, <strong>4</strong> bytes. The alignment should
be <strong>4</strong> bytes, then, not <strong>1</strong>.</p>
<p>There might be an issue with that. If you store a structure with attributes
which sizes are not a multiple of <strong>4</strong> bytes, it’s likely you need to add
padding.</p>
<p>However, I just reviewed my code, and found this:</p>
<pre><code class="language-haskell">instance (GPU a,KnownNat n,Storable a) =&gt; Vertex (V n a) where
instance (Vertex a,Vertex b) =&gt; Vertex (a :. b) where
</code></pre>
<p>Those are the single instances for <code>Vertex</code>. That means you can only use <code>V</code>
and <code>(:.)</code> to build up vertices. Look at the <code>V</code> instance. You’ll find a <code>GPU</code>
typeclass constraint. Let’s look at its definition and instances:</p>
<pre><code class="language-haskell">class GPU a where
  glType :: Proxy a -&gt; GLenum

instance GPU Float where
  glType _ = GL_FLOAT

instance GPU Int32 where
  glType _ = GL_INT

instance GPU Word32 where
  glType _ = GL_UNSIGNED_INT

</code></pre>
<p>Woah. How did I forget that?! Let me translate those information to you. That
means we can only have 32-bit vertex component! So the memory inside vertex
buffers will always be aligned on <strong>4</strong> bytes! No need to worry about padding
then!</p>
<p>The first implication is the fact you won’t be able to use <code>Word16</code>, for
instance. You’ll need to stick to the three types that have a <code>GPU</code> instance.</p>
<p><strong>Note</strong>: that doesn’t prevent us from adding <code>Double</code> later on, because a
<code>Double</code> is a 64-bit type, which is a multiple of <strong>4</strong> bytes!</p>
<p>That’s all I have for today. I’m working on something very exciting linked to
render batching. I’ll talk about that when it’s cooked. ;)</p>
<p>Keep the vibe; keep building awesome things, and as always, thank you for
reading me!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 11 Aug 2015 00:00:00 GMT</pubDate></item><item><title>Auto-derive uniform interfaces in luminance-0.25.5</title><link>https://strongly-typed-thoughts.net/blog/auto_derive_luminance-0.25.5</link><description><![CDATA[<p><a href="https://crates.io/crates/luminance">luminance</a> was released in <a href="https://crates.io/crates/luminance/0.25.5">version 0.25.5</a> two weeks ago and I realized I haven’t blogged about it
while it’s an important release!</p>
<blockquote>
<p>Actually, it’s from 0.25.2, but I’ll just talk about the latest patched.</p>
</blockquote>
<p>That new release received several patches:</p>
<ul>
<li>The <code>gl</code> dependencies was updated to <a href="https://crates.io/crates/gl/0.10.0">gl-0.10.0</a>.</li>
<li>A new <code>uniform_interface!</code> macro was added.</li>
</ul>
<p>I’ll introce that <code>uniform_interface!</code> macro in this blog entry.</p>
<h1>Auto derive <strike>free</strike> <em>almost free</em></h1>
<p>That macro was written to provide a smoother and better experience when writing types that will act
as <em>uniform interfaces</em> in shader. For the record, when you create a <code>Program&lt;_, _, _&gt;</code> with
<a href="https://crates.io/crates/luminance">luminance</a>, the third type variable is called the <em>uniform interface</em>. You’ll be handed back a
value of that type when your shader is being used. That will give you the possibility to send data
to the sader prior to making any rendering.</p>
<p>The main idea is to have a type like this:</p>
<pre><code>struct Common {
  resolution: Uniform&lt;[f32; 2]&gt;,
  time: Uniform&lt;f32&gt;,
  jitter: Uniform&lt;f32&gt;
}
</code></pre>
<p>If you build a program which type is <code>Program&lt;_, _, Common&gt;</code>, whenever you ask a pipeline to use
that program, you’ll be handed a <code>Common</code> object so that you can access back the <em>uniforms</em>.</p>
<p>In order for that to happen, though, there’s a constraint: your type must implement a trait. That
trait is <a href="https://docs.rs/luminance/0.25.5/luminance/shader/program/trait.UniformInterface.html"><code>UniformInterface</code></a>. There’s no magic behind that – though, it can be a bit harsh to
implement that trait. If you’re using <a href="https://crates.io/crates/luminance">luminance</a> or plan to, I strongly advise you to try an
<code>impl</code> that trait by hand first.</p>
<p>Anyway, after the fifth or sixth <em>uniform interface</em> you’ll have written, you’ll start to get bored
of writing the same code every now and then. That’s the exact same feeling as for <em>serializing</em> and
<em>deserializing</em> with <a href="https://crates.io/crates/serde">serde</a>: it’s interesting to do it by hand the first time, but it gets really,
<strong>really</strong> annoying after a while.</p>
<p>So it’d be great to be able to automatically <em>derive</em> <code>UniformInterface</code> for your own types.
There’re two solutions here:</p>
<ol>
<li>Use <a href="https://doc.rust-lang.org/book/first-edition/procedural-macros.html">procedural macros</a> so that we can write something like <code>#[derive(UniformInterface)]</code>.</li>
<li>Use standard <code>macro_rules!</code>.</li>
</ol>
<p>(1) is great because it seems <em>seamless</em> when you use it – you already use
<code>#[derive(Clone, Debug, Eq, PartialEq, Etc)]</code>. However, even though it’s doable to learn <a href="https://crates.io/crates/syn">syn</a> in
order to parse the Rust token stream, it’ll require some time and patience while I wanted something
quick to mockup the idea. For that, (2) is perfect, because regular macros are <em>easy</em> and if
correctly written, shouldn’t add too much weirdness over the type.</p>
<blockquote>
<p>The idea is to move to (1) if I find (2) to be a success and really useful.</p>
</blockquote>
<h1>The macro</h1>
<p>So… we’re talking about the <a href="https://docs.rs/luminance/0.25.5/luminance/macro.uniform_interface.html"><code>uniform_interface!</code></a> macro. This macro has already a pretty decent
documentation, so feel free to visit it, but I’ll explain more here.</p>
<p>A macro is generally used to introduce an <a href="https://wiki.haskell.org/Embedded_domain_specific_language">EDSL</a>. For instance, <a href="https://crates.io/crates/nom">nom</a>’s <a href="https://docs.rs/nom/3.2.1/nom/macro.do_parse.html"><code>do_parse</code></a> macro
uses a funny EDSL (you use operators like <code>&gt;&gt;</code> and return result with <code>(…)</code>, which is not
<em>normal</em> Rust code).</p>
<p>The <a href="https://docs.rs/luminance/0.25.5/luminance/macro.uniform_interface.html"><code>uniform_interface!</code></a> macro uses an EDSL that you know very well: it’s plain Rust code! Hot
news: you’ll have nothing more to learn!</p>
<blockquote>
<p>Note: it’s not really <em>any</em> Rust code, since you can only define <code>struct</code>s. Then, see that as a
subset of Rust.</p>
</blockquote>
<p>The syntax is very simple. Let’s take back our sample from above with the <code>Common</code> interface but now
let’s the macro do all the magic for us!</p>
<pre><code>#[macro_use]
extern crate luminance;

uniform_interface! {
  struct Common {
    resolution: [f32; 2],
    time: f32,
    jitter: f32
  }
}
</code></pre>
<p>You’ll notice two interesting properties:</p>
<ul>
<li>You define your fields without annotating them with <code>Uniform&lt;_&gt;</code>: the macro does it for you.</li>
<li>The <code>UniformInterface</code> <code>impl</code> is automatically generated for you!</li>
</ul>
<p>The second point is the most interesting one, since it’s akin to writing
<code>#[derive(UniformInterface)]</code>. As you can see, the syntax overhead is not really a problem.</p>
<p>The cool part of that macro is that it also supports Rust annotations on its fields. You have two
possible annotations available:</p>
<ul>
<li><code>as("something")</code> behaves by not using the field’s direct name and instead use the one provided as
argument.</li>
<li><code>unbound</code> enables not to make the whole <em>uniform interface</em> fail if a field cannot be mapped on
the GPU side.</li>
</ul>
<p>The <code>as(…)</code> annotation is very simple and straight-forward to get, since it lets you change the
binding name of the field. The <code>unbound</code> annotation is a bit trickier and you need to understand how
<code>UniformInterface</code> expects the value to be constructed.</p>
<p>By default, when you try to map a field to something on the GPU side (shader program), you use the
<a href="https://docs.rs/luminance/0.25.5/luminance/shader/program/struct.UniformBuilder.html#method.ask"><code>UniformBuilder::ask</code></a> function. If you have a closer look at function, you can see that it returns
<code>Result&lt;Uniform&lt;T&gt;, UniformWarning&gt;</code>. In order to get the <code>Uniform&lt;T&gt;</code> out, you’re left with the
choice of how you should handle errors. And that will depend on how you want your value to be built.
For instance, some fields might be completely mandatory for your shader to work and others optional.
<code>unbound</code> tags a field as optional.</p>
<p>You can mix both annotations if you want to remap an optional field!</p>
<pre><code>#[macro_use]
extern crate luminance;

uniform_interface! {
  // we want to export that, so we also make it pub
  pub struct Common {
    resolution: [f32; 2], // this is really required
    #[as("t")]
    time: f32, // will be referenced with "t" in the shader and is mandatory as well
    #[unbound, as("bias")]
    jitter: f32 // will be referenced with "bias" in the shader and is optional
  }
}
</code></pre>
<p>That’s all for today! I hope that feature will be useful for whomever uses <a href="https://crates.io/crates/luminance">luminance</a> – I use it
extensively and I’ve been using <code>uniform_interface!</code> for several days now and it’s <em>really</em>
appreciated! :D</p>
<blockquote>
<p>I plan to add another macro to create <em>buffer types</em>, since they must meet GPU-specific alignment
rules, who are boring to maintain without code generation.</p>
</blockquote>
<p>Feel free to provide feedback on Reddit or GitHub and have fun!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 01 Mar 2018 02:18:00 GMT</pubDate></item><item><title>My thoughts about editors in 2022</title><link>https://strongly-typed-thoughts.net/blog/editors-in-2022</link><description><![CDATA[<p>Today is the 1st of January, 2023. I think it’s the right moment to write another blog article about editors and
productivity platforms. If you haven’t read my previous iterations in that series, here are some links you might want
to have a look at (not required for this article, though):</p>
<ul>
<li><a href="https://phaazon.net/blog/editors-in-2020">My thoughts about editors in 2020</a>.</li>
<li><a href="https://phaazon.net/blog/editors-in-2021">My thoughts about editors in 2021</a>.</li>
<li><a href="https://phaazon.net/blog/neovim-plugins-stability">Neovim plugins stability</a>.</li>
<li><a href="https://phaazon.net/blog/development-environments">Development environments</a>.</li>
</ul>
<p>My take on all of that has evolved a bit over the last months / weeks. A couple of things happened and I think it’s
time for an update.</p>
<h1>My views on productivity platforms</h1>
<p>If you have read my articles about development environment, you already know what I’m talking about here. If not,
let me do a little summary. Basically, as a software engineer, I need to use <em>tools</em> to solve problems. Those
problems are daily little issues while working on a project:</p>
<ul>
<li>Organizing my work.</li>
<li>Searching for stuff, like a file name, some documents, discovering a code base, etc..</li>
<li>Editing code in an efficient way.</li>
<li>Building and running programs.</li>
<li>Testing programs.</li>
<li>Debugging programs.</li>
<li>Versioning my code.</li>
<li>Recording notes, journaling, creating meeting summaries, etc..</li>
<li>Manipulating program output.</li>
<li>Composing such output.</li>
<li>Viewing read-only data and grepping into it.</li>
<li>Testing an API by issuing HTTP / gRPC / whatever calls to target endpoints.</li>
<li>Using various systems such as <code>bazel</code>, <code>helm</code>, <code>kubernetes</code>, etc. and composing them with the rest.</li>
<li>Etc. etc.</li>
</ul>
<p>Whatever the tools you are using, those problems will roughly be the same. Maybe you don’t have them all, but you
will come across a couple of them on a daily basis. Let’s start talking about what most people use: IDEs.</p>
<h2>IDEs</h2>
<p>An <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE (Integrated Development Environment)</a>
is a software that provides a solution to a subset of the problems I mentioned above. For instance, <strong>IntelliJ
IDEA</strong> has a solution to edit your code, go to definitions, implementations, lookup files, classes, symbols;
version your code and write Git commit messages; it has a debugger; it has a way to build and test your code; it
has a terminal so you can run random CLI commands; etc. It doesn’t solve all the problems, but clearly it solves
a subset of them. With programs like that, most of the time, you install it, and you’re good to go without
configuring anything. <strong>JetBrains</strong> editors are famous (and loved!) for this reason. You want to work with Java?
Install <strong>IDEA</strong>. Of course, you will still find plugins to customize the experience, but the <em>vanilla</em> editor is
most of the time enough.</p>
<p>Then you have “customizable” IDEs, such as <strong>VS Code</strong>. Such softwares will often require you to download plugins
because the default experience is unlikely to have support for your language, even though it should be enough for
most people. <strong>VS Code</strong> is clearly the most famous one and it has a plugin for everything you might need (or not
know you need!). I could have merged the two IDE sections into one, but I do make a difference in my mind because
of how <strong>JetBrains</strong> are advanced and ready-to-be-used. <strong>VS Code</strong>, whether you like it or not, is an important
piece of software. Microsoft has made a big change with it, since most developers would agree it’s a good editor
and environment to develop in, and it helped introducing things like
<a href="https://microsoft.github.io/language-server-protocol/">LSP</a> and
<a href="https://microsoft.github.io/debug-adapter-protocol/">DAP</a>. You cannot trash-talk <strong>VS Code</strong> in a non-joking way,
they contributed too much and we all use their work.</p>
<h2>The rest</h2>
<p>And then, you get into the “do one thing and do it right” way of working, but it’s more complicated than that.
Historically, you would find tools such as <strong>Vim</strong>, <strong>Neovim</strong>, the <strong>git</strong> CLI, <strong>fzf</strong>, terminals, shells, TUI
applications, etc. However, as time passes, there is a trend: people tend to transform those “unit tools” into
IDEs. The terminology doesn’t really matter (whether you want to call that IDE or your own neologism). What matters
is that such tools are not minimal nor “do one thing and do it right” anymore. <strong>Neovim</strong>, for instance, is now
more a Lua interpreter in disguise of an editor, allowing to build via the plugin ecosystem, than a minimal editor.
I was told that I was wrong thinking that Lua wasn’t part of the equation since the beginning, so yeah, I was a bit
on the wrong track from the start.</p>
<p>It’s the same trend as it has been in the <strong>Vim</strong> ecosystem for so many years. Just look at plugins like
<a href="https://github.com/tpope/vim-fugitive">vim-fugitive</a> or <a href="https://github.com/preservim/nerdtree">nerdtree</a>. In
<strong>Neovim</strong>, you have plugins like <a href="https://github.com/williamboman/mason.nvim">mason.nvim</a> (which is
basically yet another package manager), <a href="https://github.com/folke/lazy.nvim">lazy.nvim</a> (same thing but different),
<a href="https://github.com/nvim-tree/nvim-tree.lua">nvim-tree.lua</a>, a file explorer,
<a href="https://github.com/TimUntersberger/neogit">neogit</a>, a Git implementation in Lua, and the list goes on.</p>
<p>So, yes, I also contribute to that “plugin-based” ecosystem with <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>
and <a href="https://github.com/phaazon/mind.nvim">mind.nvim</a>, but I’ve been thinking about all that quite a lot. That adds
up to what I discuss in my article about configuration vs. scripting (basically, I think configuration should
remain data, not code — which what scripting is about). Quoting something I said on the <strong>Neovim</strong> Matrix room, <em>“where
people see power, I see weakness”</em>. A scripting language brings lots of powerful things, such as extensibility, but
it also brings bugs, hence unstability, and a Turing-complete language, preventing the host (i.e. <strong>Neovim</strong> or even
plugins) to easily use the configuration to discover options and data, without having to standardize an API. The
most infuriating point here to me is colorscheme. They are just scripts. Some of them are even stateful (they cache
things in <code>~/.cache/nvim</code>). So “applying a colorscheme” is not simply just switching highlights mapping: it requires
to <em>execute code</em>, which makes it super hard to reason about the colorscheme. What’s a colorscheme when you know it
can run an HTTP command?</p>
<p>I’ve been seeing a <em>lot</em> of new plugins, since I am the author and maintainer of
<a href="https://this-week-in-neovim.org">This Week in Neovim</a>. And recently, I saw a video from
<a href="https://github.com/tjdevries">TJ DeVries</a>:
<a href="https://www.youtube.com/watch?v=stqUbv-5u2s">Effective Neovim: Instant IDE</a>. And that video confirms what I said
above <strong>Neovim</strong> becoming an IDE. But it also made me realize something else; TJ uses a plugin as support for
explaining how to easily turn your favorite editor into an IDE:
<a href="https://github.com/nvim-lua/kickstart.nvim">kickstart.nvim</a>.</p>
<p>That plugin is basically a single Lua <code>init.lua</code> script which serves as a starting init script for your configuration.
You just install it and it downloads a bunch of stuff for you, and calls all the required Lua code to set up correctly
the LSP implementation, <a href="https://tree-sitter.github.io/tree-sitter/">Tree-sitter</a>, downloading managers (yes, plural,
because you need one for plugins and one for LSPs, debuggers, linters, etc.), installing various plugins for
completion, Git integration, surrounding pairs, etc. And then, I wondered: <em>“Newcomers are expected to run a
script that can download pretty much anything from the Internet… or write pretty much most of what it does — which is
a lot — on their own?!”</em> And the thing is that, my own Lua configuration, which is not based on
<code>kickstart.nvim</code> since I created years ago, has been completely obsolete and I often need to go back to it to remove
or update things, especially regarding LSP, completion, snippets and all.</p>
<h1>Why I think it’s bad</h1>
<h2>Do one thing and do it right?</h2>
<p>Most people from the community I talked to disagree with my point of view regarding <strong>Neovim</strong>. For them, plugins like
<a href="https://github.com/williamboman/mason.nvim">mason.nvim</a> are amazing, because they close the gap between their
editor and the tools required by their editor to work correctly (Mason downloads LSPs / linters / DAP
adapters / etc.). I used Mason too, but eventually stopped using it when it started downloading a version of
<a href="https://rust-analyzer.github.io/">rust-analyzer</a> that was released <strong>years ago</strong> (that was a bug in Mason, I
guess?). I came to the conclusion that I was depending on something doing HTTP calls to download tools that, in
theory, could be used by other tools on my machines, and that I could also download myself very easily. In the case
of <code>rust-analyzer</code>:</p>
<pre><code class="language-sh">rustup component add rust-analyzer
</code></pre>
<p>Worse… some of those tools are actually packaged in my package manager (<code>pacman</code>), so I’m basically using a tool
(Mason) that is doing the same thing as a package manager. As if we did not have enough package managers already.</p>
<h2>Too high and wrong expectations</h2>
<p>I then continued thinking about all those plugins (among some I use or even have created, like Mind!). Why should
I use them in <strong>Neovim</strong>? I’ve never been a file explorer guy that much, but I know about
<a href="https://github.com/nvim-tree/nvim-tree.lua">nvim-tree.lua</a> and… and why do we have to have that in our editor? I
remember the state of mind I was in when I wrote my article about
<a href="https://github.com/doomemacs/doomemacs">Doom Emacs</a>, which completely changed the way I think about software
development. <strong>Emacs</strong> doesn’t belong to the “minimal editors”, nor the IDEs directly… it’s a different beast on its own,
but if I had to compare it to something else, I wouldn’t say <strong>VS Code</strong>, or <strong>Neovim</strong>, or anything else. I would
say <em>“My terminal with all of the commands I run, including an editor, a git implementation, etc.”</em> Wait a minute.
Why are we trying to push all those features as plugins into <strong>Neovim</strong>, again?</p>
<p>The reason I didn’t stick around <strong>Emacs</strong> was basically because of its runtime choice, and ultimately, its Lisp
ecosystem. The <strong>Emacs</strong> community is one of the best I have been talking to, and they have really, really talented
people working on insanely complex topics, like turning Elisp code into native code (via C), including Emacs itself.
But even with all those optimizations, the editor was still feeling too slow, and has a lot of background that you
can feel. All the abstraction layers, all the Lisp macros (oh no), etc.</p>
<p>Eventually, I went back to thinking about that sentence… that haunting sentence… <em>“Do one thing, and do it right.</em>”
That sentence has a lot of meaning and I think people have been tearing and bending it to align with their
conviction, completely ignoring their biases. I read people from the Emacs community stating that yes, Emacs still
applies to that sentence, because <em>“It’s just a Lisp interpreter.”</em> But in the end, the experience users have highly
depends on the plugins, which is the same situation as with <strong>Neovim</strong>. And they have to configure their editor using
a Turing-complete language that might introduce bugs, complex statements (who loves to set up a colorscheme by
conditionally importing / requiring a Lua file located you-have-no-clue-where on your file system?)</p>
<p>Why are we trying to push all those features as plugins into <strong>Neovim</strong>, again? Why am I not trying to focus on using
tools with a narrower scope, but ensure that the tool remains stable, powerful to use and compose well with the rest?</p>
<h1>Exploring new paths</h1>
<p>Lately, I have discovered <a href="https://helix-editor.com/">Helix</a>, a <em>“post-modern modal editor”</em>. The first thing I
noticed was that it’s different than <strong>Neovim</strong> in terms of motions. In <strong>Neovim</strong>, in normal mode, you start with a
<em>verb</em>, like deleting is <code>d</code>, and then you type a motion, like a word is <code>w</code>. So typing <code>dw</code> on your keyboard will
delete a word. In <strong>Helix</strong>, it’s reversed. You first select with <code>w</code>, and then you apply the verb you want.
So <code>wd</code>. At first, I thought is was a neglible difference. Then I realized how more powerful the [Helix]’ way ways.
Since you “see” the selections, you can select first and then decide what to do, or even change your mind and extend
a bit the selection. You have this nice visual feedback.</p>
<p>And then comes all the good stuff. <strong>Helix</strong> comes with those included features, <strong>requiring zero configuration</strong>:</p>
<ul>
<li>An LSP native implementation (the editor is written in Rust, so is the LSP implementation then), with all the
features you expect, like preview popups, documentation, signatures, go-to-def, references, implementations, rename,
code actions, etc.</li>
<li>Tree-sitter support, including highlights, indents, text-objects, etc.</li>
<li>DAP support, both the feature and its UI.</li>
<li>Multi-cursors.</li>
<li>Surrounding pairs.</li>
<li>Git gutters.</li>
<li>Fuzzy finders for buffers, symbols, jump lists, diagnostics, project-wise / global grepping, etc.</li>
<li>Etc.</li>
</ul>
<p>And it has no plugin support for now (they plan on adding it at some point, but not for now). And that made me realize:
my editing experience in that editor — even though it took a couple of days to adjust the muscle memory — has been
flawless. So yes, I miss my <code>hop.nvim</code> plugin, but I realized I could hack around by using
<a href="https://sw.kovidgoyal.net/kitty/kittens/hints/">Kitty hints</a> until that kind of motions is built-in.</p>
<p>However, I’m just talking about editing, here. I still have that reflex of pressing <code>SPC g g</code> to bring up a Git prompt
in my editor to start committing… but <strong>Helix</strong> doesn’t have one. So I’m splitting my terminal into another window and
I use the <code>git</code> CLI. And it’s fine. Now, the way I think about it is that I could probably invest time into learning
<a href="https://github.com/jesseduffield/lazygit">lazygit</a> or anything else.</p>
<p>The point is that my editor is now minimal again. My configuration (which is public, you can find it
<a href="https://github.com/phaazon/config/blob/master/helix/config.toml">here</a>) is mostly about
<a href="https://bepo.fr/wiki/Accueil">bépo</a> remappings and some very minimal aesthetics changes. The configuration is data
(it’s just a TOML file), and I don’t have to worry about stability anymore since I’m not using any plugin. The fact
that I have an amazing editing experience (even better, honestly, due to the selection then verb principle,
multi-cursors, out-of-the-box LSP and Treesitter experience, including completion) is just the perfect fit for what
I want.</p>
<h1>Yes, but</h1>
<p>But there’s a catch. See, <strong>Helix</strong> is about editing. If you like to have a file explorer in your editor, the way I
would recommend looking at <strong>Helix</strong> is that <em>you cannot have it in Helix and you should probably use a proper,
standalone file explorer, or consider another alternative like Neovim</em>. If there is something you would like to add
to <strong>Helix</strong>, you have to open a PR and write some Rust code. You cannot extend it on your own, as it doesn’t have
plugin.</p>
<p>To me, that’s great, because it means the scope is under the responsibility of the <strong>Helix</strong> Team. And I love that. I
love it because it’s <em>easy</em> to think about the features of my editor. It’s easy because I don’t have to keep
fearing something break because of an incompatibility between a plugin and the version of the editor I’m running
(or even two plugins between each other).</p>
<p>And honestly, contributing using a statically and strongly typed language (Rust) feels so much <em>sounder</em> to me than
using something like Lua. You can benefit from all the tests of the code base, use the API without any ABI
conversion in between, and catch bugs at compile time instead of waiting for them to crawl up at runtime.</p>
<h1>So what does it mean for “productivity platforms?”</h1>
<p>My view on that hasn’t changed since my last articles. I still think that the terminal needs to be revamped and go
into a direction similar to Emacs. I have started a project a couple of months ago that tries to explore that.
Basically, I’m making something that is not a terminal, a shell nor editor. It’s “a platform”, with primitives like
tree views, item lists, read-only / read-write buffers, virtual text, popups, command outputs, cells, etc. And it
comes with no way to edit text or file explorers, or anything.</p>
<p>Then, applications targetting that platform can use all those primitives to compose and now the features are
emergent. A text  editor then uses the buffer, virtual text and popup primitives, for instance. A file explorer would
use the tree view primitive. An LSP client could be a daemon that attaches to edit buffers. Etc. etc.</p>
<p>That’s the dream tool I would love to see, and I still think that Emacs is the closest thing to that, but it comes
with too much legacy to me. And it’s unlikely that my experiment will ever be mature enough to be usable or even
used. But you know, I like experimenting. A cool project to play with is <a href="https://www.nushell.sh/">nushell</a>. It’s far
from being that dream platform of mine, but it has some very interesting ideas for composing commands that I think
are worth mentioning.</p>
<p>And no, I don’t want <strong>Helix</strong> to become such a platform. Nor <strong>Emacs</strong> to get rid of its legacy and become it. Nor
<strong>Neovim</strong>. I want to keep playing with <strong>Helix</strong> and using it for what it is (and shines for!): editing code. If my
dream platform ever exists, whether it’s mine or someone else’s, I will probably move away from <strong>Helix</strong> to whatever
that platform provides. But such a change would require a standardization, such as <code>stdout</code> and <code>stdin</code>, but with all
those primitives I mentioned. And I’m not sure whether such a thing will or can exist.</p>
<h1>And Helix? Is it good?</h1>
<p>I’m not going to give my <em>complete</em> opinion on <strong>Helix</strong> just yet. I have been using it for a couple of days only,
and at the time writing, it still has a lot of missing parts / experimental ones. For instance, its DAP support is
experimental, so I can’t judge. What I plan to do is to stick to it and move away from “making my own IDE in an
editor” and instead enjoying composing tools on the CLI. Then, when I have enough hindsight, I will give a fair review
of <strong>Helix</strong>.</p>
<h1>What about <code>mind.nvim</code>, <code>hop.nvim</code> and This Week in Neovim?</h1>
<p>So, about <code>mind.nvim</code>, I plan on rewriting the plugin as a standalone tool so that I can use it whatever the editor.
It will probably do things like <code>git</code> when you run <code>git commit</code> (opening <code>$EDITOR</code>), but I’m still not sure exactly
how I’m going to make it. Maybe I’ll get in touch with people from <a href="https://charm.sh/">Charm</a> and rewrite it using some
of their work? I still haven’t thought about it, it’s too early.</p>
<p>About <code>hop.nvim</code>, I plan on continuing maintaining it and fixing bugs, even though I haven’t been very active around <code>Hop</code>
lately. The reason is mainly a lack of spare-time.</p>
<p>As for <strong>This Week in Neovim</strong>… I honestly do not know. I discussed the project with some people from the <strong>Neovim</strong> core
team, and I’m a bit stuck. On one side, the community has received it pretty well, given the amount of upvotes I have
on each week release on Reddit; the comments; the appreciation issues on GitHub, etc. I know people have been
enjoying my work, and I’m happy they do.</p>
<p>On the other side, the core team doesn’t seem to have noticed it that much, and none of their members approached me
to talk about it. So I’m not sure what to think. The community enjoys TWiN a lot; the core team doesn’t really care.
Then I need to think about <em>exhaustion</em>: I’m really tired of maintaining TWiN.</p>
<p>See, the idea is to communicate, every week, about what has happened in the <strong>Neovim</strong> world, whether it’s core or
plugins. What I had initially in mind was to <em>bootstrap</em> the couple of first releases and let people adopt and
contribute to it. On the 2nd of January 2023 is released <strong>TWiN #25</strong>, which means that I’m currently on a
25 weeks streak. What it basically tells is that, every week (most of the time Sundays), I skim Reddit, GitHub
projects, man pages, etc. to get as much information as I can, and create a really big PR containing the weekly
update. That PR is merged and available on the very next day (Monday) for every neovimmers to enjoy reading on Monday
morning with a nice cup of coffee, tea or whatever you like for breakfast.</p>
<p>So every week, one person (me) spends hours skimming many projects, while what I thought would happen was that
many plugin authors would contribute once every two months a very small text to explain their new plugins / change.
The difference is massive: on one side, you have a single developer doing a big amount of work… every week. On the
other side, you would have many developers doing a very small amount of work every time they release something… which
is clearly not every week (and even then?).</p>
<p>I think I have enough distance with the project to admit I failed marketing my idea. Someone once told me that I was
basically doing free advertisement for plugin authors, which is actually true. People mention they would like to
donate to contribute and ensure that I keep doing what I do, but I don’t want money — hosting costs me 10€/month and the
domain name is 10€/year, I can sustain that on my own. What I need is contributions. It wouldn’t cost much for a plugin author
to open a PR to <a href="https://github.com/phaazon/this-week-in-neovim-contents/pulls">twin-contents</a> and add their
update to the upcoming week. There’s a few regular contributors, writing good PRs I rarely
need to modify. But most of the weeks are contribution-free, and it saddens me even more when I see the reaction of
plugin authors on Reddit, like <em>“Oh yeah my plugin made it to TWiN!”</em> Every time I read that, I think <em>“Great, next
time maybe they will be pushing the update themselves to help me?”</em> And most of the time, they don’t.</p>
<p>So, people started to mention that I should slow down or I will burn out. And I’m honestly pretty fed up with this
<em>read-only</em> relationship: people consume / read; they rarely contribute, even when they could contribute their own
update for their own plugins! I don’t filter out <em>anything</em>, as long as it’s about <strong>Neovim</strong> and doesn’t convey any bad
speech (you know the deal). TWiN is about new plugins, updates of existing plugins, tips of the week, blog articles,
youtube videos, etc. Anything <strong>Neovim</strong> related produced by a member of the community. And even with the exposure of
TWiN, people still do not contribute. Even after the big refactoring to ease contribution <a href="https://www.reddit.com/r/neovim/comments/yeo89j/twin_gets_a_new_collaboration_process_to_ease/">I announced on
Reddit</a>. So yes, it’s
a personal failure to market my idea regarding TWiN, and I’m not sure what the next steps are.</p>
<p>Nevertheless, we all learn from mistakes and it’s important to understand them. I will collect my thougths and decide
what to do next. For the time being, I wish you all a Happy New Year, lots of great things, and a tremedous amount of
happy hacking, in your favorite editor, IDE or whether you pipe <code>echo</code> commands directly at the end of files!</p>
<p>Keep the vibes!</p>
<blockquote>
<p><a href="https://www.reddit.com/r/programming/comments/1007yq5/my_thoughts_about_editors_in_2022/">Discuss here</a></p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 01 Jan 2023 00:00:00 GMT</pubDate></item><item><title>The Kakoune philosophy</title><link>https://strongly-typed-thoughts.net/blog/kakoune-philosophy</link><description><![CDATA[<p>I have been using <a href="https://kakoune.org">Kakoune</a> for a long time, and I want to talk about the User Experience (UX). When it comes down to
editors / productivity platforms, people tend to confuse UX with User Interface (UI). Both are related, of course, but
having a great UX doesn’t mean the UI is “great” and having a great UI doesn’t mean the UX is “great”: it’s all a matter
of perspective.</p>
<p>In this blog entry, I want to talk about UX more specifically, because I do think it’s more important than UI. I will
try to show that the default UX of Kakoune is incredible and that you can <strong>very quickly</strong> create a super expressive
and pragmatic programmer environment. It will cover:</p>
<ul>
<li>Surrounding pairs.</li>
<li>Grepping around</li>
<li>Pickers.</li>
<li>System clipboard.</li>
<li>Bonus: some tools I’ve been making.</li>
</ul>
<blockquote>
<p>Once again, I’d like to greet and thank <a href="https://github.com/alexherbo2">@Taupiqueur</a> for sharing his thoughts and joy about Kakoune.</p>
</blockquote>
<h1>What I mean with UI and UX</h1>
<p><strong>UI</strong> means “anything interfacing the user to the system.” It’s both the visual depiction of the service (the menu, the
colors, the fonts, etc.) and the way you interact with the system (with the keyboard, by clicking on buttons, when a
system event happens, etc.).</p>
<p><strong>UX</strong> means “how the user experiences the system.” For instance, something that is not UI at all but enhances the UX
is having a way to filter data in a system with high volume of events with a tag. An even better UX is having fuzzy
filtering with any tags. Etc.</p>
<p>There are many possible UI implementations for a given UX item: implementing filtering can be done with a select box in
a GUI, but the UX is not great because the user is presented with a set of finite choices, and if there are many, it’s
pretty annoying to scroll down the list to find the one we want. Even with this bad UX design, most of select box
implementations (e.g. web) allow to press keys to jump to the entry in the select options — most of the time, you don’t
see what you type -&gt; bad UX for the user. Another possible UI implementation would be to use a free text box that would
filter based on its content — it could even be live for an even better UX. Etc. etc.</p>
<p>Now, would you prefer a nice looking GUI with the select box, or a blander UI but with the fuzzy search box? Clearly,
in terms of UX, the second option is much better. But now, imagine a GUI with the fuzzy search box. It would be pretty
similar to the blander UI in terms of UX, but it would look (much) better… which is likely to enhance the UX as well!</p>
<h1>Small disgression: TUI vs. GUI</h1>
<blockquote>
<p>TUI: Text User Interface, which is a program mimicking traditional GUIs in the terminal.</p>
</blockquote>
<blockquote>
<p>GUI: Graphics User Interface, the typical window-based applications you run on your machine.</p>
</blockquote>
<p>Before jumping to the Kakoune content, I just want to disgress slightly to talk about something I often see something
that itches me a bit: many individuals seem to say that a TUI is often written in a way that optimizes UX and GUI the
UI, and hence, oftentimes, using a TUI feels much better. I agree with this (this is the reason why I’m using editors
and tools inside my terminal instead of a dedicated GUI, even though I think the UI is worse, for many reasons: no
pixel-perfect alignment of things; no direct integration of the application at the OS level, it has to go through the
terminal and shell; etc. etc.).</p>
<p>However, is there anything forcing a GUI not to provide the same kind of interactions as a TUI? I don’t think so. If
you make your TUI keyboard-oriented, everything you do is just listening to keyboard events provided by whatever
terminal protocol / library you use… which could be done exactly the same way in a GUI.</p>
<p>I do think that we should be able to make a GUI as good as a TUI in terms of UX, and some programs did it. For instance,
<a href="https://www.gnu.org/software/emacs/">emacs</a> can run both as a TUI and a GUI (and today, people recommend actually using the GUI). I think that could be the
topic of another blog article. Let’s go back to the original matter.</p>
<h1>Kakoune UX</h1>
<p>Kakoune, upon installation, already provides a lot of good things in terms of UX. But as you get more productive with
it, you will face some problems. For instance, the first one I came across (pretty quickly being honest, coming from
<a href="https://helix-editor.com">Helix</a>) was surrounding: adding, deleting and replacing surrounding delimiters. Where you need a plugin to do that in
the Vim world, in Kakoune, it’s another topic. Some plugins exist to do exactly that, but honestly, read along.</p>
<p>Selections in Kakoune — which are so much more than <em>“iT’s JUsT LikE muLtIcURsOrs oR a nOOb WaY Of DoINg RegEX in
vIM”</em> — change the Vim interpretation of <em>appending</em> and <em>preppending</em>. In <a href="https://www.vim.org">Vim</a>, <code>i</code> will start inserting on the cursor,
whereas <code>a</code> will start inserting after the cursor. In Kakoune, <code>i</code> inserts <strong>at the beginning of each selection</strong> and
<code>a</code> inserts <strong>at the end of each selection</strong>. That’s already a better UX, and it allows to do many things out of the
box.</p>
<p>For instance, since we have the power to insert at the beginning and at the end of each selection… then pressing <code>i'</code>
should insert a quote at the beginning of the selection… and <code>a'</code> will do the same at the end! So you can already have
a somewhat working surrounding add operation by typing <code>i'&lt;esc&gt;a'</code>!</p>
<blockquote>
<p>If you are adventurous, you can read the documentation of <code>&lt;a-;&gt;</code>, which allows to leave insert mode to normal mode
for a single command, and come back to insert mode. You can use this to replicate what we did above without the
<code>&lt;esc&gt;</code> key: <code>i'&lt;a-;&gt;a'</code>. Magic.</p>
</blockquote>
<p>It’s a bit annoying to have to type all that, though, right? So instead, we could make a command! Commands in Kakoune
are really simple, but they require reading a bit about them. I recommend the following reads if you want to dig in a
bit:</p>
<ul>
<li><code>:doc commands</code> to know about all the commands. Search for <code>^define-command</code>.</li>
<li><code>:doc execeval</code>, which explains what <code>evaluate-commands</code> and <code>execute-keys</code> do. Especially, you will want to read the
part of <code>-draft</code>.</li>
</ul>
<p>So let’s wrap that sequence of keys in a command. Instead of using <code>i</code> and <code>a</code>, we are going to use <code>P</code> and <code>p</code>, which,
as the name implies, <em>paste</em> from the default register. <code>p</code> <strong>pastes at the end of the each selection</strong> and <code>P</code> <strong>pastes
at the beginning of each selection</strong>.</p>
<h2>Surrounding pairs</h2>
<p>What is great is that the default register, <code>"</code>, is selection-aware: its content will be different from one selection to
another. Said otherwise, there is one <code>"</code> register for each selection. Hence, we can write this:</p>
<pre><code>define-command -override my-surround-add -params 2 %{
  evaluate-commands -draft -save-regs '"' %{
    set-register '"' %arg{1}
    execute-keys -draft P
    set-register '"' %arg{2}
    execute-keys -draft p
  }
}
</code></pre>
<p>And here you go. You can now invoke <code>:my-surround-add ( )&lt;ret&gt;</code> to add parenthesis around your selections, for instance.</p>
<p>Kakoune has the concept of user modes, which is a nice feature allowing to declare a keyset (that will be displayed
by the help in the bottom right of your screen) when entered.</p>
<pre><code>declare-user-mode my-surround-add
</code></pre>
<p>We can make one with a bunch of mappings in that user mode:</p>
<pre><code>map global my-surround-add (   ':my-surround-add ( )&lt;ret&gt;'         -docstring 'surround with parenthesis'
map global my-surround-add )   ':my-surround-add ( )&lt;ret&gt;'         -docstring 'surround with parenthesis'
map global my-surround-add [   ':my-surround-add [ ]&lt;ret&gt;'         -docstring 'surround with brackets'
map global my-surround-add ]   ':my-surround-add [ ]&lt;ret&gt;'         -docstring 'surround with brackets'
map global my-surround-add {   ':my-surround-add { }&lt;ret&gt;'         -docstring 'surround with curly brackets'
map global my-surround-add }   ':my-surround-add { }&lt;ret&gt;'         -docstring 'surround with curly brackets'
map global my-surround-add &lt;   ':my-surround-add &lt; &gt;&lt;ret&gt;'         -docstring 'surround with angle brackets'
map global my-surround-add &gt;   ':my-surround-add &lt; &gt;&lt;ret&gt;'         -docstring 'surround with angle brackets'
map global my-surround-add "'" ":my-surround-add ""'"" ""'""&lt;ret&gt;" -docstring 'surround with quotes'
map global my-surround-add '"' ":my-surround-add '""' '""'&lt;ret&gt;"   -docstring 'surround with double quotes'
map global my-surround-add *   ':my-surround-add * *&lt;ret&gt;'         -docstring 'surround with asteriks'
map global my-surround-add _   ':my-surround-add _ _&lt;ret&gt;'         -docstring 'surround with undescores'
</code></pre>
<p>Obviously, that’s not all; we would need to delete delimiters and to replace them. Deleting is actually even more
straightforward. Kakoune has some native mappings to select everything <em>inside</em> or <em>around</em> a set of delimiters:</p>
<ul>
<li><code>&lt;a-i&gt;</code> to select <em>inside</em>.</li>
<li><code>&lt;a-a&gt;</code> to select <em>outside</em>.</li>
</ul>
<p>So, pressing <code>&lt;a-a&gt;(</code> (or <code>&lt;a-a&gt;)</code>, same thing) will select everything around the cursor up to the next pair of
parenthesis.</p>
<blockquote>
<p>Note: I have personally remapped that to <code>mi</code> and <code>ma</code>, but that collides with the default meaning of the <code>m</code> key in
Kakoune, so I will use the native mapping here instead.</p>
</blockquote>
<p>We can then simply use the previous <code>i</code> and <code>a</code> command mentioned before to start editing at the beginning and end of
the selection. <code>i&lt;del&gt;</code> will start insert mode at the beginning of the selection and will remove a character (left
delimiter) and <code>a&lt;backspace&gt;</code> will insert at the end of the selection and remove the previous character (right
delimiter). Eh, that looks like so simple it’s almost stupid. But that’s what makes Kakoune so damn good: it’s <em>simple</em>
to reason about:</p>
<pre><code>define-command -hidden my-surround-delete-key -params 1 %{
  execute-keys -draft "&lt;a-a&gt;%arg{1}i&lt;del&gt;&lt;esc&gt;a&lt;backspace&gt;&lt;esc&gt;"
}

define-command my-surround-delete %{
  on-key %{
    my-surround-delete-key %val{key}
  }
}
</code></pre>
<p>Because Kakoune composes really well, you can already imagine that you should be able to use the previous commands and
mappings. And indeed:</p>
<pre><code>define-command my-surround-replace %{
  on-key %{
    surround-replace-sub %val{key}
  }
}

define-command -hidden my-surround-replace-sub -params 1 %{
  on-key %{
    evaluate-commands -no-hooks -draft %{
      execute-keys "&lt;a-a&gt;%arg{1}"

      # select the surrounding pair and add the new one around it
      enter-user-mode my-surround-add
      execute-keys %val{key}
    }

    # delete the old one
    my-surround-delete-key %arg{1}
  }
}
</code></pre>
<h2>Revisiting grepping</h2>
<p>Ah… who has never had the problem of trying to locate something in a codebase without <em>really knowing where to start</em>.
That happens a lot to me when working on a front-end project and taking an issue asking to fix a random page, that is
broken. Usually, LSPs don’t help to <em>discover</em> things that are based on the final product. For instance, if you see the
checkout page broken — like a <code>&lt;div&gt;</code> is missing an attribute or a tag is misplaced in the DOM, no LSP will help you
locate the code that needs to be fixed. Instead, you need other tools.</p>
<p>What I like to do is looking at the page and looking for what I call <em>unique tokens</em>. For instance, a short sentence
that might appear only on that page, or modal. Or a header, a title, etc. Then, using that information, I usually grep
the code base. The problem is that, doing that using <code>grep</code> or <code>ripgrep</code> as CLI is pretty boring, and not very
practical. Indeed, if you get many results, you are likely to try to reduce the result set by constraing more your
regex. Once you have some files, you usually look in your terminal scrollback buffer until you find something
interesting, then open that file in your editor.</p>
<p>People using something like <strong>IntelliJ</strong> products, or <strong>VS Code</strong>, or some plugins with <strong>emacs</strong> or <strong>vim</strong> might have
a way to perform the search from within their editors, but again, that’s not composability: it’s extensibility, and I
explained in <a href="https://phaazon.net/blog/more-hindsight-vim-helix-kakoune">a previous article</a> why it’s not a good design <em>to me</em>.</p>
<p>Kakoune, on the other side, went the <em>composition</em> route. <code>grep</code>, <code>ripgrep</code>, etc. are all <ins>amazing</ins> tools.
Kakoune comes with a bunch of what it calls <em>tools</em>, which are basically Kakoune commands shipped with the editor. Among
those, there is one that is of interest here: the <code>:grep</code> command. The <code>:grep</code> command forwards its arguments to the
underlying <code>%opt{grepcmd}</code> (which defaults to something like <code>grep -RHn</code>). Hence, <code>:grep foo</code> will run <code>grep -RHn foo</code>
in a shell behind the scene, then the result will be output in a <code>*grep*</code> buffer. That buffer will get special
highlightings, along with some keybindings, all of that provided by the <code>grep.kak</code> tool. If tried the command, you might
have noticed that it’s basically a list of lines of the form:</p>
<pre><code>&lt;path&gt;:&lt;line&gt;:&lt;column&gt;:&lt;content-of-the-line&gt;
</code></pre>
<p>Then, how do you think <code>grep.kak</code> implements _pressing <code>&lt;ret&gt;</code> jumps to the line, column and file of the line under the
cursor? The cursor can be anywhere on the line. Well, it’s simple: <code>execute-keys</code> again! <code>gh</code> will put your cursor at
the beginning of the line. Then, you can use <code>f:</code> or <code>t:</code> to jump to the next <code>:</code> — there is no need to parse anything,
we can just programmatically interact with the editor!</p>
<p>Here, <code>ghT:"py</code> will go the beginning of the line, select the path and yank it to the <code>p</code> register. We can then do
<code>2l</code> to move the cursor to beginning of the <code>&lt;line&gt;</code> number, and <code>T:"ly</code> will yank the line number to the <code>l</code> register.
<code>2l</code> again to move to the <code>&lt;column&gt;</code> section, ten <code>T:"cy</code> to yank the column number to the <code>c</code> register. And we have
everything we need. We can then just simply run the <code>edit -existing %reg{p} %reg{l} %reg{c}</code> command:</p>
<p>aoc-18.md:1:1:lol</p>
<pre><code>define-command -override my-jump-for-current-line %{
  evaluate-commands -save-regs clp %{
    execute-keys -draft 'ghT:"py2lT:"ly2lT:"cy'
    edit -existing %reg{p} %reg{l} %reg{c}
  }
}
</code></pre>
<p>A couple of comments here:</p>
<ul>
<li><code>-save-regs clp</code> saves the content of the <code>c</code>, <code>l</code> and <code>p</code> register before evaluating the commands, then restores
those registers afterwards. That is required since we copy a bunch of data to those registers, but maybe the user is
already using them. <code>-save-regs</code> allows us to specificy a set of registers we locally need, but we do not want those
registers to bleed outside.</li>
<li><code>-draft</code> makes the command evaluation run in disposable context. That prevents our selections from being changed —
<code>execute-keys</code> runs a couple of move and goto commands here, while we don’t really want the cursor to move.</li>
<li><code>-existing</code> fails if the file doesn’t exist.</li>
</ul>
<p>The <code>:grep</code> tool implements something probably very similar to this, but it uses <code>%opt{grepcmd}</code>, which you can change
to whatever you like. I personally use:</p>
<pre><code>set-option global grepcmd 'rg --column --smart-case --sort path'
</code></pre>
<h2>Kakoune lacks pickers… or does it?</h2>
<p>Something that is very important having around is being able to locate files very quickly. Most editors today ship with
a way to locate files:</p>
<ul>
<li>Vim has <em>netrw</em>. It’s old. It’s ugly. But it’s there. It’s basically a file browser with a (very) minimal set of
features.</li>
<li>VS Code has fuzzy pickers, so you can locate a file by fuzzy searching it.</li>
<li>Etc.</li>
</ul>
<p>Kakoune has a default powerful completion engine. Pressin <code>:edit </code> (or, for short, <code>:e </code>) and then typing something will
auto-complete the file in the current directory with a somewhat fuzzy algorithm. If you select a directory and type the
trailing <code>/</code>, it will list the content of that directory and will auto complete its children. It’s a pretty nice way to
start moving around with the vanilla editor.</p>
<p>However, Kakoune doesn’t ship more than this, because, well, it’s <em>composable</em>. You can use any tool you like to locate
files, and then compose them with Kakoune. I personally really like <code>fd</code> (a Rust rewrite of <code>find</code>). For instance,
<code>fd --type file</code> will locate all the files in the current directory. Piping that to a fuzzy finder would allow to
jump to any file in the current directory. And Kakoune supports that. It has several commands for that, but the one you
will be interested at first is <code>prompt</code>. It prompts the user for some text and pass the entered text to the provided
commands in the <code>%val{text}</code> variable. It supports some switches, and one of interest for us is
<code>-shell-script-candidates</code>. That switch accepts a shell command to execute, reading from its standard output
asynchronously and displaying into the completion items. For instance, try running the following command:</p>
<pre><code>prompt -shell-script-candidates ls file: %{ info "you selected %val{text}" }
</code></pre>
<p>It runs <code>ls</code>, gets the output and allows you to fuzzy complete the result. Now consider this:</p>
<pre><code>prompt -shell-script-candidates 'fd --type file' open: %{ edit -existing %val{text} }
</code></pre>
<p>And bam. Here you have it. Fuzzy picker in Kakoune. You can map that to a command, or wrap it in a command:</p>
<pre><code>define-command my-file-picker %{
  prompt -shell-script-candidates 'fd --type file' open: %{ edit -existing %val{text} }
}
</code></pre>
<p>If — like many people — you want that to be run when you type <code>SPC f</code>:</p>
<pre><code>map global user f ':my-file-picker&lt;ret&gt;'
</code></pre>
<p>You can do the same thing with anything, really. Some plugins like <code>kak-lsp</code> uses that to show document symbols, etc.</p>
<h2>System clipboard</h2>
<p>By default, Kakoune comes up with registers you can yank to. However, oftentimes, you need to yank and paste from the
system cliboard. Kakoune doesn’t have a <em>“system register”</em> as in Vim. Instead, as with the rest, you have to compose
some tools with Kakoune to get that feature. And it’s pretty simple. You need to know how to yank some text in CLI. Two
situations:</p>
<ul>
<li>On <strong>macOS</strong>, we use the <code>pbcopy</code> and <code>pbpaste</code> to respectively copy and paste from/to the system clipboard.</li>
<li>On <strong>Linux</strong>, we use the <code>xclip</code> and <code>xsel -ob</code> commands to do the same.</li>
</ul>
<p>And then, there is nothing much more to do. We can wrap those in two commands agnostic of the platform by using the
<code>uname</code> utility:</p>
<pre><code>declare-option str extra_yank_system_clipboard_cmd %sh{
  test "$(uname)" = "Darwin" &amp;&amp; echo 'pbcopy' || echo 'xclip'
}

declare-option str extra_paste_system_clipboard_cmd %sh{
  test "$(uname)" = "Darwin" &amp;&amp; echo 'pbpaste' || echo 'xsel -ob'
}
</code></pre>
<p>And the actual commands, using the <code>!</code> key (to run a shell command with the current selection piped as standard input
and replace selections with its output) and <code>&lt;a-!&gt;</code> (which runs a shell command and ignore its output):</p>
<pre><code>define-command extra-yank-system -docstring 'yank into the system clipboard' %{
  execute-keys -draft "&lt;a-!&gt;%opt{extra_yank_system_clipboard_cmd}&lt;ret&gt;"
}

define-command extra-paste-system -docstring 'paste from the system clipboard' %{
  execute-keys -draft "!%opt{extra_paste_system_clipboard_cmd}&lt;ret&gt;"
}
</code></pre>
<p>I personally map those as in Helix:</p>
<pre><code class="language-map">map global user y ':extra-yank-system&lt;ret&gt;'  -docstring 'yank to system clipboard'
map global user p ':extra-paste-system&lt;ret&gt;' -docstring 'paste selections from system clipboard'
</code></pre>
<h2>Bonus: some tools I’ve been making</h2>
<p>Because the UX in Kakoune is amazing, and because it composes so well, I have made a couple of binaries and Kakoune
commands to enhance the experience. So far (23rd December of 2023), I have wrote those</p>
<ul>
<li><a href="https://github.com/phaazon/kak-tree-sitter">kak-tree-sitter</a>, which provides <a href="https://tree-sitter.github.io/tree-sitter/">tree-sitter</a> support (so far, only semantic highlighting; semantic selections will
come later).</li>
<li><a href="https://github.com/phaazon/bookmarks.kak">bookmarks.kak</a>, a super small plugin I use to create persistent bookmarked locations (and list them in a dedicated
buffer).</li>
<li><a href="https://github.com/phaazon/git.kak">git.kak</a>, a super set of the (already great) Git integration in Kakoune. I for instance add a cursor-anchored overlay
showing git blame information.</li>
<li><a href="https://github.com/phaazon/hop.kak">hop.kak</a>, Hop brought to Kakoune. My implementation is by far <em>much</em> simpler than what I did in [hop.nvim] because I
read native Kakoune selection descriptions, instead of relying on an internal regex engine run on each line: it
composes so much better.</li>
<li><a href="https://github.com/phaazon/notes.kak">notes.kak</a>, one of my most useful plugin. It enhances Markdown with some features:
<ul>
<li>Lists marked <code>- TODO</code>, <code>- WIP</code>, <code>- DONE</code>, <code>- WONTDO</code>, <code>- IDEA</code>, etc. are highlighted with specific faces.</li>
<li>Todo items from the list above can be gathered in a buffer (similar to a grep buffer) to jump directly to the note
file containing them.</li>
<li>Open the journal of the day.</li>
<li>Open notes.</li>
<li>Search journals and notes.</li>
<li>Archive and list archives.</li>
<li>And much more.</li>
</ul>
</li>
<li><a href="https://github.com/phaazon/swiper.kak">swiper.kak</a>, my interpretation of the famous <a href="https://elpa.gnu.org/packages/swiper.html">swiper</a> emacs package; it allows to reduce a buffer by fuzzy searching
something in it, only showing the lines that matches, but showing them all at once (by removing the lines that don’t
match), and pressing <code>&lt;ret&gt;</code> jump to the line under the cursor (or <code>&lt;esc&gt;</code> goes back to where you were buffer). I
also included a <em>reduce</em> mode that helps with reducing grep buffers or any buffer that can be directly modified.</li>
</ul>
<p>All in all, I’m really happy with my current Kakoune setup, and as time passes, I realize it’s a nice <em>interactive
editing platform</em> so far. For instance, at work, I have started making a <code>k8s.kak</code> to highlight and run commands on
Kubernetes cluster from within Kakoune; and it works pretty well.</p>
<p>Have fun and keep hacking around!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 23 Dec 2023 14:15:00 GMT</pubDate></item><item><title>Never forget your git stashes again!</title><link>https://strongly-typed-thoughts.net/blog/reverse_prompt_git_stash</link><description><![CDATA[<p>It’s been a while I’m experiencing issues with <code>git stash</code>. If you don’t know that command yet,
<code>git stash</code> is used to move all the changes living in your <em>staging area</em> into a special place: the
<em>stash</em>.</p>
<p>The <em>stash</em> is a temporary area working like a stack. You can push changes onto it via <code>git stash</code>
or <code>git stash save</code>; you can pop changes from top with <code>git stash pop</code>. You can also apply a very
specific part of the stack with <code>git stash apply &lt;stash id&gt;</code>. Finally you can get the list of all
the stashes with <code>git stash list</code>.</p>
<p>We often use the <code>git stash</code> command to stash changes in order to make the working directory clear
again so that we can apply a patch, pull some changes, change branch, and so on. For those purposes,
the <em>stash</em> is pretty great.</p>
<p>However, I often forget about my stashes – I know I’m not the only one. Sometimes, I stash something
and go to cook something or just go out, and when I’m back again, I might have forgotten about what
I had stashed, especially if it was a very small change.</p>
<p>My current prompt for my shell, <a href="http://www.zsh.org/">zsh</a>, is in two parts. I set the <code>PS1</code>
environnment variable to set the regular prompt, and the <code>RPROMPT</code> environnment variable to set a
reversed prompt, starting from the right of the terminal. My reversed prompt just performs a <code>git</code>
command to check whether we’re actually in a <code>git</code> project, and get the current branch. Simple, but
nice.</p>
<p>I came up to the realization that I could use the exact same idea to know whether I have stashed
changes so that I never forget them! Here’s a screenshot to explain that:</p>
<p><img src="http://phaazon.net/pub/git_stash_shell.png" alt="" /></p>
<p>As you can see, my prompt now shows me how many stashed changes there are around!</p>
<h1>The code</h1>
<p>I share the code I wrote with you. Feel free to use it, modify it and share it as well!</p>
<pre><code># …

function gitPrompt() {
  # git current branch
  currentBranch=`git rev-parse --abbrev-ref HEAD 2&gt; /dev/null`
  if (($? == 0))
  then
    echo -n "%F{green}$currentBranch%f"
  fi

  # git stash
  stashNb=`git stash list 2&gt; /dev/null | wc -l`
  if [ "$stashNb" != "0" ]
  then
    echo -n " %F{blue}($stashNb)%f"
  fi

  echo ''
}

PS1="%F{red}%n%F{cyan}@%F{magenta}%M %F{cyan}%~ %F{yellow}%% %f"
RPROMPT='$(gitPrompt)'

# …
</code></pre>
<p>Have fun!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 16 Aug 2015 00:00:00 GMT</pubDate></item><item><title>Luminance – framebuffers and textures</title><link>https://strongly-typed-thoughts.net/blog/luminance_framebuffer_texture</link><description><![CDATA[<p>I’m happily surprised that so many <strong>Haskell</strong> people follow
<a href="https://github.com/phaazon/luminance">luminance</a>! First thing first, let’s tell
you about how it grows.</p>
<p>Well, pretty quickly! There’s – yet – no method to make actual renders, because
I’m still working on how to implement some stuff (I’ll detail that below), but
it’s going toward the right direction!</p>
<h1>Framebuffers</h1>
<p>Something that is almost done is the
<a href="https://www.opengl.org/wiki/Framebuffer_Object">framebuffer</a> part. The main
idea of <em>framebuffers</em> – in <strong>OpenGL</strong> – is supporting <em>offscreen renders</em>, so
that we can render to several framebuffers and combine them in several fancy
ways. Framebuffers are often bound <em>textures</em>, used to pass the rendered
information around, especially to <em>shaders</em>, or to get the pixels through
texture reads CPU-side.</p>
<p>The thing is… <strong>OpenGL</strong>’s <em>framebuffers</em> are tedious. You can have incomplete
framebuffers if you don’t attach textures with the right format, or to the wrong
attachment point. That’s why the <em>framebuffer</em> layer of <strong>luminance</strong> is there
to solve that.</p>
<p>In <strong>luminance</strong>, a <code>Framebuffer rw c d</code> is a framebuffer with two formats. A
<em>color</em> format, <code>c</code>, and a <em>depth</em> format, <code>d</code>. If <code>c = ()</code>, then no color will
be recorded. If <code>d = ()</code>, then no depth will be recorded. That enables the use
of <em>color-only</em> or <em>depth-only</em> renders, which are often optimized by GPU. It
also includes a <code>rw</code> type variable, which has the same role as for <code>Buffer</code>.
That is, you can have <em>read-only</em>, <em>write-only</em> or <em>read-write</em> framebuffers.</p>
<p>And of course, all those features – having a <em>write-only</em> <em>depth-only</em>
framebuffer for instance – are set through… <strong>types</strong>! And that’s what is so
cool about how things are handled in <strong>luminance</strong>. You just tell it what you
want, and it’ll create the required state and manage it for you GPU-side.</p>
<h2>Textures</h2>
<p>The format types are used to know which textures to create and how to attach
them internally. The textures are hidden from the interface so that you can’t
mess with them. I still need to find a way to provide some kind of access to the
information they hold, in order to use them in shaders for instance. I’d love to
provide some kind of <em>monoidal</em> properties between framebuffers – to mimick
<a href="https://hackage.haskell.org/package/gloss">gloss</a> <code>Monoid</code> instance for its
<a href="https://hackage.haskell.org/package/gloss-1.9.2.1/docs/Graphics-Gloss-Data-Picture.html#t:Picture">Picture</a>
type, basically.</p>
<p>You can create textures, of course, by using the <code>createTexture w h mipmaps</code>
function.  <code>w</code> is the <em>width</em>, <code>h</code> the <em>height</em> of the texture. <code>mipmaps</code> is the
number of <em>mipmaps</em> you want for the texture.</p>
<p>You can then upload <em>texels</em> to the texture through several functions. The
basic form is <code>uploadWhole tex autolvl texels</code>. It takes a <em>texture</em> <code>tex</code> and
the <code>texels</code> to upload to the whole texture region. It’s your responsibility to
ensure that you pass the correct number of texels. The <code>texels</code> are represented
with a polymorphic type. You’re not bound to any kind of textures. You can pass
a list of texels, a <code>Vector</code> of texels, or whatever you want, as long as it’s
<code>Foldable</code>.</p>
<p>It’s also possible to fill the whole texture with a single value. In <strong>OpenGL</strong>
slang, such an operation is often called <em>clearing</em> – clearing a <em>buffer</em>,
clearing a <em>texture</em>, clearing the <em>back buffer</em>, and so on. You can do that
with <code>fillWhole</code>.</p>
<p>There’re two over functions to work with subparts of textures, but it’s not
interesting for the purpose of that blog entry.</p>
<h2>Pixel format</h2>
<p>The cool thing is the fact I’ve unified pixel formats. <em>Textures</em> and
<em>framebuffers</em> share the same pixel format type (<code>Format t c</code>). Currently,
they’re all phantom types, but I might unify them further and use <code>DataKinds</code> to
promote them to the type-level. A format has two type variables, <code>t</code> and <code>c</code>.</p>
<p><code>t</code> is the underlying type. Currently, it can be either <code>Int32</code>, <code>Word32</code> or
<code>Float</code>. I might add support for <code>Double</code> as well later on.</p>
<p><code>c</code> is the channel type. There’re basically five channel types:</p>
<ul>
<li><code>CR r</code>, a red channel ;</li>
<li><code>CRG r g</code>, red and green channels ;</li>
<li><code>CRGB r g b</code>, red, green and blue channels ;</li>
<li><code>CRGBA r g b a</code>, red, green, blue and alpha channels ;</li>
<li><code>CDepth d</code>, a depth channel (special case of <code>CR</code>; for depths only).</li>
</ul>
<p>The type variables <code>r</code>, <code>g</code>, <code>b</code>, <code>a</code> and <code>d</code> represent <em>channel sizes</em>.
There’re currently three kind of <em>channel sizes</em>:</p>
<ul>
<li><code>C8</code>, for 8-bit ;</li>
<li><code>C16</code>, for 16-bit ;</li>
<li><code>C32</code>, for 32-bit.</li>
</ul>
<p>Then, <code>Format Float (CR C32)</code> is a red channel, 32-bit float – the <strong>OpenGL</strong>
equivalent is <code>R32F</code>. <code>Format Word32 (CRGB C8 C8 C16)</code> is a <em>RGB</em> channel with
red and green 8-bit unsigned integer channels and the blue one is a 16-bit
unsigned integer channel.</p>
<p>Of course, if a pixel format doesn’t exist on the <strong>OpenGL</strong> part, you won’t be
able to use it. Typeclasses are there to enforce the fact pixel format can be
represented on the <strong>OpenGL</strong> side.</p>
<h1>Next steps</h1>
<p>Currently, I’m working hard on how to represent vertex formats. That’s not a
trivial task, because we can send vertices to <strong>OpenGL</strong> as
interleaved – or not – arrays. I’m trying to design something elegant and safe,
and I’ll keep you informed when I finally get something. I’ll need to find an
interface for the actual render command, and I should be able to release
something we can actually use!</p>
<p>By the way, some people already tried it (Git HEAD), and that’s amazing! I’ve
created the <code>unstable</code> branch so that I can push unstable things, and keep the
master branch as clean as possible.</p>
<p>Keep the vibe, and have fun hacking around!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 01 Aug 2015 00:00:00 GMT</pubDate></item><item><title>Contravariance and luminance to add safety to uniforms</title><link>https://strongly-typed-thoughts.net/blog/luminance_uniform_contravariance</link><description><![CDATA[<p>It’s been a few days I haven’t posted about <a href="https://github.com/phaazon/luminance">luminance</a>. I’m
on holidays, thus I can’t be as involved in the development of the graphics framework as I’m used to
on a daily basis. Although I’ve been producing less in the past few days, I’ve been actively
thinking about something very important: <a href="https://www.opengl.org/wiki/Uniform_%28GLSL%29">uniform</a>.</p>
<h1>What people usually do</h1>
<p>Uniforms are a way to pass data to shaders. I won’t talk about <em>uniform blocks</em> nor <em>uniform
buffers</em> – I’ll make a dedicated post for that purpose. The common OpenGL uniform flow is the
following:</p>
<ol>
<li>you ask OpenGL to retrieve the location of a GLSL uniform through the function
<code>glGetUniformLocation</code>, or you can use an explicit location if you want to handle the semantics
on your own ;</li>
<li>you use that location, the identifier of your shader program and send the actual values with
the proper <code>glProgramUniform</code>.</li>
</ol>
<p>You typically don’t retrieve the location each time you need to send values to the GPU – you only
retrieve them once, while initializing.</p>
<p>The first thing to make uniforms more elegant and safer is to provide a typeclass to provide a
shared interface. Instead of using several functions for each type of uniform – <code>glProgramUniform1i</code>
for <code>Int32</code>, <code>glProgramUniform1f</code> for <code>Float</code> and so on – we can just provide a function that will
call the right OpenGL function for the type:</p>
<pre><code class="language-haskell">class Uniform a where
  sendUniform :: GLuint -&gt; GLint -&gt; a -&gt; IO ()

instance Uniform Int32 where
  sendUniform = glProgramUniform1i

instance Uniform Float where
  sendUniform = glProgramUniform1f

-- and so on…
</code></pre>
<p>That’s the first step, and I think everyone should do that. However, that way of doing has several
drawbacks:</p>
<ul>
<li>it still relies on side-effects; that is, we can call <code>sendUniform</code> pretty much everywhere ;</li>
<li>imagine we have a shader program that <strong>requires</strong> several uniforms to be passed each time we
draw something; what happens if we forget to call a <code>sendUniform</code>? If we haven’t sent the
uniform yet, we might have an undefined behavior. If we already have, we will <em>override</em> all
future draws with that value, which is very wrong… ;</li>
<li>with that way of representing uniforms, we have a very imperative interface; we can have a more
composable and pure approach than that, hence enabling us to gain in power and flexibility.</li>
</ul>
<h1>What luminance used to do</h1>
<p>In my <a href="https://github.com/phaazon/luminance">luminance</a> package, I used to represent uniforms as
values.</p>
<pre><code class="language-haskell">newtype U a = U { runU :: a -&gt; IO () }
</code></pre>
<p>We can then alter the <code>Uniform</code> typeclass to make it simpler:</p>
<pre><code class="language-haskell">class Uniform a where
  toU :: GLuint -&gt; GLint -&gt; U a

instance Uniform Int32 where
  toU prog l = U $ glProgramUniform1i prog l

instance Uniform Float where
  toU prog l = U $ glProgramUniform1f prog l
</code></pre>
<p>We also have a pure interface now. I used to provide another type, <code>Uniformed</code>, to be able to
<em>send</em> uniforms without exposing <code>IO</code>, and an operator to accumulate uniforms settings, <code>(@=)</code>:</p>
<pre><code class="language-haskell">newtype Uniformed a = Uniformed { runUniformed :: IO a } deriving (Applicative,Functor,Monad)

(@=) :: U a -&gt; a -&gt; Uniformed ()
U f @= a = Uniformed $ f a
</code></pre>
<p>Pretty simple.</p>
<h1>The new uniform interface</h1>
<p>The problem with that is that we still have the completion problem and the side-effects, because we
just wrap them without adding anything special – <code>Uniformed</code> is isomorphic to <code>IO</code>. We have no way
to create a type and ensure that <em>all</em> uniforms have been sent down to the GPU…</p>
<h2>Contravariance to save us!</h2>
<p>If you’re an advanced <strong>Haskell</strong> programmer, you might have noticed something very interesting
about our <code>U</code> type. It’s contravariant in its argument. What’s cool about that is that we could then
create new uniform types – new <code>U</code> – by contramapping over those types! That means we can enrich
the scope of the hardcoded <code>Uniform</code> instances, because the single way we have to get a <code>U</code> is
to use <code>Uniform.toU</code>. With contravariance, we can – in theory – extend those types to <strong>all types</strong>.</p>
<p>Sounds handy eh? First thing first, contravariant functor. A contravariant functor is a functor that
flips the direction of the morphism:</p>
<pre><code class="language-haskell">class Contravariant f where
  contramap :: (a -&gt; b) -&gt; f b -&gt; f a
  (&gt;$) :: b -&gt; f b -&gt; f a
</code></pre>
<p><code>contramap</code> is the <em>contravariant</em> version of <code>fmap</code> and <code>(&gt;$)</code> is the contravariant version of
<code>(&lt;$)</code>. If you’re not used to contravariance or if it’s the first time you see such a type
signature, it might seem confusing or even <strong>magic</strong>. Well, that’s the mathematic magic in the
place! But you’ll see just below that there’s no magic no trick in the implementation.</p>
<p>Because <code>U</code> is contravariant in its argument, we can define a <code>Contravariant</code> instance:</p>
<pre><code class="language-haskell">instance Contravariant U where
  contramap f u = U $ runU u . f
</code></pre>
<p>As you can see, nothing tricky here. We just apply the <code>(a -&gt; b)</code> function on the input of the
resulting <code>U a</code> so that we can pass it to <code>u</code>, and we just <code>runU</code> the whole thing.</p>
<p>A few friends of mine – not <strong>Haskeller</strong> though – told me things like <em>“That’s just theory
bullshit, no one needs to know what a contravariant thingy stuff is!”</em>. Well, here’s an example:</p>
<pre><code class="language-haskell">newtype Color = Color {
    colorName :: String
  , colorValue :: (Float,Float,Float,Float)
  }
</code></pre>
<p>Even though we have an instance of <code>Uniform</code> for <code>(Float,Float,Float,Float)</code>, there will never be an
instance of <code>Uniform</code> for <code>Color</code>, so we can’t have a <code>U Color</code>… Or can we?</p>
<pre><code class="language-haskell">uColor = contramap colorValue float4U
</code></pre>
<p>The type of <code>uColor</code> is… <code>U Color</code>! That works because contravariance enabled us to <em>adapt</em> the
<code>Color</code> structure so that we end up on <code>(Float,Float,Float,Float)</code>. The contravariance property is
then a very great ally in such situations!</p>
<h2>More contravariance</h2>
<p>We can even dig in deeper! Something cool would be to do the same thing, but for several fields.
Imagine a mouse:</p>
<pre><code class="language-haskell">data Mouse = Mouse {
    mouseX :: Float
  , mouseY :: Float
  }
</code></pre>
<p>We’d like to find a cool way to have <code>U Mouse</code>, so that we can send the mouse cursor to shaders.
We’d like to contramap over <code>mouseX</code> and <code>mouseY</code>. A bit like with <code>Functor</code> + <code>Applicative</code>:</p>
<pre><code class="language-haskell">getMouseX :: IO Float
getMouseY :: IO Float

getMouse :: IO Mouse
getMouse = Mouse &lt;$&gt; getMouseX &lt;*&gt; getMouseY
</code></pre>
<p>We could have the same thing for contravariance… And guess what. That exists, and that’s called
<strong>divisible contravariant functors</strong>! A <code>Divisible</code> contravariant functor is the exact contravariant
version of <code>Applicative</code>!</p>
<pre><code class="language-haskell">class (Contravariant f) =&gt; Divisible f where
  divide :: (a -&gt; (b,c)) -&gt; f b -&gt; f c -&gt; f a
  conquer :: f a
</code></pre>
<p><code>divide</code> is the contravariant version of <code>(&lt;*&gt;)</code> and <code>conquer</code> is the contravariant version of
<code>pure</code>. You know that <code>pure</code>’s type is <code>a -&gt; f a</code>, which is isomorphic to <code>(() -&gt; a) -&gt; f a</code>. Take
the contravariant version of <code>(() -&gt; a) -&gt; f a</code>, you end up with <code>(a -&gt; ()) -&gt; f a</code>. <code>(a -&gt; ())</code> is
isomorphic to <code>()</code>, so we can simplify the whole thing to <code>f a</code>. Here you have <code>conquer</code>. <em>Thank you
to Edward Kmett for helping me understand that!</em></p>
<p>Let’s see how we can implement <code>Divisible</code> for <code>U</code>!</p>
<pre><code class="language-haskell">instance Divisible U where
  divide f p q = U $ \a -&gt; do
    let (b,c) = f a
    runU p b
    runU q c
  conquer = U . const $ pure ()
</code></pre>
<p>And now let’s use it to get a <code>U Mouse</code>!</p>
<pre><code class="language-haskell">let uMouse = divide (\(Mouse mx my) -&gt; (mx,my)) mouseXU mouseYU
</code></pre>
<p>And here we have <code>uMouse :: U Mouse</code>! As you can see, if you have several uniforms – for each fields
of the type, you can <code>divide</code> your type and map all fields to the uniforms by applying several times
<code>divide</code>.</p>
<p>The current implementation is almost the one shown here. There’s also a <code>Decidable</code> instance, but
I won’t talk about that for now.</p>
<p>The cool thing about that is that I can lose the <code>Uniformed</code> monadic type and rely only on <code>U</code>.
Thanks to the <code>Divisible</code> typeclass, we have completion, and we can’t override future uniforms then!</p>
<hr />
<p>I hope you’ve learnt something cool and useful through this. Keep in mind that category abstractions
<strong>are powerful</strong> and are useful in some contexts.</p>
<p>Keep hacking around, keep being curious. A <strong>Haskeller</strong> never stops learning! And that’s what so
cool about <strong>Haskell</strong>! Keep the vibe, and see you another luminance post soon!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 23 Aug 2015 00:00:00 GMT</pubDate></item><item><title>Thoughts about software meta-design</title><link>https://strongly-typed-thoughts.net/blog/software_meta_design</link><description><![CDATA[<p>I’ve been thinking of writing such an article for a while. A few weeks ago, I got contacted by
people who wanted to know more about my experience with
<a href="https://github.com/phaazon/luminance">luminance</a> so that they can have more hindsight about their
own APIs and products.</p>
<p>I came to the realization that I could write a blog entry to discuss designs decisions and, at some
extent, what a good design entails. Keep in mind it’s only personal thoughts and that I won’t talk
for someone else.</p>
<h1>Elegancy</h1>
<p>I love mathematics because they’re elegant. Elegancy implies several traits, among <em>simplicity</em>,
<em>flexibility</em> and <em>transparency</em>. They solve problems with very nice abstractions. In
mathematics, we have a concept that is – astonishingly – not very spread and barely known outside
of math geeks circles: <em>free</em> objects.</p>
<p>The concept of <em>free</em> is a bit overwhelming at first, because people are used to put labels and
examples on everything. For instance, if I say that an object is <em>free</em>, you might already have
associated some kind of <em>lock</em> to that object, so that you can get why it’s <em>free</em>. But we’re
mistaken. We don’t need <em>locks</em> to define what <em>free</em> implies. In mathematic, a <em>free</em> object is an
object that can’t be defined in terms of others. It’s a bit like a <em>core</em> object. It’s <em>free</em>
because it can be there, no matter what other objects are around. It has no dependency, it doesn’t
require no other interaction. You can also say that such an object is <em>free</em> of extra features that
wouldn’t be linked to its nature.</p>
<p>This <em>free</em> property is a very interesting property in mathematics, because it’s surprisingly
simple! We can leverage that mathematic abstraction to software design. I like keeping my softwares
as much <em>free</em> as possible. That is – with a more human language to say it – constraining them to
keep low responsibilities about what they’re doing.</p>
<h1>Responsibility domains</h1>
<p>The important thing to keep in mind is that you should, at first, define what the responsibility
domain is all about. Let’s say you’d like to create a library to implement audio effects, like the
<a href="https://en.wikipedia.org/wiki/Doppler_effect">Doppler effect</a> – that effect actually exists for
any kind of wave, but it’s interesting to synthetize it for a sound-related application. If you end
up writing functions or routines to play sound or to load audio samples, you’re already doing it
wrong! You’d have violated your reponsibility domain, which is, <em>“audio effects”</em>. Unfortunately,
<strong>a lot</strong> of libraries do that. Adding extra stuff – and sometimes, worse; relying on them!</p>
<p>A lot of people tend to disagree with that – or they just <em>ignore</em> / <em>don’t know</em>. There’re plenty
of examples of libraries and softwares that can do everything and nothing. For instance, take
<a href="http://www.qt.io/">Qt</a> – pronounce <em>cute</em> or <em>cutie</em>. At first, <em>Qt</em> is a library and an API to
build up <em>GUIs</em> – Graphical User Interfaces – and handle windows, events and so on. Let’s have a
look at the documentation of modules, <a href="http://doc.qt.io/qt-5/qtmodules.html">here</a>.</p>
<p>You can see how the responsibility domain is <strong>huge</strong>! GUI, radio, audio, video, camera, network,
database, printing, concurrency and multithreading… <em>Qt</em> isn’t a library anymore; it’s a whole new
language!</p>
<p>People tend to like that. <em>“Yeah, I just have to use Qt, and I can do everything!”</em>. Well, that’s a
point. But you can also think it another way. Qt is a very massive “library” you’ll spend hours
reading the documentation and will use a lot of different classes / functions from different
aspects. That doesn’t compose at all. What happens when you want to – or when you don’t have the
choice? – use something else? For instance, if you want to use a smaller–but–dedicated threading
library? What happens if you want to use a database service you wrote or that you know it’s great?
Do you wipeout your Qt use? Do you… try to make both work in harmony? If so, do you have to write a
lot of boilerplate code? Do you forget about those technologies and fallback on Qt? Do the concepts
map to each others?</p>
<p>The problem with massive libraries is the tight bound it creates between the libraries and the
developers. It’s very hard with such libraries to say that you can use it whenever you want because
you perfectly know them. You could even just need a few things from it; like, the <em>SQL</em> part. You’ll
then have to install a lot of code you’ll perhaps use 10% of.</p>
<h1>KISS</h1>
<p>I love how the <em>free</em> objects from mathematics can be leveraged to build simpler libraries here. The
good part about <em>free</em> objects is the fact that they don’t have any extra features embedded. That’s
very cool, because thanks to that, you can reason in terms of such objects <em>as-is</em>. For instance,
<a href="http://www.openal.org">OpenAL</a> is a very <em>free</em> audio library. Its responsibility domain is to be
able to play sound and apply simple effects on them – raw and primary effects. You won’t find
anything to load music from files nor samples. And that’s very nice, because the API is <strong>small</strong>,
<strong>simple</strong> and <strong>straight-forward</strong>.</p>
<p>Those adjectives are the base of the <a href="https://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>.
The ideas behind <em>KISS</em> are simple: keep it simple and stupid. Keep it simple, because the simpler
the better. A too complex architecture is bloated and ends up unmaintainable. Simplicity implies
elegancy and then, flexibility and composability.</p>
<p>That’s why I think a good architecture is a small – in terms of responsibility – and simple one. If
you need complexity, that’s because your responsibility domain is already a bit more complex than
the common ones. And even though the design is complex for someone outside of the domain, for the
domain itself, it should stay simple and as most straight-forward as possible.</p>
<h1>API</h1>
<p>I think a good API design is to pick a domain, and stick to it. Whatever extra features you won’t
provide, you’ll be able to create other libraries to add those features. Because those features will
also be <em>free</em>, they will be useful in other projects that you don’t even have any idea they exist!
That’s a very cool aspect of <em>free</em> objects!</p>
<p>There’s also a case in which you have to make sacrifices – and crucial choices. For instance,
event-driven programming can be implemented via several techniques. A popular one in the functional
programming world nowadays is <a href="https://wiki.haskell.org/Functional_Reactive_Programming">FRP</a>. Such
a library is an <em>architectural codebase</em>. If you end up adding <em>FRP</em>-related code lines in your
networking-oriented library, you might be doing it wrong. Because, eh, what if I just want to use
imperative event-driven idioms, like <a href="https://en.wikipedia.org/wiki/Observer_pattern">observers</a>?
You shouldn’t integrate such architectural design choices in specific libraries. Keep them <em>free</em>,
so that everyone can quickly learn them, and enjoy them!</p>
<p>I like to see good-designed libraries as a set of very powerful, tiny tools I can compose and move
around freely. If a tool gets broken or if it has wrong performances, I can switch to a new one or
write my very own. Achieving such a flexibility without following the <em>KISS principle</em> is harder or
may be impossible to reach.</p>
<p>So, in my opinion, we should keep things simple and stupid. They’re simpler to reason about, they
compose and scale greatly and they of course are easier to maintain. Compose them with architectural
or whatever designs in the actual final executable project. Don’t make premature important choices!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 13 Sep 2015 00:00:00 GMT</pubDate></item><item><title>My Corne keyboard</title><link>https://strongly-typed-thoughts.net/blog/crkbd</link><description><![CDATA[<p>I mostly talk about software here, but I think I want to stray away a bit from the regular path and talk about
keyboards. I’m a really passionate keyboard guy, having built three 60% bamboo ones, bought a <a href="https://www.zsa.io/voyager">ZSA Voyager</a>
last year, and lately made my own <a href="https://github.com/foostan/crkbd">Corne</a> by soldering <em>everything</em>. It was a wild experience — and I did
broke some tracks; RIP LEDs 😥 — but overall I’m really satisfied with the end result.</p>
<img src=https://strongly-typed-thoughts.net/media/uploads/crkbd.jpg width=800 />
<p>I want to talk about it, because it’s not really the type of keyboard you see quite often, and I guess it
comes with many questions.</p>
<h1 id="the-corne-a-42-key-beauty"><a href="#the-corne-a-42-key-beauty">#</a> The Corne, a 42-key beauty</h1>
<!--toc:start-->
<ul>
<li><a href="#the-corne-a-42-key-beauty">The Corne, a 42-key beauty</a>
<ul>
<li><a href="#layers-and-mod-tap">Layers and mod-tap</a>
<ul>
<li><a href="#the-base-layer">The base layer</a></li>
<li><a href="#the-symbols-and-numbers-layer">The symbols and numbers layer</a></li>
<li><a href="#the-navmultimedia-layer">The nav/multimedia layer</a></li>
<li><a href="#the-gaming-layer">The gaming layer</a></li>
</ul>
</li>
<li><a href="#home-row-mods-a-false-good-idea">Home-Row-Mods: a false good idea?</a>
<ul>
<li><a href="#first-issue-shifts-are-too-important">First issue: shifts are too important</a></li>
</ul>
</li>
<li><a href="#second-issue-latency">Second issue: latency</a></li>
<li><a href="#whats-next">What’s next</a></li>
</ul>
</li>
</ul>
<!--toc:end-->
<p>The first thing you might have noticed is that it’s a <em>split keyboard</em>. There are a lot of litterature lying
around explaining why it’s actually a better setup than a single piece of hardware — better shoulds position,
better wrists alignment, etc. Plus, because you have the choice to put both sides close to each other, or
far apart, it can easily be adapted to your morphology.</p>
<p>Secondly, it’s easy to see that the Corne has <em>only</em> 42 keys. That comes with a massive advantage to me: when
your fingers are resting on the home row, <strong>every key on the main grid</strong> (i.e. excluding thumb keys) are only
<strong>one key away</strong>. That minimizes <em>a lot</em> the movements you will be doing. For instance, reaching the key on
the top-left corner doesn’t require any kind of stretch of my hand, nor rotation. It’s already an improvement
compared to the Voyager, which has an additional row above; reaching its top-left key does either make you
extend your pinky in a non-natural way, or does make your wrist slightly rotate towards the keyboard, which is
not comfortable on the long run.</p>
<p>So every key is reachable by just slightly moving the fingers. It’s a fantastic feeling, especially because of
the column staggered keys, which follows the curve of the hands. I’ve been touch typing for a very long time
now (&gt; 10 years), so switching to that keyboard wasn’t <em>that hard</em> — especially because I had switched to the
Voyager prior to this — but it would a lie to tell it’s natural. It takes some time and practice. If you have
never learned to touch type properly, you might suffer a lot. I’ve been touch typing in <a href="https://bepo.fr/wiki/Accueil">bépo</a>, so it took me
a couple days to be fully comfortable with the Voyager and the Corne.</p>
<p>Finally, I want to talk about the thumb keys, because it’s going to be a central topic of this article. The
Corne has three on each side, for a total of six, which is still two more than the Voyager, which has two
on each side for a total of four. Thumb keys are a way to leverage the fact that on regular keyboards, we
barely use them — on my 60%, I used the left thumb for the space bar, and the right thumb for alt-gr, but
that’s pretty much it, besides some occasional modifiers.</p>
<p>So that’s it for the introduction. Let’s dig into my setup.</p>
<h2 id="layers-and-mod-tap"><a href="#layers-and-mod-tap">#</a> Layers and mod-tap</h2>
<p>Because of the few amount of keys, the whole idea is that you want to create <em>layers</em>, and dedicate some keys
to activate modifiers and layers. Let me explain.</p>
<h3 id="the-base-layer"><a href="#the-base-layer">#</a> The base layer</h3>
<p>The <em>base</em> layer is usually full of letters, what you will be typing the most. In my case, this is my base
layer — <code>·</code> means that nothing is bound there:</p>
<pre><code class="language-plaintext">T b é p o è      ^ v d l j z
E a u i e ,      c t s r n m
% à y x . k      ' q g h f w
      1 Z ·      B R ·
</code></pre>
<p>Here, <code>T</code> stands for tab, <code>E</code> is escape, <code>Z</code> is space, <code>S</code> is left shift, <code>B</code> is a backspace, <code>R</code> is return.
Some keys have a <em>hold</em> action, which is what we usually call <em>mod-tap</em>: holding the key down and pressing
another key does something different. Even though some keys do not have any tap action (for instance, the
left inner thumb key or the right outer thumb key), I can still hold them to have a specific action.
Here’s the base layer again, but with the actions when keys are held:</p>
<pre><code class="language-plaintext">M · · · · ·      A · · · · ·
· · · · · ·      · · · · · ·
A C · · · ·      · · · · · ·
      2 · S      S 1 G
</code></pre>
<p><code>M</code> is meta, <code>A</code> is alt, <code>C</code> is control, <code>S</code> is shift, and <code>G</code> is alt-gr. That latter modifier is especially
important in bépo, as it opens up many many characters that the bépo layout has to offer — for instance, <code>…</code>
is <code>alt-gr + .</code>, <code>’</code> is <code>alt-gr +</code>, <code>œ</code> is <code>alt-gr + o</code>, etc.</p>
<p>The <code>1</code> and <code>2</code> are not typos, and I’ll talk about it pretty soon.</p>
<p>You might have noticed that I have mirrored the shifts on the inner thumb keys. This is important to me,
because shift is actually a very important modifier: it’s by far the modifier I type the most, and I need to
be able to interleave normal typing on the main grid with shifting modifiers. Think about the sentences you’re
currently reading, and where I might have pressed shift. « I » in the middle of a sentence requires a shifted
« i ». Every start of sentence requires a shifted letter.</p>
<p>Now let’s see how I type numbers and symbols.</p>
<h3 id="the-symbols-and-numbers-layer"><a href="#the-symbols-and-numbers-layer">#</a> The symbols and numbers layer</h3>
<p>I used to have this layer mirrored on the outer thumb keys, but such keys are not really practical to hit
while fast typing — I usually type around 145 words per minute, so I’m pretty fast. Today, that layer is
accessed via the <code>1</code> layer key on my base layer — middle right thumb key. If I just tap it — basically press
and release without touching any other key — it acts as return. If I hold it, it activates the symbols and
numbers layer.</p>
<p>Let’s see that layer:</p>
<pre><code class="language-plaintext">0 1 2 3 4 5      6 7 8 9 ° ·
$ " &lt; ( ) &gt;      @ + - / * =
# | &amp; [ ] ~      — { } « » ç
      · _ S      · □ ·
</code></pre>
<p>The <code>□</code> is the held key there. So, if I want to type <code>()</code>, I can simply hold the right middle thumb key and
press <code>ie</code> to yield <code>()</code>. How to do <code>-&gt;</code>? Easy: press the layer key, and <code>s,</code>. The placement of the keys
are easy to remember.</p>
<blockquote>
<p>One that is very important to me is <code>_</code>: that one is usually accessed via <code>alt-gr + &lt;space&gt;</code>, but because
my alt-gr is on the right outer thumb key, it’s not super practical to type. However, I also realize I don’t
type underscore that often, and that I might put backspace there instead. This is still something I’m
figuring out.</p>
</blockquote>
<p>This is a pretty fantastic layer, because everything is really easy to activate. The weirdest key is
definitely <code>—</code>, since the right index and thumb fingers are super close to each other — but it’s a fair
tradeoff as I very rarely type this key, but still require it.</p>
<blockquote>
<p>The <code>ç</code> is an anomaly due to bépo v1.0, which doesn’t treat <code>alt-gr + c</code> as <code>ç</code>, but as <code>©</code>, which I do not
really care. I need to switch to bépo v1.1 / AFNOR, which comes with issues with <code>'</code> vs. <code>’</code>; yes, French
is hard!</p>
</blockquote>
<p>You can see that I moved <code>0</code> on the left side; this is to prevent having to press it on the right side with a
weird <em>crab hand</em>; <code>9</code> is fine, but <code>0</code> would be too weird on the right side.</p>
<h3 id="the-navmultimedia-layer"><a href="#the-navmultimedia-layer">#</a> The nav/multimedia layer</h3>
<p>Next up is my navigation layer, accessible with the <code>2</code> key.</p>
<p>That layer is important for many different softwares, including shells, chat
applications, etc. It’s the layer that holds arrow keys, page up / down, home / end, etc. Because it’s also
a pretty empty layer, I hijack it to also hold useful multimedia keys, such as volume up, down, mute, and
the print key:</p>
<pre><code class="language-plaintext">3 · · · · VU      P H U E · ·
· · · · · VD      l d u r · ·
· · · · · VM      · · D · · ·
      · · ·       S · ·
</code></pre>
<p><code>3</code> is my gaming layer goto-key (more on that later), <code>VU</code>, <code>VD</code> and <code>VM</code> are volume up, volume down and
volume mute; <code>P</code> is the print key, <code>H</code> and <code>E</code> are home and end keys, <code>U</code> and <code>D</code> are page up / down, and
<code>l</code>, <code>d</code>, <code>u</code> and <code>r</code> are the left, down, up and right arrow keys.</p>
<blockquote>
<p>Yes, the volume keys are not optimally placed, and I might move them on the right side eventually. The
reason is that since the nav key is pressed on the left outer thumb key, and is not practical to type keys
on the left side of the keyboard.</p>
</blockquote>
<p>Alright, now let’s see the last layer I have defined:</p>
<h3 id="the-gaming-layer"><a href="#the-gaming-layer">#</a> The gaming layer</h3>
<p>The gaming layere is a bit weird and heavily optimized towards the games I play — mostly, <em>Quake</em>, or games
with a need of special keys all around my movement keys:</p>
<pre><code class="language-plaintext">T è b é p o      · · · · · 0
E , a u i e      · · · · · ·
%   à y x .      · · · · · ·
      k Z S      · · ·
</code></pre>
<p><code>0</code> here goes back to the base layer. As you can see, the layout is <em>weird</em>, but the idea is that it will make
most games work out of the box with ASDF key, but I shift them by 1 column to have the direction keys right
under the fingers without moving from the left home row. I don’t think there’s anything to debate here, it’s
just a shifted base layer.</p>
<h2 id="home-row-mods-a-false-good-idea"><a href="#home-row-mods-a-false-good-idea">#</a> Home-Row-Mods: a false good idea?</h2>
<p>Something that I wondered a lot is whether I want to use HRMs. HRMs assigns hold-actions on the four keys of
the resting home row position to control, alt, shift and meta, on each side of the keyboard. That allows to
have access to modifiers without moving your fingers. This, basically:</p>
<pre><code class="language-plaintext">· · · · · ·      · · · · · ·
· M A C S ·      · S C A M ·
· · · · · ·      · · · · · ·
      · · ·      · · ·
</code></pre>
<p>I <em>is smart</em>, but not <em>practical</em>, at least not to me. The idea is that it relies <em>heavily</em> on how it’s
implemented. I use <a href="https://qmk.fm/">QMK</a> to build my firmwares, and when you use HRMs, there is a setting you cannot really
do without: <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a>. In short, <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a> changes
the behiavor of mod-tap keys in order to active the hold action more often. By default, to get the hold action
of a key, you have to press it for more than <code>TAPPING_TERM</code> — usually set to <em>200ms</em>. With <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a>,
the hold action can be triggered sooner by pressing the mod-tap key and pressing and releasing an other key,
while still holding the mod-tap key. Imagine a mod-tap key that prints <code>t</code> if tapped, but right shift if
held. With <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a>, you can trigger the hold action without having to wait for <code>TAPPING_TERM</code>:</p>
<ol>
<li>Press down the <code>t</code> key.</li>
<li>Press down the <code>a</code> key for instance.</li>
<li>Release the <code>a</code> key.</li>
<li>You get <code>A</code> printed.</li>
<li>Release the <code>t</code> key.</li>
</ol>
<p>This is <strong>required</strong> if you want to use HRMs and still trigger hold actions without having to wait for
<code>TAPPING_TERM</code>. The other mode — which I use, and I’ll explain why — is named <a href="https://docs.qmk.fm/tap_hold#hold-on-other-key-press">hold-on-other-key-presses</a>,
which changes the behavior of mod-tap keys like so:</p>
<ol>
<li>Press down the <code>t</code> key.</li>
<li>Press down the <code>a</code> key.</li>
<li><code>A</code> is printed.</li>
<li>Release the <code>t</code> key.</li>
<li>Release the <code>a</code> key.</li>
</ol>
<p>This is usually how I type when typing fast, so that mode is really horrible as it generates <em>tons</em> of
misfires with HRMs. However, it’s the mode to use for mod-taps set on keys you rarely use doing rollovers,
such as my backspace key which is also my right shift: I usually tap it once or twice, without holding on any
other key. Whenever I press it and hit another key, 100% of the time, I wanted a shifted key.</p>
<p>So having the shifts on the thumbs and <a href="https://docs.qmk.fm/tap_hold#hold-on-other-key-press">hold-on-other-key-presses</a> allows me to shift keys immediately, which
is really important while shifting keys in the middle of sentence or words.</p>
<p>But there’s more. I really tried HRMs — switched to <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a>, and removed the shifts from the thumbs.
I basically have two big issues with it.</p>
<h3 id="first-issue-shifts-are-too-important"><a href="#first-issue-shifts-are-too-important">#</a> First issue: shifts are too important</h3>
<p>Because shifts are <em>really</em> special and important keys, having them on the home row just gets in the way too
much. You don’t realize it until you have to type complex sentences with tons of shifting, but I realized that
I often start shifting a character, and my fingers are already moving to the next digram or trigram. Having
two fingers forced to ping-pong between shifts, and because you cannot press them quickly (remember:
<a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a> requires you to press a key, and press and release another key before releasing that first
key first to get she hold action! this is not how I fast touch type, so I really feel slowed down with this
setup).</p>
<p>This issue also outlines a misconception — or a paradox, pick the word that pleases you the most there —
about symmetrical shifts. Whenever I need to shift a single character, I will pick the shift key on the
opposite hand. For instance, « I » is an <code>i</code>, which I type on the left side on the keyboard, and thus I shift
it mith the right middle thumb key held: « I ». <em>However</em>, some words sometimes require to shift a couple
keys one after the other. For instance, there’s a city in France called Anet. Let’s yell at that city, for
whatever reason: ANET!</p>
<p>The way I type ANET is by following the opposite-hand rule I mentioned above <em>for the first letter</em>, but then
I just keep the shift key held and finish the rest of the word. It looks like this:</p>
<ol>
<li>Press the right middle thumb key.</li>
<li>Press <code>a</code>.</li>
<li>Press <code>n</code>.</li>
<li>Press <code>e</code>.</li>
<li>Press <code>t</code>.</li>
</ol>
<p>Now, with HRMs, you cannot do that, because every letter of anet alternates between sides, this is what it
would look like:</p>
<ol>
<li>Press <code>t</code>.</li>
<li>Press <code>a</code>.</li>
<li>Release <code>a</code>.</li>
<li>Release <code>t</code>.</li>
<li>Press <code>e</code>.</li>
<li>Press <code>n</code>.</li>
<li>Release <code>n</code>.</li>
<li>Release <code>e</code>.</li>
<li>Press <code>t</code>.</li>
<li>Press <code>e</code>.</li>
<li>Release <code>e</code>.</li>
<li>Release <code>t</code>.</li>
<li>Press <code>e</code>.</li>
<li>Press <code>t</code>.</li>
<li>Release <code>t</code>.</li>
<li>Release <code>e</code>.</li>
</ol>
<p>Yeah. It’s crazy. And here, I really took the worst example, but in practice, that kind of alternating pattern
happens more often than you’d think. For this special reason, I think shifts should be treated as a special
key, and that HRMs should not include shifts.</p>
<h2 id="second-issue-latency"><a href="#second-issue-latency">#</a> Second issue: latency</h2>
<p>So this one is probably the most infamous one coming with HRMs, and either you just don’t care, or you’re like
me and can’t stand it. I’ve heard and read people stating they do not feel it, but on my side, it’s very
similar as playing at 30 FPS vs. playing at 244 FPS: I just do not comprehend how someone cannot feel
the difference.</p>
<p>What happens with HRMs introducing latency is easy to understand, and it’s about the taps, not the hold
action. With <a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a>, as seen above, if you press and release another key while holding down a
mod-tap key, you will get the hold action. If you fast type, and have the interleaving of press / release, you
will get taps, which is fine, <em>but</em>. Because they mod-tap key might be a shift key if
hold, the firmware <strong>must</strong> wait until the end of the tapping period, or that the key is released to actually
consider it a tap. What it means if that, if you press a key and release it, the associated character will
not immediately appear on screen: it will take the time between the moment you press the key and the moment
you release it. The character will be printed on screen when you release the key.</p>
<p>You can state that the difference in time between a <em>press</em> and the associated <em>release</em> is negligeable for
taps, right? <em>Right?</em> Well, not really. Even when I type really fast, I cannot easily go under <strong>60ms</strong> on
single taps, and rarely under <strong>40ms</strong> for super fast burts.</p>
<p>A simple and KISS way to check this out: open your web browser console, and add this JavaScript to any page:</p>
<pre><code class="language-javascript">document.onkeypress = () =&gt; { down = window.performance.now(); }
document.onkeyup = () =&gt; { console.log(window.performance.now() - down); }
</code></pre>
<p>Yeah it’s terrible code but who cares for such a test. Just simply tap a single key the way you normally type.
This is just there to show you that with HRMs, you will get most of the time around 60ms of latency if you
are a fast typer, probably much more for most of the people.</p>
<p>Just for comparison, for a game to run at 30 FPS, it must compute every frame with a maximum render time
boundary of <code>1/30 ~= 33ms</code>. If a game were taking 70ms to render each frame, it would run at 14 FPS. So this
is akin to playing a game at full FPS, like 240 FPS, but having your mouse and keyboard input at 14 Hz. I
don’t think you’d like that. So <strong>yes, HRMs introduce a very noticeable lag</strong>, and I cannot bear it. It feels
terrible. Something that people also seem to ignore is that the human brain is pretty good at making
correlation between a <em>sensation</em> and what they eyes see. For instance, if you have ever watched a movie with
out-of-sync video and audio, you might have realized how quickly you can notice that something is wrong: just
having a couple milliseconds of delay between <em>what you see</em> and <em>what you hear</em> already feels bad.</p>
<blockquote>
<p>Fun fact: because the speed of light is the top-speed, and the speed of sound is pretty slow compared to
light, desynchronizing audio and video will not feed as bad if the audio arrives <em>after</em> the video; think
thunderstrucks for instance. But your brain will freak out if audio arrives <em>before</em>.</p>
</blockquote>
<p>Contrary to what many people will say, <strong>this is an unsolvable problem</strong>. There are some options to try to
mitigate the problem — for instance, <a href="https://docs.qmk.fm/tap_hold#flow-tap">Flow Tap</a> — but those still introduce tradeoffs that are not good ones
to me. <a href="https://docs.qmk.fm/tap_hold#flow-tap">Flow Tap</a> introduces a timeout after which tapping a key will disable HRMs, so you can speed through
without having the latency. However, it doesn’t solve the problem in all situations, and I still think that
<a href="https://docs.qmk.fm/tap_hold#permissive-hold">permissive-hold</a> is not as good as <a href="https://docs.qmk.fm/tap_hold#hold-on-other-key-press">hold-on-other-key-presses</a> on keys you don’t often use.</p>
<h2 id="whats-next"><a href="#whats-next">#</a> What’s next</h2>
<p>I’m considering changing a bit my layers, especially regarding backspace, return, etc. All in all, I think
that having so few keys does require some tradeoffs, and I think I’ve found the perfect setup for my
personal use. Who knows, maybe some day I will find a revolutionary way to implement HRM and will switch to
them, but for now, I don’t think it’s actually that good.</p>
<p>It was a lighter topic, but still pretty fun to talk about it. In the end, <em>I just love keyboards</em>.</p>
<img src=https://strongly-typed-thoughts.net/media/uploads/lots-of-keyboards.jpg width=800 />
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 17 Jun 2025 15:50:00 GMT</pubDate></item><item><title>warmy-0.6.0; bug fixes and rewrite</title><link>https://strongly-typed-thoughts.net/blog/warmy-0.6.0</link><description><![CDATA[<h1>warmy-0.6.0</h1>
<p>Today I released a new version of <a href="https://crates.io/crates/warmy">warmy</a>, the <code>warmy-0.6.0</code> release. That release kicks in with a
few additions, among:</p>
<ul>
<li>Complete rewrite of its internals, enabling optimizations (mostly about allocations).</li>
<li>A nasty IO-bound bug was removed.</li>
</ul>
<p>That bug caused long-lasting reloads on stream-copied resources go into weird behavior.</p>
<h1>The bug</h1>
<p>In order to get the bug, I must give a bit of context.</p>
<p>Imagine you have a large resource, like a 4K texture or a big mesh. Whenever your favourite
software writes it to disk, it’s very likely it’ll <em>stream</em> chunks by chunks of bytes. For
instance, it might choose to copy the resource <em>2 MB</em> by <em>2 MB</em> on disk. On each copy, your
file system will generate <code>WRITE</code> events, that <a href="https://crates.io/crates/warmy">warmy</a> will intercept. Before <code>warmy-0.6.0</code>, the
default behavior was to reload the resource on the first <code>WRITE</code> event, which was already
wrong, because only a very small part of the resource would have changed – I actually witnessed
weird behaviors with textures in a demo of mine I’m working on, not seeing the new textures in my
demo.</p>
<p>But there’s worse. There’s a parameter you can set of your <code>Store</code>, called <code>update_await_time_ms</code>.
That parameter gives <a href="https://crates.io/crates/warmy">warmy</a> a hint about how much time must have passed since the last update in
order to effectively call the <a href="https://docs.rs/warmy/0.5.2/warmy/trait.Load.html#method.reload"><code>Load::reload</code></a> function. However, this is a bit twisted, because if
the resource takes more time to reload than <code>update_await_time_ms</code>, it’ll get repeatedly loaded –
for as many as <code>WRITE</code> events were generated. This is a bit sick, yeah.</p>
<h1>The fix</h1>
<p>The fix was pretty simple: change the semantics of that <code>update_await_time_ms</code>. In <code>0.5.2</code>, it has
the default value of <strong>1s</strong>, meaning that a resource wouldn’t reload if it was reloaded less than a
second ago. The new semantics works on the future. Whenever a <code>WRITE</code> event is intercepted, <a href="https://crates.io/crates/warmy">warmy</a>
will call the <a href="https://docs.rs/warmy/0.5.2/warmy/trait.Load.html#method.reload"><code>Load::reload</code></a> function only if no <code>WRITE</code> event is intercepted in the next
<code>update_await_time_ms</code>. It’s a bit like the implementation of a click and a double click: you must
wait a bit after you got a <code>MouseRelease</code> event in order to interpret is as a <code>Click</code> because
another <code>MouseRelease</code> could arrive soon (generating a <code>DoubleClick</code> if it’s soon enough).</p>
<blockquote>
<p>You’ll also notice that <code>update_await_time_ms</code> name sticks better to the new semantics!</p>
</blockquote>
<p>In <code>0.5.2</code>, the default value for <code>update_await_time_ms</code> was <strong>1s</strong>. If we kept that value, it would
result in a pretty bad overall <em>latency</em>. The value was lowered to <strong>50ms</strong> instead.</p>
<blockquote>
<p>You can still tweak that value if it doesn’t suit your needs.</p>
</blockquote>
<p>More information can be found in the <a href="https://github.com/phaazon/warmy/blob/master/CHANGELOG.md#060">changelog</a>.</p>
<h1>On the future of the crate</h1>
<p>I’ve been very happy with what <a href="https://crates.io/crates/warmy">warmy</a> has brought to me so far. Other people also gave it a try
and for now seem to enjoy it. I’ve gathered a few ideas for the future, based on IRL talks over
<strike>a beer</strike> several beers, and GitHub issues / pull requests:</p>
<ul>
<li>We want to be able to pass around a <em>context</em> when loading and reloading. This would enable
tweaking the loading behavior of the objects and pass values from the calling function. However,
even though the feature is well-identified, it is not well-defined in <a href="https://crates.io/crates/warmy">warmy</a> and more research
must be done.</li>
<li>Multithreading. There’re a few ideas I’d like to implement, like an IO thread that would do all
the file IO-bound computation and dispatch the bytes to the other threads; and using <code>Future</code>.</li>
</ul>
<p>Feel free to test it, and as always, keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 24 Feb 2018 19:46:00 GMT</pubDate></item><item><title>Generalized swap</title><link>https://strongly-typed-thoughts.net/blog/generalized_swap</link><description><![CDATA[<h1>Generalized swap</h1>
<p>I pushed a <em>pull request</em> to <a href="https://hackage.haskell.org/package/either">Edward Kmett’s either package</a>
to implement two functions some guys was complaining not to find: <code>flipEither :: Either e a -&gt; Either a e</code>
and <code>flipEitherT :: EitherT e m a -&gt; EitherT a m e</code>.</p>
<p>When implementing the functions, I wondered: “Hey, flipping stuff is a pretty
common operation. Don’t we have an abstraction for that yet?”. I haven’t found any.</p>
<h1>Meet Swap</h1>
<p>I decided to make a little typeclass to see what it’d be.</p>
<pre><code class="language-haskell">class Swap s where
  swap :: s a b -&gt; s b a

instance Swap (,) where
  swap (a,b) = (b,a)

instance Swap Either where
  swap = flipEither

-- let’s go wild and fooled
instance Swap Map where
  swap = fromList . fmap swap . toList
</code></pre>
<p>If you think that’s handy, I’ll write a little package with default instances
to make it live.</p>
<p>Happy hacking folks!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 14 Apr 2015 00:00:00 GMT</pubDate></item><item><title>Kakoune design analysis with kak-tree-sitter</title><link>https://strongly-typed-thoughts.net/blog/kak-tree-sitter</link><description><![CDATA[<p>I want to talk about Kakoune in this blog article, and more specifically, its UNIX design. See, in my
<a href="https://github.com/helix-editor/helix/blob/master/LICENSE">previous</a> blog post, I explained why I love
<a href="https://helix-editor.com/">Helix</a>, and why I love <a href="https://kakoune.org/">Kakoune</a>. The design philosophy of <strong>Kakoune</strong>
really is excellent, but I want to put more relief now. Especially, I will write this blog article along the line of
<a href="https://github.com/phaazon/kak-tree-sitter">kak-tree-sitter</a>, which is a UNIX server and daemon I’ve been writing to
add support of <a href="https://tree-sitter.github.io/tree-sitter/">tree-sitter</a>.</p>
<h1>Kakoune and the shell</h1>
<p>Because Kakoune doesn’t have a plugin interface, you are limited to what is called <em>kakspeak</em>, which is basically what
you can type in the command line (the <code>:</code> line). Contrary to other editors like Vim, everything you type in that <code>:</code>
line can be laid out in a file (a <code>.kak</code>) and be interpreted directly. Another cool aspect about that is that you can
just select some part of a <code>.kak</code> file, and simply type <code>:^r.&lt;cr&gt;</code> – in Kakoune, the <code>.</code> is the selection register. That
will execute the Kakoune command.</p>
<p>This property is really good, but still, you are limited in what you can do. Among the command you will be running:</p>
<ul>
<li><code>declare-user-mode</code>, to declare your own mode (yes, like insert mode, normal mode, etc.). Yes it’s a default / core
feature and yes it’s excellent.</li>
<li><code>set-face</code> to declare / update highlighting groups.</li>
<li><code>execute-keys</code> and <code>evaluate-commands</code> to execute keys as if the user typed them, in sandboxed / isolated
environments. This is incredibly powerful, as you can run some commands that creates, updates registers, move the
cursor around, etc. but since it’s sandboxed, as soon as the command is done, you get back to the state you were in
before issuing the commands.</li>
<li>And more.</li>
</ul>
<p>There is nothing to write plugins… except one thing.</p>
<h2>String expansions</h2>
<p>Kakoune has this concept of <em>expanding strings</em>. It’s the same feature as in any other editor: some strings can contain
special keywords and identifiers to replace their content with what they hold. For instance, in your shell, it’s very
likely that this string will contain your username: <code>"$USER"</code>, or this will be the current year <code>"$(date +%Y)"</code>.
However, this will remain verbatim: <code>'$(date +%Y)'</code>. The reason is that, in a shell, <code>"</code> expands while <code>'</code> doesn’t.</p>
<p>Kakoune has the same mechanism, but the syntax is different, and Kakoune has different <em>source</em> of expansions. For
instance, <code>%opt{foo}</code> will be replaced why the <code>foo</code> option, that can be set with <code>set-option</code>. <code>%val{timestamp}</code>
contains the current timestamp of the buffer, etc. etc.</p>
<p>There is one interesting expansion that Kakoune has: <code>%sh{}</code>. This is <em>shell expansion</em>. It will execute its content
inside a thin shell. For instance, try open Kakoune and enter in the command line, something like <code>:echo %sh{date +%Y}</code>.
You can see where we are going here. We can use that to run arbitrary shell commands.</p>
<p>Kakoune is monothreaded, so when you run a command in a shell, it blocks until the shell command finishes. On its own,
it’s not that bad. It forces us to call short-living shell commands.</p>
<p>This mechanism allows us to run external programs, but it doesn’t tell us how we can call back Kakoune.</p>
<h1>Kakoune and UNIX sockets</h1>
<p>Kakoune is monothreaded, but it’s concurrent. It listens on a UNIX socket Kakoune commands that any external programs
can send, via the <code>kak -p</code> interface.</p>
<blockquote>
<p>I initially tried to send content to the UNIX socket programmatically, but it wasn’t planned for that, and hence hit
issues while doing so. It hurts to say that but you have to spawn a programm running <code>kak -p</code>; forget about writing
directly into the UNIX socket for now.</p>
</blockquote>
<p>So, with this mechanism, we can:</p>
<ol>
<li>Run a short-living program via <code>%sh{}</code> expansion to talk to, for instance, a server, quickly accepting our request
and treating asynchronously.</li>
<li>Then, once the request is handled, send back the response to Kakoune by running a <code>kak -p</code> program, using the UNIX
socket.</li>
</ol>
<p>The cool thing is that Kakoune follows a server/client architecture and has a <em>session identifier</em> (you must pass it
to <code>kak -p</code>). You can retrieve that value with <code>%val{session}</code> — and inside <code>%sh{}</code> expansion, it’s available as an
environment variable <code>$kak_session</code>.</p>
<p>And here you have it. The formula I’ve been using successfully to add <strong>tree-sitter</strong> support to Kakoune. Whenever we
need to highlight the buffer again, simply craft a small request to send to the <code>kak-tree-sitter</code> local server and
immediately get control back (so that the UI doesn’t freeze). Then, at some point, the highlight request is computed and
arrives via <code>kak -p</code> in Kakoune.</p>
<p>But you might have a question… how do I deal with the buffer content? Indeed, the only thing we can do with with <code>%sh{}</code>
is starting a program in a shell. We could do something like laying the buffer content on the shell invocation but
that’s seriously limiting (especially on giant buffers). We could put the content of the buffer in an environment
variable, but it would suffer from the same problem (and damn it’s so dirty). What else?</p>
<h1>The final part of the recipe: FIFOs</h1>
<p>UNIX systems have this incredible thing called <em>FIFOs</em>. I FIFO — also named <em>pipe</em> — is a special kind of file. It lives
on your filesystem (so it’s located at a given path), a bit like UNIX socket. However:</p>
<ul>
<li>Its content <em>never</em> exists on the filesystem. It’s all in-memory.</li>
<li>It’s a streaming primitive, so you cannot just write into it and assume it’s stored somewhere.</li>
<li>Think of it as a buffer between two entities: a <em>reader</em> and a <em>writer</em>.</li>
<li>When a reader reads from it, it blocks until data is available to read.</li>
<li>When a writer writes to it, it blocks until a reader is available to read.</li>
</ul>
<p>So it implements a rendez-vous buffer between a single reader and a single writer. And yes, the <code>|</code> in your shell is
using something that behind the scene. The thing is: you can create your own, and you don’t even need to write any code.
Enter the <code>mkfifo</code> program. For instance, you can try it out on your own by creating a FIFO (let’s call it <code>rdv</code>),
reading its content with <code>cat</code> first (you’ll see the <code>cat</code> process freeze, so you will need another terminal session!)
and then write content to it, for instance with <code>echo</code>:</p>
<pre><code class="language-sh">mkfifo /tmp/rdv

# in shell 1
cat /tmp/rdv

# in shell 2
echo "Hello, world!" &gt; /tmp/rdv
</code></pre>
<p>As seen as the <code>echo</code> starts writing, you can see <code>cat</code> return the result.</p>
<blockquote>
<p>Now replace <code>cat</code> with <code>tail -f</code> 😏.</p>
</blockquote>
<p>Anyway, Kakoune has a mechanism where it scans <code>%sh{}</code> blocks and it sees <code>$kak_command_fifo</code> and/or
<code>$kak_response_fifo</code>, it will create those FIFOs for us (and manage their lifetimes). Because we are in the shell, we
can write Kakoune commands to execute in <code>$kak_command_fifo</code>, which will be executed as soon as you’re done writing to
the FIFO, and you can read <code>$kak_response_fifo</code> from, for instance, an external program, to get more data from Kakoune.</p>
<p>This is the exact mechanism that is used to stream buffer content between Kakoune and <code>kak-tree-sitter</code>. Here’s the
Kakoune commands used to highlight a buffer:</p>
<pre><code class="language-sh"># Send a single request to highlight the current buffer.
define-command kak-tree-sitter-highlight-buffer -docstring 'Highlight the current buffer' %{
  nop %sh{
    echo "evaluate-commands -no-hooks -verbatim write $kak_response_fifo" &gt; $kak_command_fifo
    kak-tree-sitter -s $kak_session -c $kak_client -r "{\"type\":\"highlight\",\"buffer\":\"$kak_bufname\",\"lang\":\"$kak_opt_filetype\",\"timestamp\":$kak_timestamp,\"payload\":\"$kak_response_fifo\"}"
  }
}
</code></pre>
<p>I won’t explain the protocol into two much details, but the important part is that I start a shell to run
<code>kak-tree-sitter … -r</code>, format the request (it’s just plain JSON), and ask <code>kak-tree-sitter -r</code> to read from
<code>$kak_response_fifo</code>. You can see in the previous line that we write to it (the <code>write $kak_response_fifo</code> basically
means to write the content of the current buffer to <code>$kak_response_fifo</code>, which is a file — everything is a file in
UNIX!).</p>
<p><code>kak-tree-sitter -r</code> is made in a way so that it exits quickly. It will read from the FIFO file and forward the request
to the <code>kak-tree-sitter</code> server, which, in turn, will move the request to a different thread so that it can quickly
mark the request done. The whole thing is then synchronous, but extremely fast. The only bottleneck we have here (and
mind it, it’s important) is <strong>that we must read from the FIFO synchronously in the shell <code>kak-tree-sitter -r</code></strong>.</p>
<h1>How’s it going?</h1>
<p>I’ve been running with this setup for weeks now, since I started <code>kak-tree-sitter</code> around the end of April, 2023. The
code you read above runs in a couple of Kakoune hooks (the same <code>kak-lsp</code> is using or very similar), which waits for a
<em>idle</em> time after the buffer is edited (in practice, 50ms after the last edit operation). And it’s already fast enough.</p>
<p>However, there is a nice optimization that we could do here. See, the only reason to start a shell is to format the
request to send to <code>kak-tree-sitter</code>, the server. We can completely by-pass it. Instead, we could:</p>
<ul>
<li>Create the FIFO in <code>kak-tree-sitter</code>, when a new Kakoune session is created.</li>
<li>Write the requests to the FIFO handled by <code>kak-tree-sitter</code>. No shell is needed to write to files!</li>
<li>The rest is the same, since the server sends back highlighting Kakoune commands via <code>kak -p</code>.</li>
</ul>
<p>This on its own should greatly enhance performance, which is already pretty good, even with the shell. I plan on working
on this enhancement in the upcoming days.</p>
<h1>Alright, but what’s wrong then?</h1>
<p>Well, there is one thing. See, in order to support <strong>tree-sitter</strong>, I had to write this <code>kak-tree-sitter</code> server, and
write buffer contents to FIFO files. <code>kak-lsp</code> is doing the same. All of that is synchronous, and even if it’s fast,
it’s still two sequential locks of the buffer, copy of the buffer, etc. On my side, I’m currently exploring the
<code>%val{history}</code> expanded variable, which contains the textedit operations (so that I don’t have to stream a full
buffer and diff in <code>kak-tree-sitter</code> anymore, but instead just small chunks of updates).</p>
<p>However, the problem still holds. The design of Kakoune is nice when you’re working alone with your small integration,
but when you have two big ones (LSP and tree-sitter are not small environments), I think Kakoune shows its limit.</p>
<p>We – <a href="https://github.com/krobelus">@krobelus</a> and I – discussed that matter. A middleware program or a change in how
Kakoune interfaces with external programs, especially for buffer streaming, would be greatly appreciated. But it’s not
currently the case. And even if we decide to come up with a middleware – that could act as a buffer cache / map, for
instance – we would still be writing buffer contents to different FIFOs.</p>
<p>Another thing that I’ve been thinking about a lot is… is this the right approach? Something like <strong>tree-sitter</strong> <em>seems</em>
to be ideal as an embedded library, directly inside the code of your editor. Especially since Kakoune is all about
selections, being able to have semantic selections by default seems like something pretty good. <strong>tree-sitter</strong> requires
some runtime resources (relocatable objects like <code>.so</code> / <code>.dylib</code>, queries, etc.)… and it’s the same thing with the
regular Kakoune highlighters (by default, Kakoune doesn’t have any; they come bundled up with your distribution).</p>
<p>I’m not entirely sure the approach scales very well. Of course, I still think the design is excellent, and it prevents
too much maintenance on Kakoune, which is also a good thing. For instance, this runtime resource problem is something
I’m solving in <code>kak-tree-sitter</code> (and easing with a controller tool called <code>ktsctl</code>, which can download, compile and
install grammars and queries for you), but still. The current version of <code>kak-tree-sitter</code> only supports semantic
highlighting, not text-objects just yet (but it shouldn’t be too hard to add). The state of the project is not completly
ready for people to jump in (the wiki is not written), but if you tag along on Matrix, I can help you get started.</p>
<p>Please consider <code>kak-tree-sitter</code> as an experimental project, because I’m not sure this is the right approach, even
though it’s probably the only one for Kakoune for now (I don’t think <a href="https://github.com/mawww">@mawww</a> plans on
integrating it into the core of Kakoune).</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 09 Jun 2023 19:20:00 GMT</pubDate></item><item><title>Spectra gets Rust scripting!</title><link>https://strongly-typed-thoughts.net/blog/spectra_plugins</link><description><![CDATA[<h1>Spectra gets Rust scripting!</h1>
<p><a href="https://crates.io/crates/spectra">spectra</a> is a crate I’ve been maintaining for a few months / years now. It’s a crate that I mainly
use for demoscene productions (I released two with it, <a href="https://www.youtube.com/watch?v=pYqZS1C_7PU">Céleri Rémoulade</a> and <a href="https://www.youtube.com/watch?v=OemyLQbDTSk">Outline Invitation</a>)
but I also use it to play around and experiment new rendering, animation and video game techniques.</p>
<p>The crate features a few things that I enjoy daily. Among them:</p>
<ul>
<li>A rendering layer, backed by another crate of mine, <a href="https://crates.io/crates/luminance">luminance</a>.</li>
<li>Hot reloading for scarce resources – the feature was extracted into <a href="https://crates.io/crates/luminance">warmy</a> as folks on IRC asked
me to!</li>
<li>Models (.OBJ) loaders.</li>
<li>Audio simple routines (basically: play a soundtrack and get metadata about it).</li>
<li>A shading language of mine, built upon GLSL (I’ll make a separate blog entry about this one).</li>
<li>Splines, animation primitives, etc.</li>
<li>And more.</li>
</ul>
<p>The last thing I’ve been working on is <em>being productive</em>. That might seem counter-intuitive, but
when you start building <em>“frameworks”</em> and <em>“engines”</em>, you actually end up writing a lot of code
for <em>“the beauty and power of it”</em> and lose focus on what’s important: releasing things. I know
that and looked for what I could enhance to augment my productivity.</p>
<p>Among interesting topics I came up with, I stated:</p>
<ul>
<li>JSON live editing with <a href="https://crates.io/crates/luminance">warmy</a> in <a href="https://crates.io/crates/spectra">spectra</a> is totally awesome (it was an improvement for my
productivity when I was, before, editing the curves in the code – you know, the edit-compile-run
cycle?). However, even if it’s awesome, it’s quickly limiting, especially when you want to edit
quaternions (rotations) or find a specific color mask. So I’d like to implement a way to pick
objects around with the mouse and edit them via a nice UI.</li>
<li>Scripting. Somewhat. I partially done that with hot-reloaded shaders!</li>
<li>Better tooling, especially for live-coding.</li>
</ul>
<p>Clearly, there’s something about live coding.</p>
<h1>Introducing scripting in Rust</h1>
<p>I went through several thought processes. I first had a look at <a href="https://www.lua.org">Lua</a>, because lots of people think
it’s cool. However, I don’t like the syntax nor the overall safety the language
<strike>gives</strike> <em>doesn’t</em> give you.</p>
<p>I remembered that almost a decade ago, when I was 15 or 16 year old, I implemented some kind of a
<em>plugins</em> system in C++ for a side project of mine. The idea was simple:</p>
<ul>
<li>Find functions with <code>dlopen</code>, <code>dlsym</code> to open a relocatable object / archive (.so / .dll).</li>
<li>Use them at runtime.</li>
</ul>
<p>This is not magic. When you link a crate / library, by default, a lot of compilers will perform
<em>static linking</em>. That’s the case of <a href="https://www.haskell.org">ghc</a> or <a href="https://www.rust-lang.org">rustc</a>, in most situations.</p>
<blockquote>
<p>If you don’t know yet, a library / crate / dependency statically-linked in a program means that
all of its symbols (the ones used in the program at least) are placed in a specific section of
the generated executable, so that those symbols have a proper address and “come with the binary”.</p>
</blockquote>
<p>Dynamic linking, on the other side, is a way to express a <em>dependency</em> between your binary and some
code, which is most of the time living in a relocatable archive (<code>.so</code> on Linux or macOSX, <code>.dll</code> on
Windows – macOSX also uses <code>.framework</code> but it’s just for the overall idea). When your binary needs
to call a function defined in a dynamically linked library, it’ll open the shared library with the
<code>dlopen</code> function, try to locate the function by giving its (unmangled) name to <code>dlsym</code> or <code>dladdr</code>
for instance, if the function exists, you’ll be able to run its code.</p>
<p>There exists attacks and interesting things you can do with dynamic libraries. Because the code
doesn’t live in the binary, you can for instance replace a legit and safe <code>.dll</code> on Windows with a
malicious one. Or you can patch a buggy dynamic library by shipping a new <code>.dll</code> / <code>.so</code> without
having its dependent to re-compile their applications (which would be needed in case of static
linking). Another funny thing you can do: <a href="http://www.fraps.com/">fraps</a>, a real-time recorder for your video games,
performs some kind of <code>.dll</code> injection by pushing an OpenGL <code>.dll</code> into your binary (Windows allows
this) and replacing some known functions. For instance, the function responsible in swapping your
renderbuffer chains. It can then intercepts pixels regions, adds overlay, etc. Fascinating! ;)</p>
<p>Anyway, the idea is that whenever you link a dynamic library, your compiler / linker will just
insert the required code in your binary (think of <code>dlopen</code>) so that your binary can load code at
runtime. It’s then pretty easy to implement a plugin system:</p>
<ul>
<li>Define your interface. Define a function type you will need to take from the dynamic library and
give that interface a name so that you can look it up in the library.</li>
<li>Open the library, find the symbol, and just use it!</li>
</ul>
<p>So you could imagine, as a very simple start example, a <code>loop</code> that would simply invokes such a
function. Whenever you change the library, the code getting ran will automatically changes as well!</p>
<h2>Scripting in spectra</h2>
<p>So I decided to reproduce that in Rust using the few crates of mine:</p>
<ul>
<li><code>spectra</code>: obviously, since it’s the crate that will receive the feature first.</li>
<li><code>warmy</code>: it’s indirect since <code>spectra</code> re-exports it and adds a few things over it, like a
<em>resource</em> system (error messages, timing loading and reloading, etc.).</li>
<li><a href="https://crates.io/crates/libloading"><code>libloading</code></a>: the <em>Rust way</em> to go with dynamic libraries (it has an <code>unsafe</code> interface though).</li>
<li>That’s pretty much everything of it.</li>
</ul>
<blockquote>
<p>For unix plateforms, you can see that <code>libloading</code> is just a smart wrapper over the functions I
mentionned. <a href="https://github.com/nagisa/rust_libloading/blob/master/src/os/unix/mod.rs#L281">See for yourself</a>.</p>
</blockquote>
<p>Ok, so, let’s try the experiment!</p>
<h1>Let’s write plugins in Rust!</h1>
<h2>Foreword</h2>
<p>The first thing we must accomplish is very simple: we want to be able to load some (Rust) code at
runtime, inside our application. For achieving that goal, we need to load a dynamic library (<code>.so</code>
on Linux) with <a href="https://docs.rs/libloading/0.5.0/libloading/struct.Library.html#method.new"><code>libloading::Libray::open</code></a>.
Once we have the library, we can just look a symbol up with
<a href="https://docs.rs/libloading/0.5.0/libloading/struct.Library.html#method.get"><code>libloading::Library::get</code></a>.
In case of a successful lookup, that function returns a value of type <code>Symbol</code>, which implements
<code>Deref</code> for the symbol you’re asking.</p>
<blockquote>
<p>Dynamic library typically gathers functions, so we’ll be looking for <code>fn</code> symbols.</p>
</blockquote>
<p>However, we don’t have any dynamic library yet. We only have… a Rust file – <code>.rs</code>. How can we get
a dynamic library out of it?</p>
<h2>Generating a dynamic library</h2>
<p>This is actually pretty simple. We’re gonna start easily by making a <code>.so</code> that will just contain
a function called <code>hello_world</code> that will just print <code>"Hello, world!"</code> on <code>stdout</code>. You all know how
to implement such a function:</p>
<pre><code>/// hello_world.rs
pub fn hello_world() {
  println!("Hello, world!");
}
</code></pre>
<blockquote>
<p>We make the function <code>pub</code> so that it gets actually exported when we generate the dynamic library.</p>
</blockquote>
<p>Copy that code and put it in, for instance, <code>/tmp</code>.</p>
<p>Then, let’s generate a dynamic library!</p>
<pre><code>cd /tmp
rustc --crate-type dylib hello_world.rs
</code></pre>
<p>Once <code>rustc</code> returned, you should see a new file in <code>/tmp</code>: <code>/tmp/libhello_world.so</code>! Here we are!
We have a dynamic library! Let’s try to find our function in it with the handy <code>nm</code> program.</p>
<blockquote>
<p>I reckon <code>nm</code> is already installed on your machine. If not, it should come with packages like
<code>base-devel</code>, <code>build-essentials</code> or that kind of meta packages.</p>
</blockquote>
<pre><code>nm /tmp/libhello_world.so | grep hello_world
0000000000000000 N rust_metadata_hello_world_8787f43e282added376259c1adb08b80
0000000000040ba0 T _ZN11hello_world11hello_world17h49fe1e199729658eE
</code></pre>
<p>Urgh. We have a problem. Rust has mangling for its symbol names. It means that it will alter the
symbols so that it can recognize them in a dynamic library. For instance, the mangle version of a
function name <code>foo</code> defined for a type <code>A</code> won’t be the same as the one of <code>foo</code> defined for a type
<code>B</code>. However, in our case, we don’t want mangling, because, well, we won’t be able to lookup the
name up – no one will even try to guess that <code>_ZN11hello_world11hello_world17h49fe1e199729658eE</code>
function name.</p>
<p><code>rustc</code> has a very simple solution to that: just tell it you don’t want a symbol’s name to be
mangled. It’ll be stored in the dynamic library the way you write it. This is done with the
<code>#[no_mangle]</code> attribute.</p>
<pre><code>/// hello_world.rs
#[no_mangle]
pub fn hello_world() {
  println!("Hello, world!");
}
</code></pre>
<p>Now recompile with the same <code>rustc</code> line, and run the <code>nm</code> + <code>grep</code> oneliner again.</p>
<pre><code>nm /tmp/libhello_world.so | grep hello_world
0000000000040b70 T hello_world
0000000000000000 N rust_metadata_hello_world_8787f43e282added376259c1adb08b80
</code></pre>
<p>Now you can see there exists a symbol called <code>hello_world</code>: this is our symbol!</p>
<h2>Load the library in Rust and read a symbol</h2>
<p>For our little example here, we’re just gonna load and run the code in a new project.</p>
<pre><code>cd /tmp
cargo new --bin dyn-hello-world
     Created binary (application) `dyn-hello-world` project
cd dyn-hello-world
</code></pre>
<p>Edit the <code>Cargo.toml</code> file to include <code>libloading = "0.5"</code> in the <code>[dependencies]</code> section. Ok,
we’re good to go. Run a second terminal in which you run this command to automatically check
whether your code is okay:</p>
<pre><code>cd /tmp/dyn-hello-world
cargo watch
</code></pre>
<p>Let’s edit our <code>main.rs</code> file.</p>
<pre><code>extern crate libloading;

use libloading::Library;

fn main() {
  let lib = Library::new("/tmp/libhello_world.so").unwrap();
  let symbol = unsafe { lib.get::&lt;extern fn ()&gt;(b"hello_world").unwrap() };

  symbol();
}
</code></pre>
<p>It should check. A bit of explanation:</p>
<ul>
<li>The <code>Library::new</code> accepts as first and only argument the path to the dynamic library. In our
case, we just use our freshly generated <code>libhello_world.so</code>.</li>
<li>The <code>unsafe</code> <code>get</code> call takes the symbol we look for as argument and returns it if it’s found it.</li>
<li>Because <code>Symbol</code> implements <code>Deref</code>, here, we can directly call the function with <code>symbol()</code>.</li>
</ul>
<p>Now compile and run the code:</p>
<pre><code>cargo build
   Compiling cc v1.0.4
   Compiling libloading v0.5.0
   Compiling dyn-hello-world v0.1.0 (file:///tmp/dyn-hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 2.63 secs
cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/dyn-hello-world`
Hello, world!
</code></pre>
<p>Hurray! We’ve just built our first dynamic library loader!</p>
<h2>Let’s compile Rust… from Rust!</h2>
<p>Ok, now, let’s iterate: we need to compile the Rust code from our Rust code (haha). For doing that,
we’ll need to generate the dynamic library at some place. Because I don’t like to put artifacts in
random places, I like to use the <code>tempdir</code> crate, which gives you a <code>TempDir</code> that creates a new
temporary directory with a random name when you ask for it, and removes it from your filesystem when
the <code>TempDir</code> goes out of scope. We’ll also be using the <code>std::process</code> module to run <code>rustc</code>.</p>
<p>Add the following to your <code>[dependencies]</code> section in your <code>Cargo.toml</code>:</p>
<pre><code>tempdir = "0.3"
</code></pre>
<p>Ok, let’s compile a Rust source file into a dynamic library from our Rust source code!</p>
<pre><code>extern crate libloading;
extern crate tempdir;

use libloading::Library;
use std::process::Command;
use std::str::from_utf8_unchecked;
use tempdir::TempDir;

fn main() {
  let dir = TempDir::new("").unwrap(); // we’ll drop the .so here
  let target_path = dir.path().join("libhello_world.so");
  
  let compilation =
    Command::new("rustc")
    .arg("--crate-type")
    .arg("dylib")
    .arg("/tmp/hello_world.rs")
    .arg("-o")
    .arg(&amp;target_path)
    .output()
    .unwrap();

  if compilation.status.success() {
    let lib = Library::new(&amp;target_path).unwrap();
    let symbol = unsafe { lib.get::&lt;extern fn ()&gt;(b"hello_world").unwrap() };

    symbol();
  } else {
    let stderr = unsafe { from_utf8_unchecked(compilation.stderr.as_slice()) };
    eprintln!("cannot compile {}", stderr);
  }
}
</code></pre>
<p>Compile, run, and…</p>
<pre><code>cargo run
   Compiling dyn-hello-world v0.1.0 (file:///tmp/dyn-hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.58 secs
     Running `target/debug/dyn-hello-world`
Hello, world!
</code></pre>
<p>This piece of code is the first premise of our plugin system. You can see interesting properties:</p>
<ul>
<li>You don’t have to re-compile <code>dyn-hello-world</code> to change its behavior, since it comes from a
dynamic library.</li>
<li>There’s no need to depend on a dynamic library <em>directly</em>, since we can just generate it on the
fly!</li>
<li>You can display error messages (the current code is a bit perfunctory but you could enhance to
have nice error messages!).</li>
</ul>
<p>However, there’s a problem. Try adding an <code>extern crate</code> to <code>hello_world.rs</code>, like, for instance:</p>
<pre><code>extern crate spectra;

#[no_mangle]
pub fn hello_world() {
  println!("Hello, world!");
}
</code></pre>
<p>Run <code>dyn-hello-world</code>.</p>
<pre><code>cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/dyn-hello-world`
cannot compile error[E0463]: can't find crate for `spectra`
 --&gt; /tmp/hello_world.rs:1:1
  |
1 | extern crate spectra;
  | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error
</code></pre>
<p>Damn, how are we gonna solve this?</p>
<h2>Finding dependencies!</h2>
<p>The key is to understand how <code>cargo</code> deals with the dependencies you declare in your <code>Cargo.toml</code>’s
<code>[dependencies]</code> section. To do this, clean your project, and recompile in <em>verbose</em> mode – we’re
like… <em>reverse engineering</em> <code>cargo build</code>!</p>
<pre><code>cargo clean -p dyn-hello-world
cargo build --verbose
       Fresh libc v0.2.36
       Fresh winapi-build v0.1.1
       Fresh winapi v0.2.8
       Fresh cc v1.0.4
       Fresh rand v0.4.2
       Fresh kernel32-sys v0.2.2
       Fresh libloading v0.5.0
       Fresh remove_dir_all v0.3.0
       Fresh tempdir v0.3.6
   Compiling dyn-hello-world v0.1.0 (file:///tmp/dyn-hello-world)
     Running `rustc --crate-name dyn_hello_world src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=e8b1e5e96f709abe -C extra-filename=-e8b1e5e96f709abe --out-dir /tmp/dyn-hello-world/target/debug/deps -C incremental=/tmp/dyn-hello-world/target/debug/incremental -L dependency=/tmp/dyn-hello-world/target/debug/deps --extern tempdir=/tmp/dyn-hello-world/target/debug/deps/libtempdir-9929bcad6dc8cc47.rlib --extern libloading=/tmp/dyn-hello-world/target/debug/deps/liblibloading-a2854ce154eb4d6f.rlib -L native=/tmp/dyn-hello-world/target/debug/build/libloading-4553b9f132aa813a/out`
    Finished dev [unoptimized + debuginfo] target(s) in 0.50 secs
</code></pre>
<p>We can see a few things going on here. First, there are some <code>Fresh</code> lines we’re not interested
about. Then <code>cargo</code> tries to compile our application. You can see an invokation to <code>rustc</code> with a
long list of arguments. Among them, two interest us:</p>
<ul>
<li>The one specifying the list of dependencies your project needs:
<ul>
<li><code>-L dependency=/tmp/dyn-hello-world/target/debug/deps</code>.</li>
</ul>
</li>
<li>The ones specifying the list of crates you might use in <code>extern crate</code> statements.
<ul>
<li><code>--extern tempdir=/tmp/dyn-hello-world/target/debug/deps/libtempdir-9929bcad6dc8cc47.rlib</code></li>
<li><code>--extern libloading=/tmp/dyn-hello-world/target/debug/deps/liblibloading-a2854ce154eb4d6f.rlib</code></li>
</ul>
</li>
</ul>
<blockquote>
<p>There’s also a <code>-L native=…</code> one. This is explained in the manual of <code>rustc</code> and corresponds to
native library we must link against, like a C library, for instance.</p>
</blockquote>
<p>As you can see, the path used in <code>-L dependencie=…</code> is pretty <em>constant</em>. It seems it has the form:</p>
<pre><code>/path/to/project/target/&lt;build type&gt;/deps
</code></pre>
<p>However, remember that the initial intent was to write a plugin system for <code>spectra</code>, which is a
library. We don’t know the path to the project that will be using <code>spectra</code>, so we must find it in
a way.</p>
<p>Two options:</p>
<ul>
<li>We could use a <em>macro</em> (<code>macro_rules</code>) to automatically insert it at the calling site. However,
I’m not even sure it would work (you need to find a way to have access to <code>cargo</code>’s internal…
maybe via the <code>env!</code> macro?).</li>
<li>We could find the path at runtime.</li>
</ul>
<p>I chose the second option because it was pretty easy and straight-forward to implement. However,
I’m not a huge fan of it – it’s pretty… non-elegant to me. Here’s the code that gives me the root
path of the current project a function is defined in:</p>
<pre><code>/// Try to find the project root path so that we can pick dependencies.
fn find_project_root() -&gt; Result&lt;PathBuf, PluginError&gt; {
  let result =
    Command::new("cargo")
    .arg("locate-project")
    .output()
    .unwrap();

  if !result.status.success() {
    Err(PluginError::CannotFindMetaData("cannot locate root project".to_owned()))
  } else {
    let json = unsafe { from_utf8_unchecked(result.stdout.as_slice()) };
    let root =
      json.split(':').nth(1).and_then(|x| {
        if x.len() &gt;= 3 {
          Path::new(&amp;x[1..x.len()-3]).parent().map(|x| x.to_owned())
        } else {
          None
        }
      });

    root.ok_or_else(|| PluginError::CannotFindMetaData("cannot extract root project path from metadata".to_owned()))
  }
}
</code></pre>
<p>As you can see, I use the <code>cargo locate-project</code> feature, that gives you the root path of the
project the <code>cargo</code> invokation is in. It supports calling it from a subdirectory, which is neat (a
bit like <code>git</code>, actually). Most of the code is removing the JSON sugar over it. It’s pretty unsafe
because if the output format of <code>cargo locate-project</code> changes, this code will basically break.</p>
<blockquote>
<p>Pro reminder for myself: write unit tests for that piece of code. :D</p>
</blockquote>
<p>I won’t show the <code>PluginError</code> type, it’s not important and it’s <code>spectra</code> current code for the
feature.</p>
<p>Ok, we lack two things:</p>
<ul>
<li>The build type.</li>
<li>The path to our crate – here, <code>spectra</code>.</li>
</ul>
<p>Finding the build type is pretty easy: you can use the <code>debug_assertions</code> <code>cfg!</code> argument. It’s set
to <code>true</code> on <code>debug</code> target and <code>false</code> on <code>release</code>. You can actually witness it works with the
following oneliner:</p>
<pre><code>println!("build target is debug: {}", cfg!(debug_assertions));
</code></pre>
<p>Ok, now, how do we find our <code>spectra</code> crate’s path? You saw it has a metadata glued to its name in
the previous verbose output.</p>
<p>I didn’t find any satisfying solution. I just came up with <em>a solution</em>. I warn you: it’s not
elegant, it’s highly unsafe, but for now, it works. I’ll try to find a better way later.</p>
<p>Here’s the code.</p>
<pre><code>/// Find the spectra crate in the given directory.
fn find_spectra_crate(path: &amp;Path) -&gt; Result&lt;PathBuf, PluginError&gt; {
  // we open the directory we pass in as argument to the function
  if let Ok(dir) = path.read_dir() {
    // we iterate over all of its contained files to find if it has our spectra crate
    for entry in dir {
      let path = entry.unwrap().path();

      match path.file_name() {
        // we try to pattern match its name; this is a bit unsafe because it won’t support two
        // versions of libspectra… meh.
        Some(filename) if filename.to_str().unwrap().starts_with("libspectra") =&gt; {
          return Ok(path.to_owned());
        }

        _ =&gt; ()
      }
    }

    Err(PluginError::CannotFindMetaData("cannot find the spectra crate; try to build your project’s dependencies".to_owned()))
  } else {
    Err(PluginError::CannotFindMetaData("cannot find read dependencies".to_owned()))
  }
}
</code></pre>
<p>If you put those three functions altogether, you can now implement the <code>rustc</code> call without
hardcoding any paths, since they all will be found at runtime and injected in the call!</p>
<blockquote>
<p>A bit of hindsight. I didn’t explain that, but it’s a bit obvious: you won’t be able to use all
the crates you want in your plugins, for a very simple reason: you must have them installed
somewhere. In order for the <code>find_spectra_crate</code> to find anything, you must have <code>spectra = "…"</code>
in the <code>[dependencies]</code> section of your <code>Cargo.toml</code>. If you want to use any crate, you need to
add them in the <code>[dependencies]</code> and write a smarter function that parses the <code>Cargo.toml</code>’s
<code>[dependencies]</code> section and add them to the <code>rustc</code> invokation… which is basically like
re-writting a feature of <code>cargo</code> itself!</p>
</blockquote>
<h2>How do you make auto-reload again?</h2>
<p>I didn’t speak about that, but in <code>spectra</code>, it’s <strong>very easy</strong> to have a resource to auto-reload if
it changes on the disk. This is done via the <code>warmy</code> crate. You just implement the
<a href="https://docs.rs/warmy/0.5.2/warmy/trait.Load.html"><code>Load</code></a> trait and ask for your type at a
<a href="https://docs.rs/warmy/0.5.2/warmy/struct.Store.html"><code>Store</code></a> by providing a typed key. I won’t
talk too much about <code>warmy</code> – if you’re interested, go read the online documentation
<a href="https://docs.rs/warmy">here</a>!</p>
<p>The idea is that I just created a type that wraps both <code>libloading::Library</code> and <code>tempdir::TempDir</code>.
Something like this:</p>
<pre><code>pub struct Plugin {
  lib: Library,
  dir: TempDir
}
</code></pre>
<p>I still don’t know whether I need to implement <code>Drop</code> or not – if the <code>TempDir</code> dies first, I don’t
know whether the <code>Library</code> object is in an unstable state. If not, that means that the whole library
was loaded in RAM at the end of the <code>Library::new</code> call, and that I don’t even need to keep the
temporary directory around!</p>
<h2>It’s just the beginning</h2>
<p>The interface of a <code>Plugin</code> is not yet defined. I’m writing this blog entry on Sunday night / Monday
morning, while I had that plugin experiment over the weekend. A few thoughts, though:</p>
<ul>
<li>My current code enables me to run my demo (graphics-ish) and edit Rust code to script it <em>live</em>,
which is completely amazing to me!</li>
<li>It should be easy to expose a safe interface by asking the plugin to expose a normal function and
inject a header in the Rust code that contains an <code>unsafe fn</code>, calling the safe function or a
trait’s method or whatever.</li>
<li>You have no idea how much I’m excited about all this!</li>
</ul>
<p>I have a <em>lot</em> of things to say, especially lately. I’ll be posting more thoughts and experiments of
mine soon! Thanks for having read through, and as always, keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 19 Feb 2018 02:57:00 GMT</pubDate></item><item><title>Reflections about note-taking</title><link>https://strongly-typed-thoughts.net/blog/note-taking-2023</link><description><![CDATA[<p>TL;DR I was wrong about <a href="https://orgmode.org/">org-mode</a>. I know, this is a bit click-bait, but really, I made wrong assumptions and I think I
need to explain my thoughts now.</p>
<h1>Note taking, task management, journaling… how?</h1>
<p>As an experienced Software Engineer, I know that I need to multi-task and work on many different projects in parallel,
with different chronicity. Some new tasks can be done only a couple of minutes after discovering / creating them, and
some others will span on days, weeks if not months. That leads to having many parallel things on-going, and since I want
to do my job as correctly as possible… I take notes. I take notes about pretty much <em>everything</em>, and I’ve been using
note-taking apps more and more.</p>
<p>Years ago, as I discovered <a href="https://github.com/doomemacs/doomemacs">Doom Emacs</a>, I also discovered <a href="https://orgmode.org/">org-mode</a>. I was blown away by the features brought by
<a href="https://github.com/doomemacs/doomemacs">Doom Emacs</a>, and a bit puzzled with other things. For instance, the agenda (I rarely care about ETA for my personal
notes; that’s something that is tracked at the company-wide level, for instance in the infamous Jira or whatever you’re
using for project management in your team — and I just don’t care on my spare-time). Also, I was a bit doubtful about
having notes laid as plain text (yes, <a href="https://orgmode.org/">org-mode</a> formatted, but still text). What it means is that, there is no database
nor state. Everytime you want to get a list of your <code>TODO</code>, <a href="https://orgmode.org/">org-mode</a> has to go through all your <code>.org</code> files and
generates the list for you. Back then, I found that a bit weird, but today, I have changed my mind.</p>
<h1>Zettelkasten and other tools</h1>
<p>The way I take notes, whatever I do, is to minimize the note files I create. For instance, at work, I have many notes
about commands to run with a specific product. Instead of having a note <code>That product</code>, I tend to create smaller notes,
more scoped, like <code>That product - deploy</code>, <code>That product - how to configure</code>, etc. That allows two important things:</p>
<ul>
<li>Notes are shorter, easier to read and maintain.</li>
<li>Looking up for information and fuzzy searching is easier.</li>
</ul>
<p>Yes, <em>fuzzy searching</em>. Whatever the tool I use, I often need to search for notes, and either I search by note names, or
by content.</p>
<p>A couple of months ago, I wrote <a href="https://github.com/phaazon/mind">mind</a>, a rewrite of <a href="https://github.com/phaazon/mind.nvim">mind.nvim</a>. I designed that system to be a <em>tree editor</em>, in which
nodes are either “document node” containing some metadata and a path to a file, or a “link node”, with metadata and URL.
Those tools were very useful to me, and I’ve been using them for a long time.</p>
<p>However, as I moved more and more into the UNIX world with <a href="https://kakoune.org">kakoune</a> and <a href="https://github.com/phaazon/kak-tree-sitter">kak-tree-sitter</a>, I realized that I should
maybe revisit the way I think about notes. For instance, what I often need is to list the on-going tasks I’m doing,
and updating them. Oftentimes (if not all the time), tasks emerged from a context, like a discussion with a colleague,
some notes / brainstorming with myself I’m doing / a meeting / etc. And <a href="https://github.com/phaazon/mind">mind</a>, <a href="https://www.notion.so/">Notion</a> — or whatever that is using
an external state — are not very suited. The problem relies in <em>interruption</em>. You’re in the editor of your choice,
and then you have to switch to another window, look for the task / notes, use some keybindings to change the task to
switch it from <code>TODO</code> to <code>WIP</code> or <code>WIP</code> to <code>DONE</code> or <code>WONTDO</code> or whatever.</p>
<p>As I was working more and more with <a href="https://github.com/phaazon/mind">mind</a>, I realized this kind of workflow was starting to annoy me.</p>
<h1>Composing</h1>
<p>You start to see the pattern with my blog posts: I enjoy the UNIX world <em>a lot</em>. And even though <a href="https://github.com/phaazon/mind">mind</a> is perfectly
fine regarding that (I was even able to capture notes by using <a href="https://github.com/tmux/tmux">tmux</a>’’s <code>display-popup</code> with <a href="https://github.com/phaazon/mind">mind</a>), I wanted to
switch to something… easier, yet still UNIX.</p>
<p>Then I started to think again about <a href="https://orgmode.org/">org-mode</a>. See, <a href="https://orgmode.org/">org-mode</a> is eventually just a format specification. The Emacs
mode is the reference implementation, with major modes, keybindings etc. but if Emacs can edit and do stuff to <code>.org</code>
files, then the concept is transposable to other tools. The question is: what is the core of <a href="https://orgmode.org/">org-mode</a>? The idea
behind <a href="https://orgmode.org/">org-mode</a> is that there is no state: the notes themselves are the state. There is no need for an external tool.
Any tool can open an <code>.org</code> file and edit it. Yes, convenience is better to navigate <code>.org</code> notes, but that’s just a
matter of tooling. If you want to open and edit an <code>.org</code> file using your favorite editor, you should be able to.</p>
<p>Then, for the rest, <a href="https://orgmode.org/">org-mode</a> has a couple of interesting and useful concepts:</p>
<ul>
<li>You write notes in text files, including metadata. So tasks management is also done via <code>.org</code> files.</li>
<li>Listing tasks, for instance, can be performed pretty easily by grepping headings. For instance, <code>* TODO</code> can be used
at the beginning of a line to start a TODO item, that will appear in the listing. That looks arrogantly innocent, but
it’s really powerful, and I’ll explain why just below.</li>
<li><a href="https://orgmode.org/">org-mode</a> has the concept of a <em>capture</em> buffer. The idea is simple: you open it to add stuff right off your mind,
and you come back to it later to triage its content. This is a fantastic concept, because very often, I need to take
notes about things I have to do, but I have no structure yet. For instance,
<code>* TODO talk to Bob about service-epsilon</code>. Later on, I might create a note <code>Epsilon</code> and move the <code>TODO</code> item into
the note. Since there is no state, the <code>TODO</code> will still appears in my todo-list.</li>
<li>Another important thing is <em>archiving</em>. You are encouraged to create many small notes, even for task management. At
work, I currently have a note called <code>Back from holiday Oct 2023</code>, which contains many <code>TODO</code> items taken from the
backlog I read on Slack (basically, stuff my colleagues did and that I want to catch up with). At some point, I will
be done with all of that, so instead of keeping <code>DONE</code> notes around (that will show up in my <code>DONE</code> listing), I can
just archive that note. It will not appear anymore in my fuzzy search results.</li>
</ul>
<p>All of that is really exciting, because it makes note taking easy and seamless; it doesn’t require any state, and you
can pick the tools you want to implement the workflow.</p>
<h1>It’s not a tool; it’s a workflow</h1>
<p>Yes, that’s what I think is the most important thing here. I do not want to use <a href="https://orgmode.org/">org-mode</a> for now. I like Markdown.
Even though I agree <code>.org</code> files are more powerful, Markdown, at least for now, is more than enough to me. However, I
wanted to augment it to do things slightly similar to <a href="https://orgmode.org/">org-mode</a>.</p>
<p>And so I did it. I created in my <a href="https://kakoune.org">kakoune</a> a couple of commands to:</p>
<ul>
<li>Open a capture buffer located at the same place on my disk. It defaults to <code>~/notes/capture.md</code>.</li>
<li>Quickly capture a line via <a href="https://kakoune.org">kakoune</a>’’s user prompt and append it to the capture buffer (super useful for todo items).</li>
<li>Create a new note in <code>~/notes/notes/</code>.</li>
<li>Archive a note into <code>~/notes/archives/</code>.</li>
<li>List / fuzzy search notes.</li>
<li>List / fuzzy search archives.</li>
<li>Open a daily journal in <code>~/notes/journal/&lt;year&gt;/&lt;month&gt;/&lt;day&gt;.md</code>. It’s super easy with <a href="https://kakoune.org">kakoune</a> since you have
<code>%sh{}</code> expansion. Just use the <code>date</code> command you’re done!</li>
<li>List daily journals.</li>
<li>Open task list by:
<ul>
<li><code>TODO</code>, <code>WIP</code>, <code>DONE</code> or <code>WONTDO</code> items.</li>
<li>Those items are put into a special buffers on which I have set <code>buffer</code> keybindings to jump to the place where the
item is located. For instance, if I list <code>TODO</code> items, pressing <code>&lt;ret&gt;</code> on a line will open the appropriate file and
move my cursor to the right line and column where the <code>TODO</code> item is defined.</li>
<li>Labels, like <code>:learn:</code> or <code>:hard:</code> or whatever.</li>
</ul>
</li>
<li>Local (<code>buffer</code>) keybindings to switch tasks to different status.</li>
<li>A special convenience I created: Markdown files containing a special <code>&lt;!-- github_project: &lt;user&gt;/&lt;project&gt; --&gt;</code>,
usually at the end of the file, allows me to press a keybinding on issue numbers (like <code>#123</code>) to automatically open
them in my browser.</li>
<li>Synchronize notes with a single keybinding, which is running some <code>git</code> commands to rebase / push.</li>
<li>And many more similar features.</li>
</ul>
<blockquote>
<p>I plan on releasing the <code>.kak</code> file containing all of that if people are interested.</p>
</blockquote>
<p>The great thing is that most of the features, like fuzzy searching, are implemented via external tools. I use <code>fd</code> and
<code>rg</code> fuzzy search todo items. There is nothing to do in <a href="https://kakoune.org">kakoune</a> besides gluing everything together.</p>
<p>This setup has been my daily driver for months now and I really enjoy it. Being able to take notes and just slide in a
<code>- TODO I need to check that</code> right in the middle of the file and having it appear in a listing later is a really
powerful aspect of this workflow… and <a href="https://orgmode.org/">org-mode</a> was doing that all along ever since. Eh, it almost makes me nostalgic
of my time using the Emacs plugin!</p>
<p>I hope that gave me some (not so fresh) hindsight about note taking and task management (and journaling!). Have fun
hacking around!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 12 Oct 2023 20:35:00 GMT</pubDate></item><item><title>Volumetric light shafts</title><link>https://strongly-typed-thoughts.net/blog/volumetric_light_shafts</link><description><![CDATA[<h1>Volumetric light shafts</h1>
<p>On the last weekend, I was at the Revision demoparty (Easterparty). It’s a
meeting of friends and great people doing graphics, music, code and that kind
of fun and pretty stuff. I didn’t release anything, but I’ve been working on
a release for a while now, and I was lacking something in my engine: <em>light
shafts</em>.</p>
<p>I know a few ways to do that. They almost all fall in one of the two following
classes:</p>
<ol>
<li>screen-space-based;</li>
<li>raymarching-based.</li>
</ol>
<p>Both the techniques produce interesting images. However, the <em>screen-space-based</em>
method produces bad results when you don’t look directly to the light – it may
even produce nothing if the light is out of screen – and the <em>raymarching-based</em>
is a step process that might generate artifacts and can be slow.</p>
<p>The idea I had is <strong>very simple</strong>. I haven’t tested it yet, but it’s planned for
very soon. I’ll post images with benchmarks as soon as I have something on screen.
I’m not sure it’s an unknown way to do it. I just haven’t found anything
describing that yet. If you have, please leave a link in the comments below! :)</p>
<h1>Volume extraction</h1>
<p>The idea is the following. You need a depthmap. If you’re used to <em>shadow mapping</em>
you already have a depthmap for your light. In order to simplify this, we’ll use the
depthmap used for shadow mapping, but I guess it’s totally okay to use another
depthmap – we’ll see that it could be even handier.</p>
<p>For each point in that depthmap is the distance – in a specific space coordinates
system – of the corresponding point in world space to the
position of the light. If you have the projection and the view matrices of the
light, it’s easy to deproject the depthmap. What would we get if we deproject
<strong>all the depthmap texels</strong> into the world space? We’d get the exact lit surfaces.</p>
<p>For a spot light, you can imagine the deprojected version of the depthmap as a cone
of light. The “disk” of the cone will deform and shape as the lit environment. That’s
the first part of the algorithm.</p>
<p>We have a points cloud. What happens if, for each deprojected texel – i.e. point – we
draw a line to the position of the light? We get an actual lines field representing…
photons paths! How amazing is that?! Furthermore, because of the depthmap sampling,
if you look at the light and that an object is between you and the light, the photons
paths won’t go through the obstructing object! Like the following image:</p>
<p><img src="http://i280.photobucket.com/albums/kk167/Bea_Douglas/ShaftsofLightMountBaker-SnoqualmieN.jpg" alt="" /></p>
<p>Of course, because of using raw lines, the render might be a bit blocky at first. If you
know the <em>laser trick</em><sup class="footnote-reference"><a href="#1">1</a></sup> – i.e. quad-based lasers, you can apply it to our lines as
well, in order to get better results. The first improvement is to disable depth
test and enable additive blending.</p>
<h1>Algorithm</h1>
<p>In the first place, you need to generate the depthmap of the light. Then, you need to extract
the volumetric shaft. You’ll need a vertex buffer for that. Allocate <strong>w*h</strong> elements, where
<em>w</em> and <em>h</em> are the depthmap’s <em>width</em> and <em>height</em>. Yeah, a point per texel.</p>
<p>Create a shader and make a render with <code>glDrawArrays(GL_POINTS, 0, w*h)</code> with an <em>attributeless
vertex array object</em>. Don’t forget to bind the depthmap for use in the shader.</p>
<p>In your vertex shader, use <code>gl_VertexID</code> to sample from the depthmap. Transform the resulting
texel in world-space. You can use something like the following deprojection formula:</p>
<pre><code class="language-C">vec3 deproject() {
  float depth = 2. * texelFetch(depthmap, ivec2(gl_FragCoord.xy), 0).r - 1.;
  vec4 position = vec4(vv, depth, 1.);
  position = iProjView * position;
  position.xyz /= position.w;
  return position.xyz;
}
</code></pre>
<p>Pass that to the next stage, the geometry shader. There, build whatever kind of new primitive
you want. In the first place, I’ll go for a simple line connected to the light’s position. In
further implementation, I’ll go for lasers-like base shapes, like star-crossed quads.</p>
<p>In the fragment shader, put whatever color you want the shaft to have. You could use
interpolation to reduce lighting wherever you want to create nice effects.</p>
<p>Don’t forget to use additive blending, as we do for lasers.</p>
<h1>Considerations</h1>
<p>I see two major problems. The first one is the bright aspect the shaft will have if you don’t
blend correctly. Play with alpha to reduce more if the fragment is near the light’s position and
make the alpha bigger when you’re far away from the light’s position. Because you’ll blend way more
photons paths near the light’s position than far from it.</p>
<p>The second issue is the resolution of the extracted volume. For a 512x512 depthmap, you’ll get
around 262k points, then 524k lines. That’s a lot for such an object. And that’s only for a simple
spot light. An omnidirectional light would require six times more lines. What happens if we don’t
use lines, but star-crossed quads an that we want several shafts? You see the problem.</p>
<p>A solution could be to sample from high mipmap level, so that you don’t use the full resolution of
the shadow map. That would result in less visual appealing shafts, but I’m pretty sure it’d be still
good. You could also branch a blur shader to smooth out the whole thing.</p>
<h1>Conclusion</h1>
<p>I’ll try to implement that as soon as possible, because I think my idea is pretty interesting compared
to raymarching, which is expensive, and way better than screen-space, because the shaft will still be
visible if the light goes out of screen.</p>
<div class="footnote-definition" id="1"><sup class="footnote-definition-label">1</sup>
<p>I’ll write an article about it if you don’t – leave a comment for asking</p>
</div>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 08 Apr 2015 00:00:00 GMT</pubDate></item><item><title>Vim vs. Kakoune puzzles</title><link>https://strongly-typed-thoughts.net/blog/vim-kakoune-puzzles-2025</link><description><![CDATA[<p>I have come across <a href="https://freestingo.com/en/programming/articles/esoteric-vim/">Esoteric Vim</a> lately, an
article that shows how to perform various advanced text manipulation in Vim. It dates from February 2025, so
it’s a recent article about modern Vim. This article shows exactly why I think Vim is outdated, and why I
think <a href="https://kakoune.org/">Kakoune</a> — and by extension, since it’s based on it, <a href="https://helix-editor.com/">Helix</a> as well — has a much better text edition
design. The goal of this article is to take all the puzzles form that exercise, and demonstrate how I would
solve them in a real-world, practical way with Kakoune.</p>
<blockquote>
<p>Most of the things I’m going to show here in Kakoune are also doable in Helix, but Helix is not as mature
as Kakoune, so it’s likely some features won’t be available at the time of writing this blog article,
May 2025.</p>
</blockquote>
<p><strong>Disclaimer</strong>: as always with programming tools, <em>use the tool you are comfortable with</em>. This article only
reflects the way I see editing; it doesn’t mean one tool is superior to another in an absolute way. It’s just
a way for me to share why <em>I</em> think Kakoune (Helix) is better, and provide some hindsight on both editors.</p>
<blockquote>
<p><strong>Thank you</strong>: thank you <code>@Screwtape</code> for reviewing this article!</p>
</blockquote>
<!--toc:start-->
<ul>
<li><a href="#1-assigning-numbers-to-existing-enum-classes">1. Assigning numbers to existing enum classes</a></li>
<li><a href="#2-fixing-verbose-sql-insert-scripts">2. Fixing verbose SQL insert scripts</a>
<ul>
<li><a href="#drop-all-selections-but-the-primary">Drop all selections but the primary</a></li>
<li><a href="#filter-selections">Filter selections</a></li>
<li><a href="#just-do-it-manually">Just do it manually</a></li>
</ul>
</li>
<li><a href="#3-extract-field-names-from-templates">3. Extract field names from templates</a></li>
<li><a href="#4-batch-process-multiple-files">4. Batch process multiple files</a></li>
<li><a href="#5-integrate-data-from-external-sources-into-your-project">5. Integrate data from external sources into your project</a>
<ul>
<li><a href="#execute-keys"><code>execute-keys</code></a></li>
</ul>
</li>
<li><a href="#kakoune-has-an-incredible-design">Kakoune has an incredible design</a></li>
</ul>
<!--toc:end-->
<h1 id="1-assigning-numbers-to-existing-enum-classes"><a href="#1-assigning-numbers-to-existing-enum-classes">🔗</a> 1. Assigning numbers to existing enum classes</h1>
<p>The example is this:</p>
<pre><code class="language-java">public enum TranslationCodeId
{
   // buttons
   BUTTON_Accept,
   BUTTON_Cancel,

   // labels
   LABEL_Actions,
   LABEL_Column,

   // messages
   MESSAGE_Confirm,
   MESSAGE_GoBack,

   // more fields...
}
</code></pre>
<p>Vim does it with two different actions:</p>
<ul>
<li>Assign <code>0</code> to all enum fields: <code>:g/,$/v;//;norm $i = 0</code>.</li>
<li>Increment all numeric values sequentially: <code>vi}o2/0,$&lt;Enter&gt;g&lt;C-a&gt;</code>.</li>
</ul>
<hr />
<p>How to do that with Kakoune? The first thing is to be able to get a selection on each enum field. We can use
a similar trick as what Vim does by targetting the line ending with a <code>,</code> with <code>s,$</code>. Because we assume — like
in Vim — that this enum definition is the whole buffer, we can work on the whole buffer with the <code>%</code> key
(similar to the <code>g/</code> of Vim). So we simply just type <code>%s,$&lt;ret&gt;</code>.</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part1-1.png" alt="" /></p>
<p>From now, the rest is really trivial, as we have a selection on each <code>,</code>. We can then simply type <code>i = &lt;c-r&gt;#</code>,
which will enter <code> = N</code> before every column, where <code>N</code> will have the right value.</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part1-2.png" alt="" /></p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part1-3.png" alt="" /></p>
<blockquote>
<p>Some explanations: pressing control-r will allow the next key to select a register that will be pasted after
all selections. Kakoune has many built-in registers, and <code>#</code> is the <em>selection index</em> register; it gives you
the index of each individual selection, starting from <code>1</code>, ascending.</p>
</blockquote>
<h1 id="2-fixing-verbose-sql-insert-scripts"><a href="#2-fixing-verbose-sql-insert-scripts">🔗</a> 2. Fixing verbose SQL insert scripts</h1>
<p>This one wants us to fix the ending characters of <em>some</em> lines:</p>
<pre><code class="language-sql">INSERT INTO AdminTranslationCodeText
    (AdminTranslationCodeTextId, AdminTranslationCodeId, LanguageId, Text)
    VALUES
    (NEWID(), 'BUTTON_Accept', 'it', 'Accetta'),
    (NEWID(), 'BUTTON_Accept', 'en', 'Accept'),

    (NEWID(), 'BUTTON_Cancel', 'it', 'Annulla'),
    (NEWID(), 'BUTTON_Cancel', 'en', 'Cancel'),

    (NEWID(), 'LABEL_Actions', 'it', 'Azioni'),
    (NEWID(), 'LABEL_Actions', 'en', 'Actions'),

    (NEWID(), 'LABEL_Column', 'it', 'Colonna');
    (NEWID(), 'LABEL_Column', 'en', 'Column'),

    (NEWID(), 'MESSAGE_Confirm', 'it', 'Conferma'),
    (NEWID(), 'MESSAGE_Confirm', 'en', 'Confirm');

    (NEWID(), 'MESSAGE_GoBack', 'it', 'Torna indietro'),
    (NEWID(), 'MESSAGE_GoBack', 'en', 'Go back'),
GO
</code></pre>
<p>The Vim command: <code>:/VALUES$/+,/^GO$/-2s/;$/,/ | /^GO$/-s/,$/;/</code>. No need to comment on it; it’s barely
impossible to tell what it does in a quick glance; we have to decode it, and none of it will be executed
before you actually run the whole thing. It does a lot of things, and you can see a bunch of text that has to
be typed there, like <code>VALUES</code> <code>GO</code>, negative relative lines, etc.</p>
<hr />
<p>The Kakoune version here is different, and, as expected, more interactive. The first thing we want to
do, as always, is to get a cursor on the lines starting with a <code>NEWID</code>, and put each cursor at the end of each
line. We still use <code>%</code> to select the whole buffer, <code>sNEW&lt;ret&gt;</code> to select all <code>NEW</code>, and <code>gl</code> to move all
selections on the last character, which will be a <code>,</code> or a <code>;</code>, depending on the line:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-1.png" alt="" /></p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-2.png" alt="" /></p>
<p>Then, simply type <code>r,</code> to replace all ending symbols with <code>,</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-3.png" alt="" /></p>
<p>We now need to clean up the last line, since it requires a <code>;</code>. We have a couple solutions, that I will
explain.</p>
<h2 id="drop-all-selections-but-the-primary"><a href="#drop-all-selections-but-the-primary">🔗</a> Drop all selections but the primary</h2>
<p>I like this one a lot because it’s very interactive and intuitive. Kakoune has the concept of a <em>primary</em>
selection and <em>secondary</em> selections. The primary selection is treated specifically from others, as it’s used
to perform some specific actions. For instance, imagine you select a function and all of its occurrences.
Depending on where your primary selection is, the context action you might get from e.g. LSP will be
different.</p>
<p>Kakoune allows you to cycle the primary selection with secondary ones with <code>(</code> and <code>)</code>. Usually, highlighting
will tell which selection is the primary — in my screenshot above, the primary is pink-like; the secondary
ones are white-like. If you have typed the keys as mentioned below, you should notice
that the primary selection is actually on the last line, which is already perfect! If it’s not, you can use
<code>(</code> and/or <code>)</code> to adjust it; we want the last line to have the primary selection.</p>
<p>Then, just press <code>,</code>. That key will drop all selections but the primary one, leaving a single selection on the
last <code>,</code>. You then know what to do: <code>r;</code>. And we’re done!</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-4.png" alt="" /></p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-5.png" alt="" /></p>
<h2 id="filter-selections"><a href="#filter-selections">🔗</a> Filter selections</h2>
<p>Another thing that I use from time to time is to filter (keep) or discard (keep non-matching) selections. This
is useful to filter selections based off the already selected, disjoint selections. We use the <code>&lt;a-k&gt;</code> key
chord for that. In our case, we can see that the last line has the word <code>back</code> in it, which is unique to our
selections. We can select whole lines with <code>x</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-6.png" alt="" /></p>
<p>And then <code>&lt;a-k&gt;back&lt;ret&gt;</code> to select that line:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part2-7.png" alt="" /></p>
<blockquote>
<p>Note: <code>&lt;a-k&gt;</code> (keep matching) is different from <code>s</code> (select) in the sense that it will <em>filter</em>
selections, while <code>s</code> will select whatever you type, discarding already existing selections. Try them out
to experiment!</p>
</blockquote>
<p>Then simply do the previous thing: <code>glr;</code>.</p>
<h2 id="just-do-it-manually"><a href="#just-do-it-manually">🔗</a> Just do it manually</h2>
<p>Something that I think is important to notice is that, in most cases, you will most likely just move your
cursor there and modify the character manually. Sometimes, it’s much faster to drop all selections and
manually edit a bunch of characters instead of trying to be smart and spending mental effort for no real
benefit.</p>
<p>For this reason, I think the first option is clearly the way-to-go solution: just press <code>,</code>, and the cursor
will already be at the right place. Kakouine <em>encourages</em> visual exploration and interactivity; stop
thinking too much about how to edit stuff in advance, and do it gradually, incrementally!</p>
<h1 id="3-extract-field-names-from-templates"><a href="#3-extract-field-names-from-templates">🔗</a> 3. Extract field names from templates</h1>
<p>This one is absolutely crazy in Vim. Based on this text:</p>
<pre><code>{{#if UserDetails.FirstName}}Nome: {{UserDetails.FirstName}}{{/if}}
{{#if UserDetails.LastName}}Cognome: {{UserDetails.LastName}}{{/if}}
{{#if CallbackDetails.CallbackTimeStamp}}Data e orario indicati dal cliente per la chiamata: {{dateFormat CallbackDetails.CallbackTimeStamp format="d"}}.{{dateFormat CallbackDetails.CallbackTimeStamp format="M"}}.{{dateFormat CallbackDetails.CallbackTimeStamp format="Y"}} {{dateFormat CallbackDetails.CallbackTimeStamp format="H"}}:{{dateFormat CallbackDetails.CallbackTimeStamp format="m"}}{{/if}}
{{#if UserMotivation.ReasonSell}}Motivo della vendita: {{UserMotivation.ReasonSell}}{{/if}}

Informazioni sull'immobile:

{{#if PropertyDetails.ObjectType}}Categoria: {{PropertyDetails.ObjectType}}{{/if}}
{{#if PropertyDetails.ObjectSubType}}Tipo di oggetto: {{PropertyDetails.ObjectSubType}}{{/if}}
{{#if PropertyDetails.Street}}Via: {{PropertyDetails.Street}} {{#if PropertyDetails.Number}}{{PropertyDetails.Number}}{{/if}}{{/if}}
{{#if PropertyDetails.Zipcode}}Luogo: {{PropertyDetails.Zipcode}}{{/if}} {{#if PropertyDetails.City}}{{PropertyDetails.City}}{{/if}}

Valutazione immobiliare: 

{{#if ValuationDetails.EstimatedMarketValue}}Prezzo di mercato stimato: {{ValuationDetails.EstimatedMarketValue}}{{/if}}
{{#if ValuationDetails.MinimumPrice}}Prezzo minimo:  {{ValuationDetails.MinimumPrice}}{{/if}}
{{#if ValuationDetails.MaximumPrice}}Prezzo massimo:  {{ValuationDetails.MaximumPrice}}{{/if}}
</code></pre>
<p>We want to extract all field names from that Handlebars template snippet. To do that in Vim, you need to
store the names in a register, remove duplicate lines, and format the result. Let’s see how to do that in
Kakoune.</p>
<hr />
<p>We start as always by selecting the whole buffer and follow a similar idea as in Vim by selecting the <code>if</code>,
but we will use the <code>#</code> as well — I think it’s more correct than using the trailing whitespace: <code>%s#if&lt;ret&gt;</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-1.png" alt="" /></p>
<p>That gives us a selection on each <code>#if</code>. We can then press <code>w</code> twice to fully select all field names:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-2.png" alt="" /></p>
<p>Yank them with <code>y</code>, and drop your selections with <code>,</code>. As with Vim, move your cursor in an empty location, and
paste your selections with <code>&lt;a-p&gt;</code>.</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-3.png" alt="" /></p>
<blockquote>
<p>We cannot use <code>p</code>, because <code>p</code> pastes with a 1:1 mapping between selections. What it means is that <code>p</code>
is selection-aware, so if you have two selections containing <code>foo</code> and <code>bar</code>, yank them, then move <em>both</em>
selections to somewhere else and press <code>p</code>, <code>foo</code> will be pasted to wherever the first selection is, and
<code>bar</code> will be pasted on the other place. This is incredibly powerful, but here, since we have dropped all
selections, pasting with <code>p</code> will only paste the content of the primary selection. <code>&lt;a-p&gt;</code> will, as the
doc says, paste all the selections one after the other, and will put a selection after each of them, which
is exactly what we want.</p>
</blockquote>
<p>Once pasted, you will notice they are all on the same line. Simply press <code>i&lt;ret&gt;&lt;esc&gt;x</code> to put them ont
different lines and ensure you have a training newline on each of them:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-4.png" alt="" /></p>
<p>In order to sort them, we need to end up with a single selection containing them all. We could do this by
dropping all selections with <code>,</code>, then selecting the whole paragraph with <code>&lt;a-i&gt;p</code>, but Kakoune has a key for
this exact situation: <code>&lt;a-_&gt;</code>, which merges continuous selections, across lines:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-5.png" alt="" /></p>
<p>Then simply press <code>|sort -u&lt;ret&gt;</code> to sort them via the <code>sort</code> program:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-6.png" alt="" /></p>
<p>Now, you can get a selection back on each line by pressing <code>&lt;a-s&gt;</code>, which simply adds a cursor at the end of
each line in the current selections — note the plural; if you had several disjoint selections all spanning
on several lines, it would apply this logic to <em>all of them</em>, which is really powerful:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-7.png" alt="" /></p>
<p>Press <code>;</code> to reduce all selections to a single character (anchor = cursor):</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-8.png" alt="" /></p>
<p>And then simply go with <code>r,</code> to format the whole thing as a single comma-separated line:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part3-9.png" alt="" /></p>
<p>You can drop selections to keep the primary (last one) and use the same trick as before to remove the
trailing <code>,</code>: <code>,d</code>.</p>
<p>It might sound like a lot is going on, but it actually is. The great thing is that everything is
interactive, and you see what you are doing. Meanwhile, most of that in Vim occurs without you noticing
everything, requiring to type a lot of normal commands and crazy substitutions. A single selection mistake
will require you to go back from the beginning.</p>
<h1 id="4-batch-process-multiple-files"><a href="#4-batch-process-multiple-files">🔗</a> 4. Batch process multiple files</h1>
<p>This one is pretty fun too. Given a <code>git status</code> output, we want to execute a git command on each line.</p>
<blockquote>
<p>Note: I would have actually run a single <code>git update-index</code> command with all the paths as arguments instead,
but that defeats the exercise, so let’s stick to just calling the git command for each path instead.</p>
</blockquote>
<p>The output is:</p>
<pre><code>On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git restore &lt;file&gt;..." to discard changes in working directory)
   modified:   ExcelGenerator/App.Debug.config
   modified:   ExcelGenerator/App.config
   modified:   Core/ConfigBase.cs
   modified:   Infrastructure/Security/Password.cs
   modified:   Tools.AgentExport/App.Debug.config
   modified:   Tools.AgentExport/App.config
   modified:   Tools.CheckLeadsForConfirmedEmail/App.Debug.config
   modified:   Tools.CheckLeadsForConfirmedEmail/App.config
   modified:   Tools.EmailSender/App.Debug.config
   modified:   Tools.EmailSender/App.config
   modified:   Tools.LeadChecker/App.Debug.config
   modified:   Tools.LeadChecker/App.config
   modified:   Tools.LeadImport/App.Debug.config
   modified:   Tools.LeadImport/App.config
   modified:   Tools.LeadManager/App.Debug.config
   modified:   Tools.LeadManager/App.config
   modified:   Tools.LeadReminder/App.Debug.config
   modified:   Tools.LeadReminder/App.config
   modified:   Tools.SmsSender/App.Debug.config
   modified:   Tools.SmsSender/App.config
   modified:   Web.Backend/Web.Backend.csproj
   modified:   Web.Backend/Web.Debug.config
   modified:   Web.Backend/Web.config
</code></pre>
<p>In Vim, we need to run a bunch of commands: bring the output of <code>git status</code> in the buffer, remove lines
not containing a <code>modified</code> path, map all lines to git commands, and execute all lines as <code>bash</code> commands.</p>
<p>Let’s see how I would do that in Kakoune.</p>
<hr />
<p>First, as a disclaimer, I need to mention that Kakoune has a support for <code>git</code> via its standard distribution
and the <code>:git</code> command. It would be cheating, so I will not use it.</p>
<p>From an empty buffer, let’s bring in the output of <code>git status</code> by simply typing <code>!git status</code> — let’s change
the output to match whatever the exercise has:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part4-1.png" alt="" /></p>
<p>Then, it’s pretty simple: <code>%smod&lt;ret&gt;wwlGl</code> to select all paths:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part4-2.png" alt="" /></p>
<p>And it’s almost done. Press <code>&lt;a-|&gt;</code> to pipe each selection as standard input of the program of your choice,
and use <code>xargs git update-index --skip-worktree &lt;ret&gt;</code>. This will run that program with each selection as
argument.</p>
<h1 id="5-integrate-data-from-external-sources-into-your-project"><a href="#5-integrate-data-from-external-sources-into-your-project">🔗</a> 5. Integrate data from external sources into your project</h1>
<p>Another crazy one from Vim: from a text dump of some random data (IP, masks, subnets), we want to create some
C# code that will whitelist those IP addresses. The goal is to format around the data, and it’s really bad
for Vim, because its regex thing is <strong>absolutely disgusting</strong>, while Kakoune was designed <strong>specifically</strong>
with that kind of usecase in mind.</p>
<p>Just so you see it, the Vim way requires to format the data, extract texts in many registers, map the data
to actual code, and other things, which results in those lines:</p>
<pre><code>`[v`]:'&lt;,'&gt;s/^.\(\d*\).\{-}\(255.*\)\t.*/'\1': '\2',/ | '&lt;,'&gt;j | s/.*/{ &amp; }/
/new Re&lt;Enter&gt;"ayf";;"byf";;"cy$
`[v`]:'&lt;,'&gt;g/^$/d
gv:'&lt;,'&gt;s/\(.*\)\/\(.*\)$/\='&lt;C-r&gt;a' . submatch(1) . '&lt;C-r&gt;b' . &lt;C-r&gt;m[submatch(2)] .  '&lt;C-r&gt;c'
</code></pre>
<hr />
<p>Now let’s see how to do that in Kakoune. We start with the same buffer as Vim:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-1.png" alt="" /></p>
<p>I think the best approach here is to use a macro — yes! Kakoune has them! — and a wonderful feature of Kakoune
regarding selections: executing arbitrary keys on each of the selection. First, let’s paste one IP address:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-2.png" alt="" /></p>
<p>We can start the macro by pressing the <code>Q</code> key — the macro will be registered to the <code>@</code> register by default.
Start by moving at the end of the current line with <code>gl</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-3.png" alt="" /></p>
<p>Then, we are going to do a reverse character search, yank the subnet mask index, and put a <em>mark</em> to save
the current selection with <code>&lt;a-t&gt;/yZ</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-4.png" alt="" /></p>
<blockquote>
<p><code>Z</code> here is a feature that is not available on Helix; you can probably use the jump list’s <code>&lt;c-s&gt;</code> feature
in Helix there, but I haven’t tried.</p>
</blockquote>
<p>Now, the default <code>"</code> register has the mask index. We can just do a regular search of <code>/N</code> with <code>N</code> the mask
index with the <code>//&lt;c-r&gt;"\t&lt;ret&gt;</code> keys:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-5.png" alt="" /></p>
<p>We can move the cursor to the right to select the actual mask value with <code>wwl?\tH</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-6.png" alt="" /></p>
<blockquote>
<p><code>?</code> is the extend version of <code>/</code>. We need to use this because the values are separated by <code>\t</code>. If it was
a regular space, we could have used <code>t </code> instead.</p>
</blockquote>
<p>Now, let’s yank that value in the <code>m</code> register to be sure we won’t lose it with <code>"my</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-7.png" alt="" /></p>
<p>Press <code>z</code>. It will restore your selection back to the subnet index in the IP address:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-8.png" alt="" /></p>
<p>From this place, we can start formatting the line. Let’s copy the IP address into the <code>i</code> register, and remove
the rest. Go to the beginning of the line with <code>gi</code>, select up to the <code>/</code> with <code>t/</code>, and <code>"iy</code> to yank the IP
address into the <code>i</code> register:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-9.png" alt="" /></p>
<p>Replace the whole line with <code>x_</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-10.png" alt="" /></p>
<p>Press <code>r</code> and start writing your code. Whenever you need to put the IP address, use <code>&lt;c-r&gt;i</code>. To put the mask,
use <code>&lt;c-r&gt;m</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-11.png" alt="" /></p>
<p>And now that this is done, finish recording with <code>Q</code>. Now, for every more lines we have an IP address on, just
press <code>q</code> to replace it with the code:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-12.png" alt="" /></p>
<p>However, there is a better feature of Kakoune we can use here.</p>
<h2 id="execute-keys"><a href="#execute-keys">🔗</a> <code>execute-keys</code></h2>
<p><code>execute-keys</code> — which we often use abbreviated <code>exec</code> — allows to type keys as if they were typed by the user
in a more controlled way. For instance, this command accepts the <code>-itersel</code> flag, which runs the command for
every selection. Along with the <code>-with-maps</code> (required for macros, I think?), we can select all IP addresses,
spawn a selection on each of them with <code>&lt;a-s&gt;</code> as seen previously:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-13.png" alt="" /></p>
<p>And then press <code>:exec -itersel -with-maps q</code>:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-14.png" alt="" /></p>
<p>Press <code>&lt;ret&gt;</code>. Taaadaaa:</p>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/vim-kakoune-2025-part5-15.png" alt="" /></p>
<h1 id="kakoune-has-an-incredible-design"><a href="#kakoune-has-an-incredible-design">🔗</a> Kakoune has an incredible design</h1>
<p>The design of Kakoune shines here. You can see that you can do everything by incrementally and visually
progress through your mental model, instead of having to think of everything in advance. The fact that you can
use regular commands such as <code>w</code>, <code>y</code>, <code>d</code>, <code>f</code> etc. on many selections at once creates such a friendly
environment to implement very complex workflows, without needing to use the crazy regex and substitute
commands from Vim.</p>
<p>I hope that blog article provided a good hindsight on how we can do things in Kakoune vs. Vim, and why many
people — me included — prefer the modal editing model of Kakoune.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 21 May 2025 00:20:00 GMT</pubDate></item><item><title>My thoughts about editors in (early) 2021</title><link>https://strongly-typed-thoughts.net/blog/editors-in-2021</link><description><![CDATA[<p>For the past year, I have been moving back and forth between <a href="https://neovim.io">Neovim</a>, my main, daily editor, and <a href="https://github.com/hlissner/doom-emacs">Doom Emacs</a>, an
<a href="https://www.gnu.org/software/emacs">Emacs</a> configuration distribution focusing on bringing a super consistent, powerful and overall top-notch user
experience for <a href="https://www.gnu.org/software/emacs">Emacs</a>. This article is a direct follow-up to the
<a href="https://phaazon.net/blog/editors-in-2020">previous one</a> from fall 2020.</p>
<p>In this blog entry, I want to share something a bit different: instead of presenting my experiences with all those
editors, I want to take some hindsight and analyze a bit more the technology and what it seriously means in terms of
features and productivity. Obviously, I will reference other editors I have used a lot (I use <a href="https://www.jetbrains.com/idea">IntelliJ</a> a lot at work,
and I am used to <a href="https://code.visualstudio.com">VS Code</a> and <a href="https://atom.io">Atom</a> as well).</p>
<h1>Current status as of March 2021: I am lost</h1>
<p>Yeah, you read that title right. I am lost. I have been a Vim / Neovim user for something like 12 years now. I am used to
the ecosystem, I have installed hundreds of plugins in the past decade, tried so many things, discovered pearls that I
still use today (hello <a href="https://github.com/wellle/context.vim">context.vim</a>), I have been using a workflow based on <a href="https://github.com/tmux/tmux">tmux</a>, Neovim and my shell that has proven
very powerful and yet to be fully replaced. But — and I think it is important to make that note — I did not start coding
with Vim. Today, I am 29. I started coding when I was 11, using a tool called <a href="https://www.codeblocks.org">Code::Blocks</a>. Rapidly, I switched to
Emacs – by the time, Doom Emacs did not even exist, and spent several years using it. Later, when I was between 16
and 17, I switched to Vim — I guess the modal idea got me intrigued and I felt in love with it. In 2014, I was told that
Vim was getting forked for the reasons we all know and I decided to switch to the fork, called Neovim.</p>
<p>Since then, I have been sticking to Neovim but I do not partake in those “editor wars”. “VS Code sucks”, “how do I even
exit Vim?”, “Emacs is bloated!”. I have my personal opinions on the technologies (i.e. I dislike very much the front-end
ecosystem / electron, even though I think the front-end idea — i.e. running code in a browser — is an interesting idea;
I just think the current technology for doing so is really bad), so obviously I will not pick something such as Atom /
VS Code, but I cannot say they suck. I have tried them. I see why people like them and it would be very dishonest to say
those editors suck. They really do not. Same thing applies to the products by JetBrains. In the past year, I did not
understand why some people would pay money for an editor. I have to use IntelliJ at work and even though I would not pay
for an editor, I start to grasp why people would actually want to pay for one. Not all people have the patience and time
to tweak an editor to their taste, such as with Vim / Neovim / Emacs. For those people, editors such as VS Code,
IntelliJ, Atom and others are really just superior.</p>
<p>Just take <a href="https://microsoft.github.io/language-server-protocol">LSP</a>. Configuring LSP in VS Code is a click on an install button, depending on your language. That is all.
You get all the good defaults and behaviors and you can already start enjoying coding with LSP. Doing the same in an
editor like Neovim is a very different experience. I am not saying it is bad; <strong>it is different</strong>. If you are, like me,
someone who enjoys spending time tweaking their tools, then you should not care too much. I have a colleague at work who
gave me an argument that I find very interesting: he basically wants an editor he does not have to configure, so that we
knows how to use it everywhere, on any machine of any colleague. That is not a point I would make, but for someone who
cares about that topic, it is a solid argument in favor of tools such as IntelliJ or VS Code.</p>
<p>In the previous company I was working for, I worked with <a href="https://github.com/gagbo">@gagbo</a>, who is an active Doom Emacs contributor, and I was
surprised by the editor he was using on his laptop. He mentioned Doom and on the night home, I had a look at it, and I
was really blown away. Doom Emacs (Doom for short) was a revelation to me, because it is using emacs as a productivy
platform more than a simple editor. <a href="https://github.com/hlissner">@hlissner</a>, Doom’s author, and the Doom community, have been gathering the best of
emacs and vim into a single configuration distribution. The result is so interesting and so powerful that I have been
doing back and forth between Doom and Neovim. More on that below.</p>
<p>Lately, I have been more present in the Neovim community, contributing mostly via a plugin that I have been making:
<a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>. That plugin is a complete rewrite from scratch of <a href="https://github.com/easymotion/vim-easymotion">EasyMotion</a> (I have not even looked at how they do it on
their side) I made by using the Neovim Lua API directly. Obviously, it provides a much better experience than
EasyMotion <strong>in Neovim</strong>, simply because it uses features such as <em>virtual text</em> and <em>extended marks</em> and does not
touch the buffer, allowing to benefit from Neovim-0.5 crunchy features, such as the native LSP client and treesitter
parsers. I also made a bunch of PRs to Neovim core, reading a bit on the C code, etc.</p>
<p>All that to say that… given the experience that I have with Neovim and Doom… I have no idea what to think about
“editing” anymore. Neovim is growing and lots of interesting ideas are landing / will land in the next release. I do not
really know whether people will stick around Hop, but I think it has found its place — at least, I spotted a missing
piece and provided it. I am not a fan of Lua but I have to admit that it attracts more people doing some awesome things.
Among the things that I think are going into the right direction:</p>
<ul>
<li>The native LSP client.</li>
<li>The native treesitter implementation.</li>
<li><a href="https://github.com/kyazdani42/nvim-tree.lua">Nvim Tree</a>, a file browser written in Lua. Similar to netrw but better and much (much) faster.</li>
<li><a href="https://github.com/nvim-telescope/telescope.nvim">Telescope</a>, a fuzzy finder, a bit similar to <a href="https://github.com/abo-abo/swiper#ivy">Ivy</a> / <a href="https://github.com/emacs-helm/helm#emacs-helm">Helm</a> from the emacs realm. I have issues with it right now
because I am used to <a href="https://github.com/junegunn/fzf.vim">fzf-vim</a> and all the current sorting / picking algorithms are not up to the user experience
of fzf to me. When I compare it to <a href="https://github.com/abo-abo/swiper#ivy">Ivy</a>, it is roughly the same: <a href="https://github.com/abo-abo/swiper#ivy">Ivy</a>’s algorithm is just better to me (it has the
same refinement search mechanism as fzf’s, and it’s much faster than Telescope, especially Swiper). I think it will
change in the near future as algorithms in Telescope are improved and support is added for (I hope!) fzf, but for
now, the bad performances of telescope, the choice of making it pretty before focusing on the algorithms first makes
it not really usable on some code bases. However, I do think it is going into the right direction and I will be
looking forward it in a few weeks / months, giving it time to mature a bit and get better defaults / algorithms.</li>
<li><a href="https://github.com/TimUntersberger/neogit">Neogit</a>, an implementation of the best Git client ever, <a href="https://magit.vc">magit</a>, also from the emacs realm. I think it will take them
months before it reaches the same maturity and power as magit, but I think it is cruising great.</li>
<li><a href="https://github.com/pwntester/octo.nvim">Octo</a>, a GitHub-based workflow plugin allowing to list, create, open, add, remove, etc. lots of GitHub objects
directly from within a Neovim buffer, streaming changes on buffer exit / save. Similar, again, to magit’s <a href="https://magit.vc/manual/forge.html">Forge</a>,
from the emacs world, even if Forge is a bit more powerful as it abstracts away the concept of “remote”. However, the
edit workflow of Octo seems a bit more stable right now, so kudos!</li>
<li>Probably a ton of other plugins I forgot to mention.</li>
</ul>
<h1>So what is wrong?</h1>
<p>My experience with Doom Emacs really changed the way I look at software, and more specifically at “productivity
tools.” See, my current workflow is based on tmux, Neovim and my shell, which is zsh (vanilla, I completely ditched
<a href="https://ohmyz.sh">Oh My Zsh</a> as I find it useless and I uninstalled <a href="https://starship.rs/">starship.rs</a> a few weeks ago because I realized I hated the package
manager version annotations on my prompt, that I actually never needed). For people wondering, we have spent so much
time trying to install cool things that we forgot how powerful raw and vanilla zsh is. This is my current prompt:</p>
<p><img src="https://phaazon.net/media/uploads/current_zsh.png" alt="" /></p>
<p>At work, I need a bit more information (i.e. Kubernetes (k8s) namespace and context / cluster I am currently connected
to and AWS), but as those information are not path-dependent, I put them in my tmux statusline instead.</p>
<p>The thing is… I just realized by using Doom Emacs that I do not need all of these anymore. Doom Emacs provides an
environment that is very similar to my tmux + neovim + shell combo. It provides even more, depending on the packages you
install. And the thing is: for each tool, emacs have more opportunities, because it is not limited by a terminal
implementation (i.e. cell-grid / line based application). So for instance, you can have a k8s / AWS / docker client
inside Emacs that is likely to be much much better than the one you will use in CLI (I honestly dislike <code>kubectl</code> a lot,
it has so many switches and grepping through them by piping with <code>rg</code> or using some switches such as <code>-l</code> feels weird).
You can easily see a buffer containing the list of pods / containers / whatever, that you should be able to work with
the tool you already use (i.e. <a href="https://github.com/abo-abo/swiper#ivy">Ivy</a> for instance), interact dynamically with (instead of having to type one line after
another to make effects). It is even more true when you consider the actual shell: you do not need zsh anymore when
using Emacs, because it has its <a href="https://www.gnu.org/software/emacs/manual/html_mono/eshell.html">eshell</a> implementation that is both super weird and super nice. You want to script
something for your shell? Just write ELisp. You do not have to use that horrible bash script language, and you will
benefit from the whole ELisp ecosystem. Completion for commands in eshell is basically the same as completing a regular
buffer, which is weird at first but actually makes a lot of sense. And you have dozens of other examples. Org-mode is
one of the best plugin of Emacs, allowing to take notes, make wikis, handle tasks and calendars, in a way that feels
very seamless with the rest of the Emacs ecosystem. I know some people will tell me to use another tool (I do, my
current tool for that is <a href="https://github.com/phaazon/toodoux">toodoux</a>, a tool I made, similar to <a href="https://taskwarrior.org/">task-warrior</a>). And of course, <a href="https://magit.vc">magit</a>, which is by far
my favorite Emacs plugin. As others say it, it is hard to explain why magit is so much better than the git CLI or
VS Code’s integration or basically any other editor. It is just made very well, with pretty much anything actionable
from the UI, with amazing workflows and even actions you do not have easily with the CLI version.</p>
<h1>What is an editor in 2021, anymore?</h1>
<p>Today, when I look at Doom, at Neovim, at what people are trying to do in Lua with Neovim, I feel like I need to stop
trying to implement / follow what others are doing and think a bit about whether we are doing the right thing. Neovim,
currently, is going towards a very exciting direction with all the effort regarding Lua, LSP, treesitter and community
plugins. But I think the problem is bigger than Neovim. And I think I know that because of how <strong>I can use emacs with
Doom</strong>. I think I come back to Doom so often because they understand well what it means to create a productivity
platform more than an editor. It does not mean the editor is any less good, <em>au contraire</em>. In Doom, there is a default
package that gets installed if you ask for the project module. That package is <a href="https://github.com/bbatsov/projectile">projectile</a>, a wonderful package that
provides with you project management from within emacs. If you use emacs in daemon mode, it means that you can get the
same workflow as with tmux, Neovim and a shell, but with a native implementation of this project / workspace workflow,
in a much much more powerful way (because it will obviously plug in things like <a href="https://github.com/abo-abo/swiper#ivy">Ivy</a>, allowing you to do fuzzy
searching of projects, etc.). The overall experience by having a consistent platform is something I had completely
ignored for years and now that Doom hit me, it hit me hard. I do not know <em>any other platform</em> like emacs, besides
composable environments such as tmux + Neovim + shell. Yes, you have VS Code / Atom / IntelliJ / etc. that will have
tons of plugins to get you terminals and other kinds of integrations, but for having used them, it is clearly not as
powerful as what Doom does — and it is most of the time based on electron; big no. To sum up, Doom is a bit my
VS Code: it provides this amazing productivity platform, but it is fully native, modal-centric and highly composable /
customizable.</p>
<p>Composability is really important to me, but I have to face reality: there is <em>no way</em> to make a workflow such as tmux +
Neovim as snappy and fast as <a href="https://github.com/bbatsov/projectile">projectile</a>. <a href="https://github.com/tmux/tmux">tmux</a> basically reimplements a terminal inside a terminal, which has a lot
of implications (copy-pasting with the mouse in tmux is a challenge even Chuck Norris does not want to take). And once
you turn the computer off, tmux loses everything. You have plugins, such as <a href="https://github.com/tmux-plugins/tmux-resurrect">tmux-resurrect</a>, that will allow to save
and restore the tmux layout (sessions / windows / panes), but you will obviously lose the contents of each panes. With
<a href="https://github.com/bbatsov/projectile">projectile</a>, because it is a native emacs thing, you will not lose anything.</p>
<p>This whole idea has been obsessing me for months. The concept of a “productivity platform.” People laugh at emacs as
being bloated but… it basically implements their shell, tmux and editor in a more elegant way — and more. Why would one
laugh at that? Especially when it allows more interesting kinds of interaction.</p>
<p>So I have been wondering: what is a productivity platform, today, in 2021? Is it this workflow I have been using since
I am 16, with tmux and Vim / Neovim? A friend on IRC told me lately that if I have been using this workflow (as others
do) for so long… it has proven being good so why would I want to replace it? Even though this is a valid point, I do
think this is missing the point: ask some workers to dig a hole for planting trees, they will start digging the ground
with their bare hands. Later, give them a shovel and they will accept it as a better tool and will enjoy it. But does it
mean it is the best tool for planting trees? What if a better tool existed? This is basically the reason why I never
accept something as an ultimate solution: we can always enhance our tools. And having seen what Doom Emacs do, I do
think that I have found new ideas. Not necessarily a better tool, but at least, I think Neovim needs to evolve.</p>
<p>When I think about Neovim, I try to focus on what makes it this tool I love: modal centric, snappy / performance,
integrates well with tmux, and that is pretty much all. The integration part with tmux is needed because I have to use
the TUI, which is also something that I start not liking anymore. I know since Doom that we can get rid of TUIs and make
great GUIs. So what I think I want to go to with Neovim from now on is this:</p>
<ul>
<li>Contribute to a GUI or even make my own. I have glanced at all the current solutions and none are really exciting to
me. People have basically been making window decorations around TUI buffers, which I truly do not like. Some
exceptions that I might invest time in:
<ul>
<li><a href="https://github.com/akiyosi/goneovim">goneovim</a>, which seems to have the more advanced “GUI” externalized features.</li>
<li><a href="https://github.com/smolck/uivonim">uivonim</a>, but I will not contribute nor use it because, heck, electron.</li>
</ul>
</li>
<li>Contribute to <a href="https://github.com/mjlbach/neovim-ui">neovim-ui</a> and see where I can help with abstractions.</li>
</ul>
<p>Something around one year ago, I chatted with core contributors, telling them that something that puzzles me a bit with
the current Neovim situation is that there is no unified / consistent UI experience. Most plugins will write their own
UI thing, obviously often based on TUI hacks, such as using characters as <code>─</code>, <code>╮</code>, <code>├</code>, etc. The current situation with
<a href="https://github.com/nvim-lua/plenary.nvim">plenary.nvim</a> is another interesting topic, because I think it is not the
answer to my problem. Yes, it will provide an abstraction for people needing to create bordered windows / pop-ups but…
the GUI really does not care about that. Technically, Neovim already makes the distinction between “buffers” and
“pop-ups” and “floats”, so GUIs are free to render them the way they want, but… what if developers start using APIs,
such as plenary, where they pass border arguments? How does that make sense? If you are writing a plugin that needs to
list things and requires a fuzzy interaction with the user, you should not depend on something that expects anything
from the UI implementators. And for this, I think Neovim is lacking abstractions. Obviously, we can discuss that kind of
problems for other topics:</p>
<ul>
<li>Displaying messages.</li>
<li>Displaying notifications (I wish Neovim used the D-Bus protocol or similar on Linux for instance).</li>
<li>Fully customizable sign columns and “franges.”</li>
<li>Etc. etc.</li>
</ul>
<p>I guess some of those items are already addressed, but I have yet to see a Neovim GUI going fully “native without TUI
aspects.” Maybe it already exists and I need to search again. I want to invest time in things like goneovim and some
other implementations, and I really do want to make my own (maybe with Gtk or even using my own 2D/3D graphics Rust
stack).</p>
<h1>Next obsession: CLI, TUI and GUI</h1>
<blockquote>
<p>CLI stands for Command Line Interface; TUI for Terminal User Interface and GUI for Graphics User Interface.</p>
</blockquote>
<p>Among all the things that Doom changed in my mind, there is this concept of TUI. I have been a very big user (power
user?) of the CLI. Pretty much anything that I do on a computer is via the CLI. For more advanced usage, I tend to use
a TUI. I only use GUIs if I have to, such as <a href="https://www.mozilla.org">firefox</a>, <a href="https://www.blender.org">blender</a> or a video game. However, guess what: Doom hit hard
here too, because I am not using the TUI version: I am using the GUI one. And do not get it twisted, I am not using my
mouse, and this is a very important and interesting point about CLI / TUI / GUI.</p>
<p>When talking about this CLI / TUI vs. GUI thing around me, people tend to agree, weirdly, on some misconceptions:</p>
<ul>
<li>CLIs and TUIs, because they are mostly implemented as terminal applications, would not rely (for most) on the mouse and
then implement super good user experiences (UX): good shortcuts, good navigation through the application, easy
completion, great composability, etc. The UI, on the other side, would be rather poorer, because of the limitations of
the terminals: no images, only icons, which most of the time require patched fonts to render icons; weird hacks based
on unicode characters to mimic UIs like borders, splits, buttons, inputs, etc.</li>
<li>GUIs would rely more on the mouse and then focus less on the UX, making them harder to use for people on the keyboard,
with shortcuts most of the time not that good, preventing fluid and seamless navigation on the keyboard; poor
composability with other applications, etc. However, they would have much better UI, because of vector graphics images,
proper UI elements such as real inputs, buttons, border, splits, different font families / sizes, etc.</li>
</ul>
<p>These misconceptions are interesting because, as misconceptions, they are not real. You can find some terrible CLIs
(i.e. <code>npm</code> is such a bad CLI — I had a big issue at work a few years ago when running not on purpose a command such
as <code>npm i --user</code>, thinking it would install it in user space while it just completely ignored the unknown <code>--user</code>
flag and installed the package globally), and you can find terrible GUIs too. However, and this is where I want to
focus, GUIs are not necessarily mouse-focused, and since we can implement nice UX in TUI… we should be able to do the
same in GUIs, right?</p>
<p>Aaaaaaaand… enter Doom, again. I am using the GUI version of emacs — on Archlinux, I am using this
<a href="https://aur.archlinux.org/packages/emacs-native-comp-git">patched version</a> which is a basically <a href="https://www.emacswiki.org/emacs/GccEmacs">gccemacs</a>, an emacs
that recompiles all the Lisp as C! It obviously can accept mouse inputs so that you can click on things if you want to
but… the most important still applies: you can use it exactly as you would use it in a terminal. However, because it is
a GUI, you get access to much more possibilities:</p>
<ul>
<li>Proper “pixel scrolling.”</li>
<li>Use several fonts, of different sizes, for things like Markdowns, note taking, etc.</li>
<li>Proper pop-up windows.</li>
<li>Pixel-perfect annotations, marks, hints and signs in your buffer.</li>
<li>Images, rasterized or vector graphics.</li>
<li>One of the most important part to me: proper UI elements! No more hacks!</li>
<li>Even though it is not implemented yet, since we can have images, and especially vector graphics, we should be able to
render git branches with pretty links and not the <code>/|\*</code> ASCII art mimics we see in all CLIs / TUIs.</li>
<li>Etc. etc.</li>
</ul>
<p>Today, I do think that the right direction is to allow developers to build platforms around the concept of “graphics
toolkits”, and not “terminal applications.” Even for CLIs. Think about <code>git branch</code>. My current git branch workflow, in
CLI, does not use <code>git branch</code> only: I have an alias for each Git commands that require a branch. For instance, if I want
to switch to another branch, this is what my <code>gs</code> alias looks like when invoked:</p>
<p><img src="https://phaazon.net/media/uploads/git_switch.png" alt="" /></p>
<p>This is basically a fuzzy-finder version of <code>git switch</code>, using <a href="https://github.com/junegunn/fzf">fzf</a> – if you want that script, have a look at
<a href="https://github.com/phaazon/config/tree/master/git/scripts">these ones</a>. The same feature is doable with Doom / Neovim
by using the right plugin, and I have to admit that I prefer doing that in magit as it feels more “well integrated.” Of
course, you will always want to have a way to compose applications, and for that, CLIs are great. Something such as
<code>kubectl get pods -l service=my-service | rg whatever</code>. However, I am trying to challenge my own conception of
“program composability” because I do think we can implement this <em>list k8s pods -&gt; filter them</em> inside an UI such as the
ones in Doom Emacs, without having to write a specific tool. And we can actually do it in CLI / TUI as I just showed
with my <code>gs</code> alias, so… why not providing that kind of power natively on the platform?</p>
<h1>Final obsession: configuration ≠ scripting</h1>
<p>And now one of the big issue I have with pretty much any editor today (but more with Neovim as I am actively
contributing to it): if you want to configure your editor, you have to code your configuration <strong>and I highly dislike
that.</strong> It is possible, indeed, to reduce the quantity of code to write, but you will eventually have to get your hands
dirty and write some Lua (<a href="https://phaazon.net/media/uploads/eeeeeeeeeeeeeeeeeeeeeeeew.jpg">eeew</a>). And I have a big issue with this.</p>
<p>See, configuration should be stupid and limiting. It should be <code>key = value</code>, period. If you need a more structured way
to configure things, just add sections / objects like what you can do with TOML or JSON, but that is all. Remember that
a configuration file might be read by a human, and that you <strong>must</strong> do everything possible to make it super clear what
the configuration state is. Configuring should be:</p>
<ul>
<li>Not Turing-complete. You should not have access to logical constructs, such as conditional statements / jump
statements or anything that would turn your configuration language Turing-complete.</li>
<li>Imports / includes should not be possible. Having to import things from a configuration file makes it harder for the
application to read from it and it makes it much harder for people to know what values are currently set.</li>
<li>If you really want to have “scripting” over your configuration files, use some proper languages made exactly for this
purpose, such as <a href="https://dhall-lang.org">Dhall</a>, a template engine or something similar. Leave the choice to the user to decide whether they
want to configure, or code.</li>
</ul>
<p>There is a fundamental difference between <strong>having the power to do something and explicitly refusing that power</strong> and
<strong>lacking that power.</strong> I think that configuration <em>can</em> be done via code, but it does not mean it should. The reason
for that is mainly for cognitive complexity reasons. If you are the author of your application, you will very likely
know how to script it. You will know all the quirks and all the little things to know that you will, obviously, have
well documented (right?!). Your users will not have this kind of knowledge. I think it is wrong to use Lua to configure
a software as it will force people to get into the documentation of the Lua API, while all they should have is a list
of configuration options they can set, what setting does what and what are the possible values, <strong>and that is all.</strong>
Providing more power leads to inconsistencies, broken setup and something that is pretty hard to fix without proper
semantic versioning (and an actual package manager enforcing it): a slowly rotting configuration.</p>
<p>Rotting configuration happens when you start piling up lots of “configuration code”, using different APIs, that will
eventually get deprecated or not even used anymore. If you are lucky, the application will just tell you this and that
API calls are deprecated and you should switch to something else. If you are not, you will get some random crashes and
failures in your applications and you will have to debug code. For configuration. And this happens a lot with Neovim.</p>
<p>See, blurring the line between configuration and scripting is easy, especially if you use the same language for both.
Scripting is enhancing the power of an application, such as an editor or a video game, by providing new <strong>behaviors</strong>.
Those behaviors (should them be native to the applications or new ones provided by plugins) are <strong>configured</strong>. If you
start having to script the configuration side, you lose the distinction between both.</p>
<p>At work, we are using k8s a lot. In order to configure our services, we have to deploy configuration massively
everywhere, and we need to generate that configuration. But see, there is a difference between “the configuration” and
“generating the configuration.” The “generate part” is basically a template language. This is code, because it relies on
the datacenter / cluster / environment / feature flags / etc. we want to deploy in / with. However, we do not configure
the software in the template code. We pass fragments of configuration to the template engine, which generates the final
configuration. And those fragments are basically exactly what I described above: key-value objects, sometimes nested to
implement more complex configuration setup, but nothing else. It is basically YAML. If you do not need the complexity of
the datacenter, cluster or environment, you can just run your application with a configuration file that has no code in
it, just a bunch of key-value pairs.</p>
<p>The way Neovim treats configuration right now is via a convention, well-accepted among the plugin developers (and
since I am now officially a Neovim plugin developer with <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>, I obviously follow it as well). That convention is
the <code>setup</code> function plugins expose to pass configuration (via an object often called <code>opts</code> for <em>options</em>). So people
have to know whether that convention is used, they have to call that function (i.e. it is a regular Lua function call)
and they have to dig in the Lua API. What I think I want to try (I think I am going to work on a plugin for that) is a
way to have a central “configuration store” so that users do not have to go through this <code>setup</code> thing anymore — which,
for the record, requires you to do something like <code>require'the-plugin'.setup { … }</code>, for every plugin you want to use.
The way I see things would be more like this:</p>
<pre><code class="language-lua">-- In your init.lua, for instance

require'config-store'.setup {
  telescope = {
    -- options used by telescope
  },
  hop = {
    -- options used by hop
  },
  neogit = {
    -- options used by neogit
  },
  -- etc. etc.
}

require'telescope'
require'hop'
require'neogit'
</code></pre>
<p>Then, each plugin, in their <code>init.lua</code>, could do something like this:</p>
<pre><code class="language-lua">-- Assuming this is the plugin for hop
local config = require'config-store'.hop or require'hop.config'.defaults
</code></pre>
<p>This way of doing seems much more natural to me, as it limits the amount of “coding” people have to do to configure
their packages, and it is not really different for package maintainers. Instead of exposing a <code>setup</code> function, they can
just get the configuration via this <code>config-store</code> thing. I think I will try to experiment with this idea. Obviously, it
requires plugins to adopt this <code>config-store</code> thing, and I do think that if people think it is an interesting idea, this
should be a vanilla Lua API and not a random plugin, so that we know this <code>config-store</code> object is there, whatever
plugins the user have installed. As discussed above with how we do that at work, I do not want to prevent people from
coding / scripting their editors. But I think a clear distinction between scripting and configuring is important.</p>
<h1>In the end</h1>
<p>So those were my (lost) thoughts about the current situation. I might do a follow-up later in the year to see how things
have evolved to me. Today, I stick around Neovim in tmux, using various scripts around fzf to make my shell a bit more
interesting to use. I have to admit that I often open emacs to do three / four commits. The experience is just
consistent. Navigating in projectile, Ivy and magit is unbeaten to me. The simple interactions with dired (to navigate
directories a bit like with a file browser) and edit their contents is something I truly seek everywhere else, even in
my shell. I recently did a quick benchmarked of <a href="https://github.com/abo-abo/avy">Avy</a>, the emacs implementation of
EasyMotion, versus <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>, and Hop is <em>really much faster</em>, so I do not really know what to think (I guess I should
be proud of what I made 🙂). I think that Doom currently has this level of maturity that Neovim will take years to
achieve, not because of talent, but because of the fact Neovim is an editor and has not been designed as being a
productivity platform, while emacs was.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 22 Mar 2021 21:00:00 GMT</pubDate></item><item><title>An interesting subtle property of where clauses with super traits</title><link>https://strongly-typed-thoughts.net/blog/where-clause-super-trait</link><description><![CDATA[<p>Lately, I’ve been hacking on the next version of <a href="https://github.com/phaazon/luminance-rs">luminance</a>, luminance-0.40. It should be out
<em>“soon-ish”</em> but in the meantime, I’ve been struggling a bit with some highly and strongly typed
code. I want to share something interesting I discovered with <code>rustc</code>. Especially, I haven’t seen
a mention of that property <a href="https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-supertraits-to-require-one-traits-functionality-within-another-trait">in the book</a>, so I’m happily sharing.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#what-is-a-super-trait">What is a super trait?</a></li>
<li><a href="#the-weird-stuff">The weird stuff</a></li>
<li><a href="#a-small-disgression-to-haskell-land">A small disgression to Haskell land</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>What is a super trait?</h1>
<p>The weird situation is about <em>super traits</em>. A super trait is a trait that must be implemented
for another trait to be usable, because it’s relied on. Traits can be thought of as
<em>constraints</em>, so a super trait is a bit like a <em>dependency</em> when implementing a trait, and an
<em>implication</em> when using a trait on which a super trait is declared. A trait can have zero to
several super traits (added with the <code>+</code> operator). For instance, imagine you have a trait <code>Alive</code>:</p>
<pre><code class="language-rust">trait Alive {
  fn get_health(&amp;self) -&gt; Health;
}
</code></pre>
<p>Now imagine you want a trait to move someone or something around:</p>
<pre><code class="language-rust">trait Move: Alive {
  fn go(&amp;mut self, direction: Direction);
}
</code></pre>
<p>Here, you can see that:</p>
<ul>
<li><code>Move</code> requires to implement <code>Alive</code>, because it’s a super trait. It’s a <em>dependency</em>.</li>
<li>Because <code>Move</code> requires <code>Alive</code>, <code>Alive</code> is implied when you use <code>Move</code>. Indeed, that would be
redundant to annotate a type <code>T: Move + Alive</code>, because an instance (implementor) for <code>Move</code>
cannot exist without <code>Alive</code> to be implemented as well.</li>
</ul>
<p>So now that we understand what super traits are, let’s get to the weird stuff.</p>
<h1>The weird stuff</h1>
<p>When you implement a trait which has a super trait, do you think that:</p>
<ol>
<li>Your implementation is valid <em>when the super trait is implemented</em>? After all, you could simply
assume it is implemented. If it’s not, instances of your trait won’t be pickable.</li>
<li>Or your implementation <em>requires</em> the super trait to be implemented?</li>
</ol>
<p>The distinction is important. (1.) doesn’t require <code>rustc</code> to prove <em>when implementing a trait</em>
that the super trait is implemented. That will be required when using the trait. With (2.), <code>rustc</code>
will have to prove that the super trait is, first, implemented, before even considering the
implementation of the trait you’re making.</p>
<p>Rust currently uses (2.). If you <code>impl Move for Foo</code>, <code>impl Alive for Foo</code> must be in scope for
that implementor to be possible.</p>
<p>But… it would be interesting to actually have (1.). Imagine that you want to implement <code>Move</code> for
something a bit complex, like <code>Creature&lt;T&gt;</code>, but not all <code>Creature&lt;T&gt;</code> are alive. Only a subset
of them, and you can’t tell exactly when — i.e. you just cannot assume anything about <code>T</code>. So
what are you going to write?</p>
<pre><code class="language-rust">impl&lt;T&gt; Move for Creature&lt;T&gt; {
  fn go(&amp;mut self, direction: Direction) {
    // …
  }
}
</code></pre>
<p>This code will not compile, because you haven’t implemented <code>Alive for Creature&lt;T&gt;</code>. Remember that
the trait solver requires to prove super traits. However, and this is where all the interesting /
weird stuff happens:</p>
<pre><code class="language-rust">impl&lt;T&gt; Move for Creature&lt;T&gt; where Self: Alive {
  fn go(&amp;mut self, direction: Direction) {
    // …
  }
}
</code></pre>
<p>This compiles. It compiles because the <code>where</code> clause tells <code>rustc</code> that your implementor <em>will be
valid</em> if used with <code>Creature&lt;T&gt;: Alive</code>. The distinction is really subtle, but is, to my opinion,
very powerful. With the <code>where</code> clause, you state that <code>Move</code> is implemented for any <code>Creature&lt;T&gt;</code>
that is also <code>Alive</code>, but you don’t require them all to be <code>Alive</code>! You could implement <code>Alive</code>
for a bunch of creatures, like <code>Creature&lt;Vampire&gt;</code> and <code>Creature&lt;CloudDog&gt;</code>.</p>
<p>So, I remember having read somewhere (maybe in some Rust book, but I’m not quite sure) that the
<code>where Self: _</code> clause was not really useful, but in our case, you can see that it allows to
express a completely different semantics.</p>
<blockquote>
<p>You could also have used <code>Creature&lt;T&gt;: Alive</code> in place of <code>Self: Alive</code>, as here,
<code>Self = Creature&lt;T&gt;</code>.</p>
</blockquote>
<h1>A small disgression to Haskell land</h1>
<p>In Haskell, that code requires to use <code>UndecidableInstances</code>. I don’t know <em>exactly</em> why, but GHC
states that the constraint (<code>Alive (Creature a)</code>) is no smaller than the instance head
(<code>Move (Creature a)</code>), and this is not permitted, as being undecidable. Enabling the
<code>UndecidableInstances</code> GHC extension will make it possible to compile:</p>
<pre><code class="language-haskell">class Alive a where
  getHealth :: a -&gt; Health

-- The super class is declared on the left side of the =&gt; (the parenthesis are
-- optional, but I’m used to put them all the time, as they are required when you
-- have more constraints).
class (Alive a) =&gt; Move a where
  go :: Direction -&gt; a -&gt; a

-- This instance requires UndecidableInstances to compile.
instance (Alive (Creature a)) =&gt; Move (Creature a) where
  go = -- …

instance Alive (Creature Vampire) where
  getHealth = -- …

instance Alive (Creature CloudDog) where
  getHealth = -- …
</code></pre>
<p>I’m not quite sure why this <em>very exact</em> situation requires <code>UndecidableInstances</code> though. In this
case, it seems fine.</p>
<p>I hope you’ll have learned something with this small yet fun type theory candy. Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 02 May 2020 23:50:00 GMT</pubDate></item><item><title>On dealing with owning and borrowing in public interfaces</title><link>https://strongly-typed-thoughts.net/blog/on-owning-borrowing-pub-interface</link><description><![CDATA[<blockquote>
<p>Disclaimer: this blog is about <a href="https://www.rust-lang.org">Rust</a> and some of its intrinsics semantics, along with software
design and architecture – especially for public interfaces like APIs. However, you may find
it interesting to apply the concepts to any language that would support those ideas directly or
indirectly.</p>
</blockquote>
<p>I’ve been writing on a few examples code lately to add to documentations of some crates of mine. I
write a lot of code that creates new objects that need other objects in order to be built. Most of
the APIs you can see around tend to love the borrow principle – and I do. The idea is simple:</p>
<ul>
<li>If you want to create a new type that, for instance, must own a <code>String</code>, most APIs tend to
agree that the constructor of your type should take a <code>&amp;str</code> or some sort of borrowing – typical
way to do that is to use the <code>AsRef</code> trait, that gives you more power (more about that below).
You can then just clone the <code>&amp;str</code> in your function.</li>
<li>Apply that principle to pretty much any kind of object you need but not types that don’t have
easy borrowing semantics. For instance, <code>Rc&lt;_&gt;</code> already carries sharing and dynamic borrowing
semantics, so I wouldn’t be surprised to find a constructor that takes a <code>Rc&lt;_&gt;</code> instead of a
<code>&amp;Rc&lt;_&gt;</code>.</li>
</ul>
<p>Depending on the crate you look at, the authors and how they see things, you might find a lot of
ways to pass that string to your constructor. Let’s get technical. Especially, I want this blog post
to give people a hint at how they should shape their APIs by driving the types with semantics.</p>
<h1>The base code</h1>
<pre><code>struct Person {
  ident: String
}
</code></pre>
<p>Very simple to get started. We have a <code>Person</code> type that carries the name of that person. We don’t
want to expose the internals of the type because we might add a lot of other things in there and
allowing people to pattern-match a <code>Person</code> would break their code when we add more things.</p>
<pre><code>impl Person {
  pub fn new(name: String) -&gt; Person {
    Person { name }
  }
}
</code></pre>
<p>That works. This constructor works by <em>move semantics</em>: it expects you to move a <code>String</code> in in
order to copy it into the <code>Person</code> value. Move semantics are clear here: the <em>client</em> must allocate
a <code>String</code> or move one already allocated by someone else. This can get a bit boring. Imagine:</p>
<pre><code>let someone = Person::new("Patrick Bateman"); // this won’t compile
</code></pre>
<p><code>Person::new("Patrick Bateman")</code> doesn’t typecheck because <code>"Patrick Bateman"</code> has type <code>&amp;'static str</code> here. It’s not a <code>String</code>.</p>
<h1>Drive the allocation from within your code</h1>
<p>So how can we fix the code above to have it compile?</p>
<pre><code>let someone = Person::new("Patrick Bateman".to_owned()); // okay now
</code></pre>
<p><code>"Patrick Bateman".to_owned()</code> makes a use of
<a href="https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned"><code>ToOwned::to_owned</code></a> to
clone the string. You could also have used
<a href="https://doc.rust-lang.org/std/convert/trait.Into.html"><code>Into::into</code></a>. However, we’re still
requiring clients of our API to perform the allocation – or move in, which is not ideal. A typical
solution to this is to use a borrow and clone when needing an owned version.</p>
<pre><code>impl Person {
  pub fn new(name: &amp;str) -&gt; Person {
    Person {
      name: name.to_owned()
    }
  }
}
</code></pre>
<p>This API enables the current code to compile:</p>
<pre><code>let someone = Person::new("Patrick Bateman"); // okay because 'static is a subtype of 'a
</code></pre>
<p>We’re all good now, right? Not exactly: what happens if we try to pass the initial <code>String</code> we had
when we moved something in?</p>
<pre><code>let x: String = …;
let someone = Person::new(x); // not okay now since it’s a type mismatch
</code></pre>
<p>To fix this, we need to pass a <code>&amp;str</code> out of a <code>String</code>. Since
<a href="https://doc.rust-lang.org/std/ops/trait.Deref.html#impl-Deref-3"><code>String</code> implements <code>Deref&lt;Target = str&gt;</code></a>,
it’s quite straight-forward:</p>
<pre><code>let x: String = …;
let someone = Person::new(&amp;x); // okay, here &amp;x derefs to &amp;str
</code></pre>
<blockquote>
<p>You can also use the more explicit <code>String::as_str</code> method.</p>
</blockquote>
<p>A nice trick here to be able to pass both <code>&amp;str</code> and <code>String</code> is to use the <a href="https://doc.rust-lang.org/std/convert/trait.AsRef.html"><code>AsRef</code></a> trait.</p>
<pre><code>impl Person {
  pub fn new&lt;N&gt;(name: N) -&gt; Person where N: AsRef&lt;str&gt; {
    Person {
      name: name.as_ref().to_owned()
    }
  }
}
</code></pre>
<p>In this case, <code>AsRef&lt;str&gt;</code> is implemented for both <code>&amp;str</code> (it just returns itself) and <code>String</code> – it
just deref / uses the <code>as_str</code> method).</p>
<p>However, you can still see a problem here. If we keep using <code>x</code> after the call to <code>Person::new</code>,
this code is actually okay and we can move on. If we don’t need <code>x</code> afterwards, we’re just wasting
an opportunity to move in instead of allocating!</p>
<h1>Drive the allocation from your code and allow for moving in</h1>
<p>Clearly, to me, the perfect API would enable you to pass borrows and ask the API code to clone for
you or just accept the memory region you provide (i.e. you move something in). The idea is then to
accept either <code>&amp;str</code> or <code>String</code> in our case, and clone only a <code>&amp;str</code>… There are several traits and
types that provide that feature.</p>
<h2><code>ToOwned</code> + <code>Cow</code></h2>
<p><a href="https://doc.rust-lang.org/std/borrow/enum.Cow.html"><code>Cow</code></a> – for Clone on write – is a very interesting concept. It encodes that you’re either
borrowing some data or that you’ve already borrowed it. See it as:</p>
<pre><code>enum Cow&lt;'a, T&gt; where T: 'a + ?Sized + ToOwned {
  Borrowed(&amp;'a T),
  Owned(T::ToOwned)
}

impl&lt;'a, T&gt; Cow&lt;'a, T&gt; where T: 'a + ?Sized + ToOwned {
  pub fn into_owned(self) {
    match self {
      Cow::Borrowed(b) =&gt; b.to_owned(),
      Owned(o) =&gt; o
    }
  }
}
</code></pre>
<p>Now, the interesting part: it’s possible to go from <code>&amp;'a str</code> to <code>Cow&lt;'a, str&gt;</code> and from <code>String</code>
to <code>Cow&lt;'a, str&gt;</code> by using <code>Into</code> implementors. That enables us to write this:</p>
<pre><code>impl Person {
  pub fn new&lt;'a, N&gt;(name: N) -&gt; Person where N: Into&lt;Cow&lt;'a, str&gt;&gt; {
    Person { name: name.into().into_owned() }
  }
}
</code></pre>
<p>This code will move in a <code>String</code> if you passed one and clone if you passed <code>&amp;str</code>. The following
lines of code compile and work as a charm:</p>
<pre><code>let _ = Person::new("Patrick Bateman");

let dawg = "Dawg";
let _ = Person::new(format!("Doggo {}", dawg));
</code></pre>
<p>What’s interesting here is that all cases are covered:</p>
<ul>
<li>The client <strong>has the choice</strong> to either allocate – or move in – or borrow.</li>
<li>It’s even possible to implement <code>Into&lt;Cow&lt;str&gt;&gt;</code> on other types to pass other kind of arguments.</li>
</ul>
<p>There’s just a little nit: <code>Cow::into_owned</code> obviously patterns match on the variant, inducing a
small yet present runtime overhead. We tend to prefer using <code>Cow&lt;_&gt;</code> to dynamically dispatch the
decision to clone at runtime, while in our case, it’s more about a static choice (which API function
version to use).</p>
<h2><code>Into</code>, as simple as it gets</h2>
<p>If you look at the <code>Into&lt;String&gt;</code> implementors, you’ll find <code>impl Into&lt;String&gt; for String</code> –
actually, this is a
<a href="https://doc.rust-lang.org/std/convert/trait.From.html#impl-From%3CT%3E-10">blanket implementor</a> –
and <code>impl&lt;'a&gt; Into&lt;String&gt; for &amp;'a str</code>. That implements the same semantics as <code>Cow&lt;str&gt;</code>, but at
the type level, removing any remaining runtime overhead.</p>
<p>The new code is even simpler:</p>
<pre><code>impl Person {
  pub fn new&lt;N&gt;(name: N) -&gt; Person where N: Into&lt;String&gt; {
    Person { name: name.into() }
  }
}
</code></pre>
<p>Obviously, there are drawbacks:</p>
<ul>
<li>You cannot express any lifetimes with this <code>Into&lt;String&gt;</code> trait bound. If you need, for any
reason, to dynamically check whether to clone or not, the <code>Cow&lt;str&gt;</code> is the right decision. If
you know you’ll always need to allocate, go for <code>Into&lt;String&gt;</code>.</li>
<li><code>Into&lt;String&gt;</code> is vague and some types might not even implement it.</li>
<li><code>Into&lt;String&gt;</code> doesn’t allow for cheap reading while <code>Cow&lt;str&gt;</code> does. <code>Into&lt;String&gt;</code> requires
you to move or allocate prior to reading.</li>
</ul>
<h1>Let’s draw a conclusion</h1>
<p>What I wanted to highlight in this blog post is that <code>&amp;_</code>, <code>AsRef&lt;_&gt;</code>, <code>Cow&lt;_&gt;</code>, and <code>Into&lt;_&gt;</code> all
have different semantics that can be used to <em>encode</em> different contracts in public interfaces.</p>
<ul>
<li><code>&amp;T</code> means that you don’t require your client to clone nor move because you <em>might</em> only perform
read-only computations on <code>T</code>. That gives some intuitions to your clients:
<ul>
<li>You don’t require them to own <code>T</code>.</li>
<li>If any allocation must be performed, the client doesn’t have to worry about it.</li>
<li>You force them to use a reference. It can have interesting consequences. Imagine
<code>&amp;[Something]</code>. This carries one information more: contiguity of the input data – slices are
contiguous.</li>
</ul>
</li>
<li><code>Q: AsRef&lt;T&gt;</code> is a <code>&amp;T</code> on steroids in terms of software design. It gives more power to the
client as they’ll be able to pass more types in your functions. However, you now introduce a
polymorphic API. The semantics are the same: you plan to perform only read-only computations or
accept not to use a client-provided move-in value if you need to own something. Keep in mind an
important <em>hidden property</em> here: because you accept values to be moved in while you just have
a read-only contract on them, you also accept clients values to be <em>dropped</em> by corollary.</li>
<li><code>Cow&lt;T&gt;</code> states that the client can provide a borrow or an owned variable and you will take
advantage of that <strong>at runtime</strong>. This is important as it gives a good idea about how the memory
footprint of the function works: it will take decision at runtime whether or not it needs to own
memory.</li>
<li><code>Q: Into&lt;T&gt;</code> has vague and large semantics but can be used to encode the fact that you <em>want</em>
owned data, but you accept your clients to provide a borrow as long as you can clone. If they
provide you with an owned data, you’ll just do nothing (move in).</li>
<li>(bonus) <code>Q: IntoIterator&lt;Item = T&gt;</code> is also something you can use for the slices thing just
above. If (and only if) you don’t care about contiguity or will just inspect values one by one,
this is the interface to go. This interface gives hints that your function might iterate through
the input and perform (perhaps heavy) computation on each items before stepping to the next one.
Using <code>IntoIterator</code> instead of <code>Iterator</code> enables you the same kind of trick you have with
<code>AsRef&lt;_&gt;</code> vs. <code>&amp;_</code>: you can take a <code>Vec&lt;_&gt;</code> directly instead of the iterator directly. Keep in
mind that this might not have sense for some types, especially if they have several iterator
interfaces. The <code>str</code> type has for instance the
<a href="https://doc.rust-lang.org/std/primitive.str.html#method.bytes">str::bytes</a> method that gives
you an iterator over bytes and the
<a href="https://doc.rust-lang.org/std/primitive.str.html#method.chars">str::chars</a> method that yields
an iterator over UTF-8 characters.</li>
</ul>
<p>This list gives you a good idea about what interface you should use in your public interfaces.
Read-only? Owned data? Read and maybe write? All those semantics are visible through those types and
traits, you should definitely try to wrap your finger around using all of them. Of course, if you’re
just writing a small utility function that needs to borrow the <code>.name</code> of a <code>Person</code>, passing in a
<code>&amp;Person</code> seems completely sound. Most of the time, if you don’t know where the data comes from,
be the more inclusive and generic as possible.</p>
<p>I hope you like that small article, and as always, keep the vibes.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 04 Nov 2018 01:30:00 GMT</pubDate></item><item><title>Postmortem #1 – Revision 2017</title><link>https://strongly-typed-thoughts.net/blog/what_went_revision_2017</link><description><![CDATA[<p>On the weekend of 14th – 17th of April 2017, I was attending for the forth time the
<a href="https://2017.revision-party.net">easter demoparty Revision 2017</a>. This demoparty is the biggest so
far in the world and gathers around a thousand people coming from around the world. If you’re a
demomaker, a demoscene passionated or curious about it, that’s the party to go. It hosts plenty of
competitions, among <em>photos</em>, <em>modern graphics</em>, <em>oldschool graphics</em>, <em>games</em>, <em>size-limited demos</em>
(what we call <em>intros</em>), <em>demos</em>, <em>tracked and streamed music</em>, etc. It’s massive.</p>
<p>So, as always, once a year, I attend Revision. But this year, it was a bit different for me. Revision
is <em>very</em> impressive and most of the <em>“big demogroups”</em> release their productions they’ve been
working on for months or even years. I tend to think <em>“If I release something here, I’ll just be
kind of muted by all those massive productions.”</em> Well, less than two weeks before Revision 2017, I
was contacted by another demogroup. They asked me to write an <em>invitro</em> – a kind of <em>intro</em> or
<em>demo</em> acting as a communication production to invite people to go to another party. In my case, I
was proposed to make the <a href="http://outlinedemoparty.nl">Outline 2017</a> invitro. Ouline was the first
party I attended years back then, so I immediately accepted and started to work on something. That
was something like 12 days before the Revision deadline.</p>
<p>I have to tell. It was a challenge. All productions I wrote before was made in about a month and a
half and featured less content than the Outline Invitation. I had to write a lot of code from
scratch. <em>A lot</em>. But it was also a very nice project to test my demoscene framework, written in
Rust – you can find <a href="https://github.com/phaazon/spectra">spectra here</a> for now; it’s unreleased by
the time I’m writing this but I plan to push it onto crates.io very soon.</p>
<p>An hour before hitting the deadline, the beam team told me their Ubuntu compo machine died and that
it would be neat if I could port the demo to Windows. I rushed like a fool to make a port – I even
forked and modified my OpenAL dependency! – and I did it in 35 minutes. I’m still a bit surprised
yet proud!</p>
<p>Anyways, this post is not about bragging. It’s about hindsight. I did that for
<a href="http://www.pouet.net/prod.php?which=67966">Céleri Rémoulade</a> as I was the only one working on it –
music, gfx, direction and obviously the Rust code. I want to draw a list of <em>what went wrong</em> and
<em>what went right</em>. In the first time, for me. So that I have enhancement axis for the next set of
demos I’ll do. And for sharing those thoughts so that people can have a sneak peek into the
internals of what I do mostly – I do a lot of things! :D – as a hobby on my spare time.</p>
<h1>What went wrong</h1>
<p>Sooooooooo… What went wrong. Well, a lot of things! <strong>spectra</strong> was designed to build demo
productions in the first place, and it’s pretty good at it. But something that I have to enhance is
the <em>user interaction</em>. Here’s a list of what went wrong in a concrete way.</p>
<h2>Hot-reloading went wiiiiiiiiiiild²</h2>
<p>With that version of <strong>spectra</strong>, I added the possibility to <em>hot-reload</em> almost everything I use as
a resource: shaders, textures, meshes, objects, cameras, animation splines, etc. I edit the file,
and as soon as I save it, it gets hot-reloaded in realtime, without having to interact with the
program (for curious ones, I use the straight-forward <a href="https://crates.io/crates/notify">notify crate</a>
for registering callbacks to handle file system changes). This is very great and I save a <strong>lot</strong> of
time – Rust compilation is slow, and that’s a lesson I had learned from Céleri Rémoulade: keeping
closing the program, make a change, compiling, running is a wast of time.</p>
<p>So what’s the issue with that? Well, the main problem is the fact that in order to implement
hot-reloading, I wanted performance and something very simple. So I decided to use <em>shared mutable
smart states</em>. As a <strong>Haskeller</strong>, I kind of offended myself there – laughter! Yeah, in the
Haskell world, we try hard to avoid using shared states – <code>IORef</code> – because it’s not referentially
transparent and reasoning about it is difficult. However, I tend to strongly think that in some very
specific cases, you need such side-effects. I’m balanced but I think it’s the way to go.</p>
<p>Anyway, in Rust, shared mutable state is implemented via two types: <code>Rc/Arc</code> and <code>Cell/RefCell</code>.</p>
<p>The former is a runtime implementation of the <em>borrowing rules</em> and enables you to share a pointer.
The borrowing rules are not enforced at compile-time anymore but dynamically checked. It’s great
because in some time, you can’t know how long your values will be borrowed or live. It’s also
dangerous because you have to pay extra attention to how you borrow your data – since it’s checked
at runtime, you can crash your program if you’re not extra careful.</p>
<blockquote>
<p><code>Rc</code> means <em>ref counted</em> and <code>Arc</code> means <em>atomic-ref counted</em>. The former is for values that stay
on the same and single thread; the latter is for sharing the pointer between threads.</p>
</blockquote>
<p><code>Cell/RefCell</code> are very interesting types that provide <em>internal mutation</em>. By default, Rust gives
you <em>external mutation</em>. That is, if you have a value and its address, can mutate what you have at
that address. On the other hand, <em>internal mutation</em> is introduced by the <code>Cell</code> and <code>RefCell</code>
types. Those types enable you to mutate the content of an object stored at a given address without
having the exterior mutatation property. It’s a bit technical and related to Rust, but it’s often
used to mutate the content of a value via a function taking an immutable value.</p>
<blockquote>
<p><code>Cell</code> only accepts values that can be copied bit-wise and <code>RefCell</code> works with references.</p>
</blockquote>
<p>Now, if you combine both – <code>Rc&lt;RefCell&lt;_&gt;&gt;</code>, you end up with a shareable – <code>Rc&lt;_&gt;</code> – mutable –
<code>RefCell&lt;_&gt;</code> – value. If you have a value of type <code>Rc&lt;RefCell&lt;u32&gt;&gt;</code> for instance, that means you
can clone that value and store it everywhere in the same thread, and at any time, borrow it and
inspect and/or mutate its content. All copies of the values will observe the change. It’s a bit like
C++’s <code>shared_ptr</code>, but it’s safer – thank you Rust!</p>
<p>So what went wrong with that? Well, the borrow part. Because Rust is about safety, you still need to
tell it how you want to borrow at runtime. This is done with the [<code>RefCell::borrow()</code>](https://doc.rust-lang.org/std/cell/struct.RefCell.html#method.borrow]
and <a href="https://doc.rust-lang.org/std/cell/struct.RefCell.html#method.borrow_mut"><code>RefCell::borrow_mut()</code></a>
functions. Those functions return a special object that borrows the pointed object as long as it
lives. Then, when it goes out of scope, it releases the borrow.</p>
<p>So any time you want to use an object that is hot-reloadable with my framework, you have to call
one of the borrow functions presented above. You end up with a lot of borrows, and you have to keep
in mind that you can litterally crash your program if you violate the borrowing rules. This is a
nasty issue. So far, I haven’t really spent time trying to fix that, but that something I have to
figure out.</p>
<h2>Resources declaration in code</h2>
<p>This is a bit tricky. As a programmer, I’m used to write algorithms and smart architectures to
transform data and resolve problems. I’m given inputs and I provide the outputs – the solutions.
However, a demoscene production is special: you don’t have inputs. You create artsy audiovisual
outputs from nothing but time. So you don’t really write code to solve a problem. You write code
to create something that will be shown on screen or in a headphone. This aspect of demo coding has
an impact on the style and the way you code. Especially in crunchtime. I have to say, I was pretty
bad on that part with that demo. To me, code should only be about transformations – that’s why I
love Haskell so much. But my code is clearly not.</p>
<p>If you know the <code>let</code> keyword in Rust, well, imagine hundreds and hundreds of lines starting with
<code>let</code> in a single function. That’s most of my demo. In rush time, I had to declare a <em>lot</em> of things
so that I can use them and transform them. I’m not really happy with that, because those were data
only. Something like:</p>
<pre><code class="language-rust">let outline_emblem = cache.get::&lt;Model&gt;("Outline-Logo-final.obj", ()).unwrap();
let hedra_01 = cache.get::&lt;Model&gt;("DSR_OTLINV_Hedra01_Hi.obj", ()).unwrap();
let hedra_02 = cache.get::&lt;Model&gt;("DSR_OTLINV_Hedra02_Hi.obj", ()).unwrap();
let hedra_04 = cache.get::&lt;Model&gt;("DSR_OTLINV_Hedra04_Hi.obj", ()).unwrap();
let hedra_04b = cache.get::&lt;Model&gt;("DSR_OTLINV_Hedra04b_Hi.obj", ()).unwrap();
let twist_01 = cache.get::&lt;Object&gt;("DSR_OTLINV_Twist01_Hi.json", ()).unwrap();
</code></pre>
<p>It’s not that bad. As you can see, <strong>spectra</strong> features a <em>resource cache</em> that provides several
candies – hot-reloading, resource dependency resolving and resource caching. However, having to
declare those resources directly in the code is a nasty boilerplate to me. If you want to add a new
object in the demo, you have to turn it off, add the Rust line, re-compile the whole thing, then run
it once again. It breaks the advantage of having hot-reloading and it pollutes the rest of the code,
making it harder to spot the actual transformations going on.</p>
<p>This is even worse with the way I handle texts. It’s all <code>&amp;'static str</code> declared in a specific file
called <code>script.rs</code> with the same insane load of <code>let</code>. Then I rasterize them in a function and use
them in a very specific way regarding the time they appear. Not fun.</p>
<h2>Animation edition</h2>
<p>Most of the varying things you can see in my demos are driven by animation curves – splines. The
bare concept is very interesting: an animation contains control points that you know have a specific
value at a given time. Values in between are interpolated using an interpolation mode that can
change at each control points if needed. So, I use splines to animate pretty much everything: camera
movements, objects rotations, color masking, flickering, fade in / fade out effects, etc.</p>
<p>Because I wanted to be able to edit the animation in a comfy way – lesson learned from Céleri
Rémoulade, splines can be edited in realtime because they’re hot-reloadable. They live in JSON files
so that you just have to edit the JSON objects in each file and as soon as you save it, the
animation changes. I have to say, this was very ace to do. I’m so happy having coded such a feature.</p>
<p>However, it’s JSON. It’s already a thing. Though, I hit a serious problem when editing the
orientations data. In <strong>spectra</strong>, an orientation is encoded with a
<a href="https://en.wikipedia.org/wiki/Quaternion#Unit_quaternion">unit quaternion</a>. This is a 4-floating
number – hypercomplex. Editing those numbers in a plain JSON file is… challenging! I think I really
need some kind of animation editor to edit the spline.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 20 Apr 2017 00:00:00 GMT</pubDate></item><item><title>On programming workflows</title><link>https://strongly-typed-thoughts.net/blog/workflow</link><description><![CDATA[<p>For the last month, I had technical talks with plenty of programmers from all around the globe – thank
you IRC! Something interesting that often showed up in the discussion was the actual <em>workflow</em> we have
when writing code. Some people are use to <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> and won’t change their tools for anything else. Some
others use very basic text editors (I even know someone using <a href="https://www.barebones.com/products/bbedit">BBEdit</a> for his job work! crazy,
isn’t it?). I think it’s always a good thing to discuss that kind of topic, because it might give
you more hindsight on your own workflow, improve it, share it or just show your nice color scheme.</p>
<p>I’ll be talking about:</p>
<h1>My editor</h1>
<p>I’ve tried a lot of editors in the last ten years. I spent a year using emacs but eventually
discovered – erm, learned! – vim and completely fell in love with the <em>modal</em> editor. It was
something like a bit less than ten years ago. I then tried a lot of other editors (because of
curiosity) but failed to find something that would help be better than vim to write code. I don’t
want to start an editor war; here’s just my very personal point of view on editing. The concept of
modes in vim enables me to use a very few keystrokes to perform what I want to do (moving, commands,
etc.) and I feel very productive that way.</p>
<blockquote>
<p>A year ago, a friend advised me to switch to <a href="https://neovim.io">neovim</a>, which I have. My editor of the time is then
neovim, but it’s so close to vim that I tend to use (neo)vim. :)</p>
</blockquote>
<p>I don’t use any other editing tool. I even use neovim for taking notes while in a meeting or when I
need to format something in Markdown. I just use it everywhere.</p>
<h1>My neovim setup</h1>
<p>I use several plugins:</p>
<pre><code>Plugin 'VundleVim/Vundle.vim'
Plugin 'ctrlpvim/ctrlp.vim'
Plugin 'gruvbox'
Plugin 'neovimhaskell/haskell-vim'
Plugin 'itchyny/lightline.vim'
Plugin 'rust-lang/rust.vim'
Plugin 'jacoborus/tender.vim'
Plugin 'airblade/vim-gitgutter'
Plugin 'tikhomirov/vim-glsl'
Plugin 'plasticboy/vim-markdown'
Plugin 'cespare/vim-toml'
Plugin 'mzlogin/vim-markdown-toc'
Plugin 'ElmCast/elm-vim'
Plugin 'raichoo/purescript-vim'
Plugin 'easymotion'
Plugin 'scrooloose/nerdtree'
Plugin 'ryanoasis/vim-devicons'
Plugin 'tiagofumo/vim-nerdtree-syntax-highlight'
Plugin 'mhinz/vim-startify'
Plugin 'Xuyuanp/nerdtree-git-plugin'
Plugin 'tpope/vim-fugitive'
Plugin 'MattesGroeger/vim-bookmarks'
Plugin 'luochen1990/rainbow'
</code></pre>
<h2>Vundle</h2>
<p>A package manager. It just takes the list you read above and clones / keeps updated the git
repositories of the given vim packages. It’s a must have. There’re alternatives like Pathogen but
Vundle is very simple to setup and you don’t have to care about the file system: it cares for you.</p>
<h2>ctrlp</h2>
<p>This is one is a must-have. It gives you a fuzzy search buffer that traverse files, MRU, tags,
bookmarks, etc.</p>
<p><img src="http://phaazon.net/pub/ctrlp.png" alt="" /></p>
<p>I mapped the file search to the <code>, f</code> keys combination and the tag fuzzy search to <code>, t</code>.</p>
<h2>gruvbox</h2>
<p>The colorscheme I use. I don’t put an image here since you can find several examples online.</p>
<blockquote>
<p>This colorscheme also exists for a lot of other applications, among terminals and window managers.</p>
</blockquote>
<h2>haskell-vim</h2>
<p>Because I write a lot of Haskell, I need that plugin for language hilighting and linters… mostly.</p>
<h2>lightline</h2>
<p>The <a href="https://github.com/itchyny/lightline.vim">Lightline</a> vim statusline. A popular alternative is <a href="https://github.com/powerline/powerline">Powerline</a> for instance. I like Lightline
because it’s lightweight and has everything I want.</p>
<h2>rust.vim</h2>
<p>Same as for Haskell: I wrote a lot of Rust code so I need the language support in vim.</p>
<h2>tender</h2>
<p>A colorscheme for statusline.</p>
<h2>vim-gitgutter</h2>
<p>I highly recommend this one. It gives you diff as icons in the symbols list (behind the line numbers).</p>
<p><img src="http://phaazon.net/pub/vim-gitgutter.png" alt="" /></p>
<h2>vim-glsl</h2>
<p>GLSL support in vim.</p>
<h2>vim-markdown</h2>
<p>Markdown support in vim.</p>
<h2>vim-toml</h2>
<p>TOML support in vim.</p>
<blockquote>
<p>TOML is used in Cargo.toml, the configuration file for Rust projects.</p>
</blockquote>
<h2>vim-markdown-toc</h2>
<p>You’re gonna love this one. It enables you to insert a table of contents wherever you want in a
Markdown document. As soon as you save the buffer, the plugin will automatically refresh the TOC for
you, keeping it up-to-date. A must have if you want table of contents in your specifications or RFC
documents.</p>
<h2>elm-vim</h2>
<p>Elm support in vim.</p>
<h2>purescript-vim</h2>
<p>Purescript support in vim.</p>
<h2>easymotion</h2>
<p>A must have. As soon as you hit the corresponding keys, it will replace all words in your visible
buffer by a set of letters (typically one or two), enabling you to just press the associated
characters to jump to that word. This is the <em>vim motion</em> on steroids.</p>
<h2>nerdtree</h2>
<p>A file browser that appears at the left part of the current buffer you’re in. I use it to discover
files in a file tree I don’t know – I use it very often at work when I work on a legacy project, for
instance.</p>
<h2>vim-devicons</h2>
<p>A neat package that adds icons to vim and several plugins (nerdtree, ctrlp, etc.). It’s not a must-have
but I find it gorgeous so… just install it! :)</p>
<h2>vim-nerdtree-syntax-highlight</h2>
<p>Add more formats and files support to nerdtree.</p>
<h2>vim-startify</h2>
<p>A <em>cute</em> plugin that will turn the start page of vim into a MRU page, enabling you to jump to the
given file by just pressing its associated number.</p>
<blockquote>
<p>It also features a cow that gives you fortune catchphrases. Me gusta.</p>
</blockquote>
<h2>nerdtree-git-plugin</h2>
<p>Git support in nerdtree, it adds markers in front of file that have changes, diff, that were added,
etc.</p>
<h2>vim-fugitive</h2>
<p>A good Git integration package to vim. I use it mostly for its <code>Gdiff</code> diff tooling directly in vim,
even though I like using the command line directly for that. The best feature of that plugin is
the integrated blaming function, giving you the author of each line directly in a read-only vim
buffer.</p>
<h2>vim-bookmarks</h2>
<p>Marks on steroids.</p>
<h2>rainbow</h2>
<p>This little plugins is very neats as it allows me to add colors to matching symbols so that I can
easily see where I am.</p>
<p><img src="http://phaazon.net/pub/rainbow_vim.png" alt="" /></p>
<h1>Workflow in Rust</h1>
<p>I’m a rustacean. I do a lot of Rust on my spare time. My typical workflow is the following:</p>
<ol>
<li>I edit code in neovim</li>
<li>Depending on the project (whether I have a robust unit tests base or not or whether I’m writing a
library or an app), I use several <code>cargo</code> commands:
<ul>
<li>for a library project, I split my screen in two and have a <code>cargo watch -x test</code> running; this
command is a file watcher that will run all the tests suites in the project as soon as a file
changes;</li>
<li>for an app project, I split my screen in two and have a <code>cargo watch</code> – similar to <code>cargo watch -x check</code>
running; this command is a file watcher that will proceed through the compilation of the binary
but it will not compile it down to an actual binary; it’s just a check, it doesn’t produce
anything you can run. You can manually run that command with <code>cargo check</code>. See more
<a href="https://github.com/rust-lang/cargo/pull/3296#event-893283611">here</a>;</li>
<li>for other use cases, I tend to run <code>cargo check</code> by hand and run <code>cargo build</code> to test the
actualy binary</li>
</ul>
</li>
<li>Because I’m a unix lover, I couldn’t work correctly without a terminal, so I use neovim in a
terminal and switch from the console to the editor with the keybinding <code>C-z</code> and the <code>jobs</code> and
<code>fg</code> commands. I do that especially to create directories, issue git commands, deploy things,
etc. It’s especially ultra cool to run a <code>rg</code> search or even a <code>find</code> / <code>fd</code>. I sometimes do that
directly in a neovim buffer – with the <code>:r!</code> command – when I know I need to edit things from the
results. Bonus: refactoring with <code>tr</code>, <code>sed</code> and <code>find -exec</code> is <strong>ultra</strong> neat.</li>
</ol>
<h1>Workflow in Haskell</h1>
<p>The workflow is almost exactly the same besides the fact I use the <code>stack build --fast --file-watch</code>
command to have a file watcher.</p>
<blockquote>
<p>Haskell’s stack doesn’t currently the awesome <code>check</code> command Rust’s cargo has. Duh :(</p>
</blockquote>
<p>I also have a similar workflow for other languages I work in, like Elm, even though I use standard
unix tools for the file watching process.</p>
<h1>Git workflow</h1>
<p>Aaaah… <code>git</code>. What could I do without it? Pretty much nothing. <code>git</code> is such an important piece of
software and brings such an important project philosophy and behaviors to adopt.</p>
<p>What I find very cool in git – besides the tool itself – is everything around it. For instance, on
GitHub, you have the concept of Pull Request (PR) – Merge Request in GitLab (MR). Associated with a
good set of options, like disallowing people to push on the <code>master</code> branch, hooks, forcing people
to address any comments on their MR, this allows for better code reviews and overall a way better
quality assurance of the produced code than you could have without using all this infrastructure.
Add a good set of DevOps for deployment and production relatide issues and you have a working team
that has no excuse to produce bad code!</p>
<h2>Some git candies I love working with</h2>
<p>My neovim fugitive plugin allows me to open a special buffer with the <code>:Gblame</code> command that gives
me a <code>git blame</code> annotation of the file. This might be trivial but it’s very useful, especially at
work when I have my colleagues next me – it’s always better to directly ask them about something
than guessing.</p>
<p><img src="http://phaazon.net/pub/vim_fugitive_blame.png" alt="" /></p>
<p>Another one that I love is <code>:Gdiff</code>, that gives you a <code>git diff</code> of the modification you’re about to
stage. I often directly to a <code>git diff</code> in my terminal, but I also like how this plugin nails it as
well. Very pratictal!</p>
<p><img src="http://phaazon.net/pub/vim_fugitive_diff.png" alt="" /></p>
<h1>General note on workflow</h1>
<p>It’s always funny to actually witness difference in workflows, especially at work. People who use
mostly IDEs are completely overwhelmed by my workflow. I was completely astonished at work that some
people hadn’t even heard of <code>sed</code> before – they even made a Google search! I’m a supporter of the
philosophy that one should use the tool they feel comfortable with and that there’s no “ultimate”
tool for everyone. However, for my very own person, I really can’t stand IDEs, with all the buttons
and required clicks you have to perform all over the screen. I really think it’s a waste of time,
while using a modal editor like neovim with a bépo keyboard layout (French dvorak) and going back
and forth to the terminal is just incredibly simple, yet powerful.</p>
<p>I had a pretty good experience with <a href="https://atom.io">Atom</a>, a modern editor. But when I’ve been told it’s written
with web technologies, the fact it’s slow as f<em>ck as soon as you start having your own tooling
(extensions), its pretty bad behavior and incomprensible “Hey, I do whatever the f</em>ck I want and
I’ll just reset your precious keybindings!” or all the weird bugs – some of my extensions won’t just
work if I have an empty pane open, wtf?!… well, I was completely bolstered that GUI interfaces, at
least for coding and being productive, are cleary not for me. With my current setup, my hands
<em>never</em> move from the keyboard – my thrists are completely static. With all the candies like
easymotion, ctrlp, etc. etc. I just can’t find any other setups faster and comfier than this one.</p>
<p>There’s even an extra bonus to my setup: because I use mostly unix tools and neovim, it’s pretty
straigth-forward to remote-edit something via ssh, because everything happens in a terminal. That’s
not something you can do easily with Atom, Sublime Text or any other editors / IDEs – and you even
pay for that shit! No offence!</p>
<p>However, there’s a caveat: because pretty much everything I do is linked to my terminal, the user
experience mostly relies on the performance of the terminal. Using a bad terminal will result in an
overall pretty bad experience, should it be editing, compiling, git or ssh. That’s why I keep
lurking at new terminal emulaters – <a href="https://github.com/jwilm/alacritty">alacritty</a> seems very promising, yet it’s still too buggy and
lacks too many features to be production-ready to me – but it’s written in Rust and is
GPU-accelerated, hells yeah!</p>
<h1>Conclusion</h1>
<p>Whoever you are, whatever you do, whomever you work with, I think the most important thing about
workflow is to find the one that fits your needs the most. I have a profound, deep digust for
proprietary and closed software like Sublime Text and IDEs that use GUIs while keyboard shortcut are
just better. To me, the problem is about the learning curve and actually wanting to pass it –
because yes, learning (neo)vim in details and mastering all its nits is not something you’ll do in
two weeks; it might take months or years, but it’s worth it. However, as I said, if you just feel
good with your IDE, I will not try to convert you to a modal editor or a unix-based workflow…
because you wouldn’t be as productive as you already are.</p>
<p>Keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 23 Jul 2017 00:00:00 GMT</pubDate></item><item><title>Smoothie, a Haskell library for creating smooth curves</title><link>https://strongly-typed-thoughts.net/blog/smoothie</link><description><![CDATA[<h1>The smoother the better!</h1>
<p>It’s been a while I haven’t written anything on my blog. A bit of refreshment
doesn’t hurt much, what do you think?</p>
<p>As a <a href="http://en.wikipedia.org/wiki/Key_frame">demoscener</a>, I attend
demoparties, and there will be a very important and fun one in about a month.
I’m rushing on my 3D application so that I can finish something to show up, but
I’m not sure I’ll have enough spare time. That being said, I need to be able to
represent smooth moves and transitions without any tearing. I had a look into a
few <strong>Haskell</strong> <em>spline</em> libraries, but I haven’t found anything
interesting – or not discontinued.</p>
<p>Because I do need splines, I decided to write my very own package. Meet
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a>, my <strong>BSD3</strong> Haskell
spline library.</p>
<h2>Why splines?</h2>
<p>A <em>spline</em> is a curve defined by several polynomials. It has several uses, like
vectorial graphics, signal interpolation, animation tweening or simply plotting
a spline to see how neat and smooth it looks!</p>
<p>Splines are defined using polynomials. Each polynomials is part of the curve and
connected one-by-one. Depending on which polynomial(s) you chose, you end up
with a different shape.</p>
<p>For instance, 1-degree polynomials are used to implement straight lines.</p>
<p><img src="http://phaazon.net/pub/linear_spline.png" alt="" /></p>
<p>As you can see, we can define a few points, and interpolate in between. This is
great, because we can turn a <em>discrete</em> set of points into lines.</p>
<p>Even better, we could use 3-degree polynomials or cosine functions to make each
part of the spline smoother:</p>
<p><img src="http://phaazon.net/pub/spline.png" alt="" /></p>
<p>We still have discrete points, but in the end, we end up with a smooth set of
points. Typically, imagine sampling from the spline with time for a camera
movement. It helps us to build smooth moves. This is pretty important when doing
animation. If you’re curious about that, I highly recommend having a look into
<a href="http://en.wikipedia.org/wiki/Key_frame">key frames</a>.</p>
<h2>Using splines in Haskell</h2>
<p>So I’ve been around implementing splines in Haskell the most general way as
possible. However, I don’t cover – yet? – all kinds of splines. In order to
explain my design choices, I need to explain a few very simple concepts first.</p>
<h3>Sampling</h3>
<p>A spline is often defined by a set of points and polynomials. The first point
has the starting <em>sampling</em> value. For our purpose, we’ll set that to <em>0</em>:</p>
<pre><code class="language-haskell">let startSampler = 0
</code></pre>
<p>The sampling value is often <code>Float</code>, but it depends on your spline and the use
you want to make of it. It could be <code>Int</code>. The general rule is that it <strong>should
be orderable</strong>. If we take two sampling values <code>s</code> and <code>t</code>, we should be able
to compare <code>s</code> and <code>t</code> (that’s done through the typeclass constraint <code>Ord</code> in
Haskell).</p>
<p>So, if you have a spline and a sampling value, the idea is that sampling the
spline with <code>startSampler</code> gives you the first point, and sampling with
<code>t</code> with <code>t &gt; startSampler</code> gives you another point, interpolated using points
of the spline. It could use two points, three, four or even more. It actually
depends on the polynomials you use, and the interpolating method.</p>
<p>In <a href="https://hackage.haskell.org/package/smoothie">smoothie</a>, sampling values
have types designed by <code>s</code>.</p>
<h3>Control points</h3>
<p>A spline is made of points. Those points are called <strong>control points</strong> and
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a> uses <code>CP s a</code> to refer
to them, where <code>s</code> is the sampling type and <code>a</code> the carried value.</p>
<p>Although they’re often used to express the fact that the curve should pass
through them, they don’t have to lie on the curve itself. A very common and
ultra useful kind of spline is the B-spline.</p>
<p><img src="http://phaazon.net/pub/b_spline.png" alt="" /></p>
<p>With that kind of spline, the property that the curve passes through the control
points doesn’t hold. It passes through the first and last ones, but the ones
in between are used to <em>shape</em> it, a bit like <strong>magnets</strong> attract things around
them.</p>
<p>Keep in mind that <strong>control points</strong> are very important and used to define the
main aspect of the curve.</p>
<h3>Polynomials</h3>
<p>Polynomials are keys to spline interpolation. They’re used to <em>deduce</em> sampled
points. Interpolation is a very general term and used in plenty of domains. If
you’re not used to that, you should inquiry about
<a href="http://en.wikipedia.org/wiki/Linear_interpolation">linear interpolation</a> and
<a href="http://en.wikipedia.org/wiki/Cubic_Hermite_spline">cubic interpolation</a>, which
are a very good start.</p>
<p>Polynomials are denoted by <code>Polynomial s a</code> in
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a>, where <code>s</code> and <code>a</code> have
the same meaning than in <code>CP s a</code>.</p>
<h2>Getting started with smoothie</h2>
<h3>Types and constraints</h3>
<p><a href="https://hackage.haskell.org/package/smoothie">smoothie</a> has then three important
types:</p>
<ul>
<li><code>CP s a</code>, the control points</li>
<li><code>Polynomial</code>, the polynomials used to interpolate between control points</li>
<li><code>Spline s a</code>, of course</li>
</ul>
<p>The whole package is parameterized by <code>s</code> and <code>a</code>. As said earlier, <code>s</code> is very
likely to require an <code>Ord</code> constraint. And <code>a</code>… Well, since we want to represent
points, let’s wonder: which points? What kind of points? Why even <em>“points”</em>?
That’s a good question. And this is why you may find
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a> great: it doesn’t
actually know anything about points. It accepts <strong>any kind of values</strong>. Any?
Almost. Any values that are in an <strong>additive group</strong>.</p>
<blockquote>
<p><em>“What the…”</em></p>
</blockquote>
<p>I won’t go into details, I’ll just vulgarize them so that you get quickly your
feet wet. That constraint, when applied to <strong>Haskell</strong>, makes <code>a</code> to be
an endofunctor – i.e. <code>Functor</code> – and additive – i.e. <code>Additive</code>. It also
requires it to be a first-class value – i.e. its kind should be <code>* -&gt; *</code>.</p>
<p>With <code>Functor</code> and <code>Additive</code>, we can do two important things:</p>
<ul>
<li>First, with <code>Functor</code>. It enables us to lift computation on the inner type.
We can for instance apply a single function inside <code>a</code>, like <code>*k</code> or <code>/10</code>.</li>
<li>Then, with <code>Additive</code>. It enables us to add our types, like <code>a + b</code>.</li>
</ul>
<p>We can then make <strong>linear combinations</strong>, like a<em>k + b</em>q. This property is well
known for <a href="http://en.wikipedia.org/wiki/Vector_space">vector spaces</a>.</p>
<p>The fun consequence is that providing correct instances to <code>Functor</code> and
<code>Additive</code> will make your type useable with
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a> as carried value in the
spline! You might also have to implement <code>Num</code> and <code>Ord</code> as well, though.</p>
<h3>Creating a spline</h3>
<p>Creating a spline is done with the <code>spline</code> function, which signature is:</p>
<pre><code class="language-haskell">spline :: (Ord a, Ord s) =&gt; [(CP s a, Polynomial s a)] -&gt; Spline s a
</code></pre>
<p>It takes a list of <strong>control points</strong> associated with <strong>polynomials</strong> and
outputs a <strong>spline</strong>. That requires some explainations… When you’ll be sampling
the spline,
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a> will look for which
kind of interpolation method it has to use. This is done by the lower nearest
control point to the sampled value. Basically, a pair <code>(cp,polynomial)</code> defines
a new point and the interpolation method to use for the curve ahead of the
point.</p>
<p>Of course, the latest point’s polynomial won’t be used. You can set whatever you
want then – protip: you can even set <code>undefined</code> because of laziness.</p>
<p>Although the list will be sorted by <code>spline</code>, I highly recommend to pass a
sorted list, because dealing with unordered points might have no sense.</p>
<p>A control point is created by providing a sample value and the carried value.
For instance, using <a href="https://hackage.haskell.org/package/linear">linear</a>’s <code>V2</code>
type:</p>
<pre><code class="language-haskell">let cp0 = CP 0 $ V2 1 pi
</code></pre>
<p>That’s a control point that represents <code>V2 1 pi</code> when sampling is at <code>0</code>. Let’s
create another:</p>
<pre><code class="language-haskell">let cp1 = CP 3.341 $ V2 0.8 10.5
</code></pre>
<p>Now, let’t attach a polynomial to them!</p>
<h4>Hold that for me please</h4>
<p>The simplest polynomial – wich is actually not a polynomial, but heh, don’t look
at me that way – is the 0-degree polynomial. Yeah, a constant function. It takes
the lower control point, and <em>holds</em> it everwhere on the curve. You could
picture that as a staircase function:</p>
<p><img src="http://phaazon.net/pub/hold_spline.gif" alt="" /></p>
<p>You might say that’s useless; it’s actually not; it’s even pretty nice. Imagine
you want to attach your camera position onto such a curve. It will make the
camera <em>jump</em> in space, which could be desirable for flash looks!</p>
<p>Use the <code>hold</code> <code>Polynomial</code> to use such a behavior.</p>
<h4>Oh yeah, it’s straightforward!</h4>
<p>1-degree functions often describe <em>lines</em>. That is, <code>linear</code> is the <code>Polynomial</code>
to use to connect control points with… straight lines.</p>
<h4>Have fun on the unit circle!</h4>
<p>One very interesting <code>Polynomial</code> is <code>cosine</code>, that defines a cosine
interpolation, used to smooth the spline and make it nicer for moves and
transitions.</p>
<h4>Highway to the danger zone</h4>
<p>If you’re crazy, you can experiment around with <code>linearBy</code>, which, basically, is
a 1-degree polynomial if you pass <code>id</code>, but will end up in most complex shapes
if you pass another function – <code>(s -&gt; s)</code>. Dig in documentation on <em>hackage</em> for
further information.</p>
<h4>Sampling our spline</h4>
<p>Ok, let’s use a linear interpolation to sample our spline:</p>
<pre><code class="language-haskell">let spl = spline [(cp0,linear),(cp1,hold)]
</code></pre>
<blockquote>
<p><em>Note: I used <code>hold</code> as a final polynomial because I don’t like using
<code>undefined</code>.</em></p>
</blockquote>
<p>Ok, let’s see how to sample that.
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a> exports a convenient
function for sampling:</p>
<pre><code class="language-haskell">smooth :: Ord s =&gt; Spline s a -&gt; s -&gt; Maybe a
</code></pre>
<p><code>smooth spl s</code> takes the sampling value <code>s</code> and maybe interpolate it in the
<code>spl</code> spline.</p>
<blockquote>
<p><em>“Maybe? Why aren’t you sure?”</em></p>
</blockquote>
<p>Well, that’s pretty simple. In some cases, the curve is not defined at the
sampling value you pass. Before the first point and after, basically. In those
cases, you get <code>Nothing</code>.</p>
<h2>That’s not the end</h2>
<p>I wrote <a href="https://github.com/phaazon/smoothie">smoothie</a> in a few hours, in a
single day. You might have ideas. I want it to be spread and widely used by
awesome people. Should you do graphics programming, sound programming, animation
or whatever implying splines or smoothness, please provide feedback!</p>
<p>For people that would like to get contributing, here’s the
<a href="https://github.com/phaazon/smoothie">github page</a> and the
<a href="https://github.com/phaazon/smoothie/issues">issue tracker</a>.</p>
<p>If no one comes up with, I’ll try to add some cubic interpolation methods, like
<em>hermitian splines</em>, and one of my favorite, the famous <strong>Catmull Rom</strong> spline
interpolation method.</p>
<p>As always, have fun hacking around, and keep doing cool stuff and sharing it!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 26 Feb 2015 00:00:00 GMT</pubDate></item><item><title>State of luminance</title><link>https://strongly-typed-thoughts.net/blog/state_of_luminance</link><description><![CDATA[<p>I’ve been a bit surprised for a few days now. Some rustaceans posted two links about my <a href="https://crates.io/crates/spectra">spectra</a>
and <a href="https://crates.io/crates/luminance">luminance</a> crates on reddit – links <a href="https://www.reddit.com/r/rust/comments/6z58n3/spectra_a_demoscene_engine_in_rust">here</a>
and <a href="https://www.reddit.com/r/rust_gamedev/comments/6zmtb4/luminance_typesafe_typelevel_and_stateless_rust">here</a>.
I didn’t really expect that: the code is public, on Github, but I don’t communicate about them <em>that
much</em>.</p>
<p>However, I saw interested people, and I think it’s the right time to write a blog post about some
design choices. I’ll start off with luminance and I’ll speak about spectra in another blog post
because I truly think spectra starts to become very interesting (I have my own shading language
bundled up within, which is basically GLSL on steroids).</p>
<h1>luminance and how it copes with fast and as-stateless-as-possible graphics</h1>
<h2>Origins</h2>
<p>luminance is a library that I wrote, historically, in Haskell. You can find the package
<a href="https://hackage.haskell.org/package/luminance">here</a> if you’re interested – careful though, it’s
dying and the Rust version has completely drifted path with it. Nevertheless, when I ported the
library to Rust, I imported the same “way of programming” that I had·have in Haskell – besides the
allocation scheme; remember, I’m a demoscener, I <strong>do care a lot about performance, CPU usage, cache
friendliness and runtime size</strong>. So the Rust luminance crate was made to be hybrid: it has the
cool functional side that I imported from my Haskell codebase, and the runtime performance I wanted
that I had when I wrote my two first 64k in C++11. I had to remove and work around some features
that only Haskell could provide, such as higher-kinded types, type families, functional
dependencies, GADTs and a few other things such as existential quantification (trait objects saved
me here, even though I don’t use them that much in luminance now).</p>
<p>I have to admit, I dithered a lot about the scope of luminance — both in Haskell and Rust. At first,
I thought that it’d be great to have a <em>“base”</em> crate, hosting common and abstracted code, and
<em>“backend”</em> crates, implementing the abstract interface. That would enable me to have several
backends – OpenGL, Vulkan, Metal, a software implementation, something for Amiigaaaaaaa, etc.
Though, time has passed, and now, I think it’s:</p>
<ul>
<li>Overkill.</li>
<li>A waste of framework.</li>
</ul>
<p>The idea is that if you need to be very low-level on the graphics stack of your application, you’re
likely to know what you are doing. And then, your needs will be very precise and well-defined. You
might want very specific piece of code to be available, related to a very specific technology.
That’s the reason why abstracting over very low-level code is not a good path to me: you need to
expose as most as posssible the low-level interface. That’s the goal of luminance: exposing OpenGL’s
interface in a stateless, bindless and typesafe way, with no or as minimal as possible runtime
overhead.</p>
<p>More reading <a href="http://phaazon.blogspot.fr/2016/08/luminance-designs.html">here</a>.</p>
<h2>Today</h2>
<p>Today, luminance is almost stable – it still receives massive architecture redesign from time to
time, but it’ll hit the <code>1.0.0</code> release soon. As discussed with <a href="https://github.com/kvark">kvark</a> lately, luminance is not
about the same scope as <a href="https://crates.io/crates/gfx">gfx</a>’s one. The goals of luminance are:</p>
<ul>
<li>To be a typesafe, stateless and bindless OpenGL framework.</li>
<li>To provide a friendly experience and expose as much as possible all of the OpenGL features.</li>
<li>To be very lightweight (the target is to be able to use it without <code>std</code> nor <code>core</code>).</li>
</ul>
<p>To achieve that, luminance is written with several aspects in mind:</p>
<ul>
<li>Allocation must be explicitely stated by the user: we must avoid as much as possible to allocate
things in luminance since it might become both a bottleneck and an issue to the lightweight aspect.</li>
<li>Performance is a first priority; safety comes second. If you have a feature that can be either
exclusively performant or safe, it must then be performant. Most of the current code is, for our
joy, both performant and safe. However, some invariants are left around the place and you might
shoot your own feet. This is an issue and some reworking must be done (along with tagging
some functions and traits <code>unsafe</code>).</li>
<li>No concept of backends will ever end up in luminance. If it’s decided to switch to <a href="https://www.khronos.org/vulkan">Vulkan</a>, the
whole luminance API will and <strong>must</strong> be impacted, so that people can use <a href="https://www.khronos.org/vulkan">Vulkan</a> the best
possible way.</li>
<li>A bit like the first point, the code must be written in a way that the generated binary is as
small as possible. Generics are not forbidden – they’re actually recommended – but things like
crate dependencies are likely to be forbidden (exception for the <code>gl</code> dependency, of course).</li>
<li>Windowing <strong>must not be addressed by luminance</strong>. This is crucial. As a demoscener, if I want to
write a 64k with luminance, I must be able to use a library over X11 or the Windows API to setup
the OpenGL context myself, set the OpenGL pointers myself, etc. This is not the typical usecase –
who cares besides demosceners?! – but it’s still a good advantage since you end up with loose
coupling for free.</li>
</ul>
<h2>The new luminance</h2>
<p>luminance has received more attention lately, and I think it’s a good thing to talk about how to use
it. I’ll add examples on github and its <a href="https://docs.rs/luminance">docs.rs</a> online documentation.</p>
<p>I’m going to do that like a tutorial. It’s easier to read and you can test the code in the same
time. Let’s render a triangle!</p>
<blockquote>
<p>Note: keep in mind that you need a nightly compiler to compile luminance.</p>
</blockquote>
<h3>Getting your feet wet</h3>
<p>I’ll do everything from scratch with you. I’ll work in <code>/tmp</code>:</p>
<pre><code>$ cd /tmp
</code></pre>
<p>First thing first, let’s setup a <code>lumitest</code> Rust binary project:</p>
<pre><code>$ cargo init --bin lumitest
  Created binary (application) project
$ cd lumitest
</code></pre>
<p>Let’s edit our <code>Cargo.toml</code> to use luminance. We’ll need two crates:</p>
<ul>
<li>The <a href="https://crates.io/crates/luminance">luminance</a> crate.</li>
<li>A way to open the OpenGL context; we’ll use GLFW, so the <a href="https://crates.io/crates/luminance-glfw">luminance-glfw</a> crate.</li>
</ul>
<p>At the time of writing, corresponding versions are
<a href="https://crates.io/crates/luminance/0.23.0">luminance-0.23.0</a> and
<a href="https://crates.io/crates/luminance-glfw/0.3.2">luminance-glfw-0.3.2</a>.</p>
<p>Have the following <code>[dependencies]</code> section</p>
<pre><code>[dependencies]
luminance = "0.23.0"
luminance-glfw = "0.3.2"
</code></pre>
<pre><code>$ cargo check
</code></pre>
<p>Everything should be fine at this point. Now, let’s step in in writing some code.</p>
<pre><code>
extern crate luminance;
extern crate luminance_glfw;

use luminance_glfw::{Device, WindowDim, WindowOpt};

const SCREEN_WIDTH: u32 = 960;
const SCREEN_HEIGHT: u32 = 540;

fn main() {
  let rdev = Device::new(WindowDim::Windowed(SCREEN_WIDTH, SCREEN_HEIGHT), "lumitest", WindowOpt::default());
}
</code></pre>
<p>The <code>main</code> function creates a <a href="https://docs.rs/luminance-glfw/0.3.2/luminance_glfw/struct.Device.html">Device</a>
that is responsible in holding the windowing stuff for us.</p>
<p>Let’s go on:</p>
<pre><code>
match rdev {
  Err(e) => {
    eprintln!("{:#?}", e);
    ::std::process::exit(1);
  }

  Ok(mut dev) => {
    println!("let’s go!");
  }
}
</code></pre>
<p>This block will catch any <code>Device</code> errors and will print them to <code>stderr</code> if there’s any.</p>
<p>Let’s write the main loop:</p>
<pre><code>
'app: loop {
  for (_, ev) in dev.events() { // the pair is an interface mistake; it’ll be removed
    match ev {
      WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
      _ => ()
    }
  }
}
</code></pre>
<p>This loop runs forever and will exit if you hit the escape key or quit the application.</p>
<h3>Setting up the resources</h3>
<p>Now, the most interesting thing: rendering the actual triangle! You will need a few things:</p>
<pre><code>
type Position = [f32; 2];
type RGB = [f32; 3];
type Vertex = (Position, RGB);

const TRIANGLE_VERTS: [Vertex; 3] = [
  ([-0.5, -0.5], [0.8, 0.5, 0.5]), // red bottom leftmost
  ([-0., 0.5], [0.5, 0.8, 0.5]), // green top
  ([0.5, -0.5], [0.5, 0.5, 0.8]) // blue bottom rightmost
];
</code></pre>
<p><code>Position</code>, <code>Color</code> and <code>Vertex</code> define what a vertex is. In our case, we use a 2D position and a
RGB color.</p>
<blockquote>
<p>You have a lot of choices here to define the type of your vertices. In theory, you can choose
any type you want. However, it must implement the <a href="https://docs.rs/luminance/0.23.0/luminance/vertex/trait.Vertex.html"><code>Vertex</code></a>
trait. Have a look at the implementors that already exist for a faster start off!</p>
</blockquote>
<blockquote>
<p><strong>Important</strong>: do not confuse between <code>[f32; 2]</code> and <code>(f32, f32)</code>. The former is a single 2D
vertex component. The latter is two 1D components. It’ll make a huge difference when writing shaders.</p>
</blockquote>
<p>The <code>TRIANGLE_VERTS</code> is a constant array with three vertices defined in it: the three vertices of
our triangle. Let’s pass those vertices to the GPU with the <a href="https://docs.rs/luminance/0.23.0/luminance/tess/struct.Tess.html"><code>Tess</code></a>
type:</p>
<pre><code>
// at the top location
use luminance::tess::{Mode, Tess, TessVertices};

// just above the main loop
let triangle = Tess::new(Mode::Triangle, TessVertices::Fill(&TRIANGLE_VERTS), None);
</code></pre>
<p>This will pass the <code>TRIANGLE_VERTS</code> vertices to the GPU. You’re given back a <code>triangle</code> object. The
<a href="https://docs.rs/luminance/0.23.0/luminance/tess/enum.Mode.html"><code>Mode</code></a> is a hint object that
states how vertices must be connected to each other. <code>TessVertices</code> lets you slice your vertices –
this is typically enjoyable when you use a mapped buffer that contains a dynamic number of vertices.</p>
<p>We’ll need a <em>shader</em> to render that triangle. First, we’ll place its source code in <code>data</code>:</p>
<pre><code>$ mkdir data
</code></pre>
<p>Paste this in <code>data/vs.glsl</code>:</p>
<pre><code>
layout (location = 0) in vec2 co;
layout (location = 1) in vec3 color;

out vec3 v_color;

void main() {
  gl_Position = vec4(co, 0., 1.);
  v_color = color;
}
</code></pre>
<p>Paste this in <code>data/fs.glsl</code>:</p>
<pre><code>
in vec3 v_color;

out vec4 frag;

void main() {
  frag = vec4(v_color, 1.);
}
</code></pre>
<p>And add this to your <code>main.rs</code>:</p>
<pre><code>
const SHADER_VS: &str = include_str!("../data/vs.glsl");
const SHADER_FS: &str = include_str!("../data/fs.glsl");
</code></pre>
<blockquote>
<p>Note: this is not a typical workflow. If you’re interested in shaders, have a look at how I do it
in <a href="https://crates.io/crates/spectra">spectra</a>. That is, hot reloading it via SPSL (Spectra Shading Language), which enables to
write GLSL modules and compose them in a single file but just writing functions. The functional
programming style!</p>
</blockquote>
<p>Same thing as for the tessellation, we need to pass the source to the GPU’s compiler to end up with
a shader object:</p>
<pre><code>
// add this at the top of your main.rs
use luminance::shader::program::Program;

// below declaring triangle
let (shader, warnings) = Program::<Vertex, (), ()>::from_strings(None, SHADER_VS, None, SHADER_FS).unwrap();

for warning in &warnings {
  eprintln!("{:#?}", warning);
}
</code></pre>
<p>Finally, we need to tell luminance in which framebuffer we want to make the render. It’s simple: to
the default framebuffer, which ends up to be… your screen’s back buffer! This is done this way with
luminance:</p>
<pre><code>
use luminance::framebuffer::Framebuffer;

let screen = Framebuffer::default([SCREEN_WIDTH, SCREEN_HEIGHT]);
</code></pre>
<p>And we’re done for the resources. Let’s step in the actual render now.</p>
<h3>The actual render: the pipeline</h3>
<p>luminance’s approach to render is somewhat not very intuitive yet very simple and very efficient:
the render pipeline is explicitly defined by the programmer in Rust, on the fly. That means that
you must express the actual state the GPU must have for the whole pipeline. Because of the nature of
the pipeline, which is an AST (Abstract Syntax Tree), you can batch sub-parts of the pipeline (we
call such parts <em>nodes</em>) and you end up with minimal GPU state switches. The theory is as following:</p>
<ul>
<li>At top most, you have the <a href="https://docs.rs/luminance/0.23.0/luminance/pipeline/fn.pipeline.html"><code>pipeline</code></a>
function that introduces the concept of <em>shading things to a framebuffer</em>.
<ul>
<li>Nested, you find the concept of a <em>shader gate</em>. That is, an object linked to its parent
(pipeline) and that gives you the concept of <em>shading things with a shader</em>.
<ul>
<li>Nested, you find the concept of <em>rendering things</em>. That is, can set on such nodes GPU states,
such as whether you want a depth test, blending, etc.
<ul>
<li>Nested, you find the concept of a <em>tessellation gate</em>, enabling you to render actual <code>Tess</code>
objects.</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>That deep nesting enables you to batch your objects on a very fine granularity. Also, notice that
the functions are not about slices of <code>Tess</code> or hashmaps of <code>Program</code>. The allocation scheme is
completely ignorant about how the data is traversed, which is good: you decide. If you need to
borrow things on the fly in a shading gate, you can.</p>
<p>Let’s get things started:</p>
<pre><code>
use luminance::pipeline::{entry, pipeline};

entry(|_| {
  pipeline(&screen, [0., 0., 0., 1.], |shd_gate| {
    shd_gate.shade(&shader, |rdr_gate, _| {
      rdr_gate.render(None, true, |tess_gate| {
        let t = &triangle;
        tess_gate.render(t.into());
      });
    });
  });
});
</code></pre>
<p>We just need a final thing now: since we render to the back buffer of the screen, if we want to see
anything appear, we need to <em>swap the buffer chain</em> so that the back buffer become the front buffer
and the front buffer become the back buffer. This is done by wrapping our render code in the
<a href="https://docs.rs/luminance-glfw/0.3.2/luminance_glfw/struct.Device.html#method.draw"><code>Device::draw</code></a>
function:</p>
<pre><code>
dev.draw(|| {
  entry(|_| {
    pipeline(&screen, [0., 0., 0., 1.], |shd_gate| {
      shd_gate.shade(&shader, |rdr_gate, _| {
        rdr_gate.render(None, true, |tess_gate| {
          let t = &triangle;
          tess_gate.render(t.into());
        });
      });
    });
  });
});
</code></pre>
<p>You should see this:</p>
<p><img src="https://i.imgur.com/x3jC9qm.png" alt="" /></p>
<p>As you can see, the code is pretty straightforward. Let’s get deeper, and let’s kick some time in!</p>
<pre><code>
use std::time::Instant;

// before the main loop
let t_start = Instant::now();
// in your main loop
let t_dur = t_start.elapsed();
let t = (t_dur.as_secs() as f64 + t_dur.subsec_nanos() as f64 * 1e-9) as f32;
</code></pre>
<p>We have the time. Now, we need to pass it down to the GPU (i.e. the shader). luminance handles that
kind of things with two concepts:</p>
<ul>
<li>Uniforms.</li>
<li>Buffers.</li>
</ul>
<p>Uniforms are a good match when you want to send data to a specific shader, like a value that
customizes the behavior of a shading algorithm.</p>
<p>Because buffers are shared, you can use buffers to share data between shader, leveraging the need to
pass the data to all shader by hand – you only pass the index to the buffer that contains the data.</p>
<p>We won’t conver the buffers for this time.</p>
<p>Because of type safety, luminance requires you to state which types the uniforms the shader contains
are. We only need the time, so let’s get this done:</p>
<pre><code>
// you need to alter this import
use luminance::shader::program::{Program, ProgramError, Uniform, UniformBuilder, UniformInterface, UniformWarning};

struct TimeUniform(Uniform<f32>);

impl UniformInterface for TimeUniform {
  fn uniform_interface(builder: UniformBuilder) -> Result<(Self, Vec<UniformWarning>), ProgramError> {
    // this will fail if the "t" variable is not used in the shader
    //let t = builder.ask("t").map_err(ProgramError::UniformWarning)?;

    // I rather like this one: we just forward up the warning and use the special unbound uniform
    match builder.ask("t") {
      Ok(t) => Ok((TimeUniform(t), Vec::new())),
      Err(e) => Ok((TimeUniform(builder.unbound()), vec![e]))
    }
  }
}
</code></pre>
<p>The <a href="https://docs.rs/luminance/0.23.0/luminance/shader/program/struct.UniformBuilder.html#method.unbound"><code>UniformBuilder::unbound</code></a>
is a simple function that gives you any uniform you want: the resulting uniform object will just do
nothing when you pass values in. It’s a way to say <em>“— Okay, I don’t use that in the shader yet, but
don’t fail, it’s not really an error”.</em> Handy.</p>
<p>And now, all the magic: how do we access that uniform value? It’s simple: via types! Have you
noticed the type of our <code>Program</code>? For the record:</p>
<pre><code>
let (shader, warnings) = Program::<Vertex, (), ()>::from_strings(None, SHADER_VS, None, SHADER_FS).unwrap();
</code></pre>
<p>See the type is parametered with three type variables:</p>
<ul>
<li>The first one – here <code>Vertex</code>, our own type – is for the <em>input</em> of the shader program.</li>
<li>The second one is for the <em>output</em> of the shader program. It’s currently not used at all by
luminance by is reserved, as it will be used later for enforcing even further type safety.</li>
<li>The third and latter is for the <em>uniform interface</em>.</li>
</ul>
<p>You guessed it: we need to change the third parameter from <code>()</code> to <code>TimeUniform</code>:</p>
<pre><code>
let (shader, warnings) = Program::<Vertex, (), TimeUniform>::from_strings(None, SHADER_VS, None, SHADER_FS).unwrap();
</code></pre>
<p>And <em>that’s all</em>. Whenever you <code>shade</code> with a <code>ShaderGate</code>, the type of the shader object is being
inspected, and you’re handed with the uniform interface:</p>
<pre><code>
shd_gate.shade(&shader, |rdr_gate, uniforms| {
  uniforms.0.update(t);

  rdr_gate.render(None, true, |tess_gate| {
    let t = &triangle;
    tess_gate.render(t.into());
  });
});
</code></pre>
<p>Now, change your fragment shader to this:</p>
<pre><code>
in vec3 v_color;

out vec4 frag;

uniform float t;

void main() {
  frag = vec4(v_color * vec3(cos(t * .25), sin(t + 1.), cos(1.25 * t)), 1.);
}
</code></pre>
<p>And enjoy the result! Here’s the <a href="https://gist.github.com/phaazon/268d5c0285c6c7cba90d5cea1b99db76">gist</a>
that contains the whole <code>main.rs</code>.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 14 Sep 2017 00:00:00 GMT</pubDate></item><item><title>Release of glsl-quasiquote-0.2</title><link>https://strongly-typed-thoughts.net/blog/glsl-quasiquote-0.2</link><description><![CDATA[<p><a href="https://crates.io/crates/glsl-quasiquote/0.2.0">glsl-quasiquote-0.2</a> was released early this morning. This new version provides a more stable
public API. Two major changes:</p>
<ul>
<li>The <code>glsl_str!</code> proc-macro would have only survived the <code>0.1</code> version. It’s now deprecated and
will be removed soon.</li>
<li>The <code>glsl!</code> proc-macro now supports GLSL pragmas (both <code>#version</code> and <code>#extension</code>).</li>
</ul>
<blockquote>
<p><em>“Wait. GLSL pragmas? You <a href="https://phaazon.net/blog/glsl-quasiquoting#enter-the-nice-solution-quasiquoting">said it wasn’t possible</a>
because of how the Rust tokenizer thinks newlines are meaningless while the GLSL grammar uses them
to separate pragmas from other pragmas and code.”</em></p>
</blockquote>
<p>Yes, at the time of writing <code>glsl-quasiquote</code>, I just thought that since <a href="https://doc.rust-lang.org/proc_macro/struct.TokenStream.html"><code>TokenStream</code></a> is about a
stream of Rust tokens, GLSL’s pragmas would be impossible to encode with this mechanism. In fact,
it’s actually impossible per-se: newlines are not Rust tokens and are just completely ignored by the
tokenizer… or are they?</p>
<h1>Enter the motivation</h1>
<p>At the announcement of the release of <code>glsl-quasiquote</code> on Reddit, someone – actually,
<a href="https://www.reddit.com/r/rust/comments/9lo9s4/glsl_quasiquoting_in_rust/e78nf85">somebodddy</a> just
pointed out that I could use Rust attributes to encode GLSL pragmas. That would enable me to have
them as regular Rust tokens at the cost of modifying very, very briefly the GLSL grammar for the
pragmas – nothing bad, really. Their idea, which I liked, was this:</p>
<p>Instead of:</p>
<pre><code>#version 330 core
</code></pre>
<p>We could write:</p>
<pre><code>#![version 330 core]
</code></pre>
<p>Such a small change yet effective, right? I was appealed by the idea, so I implemented it. The code
was a bit messy because I did all the token pattern-matching by hand but I sketched something up
that worked in a few minutes. After having a discussion with a friend on IRC (antoyo!), I realized
that I was plain wrong from the beginning assertion that newlines are ignored by the Rust tokenizer:
they’re not.</p>
<h1>Enter the hack</h1>
<p>The <a href="https://doc.rust-lang.org/proc_macro/index.html"><code>proc_macro</code></a> crate, which is used to manipulate Rust tokens, has several items that I used
to implement the quasiquoter:</p>
<ul>
<li><code>TokenStream</code>, a stream of tokens. This is a very opaque type, you can just create an empty one,
display it as a <code>String</code> and marshall to and from an iterator interface, which element type is
<code>TokenTree</code>.</li>
<li><code>TokenTree</code>, a single token or a delimited sequence of token trees, like a group of tokens
logically united (with brackets, parens, etc.).</li>
<li>All the types used in <code>TokenTree</code> variants, such as <code>Literal</code>, <code>Punct</code>, <code>Group</code>, etc.</li>
</ul>
<p>There’s something I just had completely forgotten when claiming that newlines were ignored: pretty
much all of the tokens contained in <code>TokenTree</code> have an associated <a href="https://doc.rust-lang.org/proc_macro/struct.Span.html"><code>Span</code></a>. I’m not sure what the
initial intent was about with this type, but in order to use it, I had to add the <code>proc_macro_span</code>
feature. That type gives positional information along with macro expansion on a token. Specifically,
it has a method that gives the line and column at which the token starts and ends.</p>
<p>This information gives us the line on which a token lays. That’s it. We have the newlines! A newline
is just whenever a token following another token has a different line in its span. That’s as easy as
it gets.</p>
<h2>The real fix</h2>
<p>So instead of implementing GLSL pragmas via Rust attributes, I decided to implement them the way
they are defined in the spec and the <a href="https://crates.io/crates/glsl">glsl</a> crate: plain GLSL. The idea was just to match a <code>#</code> and
accumulate tokens in a collection as long as tokens lay on the same line. As soon as the line
changes, we have the full pragma and we can call <code>TokenStream::from_iter</code>.</p>
<p>The current implementation expects the pragmas to be at the top part of the quasiquoted GLSL code.
You shouldn’t be trying to put the <code>#version</code> at the bottom of your file, but I wanted you to be
noticed: don’t do that!</p>
<p>Since <a href="https://crates.io/crates/glsl-quasiquote/0.2.0">glsl-quasiquote-0.2</a>, this Rust code now completely compiles and generates, at compile-time,
a GLSL AST:</p>
<pre><code>let ast = glsl!{
  #version 330 core
  #extension GL_ARB_separate_shader_objects : require

  void main() {
  }
};
</code></pre>
<h1>Future announcements about glsl and glsl-quasiquote</h1>
<p>Some future announcements to come (things that I’ve already been working on and that I might release
soon):</p>
<ul>
<li>The quasiquoter will have interpolation at some time.</li>
<li><a href="https://crates.io/crates/glsl">glsl</a> is about to be released with an experimental SPIR-V transpiler. The current
implementation uses <a href="https://crates.io/crates/shaderc">shaderc</a> as backend in order to test how everything goes.</li>
</ul>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 22 Oct 2018 02:00:00 GMT</pubDate></item><item><title>A more faithful Display for proc-macro token types</title><link>https://strongly-typed-thoughts.net/blog/proc-macro-faithful-display</link><description><![CDATA[<blockquote>
<p>This article serves as an announcement to a new crate: <a href="https://crates.io/crates/proc-macro-faithful-display">proc-macro-faithful-display</a>.</p>
</blockquote>
<h1>Enhancing faith in displaying Rust tokens</h1>
<h2>The problem</h2>
<p>I’ve been around procedural macros in Rust for a few weeks now – in order to write my
<a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a> crate. Quickly, I discovered that most types cannot be inspected. For instance,
an <a href="https://doc.rust-lang.org/stable/proc_macro/struct.Ident.html"><code>Ident</code></a> has no methods to get the underlying identifier (as a <code>&amp;str</code> for instance). The only
way to do so is to allocate a <code>String</code>, for instance, via a <code>Display</code> use, as in:</p>
<pre><code>let ident_str = format!("{}", ident);
</code></pre>
<p>Now, imagine you have a fully formatted stream of GLSL tokens flowing in.
<a href="/blog/glsl-quasiquote-0.2">I’ve already blogged</a>
lately about the fact Rust has some specific rules that will prevent you to write <em>preprocessor
pragmas</em>. The only way to authorize them was to implement a hack (explained in the blog article).</p>
<p>Lately, I was working on some not-so-trivial glsl transformations in another project when I hit a
parse error. I was surprised, because the <a href="https://crates.io/crates/glsl">glsl</a> crate contains more than 130 unit tests about
parsing. So I thought <em>“Well, okay, it’s alright, I might have forgotten a very specific case not
covered in my nom implementation. I’ll just get a small reproducible sample and use it as a
new integration test in <a href="https://crates.io/crates/glsl">glsl</a>”</em>. That kind of reasoning is very important to me: if you find a
bug, first thing to do (if you plan to fix it) is to write a unit test <strong>that fails first.</strong>
This is very important because when the test succeeds, you have solved your problem. If someone
provides a contribution later and that the test breaks again, you will have just shielded the
project against a test regression. Seriously, I know it’s boring but: <strong>write tests.</strong></p>
<p>So I wrote a test, a
<a href="https://github.com/phaazon/glsl/blob/ec7be696a53e3b7b12ff6ccf72c4fcf3465a1840/glsl/tests/missing_zero_float.rs#L7">very simple one</a>
(this is actually an integration test, since I’ve found the error in a project using <a href="https://crates.io/crates/glsl">glsl</a>). And…
the test passed. I was so surprised: <em>“So that code breaks in my crate, but succeeds in the
integration test suite?! Well, wait. That means that the parser is actually correct. The problem
might come from the way the parser is used… or the input.”</em></p>
<p>So my second reaction was to have a look at where the parser was called. It was in a <code>glsl!</code>
procedural macro invocation – in my project, I hardcoded a <em>vertex shader</em> with <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a>.
And then, I came to the realization and remembered. <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a>, in order to parse the
input Rust token, has to use <code>Display</code> on them, in order to yield a <code>String</code> usable by the <a href="https://crates.io/crates/glsl">glsl</a>
parser. The parser is okay – i.e. the integration test passes – but the input string… loses
information.</p>
<p>If you have taken a look at the integration test just above, you should have noticed an interesting
construction:</p>
<pre><code>void main() {
  float x = 1. * .5;
}
</code></pre>
<p>This is not a <em>semantically</em> valid vertex shader, but it should parse. And it does parse. But if
you write that vertex shader with <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a>, what you actually write is this:</p>
<pre><code>let vertex_shader = glsl!{
  void main() {
    float x = 1. * .5;
  }
};
</code></pre>
<p>The Rust tokenizer will eat the tokens and the <code>Display</code> implementation will render the stream
by using spaces whenever it sees fit. It will not respect the initial whitespaces. It might yield
something like this:</p>
<pre><code>void main ( ) { float x = 1. * . 5 ; }
</code></pre>
<p>Look closely: our <code>.5</code> GLSL floating point constant value (it equals <code>0.5</code>) has become <code>.</code> and <code>5</code>.
<strong>rustc</strong> has transformed it into two tokens instead of a single one. This is due to the fact that
this float format is not allowed in Rust and is interpreted the same way a method call would be.</p>
<h2>The solution</h2>
<p>So, after realizing that, I just went for a documentation addition stating that numbers cannot be
expressed this way, and that you have to use a leading <code>0</code>. I really disliked writing that
documentation exception but I didn’t have any solution to that.</p>
<p>A few days later, I was thinking about <a href="https://crates.io/crates/glsl">glsl</a> variable interpolation and got a mind click. In order
to implement preprocessor pragmas, I used a trick with <a href="https://doc.rust-lang.org/stable/proc_macro/struct.Span.html"><code>Span</code></a>, that gave me positional information
of tokens. And I came to the realization that I could use the same kind of trick to implement a
function that would yield an object implementing <code>Display</code> by respecting the initial
layout / formatting!</p>
<p>Basically, a trait – <a href="https://docs.rs/proc-macro-faithful-display/0.1.0/proc_macro_faithful_display/trait.FaithfulDisplay.html"><code>FaithfulDisplay</code></a> – is implemented for all flavours of Rust tokens:</p>
<ul>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.Ident.html"><code>Ident</code></a>.</li>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.Literal.html"><code>Literal</code></a>.</li>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.Punct.html"><code>Punct</code></a>.</li>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.Group.html"><code>Group</code></a>.</li>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.TokenTree.html"><code>TokenTree</code></a>.</li>
<li><a href="https://doc.rust-lang.org/stable/proc_macro/struct.TokenStream.html"><code>TokenStream</code></a>.</li>
</ul>
<p>All the implementations carefuly carry around the previous <a href="https://doc.rust-lang.org/stable/proc_macro/struct.Span.html"><code>Span</code></a> in order to <em>pad</em> enough spaces
and newlines before they get formatted into the <code>Formatter</code>. This allows to reconstruct the input
layout.</p>
<p>All this code is packaged in the <a href="https://crates.io/crates/proc-macro-faithful-display">proc-macro-faithful-display</a> crate. You can find the
documentation <a href="https://docs.rs/proc-macro-faithful-display">here</a>. I use it – and tested against –
the crate with <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a>. It especially allowed me to remove all the preprocessor trick
because now newlines are taken into account.</p>
<h2>Is this viable?</h2>
<p>Honestly, I have no idea. Currently, at the time of writing, it works like a charm. If you find any
bug or weird behavior, please <a href="https://github.com/phaazon/glsl/issues">open an issue here</a>.
However, <a href="https://doc.rust-lang.org/stable/proc_macro"><code>proc-macro</code></a> hasn’t yet been stabilized and the <a href="https://doc.rust-lang.org/stable/proc_macro/struct.Span.html"><code>Span</code></a> type is accessible through a
<em>feature gate</em>. All of this might change. However, I strongly think that:</p>
<ul>
<li>Either Rust will keep that <a href="https://doc.rust-lang.org/stable/proc_macro/struct.Span.html"><code>Span</code></a> type because it is really helpful (its first purpose was to    provide nice error reporting).</li>
<li>Either they’ll ditch it, but since quasiquoting is important and wanted (have a look at
<a href="https://github.com/bodil">@bodil</a>’s awesome <a href="https://github.com/bodil/typed-html">typed-html</a> crate for instance), something to replace it might come next.</li>
</ul>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 01 Dec 2018 19:51:00 GMT</pubDate></item><item><title>Porting a Haskell graphics framework to Rust (luminance)</title><link>https://strongly-typed-thoughts.net/blog/luminance_rust</link><description><![CDATA[<p>I wanted to write that new article to discuss about something important I’ve been doing for several
weeks. It’s actually been <em>a month</em> that I’ve been working on <em>luminance</em>, but not in the usual
way. Yeah, I’ve put my <em>Haskell</em> experience aside to… port <em>luminance</em> into <em>Rust</em>! There are
numerous reasons why I decided to jump in and I think it could be interesting for people to know
about the differences I’ve been facing while porting the graphics library.</p>
<h1>You said Rust?</h1>
<p>Yeah, <a href="https://www.rust-lang.org">Rust</a>. It’s a strong and static language aiming at system
programming. Although it’s an <em>imperative</em> language, it has interesting <em>functional</em> conventions
that caught my attention. Because I’m a <em>haskeller</em> and because <em>Rust</em> takes <strong>a lot</strong> from
<em>Haskell</em>, learning it was a piece of cake, even though there are a few concepts I needed a few days
to wrap my mind around. Having a strong C++11/14 experience, it wasn’t that hard though.</p>
<h2>How does it compare to Haskell?</h2>
<p>The first thing that amazed me is the fact that it’s actually not that different from <em>Haskell</em>!
<em>Rust</em> has a powerful type system – not as good as <em>Haskell</em>’s but still – and uses immutability as
a default semantic for bindings, which is great. For instance, the following is forbidden in <em>Rust</em>
and would make <code>rustc</code> – the <em>Rust</em> compiler – freak out:</p>
<pre><code class="language-rust">let a = "foo";
a = "bar"; // wrong; forbidden
</code></pre>
<p><em>Haskell</em> works like that as well. However, you can introduce mutation with the <code>mut</code> keyword:</p>
<pre><code class="language-rust">let mut a = "foo";
a = "bar"; // ok
</code></pre>
<p>Mutation should be used only when needed. In <em>Haskell</em>, we have the <code>ST</code> monad, used to introduce
local mutation, or more drastically the <code>IO</code> monad. Under the wood, those two monads are actually
almost the same type – with different warranties though.</p>
<p><em>Rust</em> is strict by default while <em>Haskell</em> is lazy. That means that <em>Rust</em> doesn’t know the concept
of <em>memory suspensions</em>, or <em>thunks</em> – even though you can create them by hand if you want to. Thus,
some algorithms are easier to implement in <em>Haskell</em> thanks to laziness, but some others will
destroy your memory if you’re not careful enough – that’s a very common problem in <em>Haskell</em> due to
thunks piling up in your stack / heap / whatever as you do extensive lazy computations. While it’s
possible to remove those thunks by optimizing a <em>Haskell</em> program – profiling, strictness, etc.,
<em>Rust</em> doesn’t have that problem because it gives you full access to the memory. And that’s a good
thing <strong>if you need it</strong>. <em>Rust</em> exposes <strong>a lot</strong> of primitives to work with memory. In contrast
with <em>Haskell</em>, it doesn’t have a <em>garbage collector</em>, so you have to handle memory on your own. Well,
not really. <em>Rust</em> has several very interesting concepts to handle memory in a very nice way. For
instance, objects’ memory is held by <em>scopes</em> – which have <em>lifetimes</em>.
<a href="https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> is a very well known
use of that concept and is important in <em>Rust</em>. You can glue code to your type that will be ran when
an instance of that type dies, so that you can clean up memory and scarce resources.</p>
<p><em>Rust</em> has the concept of <em>lifetimes</em>, used to give names to scopes and specify how long an object
reference should live. This is very powerful yet a bit complex to understand in the first place.</p>
<p>I won’t go into comparing the two languages because it would require several articles and a lot of
spare time I don’t really have. I’ll stick to what I’d like to tell you: the <em>Rust</em> implementation
of <em>luminance</em>.</p>
<h1>Porting luminance from Haskell to Rust</h1>
<p>The first very interesting aspect of that port is the fact that it originated from a realization
while refactoring some of my <em>luminance</em> <em>Haskell</em> code. Although it’s functional, stateless and
type-safe, a typical use of <em>luminance</em> doesn’t <em>really</em> require laziness nor a garbage collector.
And I don’t like using a tool – read language – like a bazooka. <em>Haskell</em> is the most powerful
language ever in terms of abstraction and expressivity over speed ratio, but all of that power comes
with an overhead. Even though you’ll find folks around stating that <em>Haskell</em> is pretty okay to code
a video game, I think it will never compete with languages that are <strong>made</strong> to solve real time
computations or reactive programming. And don’t get me wrong: I’m sure you can write a decent video
game in <em>Haskell</em> – I qualify myself as a <em>Haskeller</em> and I’ve not been writing <em>luminance</em> just for
the joy of writing it. However, the way I use <em>Haskell</em> with <em>luminance</em> shouldn’t require all the
overhead – and profiling got me right, almost no GC was involved.</p>
<p>So… I looked into <em>Rust</em> and discovered and learned the language in only three days. I think it’s due
to the fact that <em>Rust</em>, which is simpler than <em>Haskell</em> in terms of type system features and has
almost everything taken from <em>Haskell</em>, is, to me, an <strong>imperative Haskell</strong>. It’s like having a
<em>Haskell</em> minus a few abstraction tools – HKT (but they’ll come soon), GADTs, fundeps, kinds,
constraints, etc. – plus a total control of what’s happening. And I like that. A lot. A <em>fucking</em>
lot.</p>
<p>Porting <em>luminance</em> to <em>Rust</em> wasn’t hard as a <em>Haskell</em> codebase might map almost directly to
<em>Rust</em>. I had to change a few things – for instance, <em>Rust</em> doesn’t have the concept of existential
quantification as-is, which is used intensively in the <em>Haskell</em> version of <em>luminance</em>. But most
<em>Haskell</em> modules map directly to their respective <em>Rust</em> modules. I changed the architecture of
the files to have something clearer. I was working on <em>loose coupling</em> in <em>Haskell</em> for <em>luminance</em>.
So I decided to directly introduce loose coupling into the <em>Rust</em> version. And it works like a
charm.</p>
<p>So there are, currently, two packages available: <code>luminance</code>, which is the core API, exporting the
whole general interface, and <code>luminance-gl</code>, an <strong>OpenGL 3.3</strong> backend – though it will contain more
backends as the development goes on. The idea is that you need both the dependencies to have access
to <em>luminance</em>’s features.</p>
<p>I won’t say much today because I’m working on a <a href="https://en.wikipedia.org/wiki/Demoscene">demoscene</a>
production using <em>luminance</em>. I want it to be a proof that the framework is usable, works and acts
as a first true example. Of course, the code will be open-source.</p>
<p>The documentation is not complete yet but I put some effort documenting almost everything. You’ll
find both the packages here:</p>
<p><a href="https://crates.io/crates/luminance">luminance-0.1.0</a></p>
<p><a href="https://crates.io/crates/luminance-gl">luminance-gl-0.1.0</a></p>
<p>I’ll write another article on how to use <em>luminance</em> as soon as possible!</p>
<p>Keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 29 Apr 2016 00:00:00 GMT</pubDate></item><item><title>Development Environments</title><link>https://strongly-typed-thoughts.net/blog/development-environments</link><description><![CDATA[<p>It’s 2022. Today, developing a library, an application or a service is a totally different experience from what it used
to be only a few years ago. Every professional engineer or programmer is expected to know tools like <code>git</code>, a coding
editor, a terminal to run various commands, like compiling, testing, etc. You might also know a lot of people (if you
are not one already) using modern editors and IDEs, such as <strong>VS Code</strong>, <strong>IntelliJ</strong>, etc.</p>
<p>I belong to the category of people who enjoy <em>composing</em> tools. That is, I don’t use an IDE: I make my own IDE by
combining several tools I like. In the end, we all use “IDEs” (we all need some kind of <em>development environment</em>).
For the sake of generality, I will call those environments “DE” for <em>development environments</em>.</p>
<p>The tools I use are, basically:</p>
<ul>
<li>A terminal. Lately, I’ve been enjoying <a href="https://sw.kovidgoyal.net/kitty">kitty</a> a lot, for it allows me to do <em>almost</em> everything I do with <a href="https://github.com/tmux/tmux">tmux</a>
(read below).</li>
<li>A shell. I’m using <a href="https://www.zsh.org">zsh</a> mostly but I’ve been playing with others, such as <a href="https://www.nushell.sh">nushell</a>. I’m dissatisfied with <a href="https://www.zsh.org">zsh</a>
because I think it’s way more too complicated (I barely use 10% of its features).</li>
<li>A text editor. I’ve been using lots lately, but mainly, <a href="https://www.vim.org">vim</a> / <a href="https://neovim.io">neovim</a>.</li>
<li>A git client. I simply use the <a href="https://git-scm.com">git</a> CLI (<em>Command Line Interface</em>) program, but also <a href="https://github.com/tpope/vim-fugitive">fugitive</a> in <a href="https://neovim.io">neovim</a>.</li>
<li>A couple of other programs, such as <a href="https://github.com/junegunn/fzf">fzf</a>, CLI of various package managers and compilers, etc.</li>
<li>I have plugins in <a href="https://neovim.io">neovim</a> for note taking and task management, but I’ve also been using <a href="https://www.notion.so">Notion</a> at work lately to
try it out.</li>
<li>I used to use <a href="https://github.com/tmux/tmux">tmux</a> a lot but I’m moving away from it, so I’m basically left with <a href="https://sw.kovidgoyal.net/kitty">kitty</a>’s own way to do the same
thing.</li>
</ul>
<p>The more that I think about all the things I use and all the things people enjoying modern environments use, the more I
feel I try to “force” a workflow that used to be the right one a couple of years ago, but not necessarily today. I’m the
kind of person who <em>constantly</em> put things into perspective and try to balance my decisions regarding the time I have,
the budget (for work) and the knowledge. A couple of months / years ago, I spent a lot of time on <a href="https://www.gnu.org/software/emacs">emacs</a> and realized
that I needed to review my perspective, because even though <a href="https://www.gnu.org/software/emacs">emacs</a> is super old, it’s fascinating how bleeding edge it
still feels (and I can’t even imagine how bleeding edge it was twenty years ago!).</p>
<p>I want to use this blog article to ask questions and not necessarily answer them, but discuss about them. The questions
are about “how we should be developing in the modern era” and is not about “is <a href="https://www.gnu.org/software/emacs">emacs</a> better than <a href="https://neovim.io">neovim</a>” or that
kind of useless debates.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#is-tooling-composition-that-any-good">Is tooling composition that any good?</a>
<ul>
<li><a href="#the-frozen-world">The frozen world</a></li>
<li><a href="#the-nightmare-that-is-composition-iteration">The nightmare that is composition iteration</a></li>
<li><a href="#a-small-word-on-tuis-the-land-of-the-unicode-hacks">A small word on TUIs, the land of the unicode hacks</a></li>
<li><a href="#summary-of-the-problems">Summary of the problems</a></li>
</ul>
</li>
<li><a href="#what-would-be-the-next-perfect-productivity-platform-in-2022">What would be the (next) perfect productivity platform in 2022?</a>
<ul>
<li><a href="#the-terminal-needs-to-change">The terminal needs to change</a></li>
<li><a href="#editing-needs-to-come-back-to-editing">Editing needs to come back to editing</a></li>
<li><a href="#applications-need-to-compose-in-a-new-modern-way">Applications need to compose in a new, modern way</a></li>
<li><a href="#applications-need-to-change">Applications need to change</a></li>
</ul>
</li>
</ul>
<!-- vim-markdown-toc -->
<h1>Is tooling composition that any good?</h1>
<p>The first question I’m wondering is that whether using several tools and composing them is actually good in 2022. For
instance, the best example I have in mind is <a href="https://kubernetes.io">Kubernetes</a> and <a href="https://github.com/junegunn/fzf">fzf</a>. At work, I have a workflow where I use <a href="https://kubernetes.io">Kubernetes</a>
and filter/select its output via <a href="https://github.com/junegunn/fzf">fzf</a> to run other commands. I do the same with <a href="https://git-scm.com">git</a>, btw. I have some scripts where I
can do something like:</p>
<pre><code class="language-sh">gr -s # equivalent to git branch | fzf | git rebase origin --auto-stash
gs # equivalent to git branch | fzf | git switch
gm # equivalent to git branch | fzf | git merge
kubectx # switch via FZF k8s clusters (kubectl command)
kubens # same but with k8s namespaces
# etc. etc.
</code></pre>
<h2>The frozen world</h2>
<p>All of that seems to be pretty good. We can leverage the power of small tools such as <code>git</code>, <code>kubectl</code> and <code>fzf</code> and
compose them. Shell pipes allow to connect applications together, reading and writing from and to <code>stdin</code> and <code>stdout</code>.</p>
<p>The “main platform”, the “DE” is then the terminal and the shell, and building a DE requires two main ingredients:</p>
<ul>
<li>A set of programs, each of them doing <em>one thing right</em>. <code>fzf</code> is excellent at building a list of items and let the
user pick one of them, for instance. <code>kubectl</code> allows to operate Kubernetes resources one operation by one operation.</li>
<li>Some “glue” to combine the results of the programs. This is typically the shell, with its pipes and output pushed to
the terminal.</li>
</ul>
<p>However, I think that even though all of that seems pretty great, it suffers from a major drawback: everything is text
and everything is static. See, all of the programs I mentioned here are CLI, not TUI (<em>Text User Interface</em>). That is,
the UX is the following:</p>
<ol>
<li>The user enters a command in their terminal, starting with the program name, passing arguments.</li>
<li>The program runs, might have side effects on remote resources, etc.</li>
<li>The program finishes and displays its results.</li>
</ol>
<p>That way of working has been a default in the industry for decades. If the program printed more than one terminal page,
you will have to scroll to find your information back up. Once you find the information you need, how do you pick that
information? If you are using a terminal that doesn’t support hinting, you will have to use your mouse (which completely
defeats the purpose of using CLI / TUI applications to me) or the keyboard-base navigation system of your terminal to
make selections, which is, let’s be honest, really poor in most terminal. If, like me, you use a terminal supporting
hinting (i.e. being able to enter a mode that will allow you to visually select parts of the screen by simply typing a
few characters — similar to my <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a> plugin, but for the terminal), you will be able to copy parts of the screen,
but that’s all. If a program returned a JSON array, it will cost you a lot of keystrokes to reduce the list and, for
instance, use an item in the array as input of another <code>curl</code> request, for instance.</p>
<p>The <a href="https://www.nushell.sh">nushell</a> has a very interesting concept to solve that kind of problem. Programs write lines of text as output, but
they can also write <em>cells</em>, which is a <a href="https://www.nushell.sh">nushell</a> encoding that allows it to “speak the same language.” It comes with
some built-in programs such as <code>open</code> that will be able to recognize a lot of formats (JSON, XML, YAML, TOML, etc.) and
convert them into <em>cells</em>, allowing to display them in the terminal in a very convenient and “smart” way. However, it
doesn’t change the next problem: composition iteration.</p>
<h2>The nightmare that is composition iteration</h2>
<p>I think pretty much every programmer out there can relate to the following: you will oftentimes run a command, see its
result, then run the command again, piping it to a filter command or sort or field extractor like [jq]. If the output is
too long, it’s likely you will output into a file and will work on that file (which is basically a cached version of
your command output ;) ).</p>
<p>Iterating on a “session” is a pretty bad experience in a terminal to me. To my knowledge, it would be quite easy to fix
by simply allowing to automatically cache the result of all commands, and get access to it via a shell variable (such as
what we have with <code>$?</code> for the last command status, for instance). Some hacks exist to <strong>recompute</strong> the last command
(i.e. <code>$(!!)</code>) but it will still re-run the last command.</p>
<p>I thought <a href="https://www.nushell.sh">nushell</a> supported that, because the variable support and Nu language are pretty excellent, but to my
surprise, that shell doesn’t have that. Maybe it’s a technical challenge, but I really doubt it. We work with computers
with at least 8 GB of RAM these days and most of us have between 16 and 32 GB of RAM. I don’t think it would be that
hard to support.</p>
<p>But even if we had that caching… it would still require us to run a command, look at a static text, run a filter command
on it, look at the new output that would be appended to the screen… it doesn’t seem super nice in terms of UX to me. And
imagine having to change the second or third command in the pipe list while you already have six pipes. Duh. The
problem, to me, is that the terminal application scope is being overloaded. The CLI is great when we want to run a
command and expect a <em>report</em>. As soon as we need to work with <em>data</em>, I think the CLI is already lacking features.</p>
<h2>A small word on TUIs, the land of the unicode hacks</h2>
<p>And now, let’s talk about TUIs. As a lot of people, I’m using TUIs, so don’t get what I’m about to say twisted. I use
<a href="https://neovim.io">neovim</a> as my daily editor driver, and so far, I haven’t really found a way out and use something else. However, it’s
not because nothing better exists that what we use is perfect (far from that).</p>
<p>I have always despised TUIs. I think the idea, which dates from a while, was a fun and interesting idea back then, when
graphical elements and graphics kits were not mature and not a thing yet. But today, we know we can have graphical
elements that align at the pixel level. We know that we can have real vector images in applications. We can use the GPU
to draw arbitrary shapes and some terminals (text-based only!) use the GPU to render glyphs (text), and then people use
TUIs that will use the glyphs to mimic graphical elements! That hitches me!</p>
<p>I dislike TUIs because they are sub-optimal versions of GUIs. And here, I think a lot of people make a confusion with
UX and UI. A GUI can be beautiful, rich of graphical elements, and have a terrible UX (it’s the case most of the time).
People writing GUIs tend to focus on mouse-driven interactions… which doesn’t have to be. So people tend to associate
GUIs with mouse-only workflows, while a GUI can completely do everything a TUI does… but it can <em>also</em> do much better.</p>
<p>A couple of arguments against TUIs:</p>
<ul>
<li>Graphical elements are faked by using unicode symbols (drawing blocks), which implies having all the elements laid out
on a display cell grid. Oftentimes, the unicode symbols will not be correctly aligned, resulting in graphical
glitches, or simply ugly pop-ups borders, etc.</li>
<li>Also, still about unicode glyphs, if the application doesn’t implement it correctly, that text is just “part of the
text of the application”, so copy-pasting can be challenging and very boring.</li>
<li>Since everything has to be put on a display cell grid, you have no way to have pixel-based movements, alignment or
anything based on pixel. The smallest unit, the basic primitive, is a unicode character. About that, I think the most
interesting “fake” thing is <a href="https://github.com/wfxr/minimap.vim">wfxr/minimap.vim</a>, which is a minimap for <a href="https://neovim.io">neovim</a>
in a terminal. And you can’t do any better than that, because, well, you can’t go any smaller / different than a
glyph.</li>
<li>Applications run in a terminal, which drives the performance of the TUI application. That results in non-deterministic
experiences. Basically, you can write a super great application that will run like an elder snail on some terminal
emulators.</li>
</ul>
<h2>Summary of the problems</h2>
<p>So let’s do a quick recap of what’s wrong with today terminals:</p>
<ul>
<li>TUIs are not rich enough in terms of graphical experience, and everything graphical (splits, lines, pop-ups, etc.) are
somehow faked.</li>
<li>CLI-based composition often ends up being a nightmare of running the same command over and over by adding more piped
commands.</li>
<li>A terminal is a static world, where (besides TUIs) everything is just static text and nothing changes unless you run a
command again, wiping the previous result and replacing it with the new one.</li>
</ul>
<p>Those are the three main pain points I feel about using a terminal. Now, you might already have used something like a
monitor application (I work at <a href="https://www.datadoghq.com">DataDog</a>, so I’m used to them) or used a web browser or native GUI and see that you can
have animations, drop-down lists that actually look good, etc. Putting all that in contrast with terminals makes me a
bit sad, especially in 2022.</p>
<h1>What would be the (next) perfect productivity platform in 2022?</h1>
<p>My latest blog articles are centered around that topic:</p>
<ul>
<li><a href="https://phaazon.net/blog/editors-in-2020">My thoughts about editors in 2020</a></li>
<li><a href="https://phaazon.net/blog/editors-in-2021">My thoughts about editors in (early) 2021</a></li>
</ul>
<p>So it’s been an important topic to me lately. I have been thinking about productivity platforms for a while now, and the
conclusion I came to is as follows.</p>
<h2>The terminal needs to change</h2>
<p>Instead of switching to an IDE like <strong>IntelliJ</strong> or <strong>VS Code</strong>, I still think we need composition, but not in the form
of pipe shells. I think the CLI has to evolve. I have started a project on my spare-time to try and change how programs
would work with their environment. See, a shell is basically just a way to call the <code>exec</code> syscall (<code>execve</code> on Linux
for instance) and set its arguments, environment and standard input / output (plus all the candies you can get from a
shell, such as scripting languages, etc.).</p>
<p>The interpretation of the inputs and outputs of a program is actually completely left to… the running process, which is
most of the time the shell for CLI applications. This is why <a href="https://www.nushell.sh">nushell</a> is such an interesting new shell: it can
interpret <em>more</em> than just lines of text. And I think we need to move in a direction similar to this.</p>
<p>I think we should see more alternative forms of “terminals”. We could even imagine a program that doesn’t have the
concept of “shell” nor “terminal” anymore. It’s easy to imagine having an application that can simply call <code>execve</code> and
do whatever it wants the environment, inputs and outputs. It could have some primitives implemented. For instance,
imagine that a program could be run in your “new terminal” and output its results in a tree view, while other programs
are also running, displaying their own things. You could have a program run without exiting, updating the content of the
tree view or even accepting filters, sorts, field extractor or whatever see fit.</p>
<h2>Editing needs to come back to editing</h2>
<p>Today, text editors are overloaded. Heck, even small editors such as <a href="https://neovim.io">neovim</a> now have a community developing plugins
that have nothing to do with editing. For instance, fuzzy searching files or Git management. When you think about it, if
the platform (i.e. terminal) had better primitives, it should be possible to compose programs in a way that doesn’t
require people to rewrite the same kind of logic (i.e. fuzzy finders) in all the programs they use. <a href="https://github.com/junegunn/fzf">fzf</a> is a good
example of that, but it’s limited to the CLI (you can still use it in TUI but to me it’s a bit hacky). Terminals should
have a primitive to, for instance, run a program in overlay and pass the output to another, running program. That would
allow people to write “pure generic fuzzy finders” once and for all, and let people use the editors they like.</p>
<p>Today, all that logic doesn’t really exist outside of individual and scoped efforts. Yes, you can probably find programs
supporting that, but they will certainly not run in a terminal and will have their own protocols.</p>
<h2>Applications need to compose in a new, modern way</h2>
<p>Composing applications with pipes is easy to wrap your fingers around, but it gets limited and limiting as soon as
you use something like a GUI. Imagine that programs could speak a common language, based on, for instance, local
sockets, to exchange messages in a way that whatever the kind of programs you write, you can still compose them.
Composing two GUIs between each other should be something that is widely supported, and standardized.</p>
<p>Something else with piping applications: it’s limited to <em>how we use CLI applications</em>, which is via the command line.
Running programs in the command line takes the form of:</p>
<pre><code class="language-sh">program &lt;arg1&gt; &lt;arg2&gt; … | another_program &lt;arg1&gt; &lt;arg2&gt;
</code></pre>
<p>This is highly summarized, but you get the idea. CLI programs don’t provide a great discoverability experience, and it’s
harder to iterate on them. For instance, in a GUI, if you filter a list with a predicate, it’s super easy to
“experiment around” by changing the filter name. In a CLI session with piped programs, you are likely required to use
your up arrow on your keyboard (or whatever shortcut) to invoke the last command again, and change the arguments in the
middle of the line. While doing this, you will still have to run all intermediary commands, which are going to perform
side-effects, etc. etc. And if you want to be smarter and use the <code>&gt;</code> or <code>&gt;&gt;</code> operator to cache the results, then you
will still have to remember the name of the file you used, and <code>cat</code> or <code>tee</code> that file again. It gets time to get used
to using that, and I truly think that it’s not really worth it, because today I’m 30 and I’ve been developing since I’m
11, and I still don’t really enjoy that kind of process.</p>
<h2>Applications need to change</h2>
<p>The essence of an application, today, is actually platform-dependent. Heck, it’s even environment dependent. At work,
an “application” can be as simple as a small Python script or be a <a href="https://kubernetes.io">Kubernetes</a> cluster composed of several deployments,
stateful sets and DNS rules. However, when we think about applications we use on a daily basis (what people would call
“heavy” or “native” applications), they are pretty similar to the following:</p>
<ul>
<li>For CLI applications, they are akin to effectful computations. They take inputs (environment, <code>stdin</code>, arguments),
they output something (<code>stdout</code>) and they can have side-effects (calling another program, changing shared memory,
doing stuff on your file system, various kinds of I/O, etc.).</li>
<li>For TUI applications, it’s similar but they don’t read from <code>stdin</code> nor output to <code>stdout</code> in a one-shot way. Instead,
it’s like an <em>event loop</em> that reads from <code>stdin</code> and a <em>render loop</em> that writes to <code>stdout</code>.</li>
<li>For GUI, it depends on the platform but most of the time, GUIs will be closer to the hardware, because they are not
restricted by the protocol they are written for (i.e. CLIs / TUIs cannot really go any lower <code>stdin</code> and <code>stdout</code> in
terms of UX; GUIs can). A GUI application can decide to use <code>OpenGL</code>, <code>Vulkan</code> or something higher such as <code>Gtk</code>,
<code>Qt</code>, or a Web kit.</li>
</ul>
<p>I think that we should change that. Remember that running an application is just a matter of calling <code>execve</code> (or
similar on non-Linux), which basically requires a path to the application, the list of arguments (the <code>argv</code>) and the
environment (<code>envp</code>). Setting the environment is typically done by the shell (and some variables come from the terminal
too), but it could be done completely differently. On the same idea, the <code>argv</code> is filled by the CLI arguments you
provide, but they could be computed and filled in a completely different way. And finally, the interpretation of
<code>stdout</code> (mostly done by the terminal) could be interpreted in a completely different way as well.</p>
<p>Imagine that we could write to <code>stdout</code> the port / socket on which we would like to open a communication with a new,
modern protocol, that would allow us to manipulate a new kind of “terminal.” Instead of using a regular text-based
terminal, it would be a piece of software allowing to manipulate pop-ups, text buffers, image / vector image buffers,
notification queues, etc. etc.</p>
<p>Obviously, I’m not inventing all that. If you know about <a href="https://www.gnu.org/software/emacs">emacs</a>, you know that that’s basically what they do, but
instead of using <code>stdin</code>, <code>stdout</code> or <code>execve</code>, they just evaluate Elisp. However, what I think could be very
interesting would be to allow native applications to do the same thing, with a native platform that would ship without
any real applications (so unlike <a href="https://www.gnu.org/software/emacs">emacs</a>), and let applications implement their own workflow.</p>
<p>That’s basically what I’ve been working on and experimenting lately. A platform that provides buffers, tree views,
notifications, pop-pups, overlays, etc. to allow any kind of applications that can read and write to sockets to use
those features as if they were native, and platform-independent, in the same way that you use <code>stdout</code> without even
thinking how it will be displayed in the terminal.</p>
<p>Leveraging such a platform would solve most of the problems I mentioned above, because:</p>
<ul>
<li>We could write a code / text editor that wouldn’t have to be worried about extensions like Git: the Git support would
come as a native application (equivalent of the CLI <code>git</code> application) that would open an overlay, for instance. That
application wouldn’t even know about the code editor: you would make that connection via the platform.</li>
<li>Applications  could communicate between each other to manipulate buffers, get the contents of the buffer and do
various kind of  operations on them (LSP servers, for instance).</li>
<li>Because the protocol would be completely rendering-agnostic, that would allow people to write an application that
would work for any kind of “platform implementations.”</li>
<li>Composition would use the low-level protocol to compose applications. You could imagine supporting an interface between
applications allowing to filter the contents of the buffer of one by using another application. Applications such as
<a href="https://github.com/junegunn/fzf">fzf</a> could <em>branch</em> on the buffers of a text editor, for instance. Or you could even open a buffer in the text editor
by running a fuzzy finder as another application. All of that would compose in a seamless way.</li>
<li>Etc. etc.</li>
</ul>
<p>Eh, of course, I know that such a platform protocol will probably never appear unless I make it, very opinionated and
subject to never be adopted by anyone else but me (or a small group of enthusiasts about that kind of ideas). Maybe this
blog article would have given you some ideas or hindsight about your own tools. Or maybe someone knows such a platform
(that I don’t) and will make me realize it already exists, or is a <em>work in progress</em>. Maybe, after all, it will just
launch a discussion about what <em>developing</em> means in 2022. Because to me, using tools that used to be efficient and
proved to be great ones a couple of years ago, doesn’t mean they are still a thing today. And I really mean it, because
I’m currently writing this very article in <a href="https://neovim.io">neovim</a>, in a <a href="https://sw.kovidgoyal.net/kitty">kitty</a> tab among dozens, and I just wish I had the DE I just
described above.</p>
<p>Keep the vibe!</p>
<blockquote>
<p><a href="https://www.reddit.com/r/programming/comments/vlp76z/development_environments_discussion_about">Reddit discussion</a></p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 26 Jun 2022 15:01:00 GMT</pubDate></item><item><title>Emulating RFC 1598, less or… more?!</title><link>https://strongly-typed-thoughts.net/blog/hkt-1598</link><description><![CDATA[<p>Lately, I’ve been working on a huge feature that’s coming to <a href="https://crates.io/crates/luminance">luminance</a> soon. While working on
it, I’ve been facing several design problems I think are interesting to talk about.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#the-context">The context</a></li>
<li><a href="#the-problem">The problem</a></li>
<li><a href="#faking-higher-kinded-types">Faking higher-kinded types</a>
<ul>
<li><a href="#a-simple-implementor">A simple implementor</a></li>
<li><a href="#polymorphic-use">Polymorphic use</a></li>
</ul>
</li>
<li><a href="#rationale">Rationale</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>The context</h1>
<p>Imagine you want to expose a trait that defines an interface. People know about the interface and
can use provided types to switch the implementation. That’s a typical use case of traits.</p>
<pre><code class="language-rust">// This trait is pub so that users can see and use it
pub trait System {
  type Err;

  fn do_something(&amp;mut self) -&gt; Result&lt;(), Self::Err&gt;;
}
</code></pre>
<p>You can have a type <code>SimpleSystem</code> that implements <code>System</code> and that will do something special in
<code>do_something</code>, as well as a type <code>ComplexSystem</code> doing something completely different.</p>
<p>Now imagine that your <code>System</code> trait needs to expose a function that returns an object that must be
typed. What it means is that such an object’s type is tagged with another type. Imagine a
type <code>Event</code> that is typed with the type of event it represents:</p>
<pre><code class="language-rust">pub struct Event&lt;T&gt; {
  code: i64,
  msg: String,
  // …
}
</code></pre>
<p>That type must be provided and implemented by systems, not by the actual interface code. How would
we do this?</p>
<p>Furthermore, we might want another trait to restrict what <code>T</code> can be, but it’s off topic for our
current problem here.</p>
<h1>The problem</h1>
<p>Let’s try a naive implementation first:</p>
<pre><code class="language-rust">pub trait System {
  type Event;

  type Err;

  fn do_something(&amp;mut self) -&gt; Result&lt;(), Self::Err&gt;;

  fn emit_event(&amp;mut self, event: Self::Event);
}
</code></pre>
<p>That implementation allows <code>SimpleSystem</code> to implement <code>Event</code> as a single type and then
implement <code>emit_event</code>, taking its <code>Event</code> type. However, that event is not typed as we
wanted to. We want <code>Event&lt;T&gt;</code>, not <code>Event</code>.</p>
<p>However, Rust doesn’t authorize that per-se. The following is currently illegal in Rust:</p>
<pre><code class="language-rust">pub trait System {
  type Event&lt;T&gt;;

  // …

  fn emit_event&lt;T&gt;(&amp;mut self, event: Self::Event&lt;T&gt;);
}
</code></pre>
<blockquote>
<p><a href="https://github.com/rust-lang/rfcs/pull/1598">RFC 1598</a> is ongoing and will allow that, but until then, we need to come up with a solution.</p>
</blockquote>
<p>The problem is that associated types, in Rust, are completely monomorphized when the <code>trait</code> impl
is monomorphized, which is, for instance, not the case for trait’s functions. <code>emit_event</code> will
not have its <code>F</code> type variable substituted when the implementor is monomorphized — it will be sank
to a type when it’s called. So what do we really want to express with our <code>Event&lt;T&gt;</code> type?</p>
<pre><code class="language-rust">pub trait System&lt;EventType&gt; {
  type Event;

  fn emit_event(&amp;mut self, event: Self::Event);
}
</code></pre>
<p>That would work but that’s not <em>exactly</em> the same thing as our previous trait. The rest of the
trait doesn’t really depend on <code>EventType</code>, so we would duplicate code every time we want to support
a new type of event. Meh.</p>
<h1>Faking higher-kinded types</h1>
<p><code>Event&lt;T&gt;</code> is a higher-kinded type (HKT). <code>Event</code>, here, is what we call a <em>type constructor</em> — as
opposed to <em>data constuctor</em>, like associated <code>new</code> functions. <code>Event</code> takes a type and returns a
type, in the type-system world.</p>
<p>As I said earlier, Rust doesn’t have such a construct yet. However, we can emulate it with a nice
hack I came across while trying to simulate kinds.</p>
<p>See, when a HKT has its type(s) variable(s) substituted, it becomes a regular, monomorphized type. A
<code>Vec&lt;T&gt;</code>, when considered as <code>Vec&lt;u32&gt;</code>, is just a simple type the same way <code>u32</code> is one. The key is
to decompose our trait into two distinct yet entangled interfaces:</p>
<ul>
<li>One that will work with the monomorphized version <em>after</em> the trait is monomorphized.</li>
<li>One that will introduce polymorphism via a method, and not an associated type.</li>
</ul>
<p>The first trait is implemented on systems and means “[a type] knows how to handle an event of a
given type.” The second trait is implemented on systems, too, and means “can handle all events.”</p>
<pre><code class="language-rust">pub trait SystemEvent&lt;T&gt;: System {
  type Event;

  fn emit_system_event(&amp;mut self, event: Self::Event) -&gt; Result&lt;(), Self::Err&gt;;
}

pub trait System {
  type Err;

  fn do_something(&amp;mut self) -&gt; Result&lt;(), Self::Err&gt;;

  fn emit_event&lt;T&gt;(
    &amp;mut self,
    event: &lt;Self as SystemEvent&lt;T&gt;&gt;::Event
  ) -&gt; Result&lt;(), Self::Err&gt;
  where Self: SystemEvent&lt;T&gt; {
    &lt;Self as SystemEvent&lt;T&gt;&gt;::emit_system_event(self, event)
  }
}
</code></pre>
<p>The idea is that the <code>SystemEvent&lt;T&gt;</code> trait must be implemented by a system to be able to emit
events of type <code>T</code> inside the system itself. Because the <code>Event</code> type is associated in
<code>SystemEvent&lt;T&gt;</code>, it is the monomorphized version of the polymorphic type in the actual
implementation of the trait, and is provided by the implementation.</p>
<p>Then, <code>System::emit_event</code> can now have a <code>T</code> type variable representing that <code>Event&lt;T&gt;</code> we
wanted. We use a special type-system candy of Rust here: <code>Self: Trait</code>, which allows us to state
that in order to use that <code>emit_event&lt;T&gt;</code> function, the implementor of <code>System</code> must also
satisfy <code>SystemEvent&lt;T&gt;</code>. Even better: because we already have the implementation of
<code>emit_system_event</code>, we can blanket-implement <code>emit_event&lt;T&gt;</code>!</p>
<p>Several important things to notice here:</p>
<ul>
<li>First, a system <em>can</em> now implement several traits:
<ul>
<li><code>System</code>, to work as a system, obviously.</li>
<li><code>SystemEvent&lt;T&gt;</code>, to handle events of type <code>T</code>. The actual type is defined by the system itself.</li>
</ul>
</li>
<li>Second, a system doesn’t have to implement <code>SystemEvent&lt;T&gt;</code> if it doesn’t know how to handle
events of type <code>T</code>. That opt-in property is interesting for us as it allows systems to implement
only what they support without having to make our design bleed to all systems. <em>Neat</em>.</li>
<li>The usage of <code>Self: Trait</code> allows to introduce a form of polymorphism that is not available without
it.</li>
<li>Because we can blanket-implement <code>emit_event</code>, the implementor of <code>System</code> doesn’t even have to
implement <code>emit_event</code>.</li>
<li><code>SystemEvent&lt;T&gt;</code> has <code>System</code> has super-trait to share its <code>Err</code> type.</li>
</ul>
<p>The last point is the <em>hack key</em>. Without <code>Self: Trait</code>, it would be way harder or more convoluted
to achieve the same result.</p>
<h2>A simple implementor</h2>
<p>Let’s see a simple implementor that will just print out the event it gets and does nothing in
<code>do_something</code>.</p>
<pre><code class="language-rust">struct Simple;

#[derive(Debug)]
struct ForwardEvent&lt;T&gt;(pub T);

impl System for Simple {
  type Err = ();

  fn do_something(&amp;mut self) -&gt; Result&lt;(), Self::Err&gt; {
    Ok(())
  }
}

impl&lt;T&gt; SystemEvent&lt;T&gt; for Simple where T: std::fmt::Debug {
  type Event = ForwardEvent&lt;T&gt;;

  fn emit_system_event(&amp;mut self, event: Self::Event) -&gt; Result&lt;(), Self::Err&gt; {
    println!("emit: {:?}", event.0);
    Ok(())
  }
}
</code></pre>
<p>As you can see, it’s really simple and straight-forward. We can select which events we want to be
able to handle: in our case, anything that implements <code>Debug</code>.</p>
<p>Let’s use it:</p>
<pre><code class="language-rust">fn main() {
  let mut system = Simple;
  system.emit_event(ForwardEvent("Hello, world!"));
  system.emit_event(ForwardEvent(123));
}
</code></pre>
<h2>Polymorphic use</h2>
<p>The idea is that, given a type <code>S: System</code>, we might want to emit some events without knowing the
implementation. To make things even more crunchy, let’s say we want the error type to be a
<code>()</code>.</p>
<p>Again, it’s quite simple to do:</p>
<pre><code class="language-rust">// we need this to forward the event from the outer world into the system so that we don’t have to
// know the actual type of event used by the implementation
impl&lt;T&gt; From&lt;T&gt; for ForwardEvent&lt;T&gt; {
  fn from(t: T) -&gt; Self {
    ForwardEvent(t)
  }
}

fn emit_specific_event&lt;S, E&gt;(
  system: &amp;mut S,
  event: E
) -&gt; Result&lt;(), S::Err&gt;
where
  S: System&lt;Err = ()&gt; + SystemEvent&lt;E&gt;,
  S::Event: From&lt;E&gt;,
{
  system.emit_event(event.into())
}

fn main() {
  let mut system = Simple;
  system.emit_event(ForwardEvent("Hello, world!"));
  emit_specific_event(&amp;mut system, "Hello, world!");
}
</code></pre>
<p>We could event change the signature of <code>System::emit_event</code> to add that <code>From&lt;E&gt;</code> constraint to
make the first call easier, but I’ll leave you with that. The important aspect of this code snippet
is the fact that the implementor will handle a type of event <code>Event&lt;T&gt;</code> while the interface uses
<code>T</code> directly. We have injected a HKT <code>Event</code>.</p>
<h1>Rationale</h1>
<p>Why do I care about such a design? It might seem complex and hard, but it’s actually a very useful
use of a typeclass / trait type system — especially in <a href="https://crates.io/crates/luminance">luminance</a>, where I use type-system
concepts <strong>a lot</strong>; after all, it’s based on its Haskell version! If you compare my solution to the
next-to-come HKT proposal from <a href="https://github.com/rust-lang/rfcs/pull/1598">RFC 1598</a>, we have:</p>
<ul>
<li><a href="https://github.com/rust-lang/rfcs/pull/1598">RFC 1598</a> will bring the syntax <code>type Event&lt;T&gt;;</code> as associated types, which will allow to remove
our <code>SystemEvent&lt;T&gt;</code> trait.</li>
<li>With that RFC, the type will have to be <em>total</em>. What it means is that the <code>T</code> here is the same
for all implementors: an implementor cannot restrict <code>T</code>, it must be implemented for all of them.
If you want to restrict the type of events your type can receive, if I understand the RFC
correctly, you’re handcuffed. That RFC will authorize the <em>author of the trait</em> to restrict your
<code>T</code>, not the <em>author of the implementor</em>. That might have some very useful advantages, too.</li>
<li>With my solution, the <em>author of the trait</em> can still restrict <code>T</code> by applying constraints on
<code>emit_event</code>.</li>
</ul>
<h1>Conclusion</h1>
<p>In the quest of emulating HKT, I’ve found myself with a <em>type-system toy</em> I like using a lot:
<em>equality constraints</em> and self-constraints (I don’t know how those <code>Self: Trait</code> should be named).
In Haskell, since we don’t implement a typeclass <em>for a type</em> but we provide <em>an instance for a
typeclass</em>, things are slightly different. Haskell doesn’t have a <code>Self</code> type alias, since a
typeclasses can be implemented with several types variables (i.e. with the <code>MultiParamTypeClasses</code>
and <code>FlexibleInstances</code> GHC extensions), only <em>equality constraints</em> are needed.</p>
<p>In the end, Rust continues to prove that even though it’s a systems programming language, I can
express lots of powerful abstractions I miss (<em>a lot</em>) from Haskell with <em>a bit more</em> noise. I think
the tradeoff is worth it.</p>
<p>I still haven’t completely made up my mind about GAT / <a href="https://github.com/rust-lang/rfcs/pull/1598">RFC 1598</a> (for sure I’m among the ones who
want it on stable ASAP but I’m yet to figure out <em>exactly</em> how it’s going to change my codebases).</p>
<p>As always, have fun, don’t drink and drive, use condoms, keep the vibes and don’t use impl Trait in
argument position. Have fun!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 25 Nov 2019 15:50:00 GMT</pubDate></item><item><title>Useful Hop in 2022</title><link>https://strongly-typed-thoughts.net/blog/hop-2022</link><description><![CDATA[<p>Lately, I have received a PR on <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a> where the committer wanted to implement
a feature… that was already there. I realized that even with a great documentation, a wiki, a changelog, official
announcements, people still don’t read the documentation / feature list and implement things. The problem is that I have
to reject their contributions and they lose time (and I do as well, because I have to review contributors’ code).</p>
<p>So here’s a quick article I can link in the super fresh <a href="https://this-week-in-neovim.org/">TWiN</a>, website I released a
couple of days ago and that I hope will help with that kind of issues.</p>
<h1>What can you do with Hop</h1>
<p>Hop has been around for some time now. I created it back in February 2021, so roughly one year and a half. It has
changed and evolved quite a lot ever since. New hint algorithm, new commands, a <em>very big</em> number of new options and
features. I really recommand people to read the embedded documentation of Hop. Yes, completely. It took me time to write
in a way that it will take you a couple of minutes only to read. So yes, do read it. Get the habit to read the embedded
documentation of the plugins you install, especially after new updates. Or read <a href="https://this-week-in-neovim.org/">TWiN</a> from now on 😁.</p>
<h2>The wiki, the wiki, the wiki!</h2>
<p>Yes, Hop has a wiki. It’s available <a href="https://github.com/phaazon/hop.nvim/wiki">here</a> and you should read a couple of
pages from it:</p>
<ul>
<li><a href="https://github.com/phaazon/hop.nvim/wiki/Commands">The “default” commands</a>.</li>
<li><a href="https://github.com/phaazon/hop.nvim/wiki/Advanced-Hop">Mastering advanced Hop</a>. This part contains a couple of
examples showing you how to use the various Hop Lua functions and options to create a really advanced motion platform.</li>
<li><a href="https://github.com/phaazon/hop.nvim/wiki/Configuration">All the configuration options</a>.</li>
</ul>
<p>Because I know some people will not click on those links and will still open PRs trying to implement features that are
already there, here is a non-exhaustive list of advanced things you can already do, out of the box, with Hop.</p>
<h2>Recreate <code>f</code>, <code>F</code>, <code>t</code> and <code>T</code></h2>
<p>It’s in the wiki and super simple. For that, you just need three options:</p>
<ul>
<li><code>direction</code>, which must be set with <code>require'hop'.HintDirection.BEFORE_CURSOR</code> or
<code>require'hop'.HintDirection.AFTER_CURSOR</code>.</li>
<li><code>current_line_only</code>, a boolean.</li>
<li><code>hint_offset</code>, a super power allowing you to offset the actual jump off the jump target by a given number of
characters.</li>
</ul>
<p>So <code>f</code> is basically:</p>
<pre><code class="language-lua">require'hop'.hint_char1({
  direction = require'hop.hint'.HintDirection.AFTER_CURSOR,
  current_line_only = true
})
</code></pre>
<p><code>F</code> is then:</p>
<pre><code class="language-lua">require'hop'.hint_char1({
  direction = require'hop.hint'.HintDirection.BEFORE_CURSOR,
  current_line_only = true
})
</code></pre>
<p>And in the same idea, <code>t</code> is:</p>
<pre><code class="language-lua">require'hop'.hint_char1({
  direction = require'hop.hint'.HintDirection.AFTER_CURSOR,
  current_line_only = true,
  hint_offset = -1
})
</code></pre>
<p>And <code>T</code>:</p>
<pre><code class="language-lua">require'hop'.hint_char1({
  direction = require'hop.hint'.HintDirection.BEFORE_CURSOR,
  current_line_only = true,
  hint_offset = -1
})
</code></pre>
<h2>Jump at the end of things</h2>
<p>Hop already can already understand the concept of <em>beginning</em> and <em>end</em> of a jump target. It does not make sense with
all of them, but for most, it can. That is implemented with the <code>hint_position</code> option, which has to be one of:</p>
<ul>
<li><code>require'hop.hint'.HintPosition.BEGIN</code></li>
<li><code>require'hop.hint'.HintPosition.MIDDLE</code></li>
<li><code>require'hop.hint'.HintPosition.END</code></li>
</ul>
<p>So if you want to jump to the <em>end</em> of words:</p>
<pre><code class="language-lua">require'hop'.words({
  hint_position = require'hop.hint'.HintPosition.END,
})
</code></pre>
<p>You can even jump <em>right after</em> the end of a word!</p>
<pre><code class="language-lua">require'hop'.words({
  hint_position = require'hop.hint'.HintPosition.END,
  hint_offset = 1
})
</code></pre>
<h2>Callbacks!</h2>
<p>You can even use Hop to do something with the jump target that doesn’t imply jumping, like running a Lua function with
the actual column and line where the hint is. It’s done with <code>require'hop'.hint_with_callback</code>.</p>
<p>Here, no example but an exercise for you: try to use <code>require'hop'.hint_with_callback</code> to send a notification (via
<code>vim.notify</code>) displaying the jump target chosen by the user by hinting words. Tips: you will need to find the jump
target generator for words.</p>
<h2>Final recommendation</h2>
<p>Please, feel free to read <code>:h hop</code>. It contains everything I just explained and so much more, like Hop extensions, multi
windows, etc. Have fun hopping around!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 18 Jul 2022 14:26:00 GMT</pubDate></item><item><title>luminance redesign; Part 1: compatible vertex types</title><link>https://strongly-typed-thoughts.net/blog/2022-luminance-redesign-part-1</link><description><![CDATA[<p>Lately, I have decided to take a deep look at <a href="https://crates.io/crates/luminance">luminance</a>. Issues, current code, frustration I had using it, feedback
from people using it, etc. I realized two things:</p>
<ol>
<li>The first commit of <a href="https://crates.io/crates/luminance">luminance</a> was made in 2016, as I migrated it away from its
<a href="https://hackage.haskell.org/package/luminance">Haskell version</a>, which first commit was made in 2015. Since then, my
knowledge of programming, both Haskell and Rust, but also in general has enhanced quite a lot.</li>
<li>The current design has some flaws, both in terms of usability and maintainability.</li>
</ol>
<p>About the first point, I had a look at code that barely changed since I wrote it in 2016, and I’m not completely
satisfied with it anymore. Lots of things happened ever since: I have a sharper vision of how things should be done,
Rust itself has changed quite a lot (back in 2016, Rust 1.0 was just released!), we have more type system features that,
back then, I was lacking, etc.</p>
<p>About the second point, some issues in the current version of <a href="https://crates.io/crates/luminance">luminance</a> (<code>v0.47</code> as writing this blog article) are
complex to solve, such as the <a href="https://github.com/phaazon/luminance-rs/issues/304">Drop problem</a>. Other parts of the API
are insanely complex, like the
<a href="https://docs.rs/luminance/0.47.0/luminance/tess/struct.TessBuilder.html#impl-5"><code>TessBuilder</code></a> related code.</p>
<p>I want all that to change, so I started working on a completely new graphics crate, just to see what kind of interface I
could come up with. And I came up with something much, much easier. So I faced a dilemma: should I release that other
crate and give up on <a href="https://crates.io/crates/luminance">luminance</a>… or should I just port all the new knowledge and ideas from that crate into
<a href="https://crates.io/crates/luminance">luminance</a>?</p>
<p>Well, I have decided to keep <a href="https://crates.io/crates/luminance">luminance</a> around and update it. It is going to take time, because <a href="https://crates.io/crates/luminance">luminance</a> is an
ecosystem of many crates, it has lots of examples (which is great to ensure that its features are still working
correctly once I start redesign it) and because some features are obviously entangled. However, I think it’s worth it,
because even though <a href="https://crates.io/crates/luminance">luminance</a> is not as famous or widely used as <a href="https://crates.io/crates/wgpu">wgpu</a>, it has a special place in the Rust Graphics
community.</p>
<p>So I’m going to blog about the redesign of <a href="https://crates.io/crates/luminance">luminance</a> and it’s going to be a blog article series. I wanted to start
with a very new exciting feature that I have been wanting for a very long time: compatible vertex types.</p>
<h1>The vertex compatibility problem</h1>
<p>When you’re like me and are obsessed with tracking as much information as possible at compile-time, you start imagining
lots of features and ways to <em>prevent</em> people from doing things. Preventing and forbidding is actually much more complex
than allowing something. Think about it like this: assembly allows for everything and is not a complex language. It has
zero constructs, besides opcodes. However, as you add more constructs and abstractions, those abstractions <em>restricts</em>
the application domain; they forbid usage, in a constrained way. There’s a reason why, in Haskell, we use the term
<code>Contraint</code> — which is called, basically, <em>trait bound</em> in Rust.</p>
<p>The way <a href="https://crates.io/crates/luminance">luminance</a> allows a programmer to render objects, which must have a vertex type <code>V</code> that must implement
<code>Vertex</code>. <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> eases the process of implementing lots of traits from <a href="https://crates.io/crates/luminance">luminance</a>. For instance:</p>
<pre><code class="language-rust">#[repr(C)]
#[derive(Clone, Debug, Vertex)] // &lt; notice the Vertex derive here
pub struct MyVertex {
  position: Vector3&lt;f32&gt;,
  normal: Vector3&lt;f32&gt;,
  color: Vector3&lt;u8&gt;,
  weight: Vector3&lt;f32&gt;,
}
</code></pre>
<p>This is great, because the user just has to focus on writing types and just using them. For vertex objects, which can be
imagined as big arrays / buffers of <code>MyVertex</code> here, there are types and functions allowing to basically render such
object, and those render functions expects <code>MyVertex</code>. However, rendering is a multi-stage process. As you may know, the
graphics pipeline contains different steps, and among the first ones, you have a vertex shader, which is responsible in
mapping vertices (to transform them, for instance). In <a href="https://crates.io/crates/luminance">luminance</a>, vertex shaders are strongly-typed with the kind of
vertices they expect. That is to prevent a user from using a vertex shader that expects some vertex attributes that a
vertex object doesn’t have. Imagine that we write a vertex shader that expects a <code>position</code> and a <code>normal</code> but our
vertex object, which uses <code>OtherVertex</code> vertices, only has <code>position</code>. That would result in probably some visual
glitches or just a black screen.</p>
<p>So, we want to have something like this pseudocode:</p>
<pre><code class="language-rust">type VertexShader&lt;V&gt;; // accept only vertex type V
type RenderVertexObjects&lt;W&gt;; // render only vertex type W
</code></pre>
<p>If we want to have a fully typed graphics pipeline, we must have <code>V = W</code>. However, that is going to be very annoying.
Imagine a vertex shader that only needs <code>position</code>, <code>normal</code> and <code>color</code>. Something like:</p>
<pre><code class="language-rust">#[repr(C)]
#[derive(Clone, Debug, Vertex)]
pub struct ShaderVertexInput {
  position: Vector3&lt;f32&gt;,
  normal: Vector3&lt;f32&gt;,
  color: Vector3&lt;u8&gt;,
}
</code></pre>
<p>This is a different type than <code>MyVertex</code>, so if we try to render our <code>MyVertex</code> vertex objects with a vertex shader that
works on <code>ShaderVertexInput</code>, it will get a type mismatch at compile-time. The only way to fix that is to create a copy
of the shader program that works with <code>MyVertex</code> and ignore the <code>weight</code> field. Meh. Waste of resources and duplication.
Everything we don’t want.</p>
<p>When you look at it, the vertex shader is going to use <code>position</code>, <code>normal</code> and <code>color</code>, but it doesn’t have to know
there are other vertex attributes. So it should be able, in theory, to work with <code>MyVertex</code>, since <code>MyVertex</code> has
<code>position</code>, <code>normal</code> and <code>color</code>…</p>
<h1>The solution</h1>
<p>One solution to fix the problem is to introduce a trait, <code>CompatibleVertex</code>, that we will use on the vertex shader code.
The vertex shader still has its <code>ShaderVertexInput</code> type, but it will not require vertex objects to use that type.
Instead, it will require vertex objects to use <code>V</code> and will require <code>ShaderVertexInput: CompatibleVertex&lt;V&gt;</code>. What it
means is that <code>ShaderVertexInput</code> must be <em>included</em> in <code>V</code>. Said otherwise, <code>V</code> must have all of <code>ShaderVertexInput</code>’s
fields (but it can have more).</p>
<pre><code class="language-rust">pub trait CompatibleVertex&lt;V&gt; {}
</code></pre>
<p>Now the big question: how do we implement <code>CompatibleVertex</code> for a given concrete vertex type? Well, in the previous
version of <a href="https://crates.io/crates/luminance">luminance</a>, that used to be implemented manually by users of <a href="https://crates.io/crates/luminance">luminance</a>. It was both unsafe and not really
practical, so I just removed the concept (and <a href="https://crates.io/crates/luminance">luminance</a> in version <code>v0.47</code> and less is subject to the problem I
described above, where you can render a vertex object which vertex type is not compatible with what the shader expects).</p>
<h2>Const generics to the rescue</h2>
<p>Since <code>rustc-1.51</code>, const generics can be used at compile-time. The feature allows to manipulate integers at
compile-time. For instance:</p>
<pre><code class="language-rust">pub struct Array&lt;const N: usize&gt; {
  // …
}
</code></pre>
<p>Here, <code>N</code> is a const generics, and you use it with types like <code>Array&lt;3&gt;</code> or <code>Array&lt;14&gt;</code>. However, that’s currently
(<code>rust-1.63</code>) all you can do with a stable compiler… but we can do much more with a nightly compiler.</p>
<p>There is a feature called <code>adt_const_params</code> that allows using more types, and one type that is very important is
<code>&amp;'static str</code>. Yes, you heard right. Constant strings. We can write something like this:</p>
<pre><code class="language-rust">#![allow(incomplete_features)] // as of rustc-1.65 nightly, this is still required
#![feature(adt_const_params)]

fn hello&lt;const N: &amp;'static str&gt;() {
  println!("Hello {}!", N);
}

fn main() {
  hello::&lt;"world"&gt;();
}
</code></pre>
<p>That doesn’t seem like much, but this small change is going to save so many frustration in <a href="https://crates.io/crates/luminance">luminance</a>, and fix our
compatible vertex problems.</p>
<h2>Applying adt_const_params to CompatibleVertex</h2>
<p>What we basically want for one vertex type to compatible with another (i.e. one is <em>included</em> into the other) is to
ensure that the all the fields of one are present in the other. Let’s recall our vertex type definitions and trait:</p>
<pre><code class="language-rust">#[repr(C)]
#[derive(Clone, Debug, Vertex)]
pub struct MyVertex {
  position: Vector3&lt;f32&gt;,
  normal: Vector3&lt;f32&gt;,
  color: Vector3&lt;u8&gt;,
  weight: Vector3&lt;f32&gt;,
}

#[repr(C)]
#[derive(Clone, Debug, Vertex)]
pub struct ShaderVertexInput {
  position: Vector3&lt;f32&gt;,
  normal: Vector3&lt;f32&gt;,
  color: Vector3&lt;u8&gt;,
}

pub trait CompatibleVertex&lt;V&gt; {}
</code></pre>
<p>We can write a <code>HasField</code> trait like this:</p>
<pre><code class="language-rust">pub trait HasField&lt;const NAME: &amp;'static str&gt; {
  type FieldType;
}
</code></pre>
<p>And now, we can implement that trait for all the fields of our vertex types:</p>
<pre><code class="language-rust">// for MyVertex
impl HasField&lt;"position"&gt; for MyVertex {
  type FieldType = Vector3&lt;f32&gt;;
}

impl HasField&lt;"normal"&gt; for MyVertex {
  type FieldType = Vector3&lt;f32&gt;;
}

impl HasField&lt;"color"&gt; for MyVertex {
  type FieldType = Vector3&lt;u8&gt;;
}

impl HasField&lt;"weight"&gt; for MyVertex {
  type FieldType = f32;
}

// for ShaderInputVertex
impl HasField&lt;"position"&gt; for ShaderInputVertex {
  type FieldType = Vector3&lt;f32&gt;;
}

impl HasField&lt;"normal"&gt; for ShaderInputVertex {
  type FieldType = Vector3&lt;f32&gt;;
}

impl HasField&lt;"color"&gt; for ShaderInputVertex {
  type FieldType = Vector3&lt;u8&gt;;
}
</code></pre>
<p>Now, we can write a more meaningful implementor for <code>CompatibleVertex</code> and <code>ShaderInputVertex</code>:</p>
<pre><code class="language-rust">impl&lt;V&gt; CompatibleVertex&lt;V&gt; for ShaderInputVertex where
  V: HasField&lt;"position", FieldType = Vector3&lt;f32&gt;&gt;
    + HasField&lt;"normal", FieldType = Vector3&lt;f32&gt;&gt;
    + HasField&lt;"color", FieldType = Vector3&lt;u8&gt;&gt;
{
}
</code></pre>
<p>Because <code>MyVertex</code> implements the three <code>HasField</code> trait bounds required, it can be used, as well as
<code>ShaderInputVertex</code>.</p>
<p>Obviously, because <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> is a thing, people will never have to write any of those implementors. Actually,
deriving <code>Vertex</code> will do two things (besides what it already does) for a given vertex type:</p>
<ul>
<li>Provide all the <code>HasField</code> implementors for all of its field.</li>
<li>Provide the universal <code>CompatibleVertex</code> implementor.</li>
</ul>
<p>What it means is that, as soon as you start using <code>#[derive(Vertex)]</code>, you will automatically have compatible vertex
types behind the scenes working for you.</p>
<h1>Conclusion</h1>
<p>That feature is already, by itself, worth requiring a nightly compiler, as it brings the exact kind of type safety I
want for my code. And since I started with const generics strings, the next blog article will be about the redesign of
vertex objects (called <code>Tess</code> in the current version of <a href="https://crates.io/crates/luminance">luminance</a>), since, you might have guessed, it will use const
generics strings to upload vertex data per-attribute, and map / update them.</p>
<p>Thanks for having read, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 06 Sep 2022 11:30:00 GMT</pubDate></item><item><title>OpenGL 3.2 support for luminance!</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.7</link><description><![CDATA[<h1>luminance-0.7</h1>
<p>You can’t even imagine how hard it was to release <a href="http://hackage.haskell.org/package/luminance-0.7">luminance-0.7</a>.
I came accross several difficulties I had to spend a lot of time on but finally, here it is. I made
a lot of changes for that very special release, and I have a lot to say about it!</p>
<h2>Overview</h2>
<p>As for all my projects, I always provide people with a <em>changelog</em>. The 0.7 release is a major
release (read as: it was a major increment). I think it’s good to tell people what’s new, but it
should be <strong>mandatory</strong> to warn them about what has changed so that they can directly jump to their
code and spot the uses of the deprecated / changed interface.</p>
<p>Anyway, you’ll find patch, minor and major changes in luminance-0.7. I’ll describe them in order.</p>
<h2>Patch changes</h2>
<h3>Internal architecture and debugging</h3>
<p>A lot of code was reviewed internally. You don’t have to worry about that. However, there’s a new
cool thing that was added internally. It could have been marked as a minor change but it’s not
<em>supposed</em> to be used by common people – you can use it via a flag if you use <code>cabal</code> or <code>stack</code>
though. It’s about debugging the <em>OpenGL</em> part used in luminance. You shouldn’t have to use it but
it could be interesting if you spot a bug someday. Anyway, you can enable it with the flag
<code>debug-gl</code>.</p>
<h3>Uniform Block / Uniform Buffer Objects</h3>
<p>The <a href="https://www.opengl.org/wiki/Uniform_Buffer_Object">UBO</a> system was buggy and was fixed. You
might experience issue with them though. I spotted a bug and reported it – you can find the bug
report <a href="https://bugs.freedesktop.org/show_bug.cgi?id=92909">here</a>. That bug is not Haskell related
and is related to the i915 Intel driver.</p>
<h2>Minor changes</h2>
<p>The minor changes were the most important part of luminance-0.7. luminance now officially supports
<em>OpenGL 3.2</em>! When installing luminance, you default to the <code>gl32</code> backend. You can select the
backend you want with flags – <code>gl45</code> and <code>gl45-bindless-textures</code> – but keep in mind you need the
appropriate hardware to be able to use it. Because you need to use flags, you won’t be able to
switch to the backend you want at runtime – that’s not the purpose of such a change though.</p>
<p>The performance gap should be minor between <code>gl32</code> and <code>gl45</code> but still. Basically, OpenGL 4.5 adds
the support for <a href="https://www.opengl.org/wiki/Direct_State_Access">DSA</a>, which is very handy and less
ill-designed that previous iterations of OpenGL. So a lot of code had to be rewritten to implement
luminance’s stateless interface without breaking performance nor bring them down.</p>
<p>I <em>might</em> add support for other backends later on – like an <em>OpenGL ES</em> backend and <em>WebGL</em> one –
but that won’t ship that soon though because I have a ton of work to do, and yet need to provide you
with a concrete, beautiful, fast, appealing and eye-blowing demo with luminance! ;)</p>
<p>Feel free to test the <code>gl32</code> backend and give me back feedback!</p>
<p>However, if I spent so much time on that 0.7 version, it’s because I had issue whilst writing the
<code>gl32</code> backend. Indeed, I spotted several bugs on my Intel HD card. This is my OpenGL version string
for my Intel IGP card:</p>
<blockquote>
<p>OpenGL core profile version string: 3.3 (Core Profile) Mesa 11.0.4</p>
</blockquote>
<p>The architecture is Haswell. And on such a card (<code>i915</code> linux driver) I’ve found two bugs while
trying the <code>gl32</code> backend with <a href="https://hackage.haskell.org/package/luminance-samples-0.7">luminance-samples-0.7</a>.</p>
<h3><code>usampler2D</code></h3>
<p>For unknown reason, the <code>Texture</code> sample failed on my Intel IGP but ran smoothly and flawlessly on
my nVidia GPU. I spent a lot of time trying to figure out what I was missing, but eventually changed
the sampler type – it’s now a <code>sampler2D</code> – and… it worked. I reported the issue to the intel dev
team. So if you hit that error too, please leave a message here so that I can have more hindsight
about that error and see what I can do.</p>
<h3>Uniform block and <code>vec3</code></h3>
<p>This is a very nasty issue that kept me awoken for days trying to fix <strong>my</strong> code while it was a
driver bug. It’s a big technical, so I’ll just leave a <a href="https://bugs.freedesktop.org/show_bug.cgi?id=92909">link to the bug tracker</a>
so that you can read it if you want to.</p>
<h2>Breaking changes</h2>
<p>Ok, let’s talk.</p>
<p>When creating a new shader stage, you now have to use the function <code>createStage</code> – instead of
several functions like <code>createVertexShader</code>. That change is important so that I can add new shader
types without changing the interface, and because some shader can fail to be created. For instance,
on the <code>gl32</code> backend, trying to build a tessellation shader will raise an error.</p>
<p>When a shader stage creation fails, the <code>UnsupportedStage</code> error is raised and holds the type of the
stage that failed.</p>
<p>Finally, the interface for the cubemaps changed a bit – you don’t have access to <em>width</em> and
<em>height</em> anymore, that was error-prone and useless; you’re stick to a <em>size</em> parameter.</p>
<p>I’d like to thank all people supporting me and luminance. I’ll be watching reactions to that major
and important release as it will cover more people with cheaper and well-spread GPUs.</p>
<p>Happy hacking! :)</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 13 Nov 2015 00:00:00 GMT</pubDate></item><item><title>Pure API vs. IO-bound API for graphics frameworks</title><link>https://strongly-typed-thoughts.net/blog/pure_vs_io</link><description><![CDATA[<p>Hi! It’s been a while I haven’t posted something here. I haven’t been around for a few weeks and
I’ve been missing writing here. I didn’t work that much on
<a href="https://www.stackage.org/package/luminance">luminance</a> or other projects, even though I still
provided updates for stackage for my packages. I worked a bit on
<a href="https://github.com/phaazon/cheddar">cheddar</a> and I hope to be able to release it soon!</p>
<p>Although I didn’t add things nor write code at home, I thought a lot about graphics API designs.</p>
<h1>Graphics API designs</h1>
<h2>Pure API</h2>
<p>APIs such as <a href="http://www.lambdacube3d.com/">lambdacube</a> or
<a href="https://hackage.haskell.org/package/GPipe">GPipe</a> are known to be graphics <em>pure API</em>. That means
you don’t have use functions bound to <code>IO</code>. You use some kind of
<a href="http://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html">free monads</a> or
an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a> to represent the code that will run on
the target GPU. That pure design brings numerous advantages to us:</p>
<ul>
<li>it’s possible to write the GPU code in a declarative, free and elegant way;</li>
<li>because of not being <code>IO</code>-bound, side-effects are reduced, which is good and improves the code
safety;</li>
<li>one can write GPU code and several interpreters or/and with different technologies, hence a
loosely coupled graphics API;</li>
<li>we could even imagine serializing GPU code to send it through network, store it in a file or
whatever.</li>
</ul>
<p>Those advantages are nice, but there are also drawbacks:</p>
<ul>
<li>acquiring resources might not be explicit anymore and none knows exactly when sparce resources
will be loaded into memory; even though we could use <em>warmup</em> to solve that, <em>warmup</em> doesn’t
help much with dynamic scene where some resources are loaded only if the context enables it (like
the player being in a given area of the map, in a game, for instance);</li>
<li>interfacing! people will have to learn how to use free monads, AST and all that stuff and how to
interface it with their own code (<em>FRP</em>, render loops, etc.);</li>
<li>performance; even though it should be okay, you’ll still hit a performance overhead by using pure
graphics frameworks, because of internals mechanisms used to make it work.</li>
</ul>
<p>So yeah, a pure graphics framework is very neat, but keep in mind there’s – so far – no proof it
actually works, scales nor is usable for a decent high-performance for end-users. It’s the same
dilemna as with Conal’s <em>FRP</em>: it’s nice, but we don’t really know whether it works <em>“at large scale
and in the real world”</em>.</p>
<h2>IO-bound API</h2>
<p>Most of the API out there are IO-bound. <em>OpenGL</em> is a famous C API known to be one of the worst one
on the level of side-effects and global mutations. Trust me, it’s truly wrong. However, the pure
API as mentioned above are based on those impure IO-bound APIs. So we couldn’t do much without them.</p>
<p>There are side effects that are not that bad. For instance, in OpenGL, creating a new buffer is a
side-effect: it requires that the CPU tell the GPU <em>“Hey buddy, please create a buffer with that
data, and please give me back a handle to it!”</em>. Then the GPU would reply <em>”No problem pal,
here’s your handle!”</em>. This side-effect don’t harm anyone, so we shouldn’t worry about it too much.</p>
<p>However, there are nasty side-effects, like binding resources to the OpenGL context.</p>
<p>So what are advantages of IO-bound designs? Well:</p>
<ul>
<li>simple: indeed, you have handles and side-effects and you have a (too) fine control of the
instruction flow;</li>
<li>performance, because <code>IO</code> is the naked real-world monad;</li>
<li>because <code>IO</code> is the high-order kinded type of any application (think of the <code>main</code> function),
an <code>IO</code> API is simple to use in any kind of application;</li>
<li>we can use <code>(MonadIO m) =&gt; m</code> to add extra flexibility and create interesting constraints.</li>
</ul>
<p>And drawbacks:</p>
<ul>
<li><code>IO</code> is very opaque and is not referentially transparent;</li>
<li><code>IO</code> is a dangerous type in which no one has no warranty about what’s going on;</li>
<li>one can fuck up everything if they aren’t careful;</li>
<li>safety is not enforced as in pure code.</li>
</ul>
<h2>What about luminance’s design?</h2>
<p>Since the beginning, luminance has been an API built to be simple, <em>type-safe</em> and <em>stateless</em>.</p>
<p><em>Type-safe</em> means that all objects you use belong to different type sets and cannot be mixed between
each other implicitely – you have to use explicit functions to do so, and it has to be meaningful.
For instance, you cannot create a buffer and state that the returned handle is a texture: the type
system forbids it, while in OpenGL, almost all objects are in the <code>GLuint</code> set. It’s very
confusing and you might end up passing a texture (<code>GLuint</code>) to a function expecting a framebuffer
(<code>GLuint</code>). Pretty bad right?</p>
<p><em>Stateless</em> means that luminance has no state. You don’t have a huge context you have to bind stuff
against to make it work. Everything is stored in the objects you use directly and specific context
operations are translated into a different workflow so that performance are not destroyed – for
instance luminance uses batch rendering so that it performs smart resource bindings.</p>
<p>Lately, I’ve been thinking of all of that. Either turn the API pure or leave it the way it is. I
started to implement a pure API using <em>self-recursion</em>. The idea is actually simple. Imagine this
<code>GPU</code> type and the <code>once</code> function:</p>
<pre><code class="language-haskell">import Control.Arrow ( (***), (&amp;&amp;&amp;) )
import Data.Function ( fix )

newtype GPU f a = GPU { runGPU :: f (a,GPU f a) }

instance (Functor f) =&gt; Functor (GPU f) where
  fmap f = GPU . fmap (f *** fmap f) . runGPU

instance (Applicative f) =&gt; Applicative (GPU f) where
  pure x = fix $ \g -&gt; GPU $ pure (x,g)
  f &lt;*&gt; a = GPU $ (\(f',fn) (a',an) -&gt; (f' a',fn &lt;*&gt; an)) &lt;$&gt; runGPU f &lt;*&gt; runGPU a

instance (Monad m) =&gt; Monad (GPU m) where
  return = pure
  x &gt;&gt;= f = GPU $ runGPU x &gt;&gt;= runGPU . f . fst

once :: (Applicative f) =&gt; f a -&gt; GPU f a
once = GPU . fmap (id &amp;&amp;&amp; pure)
</code></pre>
<p>We can then build pure values that will have a side-effect for resource acquisition and then hold
the same value for ever with the <code>once</code> function:</p>
<pre><code class="language-haskell">let buffer = once createBufferIO

-- later, in IO
(_,buffer2) &lt;- runGPU buffer
</code></pre>
<p>Above, the type of <code>buffer</code> and <code>buffer2</code> is <code>GPU IO Buffer</code>. The first call <code>runGPU buffer</code> will
execute the <code>once</code> function, calling the <code>createBufferIO</code> IO function and will return <code>buffer2</code>,
which just stores a pure <code>Buffer</code>.</p>
<p>Self-recursion is great to implement local states like that and I advise having a look at the
<code>Auto</code> type. You can also read <a href="http://phaazon.blogspot.fr/2015/03/getting-into-netwire.html">my article on netwire</a>, which uses self-recursion a lot.</p>
<p>However, I kinda think that a library should have well defined responsibilities, and building such
a pure interface is not the responsibility of luminance because we can have type-safety and a
stateless API without wrapping everything in that <code>GPU</code> type. I think that if we want such a pure
type, we should add it later on, in a 3D engine or a dedicated framework – and that’s actually what
I do for demoscene purposes in another, ultra secret project. ;)</p>
<p>The cool thing with luminance using <code>MonadIO</code> is the fact that it’s very easy to use in any kind
of type that developpers want to use in their applications. I really don’t like frameworks which
purpose is clearly not flow control that actually enforce flow control and wrapping types! I don’t
want to end up with a <code>Luminance</code> type or <code>LuminanceApplication</code> type. It should be simple to use
and seamless.</p>
<p>I actually start to think that I did too much about that pure API design idea. The most important
part of luminance should be type-safety and statelessness. If one wants a pure API, then they
should use FRP frameworks or write their own stuff – with free monads for instance, and it’s
actually funny to build!</p>
<p>The next big steps for luminance will be to clean the uniform interfaces which is a bit inconsistent
and unfriendly to use with render commands. I’ll let you know.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 18 Feb 2016 00:00:00 GMT</pubDate></item><item><title>Losing our privacy</title><link>https://strongly-typed-thoughts.net/blog/you_are_being_spied_on</link><description><![CDATA[<h1>Big brother of NSA</h1>
<p>On the 19th of March 2015, a law was introduced in France. That law was
entitled <em>“loi du renseignement”</em> and was presented in regard of what happened
in the <em>Charlie Hebdo</em> awful week. The main idea of the law is to put peole on
wire so that terrorist activities could be spotted and dismantled.</p>
<p>That law will be voted and apply on the the 5th of May 2015.</p>
<p>Although such a law sounds great for counter-terrorism, it has several major
issues people should be aware of.</p>
<h2>You’re on wire</h2>
<p>Every people – in France and abroad as well of course – could be on wire. The French
government might have access to whatever you do with your internet connection.
Crypto-analysts working for the goverment will read dozens of thousands of
messages from individuals. They’ll know where you go to and where you go from –
think of your smartphone’s GPS capabilities. They’ll know which pictures you
take, how much time you spend in your bathroom. They’ll even be able to read
your e-mails if they want to.</p>
<p>Of course, most people “just don’t care”. <em>“Why should I care that the
government knows the litter brand I bought to my cat? I don’t give a damned
heck as long as they catch bad people</em>”. That’s a point, but that’s also being
blind. Digital communication is not only about yourself. It’s about people.
It’s about you and your friends. You, and your contacts. You can’t choose for
them whether they’d like people to watch over their shoulders every now and
then. If the government has access to your private data, it has access to your
friends’ and contacts’ data as well. Not giving a damned heck is being selfish.</p>
<h2>You’ll be violated</h2>
<p>Then comes the issue with what the law is. It gives the government the right
to spy on masses. They could even sell the data they collect. As a counter
argument, the “Commission de contrôle des techniques de renseignement” – which
French stands for “Control commission of intelligence techniques” – was
created. That committe is there to watch the government and ensure it doesn’t
go out of control and doesn’t violate people’s rights. The issue with that is
that our prime minister has the right to ignore the committee’s decision. If
the committee says <em>“Wait a minute. You don’t have the right to gather those
information without asking for M. Dupont’s approval”</em>, the prime minister may
answer back <em>“Well, fuck off. I will and you can’t stop me”</em>. And the sad truth
is that… yeah, with that law, the prime minister and their team has the right
to ignore the committee’s decision.</p>
<p>The committee then has no point. It just gives an <strong>opinion</strong>. Not a veto. What
would happen if a terrorist hacked into your computer. Would you go to jail
because the prime minister would have stated you were a terrorist? Damn me if I
know.</p>
<h1>We’re going to lose a right</h1>
<p>French people will lose a very important right: the right to privacy. It’ll be
sacrificied without your opinion for counter-terrorism, giving the government
a power it shouldn’t have. You thought the NSA was/is wrong? Sure it is. But
when the NSA watches over American and worldwide people, it is illegal whereas
when the French government watches over French and worldwide people, it is
legal. That makes the French government way worse than the NSA to me.</p>
<p>I think the first thing I’ll do will be to revoke my French VPS subscription
to move it out of France, in a country in which people still have privacy. Keep
your communication encrypted as much as possible (ssh and so on). And as a bad
joke, don’t leave your camera in your bedroom. You might be spied on by Manuel
Valls while having sex with your girlfriend.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 21 Apr 2015 00:00:00 GMT</pubDate></item><item><title>Asset management in a real time 3D engine in Haskell</title><link>https://strongly-typed-thoughts.net/blog/resource_management</link><description><![CDATA[<h1>Foreword</h1>
<p>In a real time rendering system, it’s not uncommon finding constructs about
<em>assets</em>. One famous construct is the <em>resource manager</em>. A <em>resource manager</em> is
responsible of several tasks, among:</p>
<ul>
<li>providing a simple interface to load objects from disk <em>(1)</em> ;</li>
<li>ensuring we don’t try to load the same object twice <em>(2)</em> ;</li>
<li>resolving dependencies between resources <em>(3)</em>.</li>
</ul>
<p>The first point is obvious, but the two others are less intuitive. <em>(2)</em> is
important when the user <em>might</em> try to load the same object several times – for
instance, a car model, or a character or a weapon. The most known strategy to
prevent such a situation from happening is by using a <em>software cache</em>.</p>
<p>A <em>software cache</em> – let’s just say <em>cache</em> – is an opaque object that loads the
object on the first request, then just returns that object for future same
requests. For instance, consider the following requests and the corresponding
<em>cache</em> behavior:</p>
<ol>
<li>load “wood.png” -&gt; not cached ; loading ; return</li>
<li>load “grass.png” -&gt; not cached ; loading ; return</li>
<li>load “wood.png” -&gt; cached ; return</li>
<li>load “metal.png” -&gt; not cached ; loading ; return</li>
<li>load “metal.png” -&gt; cached ; return</li>
<li>etc.</li>
</ol>
<p>That behavior is very nice because it will spare a lot of computations and
memory space.</p>
<p><em>(3)</em> is about dependencies. For instance, when you load a car model, you might
need to load its textures as well. Well, not really <em>load</em>. Consider the
following:</p>
<ol>
<li>load “car.mesh” -&gt; not cached
1. load “metal_car.png” -&gt; not cached ; loading ; return
2. loading ; return</li>
<li>load “metal_car.png” -&gt; cached ; return</li>
<li>load “other_car.mesh” -&gt; not cached
1. load “metal_car.png” -&gt; cached ; return
2. return</li>
<li>load “car.mesh” -&gt; cached ; return</li>
</ol>
<p>You got the idea. <em>(3)</em> needs <em>(2)</em> to be efficient.</p>
<h2>Possible implementations</h2>
<h3>Singleton</h3>
<p>In imperative languages and especially in those that support template and/or
generics, people tend to implement the cache system with an ugly <em>design
pattern</em> – which is actually an <em>anti design pattern</em> : <em>singleton</em>. Each type
of resource is assigned a manager by using a template parameter, and then if
a manager needs to load a dependency, it just has to reference the corresponding
manager by stating the type in the template parameter :</p>
<pre><code>Model &amp; getResource&lt;Model&gt;(std::string const &amp;name) {
  Texture &amp;dependency = getResource&lt;Texture&gt;(...);
  ...
}
</code></pre>
<p>That way of doing might sound great, but eh, singletons are just global variables
with a unicity constraint. We don’t want that.</p>
<h3>Explicit pure store</h3>
<p>We can use an explicit store object. That is, some kind of <em>map</em>. For instance,
the store that holds <em>textures</em> would have a type like (in Haskell):</p>
<pre><code>textureStore :: Map String Texture
</code></pre>
<p>A model store would have the following type:</p>
<pre><code>modelStore :: Map String Model
</code></pre>
<p>And each stores is assigned a function; <code>loadTexture</code>, <code>loadModel</code>, and so on.</p>
<p>There are several drawbacks if we go that way. First, we have to carry all stores
when using their functions. Some functions might need other stuff in order to
resolve dependencies. Secondly, because of explicit state, we need to manually
accumulate state! A loading function would have such a following type:</p>
<pre><code>loadTexture :: Map String Texture -&gt; String -&gt; m (Texture,Map String Texture)
</code></pre>
<p>That will expose <strong>a lot</strong> of boilerplate to the user, and we don’t want that.</p>
<h3>Implicit pure store</h3>
<p>We can enhance the explicit store by putting it into some kind of context; for
instance, in <code>MonadState</code>. We can then write <code>loadTexture</code> to make it nicer to
use:</p>
<pre><code>loadTexture :: (MonadState (Map String Texture) m,...)
            =&gt; String
            -&gt; m Texture
</code></pre>
<p>There is a problem with that. What happens when we add more types? For instance if
we want to handle textures <strong>and</strong> models? <code>MonadState</code> has a <em>type family
constraint</em> that forbids two instances for the pair <code>s m</code>. The following is not
allowed and will raise a compiler error:</p>
<pre><code>instance MonadState (Map String Texture) MyState where
  ...

instance MonadState (Map String Model) MyState where
  ...
</code></pre>
<p>The solution to that problem is to have the carried state a polymorphic type and
use typeclass constraint to extract and modify the map we want:</p>
<pre><code>class HasMap a s where
  extractMap :: s -&gt; Map String a
  modifyMap :: (Map String a -&gt; Map String a) -&gt; s -&gt; s
</code></pre>
<p>With that, we can do something like this:</p>
<pre><code>loadTexture :: (MonadState s m,HasMap Texture s,...)
            =&gt; String
            -&gt; m Texture

loadModel :: (MonadState s m,HasMap Texture s,HasMap Model s,...)
          =&gt; String
          -&gt; m Model
</code></pre>
<p>However, we hit a new issue here. What are <code>s</code> and <code>m</code>? Well, <code>m</code> doesn’t really
matter. For simplicity, let’s state we’re using a <em>monad transformer</em>; that is,
we use <code>StateT s m</code> as monad.</p>
<p>We still need <code>s</code>. The problem is that <code>s</code> has to be provided by the user. Worse,
they have to implement <strong>all instances</strong> we need so that the loading functions may
work. Less boilerplate than the explicit store solution, but still a lot of
boilerplate. Imagine you provide a type for <code>s</code>, like <code>Cache</code>. Expending the
cache to support new types – like user-defined ones – will be more extra
boilerplate to write.</p>
<h3>Closures</h3>
<p>The solution I use in my engine might not be the perfect solution. It’s not
<a href="https://en.wikipedia.org/wiki/Referential_transparency_(computer_science)">referentially transparent</a>,
an important concept in Haskell. However, Haskell is not designed to be used in
convenient situations only. We’re hitting a problematic situation. We need to make
a compromise between elegance and simplicity.</p>
<p>The solution required the use of <em>closures</em>. If you don’t know what a closure is,
you should check out the <a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)">wikipedia page</a>
for a first shot.</p>
<p>The idea is that our loading functions will perform some <code>IO</code> operations to
load objects. Why not putting the cache <em>directly</em> in that function? We’d have a
function with an opaque and invisible associated state. Consider the following:</p>
<pre><code>type ResourceMap a = Map String a

getTextureManager :: (MonadIO m,...)
                  =&gt; m (String -&gt; m Texture)
getTextureManager = do
  ref &lt;- newIORef empty
  pure $ \name -&gt; do
    -- we can use the ref ResourceMap to insert / lookup value in the map
    -- thanks to the closure!
</code></pre>
<p>That solution is great because now, a manager is just a function. How would you
implement <code>getModelManager</code>? Well:</p>
<pre><code>getModelManager :: (MonadIO m,...)
                =&gt; (String -&gt; m Texture)
                -&gt; m (String -&gt; m Model)
getModelManager loadTexture = ...
</code></pre>
<p>We can then get the loader functions with the following:</p>
<pre><code>loadTexture &lt;- getTextureManager
loadModel &lt;- getModelManager loadTexture
</code></pre>
<p>And you just have to pass those functions around. The cool thing is that you can
wrap them in a type in your library and provide a function that initializes them
all at once – I do that in my engine. Later on, the user can extend the available
managers by providing new functions for new types. In my engine, I provide a few
functions like <code>mkResourceManager</code> that hides the <code>ResourceMap</code>, providing two
functions – one for lookup in the map, one for inserting into the map.</p>
<h1>Conclusion</h1>
<p>I truly believe that my solution is a good compromise between elegance and ease.
It has a lot of advantages:</p>
<ul>
<li>simple to use ;</li>
<li>simple to implement; you just have to play around with closures ;</li>
<li>dependencies resolving is easy to add and hidden once the functions are
generated ;</li>
<li>little runtime overhead (due to closures, might be optimized away by the
compiler though) ;</li>
<li>can be easily extended by the user to support custom/new types ;</li>
<li>if correctly used, implementations can replace <code>IORef</code> with <code>TVar</code> or
similar objects for thread-safe implementations ;</li>
<li>several replicated functions mean several stores (can be useful in certain
cases).</li>
</ul>
<p>The huge drawback I see in that solution is its opacity. There’s also no way to
query the state of each cache. Such a feature could be added by proving a new
function, for instance. Same thing for deletion.</p>
<p>I’m one of those Haskellers who love purity. I try to keep my code the purest I
can, but there are exceptions, and that cache problem fits that kind of exception.</p>
<p>Feel free to comment, and as always, keep the vibe and happy hacking!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 16 Jun 2015 00:00:00 GMT</pubDate></item><item><title>Introducing Luminance, a safer OpenGL API</title><link>https://strongly-typed-thoughts.net/blog/introducing_luminance</link><description><![CDATA[<p>A few weeks ago, I was writing <strong>Haskell</strong> lines for a project I had been
working on for a very long time. That project was a 3D engine. There are several
posts about it on my blog, feel free to check out.</p>
<p>The thing is… Times change. The more it passes, the more I become mature in what
I do in the <strong>Haskell</strong> community. I’m a demoscener, and I need to be
productive. Writing a whole 3D engine for such a purpose is a good thing, but I
was going round and round in circles, changing the whole architecture every now
and then. I couldn’t make my mind and help it. So I decided to stop working on
that, and move on.</p>
<p>If you are a <strong>Haskell</strong> developer, you might already know
<a href="https://www.fpcomplete.com/user/edwardk?show=all">Edward Kmett</a>. Each talk with
him is always interesting and I always end up with new ideas and new knowledge.
Sometimes, we talk about graphics, and sometimes, he tells me that writing a
3D engine from scratch and release it to the community is not a very good move.</p>
<p>I’ve been thinking about that, and in the end, I agree with Edward. There’re two
reasons making such a project hard and not interesting for a community:</p>
<ol>
<li>a good “3D engine” is a specialized one – for FPS games, for simulations,
for sport games, for animation, etc. If we know what the player will do, we
can optimize a lot of stuff, and put less details into not-important part
of the visuals. For instance, some games don’t really care about skies, so
they can use simple skyboxes with nice textures to bring a nice touch of
atmosphere, without destroying performance. In a game like a flight
simulator, skyboxes have to be avoided to go with other techniques to provide
a correct experience to players. Even though an engine could provide both
techniques, apply that problem to almost everything – i.e. space partitionning
for instance – and you end up with a nightmare to code ;</li>
<li>an engine can be a very bloated piece of software – because of point <em>1</em>.
It’s very hard to keep an engine up to date regarding technologies, and
make every one happy, especially if the engine targets a large audience of
people – i.e. <a href="http://hackage.haskell.org">hackage</a>.</li>
</ol>
<p>Point <em>2</em> might be strange to you, but that’s often the case. Building a
flexible 3D engine is a very hard and non-trivial task. Because of point <em>1</em>,
you utterly need to restrict things in order to get the required level of
performance or design. There are people out there – especially in the demoscene
world – who can build up 3D engines quickly. But keep in mind those engines are
limited to demoscene applications, and enhancing them
to support something else is not a trivial task. In the end, you might end up
with a lot of bloated code you’ll eventually zap later on to build something
different for another purpose – eh, demoscene is about going dirty, right?! ;)</p>
<h1>Basics</h1>
<p>So… Let’s go back to the basics. In order to include everyone, we need to
provide something that everyone can download, install, learn and use. Something
like <a href="https://www.opengl.org/sdk">OpenGL</a>. For <strong>Haskell</strong>, I highly recommend
using <a href="https://hackage.haskell.org/package/gl">gl</a>. It’s built against the
<code>gl.xml</code> file – released by Khronos. If you need sound, you can use the
complementary library I wrote, using the same name convention,
<a href="https://hackage.haskell.org/package/al">al</a>.</p>
<p>The problem with that is the fact that <strong>OpenGL</strong> is a low-level API. Especially
for new comers or people who need to get things done quickly. The part that
bothers – wait, no, <em>annoys</em> – me the most is the fact that <strong>OpenGL</strong> is a very
old library which was designed two decades ago. And we suffer from that. A lot.</p>
<p><strong>OpenGL</strong> is a <em>stateful</em> graphics library. That means it maintains a <em>state</em>, a
<em>context</em>, in order to work properly. Maintaining a context or state is a legit
need, don’t get it twisted. However, if the design of the API doesn’t fit such a
way of dealing with the state, we come accross a lot of problems. Is there <em>one</em>
programmer who hasn’t experienced black screens yet? I don’t think so.</p>
<p>The <strong>OpenGL</strong>’s API exposes a lot of functions that perform <em>side-effects</em>.
Because <strong>OpenGL</strong> is weakly typed – almost all objects you can create in
<strong>OpenGL</strong> share the same <code>GL(u)int</code> type, which is very wrong – you might end
up doing nasty things. Worse, it uses an internal binding system to <em>select</em> the
objects you want to operate on. For instance, if you want to upload data to a
texture object, you need to <em>bind</em> the texture before calling the texture upload
function. If you don’t, well, that’s bad for you. There’s no way to verify code
safety at compile-time.</p>
<p>You’re not convinced yet? <strong>OpenGL</strong> doesn’t tell you directly how to change
things on the GPU side. For instance, do you think you have to <em>bind</em> your vertex
buffer before performing a render, or is it sufficient to <em>bind</em> the <em>vertex array
object</em> only? All those questions don’t have direct answers, and you’ll need to
dig in several wikis and forums to get your answers – the answer to that question
is <em>“Just bind the VAO, pal.”</em></p>
<h1>What can we do about it?</h1>
<p>Several attempts to enhance that safety have come up. The first thing we
<strong>have</strong> to do is to wrap all <strong>OpenGL</strong> object types into proper types. For
instance, we need several types for <code>Texture</code> and <code>Framebuffer</code>.</p>
<p>Then, we need a way to ensure that we cannot call a function if the context is
not setup for. There are a few ways to do that. For instance,
<a href="https://hackage.haskell.org/package/indexed-0.1">indexed monads</a> can be a good
start. However, I tried that, and I can tell you it’s way too complicated. You
end up with very long types that make things barely unreadable.
<a href="https://github.com/phaazon/igl/blob/master/src/Graphics/Rendering/IGL/Buffer.hs#L114">See this</a>
and <a href="https://github.com/phaazon/igl/blob/master/src/Graphics/Rendering/IGL/GL.hs#L51">this</a>
for excerpts.</p>
<h2>Luminance</h2>
<p>In my desperate quest of providing a safer <strong>OpenGL</strong>’s API, I decided to create
a library from scratch called <a href="https://github.com/phaazon/luminance">luminance</a>.
That library is not really an <strong>OpenGL</strong> safe wrapper, but it’s very close to
being that.</p>
<p><code>luminance</code> provides the same objects than <strong>OpenGL</strong> does, but via a safer way
to create, access and use them. It’s an effort for providing safe abstractions
without destroying performance down and suited for graphics applications. It’s
not a 3D engine. It’s a rendering framework. There’s no <em>light</em>, <em>asset
managers</em> or that kind of features. It’s just a <em>tiny</em> and <em>simple</em> powerful
API.</p>
<h3>Example</h3>
<p><code>luminance</code> is still a huge <em>work in progress</em>. However, I can already show an
example. The following example opens a window  but doesn’t render anything.
Instead, it creates a buffer on the GPU and perform several simple operations
onto it.</p>
<pre><code class="language-haskell">-- Several imports.
import Control.Monad.IO.Class ( MonadIO(..) )
import Control.Monad.Trans.Resource -- from the resourcet package
import Data.Foldable ( traverse_ )
import Graphics.Luminance.Buffer
import Graphics.Luminance.RW
import Graphics.UI.GLFW -- from the GLFW-b package
import Prelude hiding ( init ) -- clash with GLFW-b’s init function

windowW,windowH :: Int
windowW = 800
windowH = 600

windowTitle :: String
windowTitle = "Test"

main :: IO ()
main = do
  init
  -- Initiate the OpenGL context with GLFW.
  windowHint (WindowHint'Resizable False)
  windowHint (WindowHint'ContextVersionMajor 3)
  windowHint (WindowHint'ContextVersionMinor 3)
  windowHint (WindowHint'OpenGLForwardCompat False)
  windowHint (WindowHint'OpenGLProfile OpenGLProfile'Core)
  window &lt;- createWindow windowW windowH windowTitle Nothing Nothing
  makeContextCurrent window
  -- Run our application, which needs a (MonadIO m,MonadResource m) =&gt; m
  -- we traverse_ so that we just terminate if we’ve failed to create the
  -- window.
  traverse_ (runResourceT . app) window
  terminate

-- GPU regions. For this example, we’ll just create two regions. One of floats
-- and the other of ints. We’re using read/write (RW) regions so that we can
-- send values to the GPU and read them back.
data MyRegions = MyRegions {
    floats :: Region RW Float
  , ints   :: Region RW Int
  }

-- Our logic.
app :: (MonadIO m,MonadResource m) =&gt; Window -&gt; m ()
app window = do
  -- We create a new buffer on the GPU, getting back regions of typed data
  -- inside of it. For that purpose, we provide a monadic type used to build
  -- regions through the 'newRegion' function.
  region &lt;- createBuffer $
    MyRegions
      &lt;$&gt; newRegion 10
      &lt;*&gt; newRegion 5
  clear (floats region) pi -- clear the floats region with pi
  clear (ints region) 10 -- clear the ints region with 10
  readWhole (floats region) &gt;&gt;= liftIO . print -- print the floats as an array
  readWhole (ints region) &gt;&gt;= liftIO . print -- print the ints as an array
  floats region `writeAt` 7 $ 42 -- write 42 at index=7 in the floats region
  floats region @? 7 &gt;&gt;= traverse_ (liftIO . print) -- safe getter (Maybe)
  floats region @! 7 &gt;&gt;= liftIO . print -- unsafe getter
  readWhole (floats region) &gt;&gt;= liftIO . print -- print the floats as an array
</code></pre>
<p>Those read/write regions could also have been made <em>read-only</em> or <em>write-only</em>.
For such regions, some functions can’t be called, and trying to do so will make
your compiler angry and throw errors at you.</p>
<p>Up to now, the buffers are created <em>persistently</em> and <em>coherently</em>. That might
cause issues with <strong>OpenGL</strong> <em>synchronization</em>, but I’ll wait for benchmarks
before changing that part. If benchmarking spots performance bottlenecks, I’ll
introduce more buffers and regions to deal with special cases.</p>
<p><code>luminance</code> doesn’t force you to use a specific windowing library. You can then
embed it into any kind of host libraries.</p>
<h1>What’s to come?</h1>
<p><a href="https://github.com/phaazon/luminance">luminance</a> is very young. At the moment
of writing this article, it’s only 26 commits old. I just wanted to present it
so that people know it exists will be released as soon as possible. The idea is
to provide a library that, if you use it, won’t create black screens because of
framebuffers incorrectness or buffers issues. It’ll ease debugging <strong>OpenGL</strong>
applications and prevent from making nasty mistakes.</p>
<p>I’ll keep posting about <code>luminance</code> as I get new features implemented.</p>
<p>As always, keep the vibe, and happy hacking!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 24 Jul 2015 00:00:00 GMT</pubDate></item><item><title>luminance-0.27: cleanup, easier to use and examples!</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.27</link><description><![CDATA[<p><a href="https://crates.io/crates/luminance/0.27.1">luminance-0.27</a> was released lately and a bunch of new and interesting things was added.</p>
<h1>Easier API</h1>
<p>The <a href="https://github.com/phaazon/luminance-rs/blob/master/CHANGELOG.md#0270">CHANGELOG</a> is available to read but one of the most important change was the refactoring of
the pipeline system and of the overall API to make it less confusing. For instance, the
<code>Framebuffer::default</code> is now renamed <code>Framebuffer::back_buffer</code>. Another example is the <em>face
culling</em>, that was in pre-0.27 enabled by default (causing people who don’t now about it to be
confused because of a black screen). This is now disabled by default; hence less confusion.</p>
<p>The pipeline system types are now easier to use. For instance, redundancy was removed with the
various <code>Bound*</code> objects. When you want a <em>bound buffer of <code>f32</code></em> for instance, you just have to
use the type <code>BoundBuffer&lt;'a, f32&gt;</code> instead of the pre-0.27 <code>BoundBuffer&lt;'a, Buffer&lt;f32&gt;&gt;</code>. That is
a small change but it will eventually make working with luminance more comfortable.</p>
<p>Another change that I think is beneficial is the new <code>TessSlice</code> type – that replaces the pre-0.27
<code>TessRender</code>. This type represents a <em>GPU tessellation slice</em>. Such a slice can be used to render
part of a tessellation. What’s great is that the legacy methods to build such slices
(<code>TessRender::one_whole</code> for instance) have a new, more generalized way to do it:
<code>TessSliceIndex&lt;Idx&gt;</code>. This trait gives you a <code>slice</code> method which argument has type <code>Idx</code>. Several
implementors exist:</p>
<ul>
<li><code>impl TessSliceIndex&lt;Range&lt;usize&gt;&gt; for Tess</code>.</li>
<li><code>impl TessSliceIndex&lt;RangeFrom&lt;usize&gt;&gt; for Tess</code>.</li>
<li><code>impl TessSliceIndex&lt;RangeTo&lt;usize&gt;&gt; for Tess</code>.</li>
<li><code>impl TessSliceIndex&lt;RangeFull&lt;usize&gt;&gt; for Tess</code>.</li>
</ul>
<p>They all give you a <code>TessSlice</code> to work with (with the appropriate lifetime). The four
implementations listed above can be invoked with:</p>
<ul>
<li><code>tess.slice(0..10)</code>.</li>
<li><code>tess.slice(0..)</code>.</li>
<li><code>tess.slice(..10)</code>.</li>
<li><code>tess.slice(..)</code>.</li>
</ul>
<p>For those who wonder:</p>
<blockquote>
<p><em>Why have you not implemented <a href="https://doc.rust-lang.org/std/ops/trait.Index.html">Index</a> instead?</em></p>
</blockquote>
<p>The current <a href="https://doc.rust-lang.org/std/ops/trait.Index.html">Index</a> trait doesn’t give you enough power to index a value by returning something else
than a direct reference. Everything is summed up <a href="https://github.com/rust-lang/rfcs/pull/2473">in the RFC I wrote to fix Index</a>.</p>
<p>Feel free to have a look at the <a href="https://github.com/phaazon/luminance-rs/blob/master/CHANGELOG.md#0270">CHANGELOG</a> for further information.</p>
<h1>Examples, examples, examples!</h1>
<p>One of the most important feature people have asked is adding examples. The online documentation is
not enough – even if I spent lots of hours enhancing it. So I added several examples:</p>
<ul>
<li><strong>01-hello-world</strong>: learn how to draw two colored triangles by using vertex
colors (comes in <em>direct</em> and <em>indexed</em> geometry versions).</li>
<li><strong>02-render-state</strong>: learn how to change the render state to change the way the
triangles are rendered or how fragment blending happens.</li>
<li><strong>03-sliced-tess</strong>: learn how to slice a single GPU geometry to dynamically
select contiguous regions of it to render!</li>
<li><strong>04-shader-uniforms</strong>: send colors and position information to the GPU to
add interaction with a simple yet colorful triangle!</li>
<li><strong>05-attributeless</strong>: render a triangle without sending any vertex data to the
GPU!</li>
<li><strong>06-texture</strong>: learn how to use a loaded image as a luminance texture on the GPU!</li>
<li><strong>07-offscreen</strong>: get introduced to <em>offscreen rendering</em>, a powerful technique
used to render frames into memory without directly displaying them on your screen. Offscreen
framebuffers can be seen as a generalization of your screen.</li>
</ul>
<p>You can find them in <a href="https://github.com/phaazon/luminance-rs/blob/master/CHANGELOG.md">this place</a>.</p>
<p>Please do contribute if you thing something is missing, either by opening an issue or by opening a
PR. Any kind of contribution is highly welcomed!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 28 Jun 2018 14:00:00 GMT</pubDate></item><item><title>Asynchronous warmy: a prequel</title><link>https://strongly-typed-thoughts.net/blog/asynchronous_warmy_prequel</link><description><![CDATA[<p>Last weeks were interesting for <a href="https://crates.io/crates/warmy">warmy</a>, a crate I’ve been writing for several weeks / months
now that enables you to hot load and reload scarce resources – e.g. textures, meshes, configuration,
JSON parameters, dependency nodes, whatever. <a href="https://crates.io/crates/warmy">warmy</a> received several interesting features, among:</p>
<ul>
<li><em>Context passing</em>: it is now possible to pass a mutable reference (<code>&amp;mut</code>) to a typed context
when loading and reloading a resource. This enables per-value loadings, which is neat if you need
to add extra data when loading your resources (e.g. increment a counter).</li>
<li><em>Methods support</em>: before version 0.7, you had to implement a trait, <code>Load</code>, in order to tell
<a href="https://crates.io/crates/warmy">warmy</a> how to load a given object of a given type. This is convenient but carries a bad drawback:
if you want to represent your object with both JSON and XML for instance, you need to <em>type wrap</em>
your type so that you can <code>impl Load</code> twice. This was annoying and the type system also handed you
back an object which type was the wrapper type, not the wrapped type. This annoyance was
removed in 0.7 as you now have an extra type variable to <code>Load</code> – it defaults to <code>()</code> though –
that you can use to <code>impl Load</code> several times – think of that tag type variable as a type
representing the <em>encoding</em> or the <em>method</em> to use to load your value.</li>
<li><em>A VFS (Virtual FileSystem)</em>: the VFS makes it possible to write resource keys without caring
about their real location – e.g. <code>/splines/color_grading.json</code>. Before that, you still had to
provide a real filesystem path, which was both confusing and annoying (since you already give one
when you create a <code>Store</code>, the object that holds your resource).</li>
<li><a href="https://github.com/phaazon/warmy/blob/master/CHANGELOG.md#07">Changelog here</a>.</li>
</ul>
<p><a href="https://www.reddit.com/r/rust/comments/8fy3q4/warmy070_vfs_context_passing_reload_methods_and/">I posted on reddit</a>
in order to make people know of the new version, and interesting talks started to occur on both IRC
and GitHub. What people seem to want the most now is <em>asynchronous loading and reloading</em>. I’ve been
wanting that feature for a while too so I decided it could be a good idea to start working on it.
However, after a day of toying around, I came to the realization that I should write a small blog
post about it because I think it’s not trivial and it could help me shape my ideas.</p>
<blockquote>
<p>Note: this post is about brainstorming and setting up the frame to why and how async warmy. You
will find incomplete code, ideas and questions there. If you want to contribute to the discussion,
you’re more than welcome!</p>
</blockquote>
<h1>Synchronous versus asynchronous</h1>
<p>What does it mean to have a <em>synchronous</em> computation? What does it mean to have an <em>asynchronous</em>
one? You might find it funny, but a lot of people are still confused with the exact definition, so
I’ll try to give you more hindsight.</p>
<p>We talk about a <em>synchronous</em> task when we have to wait for it to finish before moving on to another
task. We have to wait until its completion to get the control flow back and call other functions. We
often talk about <em>blocking computations</em>, because you cannot do anything else while that computation
is running – at least on the thread this computation is running on.</p>
<p>We talk about an <em>asynchronous</em> task when you can get the control flow back without having to wait
for the task to finish. However, <strong>that doesn’t necessarily mean that the task is being executed in
parallel or concurrently</strong>. At some extent, you could easily label <a href="https://en.wikipedia.org/wiki/Generator_(computer_programming)">generators</a> as <em>asynchronous
primitives</em>, and this is what actually happens in <code>async / await</code> code: you give back the control
flow to the caller and the callee execution gets re-scheduled later. Hence, this is asynchronous
programming, yet the scheduling execution could be implemented on a single thread – hence no
parallel nor concurrency actually happen. Another example is when you perform a HTTP request: you
can send the request and instead of blocking until the response arrive, you can give the control
back, do something else, and then, at some time, handle the response. You don’t need parallelism
to do this: you need asynchronous programming.</p>
<blockquote>
<p>Note: a generalization of a generator is a <a href="https://en.wikipedia.org/wiki/Coroutine">coroutine</a>, which hands the control flow back to
another <a href="https://en.wikipedia.org/wiki/Coroutine">coroutine</a> instead of the caller when wanted.</p>
</blockquote>
<h1>What about warmy?</h1>
<p>At the time of writing this blog entry, <a href="https://crates.io/crates/warmy">warmy</a> is completely synchronous. Consider the following
example:</p>
<pre><code>extern crate serde_json;
extern crate warmy;

use std::error;
use std::fmt;
use std::fs::File;
use std::io;
use std::thread;
use std::time::Duration;
use warmy::{FSKey, Load, Loaded, Res, Store, StoreOpt, Storage};

struct JSON;

#[derive(Clone, Copy, Debug, PartialEq)]
struct Foo {
  array: [f32; 4]
}

#[derive(Debug)]
enum FooError {
  JsonError(serde_json::Error),
  IOError(io::Error)
}

impl fmt::Display for FooError {
  fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; Result&lt;(), fmt::Error&gt; {
    match *self {
      FooError::JsonError(ref e) =&gt; e.fmt(f),
      FooError::IOError(ref e) =&gt; e.fmt(f),
    }
  }
}

impl error::Error for FooError {
  fn description(&amp;self) -&gt; &amp;str {
    match *self {
      FooError::JsonError(ref e) =&gt; e.description(),
      FooError::IOError(ref e) =&gt; e.description(),
    }
  }

  fn cause(&amp;self) -&gt; Option&lt;&amp;error::Error&gt; {
    match *self {
      FooError::JsonError(ref e) =&gt; Some(e),
      FooError::IOError(ref e) =&gt; Some(e),
    }
  }
}

impl&lt;C&gt; Load&lt;C, JSON&gt; for Foo {
  type Key = FSKey;

  type Error = FooError;
  
  fn load(
    key: Self::Key,
    _: &amp;mut Storage&lt;C&gt;,
    _: &amp;mut C,
  ) -&gt; Result&lt;Loaded&lt;Self&gt;, Self::Error&gt; {
    let file = File::open(key.as_path()).map_err(FooError::IOError)?;
    let array = serde_json::from_reader(file).map_err(FooError::JsonError)?;
    let foo = Foo { array };

    Ok(foo.into())
  }
}

fn main() {
  // read scarce resources in /tmp
  let ctx = &amp;mut ();
  let opt = StoreOpt::default().set_root("/tmp");
  let mut store: Store&lt;()&gt; = Store::new(opt).expect("store creation failed");

  // get our resources via JSON decoding
  let foo: Res&lt;Foo&gt; = store.get_by(&amp;FSKey::new("/foo.json"), ctx, JSON).expect("foo should be there");
  let foo2: Res&lt;Foo&gt; = store.get_by(&amp;FSKey::new("/foo2.json"), ctx, JSON).expect("foo should be there");

  println!("{:#?}", foo);
  println!("{:#?}", foo2);

  loop {
    // sync the resources with the disk
    store.sync(ctx);
    thread::sleep(Duration::from_millis(100));
  }
}
</code></pre>
<blockquote>
<p>Note: you can test that code by inserting <code>warmy = "0.7"</code> and <code>serde_json = "1"</code> in a <code>Cargo.toml</code>
and by creating the JSON files containing 4D arrays of numbers, like <code>[0, 1, 2, 3]</code> in
<code>/tmp/foo{,2}.json</code>.</p>
</blockquote>
<p>If you look closely at that example, you can spot two important locations when we get resources: when
we create the <code>foo</code> and <code>foo2</code> bindings (search for <code>store.get_by</code> invocations). Here, it’s important
to understand what’s really happening:</p>
<ol>
<li>First, <code>/foo.json</code> is loading on the current thread.</li>
<li>Then, when it’s loaded and cached, <code>/foo2.json</code> starts loading.</li>
</ol>
<p>We can already see a pity here: both the files are completely disconnected from each other, yet, the
second file must wait for the former to finish before starting loading. We’re not using all the
cores / threads of our CPU. So we could perfectly imagine this new scenario:</p>
<ol>
<li>First, ask to load <code>/foo.json</code> and immediately get control flow back.</li>
<li>Then, ask to load <code>/foo2.json</code> and immediately get control flow back.</li>
<li>Wait for both the resources to be available, then continue.</li>
</ol>
<p>You could even do whatever you want between (2.) and (3.). The point here is that we can run tasks
without having to wait for them to finish before starting others. The explicit <em>waiting</em> part could
be a blocking call, such as:</p>
<pre><code>let foo_task = store.get_by(…);
let foo2_task = store.get_by(…);

let foo = foo_task.wait();
let foo2 = foo2_task.wait();
</code></pre>
<blockquote>
<p>Note: this is not the foreseen or planned interface. It’s just there to illustrate what we want to
achieve there.</p>
</blockquote>
<p>The goal of this blog entry is to explore possible implementations.</p>
<h1>futures</h1>
<p>The <a href="https://crates.io/crates/futures">futures</a> crate is a very promising and interesting crate. What it provides is basically a mean
to express <em>data that might get available in the future</em>. For instance:</p>
<pre><code>fn ping(host: &amp;str) -&gt; ???
</code></pre>
<p>Here, <code>ping</code> is a function that will perform an <a href="https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol">ICMP</a> request to a given host, identified by the
function argument. Because that function might take a while (at least several milliseconds, which is
<strong>a lot</strong>), we have to make a decision:</p>
<ol>
<li>Either we decide to block the current thread until a response gets available (the host responds
to our ping and we get the packet back).</li>
<li>Or either we decide to free the control flow from the <code>ping</code> function and do something else
until the response arrive.</li>
</ol>
<p>If you’ve followed all what I said since the beginning of this blog post, you might have noticed that
(1.) is synchronous and (2.) is asynchronous (also notice that we haven’t talked about parallelism
yet!).</p>
<p>With (1.), we would write <code>ping</code>’s signature like this:</p>
<pre><code>fn ping(host: &amp;str) -&gt; Result&lt;PingResponse, NetworkError&gt;
</code></pre>
<p>With (2.), we would write it like this, using the <code>futures</code> crate:</p>
<pre><code>fn ping(host: &amp;str) -&gt; impl Future&lt;Item = PingResponse, Error = NetworkError&gt;
</code></pre>
<blockquote>
<p>Note: the syntax <code>impl Trait</code> is called <em>conservative impl trait</em> and its description can be found
in the <a href="https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md">corresponding RFC on impl Trait</a>.</p>
</blockquote>
<p>So what you basically do here is to send a non-blocking request and get its result in a non-blocking
way. Sweet! How does that apply to <a href="https://crates.io/crates/warmy">warmy</a>?</p>
<h1>futures applied to warmy</h1>
<p>Asynchronous <a href="https://crates.io/crates/warmy">warmy</a> should have the following properties:</p>
<ul>
<li>When you ask for a resource to load, you immediately get the control flow back.</li>
<li>Hot-reloading mustn’t block either, as you will do it in the <code>Store::sync</code> function.</li>
<li>Because of the asynchronous nature of those computations, the <em>context passing</em> now gets a bit
tricky: we cannot handle a mutable reference on our context anymore, because otherwise we would
block the main thread. We need to share the context in a smart way.</li>
<li>Finally, all parallel computations should be ran on some kind of <em>green thread</em> so that we don’t
have to worry about destroying the OS threads amount.</li>
</ul>
<h2>Asynchronous loading</h2>
<p>The <a href="https://docs.rs/warmy/0.7.2/warmy/load/struct.Storage.html#method.get">current definition of the <code>Store::get</code> function</a>
is:</p>
<pre><code>pub fn get&lt;K, T&gt;(
  &amp;mut self, 
  key: &amp;K, 
  ctx: &amp;mut C
) -&gt; Result&lt;Res&lt;T&gt;, StoreErrorOr&lt;T, C&gt;&gt;
where
  T: Load&lt;C&gt;,
  K: Clone + Into&lt;T::Key&gt;
</code></pre>
<p>We want something like this:</p>
<pre><code>pub fn async_get&lt;K, T&gt;(
  &amp;mut self, 
  key: &amp;K, 
  ctx: &amp;mut C
) -&gt; Result&lt;impl Future&lt;Item = AsyncRes&lt;T&gt;, Error = StoreErrorOr&lt;T, C&gt;&gt;, StoreErrorOr&lt;T, C&gt;&gt;
where
  T: Load&lt;C&gt;,
  K: Clone + Into&lt;T::Key&gt;
</code></pre>
<p>Woosh. It gets hard to read – eventually, <a href="https://github.com/rust-lang/rfcs/blob/master/text/1733-trait-alias.md">trait aliases</a> will help us there. We can see the use of
the <code>futures</code> crate here, in the return type:</p>
<pre><code>impl Future&lt;Item = AsyncRes&lt;T&gt;, Error = StoreErrorOr&lt;T, C&gt;&gt;
</code></pre>
<p>Which reads as <em>“An asynchronous resource of type <code>T</code> available in the future or an error due to the
loading of <code>T</code> and <code>C</code> or due to the store”</em>.</p>
<p>However, something important to notice is that we miss a crucial point here: we actually <em>want a
parallel execution</em>. We could start try by defining a small state machine to step through the
process of loading a resource. That could be:</p>
<pre><code>enum LoadState&lt;K, T, C&gt; {
  Pending(K, Shared&lt;C&gt;), // resource loading pending
  Loading(Receiver&lt;AsyncRes&lt;T&gt;&gt;), // resource currently loading
  Done, // resource loaded
}
</code></pre>
<p>This small state machine can be stepped through by the following process:</p>
<pre><code>fn start_load&lt;K, T, C&gt;(key: K, ctx: &amp;mut C) -&gt; impl Future&lt;Item = LoadState&lt;K, T, C&gt;&gt; {
  LoadState::Pending(key, ctx.share_somehow())
}

impl&lt;K, T, C&gt; Future for LoadState&lt;K, T, C&gt; {
  type Item = AsyncRes&lt;T&gt;;

  type Error = StoreErrorOr&lt;T, C&gt;;

  fn poll(&amp;mut self, ctx: &amp;mut Context) -&gt; Result&lt;Async&lt;Self::Item&gt;, Self::Error&gt; {
    match *self {
      LoadState::Pending(..) =&gt; {
        // start the loading
        let (sx, rx) = oneshot::channel();
        let task = Task::new(move || {
          // load the resource somehow
          let res = …;
          sx.send(res);
        });

        // spawn a new task to load the resource and update the state machine
        ctx.spawn(task);
        *self = LoadState::Loading(rx);
        Ok(Async::NotReady)
      }

      LoadState::Loading(ref mut rx) =&gt; {
        match rx.map(|res| {
          *self = LoadState::Done;
          res
        }).poll()?
      }

      LoadState::Done =&gt; panic!("poll called after future has arrived")
    }
  }
}
</code></pre>
<p>That code might be incomplete or not typecheck completely (not tested), but the idea is there. I
still don’t know whether this is the good way to use <code>futures</code> or whether I should run a single, long
computation without the small state machine. I just don’t know yet.</p>
<h2>Asynchronous reloading</h2>
<p>This part is tightly bound to how the <code>Store::sync</code> will behave. Now that I tink about it, maybe it’d
be more ergonomic / elegant / simpler to have an event loop living on a separate thread that would
benefit from <code>epoll</code> and all those fancy asynchronous primitives to deal with that. That would result
in no more by-hand synchronization, since it’d be done by the event loop / reactor / whatever you
want to name it.</p>
<p>Currently, I have not settled up on any decision regarding reloading.</p>
<h2>Asynchronous context passing</h2>
<p>This will be definitely a tricky part as well, because what was before:</p>
<pre><code>store.sync(&amp;mut ctx);
</code></pre>
<p>will then become something like:</p>
<pre><code>store.sync(ctx.share());
</code></pre>
<p>Also, something that might happen is that because the synchronization will be done in an asynchronous
way, you will need to ensure that you do not try to synchronize the same resource several times.</p>
<p>Finally, if you take into account the previous section, the user might not even synchronize by hand
anymore, so the context will have to be shared anyway and moved to the event loop. Argh. That doesn’t
seem simple at all! :)</p>
<h2>Parallel execution</h2>
<p>One final aspect of asynchronous <a href="https://crates.io/crates/warmy">warmy</a> is obviously how we’re going to <code>poll</code> the various <code>Future</code>s
generated by the <code>get</code> functions. One first, naive idea would be to run those in OS threads. However,
imagine that you’ll be loading plenty of resources (on a big and release project, it’s easy to
imagine several thousands of resources loading in parallel). You don’t want to allocate that many OS
threads but instead rely on a green threading solution… which I haven’t found yet. People on IRC
advised to have a look at <a href="https://crates.io/crates/futures-cpupool">futures-cpupool</a>, but I’d like to have a complete solution set before
deciding anything.</p>
<p>I will post this blog post on reddit with hope that people will come up with ideas and I’m sure
enlighting ideas on how to cope with all of this asynchronous properties I want to push to <a href="https://crates.io/crates/warmy">warmy</a>.</p>
<p>Thanks for having read me, and keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 08 May 2018 19:30:00 GMT</pubDate></item><item><title>al 0.1 released!</title><link>https://strongly-typed-thoughts.net/blog/al_0.1_released</link><description><![CDATA[<h1>What is OpenAL?</h1>
<p><img src="http://upload.wikimedia.org/wikipedia/en/thumb/2/28/OpenAL_logo.png/200px-OpenAL_logo.png" alt="" /></p>
<p><a href="http://www.openal.org/">OpenAL</a> is an open-source and free audio library
developped by <strong>Creative Technology</strong>. The first version, however, was made by
<strong>Loki Software</strong>. The API targets spacialized audio and sound managing so that
it’s simple to simulate sound attenuation over distance and
<a href="http://en.wikipedia.org/wiki/Doppler_effect">Doppler effect</a>. <strong>OpenAL</strong> is
supported on a <a href="http://en.wikipedia.org/wiki/OpenAL#Supported_platforms">wide variety of platforms</a>,
from PC (Windows, Linux, FreeBSD, Mac OSX and so on) to consoles (PS3, Xbox…).</p>
<p>The latest version was released in 2005 (<em>9 years old ago!</em>) and is OpenAL 1.1.
It’s then pretty logical that we should have OpenAL 1.1 in <strong>Haskell</strong>.</p>
<h1>OpenAL and Haskell</h1>
<blockquote>
<p><em>“Wait… we already have <a href="https://hackage.haskell.org/package/OpenAL">OpenAL</a>
in Haskell!”</em></p>
</blockquote>
<p>That’s true. However, the <a href="https://hackage.haskell.org/package/OpenAL">OpenAL</a>
Haskell package has some serious issues.</p>
<p>The first one is that package is not a direct binding to <strong>OpenAL</strong>. It’s a
high-level wrapper that wraps stuff with obscure types, like <code>StateVar</code> or
<code>ObjectName</code>. Even though such abstractions are welcomed, we should have a raw
binding so that people who don’t want those abstractions can still use
<strong>OpenAL</strong>. Moreover, such high libraries tend to lift resources allocations /
deallocations into the GC<sup class="footnote-reference"><a href="#GC">1</a></sup>. That is a pretty bad idea because we,
sometimes, want to control how long objects live.</p>
<p>The second one, which is as terrible, is the fact that those abstractions aren’t
defined by the Haskell <a href="https://hackage.haskell.org/package/OpenAL">OpenAL</a>
package, but by another, which is…
<a href="https://hackage.haskell.org/package/OpenGL">OpenGL</a>. The latter is <strong>not</strong> a
raw binding to <strong>OpenGL</strong> – for that have a look at
<a href="https://hackage.haskell.org/package/OpenGLRaw">OpenGLRaw</a> or
<a href="https://hackage.haskell.org/package/gl">gl</a> – but the same kind of high-level
oriented package. A lot of us have asked the authors of both the package to
agree of making those abstractions shared between both the libraries; they
never seemed to agree.</p>
<p>And even though, having a dependency between <strong>OpenAL</strong> and <strong>OpenGL</strong> is
insane!</p>
<p>Sooooo… since <a href="https://github.com/ekmett">Edward Kmett</a> had the idea of
<a href="https://hackage.haskell.org/package/gl">gl</a> to fix similar issues, I think I
should have the idea of <a href="https://hackage.haskell.org/package/al">al</a> to go along
with <a href="https://hackage.haskell.org/package/gl">gl</a>. The idea is that if you want
to use one, you can use the other one without any conflict.</p>
<h1>al 0.1</h1>
<p>Yesterday night, around 20:00, I decided to make it. It took me several hours
to write the raw binding, but I finally made it and released
<a href="https://hackage.haskell.org/package/al">al 0.1</a> the very next day – uh, today!</p>
<p>Because <strong>OpenAL</strong> is not shipped with your <sup class="footnote-reference"><a href="#OS">2</a></sup> nor anything special, and thus
because you have to explicitely install it, installing
<a href="https://hackage.haskell.org/package/al">al</a> requires a bit more than just
typing <code>cabal install al</code>.</p>
<h2>Installing al</h2>
<h3>cabal update</h3>
<p>First things first:</p>
<pre><code>cabal update
</code></pre>
<p>You should have <a href="https://hackage.haskell.org/package/al">al</a> available if
you type <code>cabal info al</code> afterwards.</p>
<h3>c2hs</h3>
<p><a href="https://hackage.haskell.org/package/al">al</a> requires you to have
<a href="https://hackage.haskell.org/package/c2hs-0.23.1">c2hs</a> – version <strong>0.23.*</strong> –
installed. If not:</p>
<pre><code>cabal install c2hs
</code></pre>
<p>Be sure it’s correctly installed:</p>
<pre><code>c2hs --version
C-&gt;Haskell Compiler, version 0.23.1 Snowbounder, 31 Oct 2014
  build platform is "i386-mingw32" &lt;1, True, True, 1&gt;
</code></pre>
<h3>OpenAL SDK</h3>
<p>Of course, you should have the <strong>OpenAL SDK</strong> installed as well. If not, install
it. It should be in your package manager if you’re on Linux / FreeBSD, and you
can find it <a href="http://www.openal.org/creative-installers/">here</a> for Windows
systems.</p>
<blockquote>
<p><strong>Important: write down the installation path, because we’ll need it!</strong></p>
</blockquote>
<h3>Installing al</h3>
<p>Here we are. In order to install, <a href="https://hackage.haskell.org/package/al">al</a>
needs the paths to your <strong>OpenAL SDK</strong>. I’m not sure whether it could vary a
lot, though. The default Windows paths are:</p>
<ul>
<li><code>C:\Program Files (x86)\OpenAL 1.1 SDK\libs</code> for the libraries ;</li>
<li><code>C:\Program Files (x86)\OpenAL 1.1 SDK\include</code> for the header files.</li>
</ul>
<p>In future releases, I’ll default to those so that you don’t have to explicitely
pass the paths if you have a default installation. But for the moment, you have
to pass those paths when installing:</p>
<pre><code>cabal install al --extra-lib-dirs="C:\Program Files (x86)\OpenAL 1.1 SDK\libs"
--extra-include-dirs="C:\Program Files (x86)\OpenAL 1.1 SDK\include"
</code></pre>
<p>Of course, you may adapt paths to your situation. You may even try without that
for UNIX systems – it might already be in your <code>PATH</code>, or use slashs for
<strong>MinGW</strong>.</p>
<h2>Using al</h2>
<p><a href="https://hackage.haskell.org/package/al">al</a> lays into two module trees:</p>
<ul>
<li><code>Sound.AL</code></li>
<li><code>Sound.ALC</code></li>
</ul>
<p>The former exports anything you might need to use the <strong>OpenAL API</strong> while
the latter exports symbols about the <strong>OpenAL Context API</strong>. Please feel free
to dig in each tree to import specific types or functions.</p>
<h2>OpenAL documentation</h2>
<p><a href="https://hackage.haskell.org/package/al">al</a> doesn’t have any documentation for
a very simple reason: since it’s a raw binding to the <strong>C OpenAL API</strong>, you
should read the <strong>C</strong> documentation. Translating to <strong>Haskell</strong> is
straightforward.</p>
<p><a href="http://www.openal.org/documentation/openal-1.1-specification.pdf">The OpenAL 1.1 Specifications</a></p>
<h1>What’s next?</h1>
<p>Up to now, <a href="https://hackage.haskell.org/package/al">al</a> doesn’t cover
extensions. It’s not shipped with <strong>ALUT</strong> either as I want to keep the package
low-level and raw – and don’t even try to make me change my mind about that ;) .</p>
<p>If you have any issues, please let me know on the issues tracker. I’m responsive
and I’ll happily try to help you :) .</p>
<p>Once again, have fun, don’t eat too much chocolate nor sweets, and happy Easter
Day!</p>
<div class="footnote-definition" id="GC"><sup class="footnote-definition-label">1</sup>
<p>Garbage Collector</p>
</div>
<div class="footnote-definition" id="OS"><sup class="footnote-definition-label">2</sup>
<p>Operating System</p>
</div>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 28 Feb 2015 00:00:00 GMT</pubDate></item><item><title>Animate code with awoo!</title><link>https://strongly-typed-thoughts.net/blog/introducing-awoo</link><description><![CDATA[<p>Lately, I’ve been wanting to re-write demoscene-like applications. Not in the same mood and way as
I usually did, though. Instead, I want to build small things for people to play with. A bit like
small and easy to use audiovisual experiences (it can be seen as small video games for instance,
but focused on the artistic expression as some games do).</p>
<p>See, the type of application you need to write to make a demo is a bit special. Most programs out
there are often either <em>oneshot</em> or <em>reactive</em>.</p>
<ul>
<li>A CLI program, an encoder/decoder, a math equation solver or a script are often <em>oneshot</em>
programs. You give them a list of inputs and parameters and they do something based on their
inputs. They’re are most of the time non-interactive.</li>
<li>Programs such as GUIs, editors, video games, simulations etc. are often <em>reactive</em>, meaning that
they mostly sleep until an event happen (a key press, mouse movement, etc.). For some, sleeping
is reduced because they have a lot to do (handling network connections, performing I/O
operations, streaming audio, rendering frames to the screen, etc.).</li>
</ul>
<p>That binary split is not absolute, of course. Some programs don’t belong to any of the two prevous
sections (think of a driver, a firmware, etc.). For the purpose of this article, though, it will be
enough.</p>
<p>At some extent, we could state that a demoscene production, a video game or a simulation are also
<em>reactive</em> programs. They mostly react to time as well as user interaction, network events, etc..
About time, you could imagine dividing time passing with very small quanta and picture time as a
virtual clock that <em>ticks</em> such quanta. Every time a new quantum of time is emitted, the whole
simulation is notified and reacts. However, representing that problem this way is not necessarily
the best idea. See, a function of time is often a continuous function and then, it has an output
value for any value of its input that lies in its domain. That property is interesting for us as we
can get a value at any time with an arbitrary precision (the boundary being the precision at which
we can represent a number on a computer).</p>
<p>The thing is, the kind of program we want generates its own inputs based on, mostly, the speed at
which the hardware it’s running on is able to render a complete frame. The faster the more accurate
we sample from that continuous function. That is actually quite logical: more FPS means, literally,
more images to sample. The difference between two images will get less and less noticeable as the
number of FPS rises. That gives you smooth images.</p>
<p>The “challenge” here is to write code to schedule those images. Instead of taking a parameter like
the time on the command-line and rendering the corresponding image, we will generate a stream of
images and will do different things at different times. Especially in demoscene productions, we want
to synchronize what’s on the screen with what’s playing on the audio device.</p>
<h1>The overall idea</h1>
<p>From my point of view, we need at least two mechanisms of synchronization:</p>
<ul>
<li>A <em>high-level</em> synchronization mechanism, to state how globally the application will behave.
I also like to see that kind of mechanism as a <em>discrete space</em> problem. You don’t have values
to interpolate or sample from but only several pure values to “jump” from one to another every
time the simulation time passes a given point in time. This kind of synchronization will state
things like:
<ul>
<li>When the application starts, display this scene and let it play for <em>3 seconds</em>.</li>
<li>Then after <em>3 seconds</em>, switch to this other scene for <em>5 seconds</em>.</li>
<li>After <em>8 seconds</em>, draw this nice little cube and make it crash into a plane for about <em>10
seconds</em>. Repeat that scene once more.</li>
<li>End the application.</li>
</ul>
</li>
<li>A <em>low-level</em> synchronization system. That would be used for a movement, a color change, a
camera change, etc. This kind of synchronization is, to me, a <em>continuous space</em> problem. It
can be solved by sampling from a curve, for instance.</li>
</ul>
<p>Both those problems are solved by two crates I wrote lately. Respectively, <a href="https://crates.io/crates/awoo">awoo</a> and <a href="https://crates.io/crates/splines">splines</a>.
This blog post is about <a href="https://crates.io/crates/awoo">awoo</a>. <a href="https://crates.io/crates/splines">splines</a> already has its own dedicated articles
<a href="https://phaazon.net/blog/splines-introduction">here</a> and
<a href="https://phaazon.net/blog/splines-1.0.0-rc.1">here</a>. Nevertheless, I will make another blog article
about it because I have new ideas I will add to the crate to enrich the <a href="https://crates.io/crates/splines">splines</a> experience.</p>
<h1><a href="https://crates.io/crates/awoo">awoo</a> and the if / else if problem</h1>
<p>Taking on the example of the <em>high-level</em> synchronization described above, one can write quickly the
following naive yet working snippet:</p>
<pre><code class="language-rust">let mut time_ms: f32 = 0.;

loop {
  if time_ms &lt;= 5. {
    // render scene 1
  } else if time_ms &lt;= 8. {
    // render scene 2
  } else if time_ms &lt;= 20. {
    // render scene 1 again
  } else if time_ms &lt;= 25. {
    // render scene 3
  } else
    break; // quit
  }

  time_ms += 0.01; // 100 FPS
}
</code></pre>
<p>That code is typical in demoscene production when we have to rush or even if we have a few scenes
to write. However, it has several problems:</p>
<ul>
<li>It’s not really elegant. All those <code>if</code>s seem like naive and not necessary code.</li>
<li>The more time passes, the more conditions and branching we will do. What it means is that even
if it’s not noticeable (testing floating-point numbers like that is really fast so it won’t
change much), it’s easy to get that the second scene requires two tests in order to be rendered
while scene 3 requires four and scene 1 requires <em>either</em> one or three!</li>
<li>We notice that, every time a condition evaluates to <code>true</code>, all the previous branches can be
completely discarded — we don’t have to test them anymore! — because time will never go
backwards in a simulation (that is a strong assumption and it’s not true if you’re debugging,
but for a release application, it is).</li>
</ul>
<p>So, how can we do better? The idea is actually pretty simple. We want a very simple form a
<a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite-state machine</a>. In our case, the <em>states</em> are just what’s inside our <code>if</code>s; the initial
state is the first scene being rendered and the transitions are a predicate on the current time.
Straightforward, right?</p>
<p>The idea of <a href="https://crates.io/crates/awoo">awoo</a> is exactly that: allowing you to write the previous code like this:</p>
<pre><code class="language-rust">let windows = vec![
  Window::new(0., 5.).map(|_| println!("hey, it’s scene 1!")),
  Window::new(5., 8.).map(|_| println!("hey, it’s scene 2!")),
  Window::new(8., 20.).map(|_| println!("hey, it’s scene 1 again!")),
  Window::new(20., 25.).map(|_| println!("hey, it’s scene 3!")),
];
let mut scheduler = SequentialScheduler::new(
  SimpleF32TimeGenerator::new(0., 0.01),
  windows
);

scheduler.schedule();
</code></pre>
<p>The code is now declarative and easier to read. Internally, the <code>SequentialScheduler</code> used here
will make a single test to know which code it has to run. The implementation is not the typical
implementation you would find for a FSM (<a href="https://en.wikipedia.org/wiki/Finite-state_machine">finite-state machine</a>), which uses a graph, but it’s akin.</p>
<p>You might be wondering why we do that <code>map</code> stuff instead of creating a <code>Window</code> directly with the
actions. The answer is simple: a <code>Window</code> doesn’t hold any actions. That allows for creating windows
via JSON, for instance, without having to deal with closures (I have no idea how that would even be
possible with JSON). The idea is then to <em>zip</em> your windows to your actions by using a <em>hashmap</em>,
for instance. This following snippet showcases exactly that
(fully available <a href="https://github.com/phaazon/awoo/blob/master/examples/json-driven.rs">here</a>):</p>
<pre><code class="language-rust">const WINDOWS: &amp;str = r#"
{
  "a": {
    "start": 0,
    "end":   3
  },
  "b": {
    "start": 3,
    "end":  10
  }
}"#;

let windows: HashMap&lt;String, Window&lt;f32&gt;&gt; = from_str(WINDOWS).expect("cannot deserialize windows");
let a = windows.get("a").unwrap().map(|t| println!("in a: {}", t));
let b = windows.get("b").unwrap().map(|t| println!("in b: {}", t));
}
</code></pre>
<p>What gets interesting is that you can write your own time generator to manipulate the simulation in
other ways — and you can also use different schedulers regarding what you do with time. For
instance, you can imagine implementing a time generator that gets time from a HTTP request, a
keyboard, a network socket, etc. and then control your simulation with external stimuli.</p>
<p>What happens when the escape key is pressed and that you need to stop the simulation in order to
quit? Simple: you need an interruptible scheduler. <a href="https://crates.io/crates/awoo">awoo</a> offers that as well in this form:</p>
<pre><code class="language-rust">use std::sync::mpsc::channel;

let (sx, rx) = channel();
let mut scheduler = create_your_scheduler();

scheduler.interruptible_with(move |_| {
  // here, the closure’s argument is the time at which the scheduler is checking for interruptions
  if let Ok(_) = rx.try_recv() {
    Interrupt::Break
  } else {
    Interrupt::Continue
  }
});

scheduler.schedule();
</code></pre>
<p>Here, we use the spinning loop of the scheduler to check for interruptions in a straight-forward
way.</p>
<p>So far, I have to admit I haven’t digged the <code>async</code>, <code>await</code> and <code>Future</code> concepts in Rust too
much. For a single reason: discussions around those concepts have been heated and I will wait for an
official announcement of the feature. Schedulers, especially as simple as the ones in <a href="https://crates.io/crates/awoo">awoo</a>, don’t
necessarily requires such IO features but the interruptible feature might. To me, the current
implementation of interruptible schedulers in <a href="https://crates.io/crates/awoo">awoo</a> is sufficient, especially for animation
purposes — I might even add that feature directly in <a href="https://crates.io/crates/awoo">awoo</a> so that you don’t have to do it by hand.</p>
<h1>About the scope of the crate</h1>
<p>Currently, the crate’s scope is very narrow — and I actually like that. A tight and small scope
implies a better visibility about what the crate must do and how it must do it. The crate is
currently simple and it might get more and more complex stuff as needs appear. As I always tell
other developers and engineers, I don’t like to overthink too much features I don’t even need.
Obviously, it’s important to keep planning possible future additions… But not too much. This is why
that crate’s scope, if augmented, will only and always revolve around the concept of scheduling
animation code. It’s currently an experimental crate and I’m trying to write demos with it, so we’ll
see what time thinks about it.</p>
<p>So that’s all for me for today. I hope you liked it. Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 28 Jul 2019 11:00:00 GMT</pubDate></item><item><title>My thoughts about editors in 2020</title><link>https://strongly-typed-thoughts.net/blog/editors-in-2020</link><description><![CDATA[<p>I have been trying a lot of editors lately. When I say “trying”, I really meant I spent some time configuring and
using those editors. The ones I spent time using are:</p>
<ul>
<li><a href="https://neovim.io">neovim</a>, my main, daily editor I use for pretty much almost all projects I work on.</li>
<li><a href="https://www.jetbrains.com/idea">IntelliJ IDEA</a>, that I currently use at work to hack on Java codebases.</li>
<li><a href="https://code.visualstudio.com">VS Code</a>, that I have been trying mainly in Rust, TOML and Markdown.</li>
<li><a href="https://www.gnu.org/software/emacs">emacs</a>, that I highly hacked around to play on my Haskell and Rust codebases (and YAML / Markdown / TOML too).</li>
<li><a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a>, that I tried when I saw a colleague in a previous company I worked in (I was impressed by
the “united” feeling of the UI and by how everything looked so slick). So I gave it a try.</li>
<li><a href="https://atom.io">atom</a>, <a href="https://github.com">GitHub</a>’s take on editors. Same thing, mostly Rust, Haskell, etc.</li>
<li>A bunch of others that I won’t talk about because I quickly stopped using.</li>
</ul>
<p>The goal of this article is to create a temporal “snapshot” of my views on editors and what I think about the
current situation. I have been using vim and especially neovim for more than a decade. I need to explain about
my current workflow and what I cherish in editing before talking about each editors. Expect a point of view
from a neovimer which is looking around at lots of editors.</p>
<blockquote>
<p>This is only a personal workflow / point of view that works well <em>for me right now</em>. It doesn’t mean it will
for you and it doesn’t mean a different workflow would be worse.</p>
</blockquote>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#what-i-think-powerful-editing-should-be">What I think powerful editing should be</a>
<ul>
<li><a href="#keyboard-layout">Keyboard layout</a></li>
<li><a href="#modal-editors">Modal editors</a></li>
<li><a href="#how-i-like-to-move-around">How I like to move around</a></li>
<li><a href="#all-the-other-modal-candies">All the other modal candies</a></li>
</ul>
</li>
<li><a href="#the-editors">The editors</a>
<ul>
<li><a href="#neovim">neovim</a>
<ul>
<li><a href="#my-neovim-setup">My neovim setup</a></li>
<li><a href="#what-i-like-about-neovim">What I like about neovim</a></li>
<li><a href="#what-i-dislike-about-neovim">What I dislike about neovim</a></li>
</ul>
</li>
<li><a href="#intellij-idea">IntelliJ IDEA</a>
<ul>
<li><a href="#what-i-like-about-intellij-idea">What I like about IntelliJ IDEA</a></li>
<li><a href="#what-i-dislike-about-intellij-idea">What I dislike about IntelliJ IDEA</a></li>
</ul>
</li>
<li><a href="#vs-code">VS Code</a>
<ul>
<li><a href="#what-i-like-about-vs-code">What I like about VS Code</a></li>
<li><a href="#what-i-dislike-about-vs-code">What I dislike about VS Code</a></li>
</ul>
</li>
<li><a href="#emacs-and-doom-emacs">emacs and DOOM emacs</a>
<ul>
<li><a href="#what-i-like-about-emacs--doom-emacs">What I like about emacs / DOOM emacs</a></li>
<li><a href="#what-i-dislike-about-emacs--doom-emacs">What I dislike about emacs / DOOM Emacs</a></li>
</ul>
</li>
<li><a href="#atom">atom</a>
<ul>
<li><a href="#what-i-like-about-atom">What I like about atom</a></li>
<li><a href="#what-i-dislike-about-atom">What I dislike about atom</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#wrap-it-up">Wrap it up</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>What I think powerful editing should be</h1>
<h2>Keyboard layout</h2>
<p>I am French and I’m using a keyboard layout that is made to type very quickly in French and to code. With hindsight,
since I type more often in English than in French, maybe I should have picked another keyboard layout, but the coding
experience in my keyboard layout is really great, so I stick around.</p>
<p>The keyboard layout is <strong>bépo</strong>. I learned bépo the “recommended” way — i.e. you have to practice <em>typing</em> (« dactylographie » in French). It means that I use all my fingers to type on a keyboard, and that each key on the
keyboard is assigned <em>a single finger</em> that will press it. That helps a lot with muscle memory and to reduce wrist
pain (my wrists barely move when I type on a keyboard), among other things. The typing speed is a side-effect of
being accurate and having good comfort (if you are curious, I’m pretty fast but there are faster people — I type at
around 120 to 130 words per minute). Because I think the speed doesn’t matter when programming, I think the most
important part to remember here is the comfort: the wrists don’t move and my fingers fly around the keyboard, whatever
the speed.</p>
<h2>Modal editors</h2>
<p>I think a modal editor is superior, for various reasons. The first one is that I truly <strong>hate</strong> having to use a
<em>mouse</em> for something that can be done without having to move around hundred of pixels with your cursor and clicking
on buttons. For instance, running an application, on my current machines, is simply typing <code>alt d</code>, the name of the
program (I typically use completion, so I never type fully the name of the program) and hit enter. All this without
moving my hands from the keyboard. And I do that for programs like <code>firefox</code>, <code>kdenlive</code>, etc. but for terminal
applications, I simply type and complete them in my terminal, which I open simply with <code>alt return</code>.</p>
<p>So, using a mouse to move around a cursor in an editor feels like completely suboptimal to me, especially because we
write code (i.e. we type on a keyboard) most of the time, so moving around with the mouse implies several back and
forth movements between the keyboard and the mouse. Maybe you don’t care and it’s cool to you, but to me, this is
truly <em>horror land</em>. I feel <em>very</em> uncomfortable when doing this.</p>
<p>Also, <em>modern editors</em> that are not modal typically make people move by using the arrows keys, which are either far
on your keyboard, or — like my keyboard, a 60% home made one — not in direct access and then require a function key
to enable them.</p>
<p>So that’s the first reason why I like modal editors. They make a smarter use of your keyboard for simple yet
recurrent features, like moving around — e.g. <code>h j k l</code>. The second reason why I like them is because of the facts
they have a completely new mode for non-modal editor (i.e. the <em>normal</em> mode), you have a whole keyboard / lots of
keys to bind actions to and do lots of things people usually do with the mouse. Being able to split an editor into
several buffers, move around the buffers, go at the beginning of the paragraph, search and replace, register actions
into macros and replay them, etc. All this without even moving the wrists. The learning curve is steep if you’re used
to the mouse, but once you’ve passed the mental barrier, really, and this is a personal opinion, but I truly think
that using the mouse again after that feels like handicap to me.</p>
<h2>How I like to move around</h2>
<p>When I look at people coding, I see several kind of programmers:</p>
<ul>
<li>The ones who move with the arrows or with <code>h j k l</code> in modal editors. You can spot them very easily at how the
cursor moves in a document. It typically implies keeping a key pressed until the cursor reach a given row, then
pressing another key until the cursor reach a given column and then adjust if they went passed the location they
had in mind.</li>
<li>People using the mouse, by clicking at the place they want to put the cursor at.</li>
<li>People using <em>relative numbers</em>. That’s an enhancement of the first group: they typically use relative numbers to
know which lines they want to jump to very quickly, so that they don’t have to press the up / down keys for ages
until reaching the line they want to. Instead, they look at the number on the lines, press that number, and the
direction, and bam, they are on the lines. They then use typical motions from vim like <code>$</code> (go to end of line),
<code>f</code> (go to the first occurrence of the next character typed after <code>f</code>, like <code>f(</code> will make your cursor go to the
next <code>(</code>), <code>%</code> (go to the matching delimiter) or <code>w</code> (go to the beginning of the next word) / <code>b</code> (go to the
beginning of the previous word), etc. to move faster on the same line (it works across lines too).</li>
</ul>
<p>I think that represents 99,9% of what I see people do. Obviously, you will get that I don’t belong to the second set
of people… but I don’t really belong to any, actually. How I move is, I guess, convoluted for most people and
I know some people won’t understand how it can feel. I use <code>h j k l</code> and all the motions from vim described in the
third group (and even more; I full lecture of four hours would be required to explain all of them :D), but it all
depends on the <em>distance</em> I need to travel. If my cursor is on a word and I want to move to the beginning of a word
located very closely to my cursor on the same line, I’ll simply type <code>www</code> if it’s three words apart (or <code>3w</code> if I’m
feeling funny). If the distance is higher, I use a tool called [EasyMotion].</p>
<p>Easymotion really is a wonderful tool. The idea is that it has several modes, depending on the kind of move you want
to perform:</p>
<ul>
<li><em>By lines</em>: this mode allows you to jump to any line in the current (or all open) buffers.</li>
<li><em>By words</em>: tihs mode allows you to jump to any “word” in the current (or all open) buffers.</li>
<li><em>By characters</em>: when the <em>word</em> mode doesn’t help with jumping to a special operator or character (because it’s not
recognized as a word), this mode allows you to jump to any character in the current or (or all open) buffers.</li>
<li>It has other modes but I have never really found useful cases for them.</li>
</ul>
<p>The way I typically do it is by mapping the three modes to <code>&lt;leader&gt;l</code>, <code>&lt;leader&gt;w&gt;</code> and <code>&lt;leader&gt;c</code> (in my case,
<code>&lt;leader&gt;</code> is the <em>space</em> key).</p>
<p>Typing <code>SPC l</code> in my current buffer results in this:</p>
<p><img src="https://phaazon.net/media/uploads/easymotion_lines.png" alt="EasyMotion lines" /></p>
<p>Typing any highlighted character will make my cursor jump to it. The same applies for words, with <code>SPC w</code>:</p>
<p><img src="https://phaazon.net/media/uploads/easymotion_words.png" alt="EasyMotion words" /></p>
<p>For the <em>character</em> mode, after pressing <code>SPC c</code>, I have to press another character (the one I want to jump to). Let’s
say we want to jump to a <code>#</code> (which is not part of words): <code>SPC c #</code>:</p>
<p><img src="https://phaazon.net/media/uploads/easymotion_chars.png" alt="EasyMotion characters" /></p>
<p>This way of moving is not intuitive at first, but once you get used to it… it’s a <em>must have</em>.</p>
<h2>All the other modal candies</h2>
<p>Among all the things that I like about modal editing, here is a non-exhaustive list of features I expect to have around
my fingers:</p>
<ul>
<li><code>C-i</code> and <code>C-o</code>: those allows me to jump to something / a file / a place in a buffer and then go back to where I was
right before with <code>C-o</code> (read it like <em>out</em>) or go back again with <code>C-i</code> (read it like <em>in</em>).</li>
<li>Macros and registers: those allow me to yank content into different registers (like clipboards) by assigning a single
key to paste their content. For instance, I can put a few lines in my <code>t</code> register with <code>"tyi(</code> (“put in the <code>t</code>
register the <code>y</code>ank <code>i</code>nside matching <code>(</code>), and paste that content later with <code>"tp</code>. Macros allow more powerful
editing control by assigning a key to set of actions with the <code>q</code> keyword (<code>qa</code> will register all the next keystrokes
and actions into the <code>a</code> macro). Then simply replay the macro with <code>@a</code>, for instance.</li>
<li>Obviously, all the basic vim motions, like <code>d</code> to delete, <code>y</code> to yank, <code>c</code> to change, <code>t</code> to go to the character right
before the one you search, <code>%</code> to go to the other delimiter, etc. And more complex text manipulation, such as “Let’s
change what’s inside this function parameter list, delimited by <code>(</code>”: <code>ci(</code>.</li>
</ul>
<p>It would take too much time to list everything, but the main idea is: I need the modal features when editing code.</p>
<h1>The editors</h1>
<p>So let’s talk about the list of editors I mentioned in the preface. The idea is to give <em>my own opinion</em> on those
editors. What I like about them regarding the way I like to edit code and what I think is missing.</p>
<h2>neovim</h2>
<p>I currently use <a href="https://neovim.io">neovim</a> in its <a href="https://en.wikipedia.org/wiki/Text-based_user_interface">TUI</a> version, because it’s the most stable, fast and easy neovim experience out
there so far to me. I have tried several GUI versions but never found what I was looking for — the main reason being
that they pretty much all of them use Web™ technologies, and it’s a hard no to me. I think I should elaborate a bit
more about that last point.</p>
<p>Why not using Web tech:</p>
<ul>
<li>Editing something on my machine has nothing to do with Web technology. This applies to lot of other things, but
truly, running a complex JavaScript VM / CSS engine in an editor seems so wrong on so many levels (performance being
the first one).</li>
<li>Most of the time, editors based on Web tech take ages to load. Yes, even VS Code — remember that my daily editor is
neovim, <strong>which loads in around 23ms</strong> with almost 50 packages installed (you can profile with <code>:profile</code> to get that
value). Among those 23ms, <a href="https://github.com/neoclide/coc.nvim">coc.nvim</a> take around 12ms.</li>
<li>Scripting. Scripting in JavaScript or CoffeeScript is a hard no to me.</li>
<li><code>npm</code> is one of the worst piece of software ever written. Please don’t make me use it again.</li>
</ul>
<h3>My neovim setup</h3>
<p>So I use several plugins that I’m going to describe. I think it’s important that people know about all those
because, in the collective mind, people still think vim / neovim are editors from the past. Well, not really.</p>
<ul>
<li><a href="https://github.com/ryanoasis/vim-devicons">ryanoasis/vim-devicons</a>
<ul>
<li>Add lots of unicode icons that other packages will use to provide a fancier and sexier interface experience.</li>
</ul>
</li>
<li><a href="https://github.com/sainnhe/sonokai">sainnhe/sonokai</a>
<ul>
<li>The current colorscheme I am using. It is very similar to what you get with <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a> — even though it’s a
bit less contrasted.</li>
</ul>
</li>
<li><a href="https://github.com/neovimhaskell/haskell-vim">neovimhaskell/haskell-vim</a>
<ul>
<li>Haskell syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/rust-lang/rust.vim">rust-lang/rust.vim</a>
<ul>
<li>Rust syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/plasticboy/vim-markdown">plasticboy/vim-markdown</a>
<ul>
<li>Markdown support for <a href="https://neovim.io">neovim</a>. By default, <a href="https://neovim.io">neovim</a> already provides a good support for Markdown, but this package
has several cool features, like folds. I might get rid of it as I’m using folds that much.</li>
</ul>
</li>
<li><a href="https://github.com/mzlogin/vim-markdown-toc">mzlogin/vim-markdown-toc</a>
<ul>
<li>A very cool package that gives a way to create table of content in Markdown buffers and let neovim automatically
update the sections when you edit headers.</li>
</ul>
</li>
<li><a href="https://github.com/tikhomirov/vim-glsl">tikhomirov/vim-glsl</a>
<ul>
<li>GLSL syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/cespare/vim-toml">cespare/vim-toml</a>
<ul>
<li>TOML syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/ElmCast/elm-vim">ElmCast/elm-vim</a>
<ul>
<li>Elm syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/idris-hackers/idris-vim">idris-hackers/idris-vim</a>
<ul>
<li>Idris syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/posva/vim-vue">posva/vim-vue</a>
<ul>
<li>Vue.js syntax support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/baskerville/vim-sxhkdrc">baskerville/vim-sxhkdrc</a>
<ul>
<li><a href="https://wiki.archlinux.org/index.php/Sxhkd">sxhkd</a> support for <a href="https://neovim.io">neovim</a>.</li>
</ul>
</li>
<li><a href="https://github.com/norcalli/nvim-colorizer.lua">norcalli/nvim-colorizer.lua</a>
<ul>
<li>A super cool extension that will automatically change the background color of texts containing an hexadecimal
value, such as #f8324F or #42cf69:</li>
<li><img src="https://phaazon.net/media/uploads/nvim-colorizer.png" alt="nvim-colorizer.lua" /></li>
</ul>
</li>
<li><a href="https://github.com/airblade/vim-gitgutter">airblade/vim-gitgutter</a>
<ul>
<li>One of the best plugin I have installed. It provides the <em>gutters</em> you see on the top of your buffer when editing a
file versioned in git (<em>added</em>, <em>modified</em>, <em>deleted</em>, etc.):</li>
<li><img src="https://phaazon.net/media/uploads/vim-gitgutter-editions.png" alt="vim-gitgutter gutters" /></li>
<li>But it can do more, and among all the things it can do, it can help you preview <em>hunks</em>, stage or discard them
<strong>inside</strong> your editor. That’s just a blast to me:</li>
<li><img src="https://phaazon.net/media/uploads/vim-gitgutter-hunk-preview.png" alt="vim-gitgutter hunk preview" /></li>
</ul>
</li>
<li><a href="https://github.com/tpope/vim-fugitive">tpope/vim-fugitive</a>
<ul>
<li>All the features you expect to find from git inside <a href="https://neovim.io">neovim</a>. You can see diff, resolve merge conflicts, write
commit messages, etc. etc. However, I tend to still keep around the command line as I’m not completely solved on
how this plugin deals with <em>fixups</em> and <em>squashing</em> in general.</li>
</ul>
</li>
<li><a href="https://github.com/rhysd/git-messenger.vim">rhysd/git-messenger.vim</a>
<ul>
<li>A blame-at-cursor tool. Not really that useful actually, as fugitive already has a git blame window that will
annotate each line with the commits. I sometimes use this one, but I might get rid of it.</li>
</ul>
</li>
<li><a href="https://github.com/tveskag/nvim-blame-line">tveskag/nvim-blame-line</a>
<ul>
<li>A git blam inlined, at the right part of your lines. Very similar to the default git plugin from VS Code, for
instance.</li>
</ul>
</li>
<li><a href="https://github.com/junegunn/fzf.vim">junegunn/fzf.vim</a>
<ul>
<li>If there is <strong>one</strong> plugin you should install, it’s this one. It has <em>so many</em> features: opening files, git files,
buffers, rip-grepping, searching through history, commands, colorschemes, etc. etc. And as the name implies, it
uses <code>fzf</code> as a backend, so you get a very cool fuzzy search experience (with <em>refined</em> search, which I struggle
to find in <em>any other editor</em> — i.e. you can type something, then put a space and type again to match things
occurring before the actual match).</li>
</ul>
</li>
<li><a href="https://github.com/machakann/vim-highlightedyank">machakann/vim-highlightedyank</a>
<ul>
<li>A funny one: it will highlight the lines / objects you yank for a better visual feedback. Surprising <a href="https://neovim.io">neovim</a>
doesn’t have that by default.</li>
</ul>
</li>
<li><a href="https://github.com/liuchengxu/vista.vim">liuchengxu/vista.vim</a>
<ul>
<li>I use this one from time to time to get a <em>symbol tree</em>, but the output is not really appealing to me right now. I
might ditch it too.</li>
</ul>
</li>
<li><a href="https://github.com/neoclide/coc.nvim">neoclide/coc.nvim</a>
<ul>
<li>The best completion engine for <a href="https://neovim.io">neovim</a>, by far. I used to use others, such as <code>ale</code>, but this one is ace. It
basically provides you with LSP completion for lots of languages. It has an integrated marketplace you can use to
install new LSP servers and integrations, and it even has support for things completely unrelated (a bit
surprising, I think these should be standalone plugins), such as <code>coc-explorer</code> (which is a replacement of
<code>NERDTree</code> to me now), <code>coc-snippets</code>, etc.</li>
</ul>
</li>
<li><a href="https://github.com/tpope/vim-commentary">tpope/vim-commentary</a>
<ul>
<li>Easily comment / uncomment lines without having to insert the comment symbols yourself.</li>
</ul>
</li>
<li><a href="https://github.com/liuchengxu/vim-which-key">liuchengxu/vim-which-key</a>
<ul>
<li><code>which-key</code> from <a href="https://www.gnu.org/software/emacs">emacs</a>, but for <a href="https://neovim.io">neovim</a>. Basically, when setup properly, it will provide you with a visual list
of possible keybindings for sequences. I think it’s not that useful (or it is for people trying to use your
configuration or if you install a plugin that ships a lot of keybindings — which I truly dislike, btw), but it looks
pretty cool.</li>
</ul>
</li>
<li><a href="https://github.com/itchyny/lightline.vim">itchyny/lightline.vim</a>
<ul>
<li>A status line that looks pretty cool.</li>
</ul>
</li>
<li><a href="https://github.com/SirVer/ultisnips">SirVer/ultisnips</a>
<ul>
<li>Snippets support. The snippet engine of <code>ultisnips</code> allows for a lot of room, like interpolation via shell, viml,
python, etc.</li>
</ul>
</li>
<li><a href="https://github.com/honza/vim-snippets">honza/vim-snippets</a>
<ul>
<li>A collection of snippets for commonly used languages and file formats.</li>
</ul>
</li>
<li><a href="https://github.com/junegunn/vim-easy-align">junegunn/vim-easy-align</a>
<ul>
<li>A pretty neat plugin to easily align texts / tables with a few keystrokes only.</li>
</ul>
</li>
<li><a href="https://github.com/liuchengxu/vim-clap">liuchengxu/vim-clap</a>
<ul>
<li>A take on uniting all possible source of search / fuzzy finders in a modern and fast UI. Unfortunately, right now,
the plugin is pretty unstable to me, so I keep using <code>fzf</code> instead.</li>
</ul>
</li>
<li><a href="https://github.com/easymotion/vim-easymotion">easymotion/vim-easymotion</a>
<ul>
<li>I think I have explained what that is about earlier. ;)</li>
</ul>
</li>
</ul>
<h3>What I like about neovim</h3>
<ul>
<li>It’s fast. It really is. It opens immediately. Moving around, scrolling, etc. is smooth, whatever the terminal
I’m using (even though I’m currently using <a href="https://github.com/alacritty/alacritty">alacritty</a>).</li>
<li>The plugins and effort put into <a href="https://neovim.io">neovim</a> are really great. I love <code>vim-gitgutter</code> so much; I love the colorizer
plugin a lot too. <a href="https://github.com/neoclide/coc.nvim">coc.nvim</a> has been a blast so far (for most part). EasyMotion is typingporn to me. <code>fzf</code> is so
fast it should be illegal.</li>
<li><a href="https://neovim.io">neovim</a> has a community that is truly passionated about what they are doing, and new versions add lots of very cool
features that we quickly adopt into new plugins, such as the popup / floating windows / virtual texts for linter
annotations, etc. etc.</li>
<li>It’s lightweight: your RAM will thank you.</li>
<li>Something I haven’t talked about but is a killer-feature of vim / neovim: the <code>:help</code> pages. I think no other
software has such a cool set of help pages. Really, give it a try. You want to know how to configure <a href="https://github.com/neoclide/coc.nvim">coc.nvim</a>?
Simply type <code>:help coc-nvim</code>.</li>
<li>Plugin managers exist (I personally use <a href="https://github.com/junegunn/vim-plug">vim-plug</a> but you will find more), and they make your life so much easier.</li>
</ul>
<h3>What I dislike about neovim</h3>
<ul>
<li>Even though I love how fast the TUI is (I truly haven’t come across anything faster so far), TUIs feel like hacks
to me. For instance, if you split a window into two buffers, the vertical “marks”, “edges”,
whatever-you’d-like-to-call-them, are actual unicode characters. The way terminals work make it possible to
<em>ignore</em> those characters, but still, it feels hacky. If you want something like a minimap or a simply thin frame
around some text, or any kind of visual feedback that is a bit more complicated than simply turning bold mode or
underlying, you’re basically not going to get it.</li>
<li>The GUIs for <a href="https://neovim.io">neovim</a> are… not up to my expectations. Most of them are based on Web tech anyway, so they’re not
good candidates to me. For the rest, like clients based on Qt, they feel a bit abandonned :(. There are people
trying to make new ones, but they are not ready AFAIK.</li>
<li><a href="https://github.com/neoclide/coc.nvim">coc.nvim</a> sometimes feel like is doing the wrong thing. For instance, when editing Java, it timeouts very often
when trying to jump to the definition of a symbol (or simply looking a symbol up). That’s bad.</li>
<li>Changing my colorscheme while the editor is running is a waste of time and generates broken syntax highlighting
everywhere. A pity to me. :(</li>
</ul>
<h2>IntelliJ IDEA</h2>
<p>So I won’t going to be able to talk too much about this one, because I have only started using it at work (community
edition). I’m using the vanilla one, with few modifications to none. I edit only Java with it.</p>
<h3>What I like about IntelliJ IDEA</h3>
<p>The Java support is truly flawless. It does a lot of things for you, and among them, I was really impressed by:</p>
<ul>
<li>The refactor mechanism that would allow me to select a block of code inside a function, ask the editor to “move
that into a dedicated function”. So far, all editors can do that, but what impressed me is that <a href="https://www.jetbrains.com/idea">IntelliJ IDEA</a> was
able to find out which variables needed to be captured and put as arguments to the function, then automatically pass
them at call site, when replacing the block of moved code. Really neat.</li>
<li>The speed at which you can look for symbols, look for implemented functions, inherited classes, super classes, etc.
It’s all instant and it’s all very well presented to you. Me gusta.</li>
<li>The syntax highlighting is okay; I especially like the inlined type ascriptions for both <code>var</code> declarations but also
when passing arguments to functions.</li>
</ul>
<h3>What I dislike about IntelliJ IDEA</h3>
<ul>
<li>I’m using the community edition, which only supports Java and a bunch of other configuration languages. You don’t
get the profiler, for instance — which would be very well appreciated!</li>
<li>Even though the asynchronous part of the editor is impressive, it sometimes index projects at random times and if
you’re working on a laptop, brace yourself for Mars landing as the machine is lifting off! — i.e. it makes a hella
noise and heats up quickly.</li>
<li>I tried the Vim integration and I wasn’t able to use it correctly with my bépo keymap. Unable to remap some motion
and/or mode switches. I disabled it and cried deeply.</li>
<li>Without Vim support, even though the editor has a lot of shortcuts, it feels like you still have to use the mouse
to perform very basic tasks.</li>
</ul>
<h2>VS Code</h2>
<p>So this one is an important one, as it’s Microsoft’s take on editing. Everybody seems to love VS Code, and I get
why. The UI is slick and fast — for a Web-based editor… ;). LSP support is obviously premium and flawless. It has a
lot of community plugins, themes and integrations. For most part, even though it’s Web based, I have been enjoying
it.</p>
<h3>What I like about VS Code</h3>
<ul>
<li>The slickness of the editor / UI.</li>
<li>Language support is premium with LSP and editing with it feels very solid.</li>
<li>Millions of plugins.</li>
<li>Big community and most of people use it nowadays, so I guess that if you get any trouble, you can ask around?</li>
</ul>
<h3>What I dislike about VS Code</h3>
<ul>
<li>The fact it’s written with a Web tech. Among all editors Web-based, it’s the fastest, but still, if you’re used
to vim / <a href="https://neovim.io">neovim</a>, you’ll be dissatisfied with it.</li>
<li>The vim integration is poor / doesn’t work (I tried remapping <code>h</code> to <code>c</code> — remember, <em>bépo</em> keyboard layout). It
simply doesn’t work.</li>
</ul>
<h2>emacs and DOOM emacs</h2>
<p>I have been using <a href="https://www.gnu.org/software/emacs">emacs</a> lately (vanilla) as I saw a colleague using <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a>. I’m putting both in the same
section as they are pretty similar. The way I see <a href="https://www.gnu.org/software/emacs">emacs</a> and <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a> could be summed up with one word: united.
I don’t know how they do that, but all plugins blend so well with each other. I’m using the <em>ivy</em> interface for
completion and fuzzy search, and everything is so… well done. The UI is gorgeous, themes are awesome (I like the
default dark theme, <em>DOOM One</em>), the editor is pretty fast — still slower than <a href="https://neovim.io">neovim</a> to me, especially when
scrolling, but still much faster than a Web-based editor.</p>
<h3>What I like about emacs / DOOM emacs</h3>
<ul>
<li>Once setup correctly (better defaults, etc.), the editor feels very modern (like what you would find in
<a href="https://code.visualstudio.com">VS Code</a> / <a href="https://atom.io">atom</a>). It feels <em>slick</em> and well designed.</li>
<li>Evil-mode, the Vim mode, is to me the best out there (besides vim and neovim themselves, obviously). They cover
almost everything — they even have support for EasyMotion!</li>
<li>You script / configure it with <a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a>, which is… super great! <a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> is that kind of old treasure that has been
around forever and still feels like something new. I really love it.</li>
<li>If you are using <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a>, you are going to get a lot of candies for free. Its modules approach works pretty
well and provides a very innovative way to enable / disable features. There are tons of resources out there to
get into <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a> and I highly suggest having a look, even if you don’t plan on using <a href="https://www.gnu.org/software/emacs">emacs</a> or <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a>.
That’s how I discovered <code>which-key</code>, for instance — that I now use in <a href="https://neovim.io">neovim</a>.</li>
<li>The LSP integration is pretty well done. It will download for you the servers and if you open a file that you have
never gotten the server for before, it will gently ask you whether you’d like to.</li>
<li>On a more general note, both <a href="https://www.gnu.org/software/emacs">emacs</a> and <a href="https://github.com/hlissner/doom-emacs">DOOM emacs</a> feel more <em>interactive</em> than editors like vim or <a href="https://neovim.io">neovim</a>,
which I think is a definitive better approach.</li>
<li>It uses <a href="https://www.gtk.org">gtk</a> as main backend on Linux. I think it’s important to note it, because it’s not
Web-based! (yay!)</li>
<li>Magit is a wonderful tool.</li>
<li>Org-mode is really great too, even though I think it’s a bit too big to me.</li>
<li>The <em>daemon</em> mode is amazing and I think all editors should have it. It allows you to start an instance of <a href="https://www.gnu.org/software/emacs">emacs</a>
and connect <code>emacsclient</code> to it, reducing the loading time to zero. Very brilliant and very useful!</li>
</ul>
<h3>What I dislike about emacs / DOOM Emacs</h3>
<ul>
<li>Moving across a huge amount of code leads to stuttering and sometimes feels a bit meh, especially if you’re used to
vim / <a href="https://neovim.io">neovim</a>. Most of the time, it should be okay, but just keep in mind that scrolling in <a href="https://www.gnu.org/software/emacs">emacs</a> has always been
a problem.</li>
<li>Even though it could be seen as a positive point, I think that all the great plugins <a href="https://www.gnu.org/software/emacs">emacs</a> has make it very
bloated, and that’s a problem to me. For instance, Org-mode, which is a wonderful piece of software, should have
benefited much more people if it was a standalone application. To me, it feels like starting using <a href="https://www.gnu.org/software/emacs">emacs</a> will lead
to using your computer to run <a href="https://www.gnu.org/software/emacs">emacs</a>, and all your applications inside <a href="https://www.gnu.org/software/emacs">emacs</a>. It even has an IRC plugin and an
email reader plugin!</li>
<li>I’m not sure what’s happening with this one, but LSP servers feel… synchronous?! When opening a file for the
first time, the LSP server starts and you have to wait a few seconds before being able to jump into the file. I don’t
really know whether it’s a configuration thing, but it doesn’t feel very pleasant.</li>
<li>The defaults of <a href="https://www.gnu.org/software/emacs">emacs</a> are really, <em>really</em> bad. And making the whole thing like what you get with <a href="https://github.com/hlissner/doom-emacs">DOOM Emacs</a> is
going to cost you lots of hours reading documentation and experimenting with your configuration. I enjoyed doing it
but in the end… why simply not ship <a href="https://www.gnu.org/software/emacs">emacs</a> with those defaults? Is it due to historical reasons that no one cares
about anymore nowadays? <em>/stares at vim</em></li>
</ul>
<h2>atom</h2>
<p>I’ll finish with <a href="https://atom.io">atom</a>, <a href="https://github.com">GitHub</a>’s editor. I remember my first reaction when running <a href="https://atom.io">atom</a> for the first time
was: “Woah, that’s a pretty editor.” The default colorscheme, <em>One</em>, is a universal colorscheme everybody knows.
There are have been so many forks of it in so many different editors.</p>
<p><a href="https://atom.io">atom</a> looks a lot like <a href="https://code.visualstudio.com">VS Code</a> to me, but is a bit prettier in its UI — I do prefer <a href="https://atom.io">atom</a>’s UI to <a href="https://code.visualstudio.com">VS Code</a>’s.
The UI is slick and very clean. You can find a lot of extensions / plugins / themes, from LSP integrations to
Markdown previews and Vim modes.</p>
<h3>What I like about atom</h3>
<ul>
<li>The killer feature of <a href="https://atom.io">atom</a> to me is its ability to tell you what commands are associated (or erased) for a
keybinding you are pressing, live. It is very practical to debug keybinding issues and I wish more editors
had it. Other editors feature <em>similar</em> stuff, but not quite as good as this <em>echo mode</em> for keybinding.</li>
<li>The themes are pretty cool and the whole typing experience / completion is pretty solid and consistent.</li>
<li>Lots of plugins to play with.</li>
</ul>
<h3>What I dislike about atom</h3>
<ul>
<li>Vim mode. Once again, it’s not complete to me as it doesn’t support well my bépo keyboard layout. Worse, they
have a weird bug with <code>alt-gr</code> (they call it <code>altgraph</code> in the config), that is not correctly recognized. It
sometimes work but I never recall the steps I have to do to fix the problem, and most of the time, I spend a
lot of times finding workarounds for something that just works in terminals.</li>
<li>It’s slow af. You can feel the whole Web with you! :D</li>
<li>Sometimes, after an update, a plugin will break, and you will basically lose a feature. This is a problem that
I have observed with other Web-based softwares (such as the GNOME Desktop Environment), which makes me doubt
more and more that kind of tech choice.</li>
</ul>
<h1>Wrap it up</h1>
<p>When I started coding, I remember of people talking about IDE / editor wars. Today, as I have tried a lot of editors,
I can say that there’s no such thing as an editor war to me. All editors have drawbacks and picking the right editor
is often a matter of taste and personal experience. I’m a keyboard lover (I make my own keyboards) and I truly love
typing — not necessarily code, hence it was pretty obvious to go to <a href="https://www.gnu.org/software/emacs">emacs</a> and vim back then (I actually started
coding in <a href="https://www.gnu.org/software/emacs">emacs</a>). Several years later, I joined the vim side and <a href="https://neovim.io">neovim</a>. It’s only one year ago that I started
looking at <a href="https://www.gnu.org/software/emacs">emacs</a> again to see what has changed. And oh my. So many interesting things out there!</p>
<p>What I like about testing editors is that each and every editor has at least one killer feature no other has:</p>
<ul>
<li>vim and neovim have modal editing and are super fast. They have been the editors of choice for modal editing for
decades and all the motions, macros, commands and mnemonics are implemented the best in those two editors.</li>
<li>IntelliJ IDEA has, to me, the best Java experience out there, with impressive (and so useful!) refactoring features.
It’s not something you’d be absolutely need to be productive, but it will make you feel much comfortable working on
your Java codebase, and I truly wish a plugin existed for that in the editors I use!</li>
<li>VS Code has the best LSP implementation and has — I guess? — on the biggest community. If you like Web-based
editors, don’t seek no more: it’s the right editor for you.</li>
<li>emacs and DOOM emacs have that slick, united interface with lots of amazing plugins and applications. You’ll
completely adore Org-mode, Magit and lots of other plugins!</li>
<li>atom has that echo mode for keybindings, superb defaults for theming and syntax highlighting and is one of — if not
the most? — the friendliest editors out there.</li>
</ul>
<p>Spending some weeks in all those editors made me realize something about vim / neovim: I don’t necessarily think I
should be using them, especially since I have used emacs / DOOM Emacs with its Evil-mode. Today, my thoughts revolve
around the idea that a good neovim client could be a gtk application like emacs: slick, united, with great defaults
and full support of neovim features, with support for gtk floating windows and popups (as it’s native in neovim and
feels a bit hacky in TUI). We already have great plugins for things like git (fugitive / vim-gitgutter), completion
and syntax highlighting with coc.nvim / vim-lsp / vim-treesitter. The only piece that is missing to me is a great
GUI to leverage the “hacks” we have to do in TUI to actually use real popups, “balloons”, etc. Once we get a decent
neovim GUI, I think I will have found my favorite editor of all time.</p>
<p>Until then, I’ll stick around neovim TUI as it’s what is as close at what I would like to get. I hope this article
will have given a bit of hints about what a vim / neovim enthusiast might expect from a modern editor. And I say
<em>a vim enthusiast</em>, not <em>all of them</em>. We all look for different things and I truly thing we live in a wonderful world
with all those editors out there. They won’t fit for everybody, but everybody will find one for them, and damn, that’s
a pretty good conclusion to this article.</p>
<p><a href="https://www.reddit.com/r/programming/comments/inx2ul/my_thoughts_about_editors_in_2020">Feel free to tell me</a>
about what you think about your editor, whether there’re things you dislike about it or what you would like to see
appear in it. Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 07 Sep 2020 00:03:00 GMT</pubDate></item><item><title>luminance designs</title><link>https://strongly-typed-thoughts.net/blog/luminance_design</link><description><![CDATA[<p><a href="https://crates.io/crates/luminance/0.7.0">luminance-0.7.0</a> was released a few days ago and I
decided it was time to explain exactly what luminance is and what were the design choices I made.
After a very interesting talk with <a href="https://github.com/nical">nical</a> about other rust graphics
frameworks (e.g. <a href="https://crates.io/crates/gfx">gfx</a>, <a href="https://crates.io/crates/glium">glium</a>,
<a href="https://crates.io/crates/vulkano">vulkano</a>, etc.), I thought it was time to give people some more
information about luminance and how to compare it to other frameworks.</p>
<h1>Origin</h1>
<p>luminance started as <a href="https://hackage.haskell.org/package/luminance">a Haskell package</a>, extracted
from a <em>“3D engine”</em> I had been working on for a while called
<a href="https://github.com/phaazon/quaazar">quaazar</a>. I came to the realization that I wasn’t using the
Haskell garbage collector at all and that I could benefit from using a language without GC. Rust
is a very famous language and well appreciated in the Haskell community, so I decided to jump in and
learn Rust. I migrated luminance in a month or two. The mapping is described in
<a href="http://phaazon.blogspot.fr/2016/04/porting-haskell-graphics-framework-to.html">this blog entry</a>.</p>
<h1>What is luminance for?</h1>
<p>I’ve been writing 3D applications for a while and I always was frustrated by how OpenGL is badly
designed. Let’s sum up the lack of design of OpenGL:</p>
<ul>
<li><em>weakly typed</em>: OpenGL has types, but… it actually does not. <code>GLint</code>, <code>GLuint</code> or <code>GLbitfield</code> are
all defined as <em>aliases</em> to primary and integral types (i.e. something like
<code>typedef float GLfloat</code>). Try it with <code>grep -Rn "typedef [a-zA-Z]* GLfloat" /usr/include/GL</code>. This
leads to the fact that <em>framebuffers</em>, <em>textures</em>, <em>shader stages</em>, <em>shader program</em> or even
<em>uniforms</em>, etc. have the same type (<code>GLuint</code>, i.e. <code>unsigned int</code>). Thus, a function like
<code>glCompileShader</code> expects a <code>GLuint</code> as argument, though you can pass a framebuffer, because it’s
also represented as a <code>GLuint</code> – very bad for us. It’s better to consider that those are just
untyped – :( – handles.</li>
<li><em>runtime overhead</em>: Because of the point above, functions cannot assume you’re passing a value of
a the expected type – e.g. the example just above with <code>glCompileShader</code> and a framebuffer. That
means OpenGL implementations have to check against <em>all</em> the values you’re passing as arguments to
be sure they match the type. That’s basically <strong>several tests for each call</strong> of an OpenGL
function. If the type doesn’t match, you’re screwed and see the next point.</li>
<li><em>error handling</em>: This is catastrophic. Because of the runtime overhead, almost all functions
might set the <em>error flag</em>. You have to check the error flag with the <code>glGetError</code> function,
adding a side-effect, preventing parallelism, etc.</li>
<li><em>global state</em>: OpenGL works on the concept of global mutation. You have a state, wrapped in a
<em>context</em>, and each time you want to do something with the GPU, you have to change something in
the context. Such a context is important; however, some mutations shouldn’t be required. For
instance, when you want to change the value of an object or use a texture, OpenGL requires you to
<em>bind</em> the object. If you forget to <em>bind</em> for the next object, the mutation will occurs on the
first object. Side effects, side effects…</li>
</ul>
<p>The goal of luminance is to fix most of those issues by providing a safe, stateless and elegant
graphics framework. It should be as low-level as possible, but shouldn’t sacrifice
runtime performances – CPU charge as well as memory bandwidth. That is why if you know how to
program with OpenGL, you won’t feel lost when getting your feet wet with luminance.</p>
<p>Because of the many OpenGL versions and other technologies (among them, vulkan), luminance has an
extra aim: abstract over the trending graphics API.</p>
<h1>Types in luminance</h1>
<p>In luminance, all graphics resources – and even concepts – have their own respective type. For
instance, instead of <code>GLuint</code> for both shader programs and textures, luminance has <code>Program</code> and
<code>Texture</code>. That ensures you don’t pass values with the wrong types.</p>
<blockquote>
<p>Because of static warranties provided by compile-time, with such a scheme of strong-typing, the
runtime <strong>shouldn’t have to check for type safety</strong>. Unfortunately, because luminance <em>wraps over</em>
OpenGL in the luminance-gl backend, we can only add static warranties; we cannot remove the
runtime overhead.</p>
</blockquote>
<h1>Error handling</h1>
<p>luminance follows the Rust conventions and uses the famous <code>Option</code> and <code>Result</code> types to specify
errors. You will never have to check against a global error flag, because this is just all wrong.
Keep in mind, you have the <code>try!</code> macro in your Rust prelude; use it as often as possible!</p>
<blockquote>
<p>Even though Rust needs to provide an exception handler – i.e. panics – there’s no such thing as
exceptions in Rust. The <code>try!</code> macro is just syntactic sugar to:</p>
</blockquote>
<blockquote>
<pre><code class="language-rust">match result {
  Ok(x) =&gt; x,
  Err(e) =&gt; return e
}
</code></pre>
</blockquote>
<h1>Stateless</h1>
<p>luminance is stateless. That means you don’t have to bind an object to be able to use it. luminance
takes care of that for you in a very simple way. To achieve this and keep performances running, it’s
required to add a bit of high-level to the OpenGL API by leveraging how binds should happen.</p>
<p>Whatever the task you’re trying to reach, whatever computation or problem, it’s always better to
gather / group the computation by batches. A good example of that is how magnetic hard drive disks
work or your RAM. If you spread your data across the disk region (fragmented data) or across several
non-contiguous addresses in your RAM, it will end up by unnecessary moves. The hard drive’s head
will have to go all over the disk to gather the information, and it’s very likely you’ll destroy the
RAM performance (and your CPU caches) if you don’t put the data in a contiguous area.</p>
<p>If you don’t group your OpenGL resources – for instances, you render 400 objects with shader A, 10
objects with shader B, then 20 objects with shader A, 32 objects with shader C, 349 objects with
shader A and finally 439 objects with shader B, you’ll add more OpenGL calls to the equation – hence
more global state mutations, and those are costly.</p>
<p>Instead of this:</p>
<ol>
<li>400 objects with shader A</li>
<li>10 objects with shader B</li>
<li>20 objects with shader A</li>
<li>32 objects with shader C</li>
<li>348 objects with shader A</li>
<li>439 objects with shader B</li>
</ol>
<p>luminance <strong>forces</strong> you to group your resources like this:</p>
<ol>
<li>400 + 20 + 348 objects with shader A</li>
<li>10 + 439 objects with shader B</li>
<li>32 objects with shader C</li>
</ol>
<p>This is done via types called <code>Pipeline</code>, <code>ShadingCommand</code> and <code>RenderCommand</code>.</p>
<h2>Pipelines</h2>
<p>A <code>Pipeline</code> gathers shading commands under a <code>Framebuffer</code>. That means that all <code>ShadingCommand</code>
embedded in the <code>Pipeline</code> will output to the embedded <code>Framebuffer</code>. Simple, yet powerful, because
we can <em>bind</em> the framebuffer when executing the pipeline and don’t have to worry about framebuffer
until the next execution of another <code>Pipeline</code>.</p>
<h2>ShadingCommand</h2>
<p>A <code>ShadingCommand</code> gathers render commands under a shader <code>Program</code> along with an update function.
The update function is used to customize the <code>Program</code> by providing <em>uniforms</em> – i.e. <code>Uniform</code>.
If you want to change a <code>Program</code>s <code>Uniform</code> once a frame – and only if the <code>Program</code> is only called
once in the frame – it’s the right place to do it.</p>
<p>All <code>RenderCommand</code> embedded in the <code>ShadingCommand</code> will be rendered using the embedded shader
<code>Program</code>. Like with the <code>Pipeline</code>, we don’t have to worry about binding: we just have to use the
embedded shader program when executing the <code>ShadingCommand</code>, and we’ll bind another program the next
time a <code>ShadingCommand</code> is ran!</p>
<h2>RenderCommand</h2>
<p>A <code>RenderCommand</code> gathers all the information required to render a <code>Tessellation</code>, that is:</p>
<ul>
<li>the blending equation, source and destination blending factors</li>
<li>whether the depth test should be performed</li>
<li>an update function to update the <code>Program</code> being in use – so that each object can have different
properties used in the shader program</li>
<li>a reference to the <code>Tessellation</code> to render</li>
<li>the number of instances of the <code>Tessellation</code> to render</li>
<li>the size of the rasterized points (if the <code>Tessellation</code> contains any)</li>
</ul>
<h1>What about shaders?</h1>
<p>Shaders are written in… the backend’s expected format. For OpenGL, you’ll have to write <strong>GLSL</strong>.
The backends automatically inserts the version pragma (<code>#version 330 core</code> for OpenGL 3.3 for
instance). In the first place, I wanted to migrate <strong>cheddar</strong>, my Haskell shader EDSL. But… the sad
part of the story is that Rust is – yet – unable to handle that kind of stuff correctly. I started
to implement an EDSL for luminance with macros. Even though it was usable, the error handling is
seriously terrible – macros shouldn’t be used for such an important purpose. Then some rustaceans
pointed out I could implement a (rustc) compiler plugin. That enables the use of new constructs
directly into Rust by extending its syntax. This is great.</p>
<p>However, with the hindsight, I will not do that. For a very simple reason. luminance is, currently,
simple, stateless and most of all: it works! I released a PC demo in Köln, Germany using luminance
and a demoscene graphics framework I’m working on:</p>
<p><a href="http://www.pouet.net/prod.php?which=67966">pouët.net link</a></p>
<p><a href="https://www.youtube.com/watch?v=pYqZS1C_7PU">youtube capture</a></p>
<p><a href="https://github.com/phaazon/ion">ion demoscene framework</a></p>
<p>While developping Céleri Rémoulade, I decided to bake the shaders directly into Rust – to get used
to what I had wanted to build, i.e., a shader EDSL. So there’re a bunch of constant <code>&amp;'static str</code>
everywhere. Each time I wanted to make a fix to a shader, I had to leave the application, make the
change, recompile, rerun… I’m not sure it’s a good thing. Interactive programming is a very good
thing we can enjoy – yes, even in strongly typed languages ;).</p>
<p>I saw that <a href="https://crates.io/crates/gfx">gfx</a> doesn’t have its own shader EDSL either and requires
you to provide <strong>several shader implementations (one per backend)</strong>. I don’t know; I think it’s not
that bad if you only target a single backend (i.e. OpenGL 3.3 or Vulkan). Transpiling shaders is a
thing, I’ve been told…</p>
<blockquote>
<p><em>sneaking out…</em></p>
</blockquote>
<p>Feel free to dig in the code of Céleri Rémoulade <a href="https://github.com/phaazon/celeri-remoulade">here</a>.
It’s demoscene code, so it had been rushed on before the release – read: it’s not as clean as I
wanted it to be.</p>
<p>I’ll provide you with more information in the next weeks, but I prefer spending my spare time
writing code than explaining what I’m gonna do – and missing the time to actually do it. ;)</p>
<p>Keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 28 Aug 2016 00:00:00 GMT</pubDate></item><item><title>Easy interpolation across splines</title><link>https://strongly-typed-thoughts.net/blog/splines-introduction</link><description><![CDATA[<p>This article introduces the <a href="https://crates.io/crates/splines">splines</a> crate.</p>
<h1>Preliminary work</h1>
<p>I’ve been using interpolation (linear, bilinear, trilinear, B-splines, etc.) for a while now because
of my demoscene productions. My <a href="https://crates.io/crates/spectra">spectra</a> crate features some
interpolation primitives and I’ve been wanting to move them out of spectra for a while.</p>
<p>Basically, my need is simple:</p>
<ul>
<li>I use interpolation for <em>a lot</em> of situations: animation, procedural generation, editing,
transitions, etc.</li>
<li>I need several interpolation implicit methods / spaces:
<ul>
<li>Constant/step.</li>
<li>Linear / bilinear / trilinear.</li>
<li>Cosine.</li>
<li>Cubic Hermite spline.</li>
</ul>
</li>
<li>I also need splines with control points (knots):
<ul>
<li>Catmull-Rom.</li>
<li>Bézier.</li>
</ul>
</li>
</ul>
<p>That’s a lot of things. For what it’s worth, I’ll just use as reference a library I wrote years ago
in Haskell, <a href="https://hackage.haskell.org/package/smoothie">smoothie</a>, doing exactly that.</p>
<p>However, because I didn’t want to bloat the ecosystem, I had a look around to see whether I could
simply drop my code and add a simple dependency instead of extracting things from spectra and
creating, again, new crates. Here are the reasons why.</p>
<h2><code>bspline</code></h2>
<p>This crate is about <a href="https://en.wikipedia.org/wiki/B-spline">B-spline</a> only and the interface is highly unsafe (panics if out of control
points for instance). My current spectra code is already safer than that, so no reason to lose
features here.</p>
<h2><code>spline</code></h2>
<p>This is… a… placeholder crate – i.e. it doesn’t have anything exposed. People do this to book a
crate name for later. I highly dislike that and people doing this should really be warned not to do
this, because they prevent others – who actually have some code to put there – from using the name.
Plus, for this <code>spline</code> crate, I had already have a look years ago. In years, the so-called “author”
hasn’t even uploaded anything and the name is just reserved… for nothing.</p>
<p>I highly suggest people from the Rust team to forbid people from doing this. This is just useless,
childish and brings zero value to the community. Just remove that crate as no one has ever had <em>the
chance</em> to ever depend on it.</p>
<h2><code>trajectory</code></h2>
<p>This is an interesting crate, but it’s way too much specialized to me. It could be very interesting
to use it for camera and objects paths, but I also need splines for a lot of other situations, so
this is too much restrictive (you can see functions like <code>acceleration</code>, <code>position</code>, <code>velocity</code> that
works only for <code>T: Float</code>).</p>
<h2><code>nbez</code></h2>
<p>A – looks like – very comprehensive Bezier crate, but doesn’t provide anything for all the other
interpolation I need.</p>
<h1>Let’s build yet another crate!</h1>
<p>Looks like the crate I’m looking for doesn’t exist yet – oh yes it does: it’s spectra!, but I want
a crate that does interpolation only.</p>
<p>I could be using some of the crates listed above, add them as a bunch of dependencies to a <em>glue
crate</em> and just expose the whole thing. However, I’m not sure they will compose quite correctly and
I might just end up re-coding the whole thing.</p>
<p>So the crate I’ll be building will be about interpolation. You will find step (constant)
interpolation, linear / bilinear / trilinear interpolation, cosine, cubice, Catmull-Rom and Bézier
spline interpolations. The crate will be as abstract as possible to enable developers to plug in
their own types. Because the <code>spline</code> name is already taken – duh! – I’ll use the <code>splines</code> name…</p>
<blockquote>
<p>I’ll take my chance to try to take over <code>spline</code> first, though. I really don’t like that kind of
things in an ecosystem, it’s just unaesthetic.</p>
</blockquote>
<blockquote>
<p>Note²: Ok, I tried, and seems like it’s more complicated than I thought. I’ll just go with the
<code>splines</code> name then.</p>
</blockquote>
<p>So this blogpost serves as an introduction to <a href="https://crates.io/crates/splines">splines</a>. I added a few unit tests to start off with
and a very few examples in the documentation. Most of the interface is <em>sufficient</em> for now but more
complex needs might show up. If you have any, please shoot an issue or even better, open a PR!</p>
<p>Keep the vibes and happy hacking!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 05 Aug 2018 19:00:00 GMT</pubDate></item><item><title>HID and MSI keyboards</title><link>https://strongly-typed-thoughts.net/blog/hid_and_msi_keyboards</link><description><![CDATA[<h1>MSI keyboards</h1>
<p>I have a <a href="http://www.mobiletechreview.com/notebooks/MSI-GS60-2QE-Ghost-Pro.htm">MSI GS60 Ghost Pro 2QE</a>
I’m very proud of. It’s sexy and powerful. It comes with a fancy and
configurable backlit keyboard.</p>
<p><img src="http://www.ittipexplorer.info/wp-content/uploads/2014/04/wpid-msigs60ghostpro-8-580-90.jpg" alt="" /></p>
<p>There’s a tool called <em>SteelSeries Engine</em> for Windows we can use to change the
colors of the keyboard. It supports several features:</p>
<ul>
<li>changing colors of three parts of the keyboard (<em>left</em>, <em>middle</em> and <em>right</em>) ;</li>
<li>changing modes (<em>normal</em>, <em>breathe</em>, <em>wave</em>, <em>demo</em>, <em>gaming</em>).</li>
</ul>
<p>Unfortunately, that software doesn’t work on Linux, even with wine. I tried hard
to make it work and never actually found a way to run it. Then, I decided to
look for alternatives and… found nothing <em>working</em>.</p>
<p>Yesterday, I tried a <strong>node.js-powered</strong> tool called
<a href="https://github.com/wearefractal/msi-keyboard">msi-keyboard</a>. And it worked.
However, the interface and user interface was not my cup of tea. I decided to
dig in in order to understand how it works, and I decided to write my own tool
with a decent interface.</p>
<h2>HID access</h2>
<p>The key idea is that such keyboards are just
<a href="https://en.wikipedia.org/wiki/Human_interface_device">HID devices</a>. As a
<strong>Haskell</strong> programmer, I looked for something that would grant me access to
such devices, but nothing was working. There’s a
<a href="https://hackage.haskell.org/package/hidapi">hidapi</a> package, but it doesn’t
work and has bugs.</p>
<p>I didn’t give up though. I wrote my own <strong>Haskell HID API</strong> binding, called
<a href="https://hackage.haskell.org/package/hid">hid</a>. Several good things about it:</p>
<ul>
<li>it does work ;</li>
<li>it’s simple ;</li>
<li>I lately wrote a software using it.</li>
</ul>
<p>Feel free to install and use it!</p>
<h2>msi-kb-backlit</h2>
<p>Then, I wrote another <strong>Haskell</strong> package,
<a href="http://hackage.haskell.org/package/msi-kb-backlit">msi-kb-backlit</a>. It might
require <em>super user rights</em> to work. If you’re not a <strong>Haskeller</strong>, you can
find installation details <a href="https://github.com/phaazon/msi-kb-backlit">here</a>.</p>
<p><strong>Note: if you use Archlinux, you can directly download msi-kb-backlit through
the AUR! Search for msi-kb-backlit with yaourt, or download
<a href="https://aur4.archlinux.org/packages/msi-kb-backlit">the tarball</a>.</strong></p>
<p>The software has an embedded documentation to help you tweak with colors and
modes. ;)</p>
<p>Feel free to use all those pieces of software. I made them with love for you
all!</p>
<p>Enjoy your week end, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 20 Jun 2015 00:00:00 GMT</pubDate></item><item><title>Rust GLSL crate</title><link>https://strongly-typed-thoughts.net/blog/glsl_crate</link><description><![CDATA[<p>It’s been almost two months that I’ve been working on my <a href="https://crates.io/crates/glsl">glsl</a> crate. This crate
exposes a GLSL450 compiler that enables you to parse GLSL-formatted sources into memory in the form of an
AST. Currently, that AST is everything you get from the parsing process – you’re free to do whatever you want
with it. In the next days, I’ll write a GLSL writer (so that I can check that I can parse GLSL to GLSL…). I’d
love to see contributions from Vulkan people to write a SPIR-V backend, though!</p>
<p>Just for the record, the initial goal I had in mind was to parse a subset of GLSL for my
<a href="https://github.com/phaazon/spectra">spectra</a> demoscene framework. I’ve been planning to write my own GLSL-based
shading language (with modules, composability, etc. etc.) and hence I need to be able to parse GLSL sources.
Because I wanted to share my effort, I decided to create a dedicated project and here we are with a GLSL crate.</p>
<p>Currently, you can successfully parse a GLSL450-formatted source (or part of it, I expose all the intermediary
parsers as well as it’s required by my needs to create another shading language over GLSL). See for instance
<a href="https://gist.github.com/phaazon/fbe7a9c26bdea4a7262d2ea028c578ce">this shading snippet</a> parsed to an AST.</p>
<p>However, because the project is still very young, there a lot of features that are missing:</p>
<ul>
<li>I followed the <a href="https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.50.pdf">official GLSL450 specifications</a>,
which is great, but the types are not very intuitive – some refactoring must be done here;</li>
<li>I use <a href="https://crates.io/crates/nom">nom</a> as a parser library. It’s not perfect but it does its job very well.
However, error reporting is pretty absent right now (if you have an error in your source, you’re
basically left with <code>Error(SomeVariant)</code> flag, which is completely unusable;</li>
<li>I wrote about 110 unit tests to ensure the parsing process is correct. However, there’s for now zero semantic
check. Those are lacking.</li>
<li>About the semantic checks, the crate is also missing a semantic analysis. I don’t really know what to do here,
because it’s a lot of work (like, ensure that the assigned value to a float value is a real float and not a
boolean or ensure that a function that must returns a vec3 returns a vec3 and not something else, etc.). This
is not a trivial task and because this is already done by the OpenGL drivers, I won’t provide this feature
yet. However, to me, it’d be a great value.</li>
</ul>
<p>If you’re curious, you can start using the crate with <code>glsl = "0.2.1"</code> in your <code>Cargo.toml</code>. You probably want
to use the <code>translation_unit</code> parser, which is the most external parser (it parses an actual shader). If you’re
looking for something similar to my need and want to parse subparts of GLSL, feel free to dig in the
<a href="https://docs.rs/glsl">documentation</a>, and especially the <code>parsers</code> module, that exports all the parsers
available to parse GLSL’s parts.</p>
<p>Either way, you have to pass a bytes slice. If your source’s type is <code>String</code> or <code>&amp;str</code>, you can use the
<code>str::as_bytes()</code> function to feed the input of the parsers.</p>
<blockquote>
<p>Not all parsers are exported, only the most important ones. You will not find an octal parser, for instance,
while it’s defined and used in the crate internally.</p>
</blockquote>
<h1>Call to contribution</h1>
<p>If you think it’s worth it, I’m looking for people who would like to:</p>
<ul>
<li>write a GLSL to SPIR-V writer: it’s an easy task, because you just have to write a function of the
form <code>AST -&gt; Result&lt;SPIRV, _&gt;</code>, for being pragmatic;</li>
<li>test the crate and provide feedback about any error you’d find! Please do not open an issue if you have
an error in your source and find <em>“the error from the parsers is not useful”</em>, because it’s already
well established this is a problem and I’m doing my best to solve it;</li>
<li>any kind of contributions you think could be interesting for the crate.</li>
</ul>
<p>Happy coding, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 30 Jul 2017 00:00:00 GMT</pubDate></item><item><title>glsl-0.13 and its visitor pattern</title><link>https://strongly-typed-thoughts.net/blog/glsl-0.13-visitor</link><description><![CDATA[<blockquote>
<p>This article serves as an announcement for <a href="https://crates.io/crates/glsl/0.13.2">glsl-0.13</a>.</p>
</blockquote>
<p>I’ve been working on <a href="https://crates.io/crates/glsl/0.13.2">glsl-0.13</a> for a few days now since my <a href="https://phaazon.net/blog/glsl-pest-part-2">previous blog entry</a> about adding
<a href="https://crates.io/crates/pest">pest</a> to <a href="https://crates.io/crates/glsl">glsl</a> – and no, the current code is not pest-based, it’s still good old <a href="https://crates.io/crates/nom">nom</a>. Lately,
I’ve been wanting to enhance my <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a> crate to add <a href="https://en.wikipedia.org/wiki/String_interpolation">variable interpolation</a>. I think I
will dedicate a whole article to <em>variable interpolation</em> in GLSL because it’s actually tricky to
get done right – without duplicating a whole parser or introducing problematic code with pest (see
my last article).</p>
<p>Since I’m not a programmer who solves problems that don’t exist, I have problems to get resolved in
other crates and binary projects of mine (mostly demoscene and demoscene tooling). However, I’ve
been spending days solving those problems because they’re not related to demoscene only: pretty much
anyone who would like to cope with shaders might be interested.</p>
<h1>The <a href="https://crates.io/crates/glsl">glsl</a> crate prior to 0.13</h1>
<p>The <a href="https://crates.io/crates/glsl">glsl</a> crate provides you with:</p>
<ul>
<li>A GLSL450 parser. I wrote the parser with <a href="https://crates.io/crates/nom">nom</a> back then by implementing the strict OpenGL
Shading Language Spec (<a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwj3uZTS-ebeAhUuyoUKHcW1D04QFjAAegQIAhAC&amp;url=https%3A%2F%2Fwww.khronos.org%2Fregistry%2FOpenGL%2Fspecs%2Fgl%2FGLSLangSpec.4.50.pdf&amp;usg=AOvVaw01MeCZu7SMf3NwCmD83SAr">here</a>).
That parser works mostly on strings and byte slices (I really doubt people use the byte slice
interface and I might deprecate it).</li>
<li>The parser parses into an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">Abstract Syntax Tree</a> – AST for short. That AST is, as the name
implies, a tree that contains typed <em>nodes</em>. It’s a bit more complicated than having a
<code>BTreeMap</code> for instance, because each nodes and levels are completely typed. You can picture the
AST as both a <em>type tree</em> and <em>value tree</em> instead of just a <em>value tree</em>.</li>
<li>Before 0.13, transforming the AST was tedious, because you had to pattern match on the whole
structure of the AST, and since it’s a <em>type tree</em>, it’s really, really boring to do so.</li>
<li>One typical transformation that I always need in several crates of mine is <em>construction</em>.
Building ASTs was overwhelming and complicated because you had to create the tree by hand. To
solve that issue, I introduced the <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a> crate, that enables you to create the AST
out of a regular GLSL string. I hit some issues with that, like the impossibility to have typing
information exchanged between two compiler phases (the procedural macros are all evaluated
before the rest, making it impossible to have type inference like in <code>let x: Expr = glsl!{…};</code>.
I will try to fix that later.</li>
<li>The quasiquote crate works great for static ASTs but as soon as you want to build dynamic ones (
i.e. that depend on variables for instance), since <a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a> doesn’t have variable
interpolation (yet!), you’re back to the boring by-hand construction.</li>
</ul>
<p><a href="https://crates.io/crates/glsl/0.13.2">glsl-0.13</a> comes with several new features and enhancements to partially fix those points that I
will explain in this blog entry.</p>
<h1>The new features!</h1>
<p>First, let’s talk about contribution. I received a contribution (both an issue <em>and</em> a PR, how
amazing!) from <a href="https://github.com/JDemler">@JDemler</a> about the <code>#define</code> preprocessor pragma. Those were not taken into
account so far and he provided the fix to add them! As for all other pragmas, those have to lay in
the global scope – they’re not expected to be found in function bodies, for instance.</p>
<p>Thanks for your contribution! :)</p>
<p>Then, a lot of work has been done around the <code>glsl::syntax</code> module. This module contains all the AST
types and some methods were added to, for instance, create <em>functions</em>, <em>variable declarations</em>,
<em>if-else</em> blocks, etc. Those functions were made the most general as possible and heavily depend on
<code>From</code> / <code>Into</code>, allowing for very short oneliners to create complex AST nodes. The best example
for this is the <a href="https://docs.rs/glsl/0.13.2/glsl/syntax/enum.Statement.html#method.declare_var"><code>Statement::declare_var</code></a> that declares a new variable as a <code>Statement</code>. Most of
the inner AST nodes won’t ever be needed in the public interface, so those functions hide them from
you and give you the easy way to build bigger nodes.</p>
<blockquote>
<p>There is an example below that uses <a href="https://docs.rs/glsl/0.13.2/glsl/syntax/enum.Statement.html#method.declare_var"><code>Statement::declare_var</code></a>. Keep on reading! :)</p>
</blockquote>
<p>Not all AST nodes have helper methods for construction. The good thing is that adding a new function
<em>shouldn’t</em> be a breaking change, so I’ll keep adding them as needed – if you have a specific need
for one, feel free to open an issue, I will make the change. That might be surprising but since I
haven’t witnessed lots of people using the crate yet, I don’t implement what I don’t need yet – but
as soon as someone tells me they need something, either I immediately review the PR, or plan some
time to make the patch myself.</p>
<p>Finally, the biggest feature which will be explained in further details in this blog post: <strong>AST
visitors</strong>. AST visitors are the way I imagined would be the best to traverse an AST in order to
mutate some nodes, all the nodes, filter, query information, etc. It’s like <em>lenses</em> for <a href="https://crates.io/crates/glsl">glsl</a>! –
but trust me: it’s way lighter! :D</p>
<h2>Visiting ASTs</h2>
<p>AST visitors solve a problem I had when implementing a specific feature in <a href="https://crates.io/crates/spectra">spectra</a>. I needed to be
able to change <strong>all</strong> the references to a given identifier in all the AST at once. Either the
identifier is used in an expression assigned to a new variable, returned from a function or passed
as argument to a function, I needed to catch that identifier… and change its name.</p>
<p>That was quite of a desperate challenge without a proper solution. Imagine: I would have to write
the code to change the identifier myself and… find ALL the places where identifiers <em>might</em> appear
in <em>any</em> AST. This is possible, but that would drive many developers mad – especially whevener the
<a href="https://crates.io/crates/glsl">glsl</a> crate changes.</p>
<p>So I came up with a better solution. I will not drop you a technical term and have you read
Wikipedia, I would dislike that. I will just explain from bottom up why it’s designed this way and
why I think it’s the best solution.</p>
<h2>Visiting summarized</h2>
<p>The idea is that pattern-matching on an identifier might appear in several places in an AST. So
whatever solution we choose, we will have to find all those spots and call a function that states:</p>
<blockquote>
<p><em>Hey there. Here is an <code>Identifier</code>. I give you a <code>&amp;mut Identifier</code> so that you can change it.</em></p>
</blockquote>
<p>An <code>Identifier</code> is an AST (a really simple one, but still is). So you might want to implement that
function on <code>Identifier</code>… but what happens when your AST is an <code>Expr</code>, and that expr is a variable –
that is, <code>Expr::Var(identifier_here)</code>? You will want to implement your function on <code>Expr</code> <em>also</em>,
then. And here you see that you need a trait, because, as said earlier, the AST is a <em>type tree</em>.</p>
<p>However, what trait? We could imagine implementing that trait only on AST types that have an
<code>Identifier</code> as field or variant. But the most general AST node, <code>TranslationUnit</code>, is a non-empty
list of <code>ExternalDeclaration</code>s. If we want to visit that, we won’t be able to type match and pass
down our function transforming identifiers.</p>
<p>We see that we will need to implement that trait for all AST types so that they can pass the
function down the AST to a node that actually has an <code>Identifier</code>.</p>
<p>And since we’re doing it with <code>Identifier</code>, we might want to do it with <em>any</em> AST node. But if we
do that, we cannot pass one single function anymore…</p>
<h2>The Visitor</h2>
<p>So you need an object that will be able to treat an <code>Identifier</code>… or a <code>TranslationUnit</code>, or
anything AST. This is a bit boring to implement, but we need a trait that enables a type to visit
any AST node:</p>
<pre><code>trait Visitor {
  fn visit_identifier(&amp;mut self, _: &amp;mut Identifier);
  fn visit_translation_unit(&amp;mut self, _: &amp;mut TranslationUnit);
  // …
}
</code></pre>
<p>This is great, because if a type implements <code>Visitor</code>, it means that we can call <code>visit_identifier</code>
on it if we have an <code>Identifier</code>! When we will be implementing our traversing function, we will just
have to carry around a mutable reference to an object implementing <code>Visitor</code>, and call the right
function depending on the AST node / type we are at!</p>
<p>We also use mutable references here so that we can also mutate information as we sink into the AST.
This might be very useful to know at which block depth we are at, or if we’re in a function’s body,
etc.</p>
<p>Something important with that trait though: the current implementation (from this article) would be
very boring to implement, because it has a lot of <code>visit_*</code> methods. What if we’re only interested
in <code>Identifier</code>? We don’t want to have to implement <code>visit_translation_unit</code> because we don’t care.</p>
<p>A simple fix to that is to give all those <code>visit_*</code> methods a default implementation… that does
nothing.</p>
<pre><code>trait Visitor {
  fn visit_identifier(&amp;mut self, _: &amp;mut Identifier) {}
  fn visit_translation_unit(&amp;mut self, _: &amp;mut TranslationUnit) {}
  // …
}
</code></pre>
<p>Let’s try it!</p>
<pre><code>struct ReplaceIdentifier&lt;'a&gt; {
  replacement: &amp;'a str
}

impl&lt;'a&gt; Visitor for ReplaceIdentifier&lt;'a&gt; {
  fn visit_identifier(&amp;mut self, ident: &amp;mut Identifier) {
    *ident = self.replacement.clone().into()
  }
}
</code></pre>
<p>And that’s all! We now have a visitor that can be used to traverse any AST and change any
<code>Identifier</code> to what we have set. For instance:</p>
<pre><code>let mut ast = …;
let mut visitor = ReplaceIdentifier { replacement: "foobar" }

// wait, how do we pass the visitor to the AST here?
</code></pre>
<p>Argh, we’re still missing something!</p>
<h2>Hosting visitors</h2>
<p>We need a <code>visit</code> function, like:</p>
<pre><code>fn visit&lt;V&gt;(ast: &amp;mut TranslationUnit, visitor: &amp;mut V) where V: Visitor
</code></pre>
<p>So let’s write it!</p>
<pre><code>fn visit&lt;V&gt;(ast: &amp;mut TranslationUnit, visitor: &amp;mut V) where V: Visitor {
  for external_decl in ast {
    // ah.
  }
}
</code></pre>
<p>We cannot call <code>visit</code> again on <code>ExternalDeclaration</code>. Seems like we need another trait! :D</p>
<pre><code>trait Host {
  fn visit&lt;V&gt;(&amp;mut Self, visitor: &amp;mut V) where V: Visitor;
}
</code></pre>
<p>Here, a type that implements <code>Host</code> means that it can call a <code>Visitor</code> on itself, and might pass it
down to children AST if any. Since we’re going to implement <code>Host</code> for all our AST types, we will be
able to do something like this:</p>
<pre><code>impl Host for TranslationUnit {
  fn visit&lt;V&gt;(&amp;mut Self, visitor: &amp;mut V) where V: Visitor {
    visitor.visit_translation_unit(self); // first, we have the visitor visit the AST node

    // then, for all children, we pass down the visitor!
    for external_decl in self {
      external_decl.visit(visitor);
    }
  }
}
</code></pre>
<p>And here we go. We are able to pass down the visitor, that will be called for each node. If you have
provided an implementation for a given <code>visit_*</code>, it will get invoked, otherwise, the default
implementation will fire – and it does nothing.</p>
<p>A simple optimization can be done, here. Since you might <em>know</em> that you’re done at a given level
regarding your visitor, we could add a way to make a <code>Host</code> stop visiting and go any deeper. For
this, we introduce a simple enum:</p>
<pre><code>enum Visit {
  Children, // keep visiting children
  Parent // stop visiting, go back to parent
}
</code></pre>
<p>And we change all the <code>Visitor</code> methods to return a <code>Visit</code>. That will give us the information we
need when implementing <code>Host::visit</code> now:</p>
<pre><code>impl Host for TranslationUnit {
  fn visit&lt;V&gt;(&amp;mut Self, visitor: &amp;mut V) where V: Visitor {
    let visit = visitor.visit_translation_unit(self); // first, we have the visitor visit the AST node

    if visit == Visit::Children {
      // then, for all children, we pass down the visitor!
      for external_decl in self {
        external_decl.visit(visitor);
      }
    }
  }
}
</code></pre>
<p>We also need to change the default implementation of the <code>visit_*</code> methods. My choice was to have
them return <code>Visit::Children</code> by default, because I think it’s a saner default – if people don’t
want to go any deeper in the AST, they will just have to dummy-implement the right method.</p>
<p>And we are done! The actual implementation is a bit more complex than that but is really really
close to what is described in this article. I’ll give you the example from the official
documentation so that can you can see how it’s <em>really</em> used:</p>
<pre><code>use glsl::syntax::{CompoundStatement, Expr, SingleDeclaration, Statement, TypeSpecifierNonArray};
use glsl::visitor::{Host, Visit, Visitor};
use std::iter::FromIterator;

let decl0 = Statement::declare_var(
  TypeSpecifierNonArray::Float,
  "x",
  None,
  Some(Expr::from(3.14).into())
);

let decl1 = Statement::declare_var(
  TypeSpecifierNonArray::Int,
  "y",
  None,
  None
);

let decl2 = Statement::declare_var(
  TypeSpecifierNonArray::Vec4,
  "z",
  None,
  None
);

let mut compound = CompoundStatement::from_iter(vec![decl0, decl1, decl2]);

// our visitor that will count the number of variables it saw
struct Counter {
  var_nb: usize
}

impl Visitor for Counter {
  // we are only interested in single declaration with a name
  fn visit_single_declaration(&amp;mut self, declaration: &amp;mut SingleDeclaration) -&gt; Visit {
    if declaration.name.is_some() {
      self.var_nb += 1;
    }

    // do not go deeper
    Visit::Parent
  }
}

let mut counter = Counter { var_nb: 0 };
compound.visit(&amp;mut counter);
assert_eq!(counter.var_nb, 3);
</code></pre>
<h1>Future work</h1>
<p>The current state of <code>Visitor</code> is great but there is a drawback: your AST has to be mutable. You
might want to only traverse it read-only but have your visitor mutated. I might then modify slightly
the <code>Visitor</code> trait and <code>Host</code> one with <code>Host::visit_mut</code> if I think it’s needed.</p>
<p>Feel free to experiment around. Next work planned for <a href="https://crates.io/crates/glsl">glsl</a> – besides contributions – quasiquoting
variable interpolation.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 22 Nov 2018 04:20:00 GMT</pubDate></item><item><title>Luminance – Vertex Arrays</title><link>https://strongly-typed-thoughts.net/blog/luminance_vertex_arrays</link><description><![CDATA[<p>I’ve been up working on vertex arrays in my work-in-progress graphics
framework, <a href="https://github.com/phaazon/luminance">luminance</a>, for several
days. I’m a bit slow, because I’ve been through a very hard breakup and have
been struggling to recover and focus. But here I am!</p>
<h1>So, what’s new?</h1>
<p><strong>OpenGL</strong> allows programmers to send <em>vertices</em> to the GPU through what
is called a <a href="https://www.opengl.org/wiki/Vertex_Specification">vertex array</a>.
Vertex specification is performed through several functions, operating on
several objects. You need, for instance, a <em>vertex buffer object</em>, an <em>index
buffer object</em> and a <em>vertex array object</em>. The <em>vertex buffer</em> stores the
vertices data.</p>
<p><img src="http://wiki.splashdamage.com/upload/2/2f/Teapot_mesh.jpg" alt="Teapot" /></p>
<p>For instance, you could imagine a <em>teapot</em> as a set of vertices. Those
vertices have several attributes. We could use, for instance, a <strong>position</strong>, a
<strong>normal</strong> and a <strong>bone index</strong>. The vertex buffer would be responsible of storing those
positions, normals and bone indices. There’re two ways to store them:</p>
<ol>
<li>interleaved arrays ;</li>
<li>deinterleaved arrays.</li>
</ol>
<p>I’ll explain those later on. The <em>index buffer</em> stores integral numbers –
mainly set to <code>unsigned int</code> – that index the vertices, so that we can connect
them and create lines, triangles or more complex shapes.</p>
<p>Finally, the <em>vertex array object</em> is a state object that stores links to the
two buffers and makes a connection between pointers in the buffer and attribute
indices. Once everything is set up, we might only use the <em>vertex array object</em>.
The exception is when we need to change the geometry of an object. We need to
access the vertex buffer and the index buffer and upload new data. However,
<strong>for now</strong>, that feature is disabled so that the buffers are not exposed to the
programmer. If people think that feature should be implemented, I’ll create
specialized code for that very purpose.</p>
<h2>Interleaved and deinterleaved arrays</h2>
<p>Interleaved arrays might be the most simple to picture, because you use such
arrays every day when programming. Let’s imagine you have the following type in
<strong>Haskell</strong>:</p>
<pre><code class="language-haskell">data Vertex = Vertex {
    vertPos    :: X
  , vertNor    :: Y
  , vertBoneID :: Z
  } deriving (Eq,Show)
</code></pre>
<p>Now, the teapot would have several vertices. Approximately, let’s state the
teapot has five vertices – yeah, ugly teapot. We can represent such vertices
in an interleaved array by simply recording them in a list or an array:</p>
<p><img src="http://phaazon.net/pub/interleaved.png" alt="Interleaved" /></p>
<p>As you can see, the attributes are interleaved in memory, and the whole pattern
is cycling. That’s the common way to represent an array of struct in a lot of
languages, and it’s very natural for a machine to do things like that.</p>
<p>The deinterleaved version is:</p>
<p><img src="http://phaazon.net/pub/deinterleaved.png" alt="Deinterleaved" /></p>
<p>As you can see, with deinterleaved arrays, all attributes are extracted and
grouped. If you want to access the third vertex, you need to read the third <code>X</code>,
the third <code>Y</code> and the third <code>Z</code>.</p>
<p>Both the methods have advantages and drawbacks. The cool thing about
deinterleaved arrays is that we can copy huge regions of typed memory at once
whilst we cannot with interleaved arrays. However, interleaved arrays store
continuous structures, so writing and reading a structure back might be faster.</p>
<p>An important point to keep in mind: because we plan to pass those arrays to
<strong>OpenGL</strong>, there’s no
<a href="https://en.wikipedia.org/wiki/Data_structure_alignment">alignment</a> restriction
on the structure. That is, everything is <em>packed</em>, and we’ll have to pass extra
information to <strong>OpenGL</strong> to tell it how to advance in memory to correctly
build vertices back.</p>
<h2>Generalized tuple</h2>
<p>I think I haven’t told you yet. I have a cool type in
<a href="https://github.com/phaazon/luminance">luminance</a>: the <code>(:.)</code> type. No, you
don’t have to know how to pronounce that. I like to call it the <em>gtuple</em>
type, because it’s a generalized tuple. You can encode <code>(a,b)</code>, <code>(a,b,c)</code> and
all kind of tuples with <code>(:.)</code>. You can even encode single-typed infinite
tuple! – a very special kind of list, indeed.</p>
<pre><code class="language-haskell">data a :. b = a :. b

infixr 6 :.

-- a :. b is isomorphic to (a,b)
-- a :. b :. c is isomorphic to (a,b,c)

newtype Fix f = Fix (f (Fix f)) -- from Control.Monad.Fix
type Inf a = Fix ((:.) a) -- infinite tuple!
</code></pre>
<p>Pretty simple, but way more powerful than the regular, monomorphic tuples. As
you can see, <code>(:.)</code> is a right-associative. That means that
<code>a :. b :. c = a :. (b :. c)</code>.</p>
<p>That type will be heavily used in
<a href="https://github.com/phaazon/luminance">luminance</a>, thus you should get your fet
wet with it. There’s actually nothing much to know about it. It’s a <code>Functor</code>.
I might add other features to it later on.</p>
<h3>The Storable trick</h3>
<p>The cool thing about <code>(:.)</code> is that we can provide a <code>Storable</code> instance for
packed memory, as <strong>OpenGL</strong> requires it. Currently, the <code>Storable</code> instance
is <a href="https://github.com/phaazon/luminance/blob/05ef2e4879aae92189535f0121765931b20de2fd/src/Graphics/Luminance/Tuple.hs#L23">implemented like this</a>:</p>
<pre><code class="language-haskell">instance (Storable a,Storable b) =&gt; Storable (a :. b) where
  sizeOf (a :. b) = sizeOf a + sizeOf b
  alignment _ = 1 -- packed data
  peek p = do
    a &lt;- peek $ castPtr p
    b &lt;- peek . castPtr $ p `plusPtr` sizeOf (undefined :: a)
    pure $ a :. b
  poke p (a :. b) = do
    poke (castPtr p) a
    poke (castPtr $ p `plusPtr` sizeOf (undefined :: a)) b
</code></pre>
<p>As you can see, the <code>alignment</code> is set to <code>1</code> to express the fact the memory
is packed. The <code>peek</code> and <code>poke</code> functions use the size of the head of the tuple
to advance the pointer so that we effectively write the whole tuple in packed
memory.</p>
<p>Then, let’s rewrite our <code>Vertex</code> type in terms of <code>(:.)</code> to see how it’s going
on:</p>
<pre><code class="language-haskell">type Vertex = X :. Y :. Z
</code></pre>
<p>If <code>X</code>, <code>Y</code> and <code>Z</code> are in <code>Storable</code>, we can directly <code>poke</code> one of our
<code>Vertex</code> into a <a href="http://phaazon.blogspot.fr/2015/07/introducing-luminance-safer-opengl-api.html">luminance buffer</a>! That is, directly into the GPU buffer!</p>
<p>Keep in mind that the <code>Storable</code> instance implements packed-memory uploads and
reads, and won’t work with special kinds of buffers, like <em>shader storage</em> ones,
which require specific memory alignment. To cover them, I’ll create specific
typeclasses instances. No worries.</p>
<h1>Creating a vertex array</h1>
<p>Creating a vertex array is done through the function <code>createVertexArray</code>. I
might change the name of that object – it’s ugly, right? Maybe <code>Shape</code>, or
something cooler!</p>
<pre><code class="language-haskell">createVertexArray :: (Foldable f,MonadIO m,MonadResource m,Storable v,Traversable t,Vertex v)
                  =&gt; t v
                  -&gt; f Word32
                  -&gt; m VertexArray
</code></pre>
<p>As you can see, the type signature is highly polymorphic. <code>t</code> and <code>f</code> represent
<em>foldable</em> structures storing the vertices and the indices. And that’s all.
Nothing else to feed the function with! As you can see, there’s a typeclass
constraint on <code>v</code>, the inner vertex type, <code>Vertex</code>. That constraint ensures the
vertex type is representable on the <strong>OpenGL</strong> side and has a known vertex
format.</p>
<p><strong>Disclaimer:</strong> the <code>Traversable</code> constraint might be relaxed to be <code>Foldable</code>
very soon.</p>
<p>Once tested, I’ll move all that code from the <code>unstable</code> branch to the <code>master</code>
branch so that you guys can test it. :)</p>
<h2>About OpenGL…</h2>
<p>I eventually came to the realization that I needed to inform you about the
<strong>OpenGL</strong> prerequisites. Because I want the framework to be as modern and
well-designed as possible, you’ll need… <strong>OpenGL 4.5</strong>. The latest version,
indeed. You <strong>might</strong> also need an extension, <a href="https://www.opengl.org/wiki/Bindless_Texture">ARB_bindless_texture</a>. That would enable the framework to pass textures to
shader in a very stateless way, which is our objective!</p>
<p>I’ll let you know what I decide about that. I don’t want to use an extension
that is not implemented almost everywhere.</p>
<h1>What’s next?</h1>
<p>Well, tests! I need to be sure everything is correctly done on the GPU side,
especially the vertex format specification. I’m pretty confident though.</p>
<p>Once the vertex arrays are tested, I’ll start defining a <em>render interface</em> as
stateless as I can. As always, I’ll keep you informed!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 10 Aug 2015 00:00:00 GMT</pubDate></item><item><title>al 0.1.0.2 – documentation and default paths</title><link>https://strongly-typed-thoughts.net/blog/al_0.1.0.2</link><description><![CDATA[<h1>al patch</h1>
<p>This is a very short article to make you notice that
<a href="https://hackage.haskell.org/package/al">al</a> received two important changes:</p>
<ul>
<li>I uploaded documentation (hourra!) ;</li>
<li>OpenAL paths will default to default installation on <strong>Windows systems</strong>.</li>
</ul>
<p>I tested the latter with a Windows 64b:</p>
<pre><code>cabal update
cabal install al
</code></pre>
<p>That’s all. You should try it out as well. If you have errors about OpenAL
libraries and/or header files, check whether OpenAL is correctly installed.
If the error comes up again, proceed as I said
<a href="http://phaazon.blogspot.fr/2015/02/al-01-released.html#installing-al-1">here</a>.</p>
<p>Also, I take people on linux feedback about installs ;).</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 01 Mar 2015 00:00:00 GMT</pubDate></item><item><title>luminance-0.3 – Adding more texture kinds to the equation…</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.3</link><description><![CDATA[<h1>Unleashing the power of textures!</h1>
<p>From <a href="https://hackage.haskell.org/package/luminance-0.1">luminance-0.1</a> to
<a href="https://hackage.haskell.org/package/luminance-0.2">luminance-0.2</a> included, it was not possible to
use any texture types different than two-dimensional textures. This blog entry tags the new
release, <a href="https://hackage.haskell.org/package/luminance-0.3">luminance-0.3</a>, which adds support for
several kinds of texture.</p>
<h2>A bit more dimensions</h2>
<p><a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#t:Texture1D"><code>Texture1D</code></a>,
<a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#t:Texture2D"><code>Texture2D</code></a> and
<a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#t:Texture3D"><code>Texture3D</code></a>
are all part of the new release. The interface has changed – hence the breaking changes yield a
major version increment – and I’ll explain how it has.</p>
<p>Basically, textures are now fully polymorphic and are constrained by a typeclass:
<a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#t:Texture"><code>Texture</code></a>.
That typeclass enables ad hoc polymorphism. It is then possible to add more texture types without
having to change the interface, which is cool. Like everything else in luminance, you just have to
ask the typesystem which kind of texture you want, and everything will be taken care of for you.</p>
<p>Basically, you have three functions to know:</p>
<ul>
<li><a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#v:createTexture"><code>createTexture</code></a>,
which is used to create a new texture ;</li>
<li><a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#v:uploadSub"><code>uploadSub</code></a>,
used to upload texels to a subpart of the texture ;</li>
<li><a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#v:fillSub"><code>fillSub</code></a>,
used to fill – <em>clear</em> – a subpart of the texture with a given value.</li>
</ul>
<p>All those functions work on <code>(Texture t) =&gt; t</code>, so it will work with all kinds of texture.</p>
<h2>Cubemaps</h2>
<p><a href="https://hackage.haskell.org/package/luminance-0.3/docs/Graphics-Luminance-Texture.html#t:Cubemap"><code>Cubemap</code>s</a>
are also included. They work like other textures but add the concept of <em>faces</em>. Feel free to dig in
the documentation for further details.</p>
<h1>What’s next?</h1>
<p>I need to find a way to wrap <em>texture arrays</em>, which are very nice and useful for
<em>layered rendering</em>. After that, I’ll try to expose the change to the framebuffers so that we can
create framebuffers with cubemaps or that kind of cool feature.</p>
<p>In the waiting, have a good a week!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 06 Oct 2015 00:00:00 GMT</pubDate></item><item><title>Lifetimes limits – self borrowing and dropchecker</title><link>https://strongly-typed-thoughts.net/blog/lifetimes_and_self_borrowing</link><description><![CDATA[<p>Lately, I’ve been playing around with <a href="https://crates.io/crates/alto">alto</a> in my demoscene framework. This crate in the <em>replacement of <a href="https://crates.io/crates/openal-rs">openal-rs</a></em> as <strong>openal-rs</strong> has
been deprecated because <em>unsound</em>. It’s a wrapper over <a href="https://www.openal.org">OpenAL</a>, which enables you to play 3D sounds and gives you several physical properties and effects you can apply.</p>
<h1>The problem</h1>
<p>Just to let you fully understand the problem, let me introduce a few principles from alto. As a wrapper over OpenAL, it exposes quite the same interface, but adds several safe-related types. In order to use
the API, you need three objects:</p>
<ul>
<li>an <code>Alto</code> object, which represents the <em>API object</em> (it holds dynamic library handles, function pointers, etc. ; we don’t need to know about that)</li>
<li>a <code>Device</code> object, a regular device (a sound card, for example)</li>
<li>a <code>Context</code> object, used to create audio resources, handle the audio context, etc.</li>
</ul>
<p>There are well-defined relationships between those objects that state about their lifetimes. An <code>Alto</code> object must outlive the <code>Device</code> and the <code>Device</code> must outlive the <code>Context</code>.
Basically:</p>
<pre><code class="language-rust">let alto = Alto::load_default(None).unwrap(); // bring the default OpenAL implementation in
let dev = alto.open(None).unwrap(); // open the default device
let ctx = dev.new_context(None).unwrap(); // create a default context with no OpenAL extension
</code></pre>
<p>As you can see here, the lifetimes are not violated, because <code>alto</code> outlives <code>dev</code> which outlives <code>ctx</code>. Let’s dig in the type and function signatures to get the lifetimes right
(documentation <a href="https://docs.rs/alto/1.0.5/alto">here</a>).</p>
<pre><code class="language-rust">fn Alto::open&lt;'s, S: Into&lt;Option&lt;&amp;'s CStr&gt;&gt;&gt;(&amp;self, spec: S) -&gt; AltoResult&lt;Device&gt;
</code></pre>
<p>The <code>S</code> type is just a convenient type to select a specific implementation. We need the default one, so just pass <code>None</code>. However, have a look at the result. <code>AltoResult&lt;Device&gt;</code>. I told you about
lifetime relationships. This one might be tricky, but you always have to wonder “is there an elided lifetime here?”. Look at the <code>Device</code> type:</p>
<pre><code class="language-rust">pub struct Device&lt;'a&gt; { /* fields omitted */ }
</code></pre>
<p>Yep! So, what’s the lifetime of the <code>Device</code> in <code>AltoResult&lt;Device&gt;</code>? Well, that’s simple: the lifetime elision rule in action is one of the simplest:</p>
<blockquote>
<p>If there are multiple input lifetime positions, but one of them is &amp;self or &amp;mut self, the lifetime of self is assigned to all elided output lifetimes.
(<a href="https://doc.rust-lang.org/beta/nomicon/lifetime-elision.html">source</a>)</p>
</blockquote>
<p>So let’s rewrite the <code>Alto::open</code> function to make it clearer:</p>
<pre><code class="language-rust">fn Alto::open&lt;'a, 's, S: Into&lt;Option&lt;&amp;'s CStr&gt;&gt;&gt;(&amp;'a self, spec: S) -&gt; AltoResult&lt;Device&lt;'a&gt;&gt; // exact same thing as above
</code></pre>
<p>So, what you can see here is that the <code>Device</code> must be valid for the same lifetime as the reference we pass in. Which means that <code>Device</code> cannot outlive the reference. Hence, it cannot outlive the
<code>Alto</code> object.</p>
<hr />
<pre><code class="language-rust">impl&lt;'a&gt; Device&lt;'a&gt; {
  // …
  fn new_context&lt;A: Into&lt;Option&lt;ContextAttrs&gt;&gt;&gt;(&amp;self, attrs: A) -&gt; AltoResult&lt;Context&gt;
  // …
}
</code></pre>
<p>That looks a bit similar. Let’s have a look at <code>Context</code>:</p>
<pre><code class="language-rust">pub struct Context&lt;'d&gt; { /* fields omitted */ }
</code></pre>
<p>Yep, same thing! Let’s rewrite the whole thing:</p>
<pre><code class="language-rust">impl&lt;'a&gt; Device&lt;'a&gt; {
  // …
  fn new_context&lt;'b, A: Into&lt;Option&lt;ContextAttrs&gt;&gt;&gt;(&amp;'b self, attrs: A) -&gt; AltoResult&lt;Context&lt;'b&gt;&gt;
  // …
}
</code></pre>
<p>Plus, keep in mind that <code>self</code> is actually <code>Device&lt;'a&gt;</code>. The first argument of this function then awaits a <code>&amp;'b Device&lt;'a&gt;</code> object!</p>
<blockquote>
<p>rustc is smart enough to automatically insert the <code>'a: 'b</code> lifetime bound here – i.e. the ’a lifetime outlives ’b. Which makes sense: the reference will die before the <code>Device&lt;'a&gt;</code> is dropped.</p>
</blockquote>
<p>Ok, ok. So, what’s the problem then?!</p>
<h1>The (real) problem</h1>
<p>The snippet of code above about how to create the three objects is straight-forward (though we don’t take into account errors, but that’s another topic). However, in my demoscene framework, I really don’t
want people to use that kind of types. The framework should be completely agnostic about which technology or API is used internally. For my purposes, I just need a single type with a few methods to work
with.</p>
<p>Something like that:</p>
<pre><code class="language-rust">struct Audio = {}

impl Audio {
  pub fn new&lt;P&gt;(track_path: P) -&gt; Result&lt;Self&gt; where P: AsRef&lt;Path&gt; {}

  pub fn toggle(&amp;mut self) -&gt; bool {}

  pub fn playback_cursor(&amp;self) -&gt; f32 {}

  pub fn set_playback_cursor(&amp;self, t: f32) {}
}

impl Drop for Audio {
  fn drop(&amp;mut self) {
    // stop the music if playing; do additional audio cleanup
  }
}
</code></pre>
<p>This is a very simple interface, yet I don’t need more. <code>Audio::set_playback_cursor</code> is cool when I debug my demos in realtime by clicking a time panel to quickly jump to a part of the music.
<code>Audio::toggle()</code> enables me to pause the demo to inspect an effect in the demo. Etc.</p>
<p>However, how can I implement <code>Audio::new</code>?</p>
<h1>The (current) limits of borrowing</h1>
<p>The problem kicks in as we need to wrap the three types – <code>Alto</code>, <code>Device</code> and <code>Context</code> – as the fields of <code>Audio</code>:</p>
<pre><code class="language-rust">struct Audio&lt;'a&gt; {
  alto: Alto,
  dev: Device&lt;'a&gt;,
  context: Context&lt;'a&gt;
}
</code></pre>
<p>We have a problem if we do this. Even though the type is correct, we cannot correctly implement <code>Audio::new</code>. Let’s try:</p>
<pre><code class="language-rust">impl&lt;'a&gt; Audio&lt;'a&gt; {
  pub fn new&lt;P&gt;(_: P) -&gt; Result&lt;Self&gt; where P: AsRef&lt;Path&gt; {
    let alto = Alto::load_default(None).unwrap();
    let dev = alto.open(None).unwrap();
    let ctx = dev.new_context(None).unwrap();

    Ok(Audio {
      alto: alto,
      dev: dev,
      ctx: ctx
    })
  }
}
</code></pre>
<p>As you can see, that cannot work:</p>
<pre><code>error: `alto` does not live long enough
  --&gt; /tmp/alto/src/main.rs:14:15
   |
14 |     let dev = alto.open(None).unwrap();
   |               ^^^^ does not live long enough
...
22 |   }
   |   - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the body at 12:19...
  --&gt; /tmp/alto/src/main.rs:12:20
   |
12 |   fn new() -&gt; Self {
   |                    ^

error: `dev` does not live long enough
  --&gt; /tmp/alto/src/main.rs:15:15
   |
15 |     let ctx = dev.new_context(None).unwrap();
   |               ^^^ does not live long enough
...
22 |   }
   |   - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the body at 12:19...
  --&gt; /tmp/alto/src/main.rs:12:20
   |
12 |   fn new() -&gt; Self {
   |                    ^

error: aborting due to 2 previous errors
</code></pre>
<p>What’s going on here? Well, we’re hitting a problem called the problem of <strong>self-borrowing</strong>. Look at the first two lines of our implementation of <code>Audio::new</code>:</p>
<pre><code class="language-rust">let alto = Alto::load_default(None).unwrap();
let dev = alto.open(None).unwrap();
</code></pre>
<p>As you can see, the call to <code>Alto::open</code> borrows <code>alto</code> – via a <code>&amp;Alto</code> reference. And of course, you cannot move a value that is borrowed – that would invalidate all the references pointing to it.
We also have another problem: imagine we could do that. All those types implement <code>Drop</code>. Because they basically all have the same lifetime, there’s no way to know which one borrows information from whom.
The <em>dropchecker</em> has no way to know that. It will then refuse to code creating objects of this type, because dropping might be unsafe in that case.</p>
<h1>What can we do about it?</h1>
<p>Currently, this problem is linked to the fact that the lifetime system is a bit too restrictive and doesn’t allow for <strong>self-borrowing</strong>. Plus, you also have the <em>dropchecker</em> issue to figure out. Even
though we were able to bring in <code>alto</code> and <code>device</code> altogether, how do you handle <code>context</code>? The <em>dropchecker</em> doesn’t know which one must be dropped first – there’s no obvious link at this stage between
<code>alto</code> and all the others anymore, because that link was made with a reference to <code>alto</code> that died – we’re moving out of the scope of the <code>Audio::new</code> function.</p>
<p><img src="http://phaazon.net/pub/rust_self_borrowing.jpg" alt="" /></p>
<p>That’s a bit tough. The current solution I implemented to fix the issue is ok–ish, but I dislike it because it adds a significant performance overhead: I just moved the initialization code in a thread that
stays awake until the <code>Audio</code> object dies, and I use a synchronized channel to communicate with the objects in that thread. That works because the thread provides us with a stack, that is the support of
lifetimes – think of scopes.</p>
<p>Another solution would be to move that initialization code in a function that would accept a closure – your application. Once everything is initialized, the closure is called with a few callbacks to
toggle / set the cursor of the object living “behind” on the stack. I don’t like that solution because it modifies the main design – having an <code>Audio</code> object was the goal.</p>
<p>Other solutions are:</p>
<ul>
<li><code>std::mem::transmute</code> to remove the lifetimes (replace them with <code>'static</code>). That’s <strong>hyper dangerous</strong> and we are just breaking Rust’s lifetimes… <em>not okay</em> :(</li>
<li>change our design to meet the same as alto’s (in a word: use the same three objects)</li>
<li>cry deeply</li>
</ul>
<p>I don’t have a satisfying solution yet to that problem. My thread solution works and lets me have a single type abstracting all of that, but having a thread for such a thing is a waste of resources to me. I
think I’ll implement the closure solution as, currently, it’s not possible to embed in struct lifetimes’ semantics / logic. I guess it’s okay; I guess the problem is also linked to the fact the concept is
pretty young and we’re still kind of experimenting it. But clearly, lifetimes hit a hard problem here that they cannot solve correctly. Keep in mind that even if unsafe solutions exist, we’re talking about
a library that’s designed to work with Rust lifetimes as a pretty high level of abstraction. Firing <code>transmute</code> is very symptomatic of something wrong. I’m open to suggestions, because I’ve been thinking the
problem all day long without finding a proper solution.</p>
<p>Keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 07 Feb 2017 00:00:00 GMT</pubDate></item><item><title>New cargo subcommand: sync-readme</title><link>https://strongly-typed-thoughts.net/blog/cargo-sync-readme</link><description><![CDATA[<p>Have you ever struggled with welcoming people on your project pages, be them on <a href="https://github.com">GitHub</a> or
<a href="https://docs.rs">docs.rs</a>? One major problem we’re facing when developping crates is that we need to maintain
several places where documentation is important:</p>
<ul>
<li>The <code>lib.rs</code> / <code>main.rs</code> files, that get rendered as front page on <a href="https://docs.rs">docs.rs</a>. This is important
because we want people to have nice onboardings when hitting the official documentations of our
crates.</li>
<li>The README file — often <code>README.md</code>, that gets automatically rendered as HTML when you end up on
the project’s <a href="https://github.com">GitHub</a> page. This page should provide as many as possible hints and information
about what the project is and how to use it.</li>
</ul>
<p>However, sometimes, I just don’t want to bother writing twice the same thing and honestly, I don’t
really know whether the README file is a good place to write an onboarding section, since that
should also be included in your Rust documentation on <a href="https://docs.rs">docs.rs</a>.</p>
<p>So… I’ve been thinking of a way to fix that, without really investing too much time in it. But
lately, I came to the realization that I often have this pattern:</p>
<ol>
<li>Write the onboarding documentation in my <code>lib.rs</code> or <code>main.rs</code>. For instance, I’m pretty proud
of the <a href="https://docs.rs/warmy">onboarding documentation of warmy</a>. It’s exhaustive, easy for newcomers, it has plenty
of links and explanations and it renders pretty nice on <a href="https://docs.rs">docs.rs</a>.</li>
<li>Write a header in the README file with badges etc. and then copy the Rust onboarding
documentation in the readme.</li>
</ol>
<p>Here, (2.) is a manual and annoying task: I open my editor, make a block selection of the
documentation, store it in a buffer, apply a smart macro on it to remove the Rust annotation and
then paste the result in the README after having purged it from the previous documentation… That’s
tedious and not very interesting.</p>
<h1><code>cargo sync-readme</code> to the rescue!</h1>
<p>So, yesterday, I decided to start working on a small tool that would automate all this for me. The
idea is the following:</p>
<ul>
<li><code>cargo sync-readme</code> synchronizes your README (the file specified by the <code>readme</code> key in your
<code>Cargo.toml</code>, or just <code>README.md</code> by default) with the entrypoint of your library or binary
crate (by default, <code>lib.rs</code> or <code>main.rs</code>, or what is defined at the <code>path</code> key in your
manifest).</li>
<li>In order to perform the synchronization, you need to have put a special marker so that the
command is instructed where to put the synchronized documentation.
<ul>
<li>The <code>&lt;!-- cargo-sync-readme --&gt;</code> marker must lie on a single line where you want the
documentation to be inserted in your README.</li>
<li>Post generation, this marker will disappear and will get replaced by the synchronized
documentation, surrounded by two markers: <code>&lt;!-- cargo-sync-readme start&gt;</code> and
<code>&lt;!-- cargo-sync-readme end --&gt;</code>.</li>
</ul>
</li>
</ul>
<p>This is really all you have to do. <code>cargo sync-readme</code> will take care of the rest for you. There’re
two hypothesis that the command requires to be true, though:</p>
<ul>
<li>Your README file should be a Markdown-formatted file. It doesn’t have to be, but since Rust’s
documentation is written in Markdown, it should be.</li>
<li>You use the <code>//!</code> annotation to write your documentation. This is currently how
<code>cargo sync-readme</code> works. It doesn’t have to be solely using this annotation on longer term,
but currently, this is the only annotation supported. More can be added if needed.</li>
</ul>
<p>Basically, insert the marker once, and run <code>cargo sync-readme</code>. Every time you change your Rust
documentation, just call <code>cargo sync-readme</code> once again to synchronize the documentation in your
README file.</p>
<h2>On workspace crates</h2>
<p>Currently, <code>cargo sync-readme</code> doesn’t work with workspace crates. You will have to go into each of
the workspace’s members to run <code>cargo sync-readme</code> if you want to synchronize their respective
READMEs.</p>
<h1>Conclusion</h1>
<p><code>cargo sync-readme</code> is already available on <a href="https://crates.io/crates/cargo-sync-readme">crates.io</a> for you to use. You can install it as a
development tool with the following command:</p>
<pre><code>cargo install cargo-sync-readme
</code></pre>
<blockquote>
<p>Disclaimer: after having published <code>cargo-sync-readme</code>, I was told that there is already another
cargo plugin, <a href="https://crates.io/crates/cargo-readme">cargo-readme</a>, that already does that. Indeed, that crate does more or less the
same job. However, the way it does it is very different. First, it uses a template file while
<code>cargo-sync-readme</code> lets you use your README file without having to depend on a template. Also,
<a href="https://crates.io/crates/cargo-readme">cargo-readme</a> has special semantics in its template (like {{crate_name}}, etc.) while
<code>cargo-sync-readme</code> is simpler and just requires a single marker. To sum up: <code>cargo-readme</code> is
heavier and is likely to require you to break your file into two separate files but contains more
customization options while <code>cargo-sync-readme</code> only requires a single line in your README and
will synchronize from within that same file.</p>
</blockquote>
<p>Feel free to comment, open issues, drink beers, share that <a href="https://phaazon.net/media/uploads/bear_sausage.mp4">awesome bear driving a sausage podracer</a>
and most of all… keep the vibes!</p>
<ul>
<li><a href="https://github.com/phaazon/cargo-sync-readme"><code>cargo-sync-readme</code> on GitHub</a></li>
<li><a href="https://crates.io/crates/cargo-sync-readme"><code>cargo-sync-readme</code> on crates.io</a></li>
</ul>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 25 Feb 2019 18:50:00 GMT</pubDate></item><item><title>Pre luminance-1.0.0 and random thoughts</title><link>https://strongly-typed-thoughts.net/blog/pre-luminance-n-random-thoughts</link><description><![CDATA[<h1>Random thoughts on making luminance-1.0.0</h1>
<p>Hello people. It’s been weeks I have started to work on luminance-1.0.0. For a brief recap,
<a href="https://crates.io/crates/luminance">luminance</a> is a graphics crate that I originally created in Haskell, when I ripped it off from a
<em>demoscene engine</em> called <a href="https://github.com/phaazon/quaazar">quaazar</a> in order to make and maintain tiner packages. The Rust port was
my first Rust project and it became quickly the default language I would develop graphics
applications in.</p>
<blockquote>
<p>So if you’re interested, yes, <a href="http://hackage.haskell.org/package/luminance">luminance in Haskell</a>
still exists but I maintain it for strictly minimal support (i.e. support compiler bumps, for
instance). The default and official language to use, if you’re interested by <a href="https://crates.io/crates/luminance">luminance</a>, is Rust.</p>
</blockquote>
<p>Another important thing I have to explain is the current feature list of <a href="https://crates.io/crates/luminance">luminance</a> and what it’s
going to be soon. Currently, <a href="https://crates.io/crates/luminance">luminance</a>’s goals are (according to the
<a href="https://docs.rs/luminance/0.30.1/luminance/">documentation</a>):</p>
<ul>
<li>Making the <strong>unsafe</strong> and <strong>stateful</strong> OpenGL API <strong>safe</strong> and <strong>stateless</strong>.
<ul>
<li><strong>Unsafe</strong> means that OpenGL performs operations by relying on the fact you know what you’re
doing: pointer arithmetic, memory alignment, etc. Lots of situations in which you can
literally <em>RIP in pepperonis</em>.</li>
<li><strong>Stateful</strong> means that lots of OpenGL functions expect a context to be in a special state for
a correct behavior. For instance, when uploading texels to the GPU’s VRAM, the texture that
must receive the texels must be bound to a texture unit. If you forget to do
that, you get a weird behavior that is seen at runtime and not always correctly interpreted.
<strong>It is sometimes hard to know why and what something has gone wrong.</strong> It makes also a <em>pain
in the @!#=$</em> as soon as you want to share work on several threads.</li>
</ul>
</li>
<li>Providing a simple and easy interface; that is, exposing core concepts without anything
extra – just the bare stuff.
<ul>
<li>Most low-level graphics library are hard for newcomers to grasp knowledge about. <a href="https://crates.io/crates/gfx-hal">gfx-hal</a>
is an excellent graphics crate (so far, I think it’s the one everyone is recommending
everyone in the Rust gamedev community) but it is hard to play with it when you don’t know
either OpenGL 4.5 or Vulkan — and all the required concepts might be a bit overwhelming at
first.</li>
<li>Even though <a href="https://crates.io/crates/luminance">luminance</a> is still a pretty low-level API (you have renderbuffers,
framebuffers, shader stages, etc.), the interface <strong>is opinionated</strong> and — in my opinion —
easier to work with. Because it is my idea and my design, the experience might be slightly
different from e.g. <a href="https://crates.io/crates/gfx-hal">gfx-hal</a>. It’s neither bad or good, just different. That also responds
to the folks who don’t understand why I’m still maintaining <a href="https://crates.io/crates/luminance">luminance</a>: because I want to
do graphics code my way, and more choices is always a good sign to me. Yes, we could talk
about community efforts, but OH LOOK A BIRD!</li>
</ul>
</li>
<li>Easy to read with a good documentation and set of tutorials, so that newcomers don’t have to
learn a lot of new concepts to get their feet wet.</li>
<li>Need-driven: every piece of code added in the project must come from a real use case. If you
feel something is missing, feel free to open an issue or even contribute!
<ul>
<li>This is an important aspect to me. I don’t write code for the sake of writing code (even
libraries). I have a problem and write code to solve it. So, on the surface, <a href="https://crates.io/crates/luminance">luminance</a>
might seem a bit less complete that, again, e.g. <a href="https://crates.io/crates/gfx-hal">gfx-hal</a>. And that would be likely right.
However, if someone likes the API and wants something in, I accept both issues and PRs. There
are several examples of people asking for features and having me implement them. There’s also
some folks who actually wrote the code and it’s now in <a href="https://crates.io/crates/luminance">luminance</a>!</li>
<li>Head over to <a href="https://github.com/phaazon/luminance-rs/issues">https://github.com/phaazon/luminance-rs/issues</a> if you want to start discussing
the feature set.</li>
</ul>
</li>
</ul>
<p>That’s the current situaton. A very important other aspect is that <a href="https://crates.io/crates/luminance">luminance</a> was designed, in the
first place, to be <em>backend agnostic</em>. I quickly decided not to go this way and stick to a simpler
design with OpenGL only.</p>
<blockquote>
<p>A comparison between <a href="https://crates.io/crates/luminance">luminance</a> and other famous crates is
<a href="https://phaazon.net/blog/luminance_comparing">available here</a>.</p>
</blockquote>
<p>However, I have — again! — changed my mind. Today, I want to have a system of backend in
<a href="https://crates.io/crates/luminance">luminance</a>. Maybe not something as complex and complete as the <strong>gfx-rs</strong> project, but something
that will allow those backends:</p>
<ul>
<li>Any <strong>OpenGL</strong> version: missing features from older versions will be implemented (if not too
much work is needed) by <a href="https://crates.io/crates/luminance">luminance</a> software patches.</li>
<li>At least one <strong>WebGL</strong> implementation and it must be WASM compatible.</li>
<li><strong>Vulkan</strong>.</li>
</ul>
<p>I really do not target <strong>Metal</strong>, <strong>DirectX12</strong> nor any other graphics backend you might have in
mind. However, if you think it’s possible to do so, then we should discuss about it. Nevertheless,
as I said above, <a href="https://crates.io/crates/luminance">luminance</a> is need-driven: as I don’t need <strong>DirectX12</strong>, I will not work on it
unless someone REALLY needs it and provides a PR (or at least enough for me to work on it… including
beers).</p>
<h1>What to expect</h1>
<p><strong>luminance-1.0.0</strong> will ship… as soon as possible. The current feature set is huge, though:</p>
<ul>
<li>Add the <code>"std"</code> feature-gate. This will help use <a href="https://crates.io/crates/luminance">luminance</a> with specific contexts, especially
for demoscene or size-limited devices. Currently, the work on <code>no_std</code> has been saved for later
as <a href="https://github.com/rust-lang/rust/issues/51540">I had hit a bug last time I checked</a>.</li>
<li>Change the <code>Tess</code> interface. Now, a <code>TessBuilder</code> must be used. This allows for three major
enhancements:
<ul>
<li>Vertex instancing.</li>
<li>Deinterleaved memory.</li>
<li>Builder pattern.</li>
</ul>
</li>
<li>Enhance the quality and variety of examples.</li>
<li>Introduce the <strong>luminance-derive</strong> crate. This crate provides several proc-macros used to
implement various <code>unsafe</code> traits easily, without writing any <code>unsafe</code> code. Among those:
<ul>
<li>The <code>Vertex</code> trait, used to create vertex types.</li>
<li>The <code>Semantics</code> trait, used to create vertex semantics types.</li>
<li>The <code>UniformInterface</code> trait, used to create shader <em>uniform interfaces</em>.</li>
<li>All the previous <code>macro_rules</code> are removed.</li>
</ul>
</li>
<li>Support a new dynamic way to get uniforms. Sometimes, you don’t know in advance the uniforms
your shader is going to use. When such a case arise, you might be tempted to have a minimal
<em>uniform interface</em> — if none. <em>Dynamic uniform lookups</em> allow you to query uniforms on the
fly. That has obviously a runtime cost but it can be handy for lots of situations (GUI editors,
scripting, etc.).</li>
<li>Update framebuffer code so that <em>color slots</em> and <em>depth slots</em> are easier to use.</li>
<li>Introduce the concept of <em>drivers</em>. Drivers allow code to be parametered by a type which
represents a given implementation to use for the graphics code. You can have a <code>GL33</code> driver
for <strong>OpenGL 3.3</strong>, a <code>GL40</code>, <code>GL44</code>, <code>GL45</code> etc., but you can also have <code>WebGL</code> and <code>VK10</code>.</li>
<li>Add vertex instancing. Vertex instancing allows to instantiate objects by providing instance
data directly in a <code>Tess</code> — instead of using the current method with GPU <code>Buffer&lt;_&gt;</code>. This was
asked by several people and I just couldn’t ignore such an interesting and useful feature!</li>
<li>Support for deinterleaved memory. Deinterleaved memory allows for more granularity on how data
is fetched GPU-side. With the legacy <a href="https://crates.io/crates/luminance">luminance</a> situation, data is completely interleaved,
which means that a vertex’s attributes all follow each other in a GPU buffer. This is both nice
and bad: if you need to access all attributes of a vertex, this is pretty good, since
cache-locality will be in your advantage. However, if you’re only interested in positions, for
instance, you will waste your cache lines with attributes you will never read from!
Deinterleaved memory stores each attributes in a different GPU memory region, allowing for a way
better memory scheme for such uses. People using the <a href="https://en.wikipedia.org/wiki/Entity_component_system">ECS</a> pattern might be used to
deinterleaved memory.</li>
<li>Add vertex primitive restart. This allows to use tessellation indexing modes such as
<code>TriangleFan</code> or <code>LineStrip</code> and “cut” a primitive if an index is equal to a given value. This
is very useful for implementing terrains, quadrics or a lot of other nice things.</li>
<li>Introduce <em>vertex semantics</em>. This is one of the sexiest and most exciting feature of this next
release. I got the idea by thinking about the fact <a href="https://crates.io/crates/luminance">luminance</a> should be low-level but provide
very quick access to building high-level blocks. I got the idea with vertex semantics via my
<a href="https://crates.io/crates/spectra">spectra</a> crate. The idea is simple: currently, we must define a <code>Vertex</code> type and the order
in which the fields appear in the <code>struct</code> defines the bindings the GPU will present to
shaders… but it’s the user responsibility to tell how the shader will fetch those attributes.
This can lead to very wrong situations in which, for instance, the GPU present the <em>normal</em>
attribute as having the index <code>4</code> but the shader tries to fetch them on index <code>2</code>. Vertex
semantics fix this situation by adding an intermediate layer both the user and the GPU (i.e. via
<a href="https://crates.io/crates/luminance">luminance</a>) must speak: semantics. Semantics are user-defined and none is hardcoded in
<a href="https://crates.io/crates/luminance">luminance</a>. As soon as a type uses vertex semantics, all the type system knows how to forward
that information to shaders and what shaders should do to fetch them. The other bonus, ultra
cool thing is that shaders and memory buffers now compose way better: since vertex semantics
define a sort of namespace, you will never have two shaders using the same index for different
semantics (unless you use completely different vertex semantics type). A very cool feature I’m
proud of and that I will detail in a future article / <a href="https://crates.io/crates/luminance">luminance</a> tutorial.</li>
<li>And lots of other doc fixes, enhancements, etc.</li>
</ul>
<h1>One word about the feature set and spare-time</h1>
<p>You might be wondering <em>“Woah, this is such a big feature set. A lot of things are coming! Why not
having split the feature set into several releases?”</em>, and that’s a good question.</p>
<p>Lately, I’ve been feeling on-and-off about everything I do on my spare-time on the FOSS level. I
have several crates I care about and sometimes people show up and start asking for features and/or
bug fixes. This takes some time and after a long day of work, I sometimes feel that I just want to
hack on things I like, play my electric guitar, go to swim or wakeboard or just spend time with
people I care about and love.</p>
<p>Currently, <a href="https://crates.io/crates/luminance">luminance</a> — even though it has an unexpected amount of downloads! — is not a famous
crate. People nowadays talk about crates living in a higher-level stack, such as <a href="https://crates.io/crates/ggez">ggez</a>, <a href="https://crates.io/crates/gfx">gfx</a>,
<a href="https://crates.io/crates/amethyst">amethyst</a>, <a href="https://crates.io/crates/piston">piston</a>, etc. I don’t communicate <em>a lot</em> about <a href="https://crates.io/crates/luminance">luminance</a> because of, mostly, two
things:</p>
<ol>
<li>Imposter syndrom.</li>
<li><em>Avoid success at all cost</em>.</li>
</ol>
<blockquote>
<p>I guess Haskellers will get the second point. ;)</p>
</blockquote>
<p>My own philosophy about software and especially my own software is that it should have a top-tier
quality, both in terms of API design, elegance, type safety, performance and documentation. The
<a href="https://docs.rs/warmy">front documentation of warmy</a> is a good example of something I continuously
seek, because good documentation helps my future myself to read the code and public interface but
also for onboarding people. Lots of projects — especially in professional industries — suffer from
<em>knowledge bubbles</em>, in which just a bunch (often only one!) of people know about <code>A</code> and <code>B</code> and
when someone joins in, they’re lost and have to ask questions. As an engineer, I always write
everything I know in whatever source-of-truth the company I’m in can offer (it can be a Confluence
server; a git repository; a wiki; whatever) because sharing knowledge and writing processes is key
in engineering — also, pro tip: I highly value that when interviewing candidates!</p>
<p>But the more I advance and grow up, the more things I learn and the more things I master. And as I
master new things, I tend to get used to them. And the “mental effort” it requires tends to slowly
disappear. I remember when I tried (hard!) to wrap my finger around what a <em>profunctor</em> is, or what
<em>free monads</em> or even <em>natural transformations</em> are and how to use them. It was hard. As a friend of
mine (<a href="http://dicioccio.fr">@lucasdicciocio</a>) perfectly
summarized it some times ago, when we learn something as a beginner, we struggle. But people who
master those concepts struggle even harder when trying to learn the next concepts at hand. And I’m
not an exception. Today, <em>free monads</em> are something I know (even though I don’t use them; it’s a
myth! it’s a trap! :D) and I feel like I’m not legitimate to talk about them because <em>they feel
easy</em> — and it’s normal, since I’ve learned them. But I see new things to learn, new hard things
that I know I’ll be struggling with for a while. And now it feeds the imposter syndrom. You know,
when you’re 20, you feel like you know a lot of things (and you likely do) and that you’re pretty
confident with your skill base. Today, I’m 27, and I feel like I will always be a rookie, because
there is always something I don’t know to struggle with. And I find that very exciting. Of course,
I don’t project that on others, which is soooo paradoxal: I don’t expect a newcomer to tell me what
a <em>fundep</em> is or know lifetime elision rules in Rust. But I guess I have a very very strict way to
judge my knowledge. I still haven’t figured out if it’s a good thing or if it’s not.</p>
<p>I’ve been told that talking about what I do, what I think, what I know and my engineering philosophy
should help with this kind of questioning. So I decided to use that blog article to share some
thoughts about the next release of <a href="https://crates.io/crates/luminance">luminance</a> that took me so much of my spare-time (but happily!)
and to talk a bit about myself. I know a lot of people in the Rust and Haskell community don’t know
me, but I also know lots of other folks do know me, in both communities. I hope my little vent will
give people some relief — maybe you too have a similar experience? Please share your comments on
<a href="https://www.reddit.com/r/rust/comments/bhslno/pre_luminance100_random_thoughts_about">the Reddit thread this article is post in</a>.</p>
<p>As always, stick doing awesome things and spend your spare-time wisely. Spend time with the ones you
love and do the things you hecking love too!</p>
<p><em>Keep the vibes!</em></p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 26 Apr 2019 22:30:00 GMT</pubDate></item><item><title>The new luminance is there</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.40</link><description><![CDATA[<p>Today is the day <a href="https://crates.io/crates/luminance/0.40.0">luminance-0.40</a> got released. This is a <em>massive</em> update. Like never before in
the luminance ecosystem.</p>
<h1>(Short) story of what happened</h1>
<p>A huge amount of features, bug fixes, usability / comfort enhancement, new crates and architecture
redesign are gathered in that update. First, let’s talk about the new most important feature: the
redesign.</p>
<h2>The backend (re)design</h2>
<p>Before 0.40, luminance was an <a href="https://www.opengl.org">OpenGL 3.3</a> crate. If you’re not into graphics APIs, OpenGL is a
graphics normalized specification, built by an open group (Khronos) that has been around for
decades. Because it’s an open  specification, lots of vendors can implement and provide drivers to
have OpenGL support, but also Free and OpenSource projects. The 3.3 version was old enough to
support a wide variety of devices, even old ones, while still having interesting features allowing
to do lots of useful things.</p>
<p>However, as time passes, new devices and technologies emerge. <a href="https://www.khronos.org/registry/webgl/specs/latest/1.0">WebGL 1.0</a>, which appeared in 2011,
is to the Web what OpenGL is to desktop and console graphics programming. It is an open API
browsers can implement to provide support for accelerated 2D/3D rendering in your browser.</p>
<p><a href="https://www.khronos.org/registry/webgl/specs/latest/2.0">WebGL 2.0</a>, which appeared more recently, in 2017 (partial support in some browsers), provide an
API almost identical to what you find in OpenGL 3.3 (with some tricky caveats).</p>
<p>I’ve been wanting to have a way to write “luminance code”, and have it compile for other
platforms than desktops, like Web and mobile (Android and iOS). With the previous architecture,
since luminance was bound to OpenGL 3.3, it was not possible. Here comes the new archicture.</p>
<h2>Backends… backends? backends!</h2>
<p>The whole luminance crate was rewritten so that its public-facing interface is completely agnostic
of what <em>executing GPU code</em> is about. No OpenGL anymore. Instead, it depends on a type variable,
referred as <code>B</code> in luminance’s types, which must satisfy a set of conditions to be considered a
<em>backend</em>.</p>
<p>A backend is an implementation of the expectations made on the public-facing interface. For
instance, you can <code>set</code> a GPU buffer by providing an index and a value. That’s the public facing
part. However, what happens on the GPU is left to the backend implementation.</p>
<p>This new way of doing split the luminance crate into several ones:</p>
<ul>
<li><a href="https://crates.io/crates/luminance">luminance</a>, which is the core, more abstract and common crate to whole. You use types coming
from this crate to explain <em>what you want to do</em>. How it’s done is made by other crates.</li>
<li><a href="https://crates.io/crates/luminance-gl">luminance-gl</a>, which is the OpenGL backend crate. It roughly contains a type — <code>GL33</code> — which
can be passed around <a href="https://crates.io/crates/luminance">luminance</a> to select the OpenGL 3.3 backend.</li>
<li><a href="https://crates.io/crates/luminance-webgl">luminance-webgl</a>, which is the WebGL 2.0 crate. I decided to go with WebGL 2.0 instead of 1.0
because of how similar it is to OpenGL 3.3 (which was the existing code base). It’s possible to
add support for WebGL 1.0, but it will require a lot of effort to support the feature that are
not present in the specification.</li>
</ul>
<p>Added to this, because of how generic and abstract <a href="https://crates.io/crates/luminance">luminance</a> now is, people might get issues when
trying to write code that will work whatever the backend. When using the <code>set</code> functions on a
buffer, it is required that the type implements a trait from <a href="https://crates.io/crates/luminance">luminance</a> —
<code>luminance::backend::buffer::Buffer</code>. So it’s likely the user will have to constrain types and it
might get boring. For this reason, a new crate was created: <a href="https://crates.io/crates/luminance-front">luminance-front</a>.</p>
<p><a href="https://crates.io/crates/luminance-front">luminance-front</a> allows people to write code using types from <a href="https://crates.io/crates/luminance">luminance</a> that got their <code>B</code> type
variable replaced by a backend type at compile-time. This is done by inspecting the compilation
target, and features you enable. You are then free to write your code without worrying about
whether traits are implemented, since <a href="https://crates.io/crates/luminance-front">luminance-front</a> takes care of that for you. The
re-exported types are simple aliases to <a href="https://crates.io/crates/luminance">luminance</a>’s types, so your generic code — if you write
any — will be compatible.</p>
<h2>Changelog, migration guide</h2>
<p>The changelog is quite massive for a single update. I wish I had the opportunity to split it into
several updates, but the redesign meant a lot of changes, and I got several very interesting PRs
and feature requests. Among very new stuff:</p>
<ul>
<li>A new platform crate has appeared: <a href="https://crates.io/crates/luminance-sdl2">luminance-sdl2</a>, which adds support for the <a href="https://crates.io/crates/sdl2">sdl2</a> crate.</li>
<li><a href="https://crates.io/crates/luminance-webgl">luminance-webgl</a> and <a href="https://crates.io/crates/luminance-web-sys">luminance-web-sys</a>, to support the Web!</li>
<li><a href="https://crates.io/crates/luminance-front">luminance-front</a>, which is a <em>front</em> crate to ease working with <a href="https://crates.io/crates/luminance">luminance</a> types.</li>
<li>The type system experience has been greatly improved. Most of the time, you will not have to
annotate types anymore — like <code>Program</code> or <code>Tess</code>.</li>
<li>About <code>Tess</code>, a BIG update has landed, has it’s now heavily typed (vertex type, index type,
vertex instance data type, memory interleaving type).</li>
<li>More render states features, such as the possibility to enable or disable depth writes, separate
RGB/alpha blending, etc. etc.</li>
</ul>
<p>A huge thanks to all the people who contributed. It means a lot to me! The list of changes is big,
so I’ll leave you with the <a href="https://github.com/phaazon/luminance-rs/blob/master/luminance/CHANGELOG.md#040">changelog here</a>.</p>
<blockquote>
<p>Disclaimer: this is the <a href="https://crates.io/crates/luminance">luminance</a> changelog, but all the crates have their own, too.</p>
</blockquote>
<p>Because that update is massive and has some breaking changes, I have also decided to include a
<em>migration guide</em>. The migration guide is a section in <a href="https://crates.io/crates/luminance">luminance</a>’s changelog to explain all
the things you need to do to migrate from luminance-0.39 to luminance-0.40. <strong>Please: if you find
something that is missing or you’re struggling with, please open an issue, a PR, or ping me on
whatever platform you like</strong>.</p>
<h2>What’s next to read</h2>
<p>The <code>Tess</code> type now supports slicing deinterleaved memory in very, very elegant ways, checked
at compile-type and type-driven. The whole new <code>Tess</code> type design is so new that a blog article
is on its way about that topic, as I find it very interesting.</p>
<p>While testing the redesign and new features, I have implemented a <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway’s Game of Life</a> for
fun — no code available yet. I plan to remake it from scratch and record the whole experience
on video. That will be a new format and I want to hear from people and know whether they like the
format.</p>
<p>Finally, the <a href="https://rust-tutorials.github.io/learn-luminance">book</a> was also updated. The three chapters have been updated and some features have
been added — like aspect ratio in chapter 3; I don’t understand why I haven’t talked about that
earlier!</p>
<p>The next steps for luminance and myself, besides the blog articles and video, are… getting some
rest. I plan to get back to demoscene production, and I’m 100% sure there’s already a lot to do
with that current release of <a href="https://crates.io/crates/luminance">luminance</a>. I plan to experiment with new backends as well, such as
an OpenGL 2.0 backend (if possible), to support really old hardware; an OpenGL 4.6 backend for
modern hardware (easy), as I already know that API pretty well; an OpenGL ES backend for mobile
development, and later, a <a href="https://www.khronos.org/vulkan">Vulkan</a> and, perhaps, a <a href="https://gpuweb.github.io/gpuweb">WebGPU</a> backends.</p>
<p>Stay tuned for the <code>Tess</code> article, the Game of Life video… and keep making super fun video games,
animation and toys with <a href="https://crates.io/crates/luminance">luminance</a>!</p>
<p><em>And keep the vibes!</em></p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 16 Jul 2020 18:49:00 GMT</pubDate></item><item><title>Optimizing the permutation algorithm of hop.nvim</title><link>https://strongly-typed-thoughts.net/blog/hop-trie-backtrack-filling</link><description><![CDATA[<p>It’s been a while since I haven’t written something here. Today was a very exciting day to me as <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a> has been
receiving more and more attention (and great contributions) from the <a href="https://neovim.io/">Neovim</a> community lately. I didn’t expect
people to adopt Hop that quickly and getting <em>actually</em> interested. So, thanks for all the feedback and the great
support!</p>
<p>Before starting up, if you don’t know what <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a> is, here’s a small excerpt from the <code>README</code>:</p>
<blockquote>
<p>Hop is an EasyMotion-like plugin allowing you to jump anywhere in a document with as few keystrokes as possible.
It does so by annotating text in your buffer with hints, short string sequences for which each character represents
a key to type to jump to the annotated text. Most of the time, those sequences’ lengths will be between 1 to 3
characters, making every jump target in your document reachable in a few keystrokes.</p>
</blockquote>
<p>Today, I want to talk about something at the core of the design of Hop: permutations, and a new algorithm I implemented
to (greatly) optimize permutations in Hop.</p>
<blockquote>
<p>Disclaimer: it might be possible that the algorithm described here has an official name, as I’m using a well-known
data structure, traversing in a way that is also well established. Even though I came up with it, I don’t really know
whether it has a name so if you recognize it, or think it is similar to something, feel free to tell me!</p>
</blockquote>
<h1>Permutations</h1>
<p>When you run a Hop command (whether as a Vim command, such as <code>:HopWord</code>, or from Lua, like <code>:lua require'hop'.hint_words()</code>), Hop is going to place <em>labels</em>, called <em>hints</em>, in your window, over your buffer,
describing sequences of keys to type to jump to the annotated location in the buffer. Those key sequences are generated
as permutations of your <em>input key set</em>, of different lengths. Your input key set is basically the set of keys you
expect to type to hop around. For instance, the default key set is made for QWERTY and is:</p>
<pre><code>asdghklqwertyuiopzxcvbnmfj
</code></pre>
<p>The way Hop works is taking those keys and generating permutations of increasing length. The goal is to obviously type
as less as possible (remember: <em>Neovim motion on speed!</em>), so we want to generate 1-sequence permutations first, such as
<code>a</code>, <code>s</code>, <code>d</code>, etc., then switch to 2-sequence permutations, e.g. <code>aj</code>, <code>kn</code>, etc. The most important part of the work
done by Hop is to generate those permutations in a smart way.</p>
<p>Until today, I had implemented a single algorithm doing this. Let’s describe it so that I can introduce the actual topic
of this article.</p>
<h1>First permutation generation algorithm</h1>
<p>The first algorithm used to generate permutations in Hop was designed around the idea of being able to generate
permutations in a <a href="https://en.wikipedia.org/wiki/Corecursion">co-recursive</a> way (even though it’s not using co-recursion in Lua): given a permutation, you can
generate the next one by running the algorithm on the permutation. Doing that over and over generates more permutations.
You can sum it up like this:</p>
<pre><code class="language-lua">let first_perm = next_perm({})
let second_perm = next_perm(first_perm)
let third_perm = next_perm(second_perm)
-- etc. etc. stop at a given condition and return all the permutations
</code></pre>
<p>In functional programming languages, we call that kind of co-recursion an <em>unfold</em>. We stop building / generating when
meeting a condition. In our case, the condition is when we reach the number of permutations to generate. This first
algorithm had several constraints I wanted satisfied:</p>
<ol>
<li>It has to be fast and co-recursive, so that we can pass it the number of permutations to generate and simply generate
them until we have reached the number of desired permutations.</li>
<li>It has to take into account the user input key set.</li>
<li>It has to generate permutations minimizing the number of keys required to jump.</li>
<li>Because permutations will be distributed based on the <a href="https://en.wikipedia.org/wiki/Taxicab_geometry">Manhattan distance</a> to the cursor, they must be ordered /
sorted in a way that the first permutations in the list are the shorter ones and the last ones are the longer ones.</li>
</ol>
<p>We already saw <em>1.</em>, so let’s talk about the three remaining points. Taking into account the key set of the user, with
this algorithm, is done by splitting it into two sub-sets:</p>
<ul>
<li>A set called <em>terminal key set</em>, containing terminal keys. Those keys are used <em>only</em> in terminal position in
sequences — i.e. at the very right-most position in the sequence. For instance, in the sequence <code>abcde</code>, <code>e</code> is a
terminal key.</li>
<li>A set called <em>sequence key set</em>, containing sequence keys. Those are keys found as prefixes of terminal keys in
sequences. For instance, in the sequence <code>abcde</code>, <code>a</code>, <code>b</code>, <code>c</code> and <code>d</code> all appear before <code>e</code>, so they are sequence
keys.</li>
</ul>
<p>How to know which key is terminal and which is sequence? Well, I use a simple parameter for that, that can be modified
in the user configuration of Hop: <code>term_seq_bias</code> (<code>:help hop-config-term_seq_bias</code> if you are interested). It is a
floating number parameter specifying the ratio of terminal vs. sequence keys to use. Imagine you have 100 keys in your
key set (you typically will have between 20 and 30), setting this parameter to <code>3 / 4</code> makes it use 75 terminal keys and
25 sequence keys.</p>
<p>Then, given those two sets, we start from the 0-sequence permutation, a.k.a. <code>{}</code>, and we start using terminal keys. If
we have <code>abc</code> as terminal keys and <code>de</code> as sequence keys, we will get the permutations <code>'a'</code>, <code>'b'</code> and <code>'c'</code> for
<code>n = 3</code>. If we ask permutations for <code>n = 4</code>, we will get <code>'a'</code>, <code>'b'</code>, <code>'c'</code> but not <code>'d'</code>, as it’s a sequence key.
Instead, we will start a new layer by incrementing the dimension of sequences, yielding 2-sequence permutations: we will
then generate <code>'da'</code>. For <code>n = 6</code>, the last permutation in the list is <code>'dc'</code>, which means that for <code>n = 7</code>, we have to
do something, as we have run out of terminal keys. Instead of starting a new layer (3-sequence permutations), we
traverse the permutation in reverse order, checking that we have exhausted all the sequence keys. Here, we can see that
<code>'d'</code> is not the last sequence key, so we can use the next one, <code>e</code>, and start a new sequence at <code>ea</code>, then <code>eb</code>, <code>ec</code>…
and guess what permutation is the next? Since we have run out of both terminal keys and sequence keys, we need to use
3-sequence keys: the next permutation will be <code>dda</code>.</p>
<p>This algorithm is interesting because it allows people to <em>bias</em> (hence the name) the distribution of terminal and
sequence keys. However, it is pretty obvious that this algorithm does a pretty poor job at minimizing the overall number
of keys to type: it will minimize the number of keys to type for short hops, mostly around the cursor and at
mid-distance. I highly advise to use either <code>3 / 4</code> or even <code>0.5</code> for the bias value. Other values yield… weird results.</p>
<h1>The problem and finding a solution</h1>
<p>Very quickly, I have noticed something a bit annoying with Hop. Even though it is pretty fast, the distribution of keys
for long jumps is often using 3-sequence permutations. And where does it make sense to use Hop the most? For mid to long
range jumps. Vim and Neovim are already pretty good at short jumps. For instance, jumping to characters on the same line
can be achieved with <kbd>f</kbd> and <kbd>F</kbd>. If you want to jump to a word on the line above, you can just press
<cmd>k</cmd> and use horizontal jumps.</p>
<p>I don’t necessarily <em>agree</em> that those are always ideal (and, well, I do use Hop for short hops too), but the situation
is not ideal for long jumps in Vim / Neovim. See, I’ve been using Vim and Neovim for more than 12 years now, and I’ve
seen lots of people using it. Most of the time, what people use for long jumps is either (or a combination of) one of
these:</p>
<ul>
<li>Turn on <code>relativenumber</code> and look at relative numbers to jump to the line where the location appears in, press
something like <code>17k</code> or <code>17j</code>, then use the <kbd>f</kbd> / <kbd>F</kbd> motions.</li>
<li>Use the real line number and commands such as <code>:153</code> or <code>153gg</code> to jump to line 153, and do the same as the previous
point.</li>
<li>Use <kbd>{</kbd> / <kbd>{</kbd> / <kbd>%</kbd> to quickly skip large portions of text and arrive at destination in
less typing effort (I’m actually a big user of <kbd>%</kbd>).</li>
<li>Use marks, LSP, tags etc. to directly jump to semantic locations.</li>
</ul>
<p>Among all those options, even when you combine them, you will always have to type more keys / take more time to do
long jumps. For instance, considering we can jump with <kbd>f</kbd> to our location <code>l</code> because it is unique on the line we
want to go to, assuming it’s on line 1064, 45 lines above our cursor:</p>
<ul>
<li>With <code>relativenumber</code>, we have to press <code>45kfl</code>: between 5 and 6 keys (depending on whether you have to press shift on
your keyboard layout for the digits).</li>
<li>With real line number, <code>1064ggfl</code>, between 8 and 9 keys.</li>
</ul>
<p>So clearly, Hop should help for those jumps as much as possible, and the previous algorithm, even though already an
enhancement over typing relative numbers or real line numbers, can still be enhanced. It is already pretty good because
it will provide you with, most of the time, between 1-sequence and 3-sequence permutations. For long jumps, assume the
longest: 3 keys, plus one key to trigger the Hop mode, you get 4 keys to type at most to jump anywhere.</p>
<p>The goal is to reduce that to 2 keys, i.e. 2-sequence at most, most of the time — 3 keys if you count the key to start
Hop. In order to understand the concept of this new algorithm, let’s focus on what was wrong with the former. Consider
the following, using <code>abc</code> as terminal keys and <code>de</code> as sequence keys:</p>
<pre><code class="language-lua">-- n = 3
a b c

-- n = 4
--  last 1-sequence
--  v
a b c da

-- n = 6
a b c da db dc

-- n = 9
a b c da db dc ea eb ec

-- n = 10
--                   last 2-sequence
--                   v
a b c da db dc ea eb ec dda
</code></pre>
<p><img src="https://phaazon.net/media/uploads/hop_term_seq_bias_showcase.png" alt="" /></p>
<p>You can see that after only 9 words, we are already using 3-sequence permutations. What about, for instance, the
following sequences:</p>
<pre><code class="language-lua">aa ab ac bb dd de …
</code></pre>
<p>Obviously, because of the rules explained above about the difference between <em>terminal</em> and <em>sequence</em> keys, those are
forbidden: indeed, they would yield <em>ambiguous</em> sequences. For instance, <code>aa</code> and <code>a</code> both start with the same key and
one is terminal at a given depth (1-sequence) while the other still has one level (2-sequence): what should we do?
Trigger the 1-sequence and never be able to jump to the 2-sequence? Use a timeout so that we have the time to type the
second <code>a</code>? But that would make jumping to terminal keys feel delayed / slow, so that’s a hard no. What’s the
problem?</p>
<p>The problem is that we are not efficiently using the key space by forbidding those sequences. If you pay attention, you
should actually be able to use <em>all possible 2-sequence permutations</em>. But in order to do that… we have to forbid using
1-sequence permutations! Forbidding that will make all 2-sequence permutations unique and unambiguous. The difference is
massive: instead of having only 9 permutations shorter than 3-sequence ones, we know have 25, which is the number of
keys in the user key set squared: <code>'abcde'</code> is 5 keys, and 5² is 25. For the default key set for QWERTY, which has 26
keys, it means 26² = 676 maximum 2-sequence permutations, which is a comfortable number for the visible part of your
buffer.</p>
<h1>The new algorithm: tries and backtrack filling</h1>
<p>The new algorithm’s idea relies on using all the keys from your input key set. It doesn’t split it into terminal and
sequence keys. It creates <a href="https://en.wikipedia.org/wiki/Trie">tries</a> (a type of search tree, optimized for packed storage and fast traversal) to store the
permutations and backtracks to remove ambiguity as we ask for more permutations. For instance, using the user input key
set <code>'abcde'</code>:</p>
<pre><code class="language-lua">-- n = 5
a b c d e

-- n = 6
--      e was transformed to ea
--      v
        a b
a b c d e e

-- n = 7
        a b c
a b c d e e e

-- n = 9
        a b c d e
a b c d e e e e e
</code></pre>
<p>As you can see, for <code>n = 6</code>, instead of adding a new layer, the algorithm backtracks to fill what is possible to fill:
in our case, <code>e</code>, by replacing it with <code>ea</code> and inserting the new permutation at <code>eb</code>. Once a given layer is saturated,
the algoritm backtracks again, trying to saturate another layer:</p>
<pre><code class="language-lua">-- n = 10
--    d was transformed to da
--    v
      a b a b c d e
a b c d d e e e e e

-- n = 13
      a b c d e a b c d e
a b c d d d d d e e e e e

-- n = 17
    a b c d e a b c d e a b c d e
a b c c c c c d d d d d e e e e e

-- n = 21
  a b c d e a b c d e a b c d e a b c d e
a b b b b b c c c c c d d d d d e e e e e

-- n = 25
a b c d e a b c d e a b c d e a b c d e a b c d e
a a a a a b b b b b c c c c c d d d d d e e e e e
</code></pre>
<p>Here, we have completely saturated the 2-sequence permutations, yielding 25 of them, and cannot backtrack anymore. When
backtracking fails, we simply augment the last trie, deepest:</p>
<pre><code class="language-lua">-- n = 26
                                                a b
a b c d e a b c d e a b c d e a b c d e a b c d e e
a a a a a b b b b b c c c c c d d d d d e e e e e e
</code></pre>
<p>And we go on. Here, you can see a repetition of the same key, such as <code>e e e e</code>, but because those are tries, they are
going to be actually encoded as a single node, having several children. Consider this example:</p>
<pre><code class="language-lua">-- encode these permutations
--
--         a b c d
-- a b c d e e e e

local trie = {
  {
    key = 'a';
    trie = {}
  },
  {
    key = 'b';
    trie = {}
  },
  {
    key = 'c';
    trie = {}
  },
  {
    key = 'd';
    trie = {}
  },
  {
    key = 'e';
    trie = {
      { key = 'a'; trie = {} },
      { key = 'b'; trie = {} },
      { key = 'c'; trie = {} },
      { key = 'd'; trie = {} },
    }
  }
}
</code></pre>
<p>Two interesting properties about this algorithm:</p>
<ul>
<li>It does distribute permutations more equitably across your buffer as the number of permutations to generate grows, but
will do it from the end of tries, making the first permutations the shortest as long as possible.</li>
<li>When tries are saturated, we can lose 1-sequence permutations, but such a scenario authorizes to jump anywhere with
only 2-sequence permutations.</li>
</ul>
<p><img src="https://phaazon.net/media/uploads/hop_trie_backtrack_filling_showcase.png" alt="" /></p>
<h1>Conclusion</h1>
<p>This new algorithm was a lot of fun to come up with, design, test, implement and eventually use, because yes, it is
already available (as a default) in <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>. If for any reason you would prefer to use the first one, you can still
change that in the user configuration (see <code>:h hop-config-perm_method</code>). Another point, too: since the support for the
<code>setup</code> workflow, it’s been possible to locally override Lua function calls <code>opts</code> argument. You can then make a key
binding using the first algorithm and another one using the new one – all this is explained in the vim help page 😉.
It’s up to you!</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 15 Mar 2021 00:00:00 GMT</pubDate></item><item><title>Optimizing Advent of Code 2021 Day 16 part 1 &amp; 2</title><link>https://strongly-typed-thoughts.net/blog/aoc-21-day-16</link><description><![CDATA[<p>I have been doing <a href="https://adventofcode.com">Advent of Code 2021</a>, like every year since 2018, and I would like to share
some reflections about it. This year, I would like to talk about the implementation I made for <a href="https://adventofcode.com/2021/day/16">day
16</a>.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#----day-16-packet-decoder------reflections">— Day 16: Packet Decoder — // reflections</a>
<ul>
<li><a href="#header-parsing">Header parsing</a></li>
<li><a href="#the-encoding-problem">The encoding problem</a></li>
<li><a href="#the-other-encoding">The other encoding</a></li>
<li><a href="#bit-packing-solution">Bit packing solution</a></li>
<li><a href="#packed-next_bits">Packed next_bits</a></li>
<li><a href="#decoding-a-literal-packet">Decoding a literal packet</a></li>
<li><a href="#parsing-operators">Parsing operators</a></li>
<li><a href="#part-1-summing-packet-headers-versions">Part 1: summing packet headers’ versions</a></li>
<li><a href="#part-2-evaluating-the-expression">Part 2: evaluating the expression</a></li>
</ul>
</li>
</ul>
<!-- vim-markdown-toc -->
<h1>— Day 16: Packet Decoder — // reflections</h1>
<p>This problem doesn’t look too hard at the first glance, but has some hidden complexities than I thought pretty
interesting. Just to sum up if you don’t want to open the link, the first part requires you to parse a hexadecimal
number and interpret its binary representation as a <em>packet</em>, like what you would find in a network packet format,
basically. You need to be able to parse correctly a single packet, which can contain nested packets. In this puzzle,
packets represent expressions, that can either be:</p>
<ul>
<li>A literal number.</li>
<li>An operation between packets.</li>
</ul>
<p>Each packet have a header, comprising a header and its type (literal or operator), and then the actual data of the
packet.</p>
<p>From this context, it seems pretty obvious to assume this is going to be a parsing problem. The first part asks you to
parse the packet and its nested packets, and add all the version numbers of the headers of all packets. The format goes
as this (quoting):</p>
<blockquote>
<p>Every packet begins with a standard header: the first three bits encode the packet <code>version</code>, and the next three bits
encode the packet <code>type ID</code>. These two values are numbers; all numbers encoded in any packet are represented as binary
with the most significant bit first. For example, a version encoded as the binary sequence 100 represents the number
4.</p>
</blockquote>
<p>Then, you have the description of a packet representing a literal number:</p>
<blockquote>
<p>Packets with <code>type ID 4</code> represent a literal value. Literal value packets encode a single binary number. To do this,
the binary number is padded with leading zeroes until its length is a multiple of four bits, and then it is broken
into groups of four bits. Each group is prefixed by a 1 bit except the last group, which is prefixed by a 0 bit.
These groups of five bits immediately follow the packet header. For example, the hexadecimal string <code>D2FE28</code> becomes:</p>
</blockquote>
<pre><code>110100101111111000101000
VVVTTTAAAAABBBBBCCCCC
</code></pre>
<blockquote>
<p>Below each bit is a label indicating its purpose:</p>
<ul>
<li>The three bits labeled <code>V</code> (<code>110</code>) are the packet <code>version</code>, <code>6</code>.</li>
<li>The three bits labeled <code>T</code> (<code>100</code>) are the packet <code>type ID</code>, <code>4</code>, which means the packet is a literal value.</li>
<li>The five bits labeled <code>A</code> (<code>10111</code>) start with a <code>1</code> (not the last group, keep reading) and contain the first four bits of the number, <code>0111</code>.</li>
<li>The five bits labeled <code>B</code> (<code>11110</code>) start with a <code>1</code> (not the last group, keep reading) and contain four more bits of the number, <code>1110</code>.</li>
<li>The five bits labeled <code>C</code> (<code>00101</code>) start with a <code>0</code> (last group, end of packet) and contain the last four bits of the number, <code>0101</code>.</li>
<li>The three unlabeled <code>0</code> bits at the end are extra due to the hexadecimal representation and should be ignored.</li>
</ul>
<p>So, this packet represents a literal value with binary representation <code>011111100101</code>, which is <code>2021</code> in decimal.</p>
</blockquote>
<p>I’m just going to focus on this first kind of packet because it’s already interesting.</p>
<h2>Header parsing</h2>
<p>From this description, if you have ever done bit parsing, you should already know that there is going to be a problem.
You can parse a literal packet using this kind of pseudo code (ignoring errors for simplicity, it’s AoC!):</p>
<pre><code class="language-rust">fn parse_packet(&amp;mut self) -&gt; Packet {
  let version = self.next_bits(3);
  let type_id = self.next_bits(3);

  match type_id {
    4 =&gt; todo!("literal parser"),
    _ =&gt; todo!("operator parser"),
  }
}
</code></pre>
<p>The big question is: how do we implement <code>next_bits</code>? Getting 3 bits of information? As you might know, the smallest
amount of information a computer can manipulate is a <em>byte</em>, which is 8 bits, so… how do we manipulate less than that?</p>
<h2>The encoding problem</h2>
<p>I would like to say one thing before going on. The input is a hexadecimal string. The first step of your solution is to
extract the binary representation. For instance, <code>A</code> (10 in decimal) is encoded as <code>1010</code> and <code>5A</code> is encoded as
<code>01011010</code>. As you can see (might already know), hexadecimal is easy to parse into bytes: group hexadecimal digits by
two, convert each digit to 4 bits, and glue them together. <code>5A</code> is <code>5 -&gt; 0101</code> and <code>A -&gt; 1010</code>, which gives <code>01011010</code>
as seen above.</p>
<p>But as we have seen with <code>next_bits</code> above, we will want to manipulate bits, not bytes. So you have several choices and
design decisions to make here:</p>
<p>You can go the « easy » way and store each bit as a byte. This is very suboptimal, because you are going to waste 8
times the amount of memory you actually need to store all this. For instance, <code>5A</code> (<code>01011010</code>), which is a single byte
on your computer, will require 8 bytes to be stored: <code>00000000, 00000001, 00000000, 00000001, 00000001, 00000000, 00000001, 00000000</code>. That is very inefficient, but it would work, if you put them in a <code>Vec&lt;u8&gt;</code>. To do that, you would
basically need to read one hexadecimal digit at a time (which has 4 bits), and then perform bitwise right shifts with
masking (<code>&amp; 0x1</code>) to extract the bit. Something like:</p>
<pre><code class="language-rust">fn inefficient_bit_extract(&amp;mut self, four_bits: u8) {
  for i in 0..4 {
    self.bits.push((four_bits &gt;&gt; i) &amp; 0x1);
  }
}
</code></pre>
<p>With this solution, implementing <code>next_bits</code> is easier, because you only need to <code>pop</code> the <code>Vec&lt;u8&gt;</code>. Let’s write
<code>next_bits</code> with this kind of implementation. Because we do not need more than 15 bits in this puzzle (explained later in
the puzzle text, not really important for now), we will return the read bits on <code>u16</code>:</p>
<pre><code class="language-rust">fn next_bits(&amp;mut self, n: usize) -&gt; u16 {
  assert!(n &lt; 16);

  let mut out = 0;

  for i in 0..n {
    out = (out &lt;&lt; i) | self.bits.pop() as u16;
  }

  out
}
</code></pre>
<p>Let’s explain quickly.</p>
<pre><code class="language-rust">    out = (out &lt;&lt; i) | self.bits.pop() as u16;
</code></pre>
<p>This will extract a single bit, like <code>00000001</code>, will convert it to 16-bit (so <code>0000000000000001</code>) and will <code>OR</code> it to
the number we are building. That number is left shifted at each iteration so that we only <code>OR</code> the least significant bit
at each iteration. For instance, if we call <code>next_bits(3)</code> and we get <code>101</code>, this implementation will basically store
this in <code>out</code> at each iteration:</p>
<ul>
<li>End of 1st iteration: <code>0000000000000001</code>.</li>
<li>End of 2nd iteration: <code>0000000000000010</code>.</li>
<li>End of 3nd iteration: <code>0000000000000101</code>.</li>
</ul>
<p>Easy, and works.</p>
<h2>The other encoding</h2>
<p>However, I am a perfectionist. This is not satisfying to me, and it has been years since I have been wanting to
implement something like what I am about to describe. I have been thinking about this problem with a different context
but it is similar: a <code>bool</code> value, in a computer, is stored on the minimal amount of data a computer can manipulate:
a byte. It means than storing a boolean value wastes 7 bits. If you have a single boolean value, there is nothing you
can do. But if you need several boolean values, like, six booleans… using 6 bytes still wastes 42 bits for 6 bits of
useful data! And 6 bits can hold in a single byte. So we should be able to store booleans in a packed array of bytes.
The idea is the following: imagine that you want to store six boolean value in a byte:</p>
<pre><code>00100101
ABCDEFGH
</code></pre>
<p>I have labelled each bit with a letter to make it easier to reference them. If you want to, for instance, the boolean
value stored at index 2 (<code>F</code> in our representation), all you have to do is to right shift that number using the index of
the element and 1-bit mask:</p>
<pre><code class="language-rust">fn read_boolean(byte: u8, index: usize) -&gt; u8 {
  assert!(index &lt; 8);

  (byte &gt;&gt; index) &amp; 0x1
}
</code></pre>
<p>If you want to set it, you need to do the opposite operation using a logical <code>OR</code>:</p>
<pre><code class="language-rust">fn set_boolean(byte: &amp;mut u8, index: usize) {
  *byte = *byte | (0x1 &lt;&lt; index);
}
</code></pre>
<p>I wanted to use this AoC 18 day to encode my solution as a packed array of bits, and still have the <code>next_bits</code>
interface that would allow me to get between 0 and 15 bits of data, encoded as <code>u16</code>. The catch is: because I bit pack
the hexadecimal number, it is going to lie in a <code>Vec&lt;u8&gt;</code>. And the whole complexity relies in the fact asking for <code>N</code>
bits can span across several bytes in the packed array…</p>
<h2>Bit packing solution</h2>
<p>The first thing we need to do is to think about the interface. I want that mutable <code>next_bits</code> interface. What it means
is that I want to be able to read as many bits as I want (given less than 16 bits), I should not have to worry about the
byte implications behind the scenes. The puzzle text clearly shows that when we are going to parse a literal value, we
will have to read groups of 5 bits. Reading two literal numbers already implies two bytes (10 bits), and even the first
group implies reading two bytes, because before that, you have the header on 6 bits (3 bits for the version, 3 bits for
the type ID), living 2 bits to read for the beginning of the group, and 3 bits in the next byte to read. That’s a lot of
complexity that should be hidden from the interface.</p>
<p>Let’s start with the <code>Decoder</code> type:</p>
<pre><code class="language-rust">#[derive(Clone, Debug)]
struct Decoder {
  bytes: Vec&lt;u8&gt;,
  bitpos: usize, // the position in bit in the byte array
}
</code></pre>
<p>This is fairly simple: <code>bytes</code> is the bit-packed array I mentioned, and <code>bitpos</code> is the current position in that array.
But it’s not the position in the <em>array elements</em>, it’s the position in <em>bits</em>. If <code>bytes</code> has 3 bytes (24 bits), then
<code>bitpos</code> can be between <code>0</code> and <code>23</code>. To know in which actual array element the bit is, we simply need to divide by 8.
For instance, if <code>bitpos = 17</code>, then <code>17 / 8 = 2</code> tells us that the bit is in <code>bytes[2]</code>. To know the actual bit
position in that byte, you will have guessed it, you need to take the modulo: <code>17 % 8 = 1</code>, which is the second most
significant bit. You can convert to least significant bit by subtracting that number to 8: <code>8 - 1 = 7</code>.</p>
<p>With all that information, let’s fill the bytes:</p>
<pre><code class="language-rust">impl Decoder {
  fn new(input: &amp;str) -&gt; Self {
    let digits: Vec&lt;_&gt; = input.chars().flat_map(|hexa| hexa.to_digit(16)).collect();
    let bytes = digits.chunks(2).map(|d| (d[0] &lt;&lt; 4 | d[1]) as u8).collect();

    Self { bytes, bitpos: 0 }
  }
</code></pre>
<p>Here, because this is AoC, I don’t care about errors, and hence use <code>flat_map</code> to remove <code>Option</code> or <code>Result</code> in
iterators. The important thing here is that I group hexadecimal digits by 2 (the <code>.chunks(2)</code> part), and then simply do
this:</p>
<pre><code class="language-rust">|d| (d[0] &lt;&lt; 4 | d[1]) as u8
</code></pre>
<p><code>d[0]</code> are the most significant 4 bits, so I left shift them by 4 bits, and <code>d[1]</code> is already correctly positioned, so
nothing to do with it. I convert to <code>u8</code> parse <code>to_digit(16)</code> yields <code>u32</code>.</p>
<p>Then, let’s implement <code>next_bits</code>.</p>
<h2>Packed next_bits</h2>
<pre><code class="language-rust">  fn next_bits(&amp;mut self, n: usize) -&gt; u16 {
    // how many bits are still available from the current byte
    //
    // 0000000011111111
    //      ^ position is 5, so we have 3 bits still available
    let bits_avail = 8 - self.bitpos % 8;

    if bits_avail &gt;= n {
      // we have enough in the current byte; compute the new position and right shift to align correctly
      let shift = bits_avail - n;
      let byte = (self.bytes[self.bitpos / 8] as u16 &gt;&gt; shift) &amp; ((1 &lt;&lt; n) - 1);
      self.bitpos += n;
      byte
    } else {
      // …
    }
</code></pre>
<p>The first step to do is to check whether the amount of bits requested would make us overlap with another byte given the
current <code>bitpos</code>. If it’s not the case, then the problem is trivial. As shown in the example, we can just simply right
shift by the number of available bits minus the requested amount (because we might still have room afterwards), and mask
the N least significant bits. By the way, this is a funny one: setting to 1 the N significant bits. If you have <code>N = 3</code>,
you want (on 8-bit here to simplify) <code>00000111</code>. How do you make that quickly? It’s actually quite simple: what is this
number? This <code>111</code>? It’s an odd number (because its least significant bit is <code>1</code>) and it looks like it’s almost 2³. It’s
actually 2³ - 1. And this makes sense: remember in school / when you were learning binary that the biggest number you
can represent on N bits is <code>2^n - 1</code>. Like, on 8-bit, 2⁸ is 256, and the biggest number you can represent is 255, as you
might already know, which is <code>11111111</code>. So, we can simply deduce <code>00111…1</code> is the biggest number for 2^N where N is the
number of <code>1</code>. And that number is <code>2^N - 1</code>. A power of 2, in binary, is a simple shift. So, <code>2^N</code> is <code>1 &lt;&lt; N</code>. And `2^N</p>
<ul>
<li>1<code>is…</code>(1 &lt;&lt; N) - 1`. You have it.</li>
</ul>
<p>We then increment <code>bitpos</code> by the number of read bits, and return the actual byte, for which the read bits are right
shifted.</p>
<p>Now, what happens when <code>bits_avail &lt; n</code>? We have to read <code>bits_avail</code> and shift them in the 16-bit output. Then we will
have to switch to the next byte, and read <code>n - bits_avail</code> additional bits, because <code>n</code> is the requested number of bits
to read and since we read every bits available from the current bytes, well, <code>n - bits_avail</code>. The « byte switching »
logic can be a bit overwhelming at first but is actually not that hard: once we have read <code>bits_avail</code>, we know that the
next byte will have 8 bits available. We can then divide the problem in two sub-problems, easier:</p>
<ol>
<li>Read and shift 8 bits of data as long as <code>n &gt;= 8</code>.</li>
<li>Read the remaining bits in the last byte.</li>
</ol>
<p>This logic is implemented in the <code>} else {</code> branch, so I will just copy the <code>else</code> and put the code, and explain it:</p>
<pre><code class="language-rust">    } else {
      // this is the annoying part; first, get all the bits from the current byte
      let byte0 = (self.bytes[self.bitpos / 8] as u16) &amp; ((1 &lt;&lt; bits_avail) - 1);
      self.bitpos += bits_avail;

      // then, left shift those bits as most significant bits for the final number
      let mut final_byte = byte0;

      // then, we need to get the remaining bits; compute the number of bytes we need
      let n = n - bits_avail;
      let bytes_needed = n / 8;

      // while we require more than 1 byte, the problem is trivial; just copy and left shift the bytes
      for _ in 1..=bytes_needed {
        let byte = self.bytes[self.bitpos / 8] as u16;
        self.bitpos += 8;
        final_byte = (final_byte &lt;&lt; 8) | byte;
      }

      // the remaining bits can be extracted from the last byte
      let n = n % 8;
      let shift = 8 - n;
      let byte = (self.bytes[self.bitpos / 8] as u16 &gt;&gt; shift) &amp; ((1 &lt;&lt; n) - 1);
      self.bitpos += n;

      final_byte = (final_byte &lt;&lt; n) | byte;

      final_byte
    }
  }
</code></pre>
<p>Let’s dive in.</p>
<pre><code class="language-rust">      let byte0 = (self.bytes[self.bitpos / 8] as u16) &amp; ((1 &lt;&lt; bits_avail) - 1);
      self.bitpos += bits_avail;
</code></pre>
<p>This is the current byte, from which, as said earlier, we need to read all available bits. Because we are going to read
everything from the current position to the least significant bit, we can just read the whole byte and apply a mask for
the <code>bits_avail</code> least significant bits (remember the explanation earlier). So <code>&amp; ((1 &lt;&lt; bits_avail) - 1)</code> does the job.
Because we have read <code>bits_avail</code> bits, we increment <code>bitpos</code> as well.</p>
<pre><code class="language-rust">      // then, left shift those bits as most significant bits for the final number
      let mut final_byte = byte0;
</code></pre>
<p>This part is not strictly needed as I could have called <code>byte0</code> <code>final_byte</code> in the first place, but I wanted a clear
mind about what was doing what. The idea for the following steps is that we are going to accumulate bits in <code>final_byte</code>
by left shifting it before applying the logical <code>OR</code>. The amount of shift to apply depends on the amount of bits read.
For the case where we read a whole byte, we will just right shift by 8 bits.</p>
<pre><code class="language-rust">      // then, we need to get the remaining bits; compute the number of bytes we need
      let n = n - bits_avail;
      let bytes_needed = n / 8;
</code></pre>
<p>This is a pretty straight-forward code given what I said above. <code>n - bits_avail</code> is the remaining amount of bits to
read. <code>bytes_needed</code> is the number of full bytes we will have to go through (in <code>self.bytes</code>), and from which we will
extract 8 bits each. The rest then makes more sense:</p>
<pre><code class="language-rust">      // while we require more than 1 byte, the problem is trivial; just copy and left shift the bytes
      for _ in 1..=bytes_needed {
        let byte = self.bytes[self.bitpos / 8] as u16;
        self.bitpos += 8;
        final_byte = (final_byte &lt;&lt; 8) | byte;
      }
</code></pre>
<p>Then, the remaining bits are the last part of the parsing problem. The idea is that we know we will have to read between
1 and 7 bits from that byte. That information is stored as <code>n</code> and <code>shift</code>:</p>
<pre><code class="language-rust">      let n = n % 8;
      let shift = 8 - n;
</code></pre>
<p>We then extract the information, and return the N-bit number:</p>
<pre><code class="language-rust">      let byte = (self.bytes[self.bitpos / 8] as u16 &gt;&gt; shift) &amp; ((1 &lt;&lt; n) - 1);
      self.bitpos += n;

      final_byte = (final_byte &lt;&lt; n) | byte;

      final_byte
</code></pre>
<h2>Decoding a literal packet</h2>
<p>Armed with <code>next_bits</code>, the problem really becomes trivial. Let’s decode a literal packet and introduce the <code>Packet</code>
type:</p>
<pre><code class="language-rust">#[derive(Clone, Debug, Eq, PartialEq)]
struct Header {
  version: u8,
  type_id: u8,
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Packet {
  Literal {
    header: Header,
    value: u128,
  },

  Operator {
    header: Header,
    length_type_id: u8,
    packets: Vec&lt;Packet&gt;,
  },
}
</code></pre>
<p>And <code>Decoder::decode</code>:</p>
<pre><code class="language-rust">impl Decoder {
  // …

  fn decode(&amp;mut self) -&gt; Packet {
    let version = self.next_bits(3) as u8;
    let type_id = self.next_bits(3) as u8;
    let header = Header { version, type_id };

    match type_id {
      // literal value
      4 =&gt; {
        let mut value = 0u128;
        loop {
          let bits = self.next_bits(5) as u128;
          value = (value &lt;&lt; 4) | (bits &amp; 0xF);

          if (bits &gt;&gt; 4) &amp; 0x1 == 0 {
            break Packet::Literal { header, value };
          }
        }
      }

      // operator
      _ =&gt; {
        todo!()
      }
    }
  }
}
</code></pre>
<p>Some explanation. Given the format, we know that a literal number is made of packets of 5 bits, where the most
significant bit does not belong to the number, but instead is a flag. If set to <code>0</code>, it means that the literal is over.</p>
<pre><code class="language-rust">          value = (value &lt;&lt; 4) | (bits &amp; 0xF);
</code></pre>
<p>This accumulates the 4 least significant bits of the 5 bits into the actual literal number.</p>
<pre><code class="language-rust">          if (bits &gt;&gt; 4) &amp; 0x1 == 0 {
</code></pre>
<p>And this performs the test on the most significant bit to check whether the literal number is fully built.</p>
<h2>Parsing operators</h2>
<p>Operators represent the other kind of packets, and are introduced, quoting:</p>
<blockquote>
<p>Every other type of packet (any packet with a type ID other than 4) represent an operator that performs some
calculation on one or more sub-packets contained within. Right now, the specific operations aren’t important;
focus on parsing the hierarchy of sub-packets.</p>
<p>An operator packet contains one or more packets. To indicate which subsequent binary data represents its
sub-packets, an operator packet can use one of two modes indicated by the bit immediately after the packet
header; this is called the length type ID:</p>
<ul>
<li>If the length <code>type ID</code> is <code>0</code>, then the next <code>15</code> bits are a number that represents the total length in bits of the
sub-packets contained by this packet.</li>
<li>If the length <code>type ID</code> is <code>1</code>, then the next <code>11</code> bits are a number that represents the number of sub-packets
immediately contained by this packet.</li>
</ul>
<p>Finally, after the <code>length type ID</code> bit and the 15-bit or 11-bit field, the sub-packets appear.</p>
</blockquote>
<p>There is nothing else to do than just applying those rules:</p>
<pre><code class="language-rust">      // in Decoder::decode’s match on type ID

      // operator
      _ =&gt; {
        let length_type_id = self.next_bits(1) as u8;
        let mut packets = Vec::new();

        if length_type_id == 0 {
          let total_length_bits = self.next_bits(15) as usize;
          let mut bits_read = 0;

          while bits_read &lt; total_length_bits {
            let bitpos = self.bitpos;
            packets.push(self.decode());
            bits_read += self.bitpos - bitpos;
          }
        } else {
          let sub_packets_nb = self.next_bits(11);

          for _ in 0..sub_packets_nb {
            packets.push(self.decode());
          }
        }

        Packet::Operator {
          header,
          length_type_id,
          packets,
        }
      }
</code></pre>
<p>Because of the <code>next_bits</code> interface, for when <code>length_type_id == 0</code>, we have no way to know deterministically when to
stop, because we don’t really know how long each packet is. Because of this, the <code>bits_read</code> variable is introduced so
that we know how many bits we read. That inforation is easy to get by comparing the value of <code>bitpos</code> before and after
reading a packet. For the other branch, there is nothing interesting to comment here.</p>
<h2>Part 1: summing packet headers’ versions</h2>
<p>The first question was:</p>
<blockquote>
<p>Decode the structure of your hexadecimal-encoded BITS transmission; what do you get if you add up the version numbers
in all packets?</p>
</blockquote>
<p>And my input was (it was a single string but I break it on several lines, because it’s pretty long):</p>
<pre><code>60552F100693298A9EF0039D24B129BA56D67282E600A4B5857002439CE580E5E5AEF67803600D2E294B2FCE8AC489BAEF37FEACB31A678548034EA0
086253B183F4F6BDDE864B13CBCFBC4C10066508E3F4B4B9965300470026E92DC2960691F7F3AB32CBE834C01A9B7A933E9D241003A520DF31664700
2E57C1331DFCE16A249802DA009CAD2117993CD2A253B33C8BA00277180390F60E45D30062354598AA4008641A8710FCC01492FB75004850EE5210AC
EF68DE2A327B12500327D848028ED0046661A209986896041802DA0098002131621842300043E3C4168B12BCB6835C00B6033F480C493003C4008002
9F1400B70039808AC30024C009500208064C601674804E870025003AA400BED8024900066272D7A7F56A8FB0044B272B7C0E6F2392E3460094FAA500
2512957B98717004A4779DAECC7E9188AB008B93B7B86CB5E47B2B48D7CAD3328FB76B40465243C8018F49CA561C979C182723D76964220041275627
1FC80460A00CC0401D8211A2270803D10A1645B947B3004A4BA55801494BC330A5BB6E28CCE60BE6012CB2A4A854A13CD34880572523898C7EDE1A9F
A7EED53F1F38CD418080461B00440010A845152360803F0FA38C7798413005E4FB102D004E6492649CC017F004A448A44826AB9BFAB5E0AA8053306B
0CE4D324BB2149ADDA2904028600021909E0AC7F0004221FC36826200FC3C8EB10940109DED1960CCE9A1008C731CB4FD0B8BD004872BC8C3A432BC8
C3A4240231CF1C78028200F41485F100001098EB1F234900505224328612AF33A97367EA00CC4585F315073004E4C2B003530004363847889E200C45
985F140C010A005565FD3F06C249F9E3BC8280804B234CA3C962E1F1C64ADED77D10C3002669A0C0109FB47D9EC58BC01391873141197DCBCEA401E2
CE80D0052331E95F373798F4AF9B998802D3B64C9AB6617080
</code></pre>
<p>So:</p>
<ol>
<li>Parse the input and decode it into a <code>Packet</code>.</li>
<li>Recurse on the <code>version</code> field of all sub-packets, summing them.</li>
</ol>
<pre><code class="language-rust">fn solve1(input: &amp;str) -&gt; u32 {
  let mut decoder = Decoder::new(input);
  checksum(&amp;decoder.decode())
}

fn checksum(packet: &amp;Packet) -&gt; u32 {
  match packet {
    Packet::Literal { header, .. } =&gt; header.version as _,
    Packet::Operator {
      header, packets, ..
    } =&gt; header.version as u32 + packets.iter().map(checksum).sum::&lt;u32&gt;(),
  }
}
</code></pre>
<p>Yes, it’s that simple once you have made the right framework. :) Note that this function is not tail-recursive. A
tail-recursive version would probably perform faster, but it was so convenient to write and took me 15s.</p>
<h2>Part 2: evaluating the expression</h2>
<p>Part 2 explains that the operator packets are actually expression operators, operating on literal values. Basically,
it’s a whole <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a>. The rules are:</p>
<ul>
<li>Packets with <code>type ID</code> <code>0</code> are sum packets - their value is the sum of the values of their sub-packets. If they only
have a single sub-packet, their value is the value of the sub-packet.</li>
<li>Packets with <code>type ID</code> <code>1</code> are product packets - their value is the result of multiplying together the values of their
sub-packets. If they only have a single sub-packet, their value is the value of the sub-packet.</li>
<li>Packets with <code>type ID</code> <code>2</code> are minimum packets - their value is the minimum of the values of their sub-packets.</li>
<li>Packets with <code>type ID</code> <code>3</code> are maximum packets - their value is the maximum of the values of their sub-packets.</li>
<li>Packets with <code>type ID</code> <code>5</code> are greater than packets - their value is 1 if the value of the first sub-packet is greater
than the value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two
sub-packets.</li>
<li>Packets with <code>type ID</code> <code>6</code> are less than packets - their value is 1 if the value of the first sub-packet is less than
the value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two sub-packets.</li>
<li>Packets with <code>type ID</code> <code>7</code> are equal to packets - their value is 1 if the value of the first sub-packet is equal to the
value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two sub-packets.</li>
</ul>
<p>This is my solution:</p>
<pre><code class="language-rust">fn solve2(input: &amp;str) -&gt; u64 {
  let mut decoder = Decoder::new(input);
  eval(&amp;decoder.decode())
}

fn eval(packet: &amp;Packet) -&gt; u64 {
  match packet {
    Packet::Literal { value, .. } =&gt; *value as _,
    Packet::Operator {
      header, packets, ..
    } =&gt; match header.type_id {
      0 =&gt; packets.iter().map(eval).sum::&lt;u64&gt;(),
      1 =&gt; packets.iter().map(eval).product::&lt;u64&gt;(),
      2 =&gt; packets.iter().map(eval).min().unwrap(),
      3 =&gt; packets.iter().map(eval).max().unwrap(),
      5 =&gt; {
        if eval(&amp;packets[0]) &gt; eval(&amp;packets[1]) {
          1
        } else {
          0
        }
      }
      6 =&gt; {
        if eval(&amp;packets[0]) &lt; eval(&amp;packets[1]) {
          1
        } else {
          0
        }
      }
      7 =&gt; {
        if eval(&amp;packets[0]) == eval(&amp;packets[1]) {
          1
        } else {
          0
        }
      }

      id =&gt; panic!("unknown type id: {}", id),
    },
  }
}
</code></pre>
<p>And here you have it all. The
<a href="https://github.com/phaazon/advent-of-code-2021/blob/master/day16/src/main.rs">complete solution is available on my GitHub repository for AoC 21</a>.
I don’t think it was the hardest problem, nor the more interesting, but the decision (and challenge) I decided to go
with (bit-packing with a bit-driven interface) made the whole thing much more fun and interesting to me.</p>
<p>I hoped you liked it and that you learned something, especially regarding bitwise operations. That was pretty heavy! I
think that you should also now have an idea about how you could write a specialized version of <code>Vec&lt;bool&gt;</code> that
bit-packs booleans value. Obviously, we do not want <code>Vec&lt;bool&gt;</code> to actually do that kind of specialization behind the
scenes (think FFI: when getting a <code>*const bool</code> or <code>*mut bool</code> from the <code>Vec&lt;bool&gt;</code>, if those pointers point to
bit-packed data… it’s going to be a nightmare if the FFI function you use expect an actual array of unpacked boolean
values).</p>
<p>For the rest of AoC, I’m probably going to continue working on it but I’m currently pausing at day 18 (Christmas,
fatigue, a bit of OSS burnout too). I will probably write some more about AoC once I feel rested.</p>
<p>Have fun and keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 24 Dec 2021 14:54:00 GMT</pubDate></item><item><title>luminance first tutorial</title><link>https://strongly-typed-thoughts.net/blog/luminance_first_tutorial</link><description><![CDATA[<h1>Woah!</h1>
<p>I’m very happy about people getting interested about my <a href="http://hackage.haskell.org/package/luminance">luminance</a>
graphics framework. I haven’t received use case feedback yet, but I’m pretty confident I will sooner
or later.</p>
<p>In the waiting, I decided to write an <em>embedded tutorial</em>. It can be found
<a href="http://hackage.haskell.org/package/luminance-0.1.1/docs/Graphics-Luminance.html">here</a>.</p>
<p>That tutorial explains all the basic types of luminance – not all though, you’ll have to dig in the
documentation ;) – and describes how you should use it. I will try to add more documentation for
each modules in order to end up with a very well documented piece of software!</p>
<h1>Let’s sum up what you need</h1>
<p>People on <a href="https://www.reddit.com/r/haskell/comments/3lyxzc/luminance_01_released">reddit</a>
complain – they are right to – about the fact the samples just <em>“didn’t work</em>. They actually did,
but the errors were muted. I released
<a href="http://hackage.haskell.org/package/luminance-samples-0.1.1">luminance-0.1.1</a> to fix that issue. Now
you’ll get the proper error messages.</p>
<p>The most common issue is when you try to run a sample without having the required hardware
implementation. luminance requires <strong>OpenGL 4.5</strong>. On Linux, you might need to use <code>primusrun</code> or
<code>optirun</code> if you have the <strong>Optimus</strong> technology. On Windows, I guess you have to allow the samples
to run on the dedicated <em>GPU</em>. And on Mac OSX… I have no idea; <code>primusrun</code> / <code>optirun</code>, I’d go.</p>
<p>Anyways, I’d like to thank all people who have/will tried/try the package. As always, I’ll keep you
informed about all the big steps I take about luminance. Keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 24 Sep 2015 00:00:00 GMT</pubDate></item><item><title>Main differences between luminance and other famous Rust crates</title><link>https://strongly-typed-thoughts.net/blog/luminance_comparing</link><description><![CDATA[<p>It’s been a while since I’ve been wanting to do this. Either on IRC, Reddit, GitHub Issues, IRL or
other any other way of communicating, there are more and more people asking me to <em>compare
<a href="https://crates.io/crates/luminance">luminance</a> to other, well known and famous, rendering-like frameworks</em>.</p>
<p>I’ve made a listing with pros and cons. in the <a href="https://github.com/phaazon/luminance-rs#how-does-it-compare-to">README</a> file of <a href="https://crates.io/crates/luminance">luminance</a>. I’ll just copy that
section and report it below so that you’re not even a click away from the listing!</p>
<h1>How does it compare to…</h1>
<p>A lot of folks have asked me on IRC / Reddit / gitter / etc. how <a href="https://crates.io/crates/luminance">luminance</a> compares to other
famous frameworks. Here are a few comparisons:</p>
<h2>luminance vs. glium</h2>
<p><a href="https://crates.io/crates/glium">glium</a> has been around for a while – way longer than <a href="https://crates.io/crates/luminance">luminance</a>.</p>
<blockquote>
<p>According to crates.io, <a href="https://crates.io/crates/glium">glium</a> is <a href="https://users.rust-lang.org/t/glium-post-mortem/7063">no longer actively developed by its original author</a>.</p>
</blockquote>
<ul>
<li>It has more macros to generate <code>impl</code>, which is neat – however, this tends to be less and less
true as <a href="https://crates.io/crates/luminance">luminance</a> has been getting new macros lately.</li>
<li>It addresses the OpenGL concepts more directly. For instance, you find a <code>glium::VertexBuffer</code>
type – a buffer to hold vertices – while <a href="https://crates.io/crates/luminance">luminance</a> gives you a <code>Tess</code> type that hides that kind
of complexity away for you.</li>
<li><a href="https://crates.io/crates/glium">glium</a> doesn’t prevent you to pick a specific version of OpenGL / GLSL if you want to. For
instance, the <a href="https://github.com/glium/glium/blob/master/book/tuto-02-triangle.md#program">tuto-02-triangle.md</a> sample uses GLSL140. <a href="https://crates.io/crates/luminance">luminance</a> is different on that
side as it <strong>must</strong> be used with an <strong>OpenGL 3.3 Core Context</strong> – hence a GLSL330. This can be
seen as a <a href="https://crates.io/crates/luminance">luminance</a> drawback as it will only work with a specific version of OpenGL (GL 3.3 and
GLSL 330).</li>
<li>Drawing is done by requesting a <code>Frame</code> object in <a href="https://crates.io/crates/glium">glium</a> and passing the whole pipeline at once.
The pipeline then must be completely resolved prior to render. See
<a href="https://docs.rs/glium/0.20.0/glium/trait.Surface.html#tymethod.draw">this</a>. <a href="https://crates.io/crates/luminance">luminance</a> is more
flexible on that side since it enables you to provide the rendering pipeline in a dynamic way,
allowing for tricky structure traversal without cloning nor borrowing, for instance. <a href="https://crates.io/crates/glium">glium</a>
requires a tuple of <code>(vertices, indices, program, uniforms, render_state)</code> while <a href="https://crates.io/crates/luminance">luminance</a> works
by building the AST node by node, level by level.</li>
<li>The program customization (uniforms) feature of <a href="https://crates.io/crates/glium">glium</a> is per-value while <a href="https://crates.io/crates/luminance">luminance</a> is
per-type. In <a href="https://crates.io/crates/glium">glium</a>, you can change the uniforms you pass to a program by using the <code>uniform!</code>
macro when invoking the <code>draw</code> command on your <code>Frame</code>. This has the effect to lookup the uniform
in the shader for every call – modulo caching – and requires you to explicitly pass the uniforms
to use. <a href="https://crates.io/crates/luminance">luminance</a> uses a <em>contravariant</em> approach here: you give the type of the <em>uniform
interface</em> you want to use and you will be, later, handed back a reference to an object of that
type so that you can alter it. All of this is done at the type level; you have nothing to do when
passing values to the shader in the pipeline. Opposite approaches, then. Also, notice that
<a href="https://crates.io/crates/luminance">luminance</a> looks up the uniform names exactly once – at <code>Program</code> creation – and provides several
hints about uniform utilization (inactive, type mismatch, etc.). <a href="https://crates.io/crates/glium">glium</a> seems to have runtime
type checking as well, though.</li>
<li>Linear algebra seems to be handled the same way (e.g. 4×4 matrices are encoded as <code>[[f32; 4] 4]</code>
in both crates).</li>
<li>Textures are handled a bit similarily, even though, because <a href="https://crates.io/crates/glium">glium</a> uses a fixed pipeline, the
user doesn’t have to do anything special but passing references around. <a href="https://crates.io/crates/luminance">luminance</a> exposes a
scoped API to use opaque texture values, which adds more noise. A good point for <a href="https://crates.io/crates/glium">glium</a> there
since the texture passing is just about references while <a href="https://crates.io/crates/luminance">luminance</a> has a specific API for that.
This is the same situation for GPU buffers.</li>
<li>About vertices, <a href="https://crates.io/crates/glium">glium</a> is more permissive about how data can be stored on the GPU. For instance,
it supports <em>deinterleaved vertices</em> – e.g. when you have your positions in
a GPU buffer and the normals or colors in another. <a href="https://crates.io/crates/luminance">luminance</a> requires you to interleave your
vertices. This is due to the fact that if you use all your vertex attributes in a shader – which
was an assumed situation when designing the vertex API – this will provide better performances
than using deinterleaved data (because of cache friendliness). However, for cases when you don’t
want to use other attributes – depth peeling, shadow mapping, z-pre-pass, etc. – that might be a
huge drawback as you’ll peek / load data from the buffer you won’t even use, killing the point of
having a cache-friendly peek.</li>
<li>Draw state (i.e. depth test, depth buffer comparison equations, culling, etc.) is done pretty much
the same way in both crates: by creating an object representing the state when calling the
rendering pipeline.</li>
<li><a href="https://crates.io/crates/glium">glium</a> supports <em>compute shaders</em> while <a href="https://crates.io/crates/luminance">luminance</a> doesn’t (so far! ;) ).</li>
<li>Additionnally, because of the dynamic nature of its pipelines, <a href="https://crates.io/crates/luminance">luminance</a> allows for better
sharing of resources, especially shaders and render states. This is due to the fact that
<a href="https://crates.io/crates/luminance">luminance</a> represents rendering pipelines as dynamic ASTs in which you can change all the
branches and leaves on the fly. See the point about the <code>Frame</code> of <a href="https://crates.io/crates/glium">glium</a> above.</li>
</ul>
<blockquote>
<p>Sources and tutorials for <a href="https://crates.io/crates/glium">glium</a> <a href="https://github.com/glium/glium/tree/master/book">here</a>.</p>
</blockquote>
<h2>luminance vs. gfx</h2>
<p><a href="https://crates.io/crates/gfx">gfx</a> is a high-performance, bindless graphics API for the Rust programming language. It aims to be
the default API for Rust graphics: for one-off applications, or higher level libraries or engines.</p>
<blockquote>
<p>The current listing concerns <a href="https://crates.io/crates/gfx">gfx</a> in its’ <em>pre- low level</em> situation. Recent <a href="https://crates.io/crates/gfx">gfx</a> APIs are
different.</p>
</blockquote>
<ul>
<li>The <a href="https://crates.io/crates/gfx">gfx</a>’s scope is not the same as <a href="https://crates.io/crates/luminance">luminance</a>’s: <a href="https://crates.io/crates/gfx">gfx</a> abstracts over <strong>several graphics API</strong>.
Among them, <strong>OpenGL</strong>, <strong>Metal</strong>, <strong>DirectX</strong> and will clearly attract people who want to <em>write
once, release everywhere</em>.</li>
<li><a href="https://crates.io/crates/gfx">gfx</a> requires you to write shader sources for all possible backends. See for instance this
<a href="https://github.com/gfx-rs/gfx/tree/pre-ll/examples/cube/shader">example</a> which uses
GLSL, HLSL, Metal and even SPIR-V – it even uses several version of GLSL, running in GLSL 100, 120
and 150.</li>
<li>Vertex types are defined differently from <a href="https://crates.io/crates/luminance">luminance</a>. In <a href="https://crates.io/crates/gfx">gfx</a>, you use the <code>gfx_defines!</code> macro
to easily introduce the types and make named bindings (to map to vertex attributes in shaders). A
lot of boilerplate is generated thanks to that macro, which is neat. <a href="https://crates.io/crates/luminance">luminance</a> has a similar
scheme even though the macro support in <a href="https://crates.io/crates/luminance">luminance</a> is less advanced. On a general note:
<a href="https://crates.io/crates/luminance">luminance</a> is less macro-driven.</li>
<li>Similarily, the uniforms are handled in the same <code>gfx_defines!</code> macro. They’re declared in very
similar ways as in <a href="https://crates.io/crates/luminance">luminance</a>. However, <a href="https://crates.io/crates/gfx">gfx</a> uses the same syntax as with vertex definition,
while <a href="https://crates.io/crates/luminance">luminance</a> has an ad hoc approach – it’s typical Rust code with <em>annotations</em>. That doesn’t
make a huge difference per-se. <a href="https://crates.io/crates/gfx">gfx</a> calls <em>uniforms</em> <code>constant</code>, which is nice because it can
abstract over the concept, while <a href="https://crates.io/crates/luminance">luminance</a> doesn’t and uses the terms <code>Uniform</code>, <code>Uniformable</code>,
<code>UniformInterface</code>, etc.</li>
<li>The pipeline system in <a href="https://crates.io/crates/gfx">gfx</a> is also defined in <code>gfx_defines!</code> macro invokations. This is
seriously cool and <a href="https://crates.io/crates/luminance">luminance</a> suffers a bit from not having that – defining pipeline in
<a href="https://crates.io/crates/luminance">luminance</a> is a bit boring because of lambda / closures boilerplate.</li>
<li>In <a href="https://crates.io/crates/gfx">gfx</a>, the GPU buffers are handled via <em>factories</em> and smart constructors, like
<code>create_vertex_buffer_with_slice</code>, for instance. This is nice as it enables coping with
multi-threading safety, which is something <a href="https://crates.io/crates/luminance">luminance</a> doesn’t support yet (it’s planned, though).</li>
<li><a href="https://crates.io/crates/gfx">gfx</a> comes in with an <em>application</em> abstraction layer, which <a href="https://crates.io/crates/luminance">luminance</a> doesn’t –
<code>gfx_app::Application</code>, defined in the <a href="https://crates.io/crates/gfx_app">gfx_app</a> crate.</li>
<li><a href="https://crates.io/crates/gfx">gfx</a> has <strong>a lot</strong> of running projects and is actively maintained and changed over time. For
instance, both <a href="https://crates.io/crates/ggez">ggez</a> and <a href="https://crates.io/crates/amethyst">amethyst</a> use <a href="https://crates.io/crates/gfx">gfx</a>.</li>
</ul>
<blockquote>
<p>On a general note, <a href="https://crates.io/crates/luminance">luminance</a> and <a href="https://crates.io/crates/gfx">gfx</a> pre-ll are way too different to be compared to each
other. If you want to target several backends, do not hesitate and go to <a href="https://crates.io/crates/gfx">gfx</a>. If you target only
one system and want simplicity, go to <a href="https://crates.io/crates/luminance">luminance</a>.</p>
</blockquote>
<h2>luminance vs. ggez</h2>
<p><a href="https://crates.io/crates/ggez">ggez</a> is a Rust library to create a Good Game Easily.</p>
<blockquote>
<p>As <a href="https://crates.io/crates/ggez">ggez</a> is primarily focused on 2D projects, it doesn’t compare directly to <a href="https://crates.io/crates/luminance">luminance</a>, as it’s
about graphics, sound, input, etc. Also, <a href="https://crates.io/crates/ggez">ggez</a> is using <a href="https://crates.io/crates/gfx">gfx</a>.</p>
</blockquote>
<h2>luminance vs. vulkano</h2>
<p><a href="https://crates.io/crates/vulkano">vulkano</a> is a wrapper around the Vulkan graphics API.</p>
<blockquote>
<p>The scope is not really the same as <a href="https://crates.io/crates/vulkano">vulkano</a> only targets Vulkan and – as for today – <a href="https://crates.io/crates/luminance">luminance</a>
is about OpenGL.</p>
</blockquote>
<h2>luminance vs. amethyst</h2>
<p><a href="https://crates.io/crates/amethyst">amethyst</a> is a fast, data-oriented, and data-driven game engine suitable for rapid prototyping and
iteration.</p>
<blockquote>
<p>As stated in the current document, <a href="https://crates.io/crates/luminance">luminance</a> is not a game engine. So if you’re looking for a
tool that already has everything needed to build a game, <a href="https://crates.io/crates/amethyst">amethyst</a> should be your toy. Also, it’s
using <a href="https://crates.io/crates/gfx">gfx</a>.</p>
</blockquote>
<h2>luminance vs. piston</h2>
<p><a href="https://crates.io/crates/piston">piston</a> is a modular game engine written in Rust.</p>
<blockquote>
<p>As for other game engines, nothing to compare here since <a href="https://crates.io/crates/luminance">luminance</a> is <strong>not a game engine</strong>!</p>
</blockquote>
<h2>luminance vs. spectra</h2>
<p><a href="https://crates.io/crates/spectra">spectra</a> is a demoscene framework / engine mainly written for demoscene purposes, even though it
can now be used to some other extents (animation / video game).</p>
<blockquote>
<p><a href="https://crates.io/crates/spectra">spectra</a> uses <a href="https://crates.io/crates/luminance">luminance</a> as primary <em>graphics backend</em>, but it provides way more features than
just graphics. You’ll find audio, animation, editing, resource system, custom shading language,
etc.</p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 28 Mar 2018 18:40:00 GMT</pubDate></item><item><title>Hindsight on Advent of Code 2018</title><link>https://strongly-typed-thoughts.net/blog/aoc-18-hindsight</link><description><![CDATA[<p>On the 1st of December 2018, I decided to give it a try to <a href="https://adventofcode.com/about">Advent of Code</a>. AoC is, basically, a
programming challenge website where you get two puzzles unlocked every day of December from 1st to
25th – hence the name. It has a ranking system which scores are based on the absolute time you took
to solve the puzzles – i.e. spent time as soon as the puzzles got unlocked. As a French living in
Paris, I find this a bit unfair (we get puzzles unlocked at around 5:00 AM!) and then I just
realized I could do the puzzles for fun only.</p>
<p>This blog post sums up what I did with AoC#18, my thoughts about the puzzles and even a
meta-discussion about programming challenges. I used Haskell for most challenges and switched to
Rust for no specific reason. Just fun.</p>
<p>Also, because the puzzles’ texts are somewhat long, I will just give a link to the text instead of
copying it and will just make a very short description of the problem each time.</p>
<p>Finally, don’t expect to find all the puzzles: I stopped at day 15. I will explain the reasons why
at the end of this article. My adventure can be found
<a href="https://github.com/phaazon/advent-of-code-2k18">here</a>. Most puzzles have <em>input data</em>. You will
find them in day directories as <code>input.txt</code> files.</p>
<p>Enjoy the reading!</p>
<blockquote>
<p><strong>If you want to try and take the challenges, I advise you not to read any further as this article
would spoil you solutions! You will need a Github account and… a lot of time.</strong></p>
</blockquote>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#day-1-chronal-calibration">Day 1: Chronal Calibration</a>
<ul>
<li><a href="#part-1">Part 1</a></li>
<li><a href="#part-2">Part 2</a></li>
</ul>
</li>
<li><a href="#day-2-inventory-management-system">Day 2: Inventory Management System</a>
<ul>
<li><a href="#part-1-1">Part 1</a></li>
<li><a href="#part-2-1">Part 2</a></li>
</ul>
</li>
<li><a href="#day-3-no-matter-how-you-slice-it">Day 3: No Matter How You Slice It</a>
<ul>
<li><a href="#part-1-2">Part 1</a></li>
<li><a href="#part-2-2">Part 2</a></li>
</ul>
</li>
<li><a href="#day-4-repose-record">Day 4: Repose Record</a>
<ul>
<li><a href="#part-1-3">Part 1</a></li>
<li><a href="#part-2-3">Part 2</a></li>
</ul>
</li>
<li><a href="#day-5-alchemical-reduction">Day 5: Alchemical Reduction</a>
<ul>
<li><a href="#part-1-4">Part 1</a></li>
<li><a href="#part-2-4">Part 2</a></li>
</ul>
</li>
<li><a href="#day-6-chronal-coordinates">Day 6: Chronal Coordinates</a>
<ul>
<li><a href="#part-1-5">Part 1</a></li>
<li><a href="#part-2-5">Part 2</a></li>
</ul>
</li>
<li><a href="#day-7-the-sum-of-its-parts">Day 7: The Sum of Its Parts</a>
<ul>
<li><a href="#part-1-6">Part 1</a></li>
<li><a href="#part-2-6">Part 2</a></li>
</ul>
</li>
<li><a href="#day-8-memory-maneuver">Day 8: Memory Maneuver</a>
<ul>
<li><a href="#part-1-7">Part 1</a></li>
<li><a href="#part-2-7">Part 2</a></li>
</ul>
</li>
<li><a href="#day-9-marble-mania">Day 9: Marble Mania</a>
<ul>
<li><a href="#part-1--2">Part 1 &amp; 2</a></li>
</ul>
</li>
<li><a href="#day-10-the-stars-align">Day 10: The Stars Align</a>
<ul>
<li><a href="#part-1-8">Part 1</a></li>
<li><a href="#part-2-8">Part 2</a></li>
</ul>
</li>
<li><a href="#day-11-chronal-charge">Day 11: Chronal Charge</a>
<ul>
<li><a href="#part-1-9">Part 1</a></li>
<li><a href="#part-2-9">Part 2</a></li>
</ul>
</li>
<li><a href="#day-12-subterranean-sustainability">Day 12: Subterranean Sustainability</a>
<ul>
<li><a href="#part-1-10">Part 1</a></li>
<li><a href="#part-2-10">Part 2</a></li>
</ul>
</li>
<li><a href="#day-13-mine-cart-madness">Day 13: Mine Cart Madness</a>
<ul>
<li><a href="#part-1-11">Part 1</a></li>
<li><a href="#part-2-11">Part 2</a></li>
</ul>
</li>
<li><a href="#day-14-chocolate-charts">Day 14: Chocolate Charts</a>
<ul>
<li><a href="#part-1-12">Part 1</a></li>
<li><a href="#part-2-12">Part 2</a></li>
</ul>
</li>
<li><a href="#day-15-beverage-bandits">Day 15: Beverage Bandits</a>
<ul>
<li><a href="#part-1-13">Part 1</a></li>
</ul>
</li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>Day 1: Chronal Calibration</h1>
<p><a href="https://adventofcode.com/2018/day/1">Text</a></p>
<h2>Part 1</h2>
<blockquote>
<p>This puzzle is about summing arrays and finding duplicates (without counting).</p>
</blockquote>
<p>First puzzle I discovered and first AoC challenge I ever took, I was surprised at the simplicity.
Basically, given lines containing a single number preceded by either <code>+</code> or <code>-</code>, we must compute
the sum of all the numbers.</p>
<p>The first thing to do is to parse the file into a stream of numbers. In Haskell, this was almost
a one liner:</p>
<pre><code>main :: IO ()
main = do
    numbers &lt;- fmap (map parse . lines) getContents
  where
    parse ('+':xs) = read xs -- (1)
    parse n = read n
</code></pre>
<p>If I removed the <code>+</code> from the file, I could even have gone with a one liner:</p>
<pre><code>    numbers &lt;- fmap (map read . lines) getContents
</code></pre>
<blockquote>
<p>The <code>read</code> function is not safe and I don’t recommend using it for real, production projects.
In the context of this challenge, though, it was alright.</p>
</blockquote>
<p>I don’t have much to say about this puzzle as I found it not very interesting.</p>
<h2>Part 2</h2>
<p>We just want to get the first cumulative sum that appears twice. That is, each time a new number
is summed, we get a given “current sum”. We add positive and negative integers, so we might end up,
at some time, with the same number. Because we don’t know when that will happen, we need to turn
the input stream into an infinite stream of numbers that repeat over and over. Haskell has the
<code>cycle</code> function that does exactly that: it takes a list and makes it repeat infinitely.</p>
<p>What is great about <code>cycle</code> in Haskell is that it could be implemented in a very naive way and yet
yield good performance – thanks GHC!:</p>
<pre><code>cycle :: [a] -&gt; [a]
cycle x = let x' = x ++ x' in x'
</code></pre>
<p>Or, prefered (personal opinon):</p>
<pre><code>cycle :: [a] -&gt; [a]
cycle = fix . (++)
</code></pre>
<blockquote>
<p><code>fix</code> is a very powerful function any Haskell should know. Typically, when you want to
corecursively build something, it’s very likely you’ll need <code>fix</code>. I advise you to have a look at
my <a href="https://phaazon.net/blog/getting_into_netwire">netwire tutorial</a> in which I used <code>fix</code> pretty much everywhere.</p>
</blockquote>
<p>There are several ways to fix this problem. I chose to use a <strong>set</strong> to hold all the known
intermediate sums. Because we’re working with integers, the Haskell <code>IntSet</code> type is perfect for
that. The algorithm is then just a special fold over an infinite list that stops when a sum is seen
twice by looking it up in the integer set:</p>
<pre><code>findFreq _ freq [] = freq -- this shouldn’t ever happen since the list is infinite
findFreq knownFreq freq (change:xs)
    | newFreq `member` knownFreq = newFreq
    | otherwise = findFreq (insert newFreq knownFreq) newFreq xs
  where
    newFreq = freq + change
</code></pre>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-01/src/Main.hs">Haskell solution</a></p>
<h1>Day 2: Inventory Management System</h1>
<p><a href="https://adventofcode.com/2018/day/2">Text</a></p>
<h2>Part 1</h2>
<p>This puzzle requires you to recognize and count characters in strings. For each string, you must
check whether they have any letter that appears exactly once and any letter that appears exactly
three times. Adding those counts provides a checksum for a list of strings.</p>
<p>I did that in Haskell as well. My idea was to use a count-array of size 26 (since the strings are
only in <code>[a-z]</code>. So the first part of my algorithm is to count, for a given string, the number of
time each letter occurs. This is done really easily with a fold that increments the proper value
value in the array at the index representing the character folded on in the string. That index
is obtained by turning a character into a numerical representation and subtracting it the numerical
value of <code>'a'</code>:</p>
<pre><code>treatHash :: (Natural, Natural) -&gt; String -&gt; (Natural, Natural)
treatHash occurs hash =
    let result :: State = foldl' replace initialState hash
    in addPair occurs (foldl' incrOccur (0, 0) result)
  where
    replace st l = accum (+) st [(ord l - ord 'a', 1)]
    incrOccur i@(twos, threes) x
      | x == 2 = (1, threes)
      | x == 3 = (twos, 1)
      | otherwise = i
addPair (a, b) (c, d) = (a + c, b + d)
</code></pre>
<p>The <code>replace</code> function performs the update in an input vector (point-free function). This will
effectively increment the vector’s value at index <code>3</code> if the character is <code>d</code> (because
<code>ord 'd' - ord 'a' = 3</code>. <code>incrOccur</code>, here, is used to fold over the resulting array (<code>Vector</code>) and
compute if a letter appears twice and if a letter appears three times. You will notice that this
function doesn’t sum. For instance, if you have <code>aabb</code>, you get no letter that appears three times
but you have two that appear exactly twice. However, <code>incrOccur</code> will give you <code>(1, 0)</code>, because the
puzzle states it should be done this way. Finally, <code>addPair</code> add the number of twos and threes to a
pre-defined input – that will be handy for an outer fold.</p>
<p>The algorithm is then run on the input and we get the final result:</p>
<pre><code>hashes &lt;- fmap lines getContents
let checksum = uncurry (*) $ foldl' treatHash (0, 0) hashes
</code></pre>
<p>We transform the standard input into lines – i.e. <code>[String]</code> – and then fold them with our
<code>treatHash</code> function. Finally, <code>uncurry (*)</code> is a little oneliner to simply multiply the left part
of a pair with its right part.</p>
<h2>Part 2</h2>
<p>This part is about finding strings that are almost the same and differ and only by one letter. As
doing a programming challenge, the first thing I want to find is <em>a solution</em>, not the best
solution. I also think people should really learn from this:</p>
<ol>
<li>Make it work.</li>
<li>Make it clean.</li>
<li>Make it fast.</li>
</ol>
<p>Especially on programming challenges, you’ll be very, very unlikely to implement anything more than
(1.), because the inputs are often two small to really benefit from a real optimization. Your time
is a real important resource, don’t waste it. Measure whether you really need an optimization. I’m
not saying that you should be doing something bad because it’s easier. I’m saying that if it takes
you N minutes to write a solution that runs in M milliseconds, if you know that you could do it in,
for instance, 10N minutes to write a solution that runs in only 0,7M milliseconds for the foreseen
milliseconds, will, you’re going to waste your time.</p>
<p>So, in my case, I started with a naive approach that runs in <em>O(N²)</em>: comparing all strings to all
others:</p>
<pre><code>searchCommon :: [String] -&gt; String
searchCommon [] = "" -- the empty list has nothing in common
searchCommon (h:hs) = search h hs -- for each string, search in all remaining
  where
    search h [] = searchCommon hs -- exhausted,  search with the next string
    search h (x:xs)
      | almostSame h x = commonLetters h x -- grab the common letters if almost the same
      | otherwise = search h xs -- if not, just try the next string in the sub-list
</code></pre>
<p>The algorithm is pretty straight-forward. The <code>searchCommon</code> and <code>search</code> functions are mutually
recursive functions that go from, respectively, the whole list of strings to test and the local
tail as we advance. <code>almostSame</code> is defined as follows:</p>
<pre><code>almostSame :: String -&gt; String -&gt; Bool
almostSame = go 0
  where
    go diffNb (l:ls) (r:rs)
      | diffNb &gt; 1 = False
      | l /= r = go (succ diffNb) ls rs
      | otherwise = go diffNb ls rs
    go diffNb _ _ = diffNb == 1
</code></pre>
<p>This function is a special <code>zip</code> that short-circuits if it knows there are two many differences.
When both the input strings are exhausted, if <code>diffNb == 1</code>, then only one character has changed, so
the the overwhole function evaluates to <code>True</code>.</p>
<p>The final part we need is <code>commonLetters</code>, that is pretty straight-forward, too:</p>
<pre><code>commonLetters :: String -&gt; String -&gt; String
commonLetters (l:ls) (r:rs)
  | l == r = l : commonLetters ls rs
  | otherwise = ls -- all the rest is identic, smartass
</code></pre>
<p>We construct a list that has the same characters as the input lists as long as they’re equal. As
soon as they differ, we discard the character and just return any of the input lists – they’re
equal. This function only works, obviously, if both the input strings are almost identical (only one
character differs and they have the same length). The <code>otherwise</code> branch is a short-circuit
optimization that prevents us from traversing the whole inputs after the difference.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-02/src/Main.hs">Haskell solution</a></p>
<h1>Day 3: No Matter How You Slice It</h1>
<p><a href="https://adventofcode.com/2018/day/3">Text</a></p>
<h2>Part 1</h2>
<p>This problem was the first “challenging” as it required more thinking. The idea is that a very large
area – the text gives us the hint it is at least <code>1000</code> inches on each side but truly, we do not
need this information – must be sliced into several rectangular smaller areas (with edges aligned
with X and Y axis). Obviously, some rectangular area might overlap and the aim of the first part of
this problem is to find out <strong>the total overlapping area</strong>.</p>
<p>My idea was simple: we can trick and change the problem by discretizing it. This is something I
have said already but most of AoC problems have <em>hidden properties</em> and <em>hidden hints</em>. By thinking
more about the form of the solution, the area to find is expressed in <em>inches²</em>. The rectangular
areas are all lying on a 2D grid (1000×1000 at least, remember?) and their coordinates are always
natural numbers (they belong to <code>[0;+∞]</code>). Then, my idea was to state that 1 inch² is actually a
single “cell” at any coordinate in that grid – imagine that as a pixel. 4 inches² would be a 2×2
region in that grad (yielding 4 cells).</p>
<p>So instead of using the general idea of an area (M×N for a rectangle which sides are M and N long
wide), we can simply break a rectangular area into its most atomic components (1×1 cells)… and sum
them up! We will effectively end up with the area of this area.</p>
<p>A more interesting property of my way of solving it: we can now have a big array mapping to each
1×1 cell the number of times a rectangle lies on it. When iterating through all the rectangles, we
just break them into a list of 1×1 cells, look ingg up and updating the big count array. Once we’re
done, we just have to filter that big array to remove any element which count is less or equal to 1.
The remaining elements are 1×1 cells that contain at least two overlapping rectangles – we don’t
really care about the number. We don’t actually care about those elements: the length of that
filtered array is the area we are looking for, because it is the sum of 1×1 elements!</p>
<p>Converting a rectangular area – called claim in the text – into 1×1 cells is very easy in Haskell:</p>
<pre><code>type Plan = Map (Int, Int) Natural -- the big array I told you
type Claim = (Id, Pos, Area)
type Id = Text -- the “name” of the fabric the rectangular area is about
type Pos = (Int, Int)
type Area = (Int, Int)

claim1x1 :: Claim -&gt; [Pos]
claim1x1 (_, (line, row), (width, height)) =
  [(x, y) | x &lt;- [line .. line + width - 1], y &lt;- [row .. row + height - 1]]
</code></pre>
<p>As you can see, I chose to use a <code>Map (Int, Int) Natural</code> instead of an array to store the number
of overlaps per 1×1 cell. That enables us not to care at all about the size of the grid (remember
when I told you we don’t need the <code>1000</code> hint?).</p>
<p>The function that updates the <code>Plan</code> to take a claim into account is:</p>
<pre><code>checkClaims :: [Claim] -&gt; Plan
checkClaims = foldl' (\p (x, y) -&gt; checkInch x y p) mempty . concatMap claim1x1

checkInch :: Int
          -&gt; Int
          -&gt; Plan
          -&gt; Plan
checkInch line row = insertWith (+) (line, row) 1
</code></pre>
<p>Given a list of claims (<code>[Claim]</code>), <code>checkClaims</code> maps the <code>claim1x1</code> function and concats the
result, yielding a <code>[Pos]</code> list. That list is then folded over with the <code>checkInch x y p</code> function,
that takes an empty map as initial value. <code>checkInch</code> just increment the value found in the map if
it already exists; otherwise, it sets that value to <code>1</code>.</p>
<p>Finally, we need to compute the area:</p>
<pre><code>overlappingInches :: Plan -&gt; Int
overlappingInches = length . M.filter (&gt; 1)
</code></pre>
<p>As I told you, that is crystal clear: it’s just the length of the filtered map.</p>
<h2>Part 2</h2>
<p>This part is interesting also: you need to find out the <code>Id</code> of the claim that doesn’t overlap with
any other claim. I will not go into too much details about the algorithm as it’s very similar to the
previous one: instead of storing the number of overlaps by 1×1 cell, we store a <code>Set Id</code>, giving all
claims that are overlapping – we can see that as a more general form of the first part. We also need
a <code>Map Id Natural</code> that maps a fabric and the number of times it overlaps another. The fabric that
doesn’t overlap any other is then easily identifiable within that map: it has its associated value
set to <code>0</code>:</p>
<pre><code>searchNonOverlapped :: [Claim] -&gt; Maybe Id
searchNonOverlapped claims =
    case M.toList filtered of
      [(i, _)] -&gt; Just i -- the text supposes there’s only one
      _ -&gt; Nothing
  where
    (_, overlapped) = indexClaims claims
    filtered = M.filter (== 0) overlapped -- hello
</code></pre>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-03/src/Main.hs">Haskell solution</a></p>
<h1>Day 4: Repose Record</h1>
<p><a href="https://adventofcode.com/2018/day/4">Text</a></p>
<h2>Part 1</h2>
<p>Aaaah, day 4… I really don’t get why I got so annoyed by this one. It is quite simple in theory. But
that made me realize something I don’t really like about AoC: the <em>hidden rules</em>. You will see, if
you read this whole article, that some puzzles require you to make very important assumptions about
the input – and there’re a lot of assumptions you could make, so you have to make the right ones!</p>
<p>This puzzle is about guards that must protect a prototype manufacturing lab. You are given an
unordered list of events that occur about the guards:</p>
<ul>
<li>When they begin their shifts (you are given the ID of the guard here).</li>
<li>When they fall asleep.</li>
<li>When they wake up.</li>
<li>Each action gives you a timestamp so that you can re-create the historical events.</li>
</ul>
<p>The goal of the part 1 is to find the guard that has the most minutes asleep, and especially, find
the minute it is asleep the most – we must multiply the ID of the guard by the minute.</p>
<p>Obviously, the first part of this puzzle is to re-order the input to have it chronologically. Here
is the setup code:</p>
<pre><code>data Entry = Entry {
    timestamp :: LocalTime,
    action :: Text
  } deriving (Show)

type GuardianId = Natural

data Action
  = BeginShift GuardianId
  | WakeUp
  | FallAsleep
    deriving (Eq, Ord, Show)

entryFromString :: Text -&gt; Maybe Entry
entryFromString s = case split (== ']') s of
    [timestamp, action] -&gt; Just $ Entry (parse . unpack $ T.drop 1 timestamp) action
    _ -&gt; Nothing
  where
    parse = parseTimeOrError False defaultTimeLocale "%Y-%-m-%-d %H:%M"
</code></pre>
<p>The parsing part is not really interesting as it’s just challenge code: nasty but working parsing
code. :D</p>
<p>I then re-ordered the input with:</p>
<pre><code>entries &lt;- fmap (fromJust . traverse entryFromString . T.lines) T.getContents
let byTimeSorted = sortBy (comparing timestamp) entries
</code></pre>
<p>By the way, that code made me want to <a href="https://twitter.com/phaazon_/status/1070054255887822848">tweet about how Haskell is actually pretty easy to read and
reason about</a>. Anyway.</p>
<p>The next part of the algorithm is to transform the entries into a list of timed action. I actually
decided to stream it so that I could benefit from Haskell’s stream fusion – and because it’s so
simple and transparent:</p>
<pre><code>readGuardianId :: Text -&gt; Natural
readGuardianId = readT . T.drop 1

treatEntries :: [Entry] -&gt; [(LocalTime, Action)]
treatEntries = map $ \entry -&gt;
  let time = timestamp entry
  in case T.words (action entry) of
    ["Guard", ident, "begins", "shift"] -&gt; (time, BeginShift $ readGuardianId ident)
    ("falls":_) -&gt; (time, FallAsleep)
    ("wakes":_) -&gt; (time, WakeUp)
    _ -&gt; error "lol"
</code></pre>
<p>That is like mixing streaming and parsing at the same time. Then, the core of my algorithm: dispatch
the actions by guard. That is mandatory if we want to actually accumulate the “state” of a guardian
(when they’re sleeping, waking up, etc.). Otherwise, we get interleaved results.</p>
<pre><code>dispatchActions :: [(LocalTime, Action)] -&gt; Map GuardianId [(LocalTime, Action)]
dispatchActions = go mempty Nothing
  where
    go guardians _ ((t, action@(BeginShift gid)):xs) =
      go (insertAction gid t action guardians) (Just gid) xs

    go guardians jgid@(Just gid) ((t, action):xs) = go (insertAction gid t action guardians) jgid xs

    go guardians _ [] = fmap V.toList guardians

    go _ _ _ = error "dispatchActions: the impossible fucking occurred!"

    insertAction gid t action guardians =
      M.insertWith (flip (&lt;&gt;)) gid (V.singleton (t, action)) guardians
</code></pre>
<p>This is a by-hand fold that just applies the rule of beginning a shift (storing the ID of the
guardian that went napping so that we can correctly dispatch the remaining events).</p>
<p>Then the tricky part:</p>
<pre><code>type Minute = Natural
type Minutes = [Minute]

minutesCounts :: [(LocalTime, Action)] -&gt; Minutes
minutesCounts = go zeroMinutes Nothing
  where
    zeroMinutes = replicate 60 0 -- (1)
    asMinutes = todMin . localTimeOfDay

    -- the guard was sleeping
    go minutes (Just sleepTime) ((t, action):xs) =
      case action of
        BeginShift _ -&gt; go minutes Nothing xs
        FallAsleep -&gt; go minutes (Just t) xs -- not sure if that would even occur in the input
        WakeUp -&gt; go (addSleepCount minutes (asMinutes sleepTime) (asMinutes t)) Nothing xs

    -- the guard was awake, so we’re only interested in when they go to sleep
    go minutes Nothing ((t, action):xs) =
      case action of
        FallAsleep -&gt; go minutes (Just t) xs
        _ -&gt; go minutes Nothing xs

    go minutes _ [] = minutes

    addSleepCount minutes sleepTime t = zipWith (+) minutes range -- (2)
      where
        -- this function is a bit hacky but it generates, for a given range of time, a list of 60
        -- elements where the time period has 1 and all the other has 0 (I leave you to the
        -- exercise of making that a better function)
        range :: Minutes
        range = replicate sleepTime 0 &lt;&gt; replicate (fromIntegral t - sleepTime) 1 &lt;&gt; replicate (60 - t) 0
</code></pre>
<p>This big function generates a list which length is 60 – mapping the number of times a guard has
passed sleeping at a given minute from midnight to 1:00 AM (see <code>(1)</code> and <code>(2)</code>).</p>
<p>Finally, what we need is a way to compute frequencies – or counts. That is, given a list of anyting,
compute that number of time a given anything happens in the list. I wrote a small utility function
for that – I got inspired by [@jle], thanks!:</p>
<pre><code>freqTable :: (Ord a) =&gt; [a] -&gt; Map a Count
freqTable = M.fromListWith (+) . map (,1)
</code></pre>
<p>Then, finding the guard that has slept the more and the minute is easy:</p>
<pre><code>findMostOccurring :: Map a Count -&gt; (a, Count)
findMostOccurring = maximumBy (comparing snd) . M.toList -- and Haskell is hard?! ;)

findSleepiest :: Map GuardianId [(LocalTime, Action)] -&gt; (GuardianId, (Minute, Count))
findSleepiest =
    fmap (findMostOccurring . freqTable . spanIndex) . maximumBy (comparing $ sum . snd) . M.toList . fmap minutesCounts
  where
    spanIndex = concatMap (\(i, x) -&gt; replicate (fromIntegral x) i) . zip [0..]
</code></pre>
<p>We first find the guard that has the most time asleep (<code>maximumBy (comparing $ sum . snd)</code>. Then,
we find the minutes at which they were asleep the most (<code>findMostOccurring</code>). We are given the guard
ID, the given minute and the number of times they were asleep at that minute. Yay!</p>
<h2>Part 2</h2>
<p>For this part, we would like to know which guard is most frequently asleep on the same minute? We
already have written all the code needed for that:</p>
<pre><code>findMostFrequentlySleepy :: Map GuardianId [(LocalTime, Action)] -&gt; (GuardianId, Minute)
findMostFrequentlySleepy =
    fmap findMin . maximumBy (comparing $ maximum . snd) . M.toList . fmap minutesCounts
  where
    findMin = fst . maximumBy (comparing snd) . zip [0..]
</code></pre>
<p>Instead of summing, we find the maximum time a guard was asleep. Pretty easy.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-04/src/Main.hs">Haskell solution</a></p>
<h1>Day 5: Alchemical Reduction</h1>
<p><a href="https://adventofcode.com/2018/day/5">Text</a></p>
<h2>Part 1</h2>
<p>That puzzle is very natural to solve in Haskell. You are given an ASCII string that contains only
letters (lower case and upper case) that represent polymers. You must compute their final reduction
by following some basic rules:</p>
<ul>
<li>Two adjacent letters will cancel them out if they don’t have the same case (lower vs. upper) but
are the same letter. For instance, <code>aA</code> and <code>Bb</code> cancel each other but not <code>aa</code> nor <code>BB</code>.</li>
<li>This rule happens until no more reduction is possible. For instance, <code>AbBa</code> will first get
reduced to <code>Aa</code> because <code>aB</code> will cancel out, but then <code>Aa</code> will also cancel out and we will be
left with nothing.</li>
</ul>
<p>You must give the number of units left in the final reducted polymer after all reductions have
occurred.</p>
<p>As I said, that is very simple and elegant in Haskell:</p>
<pre><code>reduce :: String -&gt; String
reduce = go []
  where
    go [] (x:xs) = go [x] xs
    go a [] = a
    go (a:xs) (b:bs)
      | not (isLetter b) = go (a:xs) bs
      | (toLower a /= toLower b) || (isLower a &amp;&amp; isLower b) || (isUpper a &amp;&amp; isUpper b) = go (b:a:xs) bs
      | otherwise = go xs bs
</code></pre>
<p>I decided to use a <a href="https://wiki.haskell.org/Zipper">zipper</a>-like traversal. My idea is the following:</p>
<ul>
<li>Read a character. If we have nothing previously seen, just place it in the previous list.</li>
<li>If we have something previously seen, check whether it reacts with the last previously seen
character. If so, just drop it and drop the previously seen character, and go on with the next
character.</li>
<li>When we have exhausted the input list, the previously seen list of characters is the final form
of the reduction.</li>
</ul>
<p>This algorithm allows me to reduce by doing a forwards-and-backwards kind of sweeping, yielding
nice performance. Also, notice that the resulting list is reversed because of how we accumulate the
seen characters. Because we don’t care about the order, we will not reverse it back to its original
order.</p>
<p>The result is just the length of the output list.</p>
<h2>Part 2</h2>
<p>This part asks us to find the polymer that is the smaller if we remove one kind of unit (a single
letter type). So if we remove <code>a</code> for instance, we must remove all <code>a</code> and <code>A</code>.</p>
<p>As there’re only 26 possible solutions (from <code>a</code> to <code>z</code>), and because my solution to part 1 was
already fast, I decided to go brute-force with this one: reducing the input string without a’s,
reducing the input string without b’s, reducing without c’s, etc. And then simply take the shorter
one.</p>
<pre><code>bruteForce :: String -&gt; Int
bruteForce polymers = minimum (map length allReduced)
  where
    types = ['a' .. 'z']
    allReduced = map (\c -&gt; reduce $ filter (\x -&gt; toLower x /= c) polymers) types
</code></pre>
<p>Winner winner chicken dinner.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-05/src/Main.hs">Haskell solution</a></p>
<h1>Day 6: Chronal Coordinates</h1>
<p><a href="https://adventofcode.com/2018/day/6">Text</a></p>
<h2>Part 1</h2>
<p>That puzzle was one of the funniest I did. The idea is that, given an infinite 2D map, you are given
a list of several <em>points of interest</em> (POIs) in the form of <code>(x, y)</code> coordinates. The goal, for
this first part, is to find the largest zone in which all points have the same POI. What it means
is that, given several POIs, every positions on the map has a nearest POI (it can have several if
it’s at equal distance to several POIs – those must be discarded by the algorithm so that they do
not count into any zone). Several positions with the same nearest POI and adjacent to each others
form a zone, so that anyone in that zone knows that the nearest POI is the same accross all spanning
positions of the zone – you can picture the zone easily as discs centered on the POIs, but deformed
by other POIs.</p>
<p>The tricky part is that the map is infinite and POIs are scattered around it.</p>
<p>My idea was based on something I do <strong>a lot</strong> on my spare time with my 3D projects: compute AABBs.
An <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a> is an enclosing box in which all points lie. The idea is that its size must be as minimal
as possible. An <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a>, which stands for Axis-Aligned Bounding Box, is the minimal bounding volume
that enclose a set of points and which has its edged aligned with the axis (in our case, the X and
Y axis). By the way, an <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a> in 2D is also called <strong>MBR</strong>, which stands for Minimum Bounding
Rectangle. I chose AABB instead of MBR because I’m more used to work in 3D, but they’re essentially
the same thing.</p>
<p>So, the first thing I wanted to do is to compute the AABB of all the POIs for a two reasons:</p>
<ul>
<li>The most important one: it helped me reduce the problem space from an infinite space to a
finite one: instead of having an infinite 2D map, I now have a finite rectangular 2D region to
explore. Which is, you’ll have to admit, much more comfortable. ;)</li>
<li>If you read the puzzle’s text, you’ll have notice this:
<ul>
<li><em>“Your goal is to find the size of the largest area that isn’t infinite.”</em></li>
<li>What it means is that all the points <strong>outside</strong> of the AABB are in an area that is always
infinite, so we will never be interested in those zones.</li>
</ul>
</li>
</ul>
<p>Ok, let’s see my code:</p>
<pre><code>type Point = (Int, Int)

-- Note to readers: this the way I like to encode AABB because it eases some operations on points.
-- The lower point is a point that belongs to the AABB that satisfies the rule that no other point
-- with different coordinate are lower than it. Same thing for upper. I also like that encoding
-- because generating an AABB from a set of points is trivial.
data AABB = AABB {
    aabbLower :: Point,
    aabbUpper :: Point
  } deriving (Eq, Show)

findAABB :: [Point] -&gt; AABB
findAABB [] = error "nein" -- this will never be called, so we don’t care about type safety here
findAABB (a:ls) = foldl' updateAABB (AABB a a) ls
  where
    updateAABB (AABB (lx, ly) (ux, uy)) (x, y) = AABB {
        aabbLower = (min (min lx x) lx, min (min ly y) ly),
        aabbUpper = (max (max ux x) ux, max (max uy y) uy)
      }

-- This function gives me a list of points that are in the AABB. It actually gives me all the points
-- the AABB wraps.
aabbToStream :: AABB -&gt; [Point]
aabbToStream (AABB (lx, ly) (ux, uy)) = [(x, y) | x &lt;- [lx .. ux], y &lt;- [ly .. uy]]

-- Test whether a point lies on any edges of the AABB. You’ll get why this function is important
-- later.
liesOnAABB :: Point -&gt; AABB -&gt; Bool
liesOnAABB (x, y) (AABB (lx, ly) (ux, uy)) = x == lx || x == ux || y == ly || y == uy
</code></pre>
<p>I annotated the code with comments so that you can get what it’s for.</p>
<p>So, I create the <code>AABB</code> and then I call <code>aabbToStream</code> in order to get all points. You might already
have guessed the next step: we are going to find the nearest POI to all the points. For this, I
just went naive and just computed the <a href="https://en.wikipedia.org/wiki/Taxicab_geometry">Manhattan distance</a> to all POI and kept the smallest. If we
map that function to all coordinates generated by the AABB, we get the first part of the solution.</p>
<pre><code>manhDist :: Point -&gt; Point -&gt; Int
manhDist (a, b) (c, d) = abs (a - c) + abs (b - d)

nearest :: Point -&gt; [(Int, Point)] -&gt; Maybe Int
nearest p points =
  case sortBy (comparing snd) $ map (\(i, x) -&gt; (i, manhDist p x)) points of
    [a] -&gt; Just (fst a)
    a:b:_ -&gt; if snd a == snd b then Nothing else Just (fst a) -- (1)
    _ -&gt; error "nearest"
</code></pre>
<p>Here, <code>(1)</code> applies the rule I described earlier about at least two POIs at the same distance: we
just discard the point and it doesn’t participate in creating any zone.</p>
<p>Then, how do we find the biggest area? Easy: we re-use our <code>freqTable</code> function from Day 4 to
compute a frequency table! In my case, I just renamed that function <code>freqs</code>:</p>
<pre><code>freqs :: (Ord a) =&gt; [a] -&gt; Map a Natural
freqs = fromListWith (+) . map (,1)
</code></pre>
<p>If we call that function on a list of <code>[Int]</code>, we end up with <code>Map (Maybe Int) Natural</code> that
gives us the number of positions a given POI is the nearest. It’s perfect, because it’s exactly
what we are looking for!</p>
<pre><code>biggestArea :: [Maybe Int] -&gt; Natural
biggestArea = snd . maximumBy (comparing snd) . M.toList . freqs . catMaybes
</code></pre>
<p>Here, <code>catMaybes</code> just remove the <code>Nothing</code> case so that we go from <code>[Maybe Int]</code> to <code>[Int]</code>. We
then find out which POI has the biggest number of nearest positions and we simply return it. Because
those are positions, their sum is then the area of the zone: we’re done. Or almost. Remember that
some zones have an infinite areas. Thanks to the AABB, it’s actually easy to find which ones: those
have at least one point that lies on the AABB’s edges. We just have to iterate through all the
points and black list some points:</p>
<pre><code>blackListPoints :: [(Point, Maybe Int)] -&gt; AABB -&gt; Set Int
blackListPoints points aabb = foldl' blacklist mempty points
  where
    blacklist blist (p, Just i) = if liesOnAABB p aabb then S.insert i blist else blist
blacklist blist _ = blist
</code></pre>
<h2>Part 2</h2>
<p>The part 2 asks something different: now we want to find the area of the region containing all
locations for which the total distance to all POI is less than a given constant (<code>10000</code>). My
solution was actually way easier than expected, surprisingly:</p>
<pre><code>safeArea = filter (\p -&gt; sum (map (manhDist p) coords) &lt;= 10000) points
</code></pre>
<p>Done. :)</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-06/src/Main.hs">Haskell solution</a></p>
<h1>Day 7: The Sum of Its Parts</h1>
<p><a href="https://adventofcode.com/2018/day/7">Text</a></p>
<h2>Part 1</h2>
<p>Here we go again: graph theory. Fortunately for us, it’s not a hard graph puzzle. That first part is
to simply display a string that shows the order in which a graph must be traversed. If two nodes can
be traversed at the same time, the node which letter comes first alphabetically is traversed first.</p>
<p>I’ll just show the traversal because the rest is not really interesting for that puzzle:</p>
<pre><code>-- The graph encodes the relationship in reverse: it maps each node its list of dependencies.
-- So if we have something like A -&gt; [], it means that the A node doesn’t have any dependency.
type Graph = Map Step (Set Step)
type Step = Char

-- Get the list of all available steps; i.e. they don’t have any dependency.
getAvailable :: Graph -&gt; [Step]
getAvailable gr = [step | (step, set) &lt;- M.toList gr, S.null set]

-- Traverse the graph and get the ordered steps to go through.
stepAvailable :: Graph -&gt; [Step]
stepAvailable gr = case sort (getAvailable gr) of
  [] -&gt; []
  (s:sx) -&gt; s : stepAvailable (removeStep s gr)

removeStep :: Step -&gt; Graph -&gt; Graph
removeStep s = purgeDep s . M.delete s
  where
    purgeDep = fmap . S.delete
</code></pre>
<p>It’s a typical functional problem that gets solved very easily in Haskell.</p>
<h2>Part 2</h2>
<p>The second part is pretty interesting. Instead of stepping through all the steps sequentially, you
ar given a <em>pool of workers</em>. It will take a given amount of time for a given worker to complete a
task and able us to visit a given node in the graph. We have to guess how many time it will take to
complete all of the steps.</p>
<p>I won’t post the code (it’s on GitHub if you want to have a look at it) as it’s a bit boring and the
idea of my solution is enough. The concept is to have a stepped simulation (i.e. you perform a set
of action in a given <em>“round”</em>, then repeat). In my case, each round is composed of several steps:</p>
<ol>
<li>First, partition the current work load into a set of <em>done tasks</em> and <em>running tasks</em>. This is
quite easy to do by just checking at the remaining time of each task. If the remaining time is
<code>0</code>, then it’s done, otherwise it’s still running.</li>
<li>Generate the <em>time increment</em>. This is the minimal duration until a next interesting action
occurs (i.e. a task gets done). Nothing can happen below that duration. That value can easily
be found by looking up the remaining duration of the running tasks and taking the minimum.</li>
<li>If we still have running tasks, step forward (i.e. recursively call the same function) by
advancing the current time by the time increment and removing the done tasks.</li>
<li>The <em>backlog</em>, that is implicit, can be created by monoidal operations and is detailed in the
code on GitHub.</li>
<li>When the backlog gets empty, we have the final time and the answer to the initial question.</li>
</ol>
<p>This part was interesting because it made me write a parallel graph traversal (a graph task
scheduler) that could be used as base for a real and parallelized (I/O) task scheduler. Interesting
stuff.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-07/src/Main.hs">Haskell solution</a></p>
<h1>Day 8: Memory Maneuver</h1>
<p><a href="https://adventofcode.com/2018/day/8">Text</a></p>
<h2>Part 1</h2>
<p>In its simple form, this puzzle is not really interesting and I could definitely present you the
solution in a single paragraph. However, I found it pretty fun to do so I’ll go a bit further.</p>
<p>The problem is the following: we are given a list of numbers that represent a data structure. That
data structure is basically a tree with tagged metadata. The goal is to parse the list of numbers
to generate a memory representation of that tree and compute checksums on it. The structure is:</p>
<ul>
<li>A header, that has the number of children of a given node and the number of metadata entries.</li>
<li>Zero or many children.</li>
<li>At least one or many metadata entries.</li>
</ul>
<p>The file format is made so that the data are nested. I’ll copy and explain an example:</p>
<pre><code>2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
A----------------------------------
    B----------- C-----------
                     D-----
</code></pre>
<p>Here, only the first line is present in the input file. The first <code>2</code> means that the first (<code>A</code>)
node has two children (we don’t know anything about them yet) and the <code>3</code> means it has three
metadata. Those are the header. Then, since it has two children, the next <code>0</code> is the start of the
header of its first children (<code>B</code>), which has no child and three metadata (<code>3</code>). The next <code>10</code>, <code>11</code>
and <code>12</code> are then those metadata (since it doesn’t have any child). This node is then complete. If
you go back up in the tree, you know that <code>A</code> still has another child. So the next number, <code>1</code>, is
the number of child of <code>C</code> and <code>1</code> its number of metadata. The next number <code>0</code> is the number of
child of <code>D</code> and it has <code>1</code> metadata, which is <code>99</code>. <code>C</code>, as seen above, has one metadata, so <code>2</code> is
<code>C</code>’s metadata. Then, since <code>A</code> has three metadata, <code>1</code>, <code>1</code> and <code>2</code> are its.</p>
<p>Pfiou. Seems hard to read for a human, right? However, if you’re used a bit to recursive data
structure and more specifically recursive parsers, this kind of encoding is actually pretty neat!</p>
<p>Let’s go and implement the parser of that tree. First, the structure. We will not need the header in
the output (it’s only used for parsing), so we will not encode that directly (it’s still available
as the length of the list of children and length of the list of metadata entries):</p>
<pre><code>data Node = Node {
    nodeChildren :: [Node],
    nodeMetadata :: NonEmpty Natural
  } deriving (Eq, Show)
</code></pre>
<p>Pretty simple, right? This is a self-recursing data structure that is pretty simple and basic to any
functional programmer.</p>
<blockquote>
<p>The <code>NonEmpty a</code> data type, in Haskell, is a list that cannot have zero element. That is enforced
at compilation as it’s impossible to create such a list without explicitly giving at least one
element. All the operations defined on <code>NonEmpty a</code> respect that rule (for instance, removing an
element from it might not remove anything if it has only one element – otherwise you’d break its
invariant and Haskell is not Javascript. <a href="https://phaazon.net/media/uploads/inner_troll.png">Lol</a>.</p>
</blockquote>
<p>So, how do we parse that? I’ve already spoiled you the solution: we need to implement a recursive
parser. I know that because I’ve been using <a href="http://hackage.haskell.org/package/parsec">parsec</a> for like 7 years now, so I’m pretty used to
that kind of parsing and as you use it, you will quickly recognize when you can use such an idiom.</p>
<p>However, instead of using <a href="http://hackage.haskell.org/package/parsec">parsec</a> directly, I will implement it myself with some very basic types
every Haskellers know – if you don’t: go learn them! I’ll be using the <code>State</code> type only, which is
basically <em>just</em> a recursive function used in a monadic fancy way:</p>
<pre><code>-- A possible representation of the State monad is just a function that takes a value 's' and
-- returns a new, altered 's' (we call that a state, but as you can see, it’s completely pure code,
-- no mutation happens at all) and an output value 'a'.
data State s a = State { runState :: s -&gt; (s, a) }
</code></pre>
<blockquote>
<p>The real <code>State</code> type is defined in the <a href="http://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-State-Lazy.html#t:State">mtl</a>
Haskell library.</p>
</blockquote>
<p>So here, you have the <em>impression</em> or <em>illusion</em> of <code>s</code> being a state, but what it truly is is just
a value that is passed around to a recursive function – and plot twist: recursion is the way to
implement locally-defined states, but I will not explain that (read my <a href="https://phaazon.net/blog/getting_into_netwire">netwire tutorial</a> and the
<code>Auto</code> type, if you are interested).</p>
<p>The functor / monadic part:</p>
<pre><code>instance Functor (State s) where
  fmap f = State . fmap (fmap f) . runState

instance Applicative (State s) where
  pure x = State (, x)
  p &lt;*&gt; q = State $ \s -&gt;
    let (s', f) = runState p s
        (s'', a) = runState q s'
    in (s'', f a)

instance Monad (State s) where
  return = pure
  q &gt;&gt;= f = State $ \s -&gt; let (s', a) = runState q s in runState (f a) s'
</code></pre>
<blockquote>
<p>All of this can be generated automatically by GHC with <code>deriving</code> data annotation.</p>
</blockquote>
<p>And some combinators we’ll need:</p>
<pre><code>-- Get the current value of the “state”.
get :: State s s
get = State $ \s -&gt; (s, s)

-- Get the current value of the “state” with a function pre-applied to it.
gets :: (s -&gt; a) -&gt; State s a
gets = flip fmap get

-- Change the value of the “state” by applying a function to the state.
modify :: (s -&gt; s) -&gt; State s ()
modify f = State $ \s -&gt; (f s, ())

-- Just a convenient method to just get the output value and discard the final state. You need the
-- initial value to use as state.
evalState :: State s a -&gt; s -&gt; a
evalState st = snd . runState st
</code></pre>
<p>So basically, since this is a very basic and simple code (I think all Haskellers should write that
in their first month using Haskell, it’s a good exercise), I just included the <code>mtl</code> library and
used its <code>State</code> type to write my recursive parser.</p>
<p>This is my parser:</p>
<pre><code>newtype Parser a = Parser { runParser :: State [Natural] a } deriving (Applicative, Functor, Monad)
</code></pre>
<p>So basically, a <code>Parser a</code> generates value of type <code>a</code> and maintains a list of <code>Natural</code> around.
Those <code>Natural</code> are the numbers from the input we are going to parse. Let’s write the actual parser
now.</p>
<pre><code>-- Turns the (string-encoded) list of numbers and generates the root node, that contains all of
-- the children.
parse :: String -&gt; Node
parse = evalState (runParser parseNode) . map read . words

-- Read a single number from the input and consume it from the state.
readInput :: Parser Natural
readInput = Parser $ gets head &lt;* modify tail

parseNode :: Parser Node
parseNode = do
  -- We read the two first numbers (header)
  childrenNb &lt;- readInput
  metadataNb &lt;- readInput

  -- Recursive parser! The NE.fromList is an unsafe function that is used for convenience for this
  -- puzzle part.
  children &lt;- replicateM (fromIntegral childrenNb) parseNode
  metadata &lt;- fmap NE.fromList (replicateM (fromIntegral metadataNb) readInput)

  pure $ Node children metadata
</code></pre>
<p>As you can see, the parser code is extremely simple with a recursive combinator parser! And we’re
actually done for the first part. The checksum is simple and is:</p>
<pre><code>checksum :: Node -&gt; Natural
checksum node = metadataChecksum (nodeMetadata node) + sum (map checksum $ nodeChildren node)

metadataChecksum :: NonEmpty Natural -&gt; Natural
metadataChecksum = sum . NE.toList
</code></pre>
<h2>Part 2</h2>
<p>The second part is not interesting as it just requires a new method to compute the “value” of a
given node:</p>
<pre><code>nodeValue :: Node -&gt; Natural
nodeValue (Node [] metadata) = metadataChecksum metadata
nodeValue (Node children metadata) = sum [nodeValue n | Just n &lt;- map index (NE.toList metadata)]
  where
    index i =
      let i' = fromIntegral i - 1
      in if i' &lt; 0 || i' &gt;= length children then Nothing else Just (children !! i')
</code></pre>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-08/src/Main.hs">Haskell solution</a></p>
<h1>Day 9: Marble Mania</h1>
<p><a href="https://adventofcode.com/2018/day/9">Text</a></p>
<h2>Part 1 &amp; 2</h2>
<p>This puzzle was the first one when I decided to go full Rust! All the remaining puzzles were solved
in Rust – if you were reading only for Haskell, sorry for your loss. :(</p>
<p>This puzzle is not really interesting as it’s just a fancy algorithm that adds element to a
collection and sometimes removes from it. There was a trick, though: the second part requires to run
our algorithm on an input that was a hundred times larger.</p>
<p>The typical trap is that when you add value in the middle of a collection, the complexity in terms
of memory and CPU can largely vary. Everything depends on what you do. For very rare additions /
deletions, it’s possible that you can accept <em>O(n)</em> complexities. However, if you often insert
stuff, you might want something else. In the same spirit, some data structure can efficiently add
in <em>O(1)</em> at the beginning or end of the collection or might require a complete copy.</p>
<p>Even though the puzzle is not interesting in itself, it reminds us how crucial and critical it is
that a programmer must know what structure to use depending on the inputs <strong>and operations that will
be performed on the data</strong>. In our case, we are going to add and remove <em>a lot</em> at arbitrary places
in the collection. Vectors are really bad candidates at that kind of operations, because they will
require a complete scan of the right part of the collection, which is <em>O(n)</em>, every time you add
or delete something (to shift right / left, respectively). This is bad. Vectors are also bad when
you want to add at its beginning (it requires the same right shift as the random case).</p>
<p>Double-ended queue (<a href="https://doc.rust-lang.org/std/collections/struct.VecDeque.html"><code>VecDeque</code></a> in
Rust) are a solution to the problem to insert at the beginning. That insertion is <em>O(1)</em> amortized.</p>
<p>My solution to this was to use a <a href="https://wiki.haskell.org/Zipper">zipper</a> to stay focus on a given number in the collection but also
“move around” in <em>O(1)</em>. The idea is the following:</p>
<pre><code>struct Zipper {
  left: VecDeque&lt;isize&gt;,
  middle: isize,
  right: VecDeque&lt;isize&gt;,
}
</code></pre>
<p>When you want to move to the left, you just take the <code>middle</code> number and <code>push_front</code> it to the
<code>right</code> double-ended queue and you <code>pop_back</code> the <code>left</code> one and that value becomes the new
<code>middle</code>. You do the opposite thing to go to the right.</p>
<p>To insert an element at the current location, you just <code>push_front</code> <code>middle</code> to the <code>right</code> and then
<code>middle</code> is assigned the new value. To remove, you just <code>pop_front</code> <code>right</code> into <code>middle</code>.</p>
<p>Then, the all puzzle is just adding and removing according to a predicate. Since all the operations
I mentioned above run in <em>O(1)</em> amortized (they might allocate if the buffer is too small), we will
not suffer from the typical <em>O(n²)</em> complexity a <code>Vec</code> implementation has.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-09/src/main.rs">Rust solution</a></p>
<h1>Day 10: The Stars Align</h1>
<p><a href="https://adventofcode.com/2018/day/10">Text</a></p>
<h2>Part 1</h2>
<p>This is the kind of problem I suck the most at. Not because they’re hard. Because they’re easier
than expected. As an engineer, I tend to overthink about the context, the input’s hidden properties,
the possible errors, the heuristics, what could go wrong, etc. On a regular job basis, that is
actually a good thing – it’s better to foresee things than to have to repair them. However, at a
given extreme, that way of thinking will make you go nuts and will make you think of an easy and
straightforward problem as a complex and convoluted one. I know that and the main thing my mind does
when solving a problem is to think about how <strong>really hard</strong> a problem is. I just completely failed
on this one, haha. :D</p>
<p>So, you are given a short list (~360) of 2D points. You know nothing about how they’re spread nor
the limits they lie in. You <strong>only</strong> have ~360 points on a 2D plane. Those points, however, come
with two attributes:</p>
<ul>
<li>Their positions, as a pair of relative numbers ([<code>-∞; +∞]</code>).</li>
<li>Their (constant) velocities, as a pair of relative numbers as well ([<code>-∞; +∞]</code>).</li>
</ul>
<p>So basically, each point starts at a given position and goes into a straight line forever. The unit
of the velocity is not known and is not really needed – even though it might be <code>unit/s</code>.
Nevertheless, the text gives the hinting that, at a given (unknown) time, the points form a message
that can be visually witness. The goal is to give the message.</p>
<p>In the first 15 minutes, I went through several thoughts. “Whoa, they want us to write an <a href="https://en.wikipedia.org/wiki/Optical_character_recognition">OCR</a>?!
Really?!”. Nah, you dumbass. Since we all have a unique input (and hence, a unique expected output),
we don’t have to write an algorithm that can recognize any text. We just have to get to visualize
our input.</p>
<p>However, when does the text appear? At <code>t = 0</code>, we have a large cloud of spread points. We don’t
know when the text will form. Also, the challenge explicitly states that the text forms only once:
the points will never gather into text afterwards. We must not miss it then.</p>
<p>My idea was that to find hidden properties of the overall text first. By being able to extract a
useful information telling me whether or not I’m far or close from having a visualizable text, I was
able to run a loop-like simulation, moving each points by its respectiv velocities, until that
hidden information reaches a local minimum. As an engineer, I was annoyed by that, because I had no
idea whether the first local minimum was the right one – the puzzle’s text doesn’t state anything
about that and I had not found any information to help with that in the input. I could also use the
wrong criteria (maybe we’re looking for a local maximum?). I got stuck with those ideas for long
minutes.</p>
<p>Finally, I decided to implement a specific criteria:</p>
<ul>
<li>At each simulation step, we compute the <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a> that encloses all the input points that have
moved.</li>
<li>If that <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a>’s <a href="https://en.wikipedia.org/wiki/Area">area</a> is less then the previous one, we store the <a href="https://en.wikipedia.org/wiki/Area">area</a>, the width and height
of the <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a> and the current time of the simulation. We also print those values on the standard
output.</li>
<li>If not, we continue.</li>
</ul>
<p>When I ran that loop, I got the first local minimum in <strong>10011</strong> seconds. Clearly, if you tried to
actually run that simulation with the real time, you’d be waiting for a long time – <strong>10011</strong>
seconds is 2 hours, 46 minutes and 51 seconds.</p>
<p>The size of the <a href="https://en.wikipedia.org/wiki/Bounding_volume">AABB</a> at <code>t = 10011</code> was also pretty small (around <strong>60×60</strong>). I then decided to
display the message directly in the console. In order to do that, I had to transform my 2D points
(expressed in the natural ℝ² basis we use in <em>world space</em> coordinates) into a space that I could
easily use to display (basically, <code>[0; w]</code> and <code>[0; h]</code>). That transformation is done with the
following code:</p>
<pre><code>// The rendered “map”
let mut lol = vec!['.'; w as usize * h as usize];

for p in points {
  let x = (p.position.0 - aabb.lower.0) * (w - 1) / w;
  let y = (p.position.1 - aabb.lower.1) * (h - 1) / h;
  let o = x + y * w;

  lol[o as usize] = '#';
}
</code></pre>
<p>Then, we just need to iterate on all the points and render them to the terminal to finish the
challenge:</p>
<pre><code>for row in 0 .. h {
  for col in 0 .. w {
    print!("{}", lol[(col + row * w) as usize]);
  }

  println!("");
}
</code></pre>
<h2>Part 2</h2>
<p>Part 2 was almost a joke: we were asked to give the time at which the text appeared. As this was a
<em>hidden property</em> to find in the first place, completing part 2 took a few seconds: <code>10011</code>.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/tree/master/day-10/src/main.rs">Rust solution</a></p>
<h1>Day 11: Chronal Charge</h1>
<p><a href="https://adventofcode.com/2018/day/11">Text</a></p>
<h2>Part 1</h2>
<p>A pretty common algorithm to implement: sliding window. Basically, you are given a matrix of numbers
and you have to compute several sums using a sliding <em>kernel</em> which size is <em>3×3</em>. The size of the
matrix is <em>300×300</em> and you just want to compute the biggest <em>3×3</em> square (and give its index in the
matrix as row / column).</p>
<p>This was my solution:</p>
<pre><code>let mut largest = (0, i8::min_value()); // (index, power)

// 298 is 300 - 2: we want to stop there so that the 3×3 square won’t overflow
for row in 0 .. 298 {
  for col in 0 .. 298 {
    let mut power = 0;

    // sum the square
    for i in 0 .. 3 {
      for k in 0 .. 3 {
        power += grid[index(col + i, row + k)];
      }
    }

    let i = index(col, row);

    // if its power is any larger, store it along with its index
    if (power == largest.1 &amp;&amp; i &lt; largest.0) || power &gt; largest.1 {
      largest = (i, power);
    }
  }
}

println!("Largest fuel cell: ({}, {})", 1 + largest.0 % 300, 1 + largest.0 / 300);
</code></pre>
<p>That’s pretty much it. Second part is more interesting.</p>
<h2>Part 2</h2>
<p>For this part, the problem changes a bit: we still want to sum squares, but we want to get the find
the square that has the largest total power of any size comprised between <em>1×1</em> and <em>300×300</em> – we
want its index and its size.</p>
<p>That problem can be solved in several ways, with different complexities. It’s easy to see that you
can quickly go with a bad complexity if you decide to refactor the previous algorithm to take a
dimension (that will be squared) and call it 300 times. Maybe that would be enough.</p>
<p>However, I wanted to implement something smarter on this one. It’s easy to see that a lot of
spanning squares will overlap. For instance:</p>
<pre><code>+-+-+-+-+-+
|0|1|2|3|4|
+-+-+-+-+-+
|6|7|8|9|A|
+-+-+-+-+-+
|B|C|D|E|F|
+-+-+-+-+-+
</code></pre>
<p>If you consider the first, top-leftmost <em>2×2</em> square:</p>
<pre><code>+-+-+
|0|1|
+-+-+
|6|7|
+-+-+
</code></pre>
<p>And the top-left-mostmost <em>3×3</em> square:</p>
<pre><code>+-+-+-+
|0|1|2|
+-+-+-+
|6|7|8|
+-+-+-+
|B|C|D|
+-+-+-+
</code></pre>
<p>You can see that a the smaller one is included in the bigger one. What it means is that each
spanning square is a partial sum to spanning square of a higher dimension. My algorithm benefits
from that in order to reduce the number of elements to sum at each given dimension.</p>
<p>Also, another thing I did that suprised people on IRC: I reversed the way the algorithm works in
terms of traversal. Instead of traversing dimensions and then traversing the grid (for all
dimensions then for all squares), I traverse the grid and then I traverse the dimensions (for all
square in the grid, for all the dimensions). This gives me a more natural way to write the partial
sums in my code.</p>
<p>Finally, I also work out some formalæ to know “what’s the biggest dimension we can go up to given
the current grid cell.” Yeah, think twice: when you want to go through all dimensions from the
top-leftmost cell, you will be able to sum squares from <em>1×1</em> up to <em>300×300</em>. But which dimension
can you go to when the starting (<em>1×1</em>) cell is in the middle of the grid? This is actually pretty
easy. The formalæ can be found very quickly by thinking in terms of size of the grid (<em>300×300</em>) and
the index of a grid cell. The biggest dimension is just the minimum of the maximal allowed row
iterations and the maximal allowed column iterations. You can picture that mentally by “which edge
I am the closest to?”. For rows, it’s simply <code>300 - current_row</code> and for columns,
<code>300 - current_column</code>. The minimum value gives you the maximal spanning dimension you can go up to.</p>
<p>Finally, a word on how the partial sums are created: when you start computing the sum of the <code>N</code>
dimension, you already have the partial sum of dimension <code>N-1</code> (the algorithm starts with the first
dimension set to a given value). Then, instead of summing <code>N²</code> element, since you already have the
sum of <code>(N-1)²</code>, you just need to sum <em>2 × (N - 1) + 1</em> values. If you’re not convinced, at
dimension <code>278</code>, <code>278² = 77284</code> sums while my algorithm is <code>2 × (278 - 1) + 1 = 555</code> sums. It’s
around <strong>139 times less</strong>.</p>
<p>In my code, I do that by adding – relative to the previous spanning square – the right column
(which size is <em>N - 1</em>), the bottom line (<em>N - 1</em> as well) and the single element in the diagonal.
Hence <code>2 × (N - 1) + 1</code>. And that completes a new partial sum, that will be used for higher
dimensions!</p>
<p>Here’s just a very quick schema to show you how to compute the sum at dimension <code>5</code> by using the
sum of the spanning square of dimension <code>4</code> – <code>·</code> is already computed and <code>R</code> are the right column,
<code>B</code> the bottom line and <code>D</code> the element in the diagonal:</p>
<pre><code>+-+-+-+-+-+
|·|·|·|·|R|
+-+-+-+-+-+
|·|·|·|·|R|
+-+-+-+-+-+
|·|·|·|·|R|
+-+-+-+-+-+
|·|·|·|·|R|
+-+-+-+-+-+
|B|B|B|B|D|
+-+-+-+-+-+
</code></pre>
<p>So, here’s the code:</p>
<pre><code>let mut largest2 = (0, i64::min_value(), 0); // (index, power, dimension)

// for all rows…
for row in 0 .. 300 {
  let max_iter_row = 300 - row; // 300 -&gt; 1

  // for all columns…
  for col in 0 .. 300 {
    let max_iter_col = 300 - col; // 300 -&gt; 1
    let max_dim_squared = max_iter_row.min(max_iter_col); // 300x300 -&gt; 1x1

    // power used for nested dimensions
    let mut nested_power = grid[index(col, row)] as i64;

    // note: we don’t have to compute the first dimension because it’s set right away to the given
    // nested power

    // for all dimensions up to the max
    for d in 1 .. max_dim_squared {
      let mut power = nested_power;

      // compute the 2 × (N - 1) elements
      for k in 0 .. d {
        power += grid[index(col + d, row + k)] as i64;
        power += grid[index(col + k, row + d)] as i64;
      }

      // add the diagonal
      power += grid[index(col + d, row + d)] as i64;

      let i = index(col, row);

      if (power == largest2.1 &amp;&amp; i &lt; largest2.0) || power &gt; largest2.1 {
        largest2 = (index(col, row), power, d + 1);
      }

      nested_power = power;
    }
  }
}

println!("Largest fuel cell of all: ({}, {}, {}, of power {})", 1 + largest2.0 % 300, 1 + largest2.0 / 300, largest2.2, largest2.1);
</code></pre>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/tree/master/day-11/src/main.rs">Rust solution</a></p>
<h1>Day 12: Subterranean Sustainability</h1>
<p><a href="https://adventofcode.com/2018/day/12">Text</a></p>
<h2>Part 1</h2>
<p>This puzzle looked a bit like the double-ended queue one from day 9. The extra bit of information
is that you now have to apply a pattern on several values to know how they should be mutated. Given
a list of flower pots and some rules that give you how a pot should grow flowers (or not) according
to the state of itself and its neighbors, the goal is to predict the sum of the pots (their index in
the list) for all pots that contain flowers after <strong>20 generations</strong>.</p>
<p>In the first place, I had to recognize that I needed a double-ended queue. As always, the puzzle’s
text doesn’t explicitly tell you that the pots at leftmost and rightmost positions can “spawn” new
pots by applying the rules on empty pots (infinite). I was confused at that for a while.</p>
<p>My encoding of rules is pretty simple and wasteful: since a rule gives you a pattern (which pots)
and an output (should have flowers / shouldn’t), a single byte should be enough for that (the length
of a rule is five: it gives you the state of the two left neighbors, the state of the current pot
and the state of the two right neighbors). However, I encoded those with a simple array of five
binary states (<code>Rule::Empty</code> and <code>Rule::Pot</code>).</p>
<p>In order to apply a pattern, we must retreive the current pot and two at its left position and two
at its right position (if pots are missing because we’re at edges, we must spawn empty pots with
negative / positive indices). Then we can just apply the pattern by looking it up in a hashmap: we
get the next generation value.</p>
<p>Nothing really interesting code-wise to show here.</p>
<h2>Part 2</h2>
<p>Part 2 is funny. We’re told that instead of finding the sums after <strong>20 generations</strong>, we need to
find it <strong>after fifty billion (50000000000) generations.</strong> Obviously, trying to run the above
algorithm for 50000000000 generations will take ages, so we need to find a better way.</p>
<p>My first inital idea was that if I took a look at the actual sum value at each generation, I could
– perhaps – see some kind of patterns. At first I was looking for cycles and hence cycling sums. I
then run my algorithm and had a look at the output data. I was suprised to find that, very quickly,
the flowers grow linearily. What it means is that, after a given number of generations, you can
guess how many flowers there will be at a given future generation by applying a linear formula
(typically, a simple multiplication and addition).</p>
<p>In my case, I noticed that at generation <code>100</code>, the sum was <code>6346</code>. At <code>101</code>, it was <code>6397</code>. At
<code>102</code>, it was <code>6448</code>. At <code>200</code>, it was <code>16546</code>. You can see the pattern – if you don’t, compute the
difference between the sum at <code>101</code> and the sum at <code>100</code>… and the difference of sum at <code>102</code> and
<code>101</code>.</p>
<p>Hence, I came up with the following linear formula:</p>
<pre><code>// O(1) get the score at a given generation – works only for gen ≥ 100.
fn score_at(gen: u64) -&gt; u64 {
  6346 + (gen - 100) * 51
}
</code></pre>
<p>The actual implementation uses <code>101</code> instead of <code>100</code> because we want to get the sum <strong>after</strong> a
given number of generations, not <strong>at</strong>.</p>
<p>That kind of linear optimization was really fun to write yet a bit tricky to find. :)</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-12/src/main.rs">Rust solution</a></p>
<h1>Day 13: Mine Cart Madness</h1>
<p><a href="https://adventofcode.com/2018/day/13">Text</a></p>
<h2>Part 1</h2>
<p>I think this was the puzzle I enjoyed the most – among the ones I did. The goal is to parse is rails
map on which wagons go and make wagons move around by respecting some specific rules: we must find
wagon collision is report their positions 2D position.</p>
<p>Parsing is actually pretty simple: the input data is the 2D map that contains the rail system along
with the initial position of wagons. About the map, I decided to store only crossings, not the
actual rails, because I didn’t need them! So in order to do so, I changed a bit the regular way I
encode 2D maps in memory and decided to use a hashmap!</p>
<pre><code>#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Rail {
  Cross, // +
  RampRight, // /
  RampLeft // \
}

struct Map(HashMap&lt;(u32, u32), Rail&gt;);
</code></pre>
<p>The rest of the code is actually pretty simple: there are several functions, one for moving carts,
one for changing directions at cross, one for detecting collision. A main <code>loop</code> is responsible in
moving carts and checking if there’s any collision. If no collision is detected, we just loop back
up. If a detection is detected, we break the loop and display the position of the crash. The
collision algorith returns the IDs of the carts that collided into each other.</p>
<pre><code>let collision = loop {
  // sort the cart by y component
  carts.sort_by(|a, b| a.pos.cmp(&amp;b.pos));

  let collisions = move_carts(&amp;map, &amp;mut carts);
  if !collisions.is_empty() {
    break collisions[0];
  }
};

println!("First collision: {:?}", carts[collision.0].pos);
</code></pre>
<p>The <code>sort_by</code> is needed because of priority rules in the puzzle’s text.</p>
<p>Moving carts and detecting collision is pretty straightforward:</p>
<pre><code>fn move_carts(
  map: &amp;Map,
  carts: &amp;mut Vec&lt;Cart&gt;,
) -&gt; Vec&lt;(usize, usize)&gt; {
  let mut collisions: Vec&lt;(usize, usize)&gt; = Vec::new(); // no collision to begin with

  // move and check collision for all carts
  'outer: for i in 0 .. carts.len() {
    // check that this cart hasn’t been collided into yet
    for &amp;collision in &amp;collisions {
      if i == collision.0 || i == collision.1 {
        // already collided, don’t move that
        continue 'outer;
      }
    }

    // move the cart and check if it’s collided into another
    move_cart(map, &amp;mut carts[i]);
    let collision = find_collision(&amp;carts, i);

    if let Some(collider) = collision {
      collisions.push((collider, i));
    }
  }

  collisions
}
</code></pre>
<p>This code is not really optimized – we redo the same thing very often – but it’s way than enough to
solve that puzzle’s part. Finding collision is very simple: we just try to find a cart with the
same position.</p>
<p>The tricky part is for moving at cross. The rules state that if you arrive at a cross, you have to
turn in a given direction and change the future direction you will take at the future cross, if any.
This was encoded inside each cart, so that they have a <em>“memory”</em> of turns to take.</p>
<pre><code>#[derive(Debug)]
struct Cart {
  pos: (u32, u32),
  dir: Direction,
  next_turn: Turn
}
</code></pre>
<p>A cart starts by going on its (relative!) <code>Turn::Left</code>, then at the next turn it will go
<code>Turn::Straight</code>, then <code>Turn::Right</code> and finaly will loop back to <code>Turn::Left</code>. Note how different
it is to <code>Direction</code>: a <code>Turn</code> is relative to the current movement of a cart while a <code>Direction</code> is
absolute (at first, I wanted to have <code>North</code>, <code>East</code> etc. for <code>Direction</code> so that <a href="https://kaamelott-soundboard.2ec0b4.fr/#son/de_tout_facon_on_dit_le_nord_selon_comment_on_est_tourne_ca_change_tout">confusion is not
possible</a>).</p>
<h2>Part 2</h2>
<p>In that part, we want to find the last standing cart, assuming that crashing carts are immediately
removed from the map. The code is actually very similar: instead of breaking the loop at the first
collision, we break it when there’s only one cart left on the map – we don’t forget te remove the
crashed carts!</p>
<pre><code>loop {
  // sort the cart by y component
  carts.sort_by(|a, b| a.pos.cmp(&amp;b.pos));

  for (ci, ck) in move_carts(&amp;map, &amp;mut carts) {
    carts = carts.into_iter().enumerate().filter(|&amp;(i, _)| i != ci &amp;&amp; i != ck).map(|(_, c)| c).collect();
  }

  if carts.len() == 1 {
    break;
  }
};

println!("Last standing cart: {:?} ", carts); // this contains only one cart
</code></pre>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-13/src/main.rs">Rust solution</a></p>
<h1>Day 14: Chocolate Charts</h1>
<p><a href="https://adventofcode.com/2018/day/14">Text</a></p>
<h2>Part 1</h2>
<p>Very similar to the double-ended queue puzzle as well, this one doesn’t actually require any
deletion, just indexing correctly into a growing buffer. Nothing really interesting to show about
this problem, except maybe the way recipes are created.</p>
<blockquote>
<p>To create new recipes, the two Elves combine their current recipes. This creates new recipes from
the digits of the sum of the current recipes’ scores. With the current recipes’ scores of 3 and 7,
their sum is 10, and so two new recipes would be created: the first with score 1 and the second
with score 0. If the current recipes’ scores were 2 and 3, the sum, 5, would only create one
recipe (with a score of 5) with its single digit.</p>
</blockquote>
<p>In order to implement that, I recognized that I will always create at least one recipe: <code>0 + 0 = 0</code>,
and as soon as I create two recipes, the other one will always be <code>1</code>, because the maximum value is
<code>9 + 9 = 18 (1 and 8)</code>. Here’s my code that gets those two recipe numbers:</p>
<pre><code>fn create_new_recipe(a: usize, b: usize) -&gt; (Option&lt;usize&gt;, usize) {
  let s = a + b;
  let x = s / 10;
  let y = s % 10;

  (if x == 1 { Some(x) } else { None }, y)
}
</code></pre>
<h2>Part 2</h2>
<p>Part 2 is really not interesting as it’s just using <code>ends_with</code> to find a suffix. I’ll let you read
the code if you’re interested.</p>
<p><a href="https://github.com/phaazon/advent-of-code-2k18/blob/master/day-14/src/main.rs">Rust solution</a></p>
<h1>Day 15: Beverage Bandits</h1>
<p><a href="https://adventofcode.com/2018/day/15">Text</a></p>
<h2>Part 1</h2>
<p>Aaaaaaaaaand this is the last puzzle I attempted. I actually decided not to finish it because it was
taking me time. I will tell you more about that in the conclusion.</p>
<p>The goal is to write a simulation of elves fighting goblins (or goblins fighting elves) and finding
paths in a map that has holes / mountains in it. So most of the code to write is about <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra</a> or
<a href="https://en.wikipedia.org/wiki/A*_search_algorithm">A*</a>. The puzzle seemed interesting but it was way too much for my spare time to spend on. I advise
you to have a look at the insanely long puzzle’s text – that will give you an idea of everything you
need to implement in order to get the your solution working.</p>
<h1>Conclusion</h1>
<p>Ah, my first Advent of Code. It was both interesting, exciting, frustrating and time-consuming. I
found several pros and drawbacks:</p>
<p>Pros, first:</p>
<ul>
<li>It’s all fun. Since I live in France, I couldn’t compete with people who get the puzzles
unlocked in the morning around 10:00 AM while they were unlocked around 5:00 AM in France – I
have a job, I have to, you know… SLEEP! So I just did it for fun.</li>
<li>Writing algorithms to solve math and operational problems is always interesting and makes it a
great training.</li>
<li>Comparing solutions on IRC was also pretty fun!</li>
</ul>
<p>And drawbacks:</p>
<ul>
<li>The fact west-europeans cannot compete (the timezone issue).</li>
<li>The difficulty of the puzzles is not correctly balanced. Some puzzle are insanely simple to
solve and some others take you hours (if not days if you don’t have a lot of spare time).
Sometimes I felt frustrated at this, because I’ve been lacking sleep time for weeks and had to
go to bed in order not to destroy my physical health. I’m curious to hear about other
west-europeans: how did you manage your time, social life and work life with AoC?</li>
<li>Some problems had a very long first part 2 and the second part was really stupid (read
<a href="#part-2-8">The Stars Align – Part 2</a>). I would have preferred to have more consistency or more
parts, for instance.</li>
</ul>
<p>My general feeling is that it was fun, but I think that I won’t do it next year, because I had to
put all my spare projects on hold for that. I didn’t learn anything new – all the algorithms had me
write algorithms I already knew, except maybe the partial dimension squared algorithm I “invented”:
someone told me that it’s very similar to a real and well-known algorithm! How funny is that! The
algorithm is <a href="https://en.wikipedia.org/wiki/Summed-area_table">Summed-area table</a> and my solution
is, indeed, very similar to it. But the thing is: I came up with the idea, and this is priceless for
training brains!</p>
<p>Now I’ll return to my graphics, Rust and over experiment projects of mine! I hope you liked that
article, it was a bit long (it took me almost two weeks to write!) but I felt I needed to make it.
To… well… scrap and forget about Advent of Code and all my spare time I didn’t use for my own
projects. :)</p>
<p>Keep the vibes – especially you, <a href="https://github.com/lqd">@lqd</a>.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 12 Jan 2019 00:20:00 GMT</pubDate></item><item><title>Let’s talk about C++ exceptions</title><link>https://strongly-typed-thoughts.net/blog/c++-exceptions</link><description><![CDATA[<blockquote>
<p>This article is a sequel to <a href="https://phaazon.net/blog/c++-constructors">Let’s talk about C++ constructors</a>.
Even though not required, reading that article first might give you more information on the motivation and
my state of mind regarding C++ and software development.</p>
</blockquote>
<p>This time, I want to talk about C++ exceptions. But talking about exceptions for the sake of talking about
them has very little value. Let’s instead talk about error handling.</p>
<h1>Infallible pure functions</h1>
<p>Imagine a function that takes some input, like a <code>String</code>, and outputs something else, like the length of the
string. Such a function is <em>pure</em>: its output depends <em>only</em> on its arguments. It cannot have any side-effect
nor return something else. It is also infallible as it can <em>always</em> return a valid length for all possible
values it can be called with. Imagine the following C++ implementation:</p>
<pre><code class="language-cpp">size_t get_len(std::string const &amp; s) {
  return s.length();
}
</code></pre>
<blockquote>
<p>Even though C++ doesn’t have the concept of purity, let’s assume we can express the concept, just because
it’s a powerful concept that we can apply to any language. The compiler will not know about it, but the way
we design things does.</p>
</blockquote>
<p>That function is pure and infallible. When you look at the signature, you see that it takes a read-only
reference on a <code>std::string</code> , and returns a <code>size_t</code>. And yes, I know about <code>noexcept</code>. However, this
specifier cannot be trusted. Consider:</p>
<pre><code class="language-cpp">#include &lt;string&gt;

void foo() {
  throw std::runtime_error("That should be seen by the compiler, right?");
}

size_t get_len(std::string const &amp; s) noexcept {
  foo();
  return s.length();
}

int main() {
  std::string foo = "foo";
  get_len(foo);
  return 0;
}
</code></pre>
<p>Compile with (I’m running Archlinux):</p>
<pre><code>g++ -std=c++2a -Wall -pedantic a.cpp
</code></pre>
<p>No compiler warning. Run it:</p>
<pre><code>terminate called after throwing an instance of 'std::runtime_error'
  what():  That should be seen by the compiler, right?
zsh: abort (core dumped)  ./a.out
</code></pre>
<p>What that little snippet tells us is that even though <code>get_len</code> is annotated with <code>noexcept</code>… it can still
throw. Not directly in its body, but functions it calls may throw. When I was introduced to that keyword,
years ago, I was suspicious. Since C++ will throw exceptions for — erm — exceptional errors, such as
<em>out of memory</em> errors, then… even a <code>noexcept</code> function can still throw errors. Then, because of that,
<code>noexcept</code> cannot propagate downwards in your call stack. If <code>A</code> is <code>noexcept</code> and <code>B</code> <em>may throw</em>, then
calling <code>B</code> from <code>A</code> is valid in C++.</p>
<p><code>noexcept</code> is just a documentation keyword and an opportunity for your compiler to optimize your code. Your
compiler can only emit warnings if you use the <code>throw</code> keyword directly in the body of the function that is
marked <code>noexcept</code>. Also, it’s important to notice that, given the team you work in, or the project you work
on, it’s possible to see the use of <code>noexcept</code>… like not at all. It’s all about convention; your compiler
will not enforce that… which is a problem to me. It’s a problem because more freedom means more opportunities
for people to make mistake. To forget about annotating a function with <code>noexcept</code>. Or, worse, it gives
opportunities to people who just don’t care and want to rush, making reviewing their code more challenging
than needed.</p>
<h1>Fallible functions</h1>
<p>In my previous article, I’ve been criticized for not explaining enough what I mean about <em>exceptional
errors</em> and that it was a highly subjective point. I’ll try to explain more in this section.</p>
<p>Imagine that you want to implement a function that will perform a lookup. If it finds the key you give
as argument, it will return the associated object. However, what to do when the key is not there? If you read
a bit my previous article, you know that I would use a sum type to encode the error in the type system. But
let’s do it <em>the C++ way</em>. Let’s use exceptions.</p>
<pre><code class="language-cpp">Object lookup(Key const &amp; key);
</code></pre>
<p>If you look at that signature, you’ll notice an important point. There is no error handling annotation. Most
of the time, people will follow some guidelines to put that information in the documentation directly.
However, several points:</p>
<ul>
<li>What happens if a teammate of yours or even yourself – after some weeks / months – forgets about that
documentation line?</li>
<li>Because you might call that function from <em>anywhere</em> — even in <code>noexcept</code> code, as demonstrated in the
previous section — how do you know, when reading the code, that a call to this function can throw?</li>
<li>The last point is especially true when refactoring. Imagine that this function belongs to a block of code
delimited with a <code>try catch</code> block. Do you assume the whole block as atomic? If so, do you lookup for the
documentation of <em>all of the functions</em> called in that block? What happens if you move that function out
of the block?</li>
</ul>
<p>Now, that just assumes a flat block. But it’s easy to guess that you will have to do that for the whole stack
of functions being called — i.e. as soon as you find a <code>noexcept</code> function in the stack… well nothing, you
have to go on, since a <code>throw</code> might be hidden in a function deeper in the stack.</p>
<p>Most of the time, the replies I get about that topic are, either:</p>
<ul>
<li><strong><em>“Just read the documentation”</em></strong>. That argument completely ignores the last part of my point above — i.e. do you
<em>really</em> read the documentation of <em>all</em> the functions being directly or transitively called in a
function? That seems insane. Also, you might argue that the documentation can say, at a function level <code>N</code>,
that there is a throw at level <code>N - k</code>. However, that seems like an impossible task to maintain. You might
forget to update the documentation if you stop throwing that exception or throw another object with a
different type, etc.</li>
<li><strong><em>“Use better names. If you use a function that, in its name, expresses the idea that it might fail, it’s
easier to refactor / use the function correctly”</em></strong>. That is true, even though you will <em>always</em> find someone
abusing it and using it without a <code>try catch</code> block.</li>
<li><strong><em>“We don’t care about handling errors: we will just handle them at the top-level of the program / function
call stack with a single try catch block”</em></strong>. That argument doesn’t survive five seconds as soon as you
talk about, for instance, serialization or map lookups.</li>
</ul>
<p>About the documentation and naming… it adds another problem: humans. We are fallible. You might work on a
project that doesn’t document correctly. Or that doesn’t even have proper convention. Or several ones. When
considering exceptions for error handling, I think it’s important to imagine what will happen in X months.
After the codebase has become complex, large, with a lot of edge cases and possible errors. Maintainability
should be a goal. No one enjoys having to read through the bodies of twenty functions to understand why their
program crashed or why the GUI displays a pop-up with the content of an exception.</p>
<p>The sooner you can see an error, the better. If that <em>sooner</em> can be “compile-time”… why would you want to
still push the error to the runtime? There are things I will never understand.</p>
<p>On the other side, consider sum types:</p>
<pre><code class="language-cpp">std::optional&lt;Object&gt; lookup(Key const &amp; key);
</code></pre>
<p>Even though it’s still pretty bad to me because of how <code>std::optional</code> is made (have a look at my previous
article), it has the advantage of being typed. No documentation is needed — but please do document to
explain what can fail though — and your compiler can safely prevent you from doing bad things. Of course,
this is limited by how you use the <code>std::optional</code>, as C++ doesn’t have pattern-matching. But I would like
to reply to an argument I hear every now and then: <strong>it’s not because a better solution is not perfect that
it should be discarded to stay on your legacy solution</strong>. Imagine that you have a tool <code>X</code> with several
issues, <code>{a, b, c, d}</code>. Now imagine we suggest to switch to a new tool, <code>Y</code>, with issues <code>{a, d}</code> only.
Yes, you still have two issues… but you have less. In the case of exceptions vs. a type-system, in the
case of C++, yes, you can still call <code>.value()</code> on an empty <code>std::optional</code> and crash your program. But
you don’t have the problem of hidden and untracked error handling. You can simply use exceptions for
exceptional cases. Those cases that are <em>not</em> function boundaries nor edge cases. And yes, I do think that
most of the standard C++ exceptions, such as <code>std::invalid_argument</code>, are to be completely avoided.</p>
<p>But here, exceptions have an advantage if we stop there: they provide an error description.</p>
<h1>Fully-typed failures</h1>
<p>Fixing that problem is pretty trivial with a strong type-system and
<a href="https://en.wikipedia.org/wiki/Algebraic_data_type">algebraic data types</a>. We want to use those to create
a <em>result</em> type, that can either be something, or a typed error, that would contain exactly the same
information you have in a regular exception.</p>
<p>C++ doesn’t really have that out of the box but it could be made, in the same way <code>std::variant</code> exists.
Imagine a hypothetical <code>std::either</code> type and let’s implement a function that parses something:</p>
<pre><code class="language-cpp">std::either&lt;Something, ParseError&gt; parse(std::string const &amp; input);
</code></pre>
<p>That function returns <em>either</em> a successful object (<code>Something</code>) or an error (<code>ParseError</code>).</p>
<p>With that signature, it’s clear that the function can fail with <code>ParseError</code>. The point is that the caller
<em>must</em> do something about it. If they don’t know what to do with it — imagine a parser combinator or some
code that doesn’t know how to handle a parse error and <em>requires</em> its caller to handle the error — then
the function needs to abort and propagate the error upwards. That looks like a bit like the interface you
have with exceptions… but here, the interface is visible at the type-level.</p>
<p>Obviously, you cannot use <code>throw</code> to propagate upwards. You need to use <code>return</code> from your function. With
either a macro or some language construct, C++ could make that propagation smoother, but currently, it
doesn’t have a proper way to do it. So we’d be left with macros only, or manual propagation. Since C++
doesn’t have pattern-matching nor exhaustive patterns, it would be pretty hard to implement that mechanism
in a complete sound way. As with <code>std::optional</code>, it’s not perfect, but it would be slightly better than
using opaque exceptions.</p>
<p>One final point. Sometimes, I wonder what it would be like to just give up on my ideas of using a strong
type system in C++. The language is using exceptions and people are used to it. That’s <em>the C++ way</em>. So…
why not changing the way exceptions work so that they’re not opaque, and propagate upwards? I remember
the <code>throw</code> specifier, used to state what a function might throw. But again, it’s not enforced by compiler.
Worse, it’s being deprecated in C++ 2020.</p>
<p>I voluntarily omitted any reference to either Haskell, Rust or any language like that so that people don’t
think I’m trying to compare C++ to another language. I’m having a look on C++ after almost two decades using
it and what else I’ve learned. The situation is, to me, frustrating. Because yes, whatever the good arguments
against exceptions, people still use them and error handling in C++ is still about exceptions. So you still
can have constructors that fail. You still depend a lot on documentation and your compiler cannot tell you
when something is not okay. We are all fallible, way more than we think.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 29 Feb 2020 20:20:00 GMT</pubDate></item><item><title>Even more hindsight on Vim, Helix and Kakoune</title><link>https://strongly-typed-thoughts.net/blog/more-hindsight-vim-helix-kakoune</link><description><![CDATA[<p>Oh my… 2023 has been such a trek so far… If you have missed my <a href="https://phaazon.net/blog/editors-in-2022">previous article</a>
about my thoughts about editors and development platforms, I think it’s probably the moment to have a look at it.</p>
<p>Today is the end of May 2023. <a href="https://helix-editor.com/">Helix</a> has been my primary editor for months now. I haven’t
come back to <a href="https://neovim.io/">Neovim</a>. In the previous article, I mentioned that I couldn’t give a fair opinion
about Helix because I had just started using it. Today, I think I have enough experience and usage (5 / 6 months) to
share what I think about Helix, and go a little bit further, especially regarding software development in general and,
of course, editing software.</p>
<p>However, before starting, I think I need to make a clear disclaimer. If you use Vim, Neovim, Emacs, VS Code, Sublime
Text, whatever, and you think that <em>“Everyone should just use whatever they want”</em>, then we agree and <strong>this is not
the point of this blog article</strong>. The goal is to discuss a little bit more than just speaking out obvious takes, but
please do not start waving off the topic because you think <em>“Everyone should use what they want”</em>. There is a place
for constructive debate even there. If you start arguing, then it means you want to debate, and then you need to be
ready to have someone with different arguments that will not necessarily go your way, nor your favorite editor.</p>
<p>Finally, if you think I haven’t used Vim / Neovim enough, just keep in mind that I have been using (notice the tense)
Vim (and by extension, Neovim) since I was 15, and I’m 31, so 16 years.</p>
<p>I have wrote that blog article already three times before deleting everything and starting over. I think I will make
another blog article about how I think about software, but here I want to stay focused on one topic: editors and
development environments.</p>
<h1>Helix</h1>
<p>From a vimmer perspective, Helix is weird. It reverses the verb and the motion (you don’t type <code>di(</code> to delete inside
parenthesis, but you type <code>mi(d</code> to first match the parenthesis and then delete). Then, you have the multi-selections
as a default way of doing things; Helix doesn’t really have the concept of a <em>cursor</em>, which is a design it gets from
<a href="https://kakoune.org/">Kakoune</a>, and I’ll explain what it means and implies later.</p>
<p>Some vimmers publicly talked about it. ThePrimeagen, for instance, made a Youtube video about it where he basically
just scratched the surface of the editor and <em>“Hell it’s not Vim so it’s not really good”</em>. He moved the video to
private then but I’m sure you can just look for Tweets. Many Vim afficionados react that way, which is not a very
serious way of trying out something new, especially if you happen to publicly talk about it to thousands of people. I
think it’s not fair to both the tool (here, Helix) and the people reading you, unless you are one of those Vim zealots
thinking you know everything better than everyone else and dismissing people’ points of views just because they are not
the same as yours.</p>
<p>Anyway, that reputation didn’t hold me from trying out, and the way I try software is simple: just give in and accept
to drop your habits and productivity. Of course, at work, I was still using Vim / Neovim, but switched to Helix on my
spare-time projects.</p>
<p>There are many aspects to talk about with Helix. The first one is that, contrary to what people who haven’t really and
seriously tried it, the difference with Vim is not only “reversed motion/verb and multi-cursors.” The first difference
is the <em>design</em> and the <em>direction</em>.</p>
<h2>Design and direction</h2>
<p>Helix is an editor that natively integrates many features that are most of the time plugins in others editors (even
in VS Code). The best examples here are <a href="https://tree-sitter.github.io/tree-sitter/">tree-sitter</a>, surrounding pairs (
being able to automatically surround regions of text with <code>(</code>, <code>[</code>, HTML tags, etc.), LSP UIs, fuzzy pickers, etc. This
is a <em>massive</em> and important difference because of two main things:</p>
<ol>
<li>Code maintenance.</li>
<li>User experience.</li>
</ol>
<p>About code maintenance, having all of those features natively integrated in the editor means that you are 100% sure that
if you get the editor to start, the features will work, and the editor developers will keep that updated within the
next iteration of the editor. For instance, having tree-sitter natively implemented (Rust) in Helix means that the
editor itself knows about tree-sitter and its grammars, highlights queries and features. The same thing goes for
surrounding-pairs or auto-pairs, for instance. If the team decides to change the way text is handled in buffers, then
the code for auto-pairs / surrounding-pairs <strong>will have to be updated for the editor to be releasable</strong>.</p>
<p>The user experience will then be better, because you get those nice features without having to start looking around
for a plugin doing it, with the problem of chosing the right one among a set of competing plugins. Plus the risk of
having your plugin break because it’s not written by the Helix team. Just install the software and start using those
features.</p>
<p>For now, Helix ships with a bunch of powerful features that makes it usable in a modern environment:</p>
<ul>
<li>tree-sitter support for semantic highligthing, semantic selection and navigation, etc. That also includes a native
support for getting grammars / queries from the CLI with <code>hx --grammar</code>.</li>
<li>LSP support, both in terms of features (go-to, references, implementations, incoming / outgoing calls, diagnostics,
inlay hints, etc. etc.) and in terms of UI. It’s the same UI for everyone.</li>
<li>DAP support for debugging (experimental at the time of writing).</li>
<li>Surrounding pairs.</li>
<li>Auto pairs.</li>
<li>Git integration (gutter diff on the right side of your buffer) and Git semantic object (go to next change, etc.).</li>
<li>Registers and user-defined registers (like in Vim), along with macros (but you won’t need them, trust me).</li>
<li>Native discoverability, including a command palette with <em>every possible available commands</em>, tagged with their
keybindings.</li>
<li>Many bundled themes (yes, <code>catppuccin</code> themes are there!).</li>
<li>Snappier than anything else.</li>
<li>Various integration with your system, including clipboards, external commands, etc.</li>
</ul>
<p>There is also one (major) thing I want to talk about and that deserves its own section: configuration.</p>
<h2>Configuration done right</h2>
<p>Helix treats configuration <em>the way it should be</em>: as data. Configuration in Helix is done in TOML. There is no
scripting implied, it’s only a rich object laid out as TOML sections. And this is a <em>joy</em> to use. For instance, this is
my current Helix configuration (excluding keys remapping, because my keyboard layout is bépo):</p>
<pre><code class="language-toml">theme = "catppuccin_macchiato"

[editor]
scroll-lines = 1
cursorline = true
auto-save = false
completion-trigger-len = 1
true-color = true
color-modes = true
auto-pairs = true
rulers = [120]
idle-timeout = 50

[editor.cursor-shape]
insert = "bar"
normal = "block"
select = "underline"

[editor.indent-guides]
render = true
character = "▏"

[editor.lsp]
display-messages = true
display-inlay-hints = true

[editor.statusline]
left = ["mode", "spinner", "file-name", "file-type", "total-line-numbers", "file-encoding"]
center = []
right = ["selections", "primary-selection-length", "position", "position-percentage", "spacer", "diagnostics", "workspace-diagnostics", "version-control"]
</code></pre>
<p>Notice the tree-sitter and LSP configuration. Yes. <em>None</em>.</p>
<p>This is so important to me. Because configuration is data, it is simple for Helix to expose it and present it to the
user by reading the TOML file without caring about having any side-effects. Helix has a command line (<code>:</code>) where you can
tweak those options dynamically. And you can dynamically reload the configuration as well.</p>
<h2>Multi-selection centric</h2>
<p>The major difference, even before the reversed motion/verb thing, is the fact that Helix <em>doesn’t really</em> have a cursor.
It has the concept of <em>selections</em>. A selection is made of two entities:</p>
<ul>
<li>An <em>anchor</em>.</li>
<li>A <em>cursor</em>.</li>
</ul>
<p>The cursor is the part of the selection that moves when you extend the selection. The anchor, as the name implies, is
the other part that stays where it is: it’s anchored. <strong>By default, you have only one selection</strong> and the anchor is
located at the same place as the cursor. It looks similar to any other editor. Things start to change when you begin
typing normal commands. Typing <code>l</code>, for instance, will move both the anchor and cursor to the right, making them a
single  visual entity. However, if you type <code>w</code>, the cursor will move to the end of the word while the anchor will move
to its beginning, visually selecting the word. If you type <code>W</code>, the anchor won’t move and only the cursor will move,
extending the selection. If you press <code>B</code>, it will move the cursor back one word, shrinking the selection. You can press
<code>&lt;a-;&gt;</code> to flip the anchor and the selection, which is useful when you want to extend on the left or on the right.</p>
<p>This concept of selection is really powerful because everything else is based on it. Pressing <code>J</code> will move the cursor
down one line, leaving the anchor on the current line, extending selected lines. Once something is selected, you can
operate on it, with <code>d</code>, <code>c</code>, <code>y</code>, <code>r</code>, etc. For instance, <code>wd</code> will select the word and delete it. <code>Jc</code> will extend the
selection with the next line and start changing. Selections in Helix are not just visual helps: they represent what
normal editing operations will work on, which is a great design, because you can extend, shrink and reason about them in
a much more seamless and natural way.</p>
<p>But it’s just starting. Remember earlier when I say that <strong>by default, you have only one selection</strong>? Well, you can have
many, and this is where Helix starts to really shine to me. The first way to create many selections is to press <code>C</code>. <code>C</code>
will duplicate your current selection on the next line. If you have the anchor at the same position as the cursor,
pressing <code>C</code> will make it like you have another cursor on the next line below. For instance, consider this text:</p>
<pre><code class="language-text">I love su|shi.
But I also love pizza.
</code></pre>
<p>The <code>|</code> is our cursor (but also anchor). If you press <code>C</code>, you will see something like this:</p>
<pre><code class="language-text">I love su|shi.
But I als|o love pizza.
</code></pre>
<p>But as I had mentioned, <code>C</code> duplicates <em>selections</em>. Let’s say you started like this, <code>&lt;</code> being the anchor and <code>|</code> the
cursor:</p>
<pre><code class="language-text">I love &lt;sus|hi.
But I also love pizza.
</code></pre>
<p>Pressing <code>C</code> now will do this:</p>
<pre><code class="language-text">I love &lt;sus|hi.
But I a&lt;lso| love pizza.
</code></pre>
<p>Once you have many selections, everything you type as normal commands will be applied to every selections. If I type
<code>f.</code>, it will set the anchor to the current cursor and extend the cursor to the next <code>.</code>, resulting in this:</p>
<pre><code class="language-text">I love sus&lt;hi|.
But I also&lt; love pizza|.
</code></pre>
<p>Press <code>&lt;a-;&gt;</code> to swap anchors and cursors:</p>
<pre><code class="language-text">I love sus|hi&lt;.
But I also| love pizza&lt;.
</code></pre>
<p>And then pressing <code>B</code> will do this:</p>
<pre><code class="language-text">I love |sushi&lt;.
But I |also love pizza&lt;.
</code></pre>
<blockquote>
<p>Erratum: <code>B</code> is not defined to this behavior in a vanilla Helix. I have remapped it to the Kakoune behavior. It
doesn’t change much to what I’m saying here, though.</p>
</blockquote>
<p>Multi-cursor is then not only a nice visual help, but also a completely new way of editing your buffers. Once you get
the hang of it, you don’t really think in terms of a single cursor but many selections, ranges, however you like to call
them.</p>
<h2>Getting more cursors</h2>
<p><code>C</code> is great, but this not something we usually use. Instead, we use features that don’t exist in Vim. I’m not entirely
sure how to call those, but I like to call them <em>selection generators</em>. They come in many different flavors, so I’ll
start with the easiest one and will finish with the most interesting and (maybe a bit?) obscure at first.</p>
<h3>Matching matching matching!</h3>
<p>The <code>m</code> key is Helix is a wonderful key. It’s the <em>match</em> key. It expects a motion and will change all your selections
to match the motion. For instance, <code>mia</code> <em>“matches inside arguments”</em> (tree-sitter). Imagine this context:</p>
<pre><code class="language-rust">fn foo(x: i32, y: |i32) {}

fn bar(a: String, |b: bool) {}
</code></pre>
<p>Press <code>mia</code> to get this:</p>
<pre><code class="language-rust">fn foo(x: i32, &lt;y: i32|) {}

fn bar(a: String, &lt;b: bool|) {}
</code></pre>
<p>Then, for instance, press <code>&lt;a-;&gt;</code> to flip the anchor and the cursor:</p>
<pre><code class="language-rust">fn foo(x: i32, |y: i32&lt;) {}

fn bar(a: String, |b: bool&lt;) {}
</code></pre>
<p><code>F,</code> to select the previous <code>,</code>:</p>
<pre><code class="language-rust">fn foo(x: i32|, y: i32&lt;) {}

fn bar(a: String|, b: bool&lt;) {}
</code></pre>
<blockquote>
<p>Erratum: same thing as with <code>B</code>; I have remapped it in my config. The default <code>B</code> doesn’t extend like this.</p>
</blockquote>
<p>And just press <code>d</code>:</p>
<pre><code class="language-rust">fn foo(x: i32) {}

fn bar(a: String) {}
</code></pre>
<p>It’s so logical, easy to think about and natural. Someting interesting to notice, too, is that contrary to Vim, which
has many keys doing mainly the same thing, making things weird and not really well designed. For instance, <code>vd</code> selects
the current character and delete it. <code>vc</code> deletes the current character and puts you into insert mode. <code>s</code> does exactly
the same. Why would you have a key in direct access doing something so specific? If you want to delete a word, you
either press <code>vwd</code>, or more simply in Vim, <code>dw</code>. All of that is already confusing, but it doesn’t end there. <code>x</code> deletes
the current character, but <code>x</code> is actually <em>cut</em>, so if you select a line with <code>V</code> and press <code>x</code>, it will cut the line.
Press <code>Vc</code> to change a line… or just <code>S</code>. What?</p>
<p>All those shortcuts feel like exceptions you have to learn, and it’s a good example of a flawed design. On the other
side, Helix (which is actually a Kakoune design it’s based on), have a single character to delete something: <code>d</code>. Since
the editor has multi-selections as a native editing entity, all of those situations will imply using the <code>d</code> key:</p>
<ul>
<li>Deleting the current character: <code>d</code>.</li>
<li>Deleting the next word: <code>wd</code>.</li>
<li>Deleting the current line: <code>xd</code> (<code>x</code> selects and extend the line).</li>
<li>Deleting delimiters but not the content: <code>md(</code>.</li>
<li>Etc. etc.</li>
</ul>
<p>That applies to everything.</p>
<h3>Selecting, splitting, keeping and removing</h3>
<p>The design of editing in Helix (Kakoune) is to be interactive and iterative. For instance, consider the following:</p>
<pre><code class="language-rust">  pub fn new(
    line_start: usize,
    col_start: usize,
    line_end: usize,
    col_end: usize,
    face: impl Into&lt;String&gt;,
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,|
      col_end,
      face: face.into(),
    }
  }
</code></pre>
<p>Let’s say we would like, to begin with, select very quickly every arguments type and switch them to <code>i32</code>. Many ways
of doing that, but let’s see one introducing a great concept: <em>selecting</em>. Selecting allows you to create new
selections that satisfy a regex. The default keybinding for that is <code>s</code> for… select (woah). <code>s</code> always applies to the
current Selections (notice the use of plural, it will be useful later). We then need to start selecting something. Here,
we can just press <code>mip</code> to select inside the paragraph, since our cursor is right after <code>line_end</code>:</p>
<pre><code class="language-rust">&lt; pub fn new(
    line_start: usize,
    col_start: usize,
    line_end: usize,
    col_end: usize,
    face: impl Into&lt;String&gt;,
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
      face: face.into(),
    }
  }|
</code></pre>
<p>We have the whole thing selected. Press <code>s</code> to start selecting with a regex. We want the arguments, so let’s select
everything with <code>:</code> and press <code>s:</code> and return:</p>
<pre><code class="language-rust">  pub fn new(
    line_start&lt;:| usize,
    col_start&lt;:| usize,
    line_end&lt;:| usize,
    col_end&lt;:| usize,
    face&lt;:| impl Into&lt;String&gt;,
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
      face&lt;:| face.into(),
    }
  } 
</code></pre>
<p>See how it created a bunch of selections for us. Also, notice that it selected <code>face: face.into()</code>, which is not
correct. We want to <em>remove</em> that selection. Again, several ways of doing it. Something to know is that, Helix (Kakoune)
has the concept of <em>primary</em> selection. This is basically the selection on which you are going to apply actions first,
like LSP hover, etc (it would be a madness to have LSP hover applies to all selections otherwise!). You can cycle the
primary selection with <code>(</code> and <code>)</code>. Once you reach the one you want, you can press <code>&lt;a-,&gt;</code> to just drop the selection.
However, we don’t want to cycle things. We want a faster way.</p>
<p>Let’s talk about <em>removing</em> selections. The default keybinding is <code>&lt;A-K&gt;</code>. However, here, our selections are all
about the same content (the <code>:</code>). As mentioned before, pressing <code>x</code> will select the current line of every selections:</p>
<pre><code class="language-rust">  pub fn new(
&lt;   line_start: usize,|
&lt;   col_start: usize,|
&lt;   line_end: usize,|
&lt;   col_end: usize,|
&lt;   face: impl Into&lt;String&gt;,|
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
&lt;     face: face.into(),|
    }
  } 
</code></pre>
<p>Let’s filter selection and remove the one matching a pattern. In our case, let’s remove selections with a
<code>(</code> in them: <code>&lt;A-K&gt;\(</code> and return:</p>
<pre><code class="language-rust">  pub fn new(
&lt;   line_start: usize,|
&lt;   col_start: usize,|
&lt;   line_end: usize,|
&lt;   col_end: usize,|
&lt;   face: impl Into&lt;String&gt;,|
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
      face: face.into(),
    }
  } 
</code></pre>
<p>Now press <code>_</code> to shrink the selections to trim leading and trailing whitespaces:</p>
<pre><code class="language-rust">  pub fn new(
    &lt;line_start: usize,|
    &lt;col_start: usize,|
    &lt;line_end: usize,|
    &lt;col_end: usize,|
    &lt;face: impl Into&lt;String&gt;,|
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
      face: face.into(),
    }
  } 
</code></pre>
<p>Imagine that we have changed our mind and now we actually want to change the <code>usize</code> to <code>i32</code>. We can use the <em>keep</em>
operator, which is bound to <code>K</code> by default. Press <code>Kusize</code> and return to get this:</p>
<pre><code class="language-rust">  pub fn new(
    &lt;line_start: usize,|
    &lt;col_start: usize,|
    &lt;line_end: usize,|
    &lt;col_end: usize,|
    face: impl Into&lt;String&gt;,
  ) -&gt; Self {
    Self {
      line_start,
      col_start,
      line_end,
      col_end,
      face: face.into(),
    }
  } 
</code></pre>
<p>Another possible way is to press <code>susize</code> to only select the <code>usize</code> directly, which might be wanted if you want to
change them quickly to <code>i32</code>, for instance.</p>
<p>The last operation that I want to mention is <em>splitting</em>. It’s introduced with <code>S</code> and will spawn several cursors
separated by a regex. For instance, consider:</p>
<pre><code class="language-rust">let |array = [1.0, 20.32, 3., 4.35];
</code></pre>
<p>Let’s say you’d like to select the numbers. With the methods described above, it’s probably challenging. With the
splitting command, it’s much easier. Put your cursor anywhere in the list and press <code>mi[</code> to select inside of it:</p>
<pre><code class="language-rust">let array = [&lt;1.0, 20.32, 3., 4.35|];
</code></pre>
<p>Then simply press <code>S,</code> to split the selection into selections separated by commas. You should end up with this:</p>
<pre><code class="language-rust">let array = [|1.0&gt;,| 20.32&gt;,| 3.&gt;,| 4.35&gt;];
</code></pre>
<p>Pressing <code>_</code> will shrink the selections to remove leading and trailing spaces:</p>
<pre><code class="language-rust">let array = [|1.0&gt;, |20.32&gt;, |3.&gt;, |4.35&gt;];
</code></pre>
<p>And here you have it! Now remember that you can combine all of this methods with semantic objects, like <code>mif</code> for
<em>inside functions</em>, <code>mat</code> for <em>around type</em>, <code>mia</code> for <em>inside arguments</em>, and many more. Recall that you can do that
<em>on each selection</em>, allowing for really powerful workflows.</p>
<h3>Do I really use all that at work / spare time projects?</h3>
<p><strong>Hell yes.</strong> It’s a muscular memory thing. For instance, I oftentimes the need to not only replace occurrences of
patterns, like <code>fooN</code> — with <code>N</code> being a number — into <code>logger.fooN</code>, <strong>but I often need to change the structure around
those occurrences.</strong> And here, Helix really stands out. In Vim, you’d have to use a super ugly regex, completely blind,
and eventually a macro. The interactive and iterative approach of Helix is so much more powerful to me. For instance,
for the case described above: <code>%</code> to select the whole buffer, <code>sfoo.</code> to select <code>foo</code> with a single character
afterwards, then return, <code>clogger.foo</code> to replace with <code>logger.foo</code>, and still in insert mode, <code>&lt;C-r&gt;"</code> to paste what
was yanked by the <code>c</code> operation. Here, the default register, <code>"</code>, makes a <em>lot</em> of sense, because this register is local
to each selection, making this replace operation trivial and interactive.</p>
<p>Another example is something like this:</p>
<pre><code class="language-rust">const COLORS: [Color; 3] = [
  Color {
    r: 255,
    g: 0,
    b: 0,
  },
  Color {
    r: 0,
    g: 255,
    b: 0,
  },
  Color {
    r: 0,
    g: 0,
    b: 255,
  },
];
</code></pre>
<p>Imagine that you want to change every <code>Color</code> constructor to a function call, that does something like
<code>Color::rgb(r, g, b)</code>. Doing that interactively and iteratively in Helix is so easy. I’d put my cursor anywhere in that
block, press <code>mi[</code> to select everything inside <code>[]</code>, then <code>sColor&lt;cr&gt;</code> to create three cursors on the <code>Cursor</code>, and from
that moment, it’s just ping-pong-ing between normal mode and insert mode like you would do with a single selection. You
will be using <code>f</code>, <code>t</code>, <code>miw</code> etc. select things but the idea is the same, and the three occurrences will be updated at
once.</p>
<h2>A simpler editor overall</h2>
<p>Contrary to other famous editors and IDEs, Helix is not supposed to be extendable; it doesn’t try to solve more
problems than it should (and you will see in the Kakoune section that we can even push that to another extreme).
Something like Neovim is a bit of a disguised IDE. Yes, a vanilla Neovim with no plugins and no configuration is just
a very basic (and I would dare say <em>featureless</em>) editor. It won’t have LSP working. It won’t have tree-sitter working
either. Nothing for git integrated. Nothing for delimiters, nothing for pickers, nothing for any modern development. The
power of something like Emacs, Neovim etc. is to build on <em>extensibility</em>.</p>
<p>I used to enjoy that, until I came to the realization that, <em>perhaps</em>, it would be great to put things into perspective:
is extensibility something we actually want? What do we try to solve with it? Well, we extend tools to add new features
and new behaviors. We extend things so that the native / core tool ships with minimal features but doesn’t prevent
people from adding specific and customized capabilities.</p>
<h3>Extensibility</h3>
<p>But is extensibility the only way to achieve that goal? The thing with extensibility is that:</p>
<ol>
<li>You have to build in advance for it. You cannot ship an editor without any extensibility support and expect it to
be an emergent feature. You have to add a basic support for it, whether it’s a plugin system, a
dynamic / relocatable system (<code>.so</code> / <code>.dylib</code> / <code>.dll</code>), a scripting language, a JIT, etc.</li>
<li>Extensibility is always walled.</li>
</ol>
<p>The last point is important. Extending a software <strong>requires the environment to adapt to the specifities of what you’re
extending</strong>, and that will require the environment to know about the specifities. Here, the environment could be
anything <strong>outside of Neovim</strong>. What it means is that, you’re not going to use external tools, but you are going to use
the interfaces, scripting languages, DSLs etc. of the tool you want to extend.</p>
<p>For instance, people might argue that extending Neovim is great because it only requires learning Lua, which is not
specific to Neovim, but that it is not actually true nor acurate. You have to learn <em>“Neovim Lua”</em>, which is basically
its own beast. It’s like the standard library, but for Neovim. It will provide you with APIs you can use — and only
use — to add new features to Neovim.</p>
<p>The same argument can be made to <em>any extensible</em> editor. VS Code, Emacs, etc.</p>
<h3>Composability</h3>
<p>Another way to add features and behaviors is to add them <em>externally</em>, by leveraging the tools themselves and compose
them instead of pushing more features into them. That vision is not very popular and famous and I’m not entirely sure
why. For instance, Vim has <a href="https://github.com/tpope/vim-fugitive">vim-fugitive</a>, a Git client for Vim / Neovim. It has
2k commits, between 8k-9k lines of code… and can be used only in Vim and Neovim. Yes, it extends and adds features
<em>inside</em> those editors, but still. If at some point you decide to switch to another editor, you can forget about this
plugin. This is even worse with something like <a href="https://magit.vc/">magit</a>, which is the best Git client I haver ever
used. Yet I don’t use it anymore, because it’s an Emacs plugin. What a shame.</p>
<p>Now repeat that reasoning for all the plugins you use. That has led some people to just install as few plugins as
possible and switch to composability.</p>
<p>Composability is the same concept as in code. You have two systems <code>A</code> and <code>B</code> doing <code>thingA</code> and <code>thingB</code>, and you
write some <em>piping</em> glue code / script to connect both. People use many different languages to glue things together.
Among the most famous approaches:</p>
<ul>
<li>Python.</li>
<li>Perl, especially when dealing with filtering text.</li>
<li>The shell, and especially any POSIX.2 compliant shell (no, don’t start talking about <code>fish</code>).</li>
</ul>
<p>I have shell functions that I source when I start my shell; for instance, one called <code>p</code>, to switch to my spare-time
projects:</p>
<pre><code class="language-sh">p () {
  proj_dir=${PROJ_DIR:-~/dev}
  project=$(ls $proj_dir | sk --prompt "Switch to project: ")
  [ -n "$project" ] &amp;&amp; cd $proj_dir/$project
}
</code></pre>
<p>This is a great example of composability, which composes the content of a project directory (the <code>$PROJ_DIR</code> environment
variable, or <code>~/dev</code> by default), with the <code>sk</code> fuzzy finder, and then changes the directory to whatever the user has
picked. I use that script all the time to quickly move to my various projects.</p>
<p>Notice that, <code>sk</code>, <code>fzf</code>, etc. already are tools that implement fuzzy searching for arbitrary inputs. Tools such as
<code>find</code> or <code>fd</code>, who are so far in my experience the fastest programs to look for stuff, can be composed with shell
pipes as well and integrated into your work environment.</p>
<p>Then the question starts to appear: why do editors / plugins re-implement all of that to have it inside the editor? It’s
pretty apparent in the Neovim community, but it often ends up with abandonware plugins, and harder to maintain
editors.</p>
<p>While I was wondering about all that, I was getting pretty productive with Helix, enjoying its simpler design, data
configuration, better editing features and overall way more stable experience. I remembered that most of the Helix
design came from Kakoune. And I started to think about one (not so) crazy idea: should I have a look at Kakoune?</p>
<p>And let’s enter Kakoune.</p>
<h1>Kakoune</h1>
<p>As mentioned above, Helix is <em>heavily</em> inspired by Kakoune. The main difference, from the surface, with distance, is
that Helix comes with more bundled features, like LSP, tree-sitter, pickers, etc. However, there are more (drastic)
differences that I need to talk about.</p>
<p>The first thing is, again, the design. And for that, I really need to quote a part from the
<a href="https://github.com/mawww/kakoune/blob/master/doc/design.asciidoc">excellent design doc</a> written by
<a href="https://github.com/mawww">@mawww</a>. The part that is the most interesting to me is, obviously, <em>composability</em>.</p>
<blockquote>
<p>Being limited in scope to code editing should not isolate Kakoune from its environment. On the contrary, Kakoune is
expected to run on a Unix-like system alongside a lot of text-based tools, and should make it easy to interact with
these tools.</p>
<p>For example, sorting lines should be done using the Unix sort command, not with an internal implementation. Kakoune
should make it easy to do that, hence the <code>|</code> command for piping selected text through a filter.
The modern Unix environment is not limited to text filters. Most people use a graphical interface nowadays, and
Kakoune should be able to take advantage of that without hindering text mode support. For example, Kakoune enables
multiple windows by supporting many clients on the same editing session, not by reimplementing tiling and tabbing.
Those responsibilities are left to the system window manager.</p>
</blockquote>
<p>And this is one of the most important things about Kakoune to me. First, it goes into the direction I’ve been wanting
for years (I’ll let you read my previous blog articles about editors and production environments; I’m basically saying
the same thing). And second, it ensures that the native editor remains small and true to its scope, ensuring an easier
maintenance, hence less bugs and more stable. Also, it doesn’t blindly ignore what everyone else is doing.</p>
<p>Let’s start with an example. Yes, Kakoune doesn’t have a fuzzy picker to pick your files. However, as mentioned above,
it composes well with its environment. It does that via different mechanisms (shell blocks, FIFOs, UNIX sockets, etc.).
Here, we can just use whatever we like to get a list of files, and let Kakoune ask the user which files to open. We then
simply use the selected value and open it. In order to do that, you need to read the design doc to understand a couple
of other things, such as the section about <em>interactive use and scripting</em>. Quoting:</p>
<blockquote>
<p>As an effect of both Orthogonality and Simplicity, normal mode is not a layer of keys bound to a text editing language
layer. Normal mode <strong>is</strong> the text editing language.</p>
</blockquote>
<p>Typing normal commands in a <code>.kak</code> file is then the way to go. And then, coming back to the fuzzy picker example, here
are three commands defined in my <code>kakrc</code>:</p>
<pre><code>## Some pickers
define-command -hidden open_buffer_picker %{
  prompt buffer: -menu -buffer-completion %{
    buffer %val{text}
  }
}

define-command -hidden open_file_picker %{
  prompt file: -menu -shell-script-candidates 'fd --type=file' %{
    edit -existing %val{text}
  }
}

define-command -hidden open_rg_picker %{
  prompt search: %{
    prompt refine: -menu -shell-script-candidates "rg -in '%val{text}'" %{
      eval "edit -existing  %sh{(cut -d ' ' -f 1 | tr ':' ' ' ) &lt;&lt;&lt; $kak_text}"
    }
  }
}
</code></pre>
<p>As you can see, it’s just about composing known and well written tools together. Another example? Alright. Kakoune
doesn’t have splits, but I still want them. Let’s go:</p>
<pre><code>## kitty integration
define-command -hidden kitty-split -params 1 -docstring 'split the current window according to the param (vsplit / hsplit)' %sh{
  kitty @ launch --no-response --location $1 kak -c $kak_session
}

## zellij integration
define-command -hidden zellij-split -params 1 -docstring 'split (down / right)' %sh{
  zellij action new-pane -cd $1 -- kak -c $kak_session
}

define-command -hidden zellij-move-pane -params 1 -docstring 'move to pane' %sh{
  zellij action move-focus $1
}

## tmux integration
define-command tmux-split -params 1 -docstring 'split (down / right)' %sh{
  tmux split-window $1 kak -c $kak_session
}

define-command tmux-select-pane -params 1 -docstring 'select pane' %sh{
  tmux select-pane $1
}
</code></pre>
<p>The design is not extensible: it’s composable, and all in all, it makes so much more sense to me.</p>
<h2>Kakoune vs. the rest</h2>
<p>If you are used to Helix, then Kakoune with a bit of configuration will feel very similar to Helix. Of course, you will
have to look around for LSP and tree-sitter support. The way we do that is by adding external processes to interact with
Kakoune servers / clients via UNIX sockets, FIFO pipes, etc.. Kakoune doesn’t know anything about LSP or tree-sitter,
but you can write a binary in any language you want and send remote commands to control the behavior of Kakoune.</p>
<ul>
<li>For LSP: <a href="https://github.com/kak-lsp/kak-lsp">kak-lsp/kak-lsp</a></li>
<li>For tree-sitter: <a href="https://github.com/phaazon/kak-tree-sitter">phaazon/kak-tree-sitter</a> (I’m stil working on it and it
lacks documentation but is usable).</li>
</ul>
<p>The interesting aspect with those tools is that, in <em>theory</em>, we could adapt them to make them editor independent. If
more editors adopted the strategy of Kakoune (composing via the shell), we wouldn’t even have to write other binaries to
add LSP / tree-sitter support, which is an interesting aspect.</p>
<p>I plan on writing a blog article detailing the design of <code>kak-tree-sitter</code>, because I think it’s a good source of
knowledge regarding UNIX and tree-sitter.</p>
<p>Besides that, Kakoune is way more mature than Helix, in the sense that it has some specificities to some edge cases with
multi-selection features (such as, what happens when you have multiple cursors inside text looking like function
argument lists, and you type <code>mia</code> to select them, but some selections are actually not arguments? Kakoune will remove
the mismatched selections, which is what we would expect, while Helix…… erm it’s complicated! but currently, it will
keep the selections around, which is confusing and dangerous).</p>
<p>Kakoune has a <em>wonderful</em> feature called <strong>marks</strong>. Marks are different from what you have in Vim. They use a specific
register to record the current selections and eventually restore them later, supporting merging selections and editing
commands. An example that I love doing; imagine the following snippet:</p>
<pre><code class="language-markdown">Thank you to [@NAME](https://github.com/NAME) for their contribution.
</code></pre>
<p>Let’s say you want to have a cursor on each <code>NAME</code>. Easy, with <code>s</code>. You just select the whole thing (you can select the
whole line with <code>x</code> for instance), then <code>sNAME</code>, return, then <code>c</code> to start changing with whatever you want.</p>
<p>Now, imagine this instead:</p>
<pre><code class="language-markdown">Thank you to [@](https://github.com/) for their contribution.
</code></pre>
<p>How do you insert at the same time at the right of <code>@</code> and before the <code>)</code>? Getting two selections will be hard (or you
will end up writing crazy regexes; remember, we are not using Vim anymore!). A super easy way to do it is to move your
selection to <code>@</code>:</p>
<pre><code class="language-markdown">Thank you to [@|](https://github.com/) for their contribution.
</code></pre>
<p>Press <code>Z</code> to register the current selection (you have only one). Then move the cursor to the <code>)</code>:</p>
<pre><code class="language-markdown">Thank you to [@](https://github.com/|) for their contribution.
</code></pre>
<p>And then press <code>&lt;a-z&gt;a</code> to merge the current selection to the one(s) already stored. You end up with this:</p>
<pre><code class="language-markdown">Thank you to [@|](https://github.com/|) for their contribution.
</code></pre>
<p>Two cursors at the right places! This is extremely powerful and a feature that should arrive in Helix, but not sure
exactly when.</p>
<p>A pretty other important thing to say about Kakoune is that it has a server/client design that allows to share session
contexts. That is the main mechanisms used to implement native splits (via Kitty, tmux, whatever), but also many other
features, such as project isolation, viewing the same buffers with different highlighters, etc. etc.</p>
<h2>…but it’s not perfect</h2>
<p>There is one important thing I need to mention. I have been playing with Kakoune for a while now, and I have been
working on <code>kak-tree-sitter</code> for almost as long as I’ve been working Kakoune (couple of months). And there is one issue
with the UNIX approach.</p>
<p>See, tree-sitter, LSP, DAP, git gutters, etc. All those things are pretty <em>fundamental</em> to a modern text editor.
Externalizing them (Kakoune) is an interesting take, but I’m not entirely sure Kakoune is still doing it completely
correctly.</p>
<p>The main problem is <em>social intelligence</em>. Because all tooling is now externalized, many people can come up with
their own efforts. For instance, <code>kak-lsp</code> and <code>kak-tree-sitter</code> are completely separate projects, and they should
remain that way (for many reasons; scopes, maintenance, dependencies, etc.). However, in order to operate on the editor
contents, both programs must interact with the editor. That implies:</p>
<ul>
<li>Retrieving buffer contents.</li>
<li>Performing some actions that can mutate (and overlap / hijack) each other.</li>
<li>Insert hooks that might be incompatible between them.</li>
</ul>
<p>This problem is important, because dumping the whole content of a big buffer to an external process is one thing, doing
it for every different external processes is a massive overhead. Because I have read a bit the source code of <code>kak-lsp</code>,
I know that we (<code>kak-tree-sitter</code>) are doing similar things: we do use FIFOs to write buffer content and communicate
with our servers / daemons without going through the disk. But we are doing it that <em>twice</em>. And it’s just two projects;
any dissociate projects needing to access the buffer content will probably perform similar things.</p>
<p>That is a massive problem to me and I’m not sure how I feel about it. I’m not against sending the content of a buffer
via a FIFO to an externalized program — I actually think it’s a pretty good design —, but doing it for every
integration… I’m not exactly sure what would be the best solution, but maybe something that would snapshot a buffer
inside some POSIX shared memory (with mutable lock access if needed) could be one way to go. Honestely, I am not sure.</p>
<p>All of that to say that, the take of Helix is pretty good here, because all of those UNIX problems are not there in that
editor: everything runs in the same process, inside the same memory region. I will come back to this problem with my
next article on <code>kak-tree-sitter</code> and its design.</p>
<h1>Conclusion</h1>
<p>Today, I’m mainly using both Kakoune and Helix. Helix still has some bugs, even with tree-sitter (which my daemon
doesn’t have, funnily!), so I sometimes use one or the other tool.</p>
<p>I have learned so many things lately, with both Helix and Kakoune (especially Kakoune, it made me love UNIX even more).
All of that echoes the disclaimer I made earlier: yes, Vim and Neovim are good, but Kakoune and Helix are so much better
to me. Better designs, better editing experiences, largely snappier, more confidence in the direction of the
native code (because a much, much smaller codebase). Helix is written in Rust, Kakoune in C++, but what matters is the
actual design. To me, Kakoune is by far the best designed software I have ever seen, and for that, I really admire the
work of <a href="https://github.com/mawww">@mawww</a> and everyone else involved in the project. My contribution to the Kakoune
world with <a href="https://github.com/phaazon/kak-tree-sitter">kak-tree-sitter</a> is, I hope, something that will help and drive
more people in. I will write another blog article about that, with the pros., cons. and tradeoffs. of composability in
editors.</p>
<p>In the meantime, have fun and use the editor you love, but remember to have a look around and stay open to change! Keep
the vibes!</p>
<blockquote>
<p>I would like to thank <a href="https://github.com/taupiqueur/">@Taupiqueur</a>, who played an important role into making me
undersand Kakoune and eventually fall in love with — the editor, I mean! :)</p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 24 May 2023 11:50:00 GMT</pubDate></item><item><title>Why has type aliasing almost killed me</title><link>https://strongly-typed-thoughts.net/blog/type_aliasing</link><description><![CDATA[<h1>Type aliasing</h1>
<p>Type aliasing is something that’s been included in most languages for a while
now. Basically, if you have a type <code>Foo</code>, aliasing <code>Bar</code> to <code>Foo</code> means that
you introduce a new <em>symbol</em> <code>Bar</code> that refers to <code>Foo</code>. It’s not a new type,
it’s just an alias, a <em>binding</em>.</p>
<p>In <strong>Haskell</strong>, we do aliasing with the keyword <code>type</code>, which is a bit stupid
since we won’t necessarily alias a type (we could alias a function, a typeclass
constraint, and so on and so forth).</p>
<p>Aliasing is great and useful in many ways. For instance, there’s a Haskell
package I love called <a href="https://hackage.haskell.org/package/linear">linear</a>. That
package is used to deal with linear algebra, and you can find common math types
in there among vectors (<code>Vec</code>) and low-algebra vectors (<code>V2</code>, <code>V3</code>, <code>V4</code>). There
are the same things for matrices (<code>M22</code> for <em>2x2</em> matrices, <code>M33</code>, <code>M44</code>).</p>
<p>If you come up with using <em>4x4</em> floating matrices a lot, you might end up with a
lot of <code>M44 Float</code> everywhere in your code. So that would be sane aliasing it.
Like so:</p>
<pre><code class="language-haskell">type V4F = V4 Float
</code></pre>
<p>This is a perfect aliasing use to me as <code>V4F</code> is straight-forward for
<em>graphics</em> people. Furthermore, <code>linear</code> is pretty a standard now in Haskell
linear algebra application, so <code>V4</code> should be familiar to you.</p>
<p>However, there is a drawback with type aliasing.</p>
<h1>Why has type aliasing almost killed me</h1>
<p>I spent more than a day crawling for a bug. I was refactoring code, a lot of
code. Nothing fancy though, just some code cleaning. Nevertheless, I created a
brand new git branch to make my cleaning. On the initial branch, the application
behavior was normal. After having cleaned the code, I got a black screen.</p>
<p>After a long investigation, I found out that the bug was due to a mismatch
between two matrices – i.e. <code>M44 Float</code>. One matrix is used as a camera
projection and the other one is used as a light projection. In term of code,
both matrices are placed in a type. I simplified (a lot), but this is the idea:</p>
<pre><code class="language-haskell">data Foo = Foo {
    -- […]
  , cameraProjection :: M44 Float
  , lightProjection :: M44 Float
  }
</code></pre>
<p>What makes those matrices different in term of compilation? Nothing.
<code>M44 Float</code> and <code>M44 Float</code> are the same type. Swapping matrices is then
silently compiled, since it’s not an error to the compiler. And such a swapping
result in a terrible and hard to find bug.</p>
<blockquote>
<p><em>Yeah, so we could have simply aliased those matrices to two different names!</em></p>
</blockquote>
<p>Remember what I said earlier. Aliased types are not new types. They’re bindings
in the type system:</p>
<pre><code class="language-haskell">type CameraProjection = M44 Float
type LightProjection = M44 Float
</code></pre>
<p>Here, both <code>CameraProjection</code> and <code>LightProjection</code> are the same type. If a
function expects a <code>CameraProjection</code>, it actually expects a <code>M44 Float</code>. You
could then pass a value of type <code>M44 Float</code> or even <code>LightProjection</code>, that
would be legit.</p>
<blockquote>
<p><em>Is there a solution yet?</em></p>
</blockquote>
<p>Yes, there is. <strong>Haskell</strong> has several keywords to create types:</p>
<ul>
<li><code>type</code>, which is, as we’ve already seen, a way to alias types;</li>
<li><code>data</code>, which is used to create data types;</li>
<li><code>newtype</code>, which is used as a type wrapper to introduce a data type.</li>
</ul>
<p><code>newtype</code> is the most important keyword for us here. Basically, it has the same
semantic as <code>data</code> except that <code>newtype</code> introduces a type with a single
<em>field</em>; it can’t have none nor several. <code>newtype</code> is often used to express the
<em>same thing</em> that the inner type, but adding / removing some properties.</p>
<p>For instance, <code>Bool</code> can’t be an instance of <code>Monoid</code> since we could define a
lot of instances. <code>mempty</code> could be <code>True</code> if we consider the monoid of
booleans with <strong>AND</strong> but it would be <code>False</code> if we consider the monoid of
booleans with <strong>OR</strong>. Well, we could write those, actually:</p>
<pre><code class="language-haskell">newtype And = And { getAnd :: Bool } deriving WhateverYouWant
newtype Or = Or { getOr :: Bool } deriving WhateverYouWant

instance Monoid And where
  mempty = True
  And a `mappend` And b = And (a &amp;&amp; b)

instance Monoid Or where
  mempty = False
  Or a `mappend` Or b = Or (a || b)
</code></pre>
<p>Those two types are defined in <code>Data.Monoid</code> as <code>All</code> and <code>Any</code>.</p>
<p>Back to our problem. We could use <code>newtype</code> to add <code>M44 Float</code> the correct
semantic, and disable us to mix them:</p>
<pre><code class="language-haskell">newtype CameraProjection = CameraProjection { getCameraProjection :: M44 Float }
newtype LightProjection = LightProjection { getLightProjection :: M44 Float }
</code></pre>
<p>And here, <code>CameraProjection</code> ≠ <code>LightProjection</code>. If a function expects a
<code>CameraProjection</code>, you can’t pass anything else than a <code>CameraProjection</code>.</p>
<p>However, be careful. You’ll have to wrap a <code>M44 Float</code> into your
<code>CameraProjection</code>. Such a function, like the <code>CameraProjection</code> constructor, is
not semantic safe since you could still pass a light representation.
Nevertheless, it’s better than the initial design since once wrapped, you can’t
make confusions anymore.</p>
<h1>Final words</h1>
<p>To sum up, use aliasing when it’s handy but don’t use it if it could break
semantics or add confusion. If you have several types that use the same
type (e.g. colors and positions could use the same <code>V3 Float</code> type), <strong>don’t
use aliases</strong>! Create proper types to prevent confusion later on. Your compiler
will reward you, then you don’t get nasty runtime bugs ;).</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 07 Jan 2015 00:00:00 GMT</pubDate></item><item><title>Zig, linear types and PwTP</title><link>https://strongly-typed-thoughts.net/blog/zig-linear-pwtp</link><description><![CDATA[<p>Lately, I have decided to jump in and learn Zig. Learn
<a href="https://ziglang.org/documentation/master/">the whole thing</a>. After a weekend of
reading the whole reference, following some guides, doing [ziglings], and even
started migrating some — WIP — Rust projects of mine just to learn correctly, I
think I’m now ready to start talking about Zig. More specifically, I want to
talk about something specific to the <em>safety</em> of Zig.</p>
<blockquote>
<p>Disclaimer: this article expects some sort of fluency with Zig to read
smoothly. However, the examples and snippets will be explained for the
uninitiated ones.</p>
</blockquote>
<h1>Problem</h1>
<p>Zig has many interesting features and safeties. I won’t cover them up here, it’s
not the aim of the article, and I’m sure you’ll find that information on your
own — there are plenty of articles talking about how well designed the language
is.</p>
<p>No, instead, I want to talk about memory safety and static checks. And actually,
it’s even more than <em>memory safety</em>. It’s more about <em>correctness</em>. Instead of
taking the — traditional and boring — example of using an allocator (people
using Rust, Python, C++, Java, etc. wouldn’t get the problem because they do not
have a direct access to allocators — I want to show that the problem exists
not only for memory allocations, but for a much more general category:
resources. So let’s take an example:</p>
<pre><code class="language-rust">const Storage = struct {
    // … hidden
    const Self = @This();

    pub fn create_resource(self: *Self) StorageError!Handle {
      // … hidden
    }

    pub fn destroy_resource(self: *Self, handle: Handle) StorageError!void {
      // … hidden
    }
};
</code></pre>
<p>Okay, so that is basically a <code>Storage</code> type with <code>create_resource</code> and
<code>destroy_resource</code> methods. The former creates a new resource in the <code>Storage</code>
and returns a <code>Handle</code> to it. The second one destroys a resource in a <code>Storage</code>
providing its <code>Handle</code>.</p>
<p>You would use it like so:</p>
<pre><code class="language-rust">// …
const h = try storage.create_resource();

// do something with the storage …

// … then later, at some point:
try storage.destroy_resource(h);
</code></pre>
<blockquote>
<p>It’s not important to use <code>defer</code> here; the problem would be the same. Also, <code>defer</code>
will not work on fallible methods.</p>
</blockquote>
<p>Now, what happens if you forget to call <code>destroy_resource</code>? Well, your code
still compiles, while it’s not <em>correct</em>. What happens if you call
<code>destroy_resource</code> twice with the same pointer? Probably something bad, but it
still compiles.</p>
<p>The rest of the article assumes that we want to solve this problem at
compile-time, because yes, Zig will catch the problem at runtime, probably
(eventually?) resulting in a resource leak.</p>
<h1>Some ideas</h1>
<p>To solve this problem, we historically used — mainly — two approaches:</p>
<ul>
<li>Introduce a garbage collected form of resource management. Every function that
may create a resource — allocate memory; open a file; etc. — may wrap the
pointer in a special pointer that the GC will track. When no more live object
exists, the resource is eventually destroyed.</li>
<li>Introduce destructors. This is the direction Rust took, for instance. The
destructor allows you to run arbitrary code when a value is <em>dropped</em>.</li>
</ul>
<p>The latter option is interesting, because it’s actually composed of two
different features:</p>
<ul>
<li>The drop part, that is automatically inserted by the compiler when something
goes out of scope.</li>
<li>Linearity — the ability to recognize that a value should be used only once.
Indeed, without it, you cannot know where and when place the drop call.</li>
</ul>
<p>If we take a bit of hindsight, the drop part is actually not that good, because
it hides some implicitly called code from the programmer. It makes it harder
to think in terms of when some code will be called — is this value of a type
that implements a drop interface? if so, where does it go out of scope? etc.</p>
<p>Moreover, the <em>real problem</em> is not really to automatically call the cleanup
code. The real problem is to be sure that the cleanup code is called. Eh, the
nuance is subtle, but I think it makes all the difference.</p>
<h1>Ensuring instead of enforcing</h1>
<p>What we really want to do is to be sure that the user call <code>destroy_resource</code>.
We should not need destructors for that. Let’s continue with our example:</p>
<pre><code class="language-rust">const handle = try storage.create_resource();
</code></pre>
<p>The only way we can emit a compiler error here — because <code>handle</code> is not
destroyed! — is to recognize that <code>handle</code> has some special – compilation-only —
properties that need to be passed to the <code>destroy_resource</code>. <a href="https://ziglang.org/documentation/master/">ATS</a> has some
answer to this — careful if you click the link; it’s a pretty complex language!
That answer is called <em>Programing with Theorem-Proving</em> (PwTP). My idea here is
not to take <em>all</em> of ATS but just the part that is interesting to our problem:
proofs.</p>
<p>A proof is a compile-time (<code>comptime</code>) value that is returned by a function,
typically during resource creation (allocation, opening files, etc.) and that is
<em>linear</em>. A linear value is a value that must be consumed at least once and at
most once — it must be consumed exactly once! Consider this:</p>
<pre><code class="language-rust">fn do_something(x: Foo) void {
    // …
}

const x = try alloc.create(Foo);
do_something(x);
</code></pre>
<p>If this program states that <code>x</code> is linear, after we pass the value to
<code>do_something</code>, <code>x</code> is not available anymore. If you have done some Rust, you
should be familiar with the concept. In Rust, we say that <code>x</code> was moved into the
function.</p>
<h2>Proof marking</h2>
<p>Back to the proof thing. We can simplify ATS proofs and create a simpler proof
system by doing the following:</p>
<ul>
<li>Define a proof as a set of tying variables, and a tag.</li>
<li>Allow a function to return a value which type can be <em>marked</em> with a proof.</li>
<li>A value which type is proof-marked becomes linear.</li>
<li>Only a function taking as argument the same type with the exact same proof
marked can consume the argument.</li>
</ul>
<p>I know, that’s a lot of new ideas. I’ll introduce the syntax I have on my
mind to explain a bit more what I think. Let’s start with a regular
alloc/destroy wrapper and then enhance it:</p>
<pre><code class="language-rust">fn create(alloc: Allocator, comptime T: type) Allocator.Error!*T {
    const ptr = try alloc.create(T);
    return ptr;
}

fn destroy(alloc: Allocator, ptr: anytype) void {
    alloc.destroy(ptr);
}
</code></pre>
<p>Nothing fancy with this base code. Now, let’s introduce proof marking. The idea
is that we want that not to be in the way too much. The syntax of a proof mark
goes as follows:</p>
<ol>
<li>Full syntax <code>| [a, b, …, e]#tag</code>.</li>
<li>Inferred syntax <code>| #tag</code>.</li>
</ol>
<p>The <code>| …</code> syntax marks a type with a proof.</p>
<p><code>[a, b, …, e]</code> is a list of tying arguments, which ties the proof to those
values. If omitted (hence using the second form), all of the function arguments
are used as tying the proof.</p>
<p>The <code>#tag</code> syntax declares a tag symbol in the current Zig file. That tag along
with the tying variables uniquely identifies the proof.</p>
<p>Proofs can only be created in return-position of a function. Let’s mark our
<code>create</code> function with a proof:</p>
<pre><code class="language-rust">fn create(alloc: Allocator, comptime T: type) Allocator.Error!*T | #create {
    const ptr = try alloc.create(T);
    return ptr;
}
</code></pre>
<p>As you can see, proofs only live at compilation time, and the implementation
has not changed. <code>| #create</code> marks the return value of <code>create</code> with a proof
which is basically <code>[alloc, comptime T]#create</code>. Now, let’s create something:</p>
<pre><code class="language-rust">test "linear allocations" {
  const alloc = std.testing.allocator;

  const ptr = try create(alloc, u32);
}
</code></pre>
<p>The type of <code>ptr</code> is <code>*u32 | [alloc, u32]#create</code>. Here, <code>alloc</code> is our testing
allocator, and thus we tie the <code>ptr</code> variable to the <code>alloc</code> variable and <code>u32</code>.</p>
<p>What we have here is what I would call a dangling proof. <code>ptr</code> type is still
marked by a proof, and this the compiler should emit an error stating that a
dangling proof is left to be consumed. To solve the problem, we must consume the
proof. Let’s modify <code>destroy</code>:</p>
<pre><code class="language-rust">fn destroy(alloc: Allocator, ptr: anytype | [alloc, @typeInfo(@TypeOf(ptr)).Pointer.child]#create) void {
    alloc.destroy(ptr);
}
</code></pre>
<p>Okay, that’s a lot. Let’s scan through the signature. We can see that <code>ptr</code> has
a proof-marked type. The proof ties <code>alloc</code> and, basicalyl, the type of the
pointee — because Zig’s <code>comptime</code> is so awesome, we can actually do all of that
at compile-time!</p>
<p>If you think this is too verbose, you can simply pass the type yourself:</p>
<pre><code class="language-rust">fn destroy2(alloc: Allocator, comptime T: type, ptr: *T | [alloc, comptime T]#create) void {
  // …
}
</code></pre>
<p>We can then properly consume our linear <code>ptr</code>:</p>
<pre><code class="language-rust">test "linear allocations" {
  const alloc = std.testing.allocator;

  const ptr = try create(alloc, u32);

  // 1. either
  destroy(alloc, ptr);

  // 2. or this
  destroy2(alloc, u32, ptr);
}
</code></pre>
<h2>Some thoughts about marking</h2>
<p>If you have noticed, marking is done on the error union:</p>
<pre><code class="language-rust">fn create(alloc: Allocator, comptime T: type) Allocator.Error!*T | #create {
</code></pre>
<p>And the question is: what happens if we return an error? I think that an error
would invalidate the proof. However, if you don’t call <code>try</code>, the type is:</p>
<pre><code class="language-rust">test "linear allocations" {
  const alloc = std.testing.allocator;

  const ptr: Allocator.Error!*T | [alloc, u32]#create = create(alloc, u32);
}
</code></pre>
<p>If <code>ptr</code> is actually an <code>Allocator.Error</code>, the proof is lost and considered
consumed.</p>
<h1>Conclusion</h1>
<p>Such a change in the language would have tons of impacts, and because of that, I
think that nothing in the standard library should use the feature, to prevent
breaking pretty much everyone — the <code>Allocator</code> interface is at the heart of
the language and its ecosystem. However, I think that having the feature
available would allow people to use it in their own wrappers, and as mentioned
before, not only for memory allocation. You can easily see how such a feature
would be useful for files, database connections, and more.</p>
<p>I know that the Zig language will probably never accept a proposal like this,
but given <code>comptime</code> that is already there, I think it’s super close of actually
being able to provide PwTP.</p>
<p>Anyway, that was my ideas, and I will probably write more about Zig in the
future.</p>
<p>Cheers!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 17 Jul 2024 18:35:00 GMT</pubDate></item><item><title>My Rust 2020 ideas</title><link>https://strongly-typed-thoughts.net/blog/rust-2020-ideas</link><description><![CDATA[<p>This blog article is a small reply to the
<a href="https://blog.rust-lang.org/2019/10/29/A-call-for-blogs-2020.html">public call for blog posts 2020 in the Rust community</a>.
I will express what I would like Rust to go to, keeping in mind that it’s solely my ideas and
opinions others’ might differ.</p>
<p>The points expressed here are written by decreasing priority, starting with the feature I would
like the most to see implemented as soon as possible.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#rank-n-quantification">Rank-N quantification</a></li>
<li><a href="#kinds">Kinds</a></li>
<li><a href="#gat">GAT</a></li>
<li><a href="#polymorphic-literals">Polymorphic literals</a></li>
<li><a href="#custom-operators">Custom operators</a></li>
<li><a href="#cargo-dependencies">Cargo dependencies</a></li>
<li><a href="#sealed-traits--private-trait-items">Sealed traits / private trait items</a></li>
<li><a href="#feature-discoverability">Feature discoverability</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>Rank-N quantification</h1>
<p>It’s for sure one among the two features I miss the most from Haskell. Rank-N quantification is a
feature that, when I discovered it almost 9 years ago, changed a lot of things in my way of thinking
and desiging interfaces.</p>
<p>For those not used to it or those having no idea what rank-N quantification is, let me explain
with simple words by taking an example.</p>
<p>Imagine a function that works on a vector of <code>u32</code>:</p>
<pre><code class="language-rust">fn process(v: Vec&lt;u32&gt;)
</code></pre>
<p>That function is <em>monomorphic</em>. You cannot, at compile-time, get several flavours of <code>process</code>.
But now imagine you want to process lots of things without actually requiring the element type
to be <code>u32</code> but, let’s say, <code>T: Into&lt;u32&gt;</code>:</p>
<pre><code class="language-rust">fn process&lt;T&gt;(v: Vec&lt;T&gt;) where T: Into&lt;u32&gt;
</code></pre>
<p>That function has a type variable, <code>T</code>, and we say that is has a <em>rank of 1</em>, or it’s a <em>rank-1</em>
function. If you add more variables, they all remain at the same <em>level</em>, so the function is
still <em>rank-1</em>:</p>
<pre><code class="language-rust">fn process&lt;T, Q&gt;(v: Vec&lt;T&gt;, other: Option&lt;Q&gt;) where T: Into&lt;u32&gt;, Q: Display
</code></pre>
<p>Now, imagine a function that would take as sole argument a function which takes a <code>u32</code> and
returns a <code>String</code>, for instance:</p>
<pre><code class="language-rust">fn foo&lt;F&gt;(f: F) where F: Fn(u32) -&gt; String {
  println!("the function returned {}", f(123));
}
</code></pre>
<p>That function is still <em>rank-1</em>. But now, imagine that we would like to express the idea that the
function that is passed as argument <em>must</em> work for <em>any</em> <code>T: Debug</code> instead of <code>u32</code>. Here, the
<em>any</em> word is important, because it means that you must pass a polymorphic function to <code>foo</code>, which
will get monomorphized <em>inside</em> the body of <code>foo</code>. If you’ve thought about this:</p>
<pre><code class="language-rust">fn foo&lt;F, T&gt;(f: F) where F: Fn(T) -&gt; String, T: Debug {
  println!("the function returned {}", f(123u32));
}
</code></pre>
<p>Then you’re wrong, because that function cannot compile. The reason for this is that the type of <code>f</code>
is <code>F: Fn(T) -&gt; String</code> and you try to pass <code>123u32</code> instead of <code>T</code>. That function definition cannot
work because the body would force <code>T</code> to be <code>u32</code>. The problem here is that, currently, Rust doesn’t
allow us to do what we want to: <code>T</code> shouldn’t be monomorphized <em>at the same rank as</em> <code>F</code>, because
<code>T</code> will be chosen by the implementation of <code>foo</code>, not the caller!</p>
<p>I would like this:</p>
<pre><code class="language-rust">fn foo&lt;F&gt;(f: F) where F: for&lt;T&gt; Fn(T) -&gt; String where T: Debug;

// or
fn foo&lt;F&gt;(f: F) where F: for&lt;T: Debug&gt; Fn(T) -&gt; String;
</code></pre>
<p>We call that a <em>rank-2</em> function, because it has two <em>levels</em> / <em>ranks</em> of type variables. We could
use it this way:</p>
<pre><code class="language-rust">fn foo&lt;F&gt;(f: F) where F: for&lt;T&gt; Fn(T) -&gt; String where T: Debug {
  println!("the function returned {}", f(123u32);
  println!("the function returned {}", f("Hello, world!");
}
</code></pre>
<p>You can imagine rank-N quantification by nesting HRTB syntax:</p>
<pre><code class="language-rust">// a rank-3 function
fn foo&lt;F&gt;(f: F) where F: for&lt;T&gt; Fn() -&gt; T where T: for&lt;X&gt; Into&lt;X&gt; where X: Debug;
</code></pre>
<p>But it’s rarely needed and I struggle to find a real usecase for them (but there are!). From my
Haskell experience, we really really rarely need more than rank-2 quantification.</p>
<p>You can find more in-details thoughts of that feature on a previous article of mine,
<a href="https://phaazon.net/blog/rank-n-rust">here</a>.</p>
<h1>Kinds</h1>
<p>“Kinds” is the second feature I would love to see in Rust. For those who don’t know what they are,
consider:</p>
<ul>
<li>A <em>value</em> is something that lives at runtime. In Rust, it has a <em>type</em>. You can refer to values
directly with literals or you can bind them via <em>let bindings</em>, for instance. In <code>let x = 3</code>,
<code>3</code> is a value and <code>x</code> is a binding to that value.</li>
<li>A <em>type</em> is like a <em>value</em> that lives at compile-time. You can refer to types directly or via
<em>type variables</em>. For instance, <code>u32</code> is a type and in <code>fn foo&lt;T&gt;()</code>, <code>T</code> is a type variable.
It’s actually a <em>free variable</em>, here. Currently, Rust stops here.</li>
</ul>
<p>In Haskell but also Idris, ATS, Coq and many others, types have types too. We name those kinds.
To understand what it means:</p>
<ul>
<li>A type is just a <em>label</em> on a set. <code>u32</code> is a label (really, imagine <code>"u32"</code>) of a very big
set that contains <strong>all the possible</strong> values that can be labelled as <code>u32</code>. You find in that
set <code>0</code>, <code>1</code>, <code>34</code>, <code>2390743</code>, etc.</li>
<li>A kind is just a <em>label</em> on a set, too. But that set doesn’t contain values; it contains types.
A kind is just a labelled set of types.</li>
</ul>
<p>For instance, imagine the kind <code>Number</code>. You can put in that set the types <code>u32</code>, <code>i32</code>, <code>usize</code>,
<code>f32</code>, etc. But now imagine: type variables are to types what variables are to values. What would
be a <em>kind variable</em>? Well, it would be something that would allow us to give more details about
what a type should be. For instance:</p>
<pre><code class="language-rust">trait Functor {
  fn map&lt;A, B, F&gt;(self: Self&lt;A&gt;, f: F) -&gt; Self&lt;B&gt;;
}

impl Functor for Option {
  fn map&lt;A, B, F&gt;(self: Self&lt;A&gt;, f: F) -&gt; Self&lt;B&gt; {
    self.map(f)
  }
}

// whatever the type of functor, just ignore its content and replace with ()
// we could also introduce a type variable A and use fct: Fct&lt;A&gt; but since we don’t
// need it, we use _
//
// The &lt;Fct&lt;_&gt;&gt; marks the kind of Fct (i.e. its kind is Type -&gt; Type, as in it expects
// a type to be type)
fn void&lt;Fct&lt;_&gt;&gt;(fct: Fct&lt;_&gt;) -&gt; Fct&lt;()&gt; where Fct: Functor {
  fct.map(|_| ())
}
</code></pre>
<p>Currently, that syntax doesn’t exist and I don’t even know how it would be formalized. The <code>void</code>
function above looks okay to me but not the <code>trait</code> definition. The syntax <code>T&lt;_, _&gt;</code> would
declare a type which must has two type variables, etc.</p>
<h1>GAT</h1>
<p><a href="https://github.com/rust-lang/rust/issues/44265">GATs</a> are a bit akin to kinds in the sense that they
allow to express type constructors (i.e. which kinds are, for instance, <code>Type -&gt; Type</code>, if they only
have one type variable).</p>
<p>That’s a feature I need a lot in several crates of mine, so I hope it will be implemented and stable
soon! :)</p>
<h1>Polymorphic literals</h1>
<p>Something I want a lot too and hasn’t been discussed a lot (I might write an RFC for that because
I want it very badly). What it means is that:</p>
<pre><code class="language-rust">let x = 3;
</code></pre>
<p>The type of <code>x</code> here would be polymorphic as <code>T: FromLit&lt;usize&gt;</code>. We would have several implementors
and it would go like this:</p>
<pre><code class="language-rust">pub trait FromLit&lt;L&gt;: L: Lit {
  const fn from_lit(lit: L) -&gt; Self;
}

// blanket impl
impl&lt;L&gt; FromLit&lt;L&gt; where L: Lit {
  const fn from_lit(lit: L) -&gt; Self {
    lit
  }
}

// generated by rustc
impl Lit for usize {}
impl Lit for isize {}
impl Lit for u32 {}
impl Lit for &amp;'static str {}
// …
</code></pre>
<p>This would allow us to do something like that:</p>
<pre><code class="language-rust">pub enum Expr {
  ConstBool(bool),
  ConstI32(i32),
  // …
}

impl FromLit&lt;bool&gt; for Expr {
  const fn from_lit(lit: L) -&gt; Self {
    Expr::ConstBool(lit)
  }
}

// in a function
let expr: Expr = false;
</code></pre>
<p>As a rationale, Haskell has that in its base language since forever and under the language extension
called <code>OverloadedStrings</code> and <code>OverloadedLists</code> for strings and lists.</p>
<h1>Custom operators</h1>
<p>A feature that wouldn’t make everyone happy, so I’m pretty sure it will not be in the Rust 2020 roadmap
(and maybe never end up in Rust at all, sadly), but I think it’s worth mentioning it.</p>
<p>I would be able to create custom operators. The reason for this is simple:
<a href="https://wiki.haskell.org/Embedded_domain_specific_language">EDSLs</a>. I love EDSLs. Having the possibility
to enrich expressiveness via custom operators is something I’ve been wanting for quite a while now and
I’m so surprised people haven’t arised that concern yet.</p>
<p>I know there is concerns from people who know the
<a href="http://hackage.haskell.org/package/lens">Haskell lens library</a> and its infamous lists of horrible and
awful operators, but that’s not a reason to block such a feature to me, for two reasons:</p>
<ul>
<li><code>lens</code> is really extreme is likely the sole real problem in the Haskell ecosystem.</li>
<li>We could mitigate that <em>fear</em> by forcing custom operators to have a <em>function</em> associated with the
operator, so that people who don’t want to use the custom operator can still use a correctly named
function.</li>
</ul>
<p>I’m a huge partisan of the idea that there are billions of people speaking Chinese, a language very
cryptic <em>to me</em>, because I just cannot speak Chinese. Yet it doesn’t prevent billions of people speaking
Chinese on a daily basis without any problem. It’s always a question of learning and an operator should
be perceived as a function name. Stop spreading fear about readability: a convoluted function name is
also pretty hard to read.</p>
<p>To mitigate fear even further, there are several very good operators in Haskell that are actually very very
simple to understand and memorize:</p>
<ul>
<li>The <code>fmap</code> function operator version is <code>&lt;$&gt;</code>.</li>
<li>When applying <code>fmap</code> to ignore what’s inside a functor and replacing it with a constant, we use
<code>fmap (const 32)</code>, for instance — or <code>fmap (\_ -&gt; 32)</code> for the lambda version. You can use <code>&lt;$&gt;</code> too,
<code>const 32 &lt;$&gt; [1, 2, 3]</code>. But there’s a very logical and simple operator to remember. Notice how the
constant value is at the left of the <code>&lt;$&gt;</code> operator? Then, you can do the exact same thing with
<code>32 &lt;$ [1, 2, 3]</code>. On the same level, if you prefer to put the value on the right side:
<code>[1, 2, 3] $&gt; 32</code>. Simple.</li>
<li>The <code>&lt;|&gt;</code> operator. It has that <code>|</code> in it, which is often <code>OR</code> in most languages. And guess what: that
operator is the <em>alternative operator</em>. <code>a &lt;|&gt; b</code> equals <code>a</code> if it’s <em>true</em> or <code>b</code> if not. The definition
of truth is up to the type, but for <code>Maybe a</code> — <code>Option&lt;T&gt;</code> in Rust, <em>true</em> is <code>Just _</code> (<code>Some(_)</code>) and
<em>false</em> is <code>Nothing</code> (<code>None</code>). See how easy it is?</li>
<li>In <a href="http://hackage.haskell.org/package/parsec">parsec</a>, one of the most famous Haskell parser, there’s
an operator you can use to provide more descriptive error messages for <em>all parsers</em> (even sub-parsers!):
the <code>&lt;?&gt;</code> operator. Once again, the <code>?</code> reminds a question, information, etc.</li>
<li>The <code>.</code> operator in Haskell composes two functions. It looks closely to the <code>∘</code> math notation.</li>
<li>The monadic bind is <code>&gt;&gt;=</code>, which expects the function on the right hand side… Guess what the
<code>=&lt;&lt;</code> operator does. The exact same thing, but expects the function to be on the left hand side.</li>
<li>When dealing with paths, the <code>&lt;/&gt;</code> operator allows to separate path parts without redundant <code>/</code>. The
<code>&lt;.&gt;</code> operator allows you to write the extension name!
<code>"/home/phaazon" &lt;/&gt; spareProjectsDir &lt;/&gt; "rust/rfcs" &lt;/&gt; lastRFC &lt;.&gt; "md"</code>.</li>
<li>Etc. etc.</li>
</ul>
<h1>Cargo dependencies</h1>
<p>A huge topic, but basically, I hope that <code>cargo</code> can now resolve dependencies without accepting
several versions of a crate, but instead resolves them by traversing the whole dependency graph.</p>
<p>This is often needed on my side as I like to make a crate compatible with several dependency
versions, so that people who don’t update often can still have their clients benefits from updates
on my side. It’s expressed as SemVer ranges (e.g. <code>stuff = "&gt;=0.4, &lt; 0.9"</code>) but <code>cargo</code> will take
the <em>best</em> one it knows. Basically, if you have such a dependency in project <code>A</code> and you depend on
<code>B</code> which has <code>stuff = "0.5"</code>, then you will end up with both <code>stuff-0.5</code> and <code>stuff-0.8</code> in your
graph dependency, which to me is very wrong. Intersecting dependencies <em>should</em> only bring
<code>stuff-0.5</code>, because it’s the highest minimal version that satisfies every crates depending on it
in the dependency graph.</p>
<h1>Sealed traits / private trait items</h1>
<p>I talked about it <a href="https://phaazon.net/blog/rust-traits-privacy">here</a>, but basically, I want to be
able to annotate trait’s items with visibility qualifiers (i.e. <code>pub</code>, <code>pub(crate)</code>, etc.) so that
I can implement a trait in a crate without having people depending on my crate see the guts of the
trait.</p>
<p>Sealed traits prevent people from implementing the trait (first new concept) and private trait
items both prevent people from implementing the trait but they also prevent them from seeing what’s
inside.</p>
<h1>Feature discoverability</h1>
<p>Long discussion occurring <a href="https://phaazon.net/blog/rust-features-documentation">here</a>. Basically,
since <em>features</em> are parts of the language (both via <code>cfg</code> attributes and in <code>Cargo.toml</code>), it
would neat to be able to show them in <code>rustdoc</code> to help people discover what’s possible to do with
a crate, instead of opening the <code>Cargo.toml</code> on GitHub / whatever you’re using.</p>
<h1>Conclusion</h1>
<p>So that’s all for me and what I would love Rust to go towards. I have no idea whether people from
the Rust project will actually read 10% of what I just wrote; I feel like I just made a wish list
for Christmas.</p>
<p>Thank you for having read. Thank you for contributing to Rust and making it the best language
in the world. And as always, keep the vibes and <strong>please</strong> let’s talk on Reddit! :)</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 31 Oct 2019 15:15:00 GMT</pubDate></item><item><title>Moving out of a Drop struct in Rust?</title><link>https://strongly-typed-thoughts.net/blog/rust-no-drop</link><description><![CDATA[<p>Today, I’ve come across a very interesting problem in Rust. This problem comes from a very specific
part of <a href="https://crates.io/crates/luminance">luminance</a>.</p>
<p>Rust has this feature called <em>move semantics</em>. By default, the <em>move semantics</em> are enforced. For
instance:</p>
<pre><code>fn foo_move&lt;T&gt;(x: T)
</code></pre>
<p>This function says that it expects a <code>T</code> and need to move it in. If you don’t want to move it, you
have to <em>borrow</em> it. Either immutable or mutably.</p>
<pre><code>fn foo_borrow&lt;T&gt;(x: &amp;T)
fn foo_borrow_mut&lt;T&gt;(x: &amp;mut T)
</code></pre>
<p>Now, when you move a value around, it’s possible that you don’t use that value anymore. If you look
at the <code>foo_move</code> function, is very likely that we’ll not use that <code>T</code> object after the function has
returned. So Rust knows that <code>x</code> will go out of scope and then its memory must be invalidated and
made available to future values. This is very simple as it will happen on the stack, but it can get
a bit tricky.</p>
<p>Rust doesn’t have the direct concept of <em>destructors</em>. Or does it? Actually, it does. And it’s
called the <code>Drop</code> trait. If a type implements <code>Drop</code>, any value of this type will call a specific
function for disposal when it goes out of scope. This is akin to running a destructor.</p>
<pre><code>struct Test;

impl Drop for Test {
  fn drop(&amp;mut self) {
    println!("drop called!");
  }
}
</code></pre>
<p>Whenever a value of type <code>Test</code> goes out of scope / must die, it will call its <code>Drop::drop</code>
implementation and then its memory will be invalidated.</p>
<p>Ok, now let’s get into my problem.</p>
<h1>Moving out of a field</h1>
<p>Consider the following code:</p>
<pre><code>struct A;

struct B;

struct Foo {
  a: A,
  b: B
}

impl Foo {
  fn take(self) -&gt; (A, B) {
    (self.a, self.b)
  }
}

fn main() {
  let foo = Foo { a: A, b: B };
  let (a, b) = foo.take();
}
</code></pre>
<p>The implementation of <code>Foo::take</code> shows that we <em>move out of <code>Foo</code></em> – two, actually. We move the
<code>a: A</code> field out of <code>Foo</code> and <code>b: B</code> out of <code>Foo</code>. Which means that we <em>destructured</em> <code>Foo</code>. The
call to <code>Foo::take</code> in the <code>main</code> function demonstrates how you would use that function.</p>
<p>Now consider this. <code>A</code> and <code>B</code> here are opaque type we don’t want to know the implementation. But
keep in mind they represent <em>scarce resources</em> – like UUID to important resources we must track in
the <code>Foo</code> object. Consider this new code:</p>
<pre><code>struct A;

struct B;

struct Foo {
  a: A,
  b: B
}

impl Drop for Foo {
  fn drop(&amp;mut self) {
    // something tricky with self.a and self.b
  }
}

impl Foo {
  fn take(self) -&gt; (A, B) {
    (self.a, self.b) // wait, that doesn’t compile anymore!
  }
}
</code></pre>
<p>You would get the following error:</p>
<blockquote>
<p>error[E0509]: cannot move out of type <code>Foo</code>, which implements the <code>Drop</code> trait</p>
</blockquote>
<p>As you can see, Rust doesn’t allow you to move out of a value which type implements <code>Drop</code>, and this
is quite logical. When <code>Foo::take</code> returns, because of <code>self</code> going out of scope, it must call
its <code>Drop::drop</code> implementation. If you have moved out of it – both <code>a: A</code> and <code>b: B</code> fields, the
<code>Drop::drop</code> implementation is now a complete <a href="https://en.wikipedia.org/wiki/Undefined_behavior">UB</a>. So Rust is right here and doesn’t allow you to
do this.</p>
<p>But imagine that we <strong>have</strong> to do this. For insance, we need to hand over both the scarce resources
<code>a</code> and <code>b</code> to another struct (in our case, a <code>(A, B)</code>, but you could easily imagine a better type
for this).</p>
<p>There’s a way to, still, implement <code>Foo::take</code> with <code>Foo</code> implementing <code>Drop</code>. Here’s how:</p>
<pre><code>impl Foo {
  fn take(mut self) -&gt; (A, B) {
    let a = mem::replace(&amp;mut self.a, unsafe { mem::uninitialized() });
    let b = mem::replace(&amp;mut self.b, unsafe { mem::uninitialized() });
    
    mem::forget(self);
    
    (a, b)
  }
}
</code></pre>
<p>There’s a few new things going on here. First, the <code>mem::replace</code> function takes as first parameter
a mutable reference to something we want to replace by its second argument and returns the initial
value the mutable reference pointed to. Because <code>mem::replace</code> doesn’t deinitialize any of its
argument, I like to think of that function as a <em>move-swap</em>. It just moves out the first argument
and put something in the hole it has made with its second argument.</p>
<p>The <code>unsafe</code> function, <code>mem::uninitialized</code>, is a bit special as it bypasses Rust’s normal
memory-initialization and actually doesn’t do anything. However, there’s no specification about what
this function really does and you shouldn’t be concerned. What you just need to know is that this
function just gives you <em>uninitialized</em> memory.</p>
<p>However, if we just returned <code>(a, b)</code> right after the two <code>mem::replace</code> calls, the <code>Drop::drop</code>
implementation would run at the end of the function, and it would run <em>badly</em>, because of the now
uninitialized data you find in the <code>self.a</code> and <code>self.b</code> fields. It could still be the old values.
Or it could be garbage. We don’t care. We mustn’t read from there. Rust provide a solution to this,
and this is the <code>mem::forget</code> function. This function is like dropping your value… without calling
its <code>Drop::drop</code> implementation. It’s an escape hatch. It’s a way to invalidate a value and in our
case that function is super handy because <code>self</code> now contains uninitialized data!</p>
<p>And here we have a complete working <code>Foo::take</code> function.</p>
<h1>So what’s the problem again?</h1>
<p>The problem is that I used to function I wish I didn’t:</p>
<ul>
<li><code>mem::uninitialized</code>, which is,
<a href="https://doc.rust-lang.org/std/mem/fn.uninitialized.html">as the docs state</a>,
<strong>incredibly hazardous</strong>.</li>
<li><code>mem::forget</code>, which is not unsafe in the Rust idea of safety, but still has some kind of very
nasty unsafety deep in it – it’s very easy to leak resources if you don’t pay extra attention.</li>
</ul>
<p>The problem is that Rust doesn’t allow you to move out of a value which implement <code>Drop</code> even if you
know that you’ll <code>forget</code> it. Rust doesn’t currently have a primitive / function to express both
principles at the same time: forgetting and moving out of a <code>Drop</code> value.</p>
<p>This had me to come to the realization that I would like an interface like this:</p>
<pre><code>fn nodrop&lt;T, F, A&gt;(
  x: T,
  f: F
) -&gt; A
where T: Drop,
      F: FnOnce(T) -&gt; A
</code></pre>
<p>This function would give you the guarantee that the closure will not <code>drop</code> its argument but would
still leak it, temporarily giving you the power to move out of it. You will also notice that if you
want to have <code>A = T</code>, it’s possible: that would mean you destructure a value and re-build it back
again inside the same type, which is still sound.</p>
<p>However, this function would make it very easy to leak any field you don’t move out. This is
actually the same situation as with <code>mem::forget</code>: if you forget to <code>mem::replace</code> a field before
calling <code>mem::forget</code>, you end up in the exact same situation.</p>
<p>I agree it’s a very niche need, and if you’re curious about why I need this, it’s because of how
<em>buffers</em> are handled in <a href="https://crates.io/crates/luminance">luminance</a>. The <code>Buffer&lt;T&gt;</code> type has an internal <code>RawBuffer</code> as field that
holds GPU handles and internal details and a <code>PhantomData&lt;T&gt;</code>. It’s possible to convert from
one type to the other but both implement <code>Drop</code>, so you have to implement the hack I just presented
in this blog post to make both conversions. With my <code>nodrop</code> function, the code would be way
easier, without needing to unfold <code>unsafe</code> blocks. Something like this would be possible:</p>
<pre><code>pub fn to_raw(self) -&gt; RawBuffer {
  mem::nodrop(self, |buffer| buffer.raw) // combine mem::forget and mem::replace + uninitialized
}
</code></pre>
<p>What do you peeps think of such a function? For sure that would add possible easy leaks, that’s a
drawback I find very difficult not to agree with. However, I just wanted to share my little
experience of Tuesday evening.</p>
<p>That’s all for me. Thanks for having read me, I love you. Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 22 May 2018 22:22:00 GMT</pubDate></item><item><title>Rank-n functions in Rust?</title><link>https://strongly-typed-thoughts.net/blog/rank-n-rust</link><description><![CDATA[<p>Yesterday, I came across a very simple problem <a href="https://www.reddit.com/r/rust/comments/8kh94b/is_there_any_way_to_have_move_semantics_rankn">that I summed up here</a>.
The idea is the following:</p>
<ul>
<li>You want to implement a unary function <code>foo</code> that takes a function <code>F</code> as sole argument and
returns its result.</li>
<li><code>F</code> is a unary function that takes a function <code>Λ</code> as sole argument.</li>
<li><code>Λ</code> doesn’t take any argument and return a type <code>T</code>.</li>
<li>We want linear typing here, but because it would make the blog post more complicated, we’ll
ignore that property.</li>
</ul>
<p>In <strong>Haskell</strong>, you would represent this scenario like this:</p>
<pre><code>data T = T -- we’re not interested in this type yet

        --     v this is F
        --------------
foo :: ((() -&gt; T) -&gt; b) -&gt; b
         -------
         -- ^ this is Λ

-- or for people less familiar with Haskell, here’s a decomposed version (but still the same thing)
type Λ = () -&gt; T
type F b :: Λ -&gt; b
foo :: F b -&gt; b
</code></pre>
<blockquote>
<p>Note: it’s pretty much known in the Haskell world but maybe not in others: if you have a value,
like <code>a</code>, it’s isomorphic to a unary function that returns this value and takes <code>()</code> as single
argument – <code>() -&gt; a</code>.</p>
</blockquote>
<blockquote>
<p>Note²: for curious, in Haskell, the syntax <code>a -&gt;. b</code> is a linear arrow. More about this on
<a href="https://ghc.haskell.org/trac/ghc/wiki/LinearTypes">linear Haskell</a>. I didn’t include the syntax in the snippet because it would make the post too
complicated for our purpose.</p>
</blockquote>
<blockquote>
<p>Note³: this is a rank-2 type function, because it’s a function that takes a function (1) that
takes another function (2). There’re levels of “nesting” there.</p>
</blockquote>
<p>In <strong>Rust</strong>, argh… <strong>Rust</strong> is not as powerful as <strong>Haskell</strong> for complex abstractions – even though
we’re working on making it better and better. To me, <strong>Rust</strong> competes with <strong>Haskell</strong>, but the
current blog post will show you that it still has a lot to learn from its parent! :)</p>
<p>Let’s sketch something in <strong>Rust</strong>.</p>
<pre><code>struct T; // we’re not interested in this type yet

fn foo&lt;F, B&gt;(f: F) -&gt; B where F: FnOnce(???) -&gt; B {
  f(|| 42)
}
</code></pre>
<p>What should we put in place of the <code>???</code> placeholder? Clearly, looking at the <strong>Haskell</strong> snippet,
we want a function that produces a <code>T</code>. Something like that:</p>
<pre><code>fn lambda() -&gt; T
</code></pre>
<p>So we could use the type <code>fn () -&gt; T</code>, but that will not work, because a lambda has not such a
type – you can see the lambda <code>|| 42</code>. Let’s try something else.</p>
<pre><code>fn foo&lt;F, B&gt;(f: F) -&gt; B where F: FnOnce(impl FnOnce() -&gt; T) -&gt; B
</code></pre>
<p>If you look at the <a href="https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md">impl Trait RFC</a>,
you see that it’s a <strong>Rust</strong> construct to build a contract that lets you handle a value through
<em>traits</em> without knowing the type – we call that an <em>existential</em>. The type is picked by the
<em>callee</em>, which in our case is the function itself.</p>
<p>However, this syntax doesn’t work because – as for now – <code>impl Trait</code> works only in return position
and the consensus seems to have agreed that when <code>impl Trait</code> will be implemented in argument
position, <strong>it will not mean an existential, but a universal</strong>, e.g.:</p>
<pre><code>fn bar&lt;I: Iterator&lt;Item = u32&gt;&gt;(i: I)
fn bar&lt;I&gt;(i: I) where I: Iterator&lt;Item = u32&gt;
fn bar(i: impl Iterator&lt;Item = u32&gt;)
</code></pre>
<p>Are all the same thing.</p>
<blockquote>
<p>Note: I’ve made a ranting about the decision to give universal semantics to <code>impl Trait</code> in
argument position <a href="https://github.com/rust-lang/rust/issues/34511#issuecomment-390358483">here</a>.
I don’t want to start a type theory war, if it’s the consensus, then we have to accept it and
move on. It’s just a bit a pity. Topic closed, let’s get back to our sheep!</p>
</blockquote>
<p>So we cannot use that. Argh! Let’s try something else then!</p>
<pre><code>fn foo&lt;F, G, B&gt;(f: F) -&gt; B where F: FnOnce(G) -&gt; B, G: FnOnce() -&gt; T
</code></pre>
<p>This cannot work because here you have two universals <code>F</code> and <code>G</code>, meaning that <code>G</code> will be picked
by the caller while you want to decide of its type directly in your function (remember the lambda).</p>
<pre><code>fn foo&lt;F, B&gt;(f: F) -&gt; B where F: for&lt;G: FnOnce() -&gt; T&gt; FnOnce(G) -&gt; B
</code></pre>
<p>I like this one <em>a lot</em>. It uses a concept called <a href="https://doc.rust-lang.org/nomicon/hrtb.html">HRTB</a> in <strong>Rust</strong> which you can see by the
<code>for&lt;_&gt;</code> syntax. However, this will not work because as for today, <a href="https://doc.rust-lang.org/nomicon/hrtb.html">HRTB</a> only works with lifetimes.</p>
<blockquote>
<p>Ok, that seems a bit desperate!</p>
</blockquote>
<p>Hm, how about this:</p>
<pre><code>fn foo&lt;F, B&gt;(f: F) -&gt; B where F: FnOnce(Box&lt;FnOnce() -&gt; T) -&gt; B
</code></pre>
<p>Even though that seems to typecheck, you won’t ever be able to use <code>Λ</code> here because you cannot move
out of a <code>Box</code> with <code>FnOnce</code>. This limitation is removed with the <code>FnBox</code> type:</p>
<pre><code>fn foo&lt;F, B&gt;(f: F) -&gt; B where F: FnOnce(FnBox() -&gt; T) -&gt; B
</code></pre>
<p>However:</p>
<ul>
<li><code>FnBox</code> is unstable and will only work on nightly right now.</li>
<li>It <em>boxes</em> the function, which is not what we want there.</li>
</ul>
<blockquote>
<p>Ok, that seems desperate. Period.</p>
</blockquote>
<p>Well, if we only stick to those tools (i.e. functions, type variables and trait bounds), currently,
yeah, there’s no solution. However, if we add the traits to our toys, we can work around the
problem:</p>
<pre><code>trait GConsumer {
  type Output;

  fn call_once&lt;G&gt;(self, f: G) -&gt; Self::Output where G: FnOnce() -&gt; i32;
}

fn foo&lt;F&gt;(f: F) -&gt; F::Output where F: GConsumer {
  f.call_once(|| 32) // hurray!
}
</code></pre>
<p>This snippet is actually quite interesting. We introduce the <em>existential</em> via a trait. The
existential is encoded with the <code>G</code> type variable of the <code>call_once</code> method of <code>GConsumer</code>. If you
look at that function from <code>foo</code> perspective, <code>G</code> is definitely an <em>existential</em>! – and it’s a
<em>universal</em> for <code>call_once</code>.</p>
<p>I really like this idea, because it’s simple and legacy <strong>Rust</strong>. But I feel a bit confused, because
it’s simple to define but hard to use. Instead of just passing a closure to <code>foo</code>, people will now
have to implement a separate type on which will be called <code>call_once</code>. That’s a lot of boilerplate.</p>
<p>I hope we’ll come to a better solution to this problem – if you can do it with any trait, it should
be possible with <code>FnOnce</code> and a few candies into the <strong>Rust</strong> language itself.</p>
<p>That’s all for me for today. Keep the vibes and have a nice weekend!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 19 May 2018 12:48:00 GMT</pubDate></item><item><title>Introducing pest into glsl and hindsight about nom vs. pest (part 1)</title><link>https://strongly-typed-thoughts.net/blog/glsl-pest-part-1</link><description><![CDATA[<p>This is the first article out of a (I guess?!) series of upcoming articles about… <em>parsing</em>. More
specifically, I’ve been writing the <a href="https://crates.io/crates/glsl">glsl</a> crate for a while now and
the current, in-use parser is backed with <a href="https://crates.io/crates/nom">nom</a>. <a href="https://crates.io/crates/nom">nom</a> is a <a href="https://en.wikipedia.org/wiki/Parser_combinator">parser combinator</a> crate written
originally by <a href="https://github.com/geal">@geal</a> and there has been a lot of fuzz around <a href="https://crates.io/crates/nom">nom</a> vs. <a href="https://crates.io/crates/pest">pest</a> lately.</p>
<p>Soooooooooooo. Because <a href="https://crates.io/crates/glsl">glsl</a> is written with <a href="https://crates.io/crates/nom">nom</a> in its third iteration and because <a href="https://crates.io/crates/nom">nom</a> is now
at version <code>4</code>, I decided it was time to update the parser code of <a href="https://crates.io/crates/glsl">glsl</a>. I heard about the
comparison between <a href="https://crates.io/crates/pest">pest</a> and <a href="https://crates.io/crates/nom">nom</a> and decided to write an implementation with <a href="https://crates.io/crates/pest">pest</a>.</p>
<p>This is the first article of a series about how writing a <a href="https://crates.io/crates/pest">pest</a> looks like is fun compared to
writing the <a href="https://crates.io/crates/nom">nom</a> parser. I’ll post several articles as I advance and see interesting matter to
discuss and share.</p>
<ul>
<li>Second article <a href="https://phaazon.net/blog/glsl-pest-part-2">here</a></li>
</ul>
<h1>Oh my; I love PEG!</h1>
<p>First thing first: <a href="https://crates.io/crates/pest">pest</a> consumes a file at compile-time that contains a <a href="https://en.wikipedia.org/wiki/Parsing_expression_grammar">PEG</a> grammar defining the
format of input you want to parse. <a href="https://en.wikipedia.org/wiki/Parsing_expression_grammar">PEG</a> – which stands for <em>Parsing Expression Grammar</em> – is a
formal language that enables you to describe your own language with <em>rules</em>. Those rules are written
with some basic blocks that belong to <em>Language Theory</em> – if you’ve ever heard of Stephen Kleene and
its famous <a href="https://en.wikipedia.org/wiki/Kleene_star">Kleene star</a> for instance, you will feel familiar with PEG.</p>
<p>What I love about PEG is that with a very limited set of constructs, you can describe a lot of
determinist languages. In the case of GLSL450 – which is the language that the <a href="https://crates.io/crates/glsl">glsl</a> crate can
parse – it’s a <em>context-free</em> and <em>determinist</em> language. So the whole language can be defined in
terms of (recursive) PEG rules.</p>
<p>The PEG primitive constructs available in <a href="https://crates.io/crates/pest">pest</a> are:</p>
<ul>
<li>Matching against a lexeme, like <code>"foo"</code>: this rule will recognize any string that is <code>"foo"</code>.</li>
<li>Matching against a range of char, like <code>'a' .. 'z'</code>: this will recognize any char in the range,
like <code>"a"</code>, <code>"d"</code> or <code>"z"</code>.</li>
<li>Matching against another rule, simply by using its name.</li>
<li>Matching a sequence of rules, like <code>a ~ b</code>: this rule will be matched if it can match the <code>a</code>
rule followed by the <code>b</code> rule; <code>"foo" ~ 'a' .. 'z'</code> will recognize <code>"foot"</code>, <code>"food"</code>, <code>"fooz"</code>
but also <code>"foo r"</code> – this whitespace behavior is explained below – etc.</li>
<li>Matching either a rule or another (alternative), like <code>a | b</code>: this rule will match either <code>a</code>
if it succeeds or <code>b</code> if <code>a</code> fails and <code>b</code> succeeds or none otherwise. <code>"foo" | 'a' .. 'z'</code> will
be matched with <code>"foo"</code> and <code>"a"</code> or <code>"f"</code>.</li>
<li>Optional matching with the <code>?</code> suffix operator, like <code>a?</code>: this will match <code>a</code> if it succeeds or
will just ignore its failure if it fails, making it optional. For instance, <code>"hello"? ~ "world"</code>
will match both <code>"hello world"</code> and <code>"world"</code>.</li>
<li>Parenthesis can be used to group rules and reason on them as a whole, like <code>(a ~ b)</code>.</li>
<li>Repetitions:
<ul>
<li>The <a href="https://en.wikipedia.org/wiki/Kleene_star">Kleene star</a> (matches zero or many), like <code>a*</code>: this rule matches zero or more times the
<code>a</code> rule. For instance, <code>('0' .. '9')*</code> will match <code>"0"</code> and <code>"314"</code> but will also match <code>""</code>.</li>
<li>The <a href="https://en.wikipedia.org/wiki/Kleene_star#Kleene_plus">Kleene plus</a> (matches one or many), like <code>a+</code>: this rule matches one or more times the
<code>a</code> rule. For instance, <code>('0' .. '9')+</code> will match <code>"0"</code> and <code>"314"</code> but will not match <code>""</code>
because it expects at least one digit.</li>
<li>The <code>{min, max}</code> notation. The rule <code>a{0,}</code> is akin to <code>a*</code>: it must be matched at least 0
times and at maximum infinite times. The rule <code>a{,3}</code> must be matched between 0 and 3
times and the rule <code>a{1, 12}</code> must be matched between 1 and 12 times.</li>
</ul>
</li>
</ul>
<p>Additionally, <a href="https://crates.io/crates/pest">pest</a> provides a notation to add <em>rule modifiers</em>:</p>
<ul>
<li>A simple rule is written <code>rule_name { definition }</code>, like <code>number = { ASCII_DIGIT+ }</code>.</li>
<li><code>~</code>, <code>*</code> and <code>+</code> rules get interleaved with two special rules <strong>if defined</strong>:
<ul>
<li><code>WHITESPACE</code>, defining how to eat a single whitespace.</li>
<li><code>COMMENT</code>, defining how to eat a comment.</li>
<li>Those rules are interleaved as <code>(WHITESPACE | COMMENT)*</code> so that <code>number = { ASCII_DIGIT+ }</code>
gets actually rewritten as <code>number = { (ASCII_DIGIT | (WHITESPACE | COMMENT)*)+ }</code>.</li>
</ul>
</li>
<li>Because of that last rule, sometimes we don’t want that to happen. In the case of the <code>number</code>
rule defined above, both <code>"324"</code> and <code>"3 2 4"</code> will get matched if we defined <code>WHITESPACE</code> to
eat <code>" "</code>. In order to fix this, the <code>@</code> modifier can be prepended to the rule definition:
<ul>
<li><code>number = @{ ASCII_DIGIT+ }</code></li>
<li>This rule will not get interleaved at all and is <em>atomic</em>.</li>
<li>Easy-to-remember: <code>@</code> for <em>@tomic</em>. :)</li>
</ul>
</li>
<li>The <code>_</code> modifier can be used to mute a rule. For instance, eating whitespaces will not provide
you with any useful tokens. That’s why the <code>WHITESPACE</code> rule is defined as
<code>WHITESPACE = _{ " " }</code>.</li>
<li>There are some other modifiers, like <code>$</code> and <code>!</code> but we’re not interested in them as they’re
very specific.</li>
</ul>
<p>So, basically, you write a (long) list of PEG rules in a file, give that file to Rust via a
proc-macro derive, <em>et voila</em>!</p>
<pre><code>#[derive(Parser)]
#[grammar = "path/to/grammar.pest"]
struct Parser;
</code></pre>
<p>It’s that simple.</p>
<h1>Facing issues</h1>
<p>PEG is really cool to use… but there are caveats. For instance, PEG / <a href="https://crates.io/crates/pest">pest</a> cannot express
<em>left-recursive</em> rules. To understand this, take this rule:</p>
<pre><code>foo = { a | b ~ foo* }
</code></pre>
<p>This rule is correct and will allow you to match against <code>"a"</code> or <code>"b"</code> or even <code>"bbbbbbbbb"</code> and
<code>"bbbbbbbba"</code>. However, the following one is left-recursive and won’t be able to be compiled:</p>
<pre><code>foo = { a | foo* ~ b }
</code></pre>
<p>As you can see, the second alternative is left-recursive. Imagine matching against <code>"a"</code>: the first
alternative fires, everything is great. Now match against <code>"b"</code>. The first alternative fails so
<a href="https://crates.io/crates/pest">pest</a> tries the second one, but in order to take the second one, it needs to try at least once
<code>foo</code> (the <code>foo*</code>) to see whether it fails or not. If it fails, it will then ignore and try to eat a
<code>"b"</code>. If it succeeds, it will try to fire the <code>foo</code> rule again. However, when it tries <code>foo</code> for
the first time, it will try the first alternative again, <code>a</code>, then will fail, then will try <code>foo*</code>,
then <code>a</code>, then <code>foo*</code>, then… You see the pattern.</p>
<p>Left-recursive rules are then forbidden so that we can escape that situation. PEGs need to be
unambiguous and the rules (especially alternatives) are tested <strong>in order</strong>. You will have to unroll
and optimize those rules by hand. In our case, we can rewrite that rule in a PEG-compliant way:</p>
<pre><code>foo = { (a | b)+ }
</code></pre>
<p>An issue I had while writing the PEG of GLSL450 was with the <code>expression</code> rule (among a loooot of
others). Khronos’ specifications are written with left-recursive rules pretty much everywhere and
this is the one for expressions:</p>
<pre><code>expression:
  assignment_expression
  expression COMMA assignment_expression
</code></pre>
<p>Read it as <em>“An expression is either an assignment expression or an expression with a comma followed
by an assignment expression”</em>. This actually means that the second alternative cannot be constructed
without the first one – because it’s recursive! So you must start with an <code>assignment_expression</code>.
Then you can have the second rule applied recursively, like so (parenthesis just for convenience to
show the left-recursion):</p>
<pre><code>((((assignment_expression) COMMA assignment_expression) COMMA assignment_expression) COMMA assignment_expression)
</code></pre>
<p>Etc., etc. If you remove the parenthesis, you get:</p>
<pre><code>assignment_expression COMMA assignment_expression COMMA assignment_expression COMMA assignment_expression
</code></pre>
<p>Which you should now be able to write with a PEG rule easily! Like so:</p>
<pre><code>expression = { assignment_expression ~ (COMMA ~ assignment_expression)* }
</code></pre>
<p>Way better and without left-recursion! :) So I spent pretty much a whole afternoon trying to remove
left-recursions (and some rules were really nasty). I even have one remaining rule I’ll have to cut
like a surgeon later to make it fully available (I had to do the same thing with <a href="https://crates.io/crates/nom">nom</a>; it’s the
rule allowing <em>postfix expressions</em> to be used as <em>function identifiers</em>; issue
<a href="https://github.com/phaazon/glsl/issues/45">here</a>).</p>
<h1>Is it really a parser?</h1>
<p>Then I started to play with those generated <code>Rule</code> types… wait, what?! <em>The</em> generated <code>Rule</code> type?
Ok so that was my first surprise: the grammar knows how the rules are (deeply) nested but <a href="https://crates.io/crates/pest">pest</a>
outputs a single <code>Rule</code> type with all the rules laid down as flat variants for that <code>Rule</code>! This was
at first very confusing for me as I thought I’d be visiting freely through the <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a> – because this
is what a grammar actually defines. Instead, you must ask your <code>Parser</code> type which rule you want
to match against. You get a <code>Pairs</code>, which is an iterator over <code>Pair</code>s.</p>
<p>A <code>Pair</code> defines a single token that is defined by its starting point and ending point (hence a
pair… yeah I know, it’s confusing). A <code>Pair</code> has some methods you can call on it, like <code>as_str()</code> to
retrieve its string slice in the (borrowed) input of your parser. Or <code>into_inner()</code>, giving you a
<code>Pairs</code> that represent the matched inner tokens, allowing you to eventually visit through the tree
of tokens.</p>
<p>I have two problems with that.</p>
<h2>My unreachable goals</h2>
<p>First, because <a href="https://crates.io/crates/pest">pest</a> doesn’t fully use the grammar information <em>at type-level</em>, you lose the one
crucial information: the actual topology of your grammar in the type system. That means that every
time you will ask for a given <code>Pair</code>, you will have to call <code>as_rule()</code> on it an pattern-match on
<strong>the whole list of variants that compose your grammar!</strong> In all situations, you will only need to
match against a very few of them and will discard every others. And how do you discard them? –
you <em>know</em> those are impossible because the very definition of the grammar? You use
<code>unreachable!()</code>, which I find almost as unsafe and ugly as using <code>unimplemented!()</code>. I know
<code>unreachable!()</code> is required in certain situations, but having it spawn in a parser using the public
API of <a href="https://crates.io/crates/pest">pest</a> makes me gnash my teeth. Example of code I’m currently writing:</p>
<pre><code>fn parse_type_specifier_non_array&lt;'a&gt;(pair: Pair&lt;'a&gt;) -&gt; Result&lt;syntax::TypeSpecifierNonArray, String&gt; {
  let inner = pair.into_inner().next().unwrap();

  match inner.as_rule() {
    Rule::struct_specifier =&gt; {
      // …
    }

    Rule::identifier =&gt; {
      let identifier = inner.as_str();

      match identifier {
        "void" =&gt; Ok(syntax::TypeSpecifierNonArray::Void),
        "float" =&gt; Ok(syntax::TypeSpecifierNonArray::Float),
        "double" =&gt; Ok(syntax::TypeSpecifierNonArray::Double),
        // …
        _ =&gt; Ok(syntax::TypeSpecifierNonArary::TypeName(identifier.to_owned())
      }
    }

    _ =&gt; unreachable!()
  }
}
</code></pre>
<p>And I reckon I will find that pattern a lot in my next few hours / days of writing those functions.</p>
<h2>Lexing and parsing are two different concepts</h2>
<p>That <code>parse_type_specifier_non_array</code> function of mine also pinpointed another issue I had: initially,
I wrote that logic in the <code>type_specifier_non_array</code> rule in the PEG grammar, like:</p>
<pre><code>type_specifier_non_array = {
  "void" |
  "float" |
  // …
  identifier
}
</code></pre>
<p>And quickly came to the realization that the <code>identifier</code> alternative would get <em>muted</em> very often
by the lexemes alternatives. Imagine two strings, <code>"int x = 3;"</code> and <code>"interop x = …;"</code>. The first
type will get the right <code>TypeSpecifierNonArray::Int</code> AST representation and the second string… will
get the same because the <code>"int"</code> rule will fire first, leaving some unmatched text without any
sense (<code>"erop x = …;"</code>).</p>
<p>What that means is that we cannot express this rule this way with PEG. I’ve been told <a href="https://crates.io/crates/pest">pest</a> should
now support the <code>!</code> operator that can be used to encode something that <em>must not be parsed</em>. But
that would just slightly hide the problem: there is no way in the PEG rules to actually do parsing
transformation. What <a href="https://crates.io/crates/pest">pest</a> really is here is a <em>lexer</em>: it generates tokens. Yes, it does place
them in a tree (token tree), but they all remains tokens (strings). And that is when I thought about
the difference between <a href="https://crates.io/crates/pest">pest</a> and <a href="https://crates.io/crates/nom">nom</a></p>
<h1>Pest, nom… let’s explain</h1>
<p><a href="https://crates.io/crates/nom">nom</a> is a parser combinator. That means you can build bigger parsers by combining small parsers.
The correct term to talk about <a href="https://crates.io/crates/nom">nom</a> is that it’s a <a href="https://en.wikipedia.org/wiki/Scannerless_parsing">scannerless parser</a>: it doesn’t require to
generate tokens prior to do parsing and prefers to do <strong>both at the same time</strong>. <a href="https://crates.io/crates/nom">nom</a> parsers are
typically implemented using macros like <code>preceded!</code>, <code>delimited!</code>, <code>take_until!</code>, <code>tag!</code>, <code>value!</code>
and <code>do_parse!</code>, allowing for matching (lexing) slices of bytes / chars <em>and</em> parsing them to actual
values with the type of your choice.</p>
<p>However, <a href="https://crates.io/crates/pest">pest</a> relies on a PEG file, representing the formal grammar of a language to tokenize.
That lexer phase takes place and has to be able to tokenize the whole input before giving hand back.
I’m not sure when I say this (but I’m pretty convince it’s the case): <a href="https://crates.io/crates/pest">pest</a> doesn’t support
streaming input, since it needs to eat the special rule <code>EOI</code> – End Of Input – or eat a rule error
(to succeed with the previous rule or propagate the error upwards) before returning. <a href="https://crates.io/crates/nom">nom</a> can be
used to eat streams of bytes, though.</p>
<p>And then, you have two choices with <a href="https://crates.io/crates/pest">pest</a>:</p>
<ol>
<li>Either you decide to expose the tokens as a token tree, in which case the AST contains string
slices, completely left to be parsed.</li>
<li>Either, like me, you have your own AST representation and you will have to write the actual
parser over <a href="https://crates.io/crates/pest">pest</a>.</li>
</ol>
<p>I’m currently experimenting with (2.). The <code>type_specifier_non_array</code> rule is one of the biggest one
in my grammar so maybe I’ve already done the worst case but I’m fearing I’ll have long nights of
coding before ending up with a benchmark <a href="https://crates.io/crates/pest">pest</a> vs. <a href="https://crates.io/crates/nom">nom</a>.</p>
<h1>Call to contributions</h1>
<p>In the meantime:</p>
<ul>
<li>I’ve written a very simple SPIR-V transpiler (that uses <a href="https://crates.io/crates/shaderc">shaderc</a> behind the hood). If you’re
looking for an easy contribution, I’m looking for someone to test the feature branch holding
the implementation, cleaning it, writing tests for it and merging it to <code>master</code>.
<a href="https://github.com/phaazon/glsl/issues/21">Issue here</a>.</li>
<li>I’m looking for a way to encode <code>#define</code> and other preprocessors pragmas. I’m taking
contributions (implementations, ideas) to fix that problem.
<a href="https://github.com/phaazon/glsl/issues/39">Issue here</a>.</li>
<li>If you want to start working on refactoring the AST a bit (it was designed based on the Khronos
grammar rules… which are quite ugly :P), please feel free to open an issue. I will eventually
do it anyway sooner or later, but you know… :)</li>
<li>A lot of the types in the <code>syntax</code> module (the AST types) don’t have <code>new</code> methods. Those
methods are important to me as they enable to bypass some limitation from the current
<code>glsl-quasiquote</code> implementation that doesn’t allow variable interpolation.</li>
<li>Anything else! Feel free to contribute!</li>
</ul>
<p>Thanks for having read through, and keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 17 Nov 2018 05:00:00 GMT</pubDate></item><item><title>Moving away from PR/MR workflow</title><link>https://strongly-typed-thoughts.net/blog/aerc-2024</link><description><![CDATA[<h1>Moving away from PR/MR workflow…</h1>
<p>In the past two years, many projects of mine got more and more contributions.
Some of those projects, such as <strong>This Week in Neovim</strong> (that I gave away),
<code>kak-tree-sitter</code> and others received many contributions. As I reviewed
contributions, I realized that I do not really enjoy the tools used to implement
the process. That is, GitHub. Moreover, I have a special thing against
Microsoft (in)famous way of doing things in (c.f. <a href="https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish">EEE</a>). So, why is it such a
big deal to me, and what is the alternative?</p>
<h2>What’s wrong with git host platforms (GitHub, GitLab, etc.)</h2>
<p><code>git</code> (I don’t think I need to explain what it is, right?) was created by Linus
Torvalds (again, I don’t think I need to present him) to solve the <a href="https://en.wikipedia.org/wiki/BitKeeper#BitKeeper_and_the_Linux_Kernel">BitKeeper
dispute</a>. Back then, people were using CVS or SVN, and something like BitKeeper
was a centralized place where the kernel was developped.</p>
<p>In 2005, after the decision to stop providing free BitKeeper copies to the
kernel community, Linus created <code>git</code>. He created <code>git</code> to provide the free and
open-source software community with a versioning tool working on a
decentralized model. Indeed, with <code>git</code>, teams can organize the way they want
and exchange patches and commits without having to depend on a third party.</p>
<p>For instance, if Alice and Bob want to work on a project together and version
it via <code>git</code>, they do not need to rely on <em>anyone else</em> but themselves. They can
send commits to each other with <code>git push</code> or <code>git pull</code> (granting they
configure their <code>ssh</code> correctly). They can use their university server to host
a bare git repository and synchronize there. They can exchange patches by mail.
Etc.</p>
<p>Now, something else they could do would be to use GitHub. Or GitLab. Or
BitBucket. In the end, it’s the option mentioned above: a repository always up
for people to send commits to and receive commits from. However, I do see one
<em>weird</em> pattern here. Using GitHub, for instance (but really, it’s the same for
most others) is basically the same as taking a decentralized tool and putting it
in jail. If someone wants to contribute to Alice and Bob project, they have to
create a GitHub account, and abide by the GitHub rules. More interesting, GitHub
is not a free and open-source project anymore. It’s owned by Microsoft. Alice
and Bob projects are physically stored on a Microsoft server. Given the history
of Microsoft in the FOSS world, I can imagine how it would cause a moral
problem.</p>
<p>Another point that is specific to GitHub: Copilot. I think Copilot is FOSS
abomination. The reason is that, even though your code is under a copyleft — or
even a copyright, <a href="https://docs.github.com/en/site-policy/github-terms/github-terms-of-service">GitHub terms of service</a> is pretty explicit about the hosted
content: they can use it to improve their own products.
<a href="https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#d-user-generated-content">This section</a>
for instance. I don’t know about you, but to me, it’s grey zone there. I do not
like the idea that my code can be “analyzed” by Microsoft to enhance Copilot.
All of my projects are licensed under a copyleft (BSD-3) that mentions that my
name should be mentioned when my code is used, and other clauses that I think
are violated by this Copilot horror.</p>
<p>And I’m not even talking about <a href="https://www.microsoft.com/en-us/store/b/copilotpro">Microsoft Copilot Pro</a>, which is a product. That
you have to pay for. That is made from public, free and open-source
contributions from millions of projects. <em>Fuck that</em>.</p>
<p>Finally, those platforms often host many different things that people will start
using and highly depend on. For instance, bug tracker, wikis, project
management, etc. Even though this is not the main problem to me, I like using
something that was made to solve a single problem. Having a giantic platform
solving basically “How to do software development” is something that now sounds
like a red flag to me. What happens when you decide to move on? Well, you have
to move <em>everything</em>. And it’s my case today.</p>
<h2>Going back to a decentralized way</h2>
<p>Something like two years ago, someone on IRC praised the email-based git
workflow. Wait, <em>email</em>-based? In 2022? Isn’t that a bit too hardcore? Well,
there is one really big misconception about emails: all-in-one providers.</p>
<p>Like many other people, I do use a webmail. If it’s not GMail, it’s Outlook or
something else. GMail is hard not to notice, because in the professional world,
every companies use it. It’s easy for them to manage and you get all the
important things at once — swarm of newcomer welcoming emails, meetings, various
meetups, etc. All in one. The problem with that is that it requires you to go
to your browser. And the online experience is honestly pretty bad — it always
has been to me. I try to spend as few times as possible in GMail, because I
don’t really like the interface.</p>
<p>So how can someone use emails to work with <code>git</code>? Well, from the beginning,
<code>git</code> was made with the email workflow on mind. The idea is simple:</p>
<ol>
<li>You make your changes exactly the same way as you normally do.</li>
<li>When you have your commits and you are ready to share your contributions with
others for review, instead of going to a centralized platform to push your
branch to and then open a PR, you simply use the <code>git send-email</code> command —
or <code>git format-patch</code> and manually send the patch, but really, you shouldn’t
have to do that. You typically send that email to a development mailing list.</li>
<li>People discuss the patch by replying to the emails.</li>
<li>Either you send other patches via other mails, or a maintainer eventually
approves and <em>applies</em> (forget about <em>merging</em> here!) the patches with
<code>git am</code>.</li>
<li>Done.</li>
</ol>
<h3>Misconception 1: reviewing is hard</h3>
<p>The main misconception part is about the <em>review</em> part. On GitHub, reviewing is
nice, because you can comment in line. In emails… well, it’s exactly the same,
and even better. What people usually do is to reply to your email by quoting it,
and dissect the email section by section. For instance, imagine I have a project
with a <code>README.md</code> file containing a single line:</p>
<pre><code class="language-markdown">Hello. My name is Dimitri and this is a README.
</code></pre>
<p>Let’s say I modify that file to hold this content instead:</p>
<pre><code class="language-markdown"># Hello world!

My name is Dimitri and this is a README.
</code></pre>
<p>I make a commit with <code>git commit -am "Add hello section to README.md."</code>. When
using <code>git send-email</code> / <code>git format-patch</code>, it will generate something like
this patch:</p>
<pre><code>From 020d9085a35d6c34bc1b7e86b78f257c4d556bbb Mon Sep 17 00:00:00 2001
From: Dimitri Sabadie &lt;hadronized@strongly-typed-thoughts.net&gt;
Date: Thu, 30 May 2024 14:28:11 +0200
Subject: [PATCH] Add hello section to README.md.

It looks better to me and allows to demonstrate the email workflow a bit more.
---
 README.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 72ae80f..70341da 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
-Hello. My name is Dimitri and this is a README.
+# Hello world!
+
+My name is Dimitri and this is a README.
--
2.45.1
</code></pre>
<p>The first 4 lines are part of the email, so when receiving your email, the
client of the people reading the mailing list should format all that correctly.
The rest is the email content! Someone could reply something like:</p>
<pre><code>&gt; -Hello. My name is Dimitri and this is a README.
&gt; +# Hello world!

Thank you, we needed that header.

&gt; +My name is Dimitri and this is a README.

May you add more info to include other maintainers as well? Alice and Bob could
provide more details there.

Cheers,
Dimitri
</code></pre>
<p>The author of the patch can then iterate and send another email (with the <code>-v2</code>
annotation, because it would be the next version; <code>-v3</code> for the next iteration,
etc.). The advantage here is that someone can reply to that first review by just
discussing the first part, quoting it:</p>
<pre><code>&gt; &gt; -Hello. My name is Dimitri and this is a README.
&gt; &gt; +# Hello world!
&gt;
&gt; Thank you, we needed that header.

I think we should also include a sub-header describing something else.
</code></pre>
<p>While at the same time, someone can discuss the second part of the change:</p>
<pre><code>&gt; &gt; +My name is Dimitri and this is a README.
&gt;
&gt; May you add more info to include other maintainers as well? Alice and Bob could
&gt; provide more details there.

Yes, like email addresses and PGP signatures, please.
</code></pre>
<p>Those will create a <em>tree</em> of emails, where each sub-thread is scoped to a
specific part of your patch. Such a feature is not possible in GitHub or any
other centalized platform.</p>
<p>So the only important thing is to use a good client. I personally use <a href="https://aerc-mail.org/">aerc</a>,
along with some other tools to synchronize IMAP maildirs and send SMTP messages.
The configuration is not that hard once you get the hang of it.</p>
<h3>Accounts?</h3>
<p>Sending an email to contribute is nice, but where to send the email? Well, it
depends on the project. For that, I think that every open-source projects should
have a <code>CONTRIBUTING.md</code> file explaining where to send patches. I use
<a href="https://sourcehut.org/">SourceHut</a>, which provides me a place to host my repository and mailing lists.
The rest is done entirely outside of SourceHut. Especially, even though you can
create an account on SourceHut, <strong>you don’t have to</strong>. You don’t have to in
order to:</p>
<ul>
<li>Contribute code. You can just send the patches to the mailing list. <strong>You
don’t need an account!</strong>.</li>
<li>Open a bug report or a feature request. You can just send your bug report or
feature request to… the bug report mailing list or the feature request mailing
list! <strong>No account needed!</strong>.</li>
<li>Etc. etc.</li>
</ul>
<p>That part here makes me smile, because it makes so much more sense to me. People
can contribute with their email address. They do not need to engage with the
platform to start contributing, which is nice and more <em>decentralized</em> to me.
One can even send a patch to a maintainer directly if they really want.</p>
<h3>Authorship</h3>
<p>I think the problem with the email-based workflow is that you don’t exchange
<em>commits</em> anymore, but <em>patches</em>. There is a slight difference.</p>
<p>A <em>patch</em> is a set of changes to be applied. It contains some metadata, like
the parent commit to apply to, the expected SHA1, some diff-stats, and that’s
pretty much all.</p>
<p>A <em>commit</em> is a patch with more metadata, like the committer / author
identities, a commit message, the parent commit, a signature, etc. etc.</p>
<p>A commit always refers to at least one parent (unless it was the first commit).
When you send a patch over email, the maintainer that applies the patch does it
with <code>git am</code>, which turns the email content into a commit. The message commit,
for instance, is taken from the mail body. However, there is one information
<strong>that is lost during the process</strong>: the signature.</p>
<p>Indeed, when you work on your local copy of the repository, you might sign your
commits with your PGP key. When you decide to send the commits as patches to a
development list, all of the metadata from the commits are ripped. That means
that the signature is simply lost.</p>
<p>For most people, in terms of security, it’s not that bad. Patches are accepted
from emails, and you can (you should!) sign your email with your PGP key. As a
maintainer, my email client (<code>aerc</code>) shows me the signature. If you don’t sign
your email, I cannot certify it’s you sending me the email. Most people don’t
seem to care that much about authorship, but I do. I think it’s important to be
able to retrace that a patch which identity is <code>foo@bar.net</code> was really provided
by that person. A signature doesn’t prove you wrote the patch, but it proves
you submitted it, the same way that signing a commit means that you integrated
that commit into the repository.</p>
<p>Currently, there is no way to do that on the email workflow and I find that a
bit of a problem. There should be an alternative, like manually signing the
patch. The problem is that, if the maintainer has to use a three-way merge, it
will invalidate both the parent commit’s SHA1 and the expected SHA1 of the
patch, invalidating the carried signature.</p>
<h3>In the end</h3>
<p>Even though the email workflow has some problems, having the contributions
locally and applying patches locally is a much more enjoyable process. The first
thing is that sending many commits ends up in a nice thread of mails. It’s super
easy to just open the thread locally and apply the patches. The only concern I
have is authorship, but people told me I might be the only person caring about
that — am? All in all, this email-based workflow allows me to do everything
from my terminal, and even <em>tickets</em> (todos in sourcehut) and bug reports /
feature requests are done via email. It just feels good.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 05 Jun 2024 10:12:00 GMT</pubDate></item><item><title>al 0.1.1.2 was shipped</title><link>https://strongly-typed-thoughts.net/blog/al_0.1.1.2</link><description><![CDATA[<h1>al 0.1.1.2</h1>
<h2>Changes</h2>
<p><a href="http://hackage.haskell.org/package/al-0.1.1.2">al 0.1.1.2</a> was shipped. It
includes several improvements, among them:</p>
<ul>
<li>fix CPU architecture issues ;</li>
<li>since version 0.1.1, the <code>stdcall</code> flag is available ;</li>
<li><code>alcIsExtensionSupported</code> was renamed to <code>alcIsExtensionPresent</code> as the
former just doesn’t exist – sorry for the typo.</li>
</ul>
<p>The <code>stdcall</code> flag might be a great ally for people compiling on a 32-bit
Windows. For people on 64-bit Windows, the default is sufficient – and for
UNIX systems, you don’t have anything special to do.</p>
<p>I’m also looking for hackers to test the library on the most OS as possible. I
have issues with it (see <a href="http://stackoverflow.com/questions/28829976/ffi-and-static-libraries-used-in-application">this</a>)
and I’d like to hear from people. Even though I have that issue, al compiles
well and runs great in <strong>ghci</strong>, which is weird regarding the fact running an
application compiled with al silently crashes at startup.</p>
<h2>About paths…</h2>
<p>That’s a nasty issue I don’t really know how to correctly fix. Up to now,
default OpenAL 1.1 SDK installation are detected for Windows. For people with
custom installation and other systems, <strong>you have to pass the path of the SDK by
hand</strong>. I know, it’s a pain in the ass, but I don’t want to depend on tools like
<code>pkg-config</code> as that tool is not available everywhere – getting it on Windows is
not that simple.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 03 Mar 2015 00:00:00 GMT</pubDate></item><item><title>Trying to tackle the orphans problem</title><link>https://strongly-typed-thoughts.net/blog/orphans-problem</link><description><![CDATA[<h1>Foreword</h1>
<p>The <a href="https://wiki.haskell.org/Orphan_instance#Description">orphan instance problem</a> is a well known problem that I’ve discovered while designing code in
Haskell years ago. According to the Haskell wiki:</p>
<blockquote>
<p>An orphan instance is a type class instance for class C and type T which is neither defined in
the module where C is defined nor in the module where T is defined.</p>
</blockquote>
<p>If it’s a bit overwhelming for you, here it is rephrased: this problem happens whenever you try to
create an instance of a typeclass <code>C</code> in a module that is not the module where <code>C</code> is defined for a
type <code>T</code> while you’re not in the module that actually introduces that <code>T</code> type. Since it’s not
possible to pick an instances by hand, GHC disables you from writing several instances for a same
combination of typeclass and types.</p>
<blockquote>
<p>GHC actually has a workaround for the problem: the <code>{-# OPTIONS_GHC -fno-warn-orphans #-}</code> pragma
annotation (or the <code>-fno-warn-orphans</code> compiler switch). However, this is not recommended if you
can find another way because you expose yourself to ambiguities there.</p>
</blockquote>
<p>Because Rust also has types and typeclasses (traits), it doesn’t escape the problem very much. You
still have orphan instances in Rust (called <a href="https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence">Orphan Rules</a>). There’re scoped to crates though, not
modules.</p>
<p>In this blog entry, I want to explore a specific problem of orphans and how I decided to solve it in
a crate of mine. The problem is the following:</p>
<blockquote>
<p><strong>Given a crate that has a given responsibility, how can someone add an implementation of a given
trait without having to use a type wrapper or augment the crate’s scope?</strong></p>
</blockquote>
<h1>A harsh solution: type wrappers</h1>
<p>The typical workaround to this problem is to use a type wrapper. It works by encapsulating the
origin type into another layer of typing so that the compiler recognize it as a complete different
type, allowing you to implement whichever traits you want. Rust has a nice feature added to that: if
you implement the <code>Deref</code> and <code>DerefMut</code> traits, you’re given access to all the implementations of
the source type via <a href="https://doc.rust-lang.org/std/ops/trait.Deref.html">deref rules</a>, which is pretty sick. This works if you’re writing a binary,
because no one will ever use your type wrapper, only you (and your team mates, but you’ll never
<em>publish</em> the type so you’re breaking the orphan problem). However, what happens if you’re writing
a library?</p>
<p>You will expose a type that is not the source one, forcing people to wrap it again if they want to
implement another feature. If your type has a very different form from the source one, it’s okay.
However, if you want to stick to the original semantics and use cases, people might get confused,
especially whenever they will write functions that accept as arguments the source type.</p>
<h1>The problem explained in <code>splines</code></h1>
<p><a href="https://crates.io/crates/splines"><code>splines</code></a> is a crate that provides you with spline interpolation. You can interpolate values in
several dimensions with several kind of interpolators (step, linear, cosine, etc.).</p>
<p>Clearly, the scope of <a href="https://crates.io/crates/splines"><code>splines</code></a> is to provide math and curves types and functions, nothing more.
However, I use them in <a href="https://crates.io/crates/spectra"><code>spectra</code></a>, a demoscene crate of mine, in which I need them to implement
some <a href="https://crates.io/crates/serde"><code>serde</code></a> traits in order to serialize and deserialize them. Are you starting to see the
problem?</p>
<p>When I thought about where I should write the code to allow serialization / deserialization, I came
across the realization that writing it directly in <code>splines</code> would add another set of dependencies
for <em>everyone</em>, even people not using the serialization part of it. I was a bit unsatisfied with
that. So I thought about <em>“Why not just adding another crate, like <code>splines-serde</code>?”</em> Obviously,
that doesn’t work because of the orphan rules: you cannot write an implementation of a trait in a
different crate than where the type or the trait is defined. Meh.</p>
<h1>A limited solution yet a solution: feature-gated impls</h1>
<p>So I came with an idea, that is not perfect, but fits my needs pretty well. Rust has that
interesting concept of <a href="https://doc.rust-lang.org/book/first-edition/conditional-compilation.html">features</a>, allowing for conditional compilation. <code>cargo</code> supports them in
the manifest and even allows you to declare dependencies as optional. The combination of both
features and optional dependencies is key here.</p>
<h2>Step 1: the manifest</h2>
<p>Let’s have a look at the <a href="https://github.com/phaazon/splines/blob/3cd65dce54510e289e85a1a680a60f06abff5f73/Cargo.toml">current <code>Cargo.toml</code> manifest</a>
in order to get how it’s done – I’ll slice the upper part and only show the features-related part:</p>
<pre><code class="language-toml">[features]
default = ["std", "impl-cgmath"]
serialization = ["serde", "serde_derive"]
std = []
impl-cgmath = ["cgmath"]

[dependencies.cgmath]
version = "0.16"
optional = true

[dependencies.serde]
version = "1"
optional = true

[dependencies.serde_derive]
version = "1"
optional = true
</code></pre>
<p>Here, you can see that we are using <em>default features</em>. That means that if you depend on <code>splines</code>
in simple ways (for instance, by just giving a version string, i.e. <code>splines = "0.2"</code>), you’ll get
those features enabled by default. In the <code>splines</code> case, you’ll get the <code>"std" </code> and
<code>"impl-cgmath"</code> features by default.</p>
<p>Looking at the <code>"std"</code> feature, you can see that it doesn’t depend on anything else. That feature
will just make the whole library use the <code>std</code> crate. If you disable it, you can compile <code>splines</code>
with <code>no_std</code>.</p>
<p>The <code>"impl-cgmath"</code> has a dependency on <code>cgmath</code> and you can see that it depends on the <code>"0.16"</code>
version and that it’s optional. What it means is that if you disable default features you will not
depend on <code>cgmath</code> anymore and thus, you will not even download / compile the dependency.</p>
<p>If you look closely at that manifest, you also see a feature that must be set explicitly:
<code>"serialization"</code>. That feature depends on both <code>serde</code> and <code>serde_derive</code>, adding support for the
serialization code we talked earlier.</p>
<p>All of that is great and cool but how do we write the <code>impl</code>s based on that manifest?</p>
<h2>Step 2: the conditional code</h2>
<p>There’re no rules here and people will give different advices on how you should write conditional
code. I tend to remain simple as long as the project is not too complex. <code>splines</code> is a pretty
simple and small project, so let’s get things straight!</p>
<p>The first thing to do is to know how we can access the features in Rust code. This is easy: via
<em>attributes</em>. Attributes in Rust are enclosed in square brackets preceded by a dash and bang –
<code>#![]</code>. There’s an interesting attribute: <code>cfg()</code>. It gives access to the configuration of the
project, for short. It may take several kind of parameters but we’re interested in only one:
<code>feature</code>. The syntax is the following:</p>
<pre><code class="language-rust">#![cfg(feature = "name_of_feature")]
</code></pre>
<p>This oneliner will evaluate the following block whenever the <code>"name_of_feature"</code> is set. If you want
to evaluate a block if a feature is not set, you can use the <code>not()</code> combinator:</p>
<pre><code class="language-rust">#![cfg(not(feature = "name_of_feature"))]
</code></pre>
<p>It’s a bit ugly, but it does the job.</p>
<p>One final and cool attribute we’ll be using: <code>cfg_attr()</code>. It takes two arguments: the first one is
a regular parameter you’d give to <code>cfg</code> that gets substituted as a boolean expression (<code>feature(…)</code>,
<code>not(feature(…))</code>, etc.) and the second one is an attribute that will get set whenever the former
argument gets substituted successfully. For instance, this:</p>
<pre><code class="language-rust">#![cfg(not(feature = "std"))]
#![no_std]
</code></pre>
<p>Can be rewritten more elegantly as this:</p>
<pre><code class="language-rust">#![cfg_attr(not(feature = "std"), no_std)]
</code></pre>
<p>For the rest, as <code>#[]</code> applies to the direct item after it, it’s easy to write conditional
<code>extern crate</code> for instance:</p>
<pre><code class="language-rust">// on no_std, we also need the alloc crate for Vec
#[cfg(not(feature = "std"))] extern crate alloc;

#[cfg(feature = "impl-cgmath")] extern crate cgmath;

#[cfg(feature = "serialization")] extern crate serde;
#[cfg(feature = "serialization")] #[macro_use] extern crate serde_derive;
</code></pre>
<p>The <code>#[cfg_attr(…)</code> is even nicer when wanting to insert attributes on a type definition, as with
the <code>Key</code> type:</p>
<pre><code class="language-rust">#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub struct Key&lt;T&gt; { … }
</code></pre>
<p>If you want <em>static ifs</em> in the actual implementation, you can cheat and use blocks as they’re
items!</p>
<pre><code class="language-rust">Interpolation::Cosine =&gt; {
  let cp1 = &amp;keys[i+1];
  let nt = normalize_time(t, cp0, cp1);
  let cos_nt = {
    #[cfg(feature = "std")]
    {
      (1. - f32::cos(nt * consts::PI)) * 0.5
    }

    #[cfg(not(feature = "std"))]
    {
      use core::intrinsics::cosf32;
      unsafe { (1. - cosf32(nt * consts::PI)) * 0.5 }
    }
  };

  Some(Interpolate::lerp(cp0.value, cp1.value, cos_nt))
}
</code></pre>
<p>And finally, the one we were looking for to solve our orphans problem:</p>
<pre><code class="language-rust">#[cfg(feature = "impl-cgmath")]
impl Interpolate for Vector2&lt;f32&gt; { … }
</code></pre>
<p>That <code>impl</code> will only exist if the <code>"impl-cgmath"</code> feature is set! Sweet!</p>
<p>I have another ultra cool use of attributes used along with existential <code>impl Trait</code>, but that’ll be
for another blog entry.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 20 Sep 2018 12:30:00 GMT</pubDate></item><item><title>Introducing pest into glsl and hindsight about nom vs. pest (part 2)</title><link>https://strongly-typed-thoughts.net/blog/glsl-pest-part-2</link><description><![CDATA[<p>This is the second article about my experience at supporting <a href="https://crates.io/crates/pest">pest</a> in my <a href="https://crates.io/crates/glsl">glsl</a> crate – without,
for now, removing the <a href="https://crates.io/crates/nom">nom</a> parser.</p>
<blockquote>
<p>You can read the first article
<a href="https://phaazon.net/blog/glsl-pest-part-1">here</a>.</p>
</blockquote>
<h1>RY: DRY without D</h1>
<p>I feel like I’m either doing something wrong… or that something is wrong with using <a href="https://crates.io/crates/pest">pest</a> to build
typed <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a>s. I spent a whole day writing the GLSL450 <a href="https://en.wikipedia.org/wiki/Parsing_expression_grammar">PEG</a>. After having the compiler stop failing
with left-recursion errors, I was finally able to test my crate!… or was I?</p>
<p>First thing first, I decided to write tests to see whether I can recognize <em>identifiers</em>. I’m a
TDD enthusiast – don’t get the idea twisted: writing tests is boring; but writing tests will save
your life and prevent small and cute kittens joining a project from drowning (true story).</p>
<p>I have a PEG rule for that, <code>identifier</code>. So the idea is the following:</p>
<pre><code>// we set the error type to String for that first version
fn parse_identifier&lt;'a, S&gt;(input: S) -&gt; Result&lt;syntax::Identifier, String&gt;
where S: Into&lt;&amp;'a str&gt; {
  // here we get a set of pairs that match the identifier
  let pairs = Parser::parse(Rule::identifier, input.into()).map_err(|e| format!("{}", e))?;

  // since only a full string identifier would match, we can just simply ask to retrieve the string
  // that composes the pair
  Ok(pairs.as_str().to_owned())
}
</code></pre>
<p>As you can see, we have lost the grammar information that states that the <code>identifier</code> rule outputs
a single string – see in the code above how we get a <code>Pairs</code> object, assigned to <code>pairs</code>. This is
my first concern: the <code>Rule</code> type should contain information about what kind of values it was
matched against.</p>
<p>A more drastic example is the <code>primary_expression</code> rule, that defines expressions that can be either
an <code>identifier</code>, a <code>float_constant</code>, <code>int_constant</code>, <code>bool_constant</code> or any general expression with
parenthesis around. Its parser code would look like this:</p>
<pre><code>fn parse_primary_expression&lt;'a, S&gt;(input: S) -&gt; Result&lt;syntax::Expr, String&gt;
where S: Into&lt;&amp;'a str&gt; {
  let pairs = Parser::parse(Rule::primary_expression, input.into()).map_err(|e| format!("{}", e))?;

  // we know we only have one expression, so this statement seems a bit out of nowhere here
  let pair = pairs.next().unwrap();

  // now, we know the primary expression is correctly tokenized, so we’re interested in “what’s
  // inside”; here, we HAVE to look at the grammar again to check what are the possible variants;
  // only one possible, let’s take the next pair then as well…
  let inner = pair.into_inner().next().unwrap();

  // here the pair represents the “real” and “useful” sub-rule that was matched; in order to write
  // that match block, we also have to look at the grammar to see all the possible variants
  match inner.as_rule() {
    Rule::identifier =&gt; {
      // the pair represents an identifier… we can just turn it to string and return it
      Ok(syntax::Expr::Variable(inner.as_str().to_owned())) // (1.)
    }

    Rule::float_constant =&gt; {
      // the pair represents a float constant; let’s just parse it
      let f = inner.as_str().parse().unwrap();
      Ok(syntax::Expr::FloatConstant(f))
    }

    Rule::etcetc. =&gt; {
      // etc. etc.
    }

    _ =&gt; unreachable!() // eeew (2.)
  }
}
</code></pre>
<p>As you can see, we have several problems here</p>
<ol>
<li>We need to write our parsers taking as input <code>Pair</code> so that we can compose them… that strangely
resembles how the <a href="https://crates.io/crates/nom">nom</a> parser is actually written… :)</li>
<li>We just repeat the actual grammar! The whole PEG file is just a way to tell our lexer (<a href="https://crates.io/crates/pest">pest</a>)
how to recognize tokens and how to store them in a tree. That’s all. All the parsing analysis
must be done by <em>repeating ourselves</em>.</li>
<li>Whenever we decide to change the grammar by, for instance, modifying the content of a rule
(without changing its name), we’re lost. Done. Terminus. <em>Destination fucked</em>. Destination
sneakily stabbed in the back. The code will still compile but now you will get a runtime error
linked to <code>Result::unwrap</code>, a <code>Parse::parse</code> failure, etc.</li>
</ol>
<p>Maybe I’m plain wrong and that there’s an easier way to do that, but I’ve been writing the parsers
of only a few rules (on more than fifty) and I already cringe.</p>
<h1>Two-stage parsers</h1>
<p>All of this brings a brand new problem: since we’re smart developers and want to write the most
reusable code, we want to be able to write the parser of <code>primary_expression</code> once and reuse it in
other parsers that might need it – like <code>postfix_expression</code>, <code>expression</code>, etc. The current code
that consumes <code>Into&lt;&amp;str&gt;</code> doesn’t allow this as <a href="https://crates.io/crates/pest">pest</a> parses to <code>Pairs</code>. So let’s just write our
functions to take <code>Pair</code> as inputs!</p>
<p>But… now we don’t have a proper public facing interface for our crate. Surely, I don’t want people
to even see that <a href="https://crates.io/crates/pest">pest</a> is used – they’ll just see it as a dependency, but I don’t want any <a href="https://crates.io/crates/pest">pest</a>
symbols in my public interface.</p>
<p>That problem can be solved by introducing a trait, <code>Parse</code>, for instance, that has the right
parsing signature:</p>
<pre><code>/// Class of types that can be parsed.
pub trait Parse: Sized {
  /// Parse an item from a string.
  ///
  /// Store errors as strings for convenience for now.
  fn parse&lt;'a, S&gt;(input: S) -&gt; Result&lt;Self, String&gt; where S: Into&lt;&amp;'a str&gt;;
}
</code></pre>
<p>Then, to support a new syntax symbol, we must:</p>
<ol>
<li>Write a <code>Pair</code> parser.</li>
<li>Implement our <code>Parse</code> trait for this type, using the <code>Pair</code> parser from just above.</li>
<li>Eventually reuse the <code>Pair</code> parser in the implementations of other <code>Pair</code> parsers if
needed.</li>
</ol>
<h1>Is it the right way to go?</h1>
<p>I must say: I’m astonished by the complexity and verbosity – and dangerosity! – of all of this. This
is “on me” because I’ve constrained myself here:</p>
<ul>
<li>I don’t want <a href="https://crates.io/crates/pest">pest</a> to leak any symbol into my public interface.</li>
<li>I want to be able to reuse parsers for a maximum of coherency between parsers – and more
readable parsing code base.</li>
<li>I want my final output to be folded into an AST type of mine. More specifically, I want users of
the <a href="https://crates.io/crates/glsl">glsl</a> crate to be able to <code>parse</code> any type from the grammar / syntax. This is currently the
case with the <a href="https://crates.io/crates/nom">nom</a> parser, that uses a similar trait. To implement the trait, I just defined a
very small <code>macro_rules</code> that simply invokes the required parser for the given type.
<a href="https://github.com/phaazon/glsl/blob/a31b701850dd7966b14cc59f10c8d455c755c9c6/src/parser.rs#L99">Code here</a>.</li>
</ul>
<p>My main problem here is that there’s no way to have <a href="https://crates.io/crates/pest">pest</a> behave as a <a href="https://en.wikipedia.org/wiki/Scannerless_parsing">scannerless parser</a>: you
will always have this two-phases lexer-parser:</p>
<ol>
<li>Run the lexer to tokenize the input string into a tree of tokens.</li>
<li>Run a (type-blind) parser that somehow <em>rediscovers</em> the token tree.</li>
</ol>
<p>I would be okay with this, however:</p>
<ol>
<li>Run the lexer to tokenize the input string into a <em>typed</em> tree of tokens.</li>
<li>Run a full-type-aware parser that visits the token tree by following typed rules.</li>
<li>It would be even easier if I could write the parser code <em>inline</em>, directly in the PEG rule.</li>
</ol>
<p>Once compiled, a <a href="https://crates.io/crates/pest">pest</a> grammar is basically a very big and flat <code>Rule</code> type. If you have this PEG:</p>
<pre><code>WHITESPACE = _{ " " | NEWLINE }

number = { ASCII_DIGIT+ }
op = { "-" | "+" | "*" }
expr = {
  number ~ (op ~ expr)? |
  "(" ~ expr ~ ")"
}
</code></pre>
<p>A possible AST – one I would definitely write – for that would be:</p>
<pre><code>#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Op {
  Minus,
  Plus,
  Mul
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum Expr {
  Number(i32),
  Op(Number, Op, Box&lt;Expr&gt;)
}
</code></pre>
<p>However, <a href="https://crates.io/crates/pest">pest</a> outputs something like this:</p>
<pre><code>enum Rule {
  WHITESPACE, // very useful, thank you!
  number,
  op,
  expr
}
</code></pre>
<p>And implements the <code>Parser</code> trait to yield <code>Pairs</code> according to the variant of <code>Rule</code> you choose to
parse with. Instead, a more practical encoding of rules would be:</p>
<pre><code>struct Rule_number;
impl SuperMegaParser&lt;i32&gt; for Rule_number {}

struct Rule_op;
impl SuperMegaParser&lt;Op&gt; for Rule_op {}

struct Rule_expr;
impl SuperMegaParser&lt;Expr&gt; for Rule_expr {}
</code></pre>
<p>That is, <code>Parser::parse(Rule_expr)</code> wouldn’t yield <code>Pairs</code> anymore, but <code>Expr</code>. In order for this
to be possible, we would need to tell our <a href="https://en.wikipedia.org/wiki/Scannerless_parsing">scannerless parser</a> what to do when matching a rule:</p>
<pre><code>WHITESPACE = _{ " " | NEWLINE } // who cares about whitespaces, seriously? they’re MUTED! :D

number: i32 = { ASCII_DIGIT+ =&gt; |s| s.parse().unwrap() }

op: Op = {
  "-" =&gt; Op::Minus |
  "+" =&gt; Op::Plus |
  "*" =&gt; Op::Mul
}

expr: Expr = {
  number ~ (op ~ expr)? =&gt; |(n, opt_op_expr)|{
    match opt_op_expr {
      Some((op, expr)) =&gt; Expr::Op(n, op, Box::new(expr)),
      None =&gt; Expr::Number(Number(n))
    }
  }|

  "(" ~ expr ~ ")"
}
</code></pre>
<p>This would be perfect to me. And I reckon it’s pretty much what <a href="https://github.com/lalrpop/lalrpop">lalrpop</a> uses.</p>
<h1>Conclusion</h1>
<p>I’ve been feeling on and off about <a href="https://crates.io/crates/pest">pest</a> lately, to be very honest. At first I was amazed at the
PEG file, because, yeah, PEG is lovely to work with. However, I think GLSL450 is a really good
candidate to test a lexer / parser and to that matter, the current vanilla <a href="https://crates.io/crates/pest">pest</a> is a nah to me.
It makes the code terribly bloated and harder to maintain – while it should be easier! – than the
reference <a href="https://crates.io/crates/nom">nom</a> implementation. The very reason to that is that even if I had to write thousands
line of macros calls – yiiiik – with <a href="https://crates.io/crates/nom">nom</a>, those macros are correctly typed. The <code>identifier</code> <a href="https://crates.io/crates/nom">nom</a>
parser is a function taking bytes and outputing… <code>syntax::Identifier</code>. Same thing for all other
parsers.</p>
<p>I had to try <a href="https://crates.io/crates/pest">pest</a>. From my – limited – experience of it, I’d say <strong>it’s definitely not a
parser</strong>. <a href="https://crates.io/crates/nom">nom</a> is – a <a href="https://en.wikipedia.org/wiki/Scannerless_parsing">scannerless parser</a>. <a href="https://crates.io/crates/pest">pest</a> <strong>is a lexer</strong> that does a <em>few parsing</em> work
to sort and fold the lexemes (tokens) into a tree. You can see <a href="https://crates.io/crates/pest">pest</a>’s output as a big regular
acyclic tree holding pairs of pointers (representing tokens in the input source). That’s everything
<a href="https://crates.io/crates/pest">pest</a> gives you. In order to turn that representation into a typed AST, a lot of work is awaiting
you. I’ll take a few hours / days to think about what I should do and work on other projects in the
meantime. I doubt I’ll keep going with <a href="https://crates.io/crates/pest">pest</a> because I feel like I’m going to spend entire days
repeating myself by looking the grammar up to, frustrated, shout on IRC that I’m writing untyped
code while I have static assurances (i.e. remember: the grammar is read at compile-time) that I will
never have to look at the <code>Rule::external_declaration</code> while parsing a <code>Rule::postfix_expression</code>.
And as I might be changing the grammar rule a bit when refactoring / fixing bugs, I really really
don’t plan to pay a visit to <em>destination fucked</em> yet.</p>
<blockquote>
<p>Someone on reddit suggested me to have a look at <a href="https://github.com/pest-parser/ast">pest-ast</a>.
Even if this looks promising, it doesn’t seem finished and I think it should be merged into <a href="https://crates.io/crates/pest">pest</a>
directly when done.</p>
</blockquote>
<p>That’s all for me today. I might try a bit further to see if I find a way to make my <a href="https://crates.io/crates/pest">pest</a>
experience less painful but if I don’t, I might try that <a href="https://github.com/lalrpop/lalrpop">lalrpop</a> cutie in a few hours / days! As
a final note, I don’t want to state that <a href="https://crates.io/crates/pest">pest</a> is bad, I think it’s pretty cool and that it does
its job greatly, but my need (typed AST) might be off its scope. That’s all to note, I’d say. :)</p>
<p>Keep the vibes.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 17 Nov 2018 20:40:00 GMT</pubDate></item><item><title>On public API and private implementation</title><link>https://strongly-typed-thoughts.net/blog/public-api-private-implementation</link><description><![CDATA[<p>In this blog article, I want to explore a problem I’ve been facing from time to time in <a href="https://crates.io/crates/luminance">luminance</a>.</p>
<h1>The manual dispatch problem</h1>
<p>The idea is simple: you are writing a crate and want to expose an API to people. You want them to
know which type they can use with a given operation (let’s call it <code>update</code>). However, the actual
implementation of this update function is not performed directly by your API but is deferred to a
<em>backend</em> implementation. Some people usually like to do that with several crates; in my case, I
really don’t care and let’s think in terms of types / modules instead.</p>
<p>There are several ways to do this. Let’s start with a nobrainer.</p>
<h2>The first attempt: dynamic dispatch</h2>
<pre><code class="language-rust">use std::marker::PhantomData;

// This type serves as to reify typing information at runtime and also serves as “public contract”.
// What it means is that this type gives you all the possible logical types you can use.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Type {
  Int,
  UInt,
  Float,
  Bool
}

// We will use a very small abstraction to represent the update function (and the backend).
// Var&lt;B, T&gt; is just a variable of type T represented in the backend B.
#[derive(Debug)]
pub struct Var&lt;B, T&gt; where B: ?Sized {
  name: String,
  _b: PhantomData&lt;*const B&gt;,
  _t: PhantomData&lt;*const T&gt;,
}

impl&lt;B, T&gt; Var&lt;B, T&gt; {
  // Just create a variable of a given name, whatever its type and backend.
  pub fn new(name: &amp;str) -&gt; Self {
    Var {
      name: name.to_owned(),
      _b: PhantomData,
      _t: PhantomData,
    }
  }
}

impl&lt;B, T&gt; Var&lt;B, T&gt; where B: Backend, T: Interface {
  // Update the value of the variable.
  //
  // For simplicity, I don’t include a mutable reference to the backend, because that is not the
  // topic of this blog article. However, you might want to if you want to be able to, for instance,
  // change the value in a map stored in the backend, for instance. But whatever.
  pub fn update(&amp;mut self, value: &amp;T) {
    B::update(self, value);
  }
}

// The interface (i.e. public API) trait. This serves as a “anything implementing that can be used
// with the API.” It simply associates a Type that is, if you remember, an exhaustive list of types
// that can be used (and reified at runtime).
trait Interface {
  const TY: Type;
}

impl Interface for i32 {
  const TY: Type = Type::Int;
}

impl Interface for u32 {
  const TY: Type = Type::UInt;
}

impl Interface for f32 {
  const TY: Type = Type::Float;
}

impl Interface for bool {
  const TY: Type = Type::Bool;
}

// The backend (i.e. private implementations) trait. An implementation should implement this trait
// to implement the actual update of a variable for a given context / backend. Notice the Interface
// constraint on the T type parameter.
trait Backend {
  fn update&lt;T&gt;(var: &amp;mut Var&lt;Self, T&gt;, value: &amp;T) where T: Interface;
}

impl Backend for () {
  fn update&lt;T&gt;(var: &amp;mut Var&lt;Self, T&gt;, _: &amp;T) where T: Interface {
    // We can reify the type of the variable at runtime thanks to the associated constant value.
    // However, you can see that we have no way to actually inspect the value. This is due to the
    // fact our update function is universally quantified on T: we cannot observe T (i.e. know its
    // exact type) from within the implementation. That might be a problem.
    match T::TY {
      Type::Int =&gt; println!("setting {} to int", var.name),
      Type::UInt =&gt; println!("setting {} to unsigned int", var.name),
      Type::Float =&gt; println!("setting {} to float", var.name),
      Type::Bool =&gt; println!("setting {} to bool", var.name),
    }
  }
}
</code></pre>
<p>Let’s dig in. The idea of this solution is to have a trait, <code>Interface</code>, that is used to create a
set of types that can be used in the API with the <code>update</code> function on the <code>Var</code> type. The
implementation is deferred to a backend via the <code>Backend</code> trait, that contains the interface of
the implementation. Basically, the <code>Var::update</code> function will select at compile-time which
backend to use, that will in its turn <em>observe</em> the type of the variable at runtime — see the
<code>match</code> block. This is not ideal as we will have branching. We would like a better way to do it.</p>
<h2>The second attempt: static dispatch</h2>
<p>Instead of dynamically dispatching the types of the variable, we can play around with it at
compile-time. That requires some changes but in the end it’s pretty clear what we need to do:</p>
<pre><code class="language-rust">use std::marker::PhantomData;

// We don’t need the Type enum anymore so I just removed it. However, even with that solution, we
// could still want to keep it around.
#[derive(Debug)]
pub struct Var&lt;B, T&gt; where B: ?Sized, T: ?Sized {
  name: String,
  _b: PhantomData&lt;*const B&gt;,
  _t: PhantomData&lt;*const T&gt;,
}

impl&lt;B, T&gt; Var&lt;B, T&gt; {
  pub fn new(name: &amp;str) -&gt; Self {
    Var {
      name: name.to_owned(),
      _b: PhantomData,
      _t: PhantomData,
    }
  }
}

impl&lt;B, T&gt; Var&lt;B, T&gt; where B: Backend, T: Interface {
  pub fn update(&amp;mut self, value: &amp;T) {
    // Notice how now, we use the update method from the Interface and not the Backend! Important
    // change, as you will see.
    T::update(self, value)
  }
}

// The interface trait loses the associated constant and gets a method, update. This method must
// work for any backend (i.e. types that implement Backend), since it’s our public facing trait.
trait Interface {
  fn update&lt;B&gt;(var: &amp;mut Var&lt;B, Self&gt;, value: &amp;Self) where B: Backend;
}

// We can then implement it for all our types by selecting manually the update_* method we want.
impl Interface for i32 {
  fn update&lt;B&gt;(var: &amp;mut Var&lt;B, Self&gt;, value: &amp;Self) where B: Backend {
    B::update_i32(var, value);
  }
}

impl Interface for u32 {
  fn update&lt;B&gt;(var: &amp;mut Var&lt;B, Self&gt;, value: &amp;Self) where B: Backend {
    B::update_u32(var, value);
  }
}

impl Interface for f32 {
  fn update&lt;B&gt;(var: &amp;mut Var&lt;B, Self&gt;, value: &amp;Self) where B: Backend {
    B::update_f32(var, value);
  }
}

impl Interface for bool {
  fn update&lt;B&gt;(var: &amp;mut Var&lt;B, Self&gt;, value: &amp;Self) where B: Backend {
    B::update_bool(var, value);
  }
}

// The trait backend is now a list of methods that will be available to the Interface’s
// implementors.
trait Backend {
  fn update_i32(var: &amp;mut Var&lt;Self, i32&gt;, value: &amp;i32);
  fn update_u32(var: &amp;mut Var&lt;Self, u32&gt;, value: &amp;u32);
  fn update_f32(var: &amp;mut Var&lt;Self, f32&gt;, value: &amp;f32);
  fn update_bool(var: &amp;mut Var&lt;Self, bool&gt;, value: &amp;bool);
}

/// Implementing the backend trait now gives us a power we didn’t have back then in solution 1: we
// now can observe the type of the variable and, then, we can actually do useful things with it, as
// displaying it!
impl Backend for () {
  fn update_i32(var: &amp;mut Var&lt;Self, i32&gt;, value: &amp;i32) {
    println!("setting {} to int {}", var.name, value);
  }

  fn update_u32(var: &amp;mut Var&lt;Self, u32&gt;, value: &amp;u32) {
    println!("setting {} to unsigned int {}", var.name, value);
  }

  fn update_f32(var: &amp;mut Var&lt;Self, f32&gt;, value: &amp;f32) {
    println!("setting {} to float {}", var.name, value);
  }

  fn update_bool(var: &amp;mut Var&lt;Self, bool&gt;, value: &amp;bool) {
    println!("setting {} to bool {}", var.name, value);
  }
}
</code></pre>
<p>We change the definition of the <code>Backend</code> trait to have all the functions dispatched statically.
Then, using the <code>Interface</code> trait, we now have one information we didn’t have in the first example:
the actual, concrete type of the variable. We can then call the right function from the <code>Backend</code>
trait.</p>
<p>To sum up, because all of this is starting to be a bit confusing:</p>
<ul>
<li>The <code>Interface</code> trait is the trait used to restrict the public API (i.e. which types can be
used publicly).</li>
<li>The <code>Backend</code> trait is the trait to implement when providing the actual implementation.</li>
</ul>
<p>However, if we add more types, that solution won’t scale easily. The problem is that we have the
typing information in several places (at the <code>impl</code> level and in a static list in a trait). It would
be much easier if we could, somehow, force an <code>impl</code> to exist in another trait. Basically, I want to
remove those <code>update_*</code> and use <code>impl</code>s instead.</p>
<h2>The third and final solution: inferred static dispatch</h2>
<p>I have no idea how to call that way of doing but I like to think of it as inferred constraints.
The idea is almost the same as the second solution but instead of declaring the list of functions
that can be used in the implementation of the <code>Interface</code> trait, we will just create a generic
dependency between the <code>Interface</code> trait and <code>Backend</code>. The advantage will also be that we don’t
have to worry about the name of the function anymore, since it will be polymorphic.</p>
<p>Let’s go.</p>
<pre><code class="language-rust">use std::marker::PhantomData;

#[derive(Debug)]
pub struct Var&lt;B, T&gt; where B: ?Sized, T: ?Sized {
  name: String,
  _b: PhantomData&lt;*const B&gt;,
  _t: PhantomData&lt;*const T&gt;,
}

impl&lt;B, T&gt; Var&lt;B, T&gt; {
  pub fn new(name: &amp;str) -&gt; Self {
    Var {
      name: name.to_owned(),
      _b: PhantomData,
      _t: PhantomData,
    }
  }
}

// Wouhouh, some changes here. We go back to the first solution by using the update method of the
// Backend trait, but notice how we bind the Backend trait to the Interface by using Backend&lt;T&gt;.
impl&lt;B, T&gt; Var&lt;B, T&gt; where T: Interface, B: Backend&lt;T&gt; {
  pub fn update(&amp;mut self, value: &amp;T) {
    B::update(self, value)
  }
}

// This trait is now a bit dumb and even dangerous as it’s very easy to implement it. In a perfect
// Rust world, we would have sealed traits — i.e. we could only implement it from the inside of the
// current crate.
trait Interface {}

impl Interface for i32 {}

impl Interface for u32 {}

impl Interface for f32 {}

impl Interface for bool {}

// The backend trait now has an associated type parameter that represents the variable type. What
// it means is that we will be able to implement the update function for all types of our choices…
// and still observe the type, since from inside the update function, it’s not universally
// quantified anymore!
trait Backend&lt;T&gt; {
  fn update(var: &amp;mut Var&lt;Self, T&gt;, value: &amp;T);
}

impl Backend&lt;i32&gt; for () {
  fn update(var: &amp;mut Var&lt;Self, i32&gt;, value: &amp;i32) {
    println!("setting {} to int {}", var.name, value);
  }
}

impl Backend&lt;u32&gt; for () {
  fn update(var: &amp;mut Var&lt;Self, u32&gt;, value: &amp;u32) {
    println!("setting {} to unsigned int {}", var.name, value);
  }
}

impl Backend&lt;f32&gt; for () {
  fn update(var: &amp;mut Var&lt;Self, f32&gt;, value: &amp;f32) {
    println!("setting {} to float {}", var.name, value);
  }
}

impl Backend&lt;bool&gt; for () {
  fn update(var: &amp;mut Var&lt;Self, bool&gt;, value: &amp;bool) {
    println!("setting {} to bool {}", var.name, value);
  }
}
</code></pre>
<p>This solution is quite interesting because of the use of the type parameter in the <code>Backend</code> trait.
It enables you to implement the <code>Backend</code> trait and still observe (i.e. <em>know</em>) the type of the
variable you’re playing with. Compare with the first solution, where we were completely generic on
the <code>T</code> type.</p>
<p>In <a href="https://crates.io/crates/luminance">luminance</a>, I use the last solution to allow a clear distinction between a set of public types
and a set of matching implementations. Notice in the last solution the <code>Interface</code> trait, which is
now reduced to something pretty dumb. It would be easy for anyone to implement it for their own
types and then implement <code>Backend&lt;TheirType&gt; for TheirBackend</code>. Rust doesn’t offer a way to have
<em>sealed traits</em> so far, so my current solution to this is to mark the trait <code>unsafe</code> (to scare
people and tell them not to implement the trait). However, a clear and first-class citizen language
construct for this would be highly appreciated.</p>
<p>That’s all for me today. If you have any question about such a design, I’d be happy to answer your
questions on Reddit, as usual.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 13 Jun 2019 17:05:00 GMT</pubDate></item><item><title>Zellij is doing too much</title><link>https://strongly-typed-thoughts.net/blog/zellij-2024</link><description><![CDATA[<p>Today, I want to write about <a href="https://zellij.dev">Zellij</a> and especially its latest news update<a href="https://zellij.dev/news/welcome-screen-pipes-filepicker/">¹</a>.</p>
<h1>Zellij, the modern tmux?</h1>
<p>I came into Zellij a wild ago, while I was looking for something more modern over <a href="https://github.com/tmux/tmux/wiki">tmux</a>. It had the terminal
multiplexer features (windows, splits, focus, etc.), but was lacking features (sessions, for instance, were added
mid-2021<a href="https://github.com/zellij-org/zellij/blob/main/CHANGELOG.md#0120---2021-05-27">²</a>). As time passed, it started to get new features and promising. For instance, its layout system, allowing
to have a more minimalistic view (the default one being really all-over-the-place).</p>
<p>And then I realized that some very basic features from tmux were absent:</p>
<ul>
<li>When I focus on a pane (<code>prefix+z</code> by default in tmux; <code>ToggleFocusFullscreen</code> action in Zellij), in Zellij, I don’t
have any indication the pane is being focused, while tmux has this nice and neat <code>Z</code> marker in the statusbar. That
doesn’t seem like important, but it is, especially when you keep zooming in and out.</li>
<li>The statusbar… well, tmux has a very-well defined one, highly customizable. Zellij… doesn’t really have one. A
statusbar is just a plugin that runs on a 1-row layout. You can hide it by switching to a different layout, that
sounds overly complicated for such a basic feature.</li>
<li>Sessions cannot be renamed easily; you have to enter something they call the “session manager”.</li>
</ul>
<h2>Session manager, and the beginning of problems</h2>
<p>What I like about a software, like, for instance, <a href="https://github.com/tmux/tmux/wiki">tmux</a> or <a href="https://kakoune.org">kakoune</a>, is that they are <em>minimal</em>. They do one thing and
they do it well. If you are not sure about whether something is <em>minimal</em>, ask yourself a simple question: <em>“What do
we expect from this tool to solve; to do?”</em>. For a terminal multiplexer, we expect:</p>
<ul>
<li>To be able to split a window into panes; navigate the panes; focus them, etc.</li>
<li>To have several windows (tabs).</li>
<li>To have sessions.</li>
<li>To have a customizable statusbar.</li>
<li>To have some general purpose utilities, such as popups, etc.</li>
<li>To provide a nice API / RPC system to be able to control the system via scripting / hooks / etc.</li>
<li>That’s roughly all.</li>
</ul>
<p>If you provide a few amounts of nice features, and composability tools, then your users can build <em>more</em> by composing
your system with others. For instance, <a href="https://github.com/tmux/tmux/wiki">tmux</a> has the <code>display-popup</code> (<code>popup</code>) command that takes a command to run
and displays it in a floating window. That’s great, because now I can do something like <code>tmux ls -F '#S'</code> to list all
running sessions, pipe it to my favorite fuzzy picker (<code>fzf</code>, <code>sk</code>, etc.), and redirect the result to
<code>xargs tmux switch-client -t</code>. Something like:</p>
<pre><code class="language-sh">popup -E "tmux ls -F '#S' | sk | xargs tmux switch-client -t"
</code></pre>
<p>Make a keybinding for that, and here you go, you have a customized session picker!</p>
<p>On the other side, Zellij ships with its “session manager” — which, honestly looks terrible to me.</p>
<p><img
  src=https://zellij.dev/img/zellij-session-manager-animated.gif
	width=800
/></p>
<p>That thing is not something you have control on, and the Zellij CLI Actions<a href="https://zellij.dev/documentation/cli-actions#cli-actions">³</a> won’t help you either.</p>
<blockquote>
<p>Note: we are also in the right to ask ourselves why there is a list of commands (that you can bind to), and a list of
CLI actions. The former is richer than the latter and it means that the commands you bind to your keys and the
commands you run on the CLI do not share the same naming / they are not from the same set.</p>
</blockquote>
<p>So you’re stuck with a “native” session picker, that could have been pushed to the users. Again, with tmux, it’s a
one-liner, and it uses my favorite fuzzy picker.</p>
<h2>It gets worse</h2>
<p>Lately, I have decided to head over the Zellij news<a href="https://zellij.dev/news/">⁴</a> section to read about recent updates, and as mentioned at the
very top of this article, I was not disappointed. The latest update features some new enhancements, which goes against
the concept of minimalism and even against the initial scope of the project:</p>
<ul>
<li>A “welcome screen”, which is just something someone would create with a single script.</li>
<li>Strider, a file picker. I was so surprised to find that in a terminal multiplexer. I guess you don’t have to use it,
but the fact it’s there sounds very weird. Yet another thing to maintain in a larger and larger codebase. Hm.</li>
<li>They introduce <code>zpipe</code> to pipe results into plugins. That’s interesting, but that’s also a new burden to maintain
because of using plugins in the first place, instead of going the composability route. I’m not convinced. With tmux,
I can simply send the result of a command to a specific session, window, pane. All of that just looks like a
justification for the extra complexity added by plugins. But at least it makes sense once you have decided to use
plugins?</li>
</ul>
<h2>But it can be for you</h2>
<p>However, I don’t want to bash on Zellij. I think this very article I’m writing should be my reflection about why Zellij
exists, why it’s not for me (anymore?) and why it could be for you. See, tmux is an old, battle-tested piece of software
that will do exactly what it’s supposed to. If you want more, well, then you have to build it yourself. Because tmux
composes well, it’s an enjoyable process for me, but it might not be for you. Maybe you don’t want to have to learn a
fuzzy picker and the default, native session manager of Zellij will be a breeze for you. Maybe the file picker they have
made will be of great help for you.</p>
<p>I could summarize my thoughts as follows:</p>
<ul>
<li>If you are like me and like minimalism, UNIX philosophy and well-scoped softwares, then Zellij is definitly not a good
pick, and you will be better off it; go to tmux, a TWM, etc.</li>
<li>If you don’t care about all that philosophy and responsability stuff, then go for it! You’ll probably enjoy it for
being more user friendly!</li>
</ul>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 25 Apr 2024 10:43:00 GMT</pubDate></item><item><title>Goodbye, GitHub</title><link>https://strongly-typed-thoughts.net/blog/so-long-and-good-night-github</link><description><![CDATA[<blockquote>
<p>TL;DR: this is a bit <em>ranty</em>, and is not necessarily well organized. You will be surprised that I have not
put that text through an AI agent to review and rework it. You have been warned: you are reading something
that was entirely written by a damn human being.</p>
<p><a href="#announcement">This blog post contains an important announcement that you should still read, though</a>.</p>
</blockquote>
<h1><a href="#">#</a> So long, and goodnight GitHub</h1>
<p>I had anticipated writing such a blog article for a long time by now (months? years?). Back in <em>June 2024</em>,
<a href="https://strongly-typed-thoughts.net/blog/aerc-2024">I wrote a blog post</a> about switching to an email-based
workflow, and the will to switch to a decentralized life. I’ve been having several issues with many different
services I use, including GitHub. The idea that Microsoft bought it started to itch me, for the simple
reason that it was <em>obvious</em> what microsoft had been doing
<a href="https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish">all along</a>.</p>
<h2 id="you-are-the-product"><a href="#you-are-the-product">#</a> You are the product</h2>
<p>Remember this?</p>
<blockquote>
<p>If you happen to be using freely a service owned by a company, you are very likely the product.</p>
</blockquote>
<p>So back in the days, I decided to start moving my main repositories — i.e. the ones I actively maintain — as
soon as possible, so that the new work process move to platforms I believe more in (<a href="https://sourcehut.org/">SourceHut</a>), and kept
archives of the existing version of the projects on GitHub.</p>
<p>As time passed, my opinion on <em>how I should run my spare-time projects</em> matured. We’ve seen the advent of
so-called <em>« AI »</em>, which is a term I despise. It means absolutely nothing and is just a marketing buzzword,
but also a massive and global engineering desillusion that is going to cause tons of harm mid- and long-
terms. What do you think the developers of tomorrow will look like, not having a single clue about what
a stack corruption means, or tail-recursion, or even simply how to think by themselves, being creative, where
all they will have learned is to reach for their Claude prompt or Copilot 3000. AI models were supposed to be
programmers <em>assistants</em>, not the other way around.</p>
<p>People won’t really thinking about <em>how</em> to solve a problem. They will ask the AI to solve the problem for
them, and <em>have to validate the solution.</em> This is currently possible because most of the programmers out
there actually have the required skills to understand and check the code (most of the time terrible) that is
spit out by Cursor. At some point, those programmers will start to actually get taught by AI, lacking the
foundational skills to verify the code. I strongly believe that <em>not coming up</em> with the actual implementation
is going to create a generation of developers that will lack the creativity, resilience and methodologies
required to be a good programmer, but also to just being able to read and understand it in the first place.</p>
<p>This is just one side of the issue.</p>
<p>I will not even mention the problem of energy, it’s well documented (i.e. AI sucks hard), and instead want to
talk about two things.</p>
<h2 id="copyleft-violation"><a href="#copyleft-violation">#</a> Copyleft violation</h2>
<p>The AI ecosystem gives little regard to copylefts and actually contributing in a collaborative way with the
rest of the world. I have shared my thoughts on that several times, but it’s a global violation to millions
of codebases (ripping off free and open-source code in order to train a company-owned, privileged,
technology that users have to pay for, one way or another), while evaporating lakes. <strong>I am not okay with my
code being used to train proprietary software</strong>, code that is part of profitable services that no one in the
free and open-source world can possibly can even dream to replicate due to obvious… budget differences.
People used to stay that companies using free and open-source code should provide a bit of money to the
people actually maintaining that software on their spare-time; it’s code they rely on, that they didn’t have
to write; that they don’t have to maintain. And yet, now, most people seem unbothered with the idea of
companies absorbing the whole Internet to train their services that they will charge you for. WTF?!</p>
<h2 id="aggressive-crawlers"><a href="#aggressive-crawlers">#</a> Aggressive crawlers</h2>
<p>The AI ecosystem is so aggressive that it causes <a href="https://drewdevault.com/2025/03/17/2025-03-17-Stop-externalizing-your-costs-on-me.html">trustworthy third parties</a>
to experience DDoS and other various kinds of issues, which is absolutely disgusting. That led to people
coming up with solutions like <a href="https://blog.cloudflare.com/ai-labyrinth/">AI Labyrinth</a> or human-detection
algorithm when accessing forges. That is not a solution, because such platforms actually have to spend CPU
resources to fight against aggressive AI crawlers. If you are implementing a crawler that doesn’t respect
<code>robots.txt</code> or similar, you are part of the problem.</p>
<h2 id="clear-evidence"><a href="#clear-evidence">#</a> A clear evidence</h2>
<p>And then, there was <a href="https://x.com/ashtom/status/1952409910236291109">this tweet from Thomas Dohmke</a> — former
GitHub CEO:</p>
<blockquote>
<p>The evidence is clear: Either you embrace AI, or get out of this career.</p>
</blockquote>
<p>This is like spitting directly to the faces of millions of developers worldwide. Of course, coming from a guy
whose <em>career</em> depends on people actually following and paying for his AI development model, this is at least
hilarious, yet disrespectful for people who, actually, do build software instead of babysitting a drunk
completer that understands nothing about your project and keeps wasting everyone’s energy asking to review
his triangle drawing while it actually drew a circle.</p>
<p>That so-called evidence is based on <strong>22 engineers who use AI in their day to day work</strong>. 22. Twenty-two.
Vingt-deux. If that is evidence for him, and that we should trust his judgement, then I officially announce
that I believe <a href="https://strongly-typed-thoughts.net/media/uploads/fsm.jpg">FSM</a> will be president of the world
by 2050 — and I am almost 100% sure you can find more people believing my claim than his.</p>
<p>I do believe that we need to take a bit of hindsight on technology, and the place where we want to stand. From
my perspective, I have read that <a href="https://tech.co/news/another-study-ai-making-us-dumb">AI makes us dumb</a>. I
do think that at some point, where we will need to come up with ideas to new problems, AI will come short. I
— probably like many others — had to deal with Cursor comments at work on PRs I opened for my colleagues to
review, just to realize that the Cursor AI left a dumb comment about my code, proving that 1. it doesn’t
understand the project and 2. even the suggestion it does is wrong, without really looking into the context of
the project. This is just a way to make us lose and waste time at work, <em>but we are using AI</em>.</p>
<p>I do think AI agents are tools, and my opinion is that they are not mature tools (yet?) that I do not want to
have to interact with (yet?). Woah, I don’t use AI to review the emails I send! Damn, I don’t use AI to write
my blog articles! Holy shit, I don’t try to ask an AI what it thinks about the code I write! And you know
what? Why would I? Why would I ask an AI to write a speech to describe a project I work on instead of actually
being a human being and sitting at my desk, and activiting the actual and real neurons I still have in my
brain? We have been having <em>AI</em> for a long time: a good compiler is a form of AI. But we have always used AI
dedicated to solving a very specific problem. What those new AI agents are doing is <em>trying to replace human
thinking</em>, with hyper advanced auto-completer. Yeah, I’ll pass.</p>
<h2 id="announcement"><a href="#announcement">#</a> Announcement</h2>
<p>I started last year to move my projects off GitHub to <a href="https://sourcehut.org/">SourceHut</a>. I want to use this blog article to
officially say that I have started to move the rest of my projects off GitHub, and that I <strong>will not let
archived repositories on GitHub</strong>. Of course, I am not doing that without first checking package managers
and any dependent systems (I’m thinking Hackage for Haskell packages, and crates.io for Rust). So it will
take a bit of time — I have actually quite a lot of source projects! — but I will eventually reach a point
where none of the repositories I have on GitHub will be source projects.</p>
<p>If you depend on any of my GitHub projects, feel free to switch to the <a href="https://sourcehut.org/">SourceHut</a> tree instead.</p>
<p>As always, keep the vibes, and goodbye GitHub.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 19 Aug 2025 15:15:00 GMT</pubDate></item><item><title>Don’t use Default</title><link>https://strongly-typed-thoughts.net/blog/dont_use_default</link><description><![CDATA[<h1>Don’t use Default</h1>
<h2>Background knowledge: typeclasses and laws</h2>
<p>In <strong>Haskell</strong>, we have a lot of
<a href="https://en.wikipedia.org/wiki/Type_class">typeclasses</a>. Those are very handy
and – in general – come with laws. Laws are very important and give hints on how
we are supposed to use a <em>typeclass</em>.</p>
<p>For instance, the <code>Semigroup</code> <em>typeclass</em> exposes an operator (<code>(&lt;&gt;)</code>) and has
an <strong>associativity</strong> law. If <code>a</code>, <code>b</code> and <code>c</code> have the same type <code>T</code>, if we know
that <code>T</code> is a <code>Semigroup</code>, we have the following law:</p>
<pre><code>a &lt;&gt; b &lt;&gt; c = (a &lt;&gt; b) &lt;&gt; c = a &lt;&gt; (b &lt;&gt; c)
</code></pre>
<p>If <code>T</code> is a <code>Monoid</code>, which is a <code>Semigroup</code> with an identity (called <code>mempty</code>),
we have a law for <em>monoids</em>:</p>
<pre><code>a &lt;&gt; mempty = mempty &lt;&gt; a = a
</code></pre>
<p>Those laws can – <em>have</em> – to be used in our code base to take advantage over the
structures, optimize or avoid boilerplate.</p>
<h2>The Default typeclass</h2>
<p>In some situations, we want a way to express default values. That’s especially
true in OO languages, like in the following <strong>C++</strong> function signature:</p>
<pre><code>void foo(float i = 1);
</code></pre>
<p>In <strong>Haskell</strong>, we cannot do that, because we have to pass all arguments to a
function. The following doesn’t exist:</p>
<pre><code>foo :: Float -&gt; IO ()
foo (i = 1) = -- […]
</code></pre>
<p>And there’s more. Even in C++, how do you handle the case when you have several
arguments and want only the first one to be defaulted? You <strong>cannot</strong>.</p>
<p>So, so… Some <strong>Haskellers</strong> decided to solve that problem with a <em>typeclass</em>.
After all, we can define the following typeclass:</p>
<pre><code class="language-haskell">class Default a where
  def :: a
</code></pre>
<p>We can then implement <code>Default</code> and have a <em>default</em> value for a given type.</p>
<pre><code class="language-haskell">instance Default Radians where
  def = Radians $ 2*pi

instance Default Fullscreen where
  def = Fullscreen False
</code></pre>
<p>However, there is an issue with that. You cannot use <code>Default</code> without creating
<code>newtype</code>s to overload them. Why? Well, consider the following <code>Default</code>
instance:</p>
<pre><code class="language-haskell">instance Default Float where
  def = -- what should we put here?
</code></pre>
<p>Remember that, in <strong>Haskell</strong>, an instance is defined only once and is
automatically imported when you import the module holding it. That means you
cannot have the following:</p>
<pre><code class="language-haskell">-- in a module A
instance Default Float where
  def = 0

-- in a module B
instance Default Float where
  def = 1

-- in a module C
import A
import B

-- What instances should we use?
</code></pre>
<blockquote>
<p><em>Hey, that’s easy. We just have to keep the modules apart, and import the one
we want to use!</em></p>
</blockquote>
<p>Yeah, well. No. No.</p>
<p><img src="http://phaazon.net/pub/god_please_no.gif" alt="" /></p>
<p>Orphan instances are <strong>wrong</strong>. You should read
<a href="https://wiki.haskell.org/Multiple_instances">this</a> for further explanations.</p>
<p>That’s why we have to use <code>newtype</code>s everywhere. And that’s boring. Writing code
should always have a goal. When we write as much boilerplate code as real code,
we can start thinking there’s something wrong. Worse, if we have more
boilerplate than real code, well, something is terribly wrong. In our case,
we’re introducing a lot of <code>newtype</code>s for only being able to use <code>def</code> at a few
spots. Is that even worth it? Of course not.</p>
<h2>Default is evil</h2>
<p>The <code>Default</code> typeclass is evil. It’s shipped with default instances, like one
for <code>[a]</code>, which defaults to the empty list – <code>[]</code>. It might be clear for you
but why would I want to default to the empty list? Why not to <code>[0]</code>? Or a more
complex list? I really doubt someone ever uses <code>def :: [a]</code>.</p>
<p>Another reason why <code>Default</code> is wrong? There’s absolutely <strong>no law</strong>. You just
have a default value, and that’s all.</p>
<pre><code class="language-haskell">bar :: (Default a) =&gt; a -&gt; Maybe String
</code></pre>
<p>Can you say what the default is for? Of course you cannot. Because there’s no
law. The instance has no real meaning. A default value makes sense only for the
computation using it. For instance, the empty list makes sense if we glue it to
the <em>list concatenation</em>.</p>
<h2>We already have a lot of defaults</h2>
<p>In <a href="https://hackage.haskell.org/package/base">base</a>, there’re already several
ways to express <em>defaulted values</em>.</p>
<p>The <code>Monoid</code>’s <code>mempty</code> is a way to express a default value regarding its
binary operation (<code>(&lt;&gt;)</code>).</p>
<p>The <code>Alternative</code>’s <code>empty</code> provides a similar default, but for first-class
types.</p>
<p>The <code>MonadZero</code>’s <code>mzero</code> provides a different default, used to <em>absorb</em>
everything. That’s a law of <code>MonadZero</code>:</p>
<pre><code>mzero &gt;&gt;= f = mzero
a &gt;&gt; mzero  = mzero
</code></pre>
<p>Those defaults are interesting because we can reason about. If I see <code>mzero</code> in
a <em>monadic code</em>, I know that whatever is following will be discarded.</p>
<h1>Conclusion</h1>
<p>So please. Stop using <code>Default</code>. It doesn’t bring any sense to your codebase. It
actually removes some! If you want to provide functions with defaulted
arguments, consider partially applied functions.</p>
<p><img src="http://phaazon.net/pub/default_stats.png" alt="" /></p>
<p><a href="https://hackage.haskell.org/package/data-default">data-default</a> is a very
famous package – look at the downloads! You can now have some hindsight about it
before downloading it and ruining your design. ;)</p>
<p>Happy hacking. :)</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 16 Jul 2015 00:00:00 GMT</pubDate></item><item><title>cargo and how crates.io doesn’t like cyclic dev-dependencies</title><link>https://strongly-typed-thoughts.net/blog/cargo-cyclic-dev-deps</link><description><![CDATA[<p>This is a very short blog article about a problem I found lately with <a href="https://doc.rust-lang.org/cargo">cargo</a>.</p>
<h1>The problem</h1>
<p>Imagine you have a crate <code>A</code> and a crate <code>B</code> and you want to upload them to crates.io. There is a
context setup:</p>
<ul>
<li>You’re working in a <a href="https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html">cargo workspace</a>.</li>
<li><code>A</code> depends on <code>B</code> via <code>[dev-dependencies]</code> because you use it in your <code>examples/</code> directory,
for instance.</li>
<li><code>B</code> depends on <code>A</code> via <code>[dependencies]</code> because it implements some interface or such.</li>
<li>You would like to upload <code>A</code> first (because uploading <code>B</code> without having <code>A</code> would make <code>B</code>
unable to compile).</li>
</ul>
<p>The main problem is that currently, you cannot do <em>anything</em> with such a setup. If you try to
upload <code>A</code> to crates.io, crates.io will reject the crate because <code>B</code> doesn’t exist. That might be
stunning, but yes, <a href="https://doc.rust-lang.org/cargo">cargo</a> also check that your <code>[dev-dependencies]</code> are correctly aligned with your
<code>A</code>’s <code>Cargo.toml</code>. Meh.</p>
<p>If you try to upload <code>B</code>, obviously, since <code>A</code> is not there, crates.io will complain.</p>
<p>What should we do?</p>
<h1>The (dirty) solution</h1>
<p>First thing first, here’s <a href="https://github.com/rust-lang/cargo/issues/4242">a discussion</a> that helped
me figure out how to solve that issue. Disclaimer, though: it’s neither elegant and satisfying.</p>
<p>The first thing to do is to edit <code>A</code>’s <code>Cargo.toml</code> in order to completely strip its
<code>[dev-dependencies]</code> section. Just remove it. All of it.</p>
<p>Then, and that’s the tricky part: do not <code>git commit</code> the change. At that point, you have removed
the dependency from <code>A</code> to <code>B</code>, which is <strong>not mandatory for people getting the crate from
crates.io.</strong> That’s the nasty part. You’re about to upload a crate which metadata are not aligned
with your repository. I don’t like it, but I don’t see any other way to do it — however, you’ll see
we can fix it afterwards.</p>
<p>Then, upload <code>A</code> to crates.io:</p>
<pre><code>cd A
cargo publish --allow-dirty
</code></pre>
<p>The <code>--allow-dirty</code> switch is needed because <a href="https://doc.rust-lang.org/cargo">cargo</a> would complain about your working tree not
being clean otherwise.</p>
<p>Now, you have <code>A</code> on crates.io. Just go on with <code>B</code>: it can now be uploaded:</p>
<pre><code>cd ../B
cargo publish
</code></pre>
<p>Now you have, on crates.io:</p>
<ul>
<li><code>A</code> in version <code>X.Y.Z</code>, without any <code>[dev-dependencies]</code>.</li>
<li><code>B</code>, whichever version.</li>
</ul>
<p>The final part of the trick is to <em>restore</em> the sanity of <code>A</code>. Because, yes, I don’t like having
something on crates.io that is not exactly the same thing as my repository.</p>
<p>Edit <code>A</code>’s <code>Cargo.toml</code> or check it out:</p>
<pre><code>cd ..
git checkout .
</code></pre>
<p>Now you are exactly in the same situation as before trying anything to solve the problem. Edit
<code>A</code>’s <code>Cargo.toml</code> and increment its patch version. In my case, I would get version <code>X.Y.(Z+1)</code>.
<code>git commit</code> your changes, <code>git push</code>. You’re ready to patch crates.io:</p>
<pre><code>cd A
cargo publish
</code></pre>
<p>Now you have:</p>
<ul>
<li><code>A</code> in version <code>X.Y.(Z+1)</code>, with the right <code>[dev-dependencies]</code>.</li>
<li><code>B</code>, whichever version.</li>
</ul>
<h1>Conclusion</h1>
<p>I know it’s not an ideal fix, but at least we end up on our (clean) feets. I really really think
the <a href="https://doc.rust-lang.org/cargo">cargo</a> team should fix that issue, though. In that case, <code>A</code> and <code>B</code> were actually <a href="https://crates.io/crates/luminance">luminance</a>
and <a href="https://crates.io/crates/luminance-derive">luminance-derive</a>. I will make a long article very soon about them to introduce new versions,
new crates and complete graphics tutorials on how to have fun with <a href="https://crates.io/crates/luminance">luminance</a>.</p>
<p>I hope you liked that article and as always, <em>keep the vibes</em>.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 23 Aug 2019 12:00:00 GMT</pubDate></item><item><title>Announcement: luminance-0.31, luminance-derive and luminance-glutin</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.31</link><description><![CDATA[<p><a href="https://crates.io/crates/luminance">luminance</a> has been having lots of activity lately. I’ve been working (hard) on making it to the
<code>1.0</code> release for months now and came to a very important realization: lots of changes have been
done and I’m still not ready to release it. I’ve kind of lost myself in the process.</p>
<p>This article is about two main topics:</p>
<ul>
<li>The new release of <a href="https://crates.io/crates/luminance">luminance</a> and its changes.</li>
<li>The introducing of ways to learn <a href="https://crates.io/crates/luminance">luminance</a> (<em>yay</em>!).</li>
</ul>
<blockquote>
<p>If you don’t care about the changes, feel free to go read
<a href="https://github.com/phaazon/luminance-rs#learning">this</a> to know how you can learn about
<a href="https://crates.io/crates/luminance">luminance</a> and graphics programming in general.</p>
</blockquote>
<h1>Motivation</h1>
<p>Lately, <a href="https://crates.io/crates/luminance">luminance</a> has received a wide range of feature additions, bug fixes and design ideas. I’ve
been wanting to release all those changes as part of the <code>1.0</code> release but the thing is: among all
the changes, some have been around on the <code>master</code> branch for months without a proper release on
crates.io… because the <code>1.0</code> milestone was not reached. People have been moving towards <a href="https://crates.io/crates/luminance">luminance</a>
more and more and some provided feedback about using <a href="https://crates.io/crates/luminance">luminance</a>. Happy but indecisive about what to
do, I faced a dilemma:</p>
<ul>
<li>Either wait and release everything as part of <code>1.0</code> but eventually <em>block</em> people from using all
the cool features of <a href="https://crates.io/crates/luminance">luminance</a> because the last release on crates.io is months old.</li>
<li>Break the feature set into two releases: <code>1.0</code> for everything and <code>0.31</code> for all the new
candies.</li>
</ul>
<p>I have decided to go with the second option.</p>
<h1>luminance-0.31</h1>
<p>Just before writing this article, the <a href="https://crates.io/crates/luminance/0.30.1">last luminance version</a> was <code>0.30</code> — just for the record,
that version is eleven months old, ~200 commits behind <code>master</code>. The new and most recent version is
thus <code>0.31</code> and crates got updated:</p>
<ul>
<li><a href="https://crates.io/crates/luminance/0.31.0">luminance-0.31</a></li>
<li><a href="https://crates.io/crates/luminance-windowing/0.3.0">luminance-windowing-0.3</a></li>
<li><a href="https://crates.io/crates/luminance-glfw/0.6.0">luminance-glfw-0.6</a></li>
<li><a href="https://crates.io/crates/luminance-derive/0.1.0">luminance-derive-0.1</a>, <strong>a new crate</strong> that brings procedural macros for the <code>#[derive(..)]</code>
construct.</li>
<li><a href="https://crates.io/crates/luminance-glutin/0.1.0">luminance-glutin-0.1</a>, <strong>a new crate</strong> supporting the <a href="https://crates.io/crates/glutin">glutin</a> windowing crate. Currently, not
all features are implemented but it’s usable.</li>
</ul>
<p>Lot of work has been accomplished and I received several contributions from people all around the
globe, including PRs and issues. I’d like to remind that I appreciate both and you are <strong>really</strong>
encouraged to contribute in <em>any way you want to</em>. Special thanks fly to:</p>
<ul>
<li><a href="https://github.com/ambihelical">@ambihelical</a></li>
<li><a href="https://github.com/linkmauve">@linkmauve</a></li>
<li><a href="https://github.com/bobtwinkles">@bobtwinkles</a></li>
<li><a href="https://github.com/twetzel59">@twetzel59</a></li>
<li>And all others whom I discussed with and who provided feedback, especially on Reddit.</li>
</ul>
<p>About that last point, my Reddit and Twitter interactions about <a href="https://crates.io/crates/luminance">luminance</a> have been <em>very</em>
interesting because I got to test the water about how people feel about <a href="https://crates.io/crates/luminance">luminance</a>, especially
when compared to (not so) similar crates, such as [gfx], <a href="https://crates.io/crates/glium">glium</a> or even <a href="https://crates.io/crates/gl">gl</a>. I came to the
realization, after reading people and what they would love to have as a graphics crate, that
<a href="https://crates.io/crates/luminance">luminance</a> <em>can</em> have the role of the <em>easy</em> crate, that is not necessarily the fastest but fast
enough to be quickly productive. I cannot help it but keep thinking about a simple question: if
some people can make games in Java or C# with a <em>garbage collector</em> or using old tech like
<em>OpenGL 2.1</em> (yes, some people still make pretty good games with that), why would one need a
perfect zero-cost abstraction down-to-the-metal unsafe ultra-optimized crate to write something?
See, I went round and squares about that topic, because I’ve already used <a href="https://crates.io/crates/luminance">luminance</a> in several
projects of mine (mostly demoscene purposes), and <em>it just does the job</em>. So, yes, <a href="https://crates.io/crates/luminance">luminance</a>
doesn’t have that cool, new and modern <em>Vulkan-type</em> API, I must confess. But it has its own API,
which is very functional-based (see the <a href="https://github.com/phaazon/luminance-rs#history">History</a>
section of <a href="https://crates.io/crates/luminance">luminance</a> for further details about that) and, to me, modern enough so that people
don’t get frustrated with the overall design being too clunky.</p>
<p>So, yeah. I gave up on the <a href="https://phaazon.net/blog/pre-luminance-n-random-thoughts">idea of introducing backends</a>
in <a href="https://crates.io/crates/luminance">luminance</a>. I really changed my mind several times about that topic and it’s actually when I
read comments on Reddit about people getting confused about the direction of the crate that I made
up my mind: <a href="https://crates.io/crates/luminance">luminance</a> must remain simple. Having a system of backends that can be hot-switched,
concurrent etc. is just going to make things hard for people to use it — and maintain it! It’s
likely that I will introduce, however, <em>feature-gates</em> to allow to compile <a href="https://crates.io/crates/luminance">luminance</a> on
WebAssembly via WebGL, <em>OpenGL ES</em> or even Vulkan at some point, but the difference is that no backend
will be implemented. That means that those feature-flags aren’t likely to be summable at first. But
all of this will be done in future releases; stay tuned.</p>
<p>The current blog post brings a description of all the changes of <a href="https://crates.io/crates/luminance/0.31.0">luminance-0.31</a>. It should be the
last <code>0.*.*</code> version before hitting the <code>1.0</code> release. To the question:</p>
<blockquote>
<p><em>Why not releasing <code>1.0</code> directly?</em></p>
</blockquote>
<p>I answer that the <code>1.0</code> milestone has a backlog with two major changes that will take time to
implement and I think it would be a pity to postpone a lot of great changes that are already
available because of two features that are yet to be implemented. The concept of versions is to
allow releasing features in a structured way without having to wait too much. So here we are.</p>
<p>This post also show cases a small tutorial about how to get started with <a href="https://crates.io/crates/luminance">luminance</a>. The very first
steps you should have would be to have a look at the <a href="https://github.com/phaazon/luminance-rs/blob/master/luminance/examples/README.md">examples/</a> directory and try to play with all
of the samples.</p>
<blockquote>
<p>Disclaimer: the following section of this article is based on
<a href="https://github.com/phaazon/luminance-rs/blob/master/luminance/CHANGELOG.md#031">luminance’s changelog</a>.</p>
</blockquote>
<h2>Bug fixes</h2>
<ul>
<li>Fix and remove <code>panic!</code> and attributeless renders.</li>
<li>Various internal bug fixes and performance improvements.</li>
<li>Fix pixel code for <code>Format::R</code> and <code>Format::RG</code> when querying a texture’s texels.</li>
</ul>
<h2>Major changes</h2>
<ul>
<li>Remove the concept of <code>GTup</code>. No code was using it and it was not really elegant.</li>
<li>Remove the <code>uniform_interface!</code> macro and replace it with the <code>UniformInterface</code> procedural
derive macro.</li>
<li>Buffer mapping is now always a <code>mut</code> operation. That is required to <em>lock-in</em> the mapped slices
and prevent to generate new ones, which would be an undefined behavior in most graphics backends
such as <em>OpenGL</em>.</li>
<li>Change the framebuffer’s slots types and meanings. Those are now more natural to use (for
instance, you don’t have to repeat the framebuffer’s associated types and dimensions nor even
use the <code>Texture&lt;..&gt;</code> type anymore, as a type family is now used to ease the generation of
color and depth slots).</li>
<li>Change the way the <code>Vertex</code> trait is implemented.
<ul>
<li>The <code>Vertex::vertex_format</code> method has been renamed <code>Vertex::vertex_desc</code>.</li>
<li>Instead of returning a <code>VertexFormat</code>, that method now returns a <code>VertexDesc</code>. Where a
<code>VertexFormat</code> was a set of <code>VertexComponentFormat</code>, a <code>VertexDesc</code> is a set of
<code>VertexBufferDesc</code>.</li>
<li><code>VertexBufferDesc</code> is a new type that didn’t exist back then in <em>0.30</em>. It provides new data
and information about how a vertex attribute will be spread in a GPU buffer. Especially, it has:
<ul>
<li>An <em>index</em>, allowing to map the vertex attribute in a shader.</li>
<li>A <em>name</em>, used by shader programs to perform mapping.</li>
<li>An <em>instancing</em> parameter, used to determine whether we want <strong>vertex instancing</strong>.</li>
<li>A <code>VertexAttribDesc</code>, the new name of <code>VertexComponentFormat</code>.</li>
</ul>
</li>
<li>As said above, <code>VertexComponentFormat</code> was renamed <code>VertexAttribDesc</code>.</li>
<li>Vertex attribute can now be <em>normalized</em> if they are <em>signed integral</em> or <em>unsigned integral</em>.
That is encoded in the <code>VertexAttribType</code>’s integral variants.</li>
<li>A new trait has appeared: <code>VertexAttrib</code>. Such a trait is used to map a type to a
<code>VertexAttribDesc</code>.</li>
<li><code>Vertex</code> has zero implementor instead of several ones in <em>0.30</em>. The reason for that is that
<code>VertexBufferDesc</code> is application-driven and depends on the <em>vertex semantics</em> in place in the
application or library.</li>
<li>Vertex semantics are introduced in this release and are represented via the <code>Semantics</code> trait.
Implementing directly <code>Semantics</code> is possible, even though not recommended. Basically,
<code>Semantics</code> provides information such as the <em>index</em> and <em>name</em> of a given semantics as long
as the list of all possible semantics, encoded by <code>SemanticsDesc</code>.</li>
<li>Users are highly advised to look at the <code>Vertex</code> and <code>Semantics</code> proc-macro derive in the
[luminance-derive] crate.</li>
</ul>
</li>
<li>Revise the <code>Tess</code> type to make it easier to work with.
<ul>
<li>The <code>Tess::new</code> and <code>Tess::attributeless</code> functions were removed.</li>
<li>The <code>TessBuilder</code> type was added and replace both the above function.</li>
<li>That last type has a lot of methods that can be combined in different ways to build powerful
situation of tessellations, among (but not limited to):
<ul>
<li>Normal and indexed tessellations.</li>
<li>Attributeless tessellations.</li>
<li>Tessellations with vertex instancing support.</li>
<li>Deinterleaved tessellations</li>
<li>Tessellations with support for <em>primitive restart indexing</em>.</li>
</ul>
</li>
<li>Slicing was revised too and now has support for two new Rust operators:
<ul>
<li>The <code>a ..= b</code> operator, allowing to slice a <code>Tess</code> with inclusive closed bounds.</li>
<li>The <code>..= b</code> operator, allowing to slice a <code>Tess</code> with inclusive bounds open on the left
side.</li>
</ul>
</li>
<li>Previously, the <code>Tess::new</code> associated function expected indices to be a slice of <code>u32</code>. This
new release allows to use any type that implements the <code>TessIndex</code> trait (mapping a type to a
<code>TessIndexType</code>. Currently, you have <code>u8</code>, <code>u16</code> and <code>u32</code> available.</li>
<li>Add <code>Tess::{as_index_slice,as_index_slice_mut}</code>. Those now enable you to conditionally slice-map
the <em>index buffer</em> of a <code>Tess</code>, if it exists.</li>
</ul>
</li>
<li>Add support for generic texture sampling.
<ul>
<li>This new feature is supported thanks to the <code>SamplerType</code> trait, used as constraint on the
<code>Pixel::SamplerType</code> associated type.</li>
<li>Basically, that feature allows you to bind a <code>Floating</code> texture without caring about the
actual type. That is especially true as you typically use <code>sampler2D</code> in a shader and not
<code>sampler2DRGB32F</code>.</li>
<li>Such a feature reduces the number of combination needed to refactorize code.</li>
</ul>
</li>
<li>Implement <em>vertex attrib explicit binding</em>. This is a huge change that is related to <em>vertex
semantics</em>. Basically, in <em>0.30</em>, you have to ensure that the <code>layout (location = _)</code> is
correctly set to the right value regarding what you have in your <code>Tess</code>’ vertex buffers. That
was both <em>unsafe</em> and terribly misleading (and not very elegant). The new situation, which
relies on <em>vertex semantics</em>, completely gets rid of <em>vertex locations</em> worries, which get
overrided by <a href="https://crates.io/crates/luminance">luminance</a> when a shader program gets linked.</li>
<li>Change boolean-like <code>enum</code>s — such as <code>DepthTest</code> — variants from <code>Enabled</code> / <code>Disabled</code> to
<code>On</code> / <code>Off</code> or <code>Yes</code> / <code>No</code>, depending on the situation.</li>
<li>Move <code>swap_buffers</code> from <code>GraphicsContext</code> to <code>Surface</code> in [luminance-windowing].</li>
<li>Switch to <code>GenMipmaps</code> instead of <code>bool</code> to encode whether mipmaps should be generated in
texture code. That change is a readability enhancement when facing texture creation code.</li>
<li>Make <code>Dimensionable::zero_offset()</code> a constant, <code>Dimensionable::ZERO_OFFSET</code>.</li>
<li>Change the way cursor modes are encoded from <code>bool</code> to <code>CursorMode</code>.</li>
</ul>
<h2>Minor changes</h2>
<ul>
<li>Add the [luminance-glutin] crate, the windowing crate support for <a href="https://crates.io/crates/glutin">glutin</a>.</li>
<li>Add the [luminance-derive] crate.
<ul>
<li>That crate provides several procedural derive macros you can use to easily implement all
required traits to work with <a href="https://crates.io/crates/luminance">luminance</a>. Especially, some traits that are <code>unsafe</code> can be
implemented in a safe way with that crate, so you should definitely try to use it.</li>
<li>Current available proc-macros are:
<ul>
<li><code>#[derive(Vertex)]</code>: derive the <code>Vertex</code> trait for a <code>struct</code>.</li>
<li><code>#[derive(Semantics)]</code>: derive the <code>Semantics</code> trait for an <code>enum</code>.</li>
<li><code>#[derive(UniformInterface)]</code>: derive the <code>UniformInterface</code> trait for a <code>struct</code>.</li>
</ul>
</li>
</ul>
</li>
<li>Support for dynamic uniform queries. Those are used whenever you don’t know which variables will
be used in a shader at compile-time. This might be the case if you’re writing a GUI tool or a
video game that uses a custom scripting language / node-ish representation of shaders. That
feature doesn’t break the already-in-place and great uniform interface but complements it. You
can use a shader <code>Program&lt;_, _, ()&gt;</code> and still set uniform values by querying the uniforms
dynamically. This feature also fully benefits from the strongly typed interface of <code>Uniform&lt;_&gt;</code>,
so you will get <code>TypeMismatch</code> runtime error if you try to trick the type system.</li>
<li>Add the <code>std</code> feature gate, allowing to compile with the standard library – this is enabled by
default. The purpose of this feature is to allow people to use <code>default-features = false</code> to
compile without the standard library. This feature is currently very experimental and shouldn’t
be used in any production releases so far – expect breakage / undefined behaviors as this
feature hasn’t been quite intensively tested yet.</li>
<li>Add support for the <code>R11FG11FB10F</code> pixel format.</li>
<li>Migrate to Rust Edition 2018.</li>
<li>The <code>WindowOpt</code> now has support for multisampling. See the <code>WindowOpt::set_num_samples</code> for
further details.</li>
<li>Implement dynamic edition of windowing types properties. That allows to change data on-the-fly,
such as the cursor mode.</li>
<li>Introduce normalized texturing. That feature is encoded as pixel formats: any pixel format which
symbol’s name starts with <code>Norm</code> is a <em>normalized pixel format</em>. Such formats state that the
texels are encoded as integers but when fetched from a shader, they are turned into
floating-point number by normalizing them. For instance, when fetching pixels from a texture
encoded with <code>R8UI</code>, you get integers ranging in <code>[0; 255]</code> but when fetching pixels from a
texture encoded with <code>NormR8UI</code>, even though texels are still stored as 8-bit unsigned integers,
when fetched, you get floating-point numbers comprised in <code>[0; 1]</code>.</li>
</ul>
<h2>Patch &amp; misc changes</h2>
<ul>
<li>Remove <code>std::mem::uninitialized</code> references, as it is now on deprecation path. Fortunately, the
codes that were using that function got patched with safe Rust (!) and/or simpler constructs.
It’s a win-win.</li>
<li>Add the <code>#[repr(C)]</code> annotation on vertex types in examples. That is a bit unfortunate because
such an annotation is very likely to be mandatory when sending data to the GPU and it should be
done automatically instead of requiring the user to do it. That situation will be fixed in a
next release.</li>
<li>Add more CI testing.</li>
<li>Update examples and made them available to the <code>cargo run --example</code> command. Read more
<a href="./examples/README.md">here</a>.</li>
<li>Massive documentation rewrite (among the use of <code>#![deny(missing_docs)]</code>. The situation is still
not perfect and patch versions will be released to fix and update the documentation. Step by
step.</li>
<li>Add design notes and documents in the repository.</li>
<li>Massive dependencies update. Special thanks to @eijebong for his help!</li>
<li>Add the <code>11-query-texture-texels</code> example, which showcases how to query a texture’s texels and
drop it on the filesystem.</li>
<li>Add and update <em>README</em> files. Especially, the gitter link was removed and an IRC link was
added. If you want to get help:
<ul>
<li>Server: <code>irc.freenode.net</code></li>
<li>Channel: <code>#luminance</code></li>
</ul>
</li>
</ul>
<h1>Learning luminance</h1>
<p>I also spent quite a lot of time and energy working on two ways to learn <a href="https://crates.io/crates/luminance">luminance</a>:</p>
<ul>
<li><a href="https://github.com/phaazon/luminance-rs/blob/master/luminance/examples/README.md">Examples</a>.</li>
<li><a href="https://github.com/phaazon/luminance-rs/wiki">The official, brand new wiki</a>.</li>
</ul>
<p>So far, I’m writing articles and the wiki is going to be updated let’s say, every week, if I find
enough spare time. You can also tell me what kind of stuff you’d like to learn and do.</p>
<p>The <a href="https://docs.rs/luminance/">official documentation</a> is up to date but is not as good as I
expect it to be. I will be adding patches versions to <em>0.31</em> to update it.</p>
<p>I hope you like <a href="https://crates.io/crates/luminance">luminance</a> and will make a good use of it. And of course, keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Thu, 29 Aug 2019 13:12:00 GMT</pubDate></item><item><title>Load geometries with wavefront-0.1!</title><link>https://strongly-typed-thoughts.net/blog/wavefront-0.1</link><description><![CDATA[<p>I’ve been away from <a href="https://hackage.haskell.org/package/luminance">luminance</a> for a few days
because I wanted to enhance the graphics world of Haskell. luminance might be interesting, if you
can’t use the art works of your artists, you won’t go any further for a real-world application. I
decided that I to write a parser/lexer to load 3D geometries from files. The
<a href="https://en.wikipedia.org/wiki/Wavefront_.obj_file">Wavefront OBJ</a> is an old yet simple and
efficient way of encoding such objects. It supports materials, surfaces and a lot of other cool
stuff – I don’t cover them yet, though.</p>
<p>There’s a <a href="http://hackage.haskell.org/package/obj">package</a> out there to do that, but it hasn’t been
updated since 2008 and has a lot of dependencies I don’t like (InfixApplicative, OpenGL,
OpenGLCheck, graphicsFormats, Codec-Image-Devil, and so on…). I like to keep things ultra simple and
lightweight. So here we go. <a href="http://hackage.haskell.org/package/wavefront">wavefront</a>.</p>
<p>Currently, my package only builds up a pure value you can do whatever you want with. Upload it to
the GPU, modify it, pretty print it, perform some physics on it. Whatever you want. The interface is
not frozen yet and I need to perform some benchmarks to see if I have to improve the
performance – the lexer is very simple and naive, I’d be amazed if the performance were that good
yet.</p>
<p>As always, feel free to contribute, and keep in mind that the package will move quickly along the
performance axis.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 11 Oct 2015 00:00:00 GMT</pubDate></item><item><title>kak-tree-sitter v0.4 and reflections</title><link>https://strongly-typed-thoughts.net/blog/kak-tree-sitter-v0.4</link><description><![CDATA[<h1>kak-tree-sitter v0.4.0 announcement and reflections</h1>
<blockquote>
<p>Currently, <a href="https://kakoune.org/">Kakoune</a> doesn’t support writing to files with a timeout, which could help.
<a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a> is a UNIX server bridging <a href="https://kakoune.org/">Kakoune</a> with <a href="https://tree-sitter.github.io/tree-sitter/">tree-sitter</a>, supporting semantic highlighting,
text-objects, indent guidelines and more!</p>
</blockquote>
<p>I want to use the opportunity of releasing <a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a> in version <code>v0.4.0</code> to talk about what’s new but also
explain a bit the new protocol used to make <a href="https://kakoune.org/">Kakoune</a> communicate with the server and vice versa. I think it’s a pretty
fun (and efficient!) way of making things, and it can give people ideas.</p>
<h2>What’s new?</h2>
<p>This new version adds a bunch of things to <a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a>. Among the interesting things:</p>
<ul>
<li>Add support for many languages by default in the shipped <code>config.toml</code>.</li>
<li>Fix PID detection to make it easier to restart / fix the server in case of problems.</li>
<li>Allow to specify the log level of the server.</li>
<li><code>C-c</code> support to stop the server if started as standalone.</li>
<li>Remove <code>tokio</code> and replace with <code>mio</code>. It’s easier, faster and lighter.</li>
<li>And obviously, rewrite the internals to use a new protocol.</li>
</ul>
<p><a href="https://github.com/phaazon/kak-tree-sitter/releases/tag/kak-tree-sitter-v0.4.0">Full changelog here</a>.</p>
<p>Additionnaly, you get <code>v0.4.1</code>, which also brings some interesting changes:</p>
<ul>
<li><code>%opt{kts_lang}</code> is now used to know how to highlight a buffer instead of <code>%opt{filetype}</code> directly. A hook
automatically calls <code>kak-tree-sitter-set-lang</code> (which you can override) to set <code>%opt{kts_lang}</code> before talking to the
server. By default, it simply forwards <code>%opt{filetype}</code>. The goal here is to allow people to provide their own
filetype detection by overriding <code>kak-tree-sitter-set-lang</code>.</li>
<li>Some more internal fixes.</li>
</ul>
<h2>New protocol?</h2>
<p>So yes, the big improvement is the introduction of a new communication protocol between <a href="https://kakoune.org/">Kakoune</a> and <a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a>.
Before <code>v0.4.0</code>, the way things worked was:</p>
<ol>
<li>The user typically starts the server from their <code>kakrc</code>, with a line such as
<code>eval %sh{ kak-tree-sitter --kakoune --daemon --session $kak_session }</code>.</li>
<li>This blocks the editor while the server is starting. Because <code>--daemon</code> is passed, once the server is ready, it’s
going to daemonize so that its main process exits and Kakoune gets control back.</li>
<li>The <code>--kakoune</code> argument does one important thing: it asks the server to send some data back to the editor, once the
server is initialized. The data is sent back via the UNIX socket of Kakoune (<code>kak -p</code>). The session is known via
<code>--session $kak_session</code>.</li>
<li>The data is basically a bunch of options, commands and hooks that automatically call back to the server when a
buffer is opened with a supported filetype. Each command are wrapped in a <code>%sh{}</code> block, and uses the
<code>kak-tree-sitter -r</code> interface to format requests as JSON.</li>
<li>For highlighting requests, the FIFO interface of Kakoune (i.e. <code>kak_command_fifo</code> and <code>kak_response_fifo</code>) is used
to stream buffer content via <code>write</code>.</li>
</ol>
<p>What was the issue with that? Well, all those <code>%sh{}</code> implied to open a shell everytime we wanted to perform a request
to the server. And for highlighting, that happens <em>very often</em> (by default, the trigger hook runs for <code>NormalIdle</code> and
<code>InsertIdle</code>, but you can easily imagine that it runs almost on all keypresses modifying the window or the buffer).
That’s a lot. I quickly came to the realization that I wanted something… faster and with less layers.</p>
<p>See, this <code>kak_command_fifo</code> and <code>kak_response_fifo</code> are pretty interesting FIFO files. The idea is that Kakoune
handles their lifetimes, and they work when you do shell expansions (i.e. <code>%sh{}</code>). You can write to the
<code>$kak_command_fifo</code> Kakoune commands to be executed. This allows to execute Kakoune commands interleaved with shell
commands. You can then use <code>$kak_response_fifo</code> to write back results from Kakoune. I used to write the content of
buffers to <code>$kak_response_fifo</code> and pass the path to this FIFO in the highlight requests so that the server just had
to read from the FIFO and provide the highlights back to Kakoune asynchronously, via <code>kak -p</code>.</p>
<p>FIFOs are wonderful because you can think about them as a 1-1 blocking rendez-vous point between a producer and a
consumer. If a consumer arrives, it blocks until a writer arrives and starts writing, and a writer won’t write until a
consumer arrives. FIFOs do not live on your disk, so writing to them (i.e. <code>write</code> syscall) is super fast and can be
tought as memory-to-memory communication. It’s honestly pretty good.</p>
<p>However, we still have to open a shell to send the requests to the server with this approach… so I needed something
else.</p>
<h2>More FIFOs… MOOOOOORE!</h2>
<p>The idea is actually pretty simple: we can use FIFOs to send requests, in the end. If we have one FIFO for each Kakoune
session, we know that we should only have one writer (i.e. the Kakoune session), and we have a single consumer (the
KTS server). Inside Kakoune, writing to files can be done with the <code>echo -to-file</code> and <code>write</code> command, and we don’t
need a shell for that!</p>
<p>Now the important thing is that Kakoune doesn’t know the path to the FIFO to write requests to, so we still need a
way to get that information. I had two different ideas:</p>
<ol>
<li>We could <em>standardize</em> the place by using the session name. For instance,
<code>$XDG_RUNTIME_DIR/kak-tree-sitter/commands/$kak_session</code>.</li>
<li>Or we could let the server send the path asynchronously, since a Kakoune starting session doesn’t know anything about
the server initially.</li>
</ol>
<p>Currently, this is done with the <code>--kakoune</code> flag. The data that is sent back to the editor contains options
(e.g. <code>%opt{kts_cmd_fifo_path}</code>) so that we know where to write requests.</p>
<blockquote>
<p>In the future, I might actually just standardize the location, since it’s not a random temporary directory.</p>
</blockquote>
<p>The installed hooks, in the <code>kak-tree-sitter</code> group, will contain only a few options, mostly set in the <code>global</code> scope.
The main hook is run whenever a new window is open on a given buffer. A FIFO request is made to KTS with the filetype
(to be more correct, <code>%opt{kts_lang}</code>) and if KTS has the runtime files for this language (grammar, queries, etc.), it
will send back more data to Kakoune, that will be set for the <code>buffer</code> scope. This code will contain the actual
interesting hooks (e.g. <code>NormalIdle</code> and <code>InsertIdle</code>) that will make more FIFO requests to, for instance, highlightthe
buffer.</p>
<p>The content of the buffer is streamed via a FIFO, too — <code>%opt{kts_buf_fifo_path}</code>. Using this pair of FIFOs, we can
send requests to the server and stream buffer content by just using <code>echo -to-file</code> and <code>write</code> Kakoune commands. No
more shell expansions on the hot path!</p>
<blockquote>
<p>Note: currently, buffers must be entirely read by <a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a> because the diffing algorithm is done by
<a href="https://crates.io/crates/tree-sitter-highlight">tree-sitter-highlight</a>, which was great as a PoC, but not ideal now. Partial updates using <code>%val{history}</code> and
<code>%val{uncommitted_modifications}</code> are planned for a future release.</p>
</blockquote>
<p>The only drawback of this approach is that, because we are writing to FIFO files… if one side of the channel is not
there, the other side will block and hang. That could lead to <a href="https://kakoune.org/">Kakoune</a> freezing (for instance, if you kill the server
before trying to quit the editor). However, this is unlikely to happen and is an acceptable tradeoff to me.</p>
<blockquote>
<p>Currently, <a href="https://kakoune.org/">Kakoune</a> doesn’t support writing to files with a timeout, which could help.</p>
</blockquote>
<h2>Conclusion</h2>
<p>This new release is exciting to me. I have learned many things, especially regarding <code>mio</code> vs. <code>tokio</code> (all of those
FIFOs requires a lot of IO synchronization) and optimizing. This new pair of FIFO per session made me realize that,
besides the initial “setup” that requires a shell, we can perfectly communicate with the external world by only writing
to files. In UNIX, everything is, <em>indeed</em>, a file!</p>
<p>The next releases will focus on two main topics:</p>
<ol>
<li>Optimizing even further highlighting, by streaming buffer updates instead of streaming its content. That will
massively increase performance for large buffers.</li>
<li>New features! I plan on adding <strong>theming</strong> (the current situation is weird); <strong>semantic selections</strong> (of course) and…
<strong>indent guidelines</strong>!</li>
</ol>
<p>If you would like to know more about <a href="https://github.com/phaazon/kak-tree-sitter/">kak-tree-sitter</a>, head over
<a href="https://github.com/phaazon/kak-tree-sitter/wiki">to the wiki</a>, which is up to date with <code>v0.4.1</code>.</p>
<p>Have fun and keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 29 Aug 2023 10:35:00 GMT</pubDate></item><item><title>defer and errdefer in Rust</title><link>https://strongly-typed-thoughts.net/blog/rust-defer-errdefer.md</link><description><![CDATA[<p>I have been doing a bunch of experiments in the past months/year around Zig, out of curiosity and because I
wanted to assess whether the language could actually replace Rust in some places — turns out no (<a href="https://strongly-typed-thoughts.net/blog/zig-linear-pwtp">1</a>, <a href="https://strongly-typed-thoughts.net/blog/zig-2025">2</a>).
Most of the feature of Zig can actually be expressed in Rust, but not vice-versa. For instance, there is no
way to implement move semantics / ownership and borrowing in a safe way in Zig. Because the language doesn’t
discriminate between safe and unsafe code, the audit surface is the whole codebase whenever it comes down to
memory safety issue, which is already different in Rust, where you want to minimize the unsafe surface as much
as possible — this is already a huge misconception that C developers make while considering Rust vs. Zig: you
don’t write Rust as you write C, because most of the code you write in Rust is actually safe code, so you
rarely need to actually use pointers (you use refs and safe abstractions).</p>
<p>Anyway, among the features that Zig needs (and that we don’t really need in a pure Rust environment) are
<code>defer</code> and <code>errdefer</code>. It’s actually pretty simple to understand what they do:</p>
<ul>
<li><code>defer &lt;stuff&gt;</code> will execute <code>&lt;stuff&gt;</code> when control flow leaves the current scope, whether normally (end of block),
or abruptly (early-return; <code>try</code>). <code>&lt;stuff&gt;</code> can be a single statement, or a block.</li>
<li><code>errdefer &lt;stuff&gt;</code> will execute <code>&lt;stuff&gt;</code> when control flow leaves on an error.</li>
</ul>
<h1 id="defer-and-errdefer-in-zig"><a href="#defer-and-errdefer-in-zig">#</a> <code>defer</code> and <code>errdefer</code> in Zig</h1>
<p>You usually use <code>defer</code> to deallocate resources after you are done with. For instance:</p>
<pre><code class="language-rust">const ptr = try allocator.alloc(u8, len);
defer allocator.free(ptr);
</code></pre>
<p>At the end of the block or if an error occurs afterwards, <code>ptr</code> will be deallocated. This already creates a
bunch of issues you have to be concerned with:</p>
<ul>
<li><code>ptr</code> cannot escape the current block/function, because at the end of the block, <code>allocater.free(ptr)</code> will
be called. Zig has no way to broadcast this contract, so it’s on the programmer’s to ensure they don’t use
the pointer after being freed.</li>
<li>Zig doesn’t have ownership, so if you copy the pointer, ensure copies do not outlive the current block, or you get
into dangling pointer issues (which are just UB right now; there’s no way to check for it, even at runtime
<a href="https://zigbin.io/4e9d90">zigbin.io</a> — the best to hope for is a segfault).</li>
</ul>
<p>Sometimes, you might want to allocate some fields to return an owned version of them, such as:</p>
<pre><code class="language-rust">const field1 = try allocator.create(Field1);
const field2 = try allocator.create(Field2);
// …

return .{ .field1 = field1, .field2 = field2 };
</code></pre>
<p>You do not want to <code>defer</code>-free them, because that’s on the caller to do so properly (imagine a constructor function,
for instance). <em>However</em>, you might have spotted the problem here: what happens if the allocation of <code>field2</code>
fails? We leak <code>field1</code>. So you would need to do something like this:</p>
<pre><code class="language-rust">const field1 = try allocator.create(Field1);
const field2 = allocator.create(Field2) catch |err| {
  allocator.destroy(field1);
  return err; // pass-through
};
// …
</code></pre>
<p>This is pretty annoying, so Zig has a solution: <code>errdefer</code>. It executes its right-side statement only in case of errors.
So you can write the same code like this:</p>
<pre><code class="language-rust">const field1 = try allocator.create(Field1);
errdefer allocator.destroy(field1);

const field2 = try allocator.create(Field2);
</code></pre>
<p>Those two keywords exist in Zig to make resource management easier. But what about Rust?</p>
<h1 id="what-about-rust"><a href="#what-about-rust">#</a> What about Rust?</h1>
<p>Rust doesn’t need <code>defer</code> nor <code>errdefer</code> because it has the concept of <em>dropping</em> automatically — <code>Drop</code>, also known
as <em>destructors</em>. How does it work? Contrary to the value-based approach of Zig, dropping in Rust is type-based: a type
that implements <code>Drop</code> automatically gets its values dropped by <em>drop glue code</em> at the appropriate place in the code.
You can picture this as inserting the code <code>defer</code> and <code>errdefer</code> would put for you, but automatically. That prevents
forgetting calling destructors:</p>
<pre><code class="language-rust">let name = String::from("Chuck Norris");
</code></pre>
<p>At the end of the block <code>name</code> is declared in, <code>name</code> will be cleaned up by calling its <code>Drop::drop()</code> implementation,
and eventually deallocated as part of its drop logic.</p>
<p>There are interesting consequences of this design:</p>
<ul>
<li>Dropping values implies code running, which means that some code runs when a value is dropped automatically, which is
not apparent — you don’t see a call to <code>Drop::drop()</code>, as the compiler inserts that call at the right places for you.</li>
<li>Users cannot <em>forget</em> to run destructors/cleanup logic, which is safer in terms of memory management, and combined with
ownership, greatly enhance resource management. However, it is possible to leak values to defer call them
<code>Drop</code> glue code by using <code>std::mem::leak()</code>. That has some usecases, but should be avoided in 99,9999% for
obvious reasons.</li>
</ul>
<p>An interesting aspect of <code>Drop</code> is that if you have several ways of implementing the cleanup logic for a given value,
you can create as many types as logic of dropping you can think of, and move the value in the appropriate type wrapper
to select the drop logic.</p>
<p>That last point led me to a funny idea: we can actually implement <code>defer</code> and <code>errdefer</code> in Rust! Why you would want to
is open to discussions, but among the main reasons:</p>
<ul>
<li>Running some logic at the end of the code. For instance, incrementing an atomic counter.</li>
<li>Implementing a C-based API without having to introduce Drop-types. This is probably the main incentive for <code>defer</code>, as you
might need to perform the cleanup logic in a single function. Introducing a type just for that function might look a
bit overkill, so <code>defer</code> could be nice there.</li>
</ul>
<h2 id="defer-in-rust"><a href="#defer-in-rust">#</a> <code>defer</code> in Rust</h2>
<p>Implementing <code>defer</code> in Rust is pretty straight-forward. Remember what Zig does: it actually inserts code that runs at
the right place for you — Zig people keep stating that everything is explicit in Zig, but it’s not completely true,
since <code>defer</code> and <code>errdefer</code> statements will run at various places that the compiler will insert at; it’s not apparent
and thus is very similar to <code>Drop</code> in that regard; you just need to manually ask the compiler to do so.</p>
<p>Let’s look at the Zig example again:</p>
<pre><code class="language-rust">const ptr = try allocator.alloc(u8, len);
defer allocator.free(ptr);
</code></pre>
<p>The <code>defer</code> will call <code>allocator.free(ptr)</code> when we leaves the <strong>current block</strong>, which is akin as having a value
dropped at the end of the current scope in Rust. We can do that by introducing a simple type wrapper (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2024&amp;gist=1791b5bf814dee91baff9182c5e43799">3</a>):</p>
<pre><code class="language-rust">struct Defer&lt;F&gt;
where
    F: FnMut(),
{
    drop: F,
}

impl&lt;F&gt; Drop for Defer&lt;F&gt;
where
    F: FnMut(),
{
    fn drop(&amp;mut self) {
        (self.drop)()
    }
}
</code></pre>
<p>We can then introduce a simple way to construct such a value in the scope of the call site:</p>
<pre><code class="language-rust">macro_rules! defer {
    ($($item:tt)*) =&gt; {
        let _defer = Defer {
            drop: || { $($item)* }
        };
    }
}
</code></pre>
<blockquote>
<p>Why the <code>let _defer</code>? Because Rust, by default, is smart enough to realize that we won’t be using the result, and will
likely immediately deallocate it without waiting for the end of the scope. By using an ignore-binding like that, we
force Rust to drop the value at the end of the scope.</p>
</blockquote>
<p>Using this is easy:</p>
<pre><code class="language-rust">pub fn main() {
    let mut counter = std::sync::atomic::AtomicU32::new(0);

    defer! {
        println!("should be called last: {counter:?}");
        counter.store(1, std::sync::atomic::Ordering::Relaxed);
    }

    {
        defer! {
            println!("should be called first: {counter:?}");
            counter.store(2, std::sync::atomic::Ordering::Relaxed);
        }
    }

    println!("should be called second: {counter:?}");
}
</code></pre>
<p>The first <code>defer!</code> will be called last — at the end of <code>main</code>, and the second one will be called when the enclosing
scope exits, which will then make it the first one to execute. The full output will be:</p>
<pre><code>should be called first: 0
should be called second: 2
should be called last: 2
</code></pre>
<p>Now, how would we take care of implementing <code>errdefer</code> in Rust, though?</p>
<h2 id="errdefer-in-rust"><a href="#errdefer-in-rust">#</a> <code>errdefer</code> in Rust</h2>
<p><code>errdefer</code> is trickier, because it requires the compiler to insert code <em>when an error occurs</em>. But Rust doesn’t have
the concept of error baked-in. <code>Result&lt;A, E&gt;</code> and <code>Option&lt;A&gt;</code> can be used for error handling. Actually, anything
implementing the <code>std::ops::Try</code> trait.</p>
<p>Besides checking whether the thread panicked during a <code>Drop</code> implementation, we cannot really detect and run code when
an error occurred. We have to manually introduce the concept. The idea is actually a combination of two principles:</p>
<ul>
<li>A value which type implements <code>Drop</code> will call code when it’s dropped (end of scope, like for <code>defer</code>).</li>
<li>We can <em>prevent</em> code from running when exiting on success paths, since we do have control on those.</li>
</ul>
<p>The second idea is key. Because we eventually need to return something, we will have to wrap our result in an <code>Ok</code>-like
enum variant. The whole idea goes like so:</p>
<ol>
<li>Build a value by passing a closure that will run in case of errors. We type this value with <code>errdefer: ErrDefer&lt;F&gt;</code>,
which is similar to <code>Defer&lt;F&gt;</code>, but this time we want it to be explicitly named and used by the user.</li>
<li>The function should return an <code>ErrResult&lt;A, E&gt;</code>, which is a type wrapper over a <code>Result&lt;A, E&gt;</code>, but can only be
constructed from an <code>ErrDefer&lt;F&gt;</code>.</li>
<li>Whenever we want to return a success value, we just call something like <code>errdefer.ok(value)</code>, which wraps <code>value</code> in
a <code>ErrResult&lt;A, E&gt;</code>. This call is responsible for ensuring the error closure will not run when the <code>ErrDefer&lt;F&gt;</code> is
dropped.</li>
<li>Whenever we want to return an error value, we call something like <code>errdefer.err(err_value)</code>. This will simply wrap
<code>err_value</code> in an <code>ErrResult&lt;A, E&gt;</code>, and will let the <code>ErrDefer&lt;F&gt;</code> be dropped, calling the closure.</li>
<li>We will optionally need a way to <code>try</code> function calls returning <code>ErrResult&lt;A, E&gt;</code>. More on that below.</li>
</ol>
<p>Let’s start with <code>ErrDefer&lt;F&gt;</code>:</p>
<pre><code class="language-rust">struct ErrDefer&lt;F&gt;
where
    F: FnMut(),
{
    err: Option&lt;F&gt;,
}

impl&lt;F&gt; Drop for ErrDefer&lt;F&gt;
where
    F: FnMut(),
{
    fn drop(&amp;mut self) {
        if let Some(mut f) = self.err.take() {
            f();
        }
    }
}
</code></pre>
<p>Here, we wrap the closure in an <code>Option&lt;F&gt;</code> so that we can specifically decide whether we want to call it in <code>Drop</code> if
it’s <code>Some(f)</code>, or just drop the function without calling it with <code>None</code>. That decision is taken later when returning
values with <code>ErrResult&lt;A, E&gt;</code>, which we can have a look at right now:</p>
<pre><code class="language-rust">struct ErrResult&lt;A, E&gt; {
    result: Result&lt;A, E&gt;,
}

impl&lt;A, E&gt; ErrResult&lt;A, E&gt; {
    fn into_result(self) -&gt; Result&lt;A, E&gt; {
        self.result
    }
}
</code></pre>
<p>Nothing fancy, just a type wrapper over a <code>Result&lt;A, E&gt;</code>. Now let’s see how we can build it:</p>
<pre><code class="language-rust">impl&lt;F&gt; ErrDefer&lt;F&gt;
where
    F: FnMut(),
{
    fn new(f: F) -&gt; Self {
        Self { err: Some(f) }
    }

    fn ok&lt;A, E&gt;(mut self, success: A) -&gt; ErrResult&lt;A, E&gt; {
        self.err = None;
        ErrResult { result: Ok(success) }
    }

    fn err&lt;A, E&gt;(self, e: E) -&gt; ErrResult&lt;A, E&gt; {
        ErrResult { result: Err(e) }
    }
}
</code></pre>
<p>If you look at <code>ErrDefer::ok()</code>, you can see that before wrapping the <code>success: A</code> into an <code>ErrResult&lt;A, E&gt;</code>, we set
the <code>Option&lt;F&gt;</code> closure in <code>ErrDefer&lt;F&gt;</code> to <code>None</code>. That will cause its <code>Drop</code> implementation not to call the closure.
However, if you look at <code>ErrDefer::err()</code>, can see that the closure is not dropped, and as such, when the <code>ErrDefer&lt;F&gt;</code>
will be dropped, it will call its carried error closure.</p>
<p>You will also see the <code>ErrDefer::new()</code> method, which will not be practical to use, as we will have to wrap our code in
a closure. We will use the same idea as with <code>Defer&lt;F&gt;</code> here and use a macro:</p>
<pre><code class="language-rust">macro_rules! errdefer {
    ($($block:tt)*) =&gt; {
        ErrDefer::new(|| { $($block)* })
    }
}
</code></pre>
<p>We can then write code like this:</p>
<pre><code class="language-rust">fn success() -&gt; ErrResult&lt;i32, &amp;'static str&gt; {
    let result = errdefer! {
        println!("will not be called");
    };

    result.ok(10)
}

fn fail() -&gt; ErrResult&lt;i32, &amp;'static str&gt; {
    let result = errdefer! {
        println!("an error occurred");
    };

    result.err("nope")
}
</code></pre>
<p>This is great, but not very useful. Indeed, something that would be more useful would be to able to compose functions
that might fail like we do with the <code>try</code> keyword in Zig; or with the <code>?</code> operator in Rust. The key here is that
<code>ErrResult&lt;A, E&gt;</code> type. As with the previous problem where the compiler was not aware of error paths, the compiler here
cannot help us understand that a function call returning <code>ErrResult&lt;A, E&gt;</code> can actually make the current function fail,
so we need to help it:</p>
<pre><code class="language-rust">macro_rules! errtry {
    ($errdefer:ident, $res:expr) =&gt; {
        match $res.result {
            Ok(success) =&gt; success,
            Err(err) =&gt; return $errdefer.err(err),
        }
    };
}
</code></pre>
<p>This macro is not production-ready and just there to demonstrate the idea, but it still holds: it inspects the content
of the <code>ErrResult&lt;A, E&gt;</code> (actually, anything that has a <code>err()</code> method as first argument, and <code>result: Result&lt;_, _&gt;</code> as
second’s field argument, but this is not important), and convert it to either <code>A</code>, or wrap the <code>E</code> error into the
current <code>ErrDefer&lt;A, E&gt;</code>.</p>
<p>You can use this like so:</p>
<pre><code class="language-rust">fn transitive() -&gt; ErrResult&lt;i32, &amp;'static str&gt; {
    let result = errdefer! {
        println!("something failed deeper the stack…");
    };

    let failed = errtry!(result, fail());

    result.ok(failed * 10)
}
</code></pre>
<p>Combining and calling all of our previous functions:</p>
<pre><code class="language-rust">fn main() {
    let a = success().into_result();
    println!("a = {a:?}");

    let b = fail().into_result();
    println!("b = {b:?}");

    let c = transitive().into_result();
    println!("c = {c:?}");
}
</code></pre>
<p>We can this output:</p>
<pre><code>a = Ok(10)
an error occurred
b = Err("nope")
an error occurred
something failed deeper the stack…
c = Err("nope")
</code></pre>
<p>Something interesting to notice here is that <code>errtry!</code> works only for a <em>function</em>, which is also the case in Rust with
the <code>?</code> operator. This is similar to Zig, but there is a slight difference with my current implementation: Zig will
evaluate the <em>exit scope value</em>, meaning that if we wanted to have the same result in Rust, we would need to exit
all scopes (blocks) manually with a <code>errdefer.ok(value)</code> to have the same effect. Another idea would be to have the
function stored as <code>F</code> directly, in a <code>bool</code> — defaulted to <code>false</code> — set to <code>true</code> when <code>errdefer.err(_)</code> is called,
allowing inspecting that value in <code>Drop::drop()</code> to call if needed.</p>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2024&amp;gist=271920d6981411f98faefe6604eb89ae">Full code</a>.</p>
<p>Now, let’s have a rationale.</p>
<h1 id="okay-and-should-i-use-that"><a href="#okay-and-should-i-use-that">#</a> Okay and… should I use that?</h1>
<p>The honest and short answer is: no, probably not. However, it can be useful in some circumstances, as mentioned earlier,
especially when writing code to interface with C. Sometimes, you might need to have that kind of cleanup that requires
<code>defer</code> and <code>errdefer</code>, and having those macros can massively help you.</p>
<p>On the other side, you could <em>also</em> just go for a type wrapper that implements <code>Drop</code> for you, and you have the same
logic that can be run for both situations automatically (<code>defer</code> / <code>errdefer</code>) without thinking too much about it. This
whole blog entry was an experiment I wanted to make to show that type-based <code>Drop</code> — proper destructors — are more
general and reliable than <code>defer</code> and <code>errdefer</code> — you can’t forget to call them.</p>
<p>With hindsight, I find it a bit a pity to have the Zig compiler infrastructure be able to inject code at scope exit / on
error, but require the user to do it. I <em>know</em> Zig people would tell you that <em>everything should be explicit</em>, which I
think is both a lie and not a strong argument. I think a better argument would be to state that without move semantics,
which is pretty hard to implement correctly (to my knowledge, only Rust does it correctly; more on that in the next
paragraph), Zig cannot have destructors, otherwise, how do you make a difference between a function that returns an
<code>ArrayList(T)</code> — and thus must not deallocate it! — and a function that allocates one, performs some stuff with it, and
wants to get rid of it upon exit? <em>This</em> kind of questioning is why I think people are wrong about <code>defer</code> and
<code>errdefer</code>: they are not a <em>feature</em> per-se, but a <em>required mean</em> because Zig doesn’t have ownership and move
semantics. That strengthened my opinion about the fact that holistic language archictecture moves problems from
user-land into compiler / language. The lack of a static construct forces you to introduce keywords and solutions that
wouldn’t be required if you had the construct.</p>
<p>And why do I think only Rust implements move semantics correctly? The only other language that I know of which has
<em>move</em> semantics is C++, and the way this is implemented in C++ is completely flawed by <em>constructors</em>. Because bindings
cannot be invalidated in C++; when a value is constructed, it can have its content <em>moved-out</em> from another value. You
then need to just copy those fields (bit-wise copy), but manually invalidate the internals of the other value. That
means that the other value’s destructor will still be called, and it will have to inspect its state to realize its
content has been moved. I <strong>hate</strong> this design. It’s overly complicated, and super error-prone.</p>
<p>As always, I hope you enjoyed this small experiment and that it gave you ideas.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 23 Sep 2025 18:00:00 GMT</pubDate></item><item><title>Abstracting shader – Haskell ash library</title><link>https://strongly-typed-thoughts.net/blog/abstracting_over_shader</link><description><![CDATA[<h1>Foreword</h1>
<h2>Abstracting what?</h2>
<p><a href="http://en.wikipedia.org/wiki/Shader">Shaders</a> are very common units in the world of graphics. Even though we’re used to using them for shading<sup class="footnote-reference"><a href="#shading">1</a></sup> purposes, they’re not limited to that. Vulgarisation has ripped off the meaning up and down so much that nowadays, a <strong>shader</strong> might have nothing related to shading. If you’re already doing some graphics, you may know <em>OpenGL</em> and its <em>compute shaders</em>. They have nothing to do with shading.</p>
<p>You might also already know <a href="https://www.shadertoy.com/">shader toy</a>. That’s a great place to host cool and fancy <em>OpenGL</em> shaders<sup class="footnote-reference"><a href="#shadertoy_shaders">2</a></sup>. You write your shaders in GLSL<sup class="footnote-reference"><a href="#GLSL">3</a></sup> then a GLSL compiler is invoked, and your shader is running on the GPU.</p>
<h2>The problem with source based shaders</h2>
<p>So you write your shader as a source code in a host language, for instance in <em>C/C++</em>, <em>Java</em>, <em>Haskell</em>, <em>whatever</em>, and you end up with a shader running on GPU.</p>
<p>There’re two nasty issues with that way of doing though:</p>
<ul>
<li>the shader is compiled at runtime, so if it contains error, you’ll know that after your application starts ;</li>
<li>you have to learn a new language for each target shader compilers.</li>
</ul>
<p>They’re both serious issues I’m going to explain further.</p>
<h3>Issue n°1: compiled at runtime</h3>
<p>This is problematic for a good reason: a lot of shaders are application dependent. <strong>Shadertoy</strong> is a nice exception, just like modeling tools or material editors, but seriously, in most applications, end users are not asked the shaders to run with. In a game for instance, you write all the shaders while writing the game, and then release the whole package.</p>
<blockquote>
<p><em>Yeah… What’s about additional content? Per-map shaders, or that kind of stuff?</em></p>
</blockquote>
<p>Those shaders are like resources. That doesn’t imply using them as is though. We could use dynamic relocatable objects (<strong>.so</strong> or <strong>.dll</strong>) for instance.</p>
<blockquote>
<p><em>What compile-time compilation gives you?</em></p>
</blockquote>
<p>It gives you something hyper cool: <strong>host language features</strong>. If you have a strongly-typed language, you’ll benefit from that. And that’s a huge benefit you can’t get away from. If you’re writing an incorrectly typed shader, your application / library won’t compile, so that the application won’t react in weird way at run-time. That’s pretty badass.</p>
<h3>Issue n°2: languages, languages…</h3>
<p>This issue is not as important as the first one, but still. If you’re working on a project and you target several platforms (among ones using <em>OpenGL</em>, <em>OpenGL ES</em>, <em>DirectX</em> and a <em>soft renderer</em>), you’ll have to learn several shading languages as well (<strong>GLSL</strong>, <strong>HLSL</strong><sup class="footnote-reference"><a href="#HLSL">4</a></sup>).</p>
<p>In order to solve that, there’re two ways to go:</p>
<ul>
<li>a DSL<sup class="footnote-reference"><a href="#DSL">5</a></sup> ;</li>
<li>an EDSL<sup class="footnote-reference"><a href="#EDSL">6</a></sup>.</li>
</ul>
<p>A DSL is appealing. You have a standalone language for writing shaders, and backends for a compiler/language. However, that sounds a bit overwhelming for such an aim.</p>
<p>An EDSL is pretty cool as well. Take a host language (we’ll be using <strong>Haskell</strong>) and provide structure and construction idioms borrowed from such a language to create a small embedded one. That is the solution I’m going to introduce.</p>
<h1>Ash</h1>
<p><strong>Ash</strong> stands for <strong>Abstract Shader</strong>. It’s a Haskell package I’ve been working on for a few weeks now. The main idea is:</p>
<ul>
<li>to provide a typesafe shading language compiled at compile-time;</li>
<li>to provide backends;</li>
<li>to provide a nice and friendly <em>haskellish</em> interface.</li>
</ul>
<p>I guessed it’d be a good idea to share my thoughts about the whole concept, since I reckon several people will be interested in such a topic. However, keep in mind that Ash is still a big work in progress. I’m gonna use several blog entries to write down my thoughts, share it with you, possibly enhance Ash, and finally release a decent and powerful library.</p>
<p>If you’re curious, you can find Ash <a href="https://github.com/phaazon/ash">here</a>.</p>
<h2>Basics</h2>
<p><strong>Ash</strong> is a library that provides useful tools to build up <em>shaders</em> in <strong>Haskell</strong>. In <strong>Ash</strong>, a <em>shader</em> is commonly function. For instance, a vertex shader is a function that <em>folds</em> vertex components down to other ones – possibly <em>maps</em>, but it could <em>add</em>/<em>remove</em> components as well – and <em>yields</em> extra values for the next stages to work with.</p>
<p>You write a shader with the Ash EDSL then you pass it along to a <strong>backend compiler</strong>.</p>
<p>Here are two examples. In order for you to understand how Ash works, I’ll first write the GLSL (330 core) shader, then the Ash one.</p>
<h3>First example: a simple vertex shader</h3>
<p>Let’s write a vertex shader that takes a position and a color, and projects the vertex using a perspective matrix, a view matrix and the object matrix of the object currently being rendered and passes the color to the next stage:</p>
<pre><code>#version 330 core

in vec3 pos;
in vec4 col;

out vec4 vcol;

uniform mat4 projViewModel;

void main() {
  vcol = col; 
  gl_Position = projViewModel * vec4(pos, 1.);
}
</code></pre>
<p>And now, the Ash one:</p>
<pre><code>vertexShader :: Ash (M44 Float -&gt; V3 Float :. V4 Float -&gt; V4 Float :. V4 Float)
vertexShader = lam $ \proj -&gt; lam $ \v -&gt;
  let pos :. col = v
  in proj #* v3v4 pos 1 :. col
</code></pre>
<p><code>Ash</code> is the type used to lift the shading expression up to Haskell. You use it to use the EDSL. It actually represents some kind of HOAST<sup class="footnote-reference"><a href="#HOAST">7</a></sup>.</p>
<p>Then, you can find <code>M44</code>, <code>V3</code>, <code>V4</code> and <code>(:.)</code>.</p>
<p><code>M44</code> is the type of <strong>4x4 matrices</strong>. Since projection matrix, view matrix and model matrix are all 4x4 floating matrix, <code>M44 Float</code> makes sense.</p>
<p><code>V3</code> and <code>V4</code> represents 3D and 4D vectors, respectively. <code>V3 Int</code> is three ints packed in a vector as well as <code>V4 Float</code> is four floats packed in a vector. You’ll also meet <code>V2</code>, which is… the 2D version.</p>
<p><code>(:.)</code> is a <a href="https://www.haskell.org/ghc/docs/7.2.1/html/users_guide/data-type-extensions.html#infix-tycons">type operator</a> used to build tuples. You can see <code>(:.)</code> as a generalized <code>(,)</code> – the default Haskell pair type – but <code>(:.)</code> is more power full since it can flatten expressions:</p>
<pre><code>a :. (b :. c) = a :. b :. c
</code></pre>
<p>The <code>(:.)</code> has a lot of uses in Ash. In our cases, a chain of <code>(:.)</code> represents a vertex’ components.</p>
<p>So our <code>vertexShader</code> value is just a function that takes a matrix and a vertex (two components) and outputs two values: the new position of the shader, and the color. Let’s see the body of the function.</p>
<pre><code>lam $ \proj -&gt; lam $ \v -&gt;
</code></pre>
<p>This is a pretty weird expression, but I haven’t found – yet? – a better way to go. <code>lam</code> is a combinator used to introduce lambdas in the EDSL. This expression then introduces a lambda that takes two values: <code>proj</code> and <code>v</code>. You can read that as:</p>
<pre><code>\proj v -&gt;
</code></pre>
<p>Next:</p>
<pre><code>let pos :. col = v
</code></pre>
<p>This is the tricky part. That <em>let expression</em> extracts the components out of the vertex and binds them to <code>pos</code> and <code>col</code> for later use.</p>
<pre><code>in proj #* v3v4 pos 1 :. col
</code></pre>
<p><code>(#*)</code> is a cool operator used to multiply a matrix by a vector, yielding a new vector.</p>
<p><code>(v3v4)</code> is a shortcut used to to build a <code>V4</code> using a <code>V3</code> by providing the missing value – here, <code>1</code>. You’ll find similar functions, like <code>v2v3</code> and <code>v2v4</code>, to respectively build a <code>V3</code> from a <code>V2</code> by providing the missing value and build a <code>V4</code> from a <code>V2</code> by providing the two missing values.</p>
<p>We finally wrap the result in a tuple <code>(:.)</code>, and we’re done.</p>
<h2>Features</h2>
<p>Ash embeds regular linear expressions (vectors, matrix), textures manipulation, tuples creation, let-bindings, lambda functions (they represent shader stages up to now), and a lot of other features.</p>
<p>Each feature is supposed to have an implementation in a given <em>backend</em>. For instance, in the GLSL backend, a lambda function is often turned into the well done <code>main</code> function. Its parameters are expanded to as <code>in</code> values, and
control parameters are <code>uniform</code> variables.</p>
<p>Each backend is supposed to export a <code>compile</code> function – the name may varies though. However, each backend is free to compiles to whatever smart they think is. For instance, compiling an Ash shader to GLuint (shader stage) is not very smart since it would use <code>IO</code> and handles error a specific way we don’t want it to do. So the GLSL compiler is a function like <code>glslCompile :: Ash … -&gt; Either CompilationError String</code>, and the <code>String</code> can be used as a regular GLSL source code string you’ll pass to whatever implementation of shader you’ve written.</p>
<h1>What’s next?</h1>
<p>I need to finish the implentation of the EDSL, and write the whole GLSL 330 compiler. If it’s a success, I’ll accept pull-requests for other famous compilers (other GLSL version compilers, HLSL, and so on and so forth).</p>
<p>Once that done, I’ll write a few other blog entries with example as a proof-of-concept :)</p>
<div class="footnote-definition" id="shading"><sup class="footnote-definition-label">1</sup>
<p>Shading is the process in which <em>primitives</em> (sets of <em>vertices</em>) are turned into colors (i.e <em>fragments</em>, a.k.a. <em>pixels</em> or <em>texels</em>).</p>
</div>
<div class="footnote-definition" id="shadertoy_shaders"><sup class="footnote-definition-label">2</sup>
<p>Actually, they’re <strong>fragment shaders</strong>.</p>
</div>
<div class="footnote-definition" id="GLSL"><sup class="footnote-definition-label">3</sup>
<p><a href="https://www.opengl.org/documentation/glsl/">OpenGL Shading Language</a>.</p>
</div>
<div class="footnote-definition" id="HLSL"><sup class="footnote-definition-label">4</sup>
<p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509638(v=vs.85).aspx">High Level Shading Language</a>.</p>
</div>
<div class="footnote-definition" id="DSL"><sup class="footnote-definition-label">5</sup>
<p><a href="http://en.wikipedia.org/wiki/Domain-specific_language">Domain-Specific Language</a>.</p>
</div>
<div class="footnote-definition" id="EDSL"><sup class="footnote-definition-label">6</sup>
<p>Embedded Specific Language.</p>
</div>
<div class="footnote-definition" id="HOAST"><sup class="footnote-definition-label">7</sup>
<p><a href="http://en.wikipedia.org/wiki/Higher-order_abstract_syntax">High-Order Abstract Syntax tree</a>; for the purpose of this paper, you don’t have to fully understand them to get your feet wet with Ash (which is cool, right? :) ).</p>
</div>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 14 Nov 2014 00:00:00 GMT</pubDate></item><item><title>luminance 0.1 released!</title><link>https://strongly-typed-thoughts.net/blog/luminance_0.1</link><description><![CDATA[<h1>Here we are</h1>
<p><a href="http://hackage.haskell.org/package/luminance-0.1">luminance-0.1</a> was released yesterday night,
along with <a href="http://hackage.haskell.org/package/luminance-samples-0.1">luminance-samples-0.1</a>! I’ll
need to enhance the documentation and add directions so that people don’t feel too overwhelmed.</p>
<p>I’m also going to write a wiki to help people get their mind wrapped around <strong>luminance</strong>.</p>
<p>If you think something is missing; if you think something could be enhanced; or if you’ve found a
bug, please, feel free to fill in an issue on the
<a href="https://github.com/phaazon/luminance/issues">issues tracker</a>.</p>
<h1>Next big steps</h1>
<p>I need to test the framework. I need a <em>lot</em> of tests. I’ll write a demoscene production with it so
that I can give a good feedback to the community and prove that <strong>luminance</strong> can be used and works.</p>
<p>In the waiting, keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 22 Sep 2015 00:00:00 GMT</pubDate></item><item><title>Mac OS X support in al-0.1.4</title><link>https://strongly-typed-thoughts.net/blog/osx_al</link><description><![CDATA[<h1>Support for Mac users!</h1>
<p>This will be a short announcement about
<a href="https://hackage.haskell.org/package/al">al</a>, a <strong>Haskell</strong> wrapper to the
<strong>OpenAL</strong> C library.</p>
<p>Currently, the wrapper has been successfully tested on Linux – at least it works
well on my <em>Archlinux</em> distro. I made a little program that reads from an <em>.ogg</em>
file and streams the PCM signal to the wrapper – see
<a href="https://hackage.haskell.org/package/libvorbis">libvorbis</a> for further details.
I’ll release the program later on if I find the spare time.</p>
<p>The wrapper might also work on <em>Windows</em> as long as you have
<a href="http://www.freedesktop.org/wiki/Software/pkg-config/">pkg-config</a> installed.
I’d be very happy with feedback from people working on <em>Windows</em>. I don’t want
anyone be put apart with my packages.</p>
<p>However, I’ve never tested the wrapper on <em>Mac OS X</em>. I guessed it wouldn’t work
out of the box because <em>Mac OS X</em> doesn’t use regular libraries to compile and
link – that would have been too easy otherwise, and hell, <em>think different
right?</em> They use something called a <a href="https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WhatAreFrameworks.html">framework</a>.
It’s possible to include a framework in a <strong>Haskell</strong> project by fulfilling
the <code>frameworks</code> field in the <em>.cabal</em> file. I received a simple patch to do
that – <a href="https://github.com/mtolly/al/commit/fef897083c89899b72c2e026a7831f9c48710b5c">here</a>, and I merged it upstream.</p>
<p>Then, <em>Mac OS X</em> is now officially supported. The release version is the
<a href="https://hackage.haskell.org/package/al-0.1.4">0.1.4</a>.</p>
<h1>About stackage</h1>
<p>There’s something else I’d like to discuss. Quickly after the first release of
<a href="https://hackage.haskell.org/package/al-0.1.0.0">al</a>, I decided to push it onto
<a href="https://www.stackage.org">stackage</a>. Unfortunately, there’s a problem with the
package and <em>Ubuntu</em>. For a very dark reason, <em>Ubuntu</em> doesn’t expose
anything when invoking <code>pkg-confg --cflags</code>, even if the files are there – on
<em>Ubuntu</em> they can be found in <code>/usr/include/AL</code>.</p>
<p>That’s very silly because I don’t want to hardcode the location – it might be
something else on other Linux distro. The problem might be related to the
<strong>OpenAL</strong> support in <em>Ubuntu</em> – the <em>.pc</em> file used by <code>pkg-config</code> might be
incomplete. So if you experience that kind of issue, you can fix it by passing
the path to your <strong>OpenAL</strong> headers:</p>
<pre><code>cabal install al --extra-include-dirs=/usr/include/AL
</code></pre>
<p>If <strong>OpenAL</strong> is installed somewhere else, consider using:</p>
<pre><code>find / -name al.h
</code></pre>
<p>I’ll do my best to quickly fix that issue.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 29 Jun 2015 00:00:00 GMT</pubDate></item><item><title>The compile-time deinterleaving interface of luminance-0.40</title><link>https://strongly-typed-thoughts.net/blog/typesafe-deinterleaving</link><description><![CDATA[<p><a href="https://crates.io/crates/luminance/0.40.0">luminance-0.40</a> is finally out, after several months of hardwork. A blog post is available
<a href="https://phaazon.net/blog/luminance-0.40">here</a> if you’ve missed it.</p>
<p>Among all the new features of <a href="https://crates.io/crates/luminance/0.40.0">luminance-0.40</a>, there is one that I think is worth explaining in
details, because it’s… <em>pretty cool</em>. If you like <em>type states</em>, <em>refinement typing</em> and on a
general level, strong typing, you should enjoy this blog article.</p>
<p>While working on <a href="https://crates.io/crates/luminance/0.40.0">luminance-0.40</a>, I spent two weeks working on a feature that is going to have a
huge beneficial impact on how people use luminance. I’ve been wanting to see what people think about
it for a long time, because I think it’s both a very powerful feature and allows to do low-level
memory stuff in a safe way via the type system. Without further ado, let’s dig in.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#the-tess-type">The Tess type</a>
<ul>
<li><a href="#digression-on-interleaved-and-deinterleaved-data-structures">Digression on interleaved and deinterleaved data structures</a></li>
<li><a href="#the-tess-type-revisited">The Tess type… revisited</a></li>
</ul>
</li>
<li><a href="#the-magic-of-type-systems">The magic of type systems</a></li>
<li><a href="#deinterleaving-compile-time-dispatching">Deinterleaving compile-time dispatching</a></li>
<li><a href="#wrap-it-up">Wrap it up</a></li>
</ul>
<!-- vim-markdown-toc -->
<h1>The Tess type</h1>
<p>In pre <strong>luminance-0.40</strong>, it is possible to gather <em>vertices</em> inside a type called <code>Tess</code> (it
stands for <em>tessellation</em>). That type is responsible for holding vertices to render them later.
They might represent point clouds, triangle-based meshes, lines, etc. It contains several properties,
but we are going to focus on three:</p>
<ul>
<li>The actual <em>vertex</em> data (i.e. the held vertices).</li>
<li><em>Indices</em>, that can be used to prevent duplicating vertices and index them to reduce the size
of the mesh.</li>
<li><em>Vertex instances</em>, which are special data that are instance-dependent. In a graphics library or
a graphics engine, an <em>instance</em> often refers to an instance of a template, a pattern, that is to
be shared among instances: the instances simply add some specific values, like positions, colors,
etc. Imagine in a simplistic video game a garden with trees: it’s likely that the trees (all of
them) are rendered via a single draw call, which contains instance data that is going to
customize each instance.</li>
</ul>
<p>In <strong>luminance-0.39</strong>, vertices, indices and instances can be retrieved, both read-only on
read-write, via a mechanism of GPU slicing: you ask luminance to give you a type with which you are
able to use <code>&amp;[T]</code> and <code>&amp;mut [T]</code>.</p>
<p>However… we have several problems. First, the type is <code>Tess</code> and is completely monomorphized. What
it means is that if you create a <code>Tess</code> that contains vertices of type <code>SimpleVertex</code> with indices
of type <code>u8</code>, that information is not encoded in the type system — it is actually encoded as a
value that is read and checked at runtime! When you ask for a slice, for instance to mutate the
content of the <code>Tess</code>, you have to use a function like <a href="https://docs.rs/luminance/0.39.0/luminance/tess/struct.Tess.html#method.as_slice"><code>Tess::as_slice&lt;V&gt; where V: Vertex</code></a>, which
expects you to pass the type of stored vertices — in our case, it would be <code>SimpleVertex</code>. What
happens if someone passed the wrong type? Well, currently, <strong>luminance</strong> checks at runtime that the
stored type is the right one, but this is both wasted checks and not a very elegant API.</p>
<p>The same applies to indices and instance data: you don’t see them in the type. What happens if
you slice the indices with <code>u32</code>? Runtime checks.</p>
<p>Now, there’s also the problem of building. When you build a <code>Tess</code>, you have to pass the vertices
to the <code>TessBuilder</code> type, using functions like <a href="https://docs.rs/luminance/0.39.0/luminance/tess/struct.TessBuilder.html#method.add_vertices"><code>TessBuilder::add_vertices</code></a>. It works, but
it’s not practical. More importantly: if you call several times that function, you will create
something that is called a <em>deinterleaved memory</em> data structure. Let’s digress a bit about that.</p>
<h2>Digression on interleaved and deinterleaved data structures</h2>
<p>Interleaving and deinterleaving makes sense when we talk about several objects / items of a same
type, laid out in memory — typically in arrays. Imagine a type, <code>Vertex</code>, such as:</p>
<pre><code class="language-rust">struct Vertex {
  position: Point3D,
  color: RGBA,
}
</code></pre>
<p>If you take a vector of <code>Vertex</code> (<code>Vec&lt;Vertex&gt;</code>), you get a continuous array of properties in
memory. If you represent that in memory, you have something similar to this for two vertices
(padding omitted):</p>
<pre><code>[x0, y0, z0, r0, g0, b0, a0, x1, y1, z1, r1, g1, b1, a1]
</code></pre>
<p>We say that the memory is <em>interleaved</em>, because you’re going to alternate between fields when
iterating in memory. Everything is interleaved. This kind of memory is what happens when you put a
<code>struct</code> in an array, slice, etc. and is perfectly suited for most situations (even on the GPU).
However, there is a small (yet important) detail: when you iterate (for instance in a <code>for</code> loop)
on that array, you’re going to ask your CPU / GPU to load a bunch of values at once. That is going
to fill your <em>cache lines</em>. If at each iteration you need to use every fields of the vertex, then
that situation is pretty convenient, because you’re going to have a bunch of fields cached ahead
(cache hits).</p>
<p>However… imagine a loop that only needs to access the <code>position</code> field. What’s going to happen is
that your CPU / GPU will still load the same data in cache lines: now you get colors in the cache
that you don’t need and your loop will make more cache misses. What could have been better would
have been to fill the cache lines only with positions. If we had, instead:</p>
<pre><code>[x0, y0, z0, x1, y1, z1]
[r0, g0, b0, a0, r1, g1, b1, a1]
</code></pre>
<p>Those two vectors can then be used independently for each need. Because we only need positions, we
can simply use the first position vector. Now, when the CPU / GPU is going to load something in the
cache, it’s going to cache much more values that we are going to actually use: we get more cache
hits and it’s <em>playa party</em>.</p>
<p>That kind of memory layout is called <em>deinterleaved memory</em>. The way we typically do that is by
simply moving the fields out of the <code>struct</code> and make several arrays of each field.</p>
<p>People tend to use two terms to describe both layouts: <a href="https://en.wikipedia.org/wiki/AoS_and_SoA">AoS and SoA</a>.</p>
<p>So… <del>months</del> <em>years</em> ago, I realized that and decided a needed I better plan. Especially, on
<strong>luminance-0.39</strong>, the way you handle slicing <em>deinterleaved data</em> is… well, inexistent. You
cannot slice such data because it was never supported.</p>
<h2>The Tess type… revisited</h2>
<p>The new type is the following:</p>
<pre><code class="language-rust">pub struct Tess&lt;B, V, I = (), W = (), S = Interleaved&gt;
where ABunchOfThings;
</code></pre>
<p>As you can see, there are a lot of new things there:</p>
<ul>
<li><code>B</code>: the new <em>backend type</em> variable that most types now have; used to forward backend
implementations down the API.</li>
<li><code>V</code>: the <em>vertex type</em>. It’s the type of <code>Vertex</code> you have typically defined yourself using
<a href="https://crates.io/crates/luminance-derive">luminance-derive</a>.</li>
<li><code>I</code>: the <em>index type</em>. Most of the time, you’ll use either <code>u8</code>, <code>u16</code> or <code>u32</code>, depending on
the kind of geometry you index.</li>
<li><code>W</code>: the <em>vertex instance data</em>. Because people might not want that, <code>()</code> is a good default, but
if you really need it, you can basically use any <code>Vertex</code> type here, since vertex instance data
must be part of a <code>Semantics</code>.</li>
<li><code>S</code>: the interleaving mode. Either <code>Interleaved</code> or <code>Deinterleaved</code>.</li>
</ul>
<p>You will find the same type variables with the <code>TessBuilder</code> type.</p>
<h1>The magic of type systems</h1>
<p>The cool thing about that change is how it enabled me to yield much, <em>much</em> better APIs. Consider
the <a href="https://github.com/phaazon/luminance-rs/blob/luminance-0.39/luminance-examples/src/hello-world.rs#L214-L219">previous API to create a deinterleaved <code>Tess</code></a>:</p>
<pre><code class="language-rust">let direct_deinterleaved_triangles = TessBuilder::new(&amp;mut surface)
  .add_vertices(TRI_DEINT_POS_VERTICES)
  .add_vertices(TRI_DEINT_COLOR_VERTICES)
  .set_mode(Mode::Triangle)
  .build()
  .unwrap();
</code></pre>
<p>Notice the two <code>add_vertices</code>. There is no type information checking and ensuring that:</p>
<ul>
<li>You are passing vertex data that are compatible with the <code>Vertex</code> type those fields correspond
to.</li>
<li>You can basically call <code>add_vertices</code> as many times as you want and get a big buffer in GPU
memory that will make no sense.</li>
</ul>
<p>Now, the new API <a href="https://github.com/phaazon/luminance-rs/blob/luminance-0.40/luminance-examples/src/hello-world.rs#L182-L188">looks like this</a>:</p>
<pre><code class="language-rust">let direct_deinterleaved_triangles = surface
  .new_deinterleaved_tess::&lt;Vertex, ()&gt;()
  .set_attributes(&amp;TRI_DEINT_POS_VERTICES[..])
  .set_attributes(&amp;TRI_DEINT_COLOR_VERTICES[..])
  .set_mode(Mode::Triangle)
  .build()
  .unwrap();
</code></pre>
<p>If you try to call <code>set_vertices</code> — the name got changed from <code>add_vertices</code> to <code>set_vertices</code>
— on the builder you get from <code>new_deinterleaved_tess</code>, you will get a compilation error, because you
cannot set vertices on deinterleaved tessellations: you need to set attribute vectors. The
<code>set_attributes</code> has the information that you are doing that for a <code>Vertex</code>, so it can check the
input data you pass and ensure it contains values which type is a field type used in <code>Vertex</code>. If
not, you get a compilation error.</p>
<p>Most importantly: because of how vertices work in luminance, a field type is unique to a vertex: it
doesn’t make sense to use twice the <code>VertexPosition</code> type. If you end up in such a situation, it
means that your <code>Semantics</code> type lacks another variant — remember: vertex fields are basically
semantics-based attributes. That leads to the possibility to automatically find out where exactly
the data you provide needs to go inside the GPU tessellation.</p>
<p>The super cool part is that you can now slice deinterleaved tessellations by simply asking for:</p>
<ul>
<li>The slice of vertices.</li>
<li><em>A</em> slice of attributes.</li>
<li>The slice of indices.</li>
</ul>
<p>In our case, we have a deinterleaved tessellation, which means we cannot slice whole vertices. If
you try to get a slice of <code>Vertex</code>, you will get a compilation error. However, we can retrieve slices
of vertex fields. The way we do this is super simple: we simply call the <code>tess.vertices()</code> or
<code>tess.vertices_mut()</code> methods. It will infer the type of slices you are asking to automatically
slice the right GPU buffer. This is all possible because our types are  unique as the vertex fields.</p>
<pre><code class="language-rust">let positions = tess.vertices().unwrap(); // you have to check for errors normally
let colors = tess.vertices().unwrap(); // you have to check for errors normally
</code></pre>
<blockquote>
<p>Remark: you need to have the types of <code>positions</code> and <code>colors</code> inferred by setting / reading /
passing them around, or you will have to put type ascriptions so that <code>vertices()</code> know what
fields you are referring to.</p>
</blockquote>
<h1>Deinterleaving compile-time dispatching</h1>
<p>So let’s dig a bit into how all this works. The first thing you need to know is that
deinterleaving — the raw concept — is really simple. If you have a type such as:</p>
<pre><code class="language-rust">struct Foo {
  a: A,
  b: B,
  c: C,
}
</code></pre>
<p>We say that <code>Vec&lt;Foo&gt;</code> is the interleaved representation of the collection. The deinterleaved
representation needs to have three vectors of fields:</p>
<pre><code class="language-rust">vec_a: Vec&lt;A&gt;
vec_b: Vec&lt;B&gt;
vec_c: Vec&lt;C&gt;
</code></pre>
<p>In order for a <code>Vertex</code> type to be valid in deinterleaving contexts, we need to have that
tuple of vectors representation. First, we need a mapping between <code>Vec&lt;Foo&gt;</code> to
<code>(Vec&lt;A&gt;, Vec&lt;B&gt;, Vec&lt;C&gt;)</code>. This is the role of two traits: <code>Deinterleave&lt;T&gt;</code> and
<code>TessVertexData&lt;S&gt;</code>.</p>
<p><code>Deinterleave&lt;T&gt;</code> gives, for <code>T</code>, the field rank for <code>T</code>. For instance:</p>
<ul>
<li><code>&lt;Foo as Deinterleave&lt;A&gt;&gt;::RANK == 0</code></li>
<li><code>&lt;Foo as Deinterleave&lt;B&gt;&gt;::RANK == 1</code></li>
<li><code>&lt;Foo as Deinterleave&lt;C&gt;&gt;::RANK == 2</code></li>
</ul>
<p>This is mandatory so that we know exactly which GPU buffers will need to be read / written to
when creating tessellations and slicing them. You don’t have to implement <code>Deinterleave&lt;T&gt;</code> by
yourself: <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> does that automatically for you when you create a <code>Vertex</code> type.
Also, you might be tempted to think that this rank will be used inside the <code>Vertex</code> type to
retrieve data, but since you cannot pass whole vertices… nah. Also, you shouldn’t assume ranks
based on fields declarations in struct (rustc can re-order that).</p>
<p>Next is <code>TessVertexData&lt;S&gt;</code>. It associates an input type — the type of data a tessellation will
receive at creation type — for <code>S</code>, for the implementor type. The easy one
is <code>TessVertexData&lt;Interleaved&gt; for V where V: Vertex</code>. The associated type is simply <code>Vec&lt;V&gt;</code>,
because interleaved geometry simply stores the vertices as a vector of the whole vertex type.
Simple.</p>
<p>It gets more complicated when we talk about deinterleaved geometry.
<code>TessVertexData&lt;Deinterleaved&gt; for V where V: Vertex</code> has its associated type set to…
<code>Vec&lt;DeinterleavedData&gt;</code>. Indeed: there is no simple way with the current stable Rust (and even
nightly) to know the full type of <code>Vertex</code> fields at compile-time here. However, don’t get it wrong:
that <code>Vec</code> is not a vector of vertices. It’s a typically small set of attributes (vectors  too). If
you look at the definition of <code>DeinterleavedData</code>, you get this:</p>
<pre><code class="language-rust">#[derive(Debug, Clone)]
pub struct DeinterleavedData {
  raw: Vec&lt;u8&gt;,
  len: usize,
}
</code></pre>
<p>Yep. Type erasure at its finest. When you pass deinterleaved data, the data is type-erased and
passed as a big blob of bytes, glued with its original size (so that we don’t have to store typing
information – this will be needed when slicing vertex attributes).</p>
<p>Implementing slicing with these traits and data types is now possible: we can add another trait
that we will use to slice vertices, for instance (the <code>backend::VertexSlice</code> trait in luminance)
and based on the type of <code>T</code> in <code>Deinterleave&lt;T&gt;</code>, we can go and grab the GPU buffer we want.
For instance, in both the OpenGL and WebGL luminance backends, buffers are stored in a <code>Vec&lt;_&gt;</code>,
so in order to know which one we need to lookup, we simple use the
<code>&lt;Vertex as Deinterleave&lt;T&gt;&gt;::RANK</code> constant value (a <code>usize</code>) and we’re good to go. You
need two other traits for vertex instance data (<code>InstanceSlice</code>) and vertex indices (<code>IndexSlice</code>),
and you’re good to go.</p>
<h1>Wrap it up</h1>
<p>So, checking at compile-time deinterleaving, in luminance, is done by:</p>
<ul>
<li>Using a trait (<code>Deinterleave&lt;T&gt;</code>) to associate a <em>rank</em> to a field <code>T</code> in a data structure.</li>
<li>Using a trait (<code>TessVertexData&lt;S&gt;</code>) to get the input type of a tessellation: either <code>Vec&lt;T&gt;</code> for
the whole vertices, or <code>Vec&lt;DeinterleavedData&gt;</code> for a set of attributes).</li>
<li>When asking for a typed slice, such as <code>vertices_mut::&lt;T&gt;()</code>, we basically simply require
<code>Deinterleave&lt;T&gt; for V</code> so that we can get a <code>RANK: usize</code>. We can then, in the backend
implementation, find the right GPU buffer and slice it. The reconstruction to a typed slice
(<code>&amp;[T]</code>) is statically assured by the length stored in the <code>DeinterleavedData</code> type and by the
fact <code>DeinterleavedData&lt;T&gt;</code> is implemented for <code>V</code>.</li>
</ul>
<p>A small note though. luminance — actually, I do that with all my code, whatever the language —
considers a lot of operations <code>unsafe</code>. In this case, implementing <code>Deinterleave&lt;T&gt;</code> is <code>unsafe</code>.
The reason is pretty obvious: you can implement <code>Deinterleave&lt;Whatever&gt;</code>, even if your vertex
type doesn’t have a <code>Whatever</code> field. Doing so would yield a hazardous / undefined behavior when
slicing the GPU buffer. <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> takes care of implementing the <code>unsafe</code> interface for
you.</p>
<p>I hope you have learned something new or gotten some ideas. Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 19 Jul 2020 19:39:00 GMT</pubDate></item><item><title>splines-1.0.0 and first release candidate</title><link>https://strongly-typed-thoughts.net/blog/splines-1.0.0-rc.1</link><description><![CDATA[<h1>splines-1.0.0: the first Release Candidate</h1>
<p>Oh hello there! It’s been a while since I wrote on here. My last article was posted on February
25th and then I decided it was time to blog more about what I do. Especially, I think I will try to
blog more often, even if it’s about trivial things, as long as I share my thoughts.</p>
<p>So today, I’m going to talk about the <a href="https://crates.io/crates/splines">splines</a> crate. And more specifically, the <a href="https://crates.io/crates/splines/1.0.0-rc.1">splines-1.0.0-rc.1</a>
release candidate I uploaded today on crates.io.</p>
<h2>Foreword: you said splines?</h2>
<p>Maybe you’re wondering what a spline is, in the first place. A spline is a mathematic curve that is
defined by several polynomials. You can picture them mentally by several small and simple curves
combined to each others, giving the curve an interesting shape and properties. Now why we want
splines is easy to understand: imagine a curve, something smooth and a bit complex (maybe even with
loops). Now, imagine you want to make an object move along that curve. How do you represent that
curve and how to you “make something advance along it?”</p>
<p>Splines are defined by several “control points”, also known “keys”, “knots” or whatever makes sense
to you. Those are just points of interest that will have a direct impact on how the curve will bend
and behave. Most of control points have the interesting property that the curve <strong>will</strong> pass
through them, allowing you to define points you want your object to pass by, for instance. Not all
control points have that property, though.</p>
<p>Now all the magic happens with those control points: when you want to sample a value at a given
control point, you just get back the value carried by this control point… but when you want to
sample in between two control points, for instance, you get back an <em>interpolated value</em>. That
interpolation is the key to why we want splines. Several interpolation mechanisms exist and I’m sure
you know at least two:</p>
<ul>
<li><strong>Constant interpolation</strong>: it means that given two control points, sampling at the first
control point or any part between the control points will give you the carried value of the
first control point. You will get the carried value of the second control point if you sample at
its sampling value. That might be like a useless feature but it is very handy when designing and
implementing “stop-by” movement with camera.</li>
<li><strong>Linear interpolation</strong>: this one you might know it, especially if you attended math courses at
high-school. Linear interpolation is often referred to as “drawing a straight line”. And
typically, “everything linear” can often graphed with a straight line! Thank about linear
complexity (<em>O(n)</em>) and what it looks like on a graph. Linear interpolation that gives you a
strict blending of arguments depending at which distance you sample from them. If sample at
<code>t = 0</code>, you get 100% of the first value and 0% of the second value. If you sample at <code>t = 0,5</code>,
you get an equal amount of both value (so you get the mean between both value). At <code>t = 1</code>, you
get 0% of the first value and 100% of the second value. Simple but really useful.</li>
<li><strong>Cosine interpolation</strong>: this one is known by people who are used to animation, graphics,
motion design, etc. Cosine interpolation gives you a smoother interpolation than the linear one
and provides a first derivative that is more interesting (i.e. it translates to <em>acceleration</em>).</li>
<li>Others: <a href="https://crates.io/crates/splines">splines</a> provide you with some other interpolation modes and some (that I really want!)
are missing. For instance, cubic <a href="https://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier interpolation</a> is something I want and that I already
have coded in the Haskell version of the <a href="https://crates.io/crates/splines">splines</a> crate. It should be easy to add but so far,
no one has asked for them and I don’t need them, so… ;)</li>
</ul>
<blockquote>
<p>Keep in mind that a spline is a just an abstractio mathematical object and that you can use it for
lots of concepts and situations. For instance, in demoscene productions of mine, I use splines for
camera movement, animation transitions, color-blending and more!</p>
</blockquote>
<h2>The splines crate</h2>
<p>The <a href="https://crates.io/crates/splines">splines</a> crate has been around for some time now and I received some feedback from people on
the <a href="https://github.com/phaazon/splines">GitHub page</a> asking for more features. Basically, the crate
can now:</p>
<ul>
<li>Support failible sampling more sanely (instead of panicking on some very trick situations, you
get a <code>None</code>).</li>
<li>Support for polymorphic sampling types (you have <code>f32</code> but also <code>f64</code> or whatever).</li>
</ul>
<p>I think something interesting to dissect in this blog article is the last feature about polymorphic
sampling types and what I had to do in order to make it happen. Also, this is highly correlated to
a design choice I have made months ago: supporting foreign crates to interpolate specific types is
done inside the <a href="https://crates.io/crates/splines">splines</a> crate, not in other crates. I will discuss why just bellow.</p>
<h2>Polymorphic sampling</h2>
<p>Basically, in <a href="https://crates.io/crates/splines">splines</a>, prior to <code>1.0.0</code>, a spline was polymorphic type mapping a key of type <code>f32</code>
to a control point of type <code>Key&lt;V&gt;</code>, <code>V</code> being the carried value. You then manipulate a <code>Spline&lt;V&gt;</code>.
The problem with that is whenever someone wants to use a different time, like, <code>f64</code> or something of
their own.</p>
<p>My first solution was to turn the spline type to <code>Spline&lt;T, V&gt;</code>. That would allow people to use both
<code>f32</code> and <code>f64</code> as sampling type. The problem kicks in with the code handling those sampling values.
For instance, for the <code>Interpolation::Cosine</code> interpolation mode, I need to take the cosine of a
value which type is either <code>f32</code> or <code>f64</code>. How can we do that in a generic and polymorphic maneer?</p>
<blockquote>
<p><a href="https://crates.io/crates/num-traits">num-traits</a> to the rescue!</p>
</blockquote>
<p>Yeah, so was my first thoughts. I then re-wrote most of the interpolating code using <a href="https://crates.io/crates/num-traits">num-traits</a>…
and then decided to tackle all the feature gates. Because, I haven’t told you yet, but <a href="https://crates.io/crates/splines">splines</a> has
several feature gates allowing you to use, e.g., implement the <code>Interpolate</code> trait for some famous
crates’ types (<a href="https://crates.io/crates/nalgebra">nalgebra</a>, <a href="https://crates.io/crates/cgmath">cgmath</a>), enable <a href="https://crates.io/crates/serde">serde</a> serialization, etc. One feature gate that is
important to me as a demoscener is the <code>"std"</code> feature gate, that, if not defined, makes the
<a href="https://crates.io/crates/splines">splines</a> crate compile with the <code>no_std</code> feature.</p>
<p>And here comes the first problems. The <a href="https://crates.io/crates/num-traits">num-traits</a> has a trait that isn’t compatible with <code>no_std</code>,
the <a href="https://docs.rs/num-traits/0.2.6/num_traits/float/trait.Float.html"><code>Float</code></a> trait. I then sat
in front of my computer and <a href="https://phaazon.net/media/uploads/trying_to_think.jpg">thought</a>. I came
to the realization that whatever library I would use for that trait, I would always get my hands
tied by the <em>contracts of the public interface of that crate</em>, for a feature that is almost central
to the whole <a href="https://crates.io/crates/splines">splines</a> crate. It quickly became obvious that I couln’t use <a href="https://crates.io/crates/num-traits">num-traits</a>.</p>
<blockquote>
<p>Yes, I know about the <a href="https://docs.rs/num-traits/0.2.6/num_traits/float/trait.FloatCore.html"><code>FloatCore</code></a>
trait. However, I was stuck with <code>FloatConst</code> right after that and I just wanted to clean the
interface.</p>
</blockquote>
<p>The monomorphic version of <a href="https://crates.io/crates/splines">splines</a> is easy to implement for <code>no_std</code>, but now I’m stuck because of
some simple traits? Naaah.</p>
<p>So I decided to write my own traits. Enter: <code>Additive</code>, a simple trait with no content but <code>Add</code> and
<code>Sub</code> as supertraits (and some non-important ones for comprehension here), <code>One</code>, a trait that
provides the neutral element of the multiplication for a given type (i.e. the multiplication monoid)
and the <code>Linear&lt;T&gt;</code> trait, providing linear combinations, mandatory for all kind of interpolations
mentioned earlier.</p>
<p>Those traits have universal implementors so that you don’t have to implement them. And enabling a
feature gate for, e.g., <a href="https://crates.io/crates/cgmath">cgmath</a> will also implement those traits accordingly in an enough
polymorphic way that you shouldn’t have to ever worry about them.</p>
<p>Just a little remark of mine: implementing the support for <a href="https://crates.io/crates/cgmath">cgmath</a> was pretty simple and
straight-forward. As feared (because I used this crate), <a href="https://crates.io/crates/nalgebra">nalgebra</a> was way harder to get right —
and I even decided not to implement the <code>Point&lt;..&gt;</code> type because I’ve been struggling all the
afternoon with error types that reminded me old and dark times with C++ Boost library. I’ve already
discussed that with people from the <a href="https://crates.io/crates/nalgebra">nalgebra</a> world and none seemed to agree with me that this
crate is <em>a little bit too overengineered</em>. For instance, adding the support for <a href="https://crates.io/crates/nalgebra">nalgebra</a> in
splines also adds several mandatory dependencies — i.e. <a href="https://crates.io/crates/num-traits">num-traits</a> and <a href="https://crates.io/crates/alga">alga</a>. It’s not that bad,
but if you want to play with simple vector spaces, I really do not recommend <a href="https://crates.io/crates/nalgebra">nalgebra</a> as it’s a
really convoluted library with lots of <code>type</code> aliases and types with infinite counts of type
variables. Maybe it’s just a confirmation bias of mine, because I’ve always used (and written, back
then in C++) linear libraries that were both simple and fast. <a href="https://crates.io/crates/nalgebra">nalgebra</a> makes me <a href="https://www.boost.org/doc/libs/1_55_0/libs/geometry/doc/html/geometry/design.html">think of this and
this is driving me crazy</a>.
I’m not saying it’s bad. I’m saying it’s not for me and that I wouldn’t recommend it for people
wanting to do simple things — and yes, video game still falls in that <em>simple things</em> bag.</p>
<h2>Let’s talk about feature gates</h2>
<p>So, the design of feature gating in <a href="https://crates.io/crates/splines">splines</a>. I know it might feel a bit weird to have gates like
<code>"impl-nalgebra"</code> or <code>"impl-cgmath"</code> but to me it makes lots of sense. For a simple reason: a crate
exposes several types and functions via its public API. Everything that is not public is not for a
very good reason. Mostly, <em>invariants</em>. An invariant is something that must remain true <em>before</em> a
function / morphism / whatever is called and <em>after</em>. What happens in between can actually break
that invariant. The idea is that <em>“If a function breaks internally an invariant, it must restore it
before returning”</em>, so that the API remains safe to use.</p>
<p>Invariants are central in my way of thinking. Everytime I write some code, everytime I design an
interaction between several piece of algorithms, crates or even cross-language and cross-systems, I
always asks myself <em>“Am I breaking an invariant here? Is it possible to fuck up a state or a hidden
property somewhere?”</em></p>
<p>In Rust, invariants are not a first-class citizen of the language (have a look at how the <a href="https://dlang.org/spec/contracts.html#Invariants">D</a>
language handles <code>invariant</code>: it’s interesting!). However, they still exist. Imagine this piece of
code:</p>
<pre><code class="language-rust">pub struct Even(pub u64);

impl Even {
  pub fn new(nth: u64) -&gt; Self {
    Even(nth * 2)
  }
}

impl Add&lt;u64, Output = Even&gt; for Even {
  fn add(self, rhs: u64) -&gt; Output {
    Even(self.0 + rhs * 2)
  }
}
</code></pre>
<p>Imagine this is wrapped in a crate <code>num-even</code>. Adding a <code>u64</code> to an <code>Even</code> gives you the nth next
even number. All of this is cool. But there is an invariant. The carried <code>u64</code> by <code>Even</code> must…
remain even. If at some time a user handle a <code>Even</code> with an odd <code>u64</code> inside of it, something has
gone wrong terribly. And with this current implementation, it’s very easy to break such an
invariant. Consider:</p>
<pre><code class="language-rust">use num_even::Even;

fn main() {
  let mut a = Even::new(1); // the 1st even number
  a.0 += 1; // oooooooh, fuuuuuuu-
}
</code></pre>
<p>I had long heated debates with people on IRC about why this kind of library should be patched. The
main argument of people who would think it shouldn’t be patched is “Yeah but we want users to have
access to the underlying objects in any way they want.” I think invariants matter most. The patch
version is actually a negative diff:</p>
<pre><code class="language-rust">pub struct Even(u64);
</code></pre>
<p>Done. Here, the invariant cannot be broken anymore because people cannot create or modify <code>Even</code>
by hand. Constraining is powerful. You should try it. :)</p>
<p><a href="https://crates.io/crates/splines">splines</a> holds keys in a <code>Spline&lt;..&gt;</code> that must remain sorted. Hence, you don’t have a direct
access to the keys nor you can mutate them. Mutating keys would imply resorting the keys in a
smart way, which is something I haven’t needed yet.</p>
<p>So, that’s all for me for today. If you have any question about <a href="https://crates.io/crates/splines">splines</a>, please feel free to open
an issue on GitHub. Also, if you’re interested in trying it, please have a look at the
<a href="https://crates.io/crates/splines/1.0.0-rc.1">splines-1.0.0-rc.1</a> release candidate. It contains everything you need to get started!</p>
<blockquote>
<p>The documentation is not up to date and some is missing, but it should be enough to start.</p>
</blockquote>
<p>As always, keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 22 Apr 2019 01:10:00 GMT</pubDate></item><item><title>Rust traits and their (lack of) privacy</title><link>https://strongly-typed-thoughts.net/blog/rust-traits-privacy</link><description><![CDATA[<h1>On traits privacy interfaces</h1>
<p>I’ve been facing an issue several times in my Rust lifetime experience and never ever have
come up with a pragmatic solution to this problem yet. This problem comes up as follows:</p>
<ul>
<li>Given a trait defined in a library, some code of the library uses its
associated types, methods and/or constraints. <strong>Those items use internal representations
or dependencies that must not appear in the public interface of the library.</strong></li>
<li>That trait is important for the public interface because it gives a practical list of
types that can be used with functions constrained with that trait.</li>
<li>Since we need that trait in the public interface, we <strong>must make it public</strong>.</li>
<li>However, <strong>in Rust, public traits will expose their whole internals to the world.</strong></li>
</ul>
<p>As you can see, we have a situation here. Rust doesn’t allow to expose a trait without
showing its internals. Imagine the following code:</p>
<pre><code>pub fn a_cool_function&lt;T&gt;(id: usize, foo: T) where T: Foo {
  // …
}

pub trait Foo: Sized {
  type Cons: Sized;

  fn compute(self, a: Self::Cons) -&gt; Self;
}

impl Foo for String {
  type Cons = char;

  fn compute(mut self, a: Self::Cons) -&gt; Self {
    self.insert(0, a);
    self
  }
}

impl&lt;T&gt; Foo for VecDeque&lt;T&gt; {
  type Cons = T;

  fn compute(mut self, a: Self::Cons) -&gt; Self {
    self.push_front(a);
    self
  }
}
</code></pre>
<p>We want to export <code>a_cool_function</code>. That function accepts as second argument <em>a type</em> that must
implement the <code>Foo</code> trait. In order for the function not to leak private symbols, <code>Foo</code> then must
be public. However, we don’t want to expose its internals (the <code>Cons</code> associated type nor the
<code>compute</code> method). Why we would want to hide those? I have several points:</p>
<ul>
<li>If you look closely at the implementation, <code>a_cool_function</code> requires its second argument to
implement <code>Foo</code>. Not exposing the internals would then force people to use stock implementors
and will prevent them from providing new ones. This might be wanted for several reasons (unsafe
traits, performances, etc.).</li>
<li>A more sensible reason: imagine that the associated type <code>Cons</code> required a bound on a very
specific trait that is there only because of your implementation details / choices (like a trait
from a dependency). That would leak that trait into the interface, which is not wanted.</li>
<li>As corollary, not exposing the internals of a trait would enable you to change the definition of
the trait without inducing any breaking change, which is an interesting feature.</li>
</ul>
<p>Currently, you can perfectly make a type public without exposing all its methods as public. Why not
having the same power with traits?</p>
<blockquote>
<p>There <em>is</em> a – non-ideal – solution to this problem: <code>#[doc(hidden)]</code> on each items to hide from
the trait. Items tagged with that annotation won’t show up in the documentation, but they will be
definitely usable if a crafty developer reads the source. Not a very good solution to me, thus.</p>
</blockquote>
<h1>Pre-RFC: Enhanced trait privacy</h1>
<p>It’s been a while since I’m looking for a good RFC to introduce this in Rust. This is some needed
jargon, so let’s explain a few terms first:</p>
<ul>
<li>A <em>trait</em> is an open set of types that have common properties, stated by the <em>trait definition</em>.</li>
<li>A <em>trait definition</em> contains associated types, type variables, methods, associated methods
and associated constants.</li>
<li>A <em>bound</em> is found in <code>where</code> clauses to constrain a type or a function.</li>
</ul>
<p>Currently, when you implement <code>Display</code>, you implement the <em>trait</em>. The <code>fmt</code> function you implement
is part of its <em>trait definition</em>. When you write a function like
<code>fn show&lt;T&gt;(x: T) where T: Display</code>, here, <code>Display</code> is not a trait: it’s a <em>bound</em>.</p>
<p><em>Bounds</em> are interesting, because you cannot directly manipulate them. They only appear when
you constrain a function or type. You can combine them, though, with the <code>+</code> operator:</p>
<pre><code>fn borrow&lt;'a, T&gt;(x: &amp;'a T) -&gt; MyBorrow&lt;'a, T&gt; where T: Display + 'a
</code></pre>
<p>This example shows you that lifetimes can be used as bounds as well.</p>
<p>The idea of this RFC is to make a clear distinction between <code>Display</code> as trait and <code>Display</code> as
bound so that it’s possible to use a trait only in bounds position and not implementation. One
major avantage of doing so is to bring completely new semantics to Rust: exposing a trait as
public so that people can pick types that implement the trait without exposing what the trait is
about. This brings a new rule to the game: it’s possible to create ad hoc polymorphism that doesn’t
leak its definition.</p>
<p>The idea is that we love types and we love our type systems. You might come across a situation in
which you need to restrict the set of types that a function can use but in the same time, the
implementation of the trait used to restrict the types is either unsafe, or complex, or depends on
invariants of your crate. In my <a href="https://crates.io/crates/spectra">spectra</a> crate, I have some traits that are currently public that
leak rendering dependencies, which is something I really dislike.</p>
<p>As a <em>prior art</em> section, here’s the wanted feature in Haskell:</p>
<pre><code>{-# LANGUAGE FlexibleInstances #-} -- don’t mind this
{-# LANGUAGE TypeFamilies #-} -- this either

module Lol (
    Foo -- here, we state that we only export the typeclass, not its definition
  ) where

import Data.Text (Text, cons)

class Foo a where
  type Cons a :: *

  compute :: Cons a -&gt; a -&gt; a

instance Foo [a] where
  type Cons [a] = a

  compute = (:)

instance Foo Text where
  type Cons Text = Char

  compute = cons
</code></pre>
<p>Trying to use either the <code>Cons</code> associated type or <code>compute</code> function in a module importing <code>Lol</code>
will result in a compiler error, because those symbols won’t be accessible.</p>
<h2>What it would look like in Rust?</h2>
<p>Currently, there is a <em>weird</em> privacy rule around traits. People not coming from Haskell might feel
okay about that, but I learned Rust years ago while being already fluent with Haskell and got
stunned at this (and I still have microseconds of <em>“Wait, do I not need a <code>pub</code> here? Oh yeah, nah
nah nah.”</em>) When you declare a trait as <code>pub trait …</code>, everything in its definition is automatically
<code>pub</code> as well.</p>
<p>This is so weird because <em>everything else</em> in Rust doesn’t work this way. For instance:</p>
<pre><code>struct Bar; // here, Bar is not pub, so it’s private and scoped to the current module it’s defined in

pub(crate) struct Zoo; // not public either but can be used in other modules of the current crate

pub struct Point { // public
  pub x: f32, // public
  pub y: f32, // public
}

pub struct File { // public
  inode: usize // private
}

enum Either&lt;L, R&gt; { // private
  Left(L), // private
  Right(R), // private
}

pub enum Choice&lt;L, R&gt; { // public
  Left(L), // public (*)
  Right(R), // public (*)
}

// (*): enums require their variants to be public if they’re public for obvious pattern-matching
// exhaustiveness reasons

pub struct Foo; // public

impl Foo {
  fn quux(&amp;self); // not public, only callable in the current module

  pub(crate) fn crab_core_is_funny(self) -&gt; Self; // not public but callable from within this crate

  pub fn taylor_swift() -&gt; Self; // public, callable from the crate and dependent crates
}
</code></pre>
<p>But:</p>
<pre><code>trait PrivTrait { // private trait
  fn method_a(); // private
  fn method_b(); // ditto
  pub fn method_c(); // compilation error and wouldn’t make sense anyway
}

pub trait PubTrait { // public trait
  fn method_a(); // public, even without the pub privacy modifier!!!
  pub(crate) fn method_b(); // won’t compile
  pub fn method_c(); // won’t compile
}
</code></pre>
<p>To me, it would make much more sense for Rust to authorize this:</p>
<pre><code>pub trait PubTrait { // public trait
  fn method_a(); // private, only usable from this module
  pub(crate) fn method_b(); // callable only from modules from this crate
  pub fn method_c(); // public
}
</code></pre>
<p>However, I know, I know. Turning this feature on would break pretty much <strong>everyone’s</strong> code. That’s
why I think – if people are interested by this feature – we should instead go for something like
this:</p>
<pre><code>trait PrivTrait { // private trait
  fn method_a(); // private
  pub(crate) fn method_b(); // compilation error: the trait is private
  pub fn method_c(); // compilation error: the trait is private
}

pub trait PubTrait { // public trait
  fn method_a(); // public (backward compatibility)
  pub(crate) fn method_b(); // callable only from modules from this crate
  pub fn method_c(); // public, akin not to use the pub modifier
  priv fn method_d(); // private; only callable from this module
}
</code></pre>
<p>I’d like to point the reader to <a href="https://github.com/rust-lang/rust/issues/8122">this issue</a>. In its
foreword, <a href="https://github.com/alexcrichton">@alexcrichton</a> rightfully explains that removing the <code>priv</code> keyword was great because it
has yielded a rule ever since – quoting him:</p>
<blockquote>
<p><em>“I think that this would really simplify public/private because there’s one and only one rule:
private by default, public if you flag it.”</em></p>
</blockquote>
<p>I feel uncomfortable with the current trait situation because that rule has been broken. <a href="https://github.com/rust-lang/rust/issues/8592">This other
issue</a> pinpoints the problem from another look:
traits shouldn’t set their items visibility based on their own visibilities. This yields weird and
unexpected code rules and precludes interesting design semantics – the one I just described
above.</p>
<p>I hope you liked that article and thoughts of mine. I might write a proper RFC if you peeps are
hyped about the feature – I have personally been wanting this for a while but never found the time
to write about it. Because I care – a lot – about that feature, even more than
<a href="https://phaazon.net/blog/rust-features-documentation">my previous RFC on features discoverability</a>, please feel free to provide constructive criticism,
especially regarding breaking-changes issues.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 27 Nov 2018 04:00:00 GMT</pubDate></item><item><title>My second opinion on Zig</title><link>https://strongly-typed-thoughts.net/blog/zig-2025</link><description><![CDATA[<h1>Zig; what I think after months of using it</h1>
<!--toc:start-->
<ul>
<li><a href="#what-i-like">What I like</a>
<ul>
<li><a href="#arbitrary-sized-integers-and-packed-structs">Arbitrary sized-integers and packed structs</a></li>
<li><a href="#generic-types-are-just-functions-at-the-type-level">Generic types are just functions at the type level</a></li>
<li><a href="#error-union-types">Error Union Types</a></li>
<li><a href="#c-interop-is-probably-the-best">C interop is probably the best</a></li>
<li><a href="#the-build-system-is-nice">The build system is nice</a></li>
</ul>
</li>
<li><a href="#what-i-like-less">What I like less</a>
<ul>
<li><a href="#error-handling">Error handling</a></li>
<li><a href="#shadowing-is-forbidden">Shadowing is forbidden</a></li>
<li><a href="#compile-time-duck-typing">Compile-time duck typing</a></li>
<li><a href="#no-typeclasses-traits">No typeclasses / traits</a></li>
<li><a href="#comptime-is-probably-not-as-interesting-as-it-looks"><code>comptime</code> is probably not as interesting as it looks</a></li>
<li><a href="#no-encapsulation">No encapsulation</a></li>
<li><a href="#memory-safety-is-highly-underestimated-and-fallacious">Memory safety is highly underestimated and fallacious</a></li>
<li><a href="#lazy-compilation-and-compilation-errors-instead-of-warnings">Lazy compilation and compilation errors instead of warnings</a></li>
<li><a href="#no-destructors">No destructors</a></li>
<li><a href="#no-unicode-strings">No (unicode) strings</a></li>
</ul>
</li>
<li><a href="#conclusion-simplicity-rhymes-with-unrestricted-power-which-rhymes-with">Conclusion: simplicity rhymes with unrestricted power, which rhymes with…</a></li>
</ul>
<!--toc:end-->
<p>Ah, <a href="https://ziglang.org/">Zig</a>. I have a love-hate relationship with this one. A “new” (reading: <em>appeared a couple years ago</em>,
already — yes, <em>already</em>), language with high ambitions. Zig was made to run at low-level, with a simple design
to solve many problems C has (macros, allocators, error handling, more powerful types like baked-in tagged
unions and bitsets, a better build system, no hidden control flow, etc.). The language claims to be the C
successor, and today, many people claim that Zig is simpler and even safer than most languages out there —
even Rust! — allowing to focus more on the technical challenges around your problem space rather
than — quoting from the Zig mantra — <em>your language knowledge</em>. I think I need to put the full mantra
because I will reuse it through this article:</p>
<blockquote>
<p>Focus on debugging your application rather than debugging your programming language knowledge.</p>
</blockquote>
<p>We will come back to that.</p>
<p>I had already <a href="https://strongly-typed-thoughts.net/blog/zig-linear-pwtp">written about Zig a while ago</a> when I
initially approached it. I thought the language was really interesting and I needed to dig deeper. That blog
article was made in July, 2024. I’m writing these lines in February, 2025. Time has passed, and yet I have been
busy rewriting some Rust code of mine in Zig, and trying out new stuff <em>not really easy or doable in Rust</em>, in
Zig, just to see the kind of power I have.</p>
<p>Today, I want to provide a more matured opinion of Zig. I need to make the obvious disclaimer that because I
mainly work in Rust — both spare-time and work — I have a bias here (and I have a long past of Haskell projects
too). Also, take notice that Zig is still in its pre-1.0 era (but heck, people still mention that <em>Bun</em>,
<em>Tigerbeetle</em>, <em>Ghostty</em> are all written in Zig, even though it hasn’t reached 1.0).</p>
<p>I split this article in two simple sections:</p>
<ul>
<li>What I like about Zig.</li>
<li>What I dislike about Zig.</li>
</ul>
<h2 id="what-i-like"><a href="#what-i-like">🔗</a> What I like</h2>
<h3 id="arbitrary-sized-integers-and-packed-structs"><a href="#arbitrary-sized-integers-and-packed-structs">🔗</a> Arbitrary sized-integers and packed structs</h3>
<p>Zig has many interesting properties. The first one that comes to mind is its <em>arbitrary-sized integers</em>. That
sounds weird at first, but yes, you can have the regular <code>u8</code>, <code>u16</code>, <code>u32</code> etc., but also <code>u3</code>. At first it
might sound like dark magic, but it makes sense with a good example that is actually a defect in Rust to me.</p>
<p>Consider the following code:</p>
<pre><code class="language-c">struct Flags {
  bool clear_screen;
  bool reset_input;
  bool exit;
};

// …
if (flags.clear_screen || flags.reset_input) {
  // …
}
</code></pre>
<p>That is some very typical need: you want a set of flags (booleans) and depending on their state, you want to
perform some actions. Usually — at least in C, but really everyone should do it this way — we don’t represent
such flags as structs of booleans, because booleans are — most of the time — 8-bit integers. What it means is
that <code>sizeof(Flags)</code> here is <code>3</code> bytes (24 bits, 8 * 3). For 3 bits of information. So what we do instead is
to use a single byte and perform some bitwise operations to extract the bits:</p>
<pre><code class="language-c">#define FLAGS_CLEAR_SCREEN 0b001
#define FLAGS_RESET_INPUT  0b010
#define FLAGS_EXIT         0b100

struct Flags {
  uint8_t bits;
};

bool Flags_contains(Flags const* flags, uint8_t bit) {
  return flags.bits &amp; bit != 0;
}

Flags Flags_set(Flags flags, uint8_t bit) {
  flags.bits |= bit;
  return flags;
}

Flags Flags_unset(Flags flags, uint8_t bit) {
  flags.bits &amp;= ~bit;
  return flags;
}
</code></pre>
<p>That is obviously very error-prone: we use CPP macros (yikes), bits are not properly typed, etc. Zig can
use its arbitrary-sized integer types and <em>packed structs</em> to automatically implement similar code:</p>
<pre><code class="language-rust">const Flags = packed struct {
  clear_screen: bool,
  reset_input: bool,
  exit: bool,
};
</code></pre>
<p>This structure has two <em>sizes</em>: its bit-size, and its byte-size. The bit-size represents the minimum
number of bits it uses (3), and the byte-size represents the number of bytes required to hold the type
(1). We can then use it like so:</p>
<pre><code class="language-rust">if (flags.clear_screen or flags.reset_input) {
  // …
}
</code></pre>
<p>This is an awesome feature, especially because lots of C libraries expect such bitfields, for instance
in the form of a <code>u32</code>. You can easily and naturally convert the <code>Flags</code> type to a <code>u32</code> with
<code>@bitCast(flags)</code> — you need to ensure the booleans are in the right order (big endianness here in Zig
if I recall correctly).</p>
<blockquote>
<p>Note: in Rust, we don’t really have a nice way to do this without requiring a dependency on
<a href="https://crates.io/crates/bitflags">bitflags</a>, which still requires you to provide the binary value of each logical boolean in binary,
usually done with <code>const</code> expressions using <code>1 &lt;&lt; n</code>..</p>
</blockquote>
<h3 id="generic-types-are-just-functions-at-the-type-level"><a href="#generic-types-are-just-functions-at-the-type-level">🔗</a> Generic types are just functions at the type level</h3>
<p>As a Haskeller, this is also something that makes a lot of sense to me. A typical <code>struct Vec&lt;i32&gt;</code> in
most languages is actually a function taking a <code>type</code> and returning a <code>type</code> in Zig;
<code>fn Vec(comptime T: type) type</code>.</p>
<p>Although more verbose, it allows a lot of flexbility, without introducing a new layer specific to the type
system. For instance, specialization can be written in the most natural way:</p>
<pre><code class="language-rust">fn Vec(comptime T: type) type {
  if (@sizeOf(T) == 0) {
    return VecAsUsize;
  } else {
    return struct {
      // …
    };
}
</code></pre>
<p>Another use case that I think is pretty nice is when you need to implement something that depends on the actual
type structure. Zig has compile-time reflection, which means that you can analyze the fields, type information
etc. of a type to implement a specialized version of your algorithm. You can then write your JSON serializer
without depending on a middleware (e.g. in Rust, <a href="https://crates.io/crates/serde">serde</a>).</p>
<h3 id="error-union-types"><a href="#error-union-types">🔗</a> Error Union Types</h3>
<p>This one is a half love / half dislike — you will find the dislike part in the appropriate section of this article.
In Zig, the core of error handling is Error Union Types. It’s straight-forward: take an <code>enum</code> (integer tags)
and glue it with a regular <code>T</code> value in a tagged union. You either get the error discriminant, or your
<code>T</code> value. In Rust terms:</p>
<pre><code class="language-rust">enum ErrorUnion&lt;T&gt; {
  Err(ErrorType),
  Ok(T),
}
</code></pre>
<p>There’s a catch, though. Unlike Rust, <code>ErrorType</code> is global to your whole program, and is structurally typed. Error
types are declared with the <code>error {}</code> construct:</p>
<pre><code class="language-rust">const MyError = error {
  FileNotFound,
  NoPermision,
};
</code></pre>
<p>Error types can be glued together to create more complex error types:</p>
<pre><code class="language-rust">const OtherError = error {
  OOM,
  NotANumber,
};

const AppError = MyError || OtherError;
</code></pre>
<p>Thus, an Error Union Type is <em>either an error or a value</em>, and it’s written <code>E!T</code> (<code>E</code> the error type, <code>T</code>
the value). An interesting aspect of that is that <em>all</em> error types are flat (there is no nesting), and
because they are nominal, you can even return error values without declaring them in the first place. If
you do not care about the actual type of your error, you can use the <code>anyerror</code> special type to refer to
the global error type, or leave it empty (<code>!T</code>) to infer the type based on the body of the function.</p>
<p>All of that is interesting, but one <em>very cool</em> aspect that I think I really miss when writing Rust is
coercion. Because of coercion rules, a regular value <code>T</code> coerces to <code>E!T</code>, and an error type <code>E</code> coerces to
<code>E!T</code>. So you can completely write this:</p>
<pre><code class="language-rust">fn foo() !i32 {
  return 3;
}
</code></pre>
<p>And the same is true for <code>void</code>:</p>
<pre><code class="language-rust">fn checkOrError(input: i32) !void {
  if (input &lt; 10) {
    return error.TooSmall;
  }
}
</code></pre>
<p>There is no need to wrap results in “success paths”, such as <code>Ok(())</code> in Rust.</p>
<h3 id="c-interop-is-probably-the-best"><a href="#c-interop-is-probably-the-best">🔗</a> C interop is probably the best</h3>
<p>If you need to work with C libraries a lot, Zig has some really good features and built-ins baked-in,
especially if you combine them with <code>comptime</code> functions to perform various transformations automatically.</p>
<p><code>@cImport / @cInclude</code> allow to read a <code>.h</code>, parse it at compile-time, and expose its content as Zig
symbols (functions, constants, etc.), exposed in a big struct. For instance:</p>
<pre><code class="language-rust">const c = @cImport({
  @cInclude("GLFW/glfw3.h");
});

// c.glfwInit() is now available
</code></pre>
<p>This is honestly pretty nice, especially since you can iterate on the contents of <code>c</code> with an <code>inline for</code>
at <code>comptime</code> to transform functions the way you want.</p>
<h3 id="the-build-system-is-nice"><a href="#the-build-system-is-nice">🔗</a> The build system is nice</h3>
<p>The build configuration of your project is written in Zig. Even though I don’t think I like <em>configuration as
code</em>, it’s still an interesting idea. It will probably feel like fresh air for
people having to use CMake or similar tools. Zig build module is not very complex to understand and allows a
great deal of flexibility when configuring your targets, optimizations, CPU architectures, ABI, CLI options,
steps and all.</p>
<p>At the current time of writing this, however, even <code>zig</code> does build and package, dependency handling is far
behind anything you might be used to (<code>cargo</code>, <code>pip</code>, <code>node</code>, <code>cabal</code>, etc.). I don’t think it would be fair
to judge it for now.</p>
<h2 id="what-i-like-less"><a href="#what-i-like-less">🔗</a> What I like less</h2>
<p>Even tough Zig is full of nice surprises, it’s also full of what I would call <em>flaws</em> that — personal
opinion – make it a bad fit for robust and sound systems.</p>
<h3 id="error-handling"><a href="#error-handling">🔗</a> Error handling</h3>
<p>As mentioned earlier, error handling in Zig is nice, but it lacks one important feature: you can’t carry
values. It’s likely due to the fact errors flow via a different path than “normal” code, and require owned
values, so in many cases it will require allocations, which is not something Zig wants on its error path.</p>
<p>It makes sense, but it’s really annoying. Something like <code>error.FileNotFound</code> will require you extra
code infrastructure to find <em>exactly which file was not found</em> — maybe you can deduce it from the caller
and match on the error via <code>catch</code> — but maybe you can’t (the path might be computed by the function
returning the error). You can’t even pass integers around in errors, since it’s already used for the
variant of the error itself.</p>
<p>Coming from Rust, obviously, that feels very weak. The <code>Result&lt;A, E&gt;</code> type — that I’ve been using in Haskell
as <code>Either e a</code> — is a godsend. Not having something like that in Zig creates frustration and will likely
generate less interesting error values, or more convoluted code infrastructure around the callers.</p>
<p>On a similar note, the <code>try</code> keyword (which takes an <code>E!A</code> expression and is equal to <code>A</code> or <code>return E;</code>,
depending on the presence of error) allows to propagate errors, but it won’t do much more than that. You can
think of <code>try foo()</code> as the same as <code>foo() catch |err| return err;</code>. That obviously works only with
error union types, so if you happen to have a function returning a <code>?A</code> (optional), you can’t shortcircuit
in a nice way with <code>try</code> and instead needs to use the more verbose <code>orelse return null;</code>. This monadic
approach is pretty powerful in Haskell and Rust, and it wouldn’t hurt much to allow it for error union
types and optionals, since both are built-ins (so you don’t get to pay for the complexity of it).</p>
<p>Another thing I dislike about <code>try</code> is that it’s a prefix keyword which chains really badly:</p>
<pre><code class="language-rust">try (try foo()).bar()
</code></pre>
<h3 id="shadowing-is-forbidden"><a href="#shadowing-is-forbidden">🔗</a> Shadowing is forbidden</h3>
<p><em>Shadowing</em> is the act of using the same name for two different bindings available in a scope hierarchy.
Rust is an excellent example of how to do shadowing correctly. For instance, in Rust:</p>
<pre><code class="language-rust">fn foo(input: &amp;str) {
  // input refers to the argument and has type &amp;str

  let input = input.to_owned();
  // input refers to this function stackframe local owned string and has type String
  // we don’t have access to the argument anymore

  {
    let input = 12;
    // input is integer, and we don’t have access to the String in this block
  }

  // input is the String
}
</code></pre>
<p>This lexical scoping in Rust prevents tons of boilerplate or having to come up with useless names. Zig not
having that causes annoyances, especially with error handling:</p>
<pre><code class="language-rust">const foo = Foo.init();
const foo2 = try foo.addFeatureA();
const foo3 = try foo2.addFeatureB();
const foo4 = try foo3.addFeatureC();
// etc.
</code></pre>
<p>This is maybe a sign of the design choice (no move semantics), but all in all, I don’t like having
to come up with either obscure names or just bad names for something that should be chained calls, or just
reusing the same name. Sometimes, naming things is something we should refrain from.</p>
<h3 id="compile-time-duck-typing"><a href="#compile-time-duck-typing">🔗</a> Compile-time duck typing</h3>
<p>This is an important issue I have with Zig, as it’s present everywhere and implies a lot of cascaded issues.
<code>comptime</code> is not only a keyword, as you might have guessed. Take this example for instance:</p>
<pre><code class="language-rust">fn doMath(x: anytype) @TypeOf(x) {
  // …
}
</code></pre>
<p>There is <strong>no way</strong> to know what that function <em>requires</em> as input. The very first line of the <code>zig zen</code> states</p>
<pre><code> * Communicate intent precisely.
</code></pre>
<p>And even though I do agree with that, I think it’s poorly implemented, for two reasons:</p>
<ol>
<li>Requiring users to read the code of a function is everything but a good and precise way to communicate intent.</li>
<li>Requiring users to read the documentation is flawed by design.</li>
</ol>
<p>The second point is even violated by the standard library itself. Consider
<a href="https://ziglang.org/documentation/master/std/#std.fs.Dir.iterate">std.fs.Dir.iterate</a>, which is a function
used to iterate on a directory entries. There is no documentation at all, nor even comment, and you are left with its
implementation, which is:</p>
<pre><code class="language-rust">pub fn iterate(self: Dir) Iterator {
    return self.iterateImpl(true);
}
</code></pre>
<p>You can click on <a href="https://ziglang.org/documentation/master/std/#std.fs.Dir.Iterator">Iterator</a> to end up on another
documentation page with no actual documentation and a lengthy implementation. I highly suggest reading the comment
on the <code>next()</code> function:</p>
<pre><code class="language-rust">/// Memory such as file names referenced in this returned entry becomes invalid
/// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
</code></pre>
<p>This is <em>incredibly error-prone</em>, and the mere fact that even the standard library fails to abide by the very first
rule of <code>zig zen</code> proves the point that communicating intent precisely via documentation is hard, and probably not a
good design decision for a modern language.</p>
<p>And there is worse. As your software matures and you write more and more code, you will <em>eventually</em> change some
internals. Imagine what would happen if the documentation and/or comments go out of sync with the code. In languages
with type systems, it’s a minor annoyance — you will get angry people complaining about the fact the documentation
has typos / errors, and fixing them will be a patch release. In a language such as Zig where the documentation <em>is</em>
the contract, the source of the issue is vague and harder to understand: do they misuse the function? Is the function
buggy? Both?</p>
<h3 id="no-typeclasses-traits"><a href="#no-typeclasses-traits">🔗</a> No typeclasses / traits</h3>
<p>Another point that I want to discuss is the lack of traits. Zig is a simple language and per-se, I guess, traits
were ruled out — and don’t <a href="https://github.com/ziglang/zig/issues/21544#issuecomment-2382542913">even think about proposing them</a>.
However, they do solve a real-world problem: programming contract. There are two important parts:</p>
<ol>
<li>
<p>Programming contracts convey useful information to the programmers. If you have a function like the <code>doMath</code>
presented above, you have no clue what you are supposed to call it with. If you have something like:</p>
<pre><code class="language-rust">fn do_math&lt;T&gt;(input: T) T where T: Add + Mul {
  // …
}
</code></pre>
<p>You know <em>exactly</em> what you can call it with. Additionally, you get <em>discoverability</em> — it’s super easy
to build a reverse list of implementations based off some traits — Rust does it for you in both the
docs and LSP.</p>
</li>
<li>
<p>Programming contracts are part of the public-facing side of your API. They should be items that are
versioned as the rest. If you decide to change the allowed properties of an input, just updating the
documentation will make it harder to realize you have to do a minor bump and not a patch bump.</p>
<p>Moreover, typeclasses allow for more control on the code <em>you don’t run / test</em>. If someone decides to
implement your trait for their types, having a clear interface to implement is far sounder than having
to go fishing for the programming contract in the documentation, assuming it even exists in the first
place.</p>
</li>
</ol>
<h3 id="comptime-is-probably-not-as-interesting-as-it-looks"><a href="#comptime-is-probably-not-as-interesting-as-it-looks">🔗</a> <code>comptime</code> is probably not as interesting as it looks</h3>
<p>I’ve been wondering a lot about this one lately. Yes, compile-time reflection is nice… but is it really?
When you think about it, implementing a JSON serializer for <code>anytype</code> is probably very unlikely to be
completely <em>true</em>. There will be hidden constraints. For instance, does it make sense to serialize a
pointer? Probably not, so the implementation might check the type info and refuse to serialize a
pointer. And — c.f. the previous section — that information is missing from the interface.</p>
<p>When you think about it, this problem is everywhere, as soon as you use <code>comptime</code>, because of the lack
of traits. If you want to understand the contracts / preconditions / postconditions / invariants… just
read the documentation… and the code, because well, you can’t be sure that the doc is not out of sync.</p>
<p><code>comptime</code> is one of those features that feel like a candy, but has a bitter taste to it. It feels like
C++ templates back again, and the community will not address that.</p>
<p>I hate the idea. After decades of programming, I can say with full certainty that besides teams with
extremely talented people that have been doing that kind of things for more than I’ve lived (old C
programmers, Linux maintainers, maybe a bunch of driver maintainers), it will only end up with tears.
I can’t in good faith suggest to use Zig at work given how complex our programming contracts are. I’m
far from being a perfect programmer — I make mistakes. We do have junior developers in our teams, work
or spare-time projects, people more or less used to our code bases and guidelines, and we are not 100%
perfect while reviewing either. Sometimes some code will be reviewed by someone else that is less severe
than you are. Sometimes it’s your future-self that will actually misuse something you thought was easy to understand. Etc.</p>
<h3 id="no-encapsulation"><a href="#no-encapsulation">🔗</a> No encapsulation</h3>
<p>This is a continuation of the previous section. For a reason that I don’t understand, Zig doesn’t have
encapsulation. What it means is that if you use a type such as <code>ArrayList(i32)</code>, you <em>must</em> know how it’s
implemented to be able to get useful functions and properties, such as its <code>len</code> length. Still with the
same example, the <a href="https://ziglang.org/documentation/master/std/#std.array_list.ArrayListAligned">documentation of <code>ArrayList</code></a>
doesn’t show anything regarding the length of the array, nor even a way to iterate on its
elements. You have to know — read the code please — that what you are looking for is <code>.items.len</code> for the
length. If you want to use the <code>for</code> loop syntax, you do it on the <code>.items</code>:</p>
<pre><code class="language-rust">for (array.items) |item| {
  // …
}
</code></pre>
<p>Even worse, you can actually mutate the internals. How do we ensure that we can expose types to our users
without causing them to break invariants? Well, <code>zig zen</code>: communicate intent precisely, of course! Redirect
them to the code and ensure to slap <code>// INVARIANT</code> in your comments / documentation.</p>
<blockquote>
<p>Sigh…</p>
</blockquote>
<p>I’m a bit dishonest, though. Zig does have a form of access control. At the module level, you can decide
whether a symbol is private (default), or <code>pub</code>. That’s all you get.</p>
<p>In essence, this is similar to C.</p>
<h3 id="memory-safety-is-highly-underestimated-and-fallacious"><a href="#memory-safety-is-highly-underestimated-and-fallacious">🔗</a> Memory safety is highly underestimated and fallacious</h3>
<blockquote>
<p><strong>ERRATUM (Fri Feb 7 2025): criticism was made regarding this section for being unfair, and after several days,
I think I do agree with it. I initially mentioned <a href="https://github.com/rust-lang/miri">miri</a> because it’s easy to get it via <code>rustup</code> and
feels baked in, but a honest comparison would have be to compare it to Zig + Valgrind (or similar).</strong></p>
</blockquote>
<p>I read in numerous places that Zig is safer than <code>unsafe</code> Rust, which is not true, and fallacious.
Andrew Kelley <a href="https://andrewkelley.me/post/unsafe-zig-safer-than-unsafe-rust.html">wrote a blog article</a>
where he built his argumentation on the fact that Rust will compile and will run into UB, while Zig won’t.
Here’s the Rust code:</p>
<pre><code class="language-rust">struct Foo {
    a: i32,
    b: i32,
}

fn main() {
    unsafe {
        let mut array: [u8; 1024] = [1; 1024];
        let foo = std::mem::transmute::&lt;&amp;mut u8, &amp;mut Foo&gt;(&amp;mut array[0]);
        foo.a += 1;
    }
}
</code></pre>
<p>The first thing to say is that there is indeed a UB, and it’s not easy to spot; transmuting a <code>&amp;mut u8</code> to
<code>&amp;mut Foo</code> is the same as casting an aligned pointer to an unaligned pointer, which is indeed UB. Back when
he wrote his article, I’m not sure he knew about <a href="https://github.com/rust-lang/miri">miri</a> — which has been around since
<a href="https://github.com/rust-lang/rust/pull/45002">October 2017</a> — given that his article was released in 2018,
but even then, I think it’s important to draw people’s attention to something important here. Zig does have
this unaligned pointer cast detection by default, but you’re just one <code>rustup components add miri</code> call away:</p>
<pre><code>cargo miri run
…
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
 --&gt; src/main.rs:9:19
  |
9 |         let foo = std::mem::transmute::&lt;&amp;mut u8, &amp;mut Foo&gt;(&amp;mut array[0]);
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: BACKTRACE:
  = note: inside `main` at src/main.rs:9:19: 9:74

note: some details are omitted, run with `miriFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error; 1 warning emitted
</code></pre>
<p>It would have been nice to point that out. Yes, Zig has it by default, but Rust has it too by adding a tool
that is available via <code>rustup</code>. I do have to agree that <code>unsafe</code> Rust is probably harder to write than
regular Zig, because of all the rules you have to manually ensure not to violate (stacked borrows, etc.), and
for 90% of the time, what you will be doing will be to call a C function via the FFI, so you should be fine.
For more complicated <code>unsafe</code> usage, just use <a href="https://github.com/rust-lang/miri">miri</a>. Actually, I think it should be a good practice to use
<a href="https://github.com/rust-lang/miri">miri</a>, at least in the CI.</p>
<p>I could understand Andrew missed that when he wrote his article (hm…). But today? People still mention that
Zig is <em>sooooo</em> much safer than Rust.</p>
<p>So let’s see how Zig is safer now. Up to this day, Zig doesn’t see Use After Free (UAF). Not at compile-time. Not at
runtime. They are just plain Undefined Behavior (UB). For instance:</p>
<pre><code class="language-rust">const std = @import("std");

fn getPtr(value: i32) *i32 {
    var x = value;
    x += 1;
    return &amp;x;
}

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();

    const ptr = getPtr(123);
    try stdout.print("ptr.* = {d}\n", .{ptr.*});

    ptr.* += 10;
    try stdout.print("ptr.* = {d}\n", .{ptr.*});
}
</code></pre>
<p>If you compile this with the default optimization option (<code>Debug</code>), you get this:</p>
<pre><code>$ zig build run
ptr.* = 124
ptr.* = 134
</code></pre>
<p>If you compile with <code>ReleaseFast</code>:</p>
<pre><code>$ zig build -Doptimize=ReleaseFast run
ptr.* = 0
ptr.* = 0
</code></pre>
<p><em>Oh le malaise</em>. This, here; this exact situation is the main reason why I refrain myself from using Zig
in production. The idea that I could end up in such a situation <em>and not even knowing about it</em>. There
are discussions to track all possible UB and turn them into <a href="https://github.com/ziglang/zig/issues/2301">checked illegal behavior</a>.
That issue has been opened since 2019. I’m not saying it won’t ever be a thing — I guess it’s required
to hit 1.0. <em>But still</em>. The language cannot in good faith state that it’s safer than <code>unsafe</code> Rust. Let’s
convert that code to Rust and run miri on it…</p>
<blockquote>
<p>I would <em>obviously</em> never suggest to use raw pointers but instead references, but references will trigger
a compilation error so it’s not really fair for this comparison, and the Zig community always compares it
to <code>unsafe</code> Rust, so let’s get unsafe!</p>
</blockquote>
<pre><code class="language-rust">fn get_ptr(input: i32) -&gt; *mut i32 {
    let mut value = input;
    value += 1;
    &amp;mut value as *mut _
}

fn main() {
    unsafe {
        let ptr = get_ptr(123);
        println!("*ptr = {}", *ptr);

        *ptr += 10;
        println!("*ptr = {}", *ptr);
    }
}
</code></pre>
<p>Running miri:</p>
<pre><code>error: Undefined Behavior: out-of-bounds pointer use: alloc565 has been freed, so this pointer is dangling
  --&gt; src/main.rs:10:31
   |
10 |         println!("*ptr = {}", *ptr);
   |                               ^^^^ out-of-bounds pointer use: alloc565 has been freed, so this pointer is dangling
   |
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
help: alloc565 was allocated here:
  --&gt; src/main.rs:2:9
   |
2  |     let mut value = input;
   |         ^^^^^^^^^
help: alloc565 was deallocated here:
  --&gt; src/main.rs:5:1
   |
5  | }
   | ^
   = note: BACKTRACE (of the first span):
   = note: inside `main` at src/main.rs:10:31: 10:35
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
</code></pre>
<p>The message has some weird mentions in (<code>alloc565</code>), but the actual useful information is there: a pointer is
dangling.</p>
<p>So no, I strongly disagree that Zig is safer than — even — <code>unsafe</code> Rust. Anyone telling you otherwise is
either purposefully lying, or ignorant. And I think I need to mention the misconception that you need to
drop to <code>unsafe</code> often in Rust. This is simply not true. Some libraries — especially interfacing with C
libraries — do use <code>unsafe</code> to make FFI calls (usually, they just do that). <code>unsafe</code> might be required for
some very specific tricks required to implement safer abstractions, but you are not supposed to write a full
library or application in <code>unsafe</code>.</p>
<blockquote>
<p>Just test your software correctly!</p>
</blockquote>
<p>Again that argument… UB cannot be tested and requires statistical analysis — or some kind of runtime protections
that is currently not implemented in Zig — and coverage in a langage that is built off lazy compilation everywhere
is probably not something I will discuss here…</p>
<h3 id="lazy-compilation-and-compilation-errors-instead-of-warnings"><a href="#lazy-compilation-and-compilation-errors-instead-of-warnings">🔗</a> Lazy compilation and compilation errors instead of warnings</h3>
<p>Heck, I thought I would dodge this one. So… yeah… this is a bit embarrassing, but Zig implements lazy compilation.
The idea is that you write code, and it doesn’t compile it. That sounds so <em>exotic</em> that the standard library
has a helper function to mitigate that weird design decision (<a href="https://ziglang.org/documentation/master/std/#std.testing.refAllDecls"><code>std.testing.refAllDecls</code></a>).
You must use it in a <em>very natural way</em>:</p>
<pre><code class="language-rust">test {
  std.testing.refAllDecls(some_module_of_yours);
}
</code></pre>
<p>It’s a common idiom I have seen in many places (Ghostty <a href="https://github.com/ghostty-org/ghostty/blob/f0d276062b78658fc1f3857e9ea104788f1f4e58/src/cli.zig#L10">1</a>
<a href="https://github.com/ghostty-org/ghostty/blob/f0d276062b78658fc1f3857e9ea104788f1f4e58/src/input.zig#L38">2</a> <a href="https://github.com/ghostty-org/ghostty/blob/f0d276062b78658fc1f3857e9ea104788f1f4e58/src/termio/message.zig#L192">3</a>
<a href="https://github.com/ghostty-org/ghostty/blob/f0d276062b78658fc1f3857e9ea104788f1f4e58/src/crash/minidump/stream.zig#L29">4</a>; TigerBeetle
<a href="https://github.com/tigerbeetle/tigerbeetle/blob/d6eff0f84b76ea40ef1898778e7594a319c46698/src/lsm/forest_table_iterator.zig#L199">1</a> <a href="https://github.com/tigerbeetle/tigerbeetle/blob/d6eff0f84b76ea40ef1898778e7594a319c46698/src/lsm/groove.zig#L1505-L1507">2</a>;
vulkan-zig <a href="https://github.com/Snektron/vulkan-zig/blob/1fd5a6e217dfcc7e4406d449924a1feb1ecffbb9/test/ref_all_decls.zig#L101-L122">hmm</a>).</p>
<p>So… if everyone really mitigates this <em>“feature”</em>… was it really worth it? In the end, it makes sense not to
include code that is not participating in the final binary, but… have you thought about refactoring? Have you
thought about systems that add code that will be used <em>later</em>? That happens a lot and I was bitten so many
times while writing some disjoint sections of code and having to spend much more time later when gluing
everything together — <em>because the code was actually never checked</em>!</p>
<p>And there is the opposite problem. Zig makes some assumptions on what is important, so obviously, a parameter of
a function that is not used should be a hard error. It lazy-checks functions you wrote and ignores them if you
don’t use them right away, but refuses to compile arguments you ignore?! I mean, I get <em>why</em> (not using an
argument could hide a bug), but a warning would suffice.</p>
<blockquote>
<p>I haven’t found a way to disable that linter and make it a warning, and I think it’s unlikely to ever happen.</p>
</blockquote>
<h3 id="no-destructors"><a href="#no-destructors">🔗</a> No destructors</h3>
<p>This one <em>I could get</em>, but not implemented the way it is. Zig doesn’t have any sound way to ensure proper
resource management — just like C. See the previous section about communicating intent properly. Zig <em>requires</em>
the call site to deallocate properly, and you have to mention the deinit logic in your documentation.</p>
<p><code>defer</code> – and <code>errdefer</code>, which is for a different usecase, but it’s not really important here — is a tool you
<em>can</em> use at call site to implement resource cleanup, whether it’s memory deallocation or file descriptors close.
The concept has been around for a long time, and as mentioned in the previous sentence, it’s not automatic. The
caller <em>must know</em> that they have to call <code>defer</code>. The documentation <em>might</em> forget to mention it and the user
<em>might</em> forget to call it. On memory cleanup, if you are lucky and your tests run that code, you will get a traced
leak. For more complex resources such as GPU queues, database handles, etc., well, it’s probably a leaked file
descriptor?</p>
<p>I’m not entirely sure whether destructors are the best solution to this problem, but they allow to ensure that
the code calls the cleanup routine. There are alternatives — explored in <a href="https://www.cs.bu.edu/~hwxi/atslangweb/">ATS</a>, probably too complex
for now, requiring linear types and/or proofs to force the caller to get rid of the resource — Rust <em>could have</em>
something along those lines, since it has move semantics and an affine type system; I don’t think people will
trade <code>Drop</code> for linear types though.</p>
<p>It’s a bit a pity, to be honest, to see such a design in Zig, because it does have the infrastructure to do
better in my humble opinion. For instance, this won’t compile:</p>
<pre><code class="language-rust">fn foo() i32 { return 123; }

// …
pub fn main() !void {
  foo();
}
</code></pre>
<p>Because the return value of <code>foo</code> is ignored. To make that compile, you need to explicitly ignore the
returned value:</p>
<pre><code class="language-rust">pub fn main() !void {
  _ = foo();
}
</code></pre>
<p>So it’s a bit weird to see that Zig can prevent compilation if you do not use an argument or the returned
value of a function, but doesn’t provide the proper tooling to force the user to correctly deinit
resources. If you pull the problem a bit more, it shows that the design of Zig — in its current state —
doesn’t permit that, since you would need linear types/values (i.e. a value <em>must</em> be used once and only
once). I would have loved something like a <code>#</code> linear marker that would cause a linear resource to be dangling
if it’s not eventually consumed by a function taking it with the marker as argument:</p>
<pre><code class="language-rust">const Resource = struct {
  …

  pub fn init() #Resource {
    …
  }

  pub fn deinit(r: #Resource) {
  }
};
</code></pre>
<p>Obviously, as soon as you see that, it causes issues with the Zig type system, because you can still bit-copy
values, so you duplicate a resource and might cause double-frees — but copying should be disallowed in a linear
type system. So Zig cannot have linear types/values without changing <em>all</em> the properties of such types, and
purely linear values are often not enough; we eventually want to <em>weaken</em> the restriction (relevant type system)
to be able to use the resource value in a controlled way, via, for instance, borrowing. Which leads to more
complexity in the language, so clearly not something we will ever see in Zig.</p>
<h3 id="no-unicode-strings"><a href="#no-unicode-strings">🔗</a> No (unicode) strings</h3>
<p>The standard library doesn’t have a good support for unicode strings. There is a discussion opened by
<a href="https://drewdevault.com/">Drew DeVault</a> about <a href="https://github.com/ziglang/zig/issues/234">improving strings and unicode handling</a>.
I share the link so that you make your own opinion about what to think of all that, but not having a
string type and recommending users to “iterate on bytes” is a big no to me. It’s the open door to a wide variety
of issues / corrupted data. People in the thread even recommend using <code>.len</code> on <code>[]u8</code> to get length of strings
(never do that, unless you are doing <a href="https://adventofcode.com/">Advent of Code</a>).</p>
<p>It <a href="https://github.com/ziglang/zig/issues/7734#issuecomment-758167124">will never happen</a>, whatever your arguments.
You are left with user-land support, such as <a href="https://codeberg.org/atman/zg">zg</a>.</p>
<h2 id="conclusion-simplicity-rhymes-with-unrestricted-power-which-rhymes-with"><a href="#conclusion-simplicity-rhymes-with-unrestricted-power-which-rhymes-with">🔗</a> Conclusion: simplicity rhymes with unrestricted power, which rhymes with…</h2>
<p><img src="https://strongly-typed-thoughts.net/media/uploads/footguns_everywhere.jpg" alt="" /></p>
<p>I get it. Zig has the ambition to replace C, and as such, doesn’t want to have to deal with complex abstractions.
It trades memory safety and soundness for simplicity. If I’ve been around Zig and actually writing Zig code that
much, it’s because I wanted to check whether
<a href="https://steveklabnik.com/writing/memory-safety-is-a-red-herring">memory safety is a red herring</a> myself. In the
end, maybe Zig is right. Maybe we can achieve <em>enough</em> with a simple language, and deal with the remaining issues
with other approaches. There is nothing wrong with that.</p>
<p><em>However</em>, I think it’s too soon to make any useful conclusion. I really want to have a look at reports about
projects that are entirely written in Zig (Ghostty, TigerBeetle, etc.) after they have matured enough. It’s great
that those projects are successful with Zig; I’m honestly happy for them. But a robust and scientific approach
requires us to go further than just assumptions and feelings. I do think we have data about CVE issues (we
all know the <a href="https://www.chromium.org/Home/chromium-security/memory-safety/">70% number</a>, right?), and it took time and lots of software history to have enough hindsight. I do
think that hindsight is required on Zig to know whether it’s actually contributing to more reliable software. Very
personal opinion: given all the points I mentioned, I really doubt it.</p>
<p>I don’t think that <em>simplicity</em> is a good vector of reliable software. At most, it’s a happy side-effect. It’s
not a requirement, and should remain a secondary mission. What the industry needs is to identify problems (we have)
and designs solutions that solve those problems. Anyone has the right to select a subset of those problems (even Rust
can’t solve everything) and solve those specifically, ignoring the others or pushing their resolution to
user-land / process etc. But here, I think there is a big misconception.</p>
<p>Zig does enhance on C, there is no doubt. I would rather write Zig than C. The design is better, more modern, and
the language is safer. But why stop half way? Why fix <em>some</em> problems and ignore the most damaging ones?</p>
<p>Remember the introduction:</p>
<blockquote>
<p>Focus on debugging your application rather than debugging your programming language knowledge.</p>
</blockquote>
<p>Do you see why this is such a poignant take? Is it really better to spend time in <code>gdb</code> / whatever debugger
you are using, or having your users opening bug issues than having to spend a bit more time reading compiler
error messages? That mantra seems to make it like it’s a bad thing to have a compiler yell at you because you
are <em>very likely</em> doing something you don’t really intend to. Why? It’s a well known fact that bugs don’t
write themselves. At some point, a developer wrote some code <em>expressing</em> a solution that was, in fact,
<em>actually written</em> a different way, in dissonance with the initial intent. This is part of being a human. So
why would you complain that a compiler tells you that what you are doing is very likely wrong? Because in a
few instances, it was unable to see that what you were doing was, actually, fine? You base a full language
experience based solely on some exceptional occurrences / issues? And trust me, I also do have those moments
with Rust from time to time (especially with the lack of view types and partial mutable borrows).</p>
<p>Seeking <em>simple</em> is just not the right move to me. To me, a more robust approach is ensuring people can’t shoot
themselves, while seeking <em>simpler</em>. Simpler doesn’t mean <em>simple</em>; it means that you design a solution,
whatever the complexity, and tries to make it as simple <em>as possible</em>. Rust is honestly not that hard, but it is
definitely not a simple language. However, for all the problems it solves at compile-time, it’s definitely simpler
than all other approaches (e.g. <a href="https://www.cs.bu.edu/~hwxi/atslangweb/">ATS</a>). And it’s not unlikely that it will get simpler and simpler as we discover
new ways of expressing language constructs.</p>
<p>I think my adventure with Zig stops here. I have had too frustration regarding correctness and reliability
concerns. Where people see simplicity as a building block for a more approachable and safe-enough language, I see
simplicity as an excuse not to tackle hard and damaging problems and causing unacceptable tradeoffs. I’m also
fed up of the <em>skill issue</em> culture. If Zig requires programmers to be flawless, well, I’m probably not a good
fit for the role.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 05 Feb 2025 00:25:00 GMT</pubDate></item><item><title>Getting into netwire</title><link>https://strongly-typed-thoughts.net/blog/getting_into_netwire</link><description><![CDATA[<h1>Foreword</h1>
<p>Nowadays, there is a cool concept out there in the <em>Functional Programming</em>
world which is called <em>FRP</em>. It stands for <em>Functional Reactive Programming</em>
and is a pretty decent way to make <em>event-driven</em> programs.</p>
<p>The problem with FRP is that, beside <a href="http://en.wikipedia.org/wiki/Functional_reactive_programming">Wikipedia</a>,
<a href="https://wiki.haskell.org/Functional_Reactive_Programming">Haskell.org</a> and a
few other resources, like <a href="http://conal.net/papers/push-pull-frp/">Conal Elliott</a>’s
papers, we lack learning materials. Getting into FRP is really not trivial
and because of the lack of help, you’ll need to be brave to get your feet
wet.</p>
<p>Because I found it <strong>hard</strong> learning it from scratch and because I think
it’s a good thing to pass knowledge by, I decided to write a few about it so
that people can learn via an easier path.</p>
<p>I’ll be talking about <a href="https://hackage.haskell.org/package/netwire">netwire</a>,
which is not the <em>de-facto</em> library to use in Haskell, because, eh… we don’t
have any. However, netwire exposes a lot of very interesting concepts and
helped me to understand more general abstractions. I hope it’ll help you as
well. :)</p>
<h1>The FRP Thing</h1>
<h2>Event-driven programming context</h2>
<p>In traditional event-driven programming codebase, you’d find constructions
such as events polling (when you explicitely retrieve last occurring events),
callbacks and <em>event handlers</em>. <a href="http://www.glfw.org/">GLFW</a> is a very famous
example of callback uses for event-driven programming. Such functions as
<a href="http://www.glfw.org/docs/latest/group__window.html#gaade9264e79fae52bdb78e2df11ee8d6a">glfwSetWindowCloseCallback</a>
require you to pass a callback that will be called when the event occurs. While
that way to go seems nice, it’s actually error-prone and ill-formed design:</p>
<ul>
<li>you eventually end up with <a href="http://en.wikipedia.org/wiki/Spaghetti_code">spaghetti code</a></li>
<li>debugging callbacks is a true nightmare as the codebase grows</li>
<li>because of the first point, the code doesn’t compose – or in very minimal
ways – and is almost impossible to test against</li>
<li>you introduce side-effects that might introduce nasty bugs difficult to
figure out</li>
<li>debugging is like hitting your head on a tree log</li>
</ul>
<p><img src="http://phaazon.net/pub/spaghetti_code.jpg" alt="" /></p>
<p>However, it’s not black or white. Callbacks are mandatory. They’re useful, and
we’ll see that later on.</p>
<h2>What FRP truely is?</h2>
<p>FRP is a new way to look at event-driven programming. Instead of representing
reaction through callbacks, we consume events over time. Instead of building a
callback that will be passed as reaction to the <code>setPushButtonCallback</code>
function, we consume and transform events over time. The main idea of FRP could
be summed up with the following concepts:</p>
<ul>
<li><em>behaviors</em>: a behavior is a value that reacts to time</li>
<li><em>events</em>: events are just values that have occurrences in time</li>
<li><em>switching</em>: the act of changing of behavior</li>
</ul>
<h3>Behaviors</h3>
<p><a href="http://en.wikipedia.org/wiki/Behavior">According to Wikipedia</a>, <em>a behavior is the
range of actions and mannerisms made by individuals, organisms, systems, or artificial
entities in conjunction with themselves or their environment, which includes the other
systems or organisms around as well as the (inanimate) physical environment</em>. If we try
to apply that to a simple version of FRP that only takes into account the time as
external stimulus, a behavior isany kind of value that consumes time. What’s that?
Well…</p>
<pre><code>newtype Behavior a = Behavior { stepBehavior :: Double -&gt; a }
</code></pre>
<p>A behavior is a simple function from time (<code>Double</code>) to a given value. Let’s take an
example. Imagine you want to represent a cube rotating around the <em>X axis</em>. You
can represent the actual rotation with a <code>Behavior Rotation</code>, because the angle of
rotation is linked to the time:</p>
<p><img src="http://phaazon.net/pub/human_behavior.jpg" alt="" /></p>
<pre><code>rotationAngle :: Behavior Rotation
rotationAngle = Behavior $ \t -&gt; rotate xAxis t
</code></pre>
<p>Pretty simple, see?! However, it’d would more convenient if we could chose the type
of time. We don’t really know what the time will be in the final application. It
could be the current UTC time, it could be an integral time (think of a stepped
discrete simulation), it could be the monotonic time, the system time, something
that we don’t even know. So let’s make our <code>Behavior</code> type more robust:</p>
<pre><code>newtype Behavior t a = Behavior { stepBehavior :: t -&gt; a }
</code></pre>
<p>Simple change, but nice improvement.</p>
<p>That is the typical way to picture a <em>behavior</em>. However, we’ll see later that
the implementation is way different that such a naive one. Keep on reading.</p>
<h3>Events</h3>
<p>An event is <em>something happening at some time</em>. Applied to FRP, an event is a pair
of time – giving the time of occurrence – and a carried value:</p>
<pre><code>newtype Event t a = Event { getEvent :: (t,a) }
</code></pre>
<p>For instance, we could create an event that yields a rotation of 90° around X
at 35 seconds:</p>
<pre><code>rotate90XAt35s :: Event Float Rotation
rotate90XAt35s = Event (35,rotate xAxis $ 90 * pi / 180)
</code></pre>
<p>Once again, that’s the naive way to look at events. Keep in mind that events have
time occurrences and carry values.</p>
<h3>Behavior switch</h3>
<p>You switch your behavior every time. Currently, you’re reading this paper, but you
may go grab some food, go to bed, go to school or whatever you like afterwards.
You’re already used to behavior switching because that’s what we do every day in
a lifetime.</p>
<p>However, applying that to FRP is another thing. The idea is to express this:</p>
<blockquote>
<p><em>“Given a first behavior, I’ll switch to another behavior when a given event
occurs.”</em></p>
</blockquote>
<p>This is how we express that in FRP:</p>
<pre><code>switch :: Behavior t a -&gt; Event t (Behavior t a) -&gt; Behavior t a
</code></pre>
<p>Let me decrypt <code>switch</code> for you.</p>
<p>The first parameter, a <code>Behavior t a</code>, is the
initial behavior. For instance, currently, you’re reading. That could be the
first behavior you’d pass to <code>switch</code>.</p>
<p>The second parameter, an <code>Event t (Behavior t a)</code>, is an event that yields a
new <code>Behavior t a</code>. Do you start to get it? No? Well then:</p>
<pre><code>switch reading finished
</code></pre>
<p><code>reading</code> is the initial behavior, and <code>finished</code> is an event that occurs when
you’re done reading. <code>switch reading finished</code> is then a behavior that equals
to <code>reading</code> until <code>finished</code> happens. When it does, <code>switch reading finished</code>
extracts the behavior from the event, and uses it instead.</p>
<p>I tend to think <code>switch</code> is a bad name, and I like naming it <code>until</code>:</p>
<pre><code>reading `until` finished
</code></pre>
<p>Nicer isn’t it?! :)</p>
<h3>Stepping</h3>
<p>Stepping is the act of passing the input – i.e. the time <code>t</code> in our case – down
to the <code>Behavior t a</code> and extract the resulting <code>a</code> value. Behaviors are
commonly connected to each other and form a <em>reactive network</em>.</p>
<p>That operation is also often refered to as <em>reactimation</em> in certain
implementations, but is more complex than just stepping the world. You don’t
have to focus on that yet, just keep in mind the <code>reactimate</code> function. You
might come across it at some time.</p>
<h1>Before going on…</h1>
<p>Everything you read in that paper until now was just pop-science so that you
understand the main idea of what FRP is. The next part will cover a more
decent and real-world implementation and use of FRP, especially efficient
implementations.</p>
<h1>Let’s build a FRP library!</h1>
<p>The first common error a lot of programmers make is trying to write
algorithms or libraries to solve a problem they don’t even know. Let’s then
start with an example so that we can figure out the problem.</p>
<h2>Initial program</h2>
<h3>Setting up</h3>
<p>Let’s say we want to be able to control a camera with a keyboard:</p>
<ul>
<li><code>W</code> would go forward</li>
<li><code>S</code> would go backward</li>
<li><code>A</code> would left strafe</li>
<li><code>D</code> would right strafe</li>
<li><code>R</code> would go up</li>
<li><code>F</code> would go down</li>
</ul>
<p><img src="http://phaazon.net/pub/camera_drawing.gif" alt="" /></p>
<p>Let’s write the <code>Input</code> type:</p>
<pre><code class="language-haskell">data Input
  = W
  | S
  | A
  | D
  | R
  | F
  | Quit
    deriving (Eq,Read,Show)
</code></pre>
<p>Straight-forward. We also have a function that polls events from <code>IO</code>:</p>
<pre><code class="language-haskell">pollEvents :: IO [Input]
pollEvents = fmap treatLine getLine
  where
    treatLine = concatMap (map fst . reads) . words
</code></pre>
<p>We use <code>[Input]</code> because we could have several events at the same time
(imagine two pressed keys). The function is using dumb implementation
in order to abstract over event polling. In your program, you won’t use
<code>getLine</code> but a function from <a href="https://hackage.haskell.org/package/sdl2">SDL</a>
or similar.</p>
<p>And the camera:</p>
<pre><code class="language-haskell">newtype Camera = Camera { _cameraPosition :: V3 Float } deriving (Eq,Read,Show)

makeLenses ''Camera
</code></pre>
<p><code>V3</code> is a type from <a href="https://hackage.haskell.org/package/linear">linear</a>.
You’ll need that lib then, and <code>import Linear.V3</code> to make the <code>Camera</code> compile.
You’ll also need <a href="https://hackage.haskell.org/package/lens">lens</a> and the GHC
<code>TemplateHaskell</code> extension enabled as well as <code>import Control.Lens</code>.</p>
<p>Ok, let’s react to events!</p>
<h3>First attempt: the regular and naive one</h3>
<p>The idea is to use some kind of state we’ll change on an event. In our case the
state is pretty simple:</p>
<pre><code class="language-haskell">data AppSt = AppSt {
    _appCamera :: Camera
  } deriving (Eq,Read,Show)
  
makeLenses ''AppSt

updateAppSt :: AppSt -&gt; Input -&gt; Maybe AppSt
updateAppSt appst input = case input of
  W -&gt; Just $ appst &amp; appCamera . cameraPosition . _z -~ 0.1
  S -&gt; Just $ appst &amp; appCamera . cameraPosition . _z +~ 0.1
  A -&gt; Just $ appst &amp; appCamera . cameraPosition . _x -~ 0.1
  D -&gt; Just $ appst &amp; appCamera . cameraPosition . _x +~ 0.1
  F -&gt; Just $ appst &amp; appCamera . cameraPosition . _y -~ 0.1
  R -&gt; Just $ appst &amp; appCamera . cameraPosition . _y +~ 0.1
  Quit -&gt; Nothing
</code></pre>
<p>A lot of boilerplate on <code>updateAppSt</code>, but that doesn’t matter that much.
The idea is to modify the application state and just return it for all
inputs but <code>Quit</code>, in which case we return <code>Nothing</code> to express the wish
to quit the application.</p>
<p>I’ve been using that idea for a while. It’s simple, nice and neat, because
we don’t spread <code>IO</code> actions in our program logic, which remains then pure.
That’s a very good way of doing it, and in most cases, it’ll even be
sufficient. However, that idea suffers from a serious issue: <em>it doesn’t
scale</em>.</p>
<p>Who has only one camera? No one. You have a camera – maybe more than just
one, lights, objects, terrains, AI, sounds, assets, maps and so on and so
forth. Our little <code>AppSt</code> type would explode as we add objects. That
<em>doesn’t scale at all</em>. You could, though, go on and add all your objects
in your <code>AppSt</code> – I did that once, it was a pretty harsh experience.</p>
<p>Furthermore, imagine you want to add a new behavior to the camera, like
being able to handle the mouse cursor move – <code>Input</code> being augmented, of
course. You’d need to change / add lines in our <code>updateAppSt</code> function.
Imagine how messy <code>updateAppSt</code> would be… That would, basically, gather
all reactions into a single function. Not neat.</p>
<h2>Adding FRP</h2>
<p>FRP enables us to use our reactive values as if they are regular values.
You can add reactive values, you can substract them, combine them in any
way you want. The semantics of your values should be true for the
reactive values.</p>
<p>Typically, with FRP, you don’t have event handlers anymore. The codebase
can then grow sanely without having to accumulate big states every now
and then. FRP applications scale and compose pretty well.</p>
<p>Let’s start with a simple FRP implementation for our example.</p>
<h3>Naive FRP implementation</h3>
<p>Let’s start with the behavior:</p>
<pre><code>newtype Behavior t a = Behavior { stepBehavior :: t -&gt; a }
</code></pre>
<p>How could we implement our camera’s behavior with that? We actually
can’t since we don’t have any ways to pass events.</p>
<blockquote>
<p><em>“I guess we could build a <code>Behavior t Camera</code> by passing our <code>Input</code>
to the initial function?”</em></p>
</blockquote>
<p>Something like this?</p>
<pre><code class="language-haskell">camera inputs = Behavior $ \_ -&gt; -- implement the behavior with inputs
</code></pre>
<p>That could be a way to go, yeah. However, how would you implement
switching with that? Remember the type of <code>until</code>:</p>
<pre><code>until :: Behavior t a -&gt; Event (Behavior t a) -&gt; Behavior t a
</code></pre>
<p><code>camera</code> is not a behavior, it’s a function from events to a
behavior. You have to apply the events on <code>camera</code> in order to get its
behavior. Once you’ve done that, you cannot pass events to the next
behavior. What a pity. That is more a configured behavior than a
behavior consuming inputs / events.</p>
<p>With the current <code>Behavior t a</code> implementation, a behavior network is
reduced to a function <code>t -&gt; a</code>, basically. Then, the only stimulus you
got from outside is… <em>time</em>. We lack something.</p>
<h3>Arrowized behaviors</h3>
<blockquote>
<p><em>“A way to forward events?”</em></p>
</blockquote>
<p><img src="https://www.haskell.org/arrows/first.png" alt="" /></p>
<p>Yes! But more mainly, a way to extend our <code>Behavior t a</code> with inputs!
Don’t get me wrong, we are not talking about a reactive <em>value</em> here.
We are talking about a reactive <em>relationship</em>:</p>
<pre><code>newtype Behavior t a b = Behavior { stepBehavior :: t -&gt; a -&gt; b }
</code></pre>
<p>That’s way better! Our new behavior represents a relationship between
two reactive objects. The <code>b</code> is our old <code>a</code>, and the new <code>a</code> is the
input! Let’s see what we can do with that.</p>
<pre><code class="language-haskell">camera :: Behavior t [Input] Camera
camera = Behavior (const treatEvents)
  where
    treatEvents events
      | W `elem` events = Camera $ V3 0 0 (-0.1)
      | S `elem` events = Camera $ V3 0 0 0.1
      | otherwise = Camera $ V3 0 0 0
</code></pre>
<p>That is not exactly what we intented to express. Here, if we push the
<code>W</code> button, we just put the camera in <code>V3 0 0 (-0.1)</code>, while we’d like
to move it forward. That is due to the fact we need switching.</p>
<p>The idea is the following: the initial behavior is idleing. We just
don’t do anything. Then, we switch to a given behavior when a given
event occurs. We’ll need recursion so that we can <em>ping-pong</em> between
behaviors. Let’s write the <code>idleing</code> behavior:</p>
<pre><code class="language-haskell">idleing :: Behavior t ([Input],Camera) Camera
idleing = Behavior (const snd)
</code></pre>
<p>That behavior requires as input our <code>Input</code> events list and a
<code>Camera</code> and simply returns the <code>Camera</code>. Pretty nice.</p>
<p>How do we switch then? We need <code>Event</code>. Consider this:</p>
<pre><code>newtype Event t a = Event { getEvent :: (t,a) }
</code></pre>
<p>In order to switch, we need <code>a</code> to be a behavior. In the first place,
we’ll create several <code>Event t [Input]</code> and pass them as input to the
behavior network. How could we change the <code>[Input]</code> to something more
interesting? Simple: <a href="https://wiki.haskell.org/Functor">Functor</a>!</p>
<pre><code class="language-haskell">instance Functor (Event t) where
  fmap f (Event e) = Event (fmap f e)
</code></pre>
<blockquote>
<p><strong>Note</strong>: because of <code>Event t a</code> being a <code>newtype</code>, you should
rather use the GHC <code>GeneralizedNewtypeDeriving</code> extension to
automatically let GHC infer the instance for you.</p>
</blockquote>
<pre><code>newtype Event t a = Event { getEvent :: (t,a) } deriving (Functor)
</code></pre>
<p>Then, we can use the <code>Functor</code> instance to change the type of the
carried value of the event. That’s great because we don’t change the
occurrence time, only the carried value. Transforming events is really
important in FRP. We can then transform the <code>[Input]</code> into a single
behavior:</p>
<pre><code class="language-haskell">inputToBehavior i = case i of
  W -&gt; Behavior $ \_ (_,cam) -&gt; cam &amp; cameraPosition . _z -~ 0.1
  S -&gt; Behavior $ \_ (_,cam) -&gt; cam &amp; cameraPosition . _z +~ 0.1
  A -&gt; -- and so on
  D -&gt; -- and so forth
  F -&gt; -- ;)
  R -&gt; -- ...
  _ -&gt; Behavior $ \_ (_,cam) -&gt; cam
</code></pre>
<p>Pretty simple, see? When we push <code>W</code>, we go forward forever. We could
have implemented the function above with another <code>until</code> call in order
to go back to <code>idleing</code>, making some kind of behavior loop.</p>
<p>However, switching is fairly poorly implemented here. It’s not very
efficient, and requires a ton of boilerplate.</p>
<h2>Auto</h2>
<p>There is a very cool type out there called <code>Auto</code>, used to implement
<a href="http://en.wikipedia.org/wiki/Automaton">automatons</a>.</p>
<pre><code>newtype Auto a b = Auto { runAuto :: a -&gt; (b,Auto a b) }
</code></pre>
<p>An <code>Auto a b</code> is a function-like structure. It has an input and an
output. The difference with a regular function is the fact it also has
a secondary output, which is another <code>Auto a b</code>. That is, it’s the next
automaton to be used.</p>
<p><code>Auto a b</code> wraps pretty cool concepts, such as <em>locally defined states</em>.
It’s also a great ally when implementing switching in a FRP system,
because we can easily state that <code>Behavior</code> ≃ <code>Auto</code>. A <code>Behavior</code> is
a function from the environment state to the next reactive value, and
has also another output representing what to do “next”.</p>
<p>Let’s then change our <code>Behavior</code> type to make it look like a bit more
like <code>Auto</code>:</p>
<pre><code>newtype Behavior t a b = Behavior { stepBehavior :: t -&gt; a -&gt; (b,Behavior t a b) }
</code></pre>
<p>Yeah, that’s it! That’s a pretty good start!</p>
<h2>Useful abstractions</h2>
<p>Before going on, I’d like to introduce those scary abstractions you are
afraid of. Because they’re actually not. They’re <strong>all simple</strong>. At least
for Haskell purposes.</p>
<p><strong>Note</strong>: I do know we could simply use the GeneralizedNewtypeDeriving`
extension but I want to detail all the implementation, so we’ll see
how to implement all the nice abstractions.</p>
<h3>Arrow</h3>
<p><a href="https://www.haskell.org/arrows">Arrows</a> are a generalization of functions
along the axis of computation. A computation has inputs and outputs. So
does a behavior.</p>
<p>Although arrows are not really used in Haskell, they’re
ultra simple (themselves and the common combinators built over the
abstraction) and useful in some cases.</p>
<p>In order to implement arrows, we need to provide code for both the <code>arr</code>
function, which type is <code>arr :: (Arrow a) =&gt; (b -&gt; c) -&gt; a b c</code> and
<code>first</code>, which type is <code>first :: (Arrow a) =&gt; a b c -&gt; a (b,d) (c,d)</code>.
<code>arr</code> is used to lift a common function into the arrowized version, and
<code>first</code> takes an arrow which takes a value as input and exposes an arrow
that takes a pair as input, applying the given function on the <em>first</em>
value of the pair. Let’s implement that:</p>
<pre><code class="language-haskell">instance Arrow (Behavior t) where
  arr f = fix $ \r -&gt; Behavior $ \t a -&gt; (f a,r)
  first f = Behavior $ \t (b,d) -&gt;
    let (c,fn) = stepBehavior f t b
    in ((c,d),first fn)
</code></pre>
<h3>Category</h3>
<p>A <a href="http://en.wikipedia.org/wiki/Category_%28mathematics%29">category</a>
basically exposes two concepts: composition and identity. In our case,
the identity represents a constant behavior in time and the composition
composes two behaviors in time. Let’s implement <code>Category</code> by providing
implementation for both <code>id</code> and <code>(.)</code>:</p>
<pre><code class="language-haskell">instance Category (Behavior t) where
  id = arr id
  x . y = Behavior $ \t a -&gt;
    let (yr,yn) = stepBehavior y t a
        (xr,xn) = stepBehavior x t yr
    in (xr,xn . yn)
</code></pre>
<p><strong>Note</strong>: because of <code>Prelude</code> exporting specialized implementation
of <code>id</code> and <code>(.)</code> – the function ones – you should hide them in order
to implement <code>Category</code>:</p>
<pre><code>import Prelude hiding ( (.), id )
</code></pre>
<h3>Semigroup</h3>
<p>A <a href="http://en.wikipedia.org/wiki/Semigroup">semigroup</a> is a pretty cool
algebraic structure used in Haskell to represent “anything that
associates”. It exposes an associative binary function over a set.
In the case of behaviors, if two behaviors output semigroup values, we
can associates the behaviors to build a single one.</p>
<p>A <code>Semigroup</code> is implemented via a single typeclass method, <code>(&lt;&gt;)</code>.
Let’s do that for behaviors:</p>
<pre><code class="language-haskell">instance (Semigroup b) =&gt; Semigroup (Behavior t a b) where
  x &lt;&gt; y = Behavior $ \t a -&gt;
    let (xr,xn) = stepBehavior x t a
        (yr,yn) = stepBehavior y t a
    in (xr &lt;&gt; yr,xn &lt;&gt; yn)
</code></pre>
<p>Simple and neat.</p>
<h3>Functor</h3>
<p>You might already know that one since I talked about it a few lines ago,
but let’s write the instance for our <code>Behavior</code>:</p>
<pre><code class="language-haskell">instance Functor (Behavior t a) where
  fmap f b = Behavior $ \t a -&gt;
    let (br,bn) = stepBehavior b t a
    in (f br,fmap f bn)
</code></pre>
<p>Pretty cool eh?</p>
<h3>Applicative</h3>
<p>A very known one too. Let’s see how we could implement <code>Applicative</code>:</p>
<pre><code class="language-haskell">instance Applicative (Behavior t a) where
  pure = arr . const
  f &lt;*&gt; x = Behavior $ \t a -&gt;
    let (fr,fn) = stepBehavior f t a
        (xr,xn) = stepBehavior x t a
    in (fr xr,fn &lt;*&gt; xn)
</code></pre>
<h3>Profunctor</h3>
<p>This one is special. You don’t have to know what a profunctor is, but
eh, you should, because profunctors are pretty simple to use in Haskell,
and are very useful. I won’t explain what they are – you should have a look
at <a href="https://www.fpcomplete.com/school/to-infinity-and-beyond/pick-of-the-week/profunctors">this article</a>
for further details.</p>
<p>If you do know them, here’s the implementation for <code>dimap</code>:</p>
<pre><code class="language-haskell">instance Profunctor (Behavior t) where
  dimap l r x = Behavior $ \t a -&gt;
    let (xr,xn) = stepBehavior x t (l a)
    in (r xr,dimap l r xn)
</code></pre>
<h2>Inhibition</h2>
<h3>Bare concept</h3>
<p>Behaviors consume environment state and have outputs. However, they
sometimes just don’t. They don’t output anything. That could be the
case for a behavior that only emits during a certain period of time.
It could also be the case for a signal function that’s defined on a
given interval: what should we output for values that lie outside?</p>
<p>Such a scenario is called <em>inhibition</em>. There’re several solutions
to implement inhibition. The simplest and most famous one is by
using <code>Maybe</code> as a wrapper over the output. Like the following:</p>
<pre><code>Behavior t a (Maybe b)
</code></pre>
<p>If <code>(Maybe b)</code> is <code>Nothing</code>, the output is undefined, then the
behavior inhibits.</p>
<p>However, using a bare <code>Maybe</code> exposes the user directly
to inhibition. There’s another way to do that:</p>
<pre><code>newtype Behavior t a b = Behavior { stepBehavior :: t -&gt; a -&gt; Maybe (b,Behavior t a b) }
</code></pre>
<p>Here we are. We have behaviors that can inhibit. If a behavior doesn’t
inhibit, it returns <code>Just (output,nextBehavior)</code>, otherwise it
outputs <code>Nothing</code> and inhibits forever.</p>
<blockquote>
<p><strong>Exercise</strong>: try to reimplement all the above abstractions with
the new type of <code>Behavior</code>.</p>
</blockquote>
<p>We can add a bunch of other interesting functions:</p>
<h3>Inhibition-related combinators</h3>
<pre><code class="language-haskell">dead :: Behavior t a b
dead = Behavior $ \_ _ -&gt; Nothing

one :: b -&gt; Behavior t a b
one x = Behavior $ \_ _ -&gt; Just (x,dead)
</code></pre>
<p><code>dead</code> is a behavior that inhibits forever. That is, it doesn’t
produce any value at any time.</p>
<p><code>one x</code> produces <code>x</code> once, and then inhibits forever. That’s a nice
combinator to use when you want to <em>pulse</em> values in time. We’ll
see later that it’s very useful to represent discrete events, like
key presses or mouse motion.</p>
<p>However, inhibiting can be useful. For instance, we can implement
a new kind of behavior switching using inhibition. Let’s try to
implement a function that takes two behaviors and switches to the
latter when the former starts inhibiting:</p>
<pre><code class="language-haskell">revive :: Behavior t a b -&gt; Behavior t a b -&gt; Behavior t a b
revive x y = Behavior $ \t a -&gt; case stepBehavior x t a of
  Just (xr,xn) -&gt; return (xr,revive xn y)
  Nothing -&gt; stepBehavior y t a

(~&gt;) :: Behavior t a b -&gt; Behavior t a b -&gt; Behavior t a b
(~&gt;) = revive
</code></pre>
<p><code>(~&gt;)</code> is a handy alias to <code>revive</code>. Then, <code>a ~&gt; b</code> is a behavior
that is <code>a</code> until it inhibits, afterwhile it’s <code>b</code>. Simple, and
useful.</p>
<p>In <em>netwire</em>, <code>revive</code> – or <code>(~&gt;)</code> – is <code>(--&gt;)</code>. There’s
also an operator that does the opposite thing: <code>(&gt;--)</code>.
<code>a &gt;-- b</code> is <code>a</code> until <code>b</code> starts producing – i.e. until <code>b</code>
doesn’t inhibit anymore.</p>
<blockquote>
<p><strong>Exercise</strong>: write the implementatof of <code>(&gt;~)</code>, our version
for netwire’s <code>(&gt;--)</code>.</p>
</blockquote>
<p><img src="http://phaazon.net/pub/wire.jpg" alt="" /></p>
<h2>Behaviors revisited</h2>
<p>Now you have a better idea of how you could implement a
behavior, let’s talk about netwire’s one.</p>
<p>netwire’s behavior type is called <code>Wire</code>. It’s actually:</p>
<pre><code>Wire s e m a b
</code></pre>
<p><code>s</code> is the <em>session</em> time – it’s basically a type that’s used
to extract time. <code>e</code> is the inhibition value. <code>m</code> is a inner
monad – yes, you can use monadic code within netwire, which is
not really useful actually, except for <code>Reader</code>, I guess. And
<code>a</code> and <code>b</code> are respectively inputs and outputs.</p>
<blockquote>
<p><em>“What is that inhibition type?”</em></p>
</blockquote>
<p>Yeah, netwire doesn’t use <code>Maybe</code> for inhibition. Picture
<code>Wire</code> as:</p>
<pre><code>newtype Wire s e m a b = Wire { stepWire :: s -&gt; a -&gt; m (Either e (b,Wire s e m a b)) }
</code></pre>
<p>Instead of using <code>Maybe (b,Wire s e m a b)</code>, it uses <code>Either</code>.
Some functions require <code>e</code> to be a <code>Monoid</code>. I guess netwire uses
that to accumulate during inhibition. I don’t see decent use
cases of such a feature, but it’s there. I tend to use this kind
of wire in all my uses of netwire:</p>
<pre><code class="language-haskell">Wire s () Identity a b -- I think this is the most common type of wire
</code></pre>
<p>Keep in mind that although you can set <code>m</code> to <code>IO</code>, it’s not
what netwire – and FRP – was designed for.</p>
<h2>Events</h2>
<p>What about events? Well, netwire exposes events as a home-made
<code>Maybe</code>:</p>
<pre><code class="language-haskell">data Event a
  = Event a
  | NoEvent
    deriving (Eq,Show)

instance Functor Event where
  fmap f e = case e of
    Event a -&gt; Event (f a)
    NoEvent -&gt; NoEvent
</code></pre>
<p>That’s actually enough, because we can attach <code>Event a</code> to time
occurences with <code>Behavior t b a</code>. You’ll find every now and then
functions using <code>Wire s e m (Event a) b</code>, for instance. You should
get used to that as you write toy examples, and real-world ones,
of course.</p>
<h1>In the end</h1>
<p>What a trek… As you can see, we were able to approach netwire’s
implementation understanding pretty closely. There are a few concepts
I haven’t covered – like intrinsic switches, continuable switches,
deferred switches… – but I don’t pretend having a comprehensive
FRP article. You’ll have to dig in a bit more ;)</p>
<p>I’ll write another article about FRP and netwire to implement the
camera example with netwire so that you can have a concrete example.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 15 Mar 2015 00:00:00 GMT</pubDate></item><item><title>GLSL quasiquoting in Rust!</title><link>https://strongly-typed-thoughts.net/blog/glsl-quasiquoting</link><description><![CDATA[<p>A year ago, <a href="https://phaazon.net/blog/glsl_crate">I announced the glsl crate</a>, a crate used to parse
GLSL450-formated sources (and possibly GLSL460 since it’s almost the same thing, but a feature flag
is needed for that). I didn’t get much feedback about it but I’ve been using it in my <a href="https://crates.io/crates/cheddar">cheddar</a>
crate with a great of success (along with <a href="https://crates.io/crates/spectra">spectra</a>).</p>
<p>The <a href="https://crates.io/crates/glsl">glsl</a> crate enables you to parse a GLSL-formatted input (raw string or byte slice) into an AST
you can manipulate in Rust.</p>
<p>It’s very likely you will never have to work with that AST representation directly. Instead, you
might be interested in all the possible outcomes you can get from using the <a href="https://crates.io/crates/glsl">glsl</a> crate:</p>
<ul>
<li>Parse a file, like a <code>scene.glsl</code>, and get its AST representation in Rust.</li>
<li>Pass that AST to a <em>writer</em>, for instance, a <a href="https://en.wikipedia.org/wiki/Source-to-source_compiler">transpiler</a>, to convert from the GLSL to <a href="https://www.khronos.org/spir">SPIR-V</a>
or <a href="https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl">HLSL</a>.</li>
<li>Algorithmically enhance or enrich the GLSL AST without having to cope with raw strings.</li>
<li>Extract information from the GLSL source code (reflection).</li>
<li>Etc.</li>
</ul>
<p>Lately, in <a href="https://crates.io/crates/spectra">spectra</a>, I’ve been working on a more abstract way to write shaders in GLSL. The idea is
that I need to ship the library with some GLSL. The typical way people do this is by embedding a
static string into your library / application, like so:</p>
<pre><code>const SOME_VERTEX_SHADER: &amp;str =
"void main() {
  // …
}";
</code></pre>
<blockquote>
<p>You can also use the <code>include_str!</code> standard macro to read it from a file at compile-time.</p>
</blockquote>
<p>The <a href="https://crates.io/crates/cheddar">cheddar</a> crate provides you with an enriched version of GLSL (the Cheddar language), and the
integration in <a href="https://crates.io/crates/spectra">spectra</a> is pretty much seamless. The GLSL is parsed from Cheddar, transpiled to the
GLSL AST and then the whole thing is passed <a href="https://crates.io/crates/luminance">luminance</a> that acts as a rendering backend of
<a href="https://crates.io/crates/spectra">spectra</a>. Becauses some AST transformation is needed, the <a href="https://crates.io/crates/glsl">glsl</a> crate is used in <a href="https://crates.io/crates/cheddar">cheddar</a>, and
some GLSL must be added in order to provide a smoother and nicer experience within <a href="https://crates.io/crates/spectra">spectra</a>.</p>
<p>The problem kicks in that transformation part. Since Cheddar is parsed directly into memory as an
AST (via the <a href="https://crates.io/crates/cheddar">cheddar</a> crate), how could we transform and add things to values from <a href="https://crates.io/crates/cheddar">cheddar</a>?</p>
<h1>The hard and manual way: the constructor syntax</h1>
<p>Pretty much all of the <a href="https://crates.io/crates/glsl">glsl</a> crate is public. There’s no invariant, so no need to hide anything.
It’s possible to create, for instance, a <code>AssignmentExpr</code> by simply calling its constructor syntax:</p>
<pre><code>let assignment_expr = AssignmentExpr {
  // fields here
  };
</code></pre>
<p>This is the regular way to go, but if you look at some types, such as the
<a href="https://docs.rs/glsl/0.9.2/glsl/syntax/enum.SimpleStatement.html"><code>SimpleStatement</code></a>, you’ll get
that it can get very noisy and verbose very quickly. So that’ll work, but that’ll also be very
incomfortable to work with.</p>
<p>The advantage of constructing and transforming that way is that you can destructure and
pattern-match ASTs.</p>
<h1>Enter the nice solution: quasiquoting</h1>
<p>As a Haskeller, I’ve been very frustrated by that construction problem, thinking that there must be
an easier and less noisy way to do so. That reminds me of all the EDSLs I wrote in my Haskeller life
and some very, <em>very</em> sweet embedded languages, like SQL, C or Java, directly into Haskell. That is
done via something we call <a href="https://wiki.haskell.org/Quasiquotation">quasiquoting</a>. Quasiquoting is a general concept but in our case,
applies to embedded languages.</p>
<p>The concept is simple: you write in a foreign language in a <em>quasiquoter</em> in your host language
(Haskell or Rust). The compiler then recognizes the quasiquoter and <strong>executes some code at compile
time</strong>. Once it’s done, it replaces the quasiquoted statement with the result of its computation.</p>
<p>One good example of quasiquoting is <a href="http://hackage.haskell.org/package/postgresql-query">postgresql-query</a>, a Haskell quasiquoting library for SQL. SQL
queries can be built with regular Haskell code but they can also be built out of a <em>quasiquoter</em>,
like so:</p>
<pre><code>-- some code elided for simplicity here
pqQuery [sqlExp|SELECT u.id, u.name, u.age
                FROM users AS u ^{cond}
                ORDER BY ^{ord}|]
</code></pre>
<p>The <code>[sqlExp|…|]</code> notation uses the quasiquoter syntax: the <code>sqlExp</code> is the quasiquoter to use, and
everything between the two pipes are the content / input to pass the quasiquoter. The compiler just
reads that, executes some code defined in the <a href="http://hackage.haskell.org/package/postgresql-query">postgresql-query</a> library and if it runs correctly,
the resulting expression is inserted directly at the place of use as if nothing has happened.
Brillant, right? :)</p>
<p>Having written <a href="https://crates.io/crates/glsl">glsl</a>, that seemed like a perfect – maybe even wanted and mandatory? – opportunity
to me to introduce GLSL quasiquoting into the Rust environment. Maybe it’ll also bring some people
onto the <a href="https://crates.io/crates/glsl">glsl</a> project – I really need hands, especially because the <a href="https://www.khronos.org/spir">SPIR-V</a> transpiler doesn’t
exist yet!</p>
<p>So, here you go. <a href="https://crates.io/crates/glsl-quasiquote/0.1.0">glsl-quasiquote-0.1</a> was released today! The crate provides you with two macros:
<code>glsl!</code> and <code>glsl_str!</code>. Both are procedural macros that requires a nightly compiler and the
<code>proc_macro_non_items</code> feature. They will both output a <a href="https://docs.rs/glsl/0.9.2/glsl/syntax/type.TranslationUnit.html"><code>TranslationUnit</code></a>, that represents a whole
shader AST.</p>
<p>The <code>glsl!</code> AST works the same way a quasiquoter from Haskell: instead of delimiting your GLSL code
with pipes, you write them inside parens or brackets, like so:</p>
<pre><code>glsl!{
  void main() {
  }
}
</code></pre>
<p>The <code>glsl_str!</code> macro does the exact same thing but expects a literal string as input. This is due
to the fact that quasiquoting in Rust is a bit tricky – who’s looking at me like I was about to
write an RFC? – because regular macros and procedural macros eat Rust tokens as inputs, forcing
their content to be Rust-friendly syntax. Well, GLSL is not: the <code>#version</code> and <code>#extension</code> pragmas
need newlines at the end, so the following will not work:</p>
<pre><code>glsl!{
  #version 330 core
  void main() {
  }
}
</code></pre>
<p>Instead, you must use the <code>glsl_str!</code> quasiquoter:</p>
<pre><code>glsl_str!{"
  #version 330 core

  void main() {
  }
"}
</code></pre>
<p>It’s a bit of an unaesthetica and pretty weird syntax but we don’t really have a choice (so far).</p>
<p>Whenever a GLSL is incorrectly formatted, you’ll get a compiler error. For instance, the following:</p>
<pre><code>glsl!{
  void main() 3 {
  }
}
</code></pre>
<p>generates the following <code>rustc</code> output:</p>
<pre><code>error: proc macro panicked
  --&gt; tests/lib.rs:26:11
   |
26 |     let _ = glsl!{
   |  ___________^
27 | |     void main() 3 {
28 | |     }
29 | |   };
   | |___^
   |
   = help: message: GLSL error: Err(ParseError { kind: Many1, info: "void main (  ) 3 {  }" })

error: aborting due to previous error
</code></pre>
<p>The crate was released today, so it needs a lot of love, attention and testing. I’m adding support
for it in <a href="https://crates.io/crates/spectra">spectra</a> for my experiences, so I should be updating it if I find anything wrong with it.</p>
<p>Feel free to use it, and have fun! I’m awaiting your feedback!</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 05 Oct 2018 19:00:00 GMT</pubDate></item><item><title>Universal JSON support in warmy-0.11.1</title><link>https://strongly-typed-thoughts.net/blog/warmy-universal-json</link><description><![CDATA[<h1>Norbert the hot-dog</h1>
<p>I just added a new feature to <a href="https://crates.io/crates/warmy/0.11.1">warmy</a> to make it more accessible and easy to use. It concerns <a href="https://fr.wikipedia.org/wiki/JavaScript_Object_Notation">JSON</a>
and <a href="https://crates.io/crates/serde">serde</a>.</p>
<h2>Universal JSON support in warmy 0.11.1</h2>
<p>If you’ve been using <a href="https://crates.io/crates/warmy/0.11.1">warmy</a> a bit, you might be used to its traits and structures, like <code>Load</code>,
<code>Loaded</code>, <code>Load::reload</code>, <code>Res</code>, etc. All those concepts are mandatory to implement loading,
reloading and scarce resource sharing. Since version
<a href="https://github.com/phaazon/warmy/blob/master/CHANGELOG.md#07">0.7</a>, <a href="https://crates.io/crates/warmy/0.11.1">warmy</a> has got <em>loading and
reloading methods</em>. That feature enables you to have several <code>impl</code> for the same type of resource by
changing the method used to load. The default is <code>()</code> but you’re free to use any you want. The idea
is that you can call the <code>Store::get_by</code> or <code>Store::get_proxied_by</code> methods to specify which method
to use explicitly when loading and reloading a given resource.</p>
<p><a href="https://crates.io/crates/warmy/0.11.1">warmy</a> 0.11.1 uses that concept to provide a universal implementor for anything that implements the
<a href="https://docs.rs/serde/1.0.85/serde/trait.Deserialize.html"><code>Deserialize</code></a> trait from <a href="https://crates.io/crates/serde">serde</a>. Basically, once your type implements <a href="https://docs.rs/serde/1.0.85/serde/trait.Deserialize.html"><code>Deserialize</code></a>, you can
load and hot-reload values of this type by using the <a href="https://docs.rs/warmy/0.11.1/warmy/json/struct.Json.html"><code>Json</code></a> type from <a href="https://crates.io/crates/warmy/0.11.1">warmy</a>.</p>
<blockquote>
<p>Universal JSON implementors are available only if the <code>"json"</code> feature is enabled in <a href="https://crates.io/crates/warmy/0.11.1">warmy</a>. It’s
the case by default. Feel free to use <code>default-features = false</code> if you don’t want it.</p>
</blockquote>
<p><img src="https://phaazon.net/media/uploads/very_nice.gif" alt="" /></p>
<p>Here’s a short example of what it looks like to load and hot-reload a <code>Dog</code> using the universal JSON
feature:</p>
<pre><code>use serde::Deserialize;
use warmy::{Res, SimpleKey, Store, StoreOpt};
use warmy::json::Json;
use std::thread::sleep;
use std::time::Duration;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
struct Dog {
  name: String,
  gender: Gender
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
enum Gender {
  Female,
  Male
}

fn main() {
  let mut store: Store&lt;(), SimpleKey&gt; = Store::new(StoreOpt::default()).unwrap();
  let ctx = &amp;mut ();

  let resource: Result&lt;Res&lt;Dog&gt;, _&gt; = store.get_by(&amp;SimpleKey::from_path("/dog.json"), ctx, Json);

  match resource {
    Ok(dog) =&gt; {
      loop {
        store.sync(ctx);

        println!("Dog is {} and is a {:?}", dog.borrow().name, dog.borrow().gender);
        sleep(Duration::from_millis(1000));
      }
    }

    Err(e) =&gt; eprintln!("{}", e)
  }
}
</code></pre>
<p>This feature should help people adopt the crate and use it without worrying too much about the
actual implementation of <code>Load</code>.</p>
<p>If you’re interested in adding other methods, like YAML, XML or whatever, feel free to open a PR on
GitHub! I’ll be very happy to accept it. More documentation about the feature can be found in the
<a href="https://github.com/phaazon/warmy/tree/193af4bc6ee75699fea1ab2cf4ecff362b919899#universal-json-support">README</a> or the <a href="https://docs.rs/warmy/0.11.1/warmy/index.html#universal-json-support">online documentation</a>.</p>
<p>Keep the vibes.</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Fri, 25 Jan 2019 00:50:00 GMT</pubDate></item><item><title>The Cheddar shading language</title><link>https://strongly-typed-thoughts.net/blog/cheddar-0.1</link><description><![CDATA[<p>It’s been a long time I wanted to announce and reveal this, and this morning is now the moment!
This blog post announces the release of <a href="https://crates.io/crates/cheddar">cheddar</a> in version 0.1!</p>
<h1>Foreword</h1>
<p>Cheddar is a GLSL <em>superset</em> language. What it means is that most of the GLSL constructs and syntax
you’re used to is valid in Cheddar – not <em>all of it</em>; <em>most of it</em>. Cheddar adds a set of features
that I think are lacking to GLSL. Among them:</p>
<ul>
<li>Some non-valid GLSL constructions made valid in Cheddar to ease the writing of certain shader
stages.</li>
<li>A more functional approach to programming shaders on the GPU.</li>
<li>Structures, types and GLSL-specific <em>constructs sharing</em>.</li>
<li>Imports and modules with live reloading and transitive dependencies.</li>
</ul>
<p>However, Cheddar is not:</p>
<ul>
<li>A new shading language that abstracts over any kind of shading language (i.e. unlike <a href="https://en.wikipedia.org/wiki/Cg_(programming_language)">Cg</a> for
instance). However, since it should be possible for GLSL to be transpiled to SPIR-V (hence
Vulkan), it should be possible to transpile Cheddar to SPIR-V.</li>
<li>A dedicated DSL to a specific graphics problem. Cheddar is very similar to GLS (actually, that’s
why it’s called a GLSL <em>superset</em>). If you’re looking for a solution to ease writing shaders in
a way that you don’t have to learn shaders, Cheddar won’t help you much with this.</li>
</ul>
<p>The documentation is still a big <em>work in progress</em> but most of it should give you enough
information to get started.</p>
<h1>A bit of backstory</h1>
<p>Cheddar was imagined and designed while I was working on <a href="https://crates.io/crates/spectra">spectra</a>, a <em>work in progress</em> demoscene
crate I’ve been working for a while now. I released two demos thanks to <a href="https://crates.io/crates/spectra">spectra</a> –
<a href="https://www.youtube.com/watch?v=pYqZS1C_7PU">this one</a> and
<a href="https://www.youtube.com/watch?v=ug7eRowgVw4">this one</a>. Because those demos are very <em>meh</em> to me, I
decided to enhance my tools. Among them was the need to write shaders a better and easier way. Then
Cheddar got born. I used Cheddar while preparing other demos, and I was talking about it on IRC and
Reddit, people seemed to be interested – I even had a friend writing demos in C++ interested!</p>
<p>So here it is. Please, provide feedback if you try it, should you like it or not!</p>
<h1>Dig in</h1>
<blockquote>
<p>Disclaimer: the current state of the language is pretty experimental and unstable. There’s no
semantics checking, for instance.</p>
</blockquote>
<p>You can read the full tutorial and design document on the <a href="https://docs.rs/cheddar/0.1.0/cheddar">official documentation page</a>.</p>
<p>Thanks for having read me, and as always, keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 09 Jul 2018 01:00:00 GMT</pubDate></item><item><title>Introducing flirt</title><link>https://strongly-typed-thoughts.net/blog/introducing-flirt</link><description><![CDATA[<p>I think pretty much all software developers have faced the situation where they get introduced to a new
codebase — or some directory they don’t know that much — and they don’t really know their place around.
The idea is that one would like to move to a specific folder, but does not remember <em>exactly</em> the path to it.
Another situation is when someone wants to apply a command to a bunch of files, and a glob cannot really be
used, like <code>*.txt</code>, because one would want only specific files — like <code>{foo,bar}.txt</code> for instance.</p>
<p>Most of the time, if you are a CLI/TUI enjoyer, you will likely have a similar workflow to this:</p>
<ul>
<li><code>ls</code> to show the directories. You will locate the first directory you want to go to.</li>
<li><code>cd &lt;that-dir&gt;</code>.</li>
<li><code>ls</code> again to show what’s there.</li>
<li><code>cd &lt;another-dir&gt;</code>.</li>
<li>Repeat until you are at the place you want.</li>
</ul>
<p>I don’t think I need to debate on the fact that this is a very time-wasting workflow, and honestly not very
pleasant. Most shells today allow you to do something like <code>cd &lt;tab&gt;</code> and have a way to virtually visit the
directories, until you find the one you want. Press <code>&lt;return&gt;</code> and your command is completed. You can use the
same method for tar-ing files, removing files, running arbitrary commands, etc.</p>
<blockquote>
<p>Another interesting way of doing that is to use a fuzzy finder, like <a href="https://github.com/junegunn/fzf">fzf</a> or <a href="https://github.com/skim-rs/skim">skim</a>. I would usually just do
<code>cd ^t</code>. That works when you know exactly what you are looking for, which is not exactly the situations we are
talking about here.</p>
</blockquote>
<p>The <code>cd</code> example is what motivated me to make a small tool that I named <a href="https://sr.ht/~hadronized/flirt/">flirt</a>, but along the way, I realized
it could do much more interesting things, without expanding its scope and not violating the UNIX philosophy.</p>
<h1>FiLe InteRacT, the file interaction tool for your CLI</h1>
<p>I never liked file browsers, like <a href="https://github.com/jarun/nnn">nnn</a>, <a href="https://github.com/gokcehan/lf">lf</a> or <a href="https://github.com/sxyazi/yazi">yazi</a>. They look cool, but there is something <em>too much</em> about
them. I like the <a href="https://strongly-typed-thoughts.net/blog/kakoune-philosophy">Kakoune’s way</a>. I think the UI from file browsers is interesting, but I think we could use it
in a much more barebone/simple (and UNIX) way. The idea is to use it as a read-only UI, and let other tools
compose around it.</p>
<p>For instance, let’s take the <code>cd</code> example from before, where we go to a directory by interleaving <code>cd</code> and <code>ls</code>
commands. What do we want to <em>actually</em> do?</p>
<ol>
<li>Locate a directory.</li>
<li><code>cd</code> into it.</li>
</ol>
<p><code>flirt</code> allows to implement the first part. You can do something like <code>cd $(flirt)</code> — or <code>cd ^s</code> for a shell
shortcut integration, which I highly recommend — and move around directories until you have found the one
you are interested in. Quit <code>flirt</code>, and you have your CLI set to <code>cd &lt;path&gt;</code>. Just press <code>&lt;return&gt;</code> to jump
to that directory.</p>
<script src="https://asciinema.org/a/692800.js" id="asciicast-692800" async="true"></script>
<p>This is a simple workflow, because it allows you to think of <code>flirt</code> just as a read-only view into your
filesystem. But it can do much more.</p>
<h2>Arbitrary files selection and reordering</h2>
<p><code>flirt</code> can select multiple files. Why would you want to select many files? Well, imagine that you want
to select a bunch of files and put them in a directory. It’s a pretty cumbersome operation to do manually
on the CLI, because you have to type a lot of text to just pick the files you want to feed your <code>mv</code> command.
With <code>flirt</code>, you can do something like <code>mv ^s</code> and pick the files you want.</p>
<p>Because <code>flirt</code> is ordered — you can see the order on the right-side view and switch between the left and right
views with the <code>&lt;tab&gt;</code> key by default — you can ensure the destination directory is the last argument — interactively.</p>
<script src="https://asciinema.org/a/692801.js" id="asciicast-692801" async="true"></script>
<h2>Unlocking the full potential with other UNIX tools</h2>
<p>Have you heard about <a href="https://joeyh.name/code/moreutils/">moreutils</a>? It’s a collection of UNIX tools that go a bit further in terms of composability. One
of those tools, <code>vipe</code>, is a pipe editor: it reads from <em>stdin</em> and pipes its content to your <code>$EDITOR</code>. Once you have
finished editing your buffer and quit your editor, it will pipe the modified data back into its <em>stdout</em>:</p>
<pre><code class="language-sh">cmd1 | vipe | cmd2
</code></pre>
<p>Composing with <code>flirt</code> is super easy and you can implement interesting workflows, such as with:</p>
<pre><code class="language-sh">flirt | vipe | sh
</code></pre>
<p>Which allows you to select a bunch of files on which operate commands — that you add in your editor, via <code>vipe</code> — and
execute the output script!</p>
<script src="https://asciinema.org/a/mfi16nGR2Dr0d9F0SS02zOzCs.js" id="asciicast-mfi16nGR2Dr0d9F0SS02zOzCs" async="true"></script>
<p>If you are interested, you can find <a href="https://sr.ht/~hadronized/flirt/">flirt here</a>, along with some additional links:</p>
<ul>
<li><a href="https://lists.sr.ht/~hadronized/flirt-discuss">Discussion mailing list</a></li>
<li><a href="https://lists.sr.ht/~hadronized/flirt-devel">Development mailing list (send your patches here!)</a></li>
<li><a href="https://todo.sr.ht/~hadronized/flirt">Feature requests / bug reports</a></li>
</ul>
<p>If you have <code>cargo</code> installed, you can install <code>flirt</code> easily:</p>
<pre><code class="language-sh">cargo install flirt
</code></pre>
<blockquote>
<p>I might add a <code>PKGBUILD</code> for Archlinux at some point.</p>
</blockquote>
<p>Do not hesitate to send your feedback on the discussion mailing list! Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 04 Dec 2024 17:08:00 GMT</pubDate></item><item><title>luminance, episode 0.6: UBO, SSBO, Stackage</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.6</link><description><![CDATA[<p>Up to now, <a href="https://hackage.haskell.org/package/luminance">luminance</a> has been lacking two cool
features: <a href="https://www.opengl.org/wiki/Uniform_Buffer_Object">UBO</a> and
<a href="https://www.opengl.org/wiki/Shader_Storage_Buffer_Object">SSBO</a>. Both are <em>buffer-backed</em> uniform
techniques. That is, a way to pass uniforms to shader stages through buffers.</p>
<p>The <a href="https://hackage.haskell.org/package/luminance-0.6">latest version of luminance</a> has one of the
two features. <em>UBO</em> were added and <em>SSBO</em> will follow for the next version, I guess.</p>
<h1>What is UBO?</h1>
<p>UBO stands for <strong>U</strong>niform <strong>B</strong>buffer <strong>O</strong>bject. Basically, it enables you to create <em>uniform
blocks</em> in GLSL in feed them with <em>buffers</em>. Instead of passing values directly to the uniform
interface, you just write whatever values you want to to buffers, and then pass the buffer as a
source for the uniform block.</p>
<p>Such a technique has a lot of advantages. Among them, you can pass <strong>a lot of values</strong>. It’s also
cool when you want to pass values instances of a structure (in the GLSL source code). You can also
use them to share uniforms between several shader programs as well as quickly change all the
uniforms to use.</p>
<p>In luminance, you need several things. First thing first, you need… a buffer! More specifically,
you need a buffer <a href="https://hackage.haskell.org/package/luminance-0.6/docs/Graphics-Luminance-Buffer.html#t:Region"><code>Region</code></a>
to store values in. However, you cannot use any kind of region. You have to use a region that can
hold values that will be fetched from shaders. This is done with a type called <code>UB a</code>. A buffer of
<code>UB a</code> can be used as <em>UBO</em>.</p>
<p>Let’s say you want to store colors in a buffer, so that you can use them in your fragment shader.
We’ll want three colors to shade a triangle. We need to create the buffer and get the region:</p>
<pre><code class="language-haskell">colorBuffer :: Region RW (UB (V3 Float)) &lt;- createBuffer (newRegion 3)
</code></pre>
<p>The explicit type is there so that GHC can infer the correct types for the <code>Region</code>. As you can see,
nothing fancy, except that we just don’t want a <code>Region RW (V3 Float</code> but
<code>Region RW (UB (V3 Float))</code>. Why <code>RW</code>?</p>
<p>Then, we’ll want to store colors in the buffer. Easy peasy:</p>
<pre><code class="language-haskell">writeWhole colorBuffer (map UB colors)

colors :: [V3 Float]
colors = [V3 1 0 0,V3 0 1 0,V3 0 0 1] -- red, green, blue
</code></pre>
<p>At this point, <code>colorBuffer</code> represents a GPU buffer that holds three colors: red, green and blue.
The next part is to get the uniform interface. That part is experimental in terms of exposed
interface, but the core idea will remain the same. You’re given a function to build <em>UBO</em> uniforms
as you also have a function to build simple and plain uniforms in
<a href="https://hackage.haskell.org/package/luminance-0.6/docs/Graphics-Luminance-Shader-Program.html#v:createProgram"><code>createProgram</code></a>:</p>
<pre><code class="language-haskell">createProgram shaderList $ \uni uniBlock -&gt; {- … -}
</code></pre>
<p>Don’t spend too much time reading the signature of that function. You just have to know that
<code>uni</code> is a function that takes <code>Either String Natural</code> – either a uniform’s name or its integral
semantic – and gives you mapped <code>U</code> in return and that <code>uniBlock</code> does the same thing, but for
uniform blocks instead.</p>
<p>Here’s our vertex shader:</p>
<pre><code class="language-C">in vec2 co;
out vec4 vertexColor;

// This is the uniform block, called "Colors" and storing three colors
// as an array of three vec3 (RGB).
uniform Colors {
  vec3 colors[3];
};

void main() {
  gl_Position = vec4(co, 0., 1.);
  vertexColor = vec4(colors[gl_VertexID], 1.);
}"
</code></pre>
<p>So we want to get a <code>U a</code> mapped to that <code>"Colors"</code> uniform block. Easy!</p>
<pre><code class="language-haskell">(program,colorsU) &lt;- createProgram shaderStages $ \_ uniBlock -&gt; uniBlock "Colors"
</code></pre>
<p>And that’s all! The type of <code>colorsU</code> is <code>U (Region rw (UB (V3 Float)))</code>. You can then gather
<code>colorBuffer</code> and <code>colorsU</code> in a uniform interface to send <code>colorBuffer</code> to <code>colorsU</code>!</p>
<p>You can find the complete sample <a href="https://github.com/phaazon/luminance-samples/blob/c4f428dc68ed38f0b80032aa9de90df01bf8ab15/src/UBO.hs">here</a>.</p>
<p>Finally, you can augment the type you can use <code>UB</code> with by implementing the <code>UniformBlock</code>
typeclass. You can derive the <code>Generic</code> typeclass and then use a default instance:</p>
<pre><code class="language-haskell">data MyType = {- … -} deriving (Generic)

instance UniformBlock MyTpe -- we’re good to go with buffer of MyType!
</code></pre>
<h1>luminance, luminance-samples and Stackage</h1>
<p>I added <em>luminance</em> and <em>luminance-samples</em> into Stackage. You can then find them in the nightly
snapshots and the future LTS ones.</p>
<h1>What’s next?</h1>
<p>I plan to add stencil support for the framebuffer, because it’s missing and people might like it
included. I will of course add support for <em>SSBO</em>* as soon as I can. I also need to work on cheddar
but that project is complex and I’m still stuck with design decisions.</p>
<p>Thanks for reading my and for your feedback. Have you great week!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 25 Oct 2015 00:00:00 GMT</pubDate></item><item><title>smoothie-0.3, Bézier curves and new user interface</title><link>https://strongly-typed-thoughts.net/blog/smoothie_bezier</link><description><![CDATA[<h1>Bezier curves</h1>
<p>It’s been a while I’ve been wanting to do that. Now it’s done!
<a href="https://hackage.haskell.org/package/smoothie">smoothie</a>, a Haskell package for
dealing with curves and splines, updated to version
<a href="https://hackage.haskell.org/package/smoothie-0.3">0.3</a>.</p>
<p>That version introduces several changes. If you’re a good programmer, you might
already have noticed that the major version got incremented. That means there’re
compatibility breaking changes. If you don’t know what I’m talking about, you
should definitely read
<a href="https://wiki.haskell.org/Package_versioning_policy">this</a>.</p>
<p>The first – non-breaking – change is that the package now supports
<a href="https://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier</a> interpolation! I’ve
been reading about <strong>Bézier curves</strong> for a while because there’re very present
and important for animation purposes – think of <strong>Blender</strong>. Feel free to
dig in the documentation on hackage for further details.</p>
<p>The second – breaking – change is that the interface has changed, especially the
implementation of <em>splines</em>. However, the interface is now simpler and doesn’t
require a lot of change in your code if you’ve been using older versions.</p>
<p>Feel free to read the
<a href="https://hackage.haskell.org/package/smoothie-0.3/changelog">CHANGELOG</a> for
technical hints.</p>
<p>As always, tell me what you think of the library, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Tue, 30 Jun 2015 00:00:00 GMT</pubDate></item><item><title>luminance-0.5.1 and wavefront-0.4.0.1</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.5.1_and_wavefront-0.4.0.1</link><description><![CDATA[<p>It’s been a few days I haven’t talked about
<a href="http://hackage.haskell.org/package/luminance">luminance</a>. I’ve been working on it a lot those days
along with <a href="http://hackage.haskell.org/package/wavefront">wavefront</a>. In order that you keep up to
date, I’ll describe the changes I made in those packages you have a talk about the future directions
of those packages.</p>
<p>I’ll also give a snippet you can use to load geometries with wavefront and adapt them to embed into
luminance so that you can actually render them! A package might come up from that kind of snippet –
<code>luminance-wavefront</code>? We’ll see that!</p>
<h1>wavefront</h1>
<p>This package has received several changes among two major increments and several fixes. In the first
place, I removed some code from the interface that was useless and used only for test purposes. I
removed the <code>Ctxt</code> object – it’s a type used by the internal lexer anyways, so you don’t have to
know about it – and exposed a type called <a href="http://hackage.haskell.org/package/wavefront-0.4.0.1/docs/Codec-Wavefront.html#t:WavefrontOBJ">WavefrontOBJ</a>.
That type reprents the parsed Wavefront data and is the <em>main</em> type used by the library in the
interface.</p>
<p>Then, I also removed most of the modules, because they’re re-exported by the main module –
<a href="http://hackage.haskell.org/package/wavefront-0.4.0.1/docs/Codec-Wavefront.html">Codec.Wavefront</a>.
I think the documentation is pretty straight-forward, but you think something is missing, please
shoot a PM or an email! ;)</p>
<p>On the bugs level, I fixed a few things. Among them, there was a nasty bug in the implementation of
an internal recursive parser that caused the last wavefront statement to be silently ignored.</p>
<p>I’d also like to point out that I performed some benchmarks – I will provide the data later on with
a heap profile and graphs – and I’m pretty astonished with the results! The parser/lexer is insanely
fast! It only takes a few milliseconds (between 7ms and 8ms) to load 50k faces (a 2MB .obj file).
The code is not yet optimized, so I guess the package could go even faster!</p>
<p>You can find the changelog <a href="http://hackage.haskell.org/package/wavefront-0.4.0.1/changelog">here</a>.</p>
<h1>luminance</h1>
<p>I made a <em>lot of work</em> on luminance lately. First, the <code>V</code> type – used to represent <em>vertex
components</em> – is not anymore defined by luminance but by <a href="http://hackage.haskell.org/package/linear">linear</a>.
You can find the type <a href="http://hackage.haskell.org/package/linear-1.20.2/docs/Linear-V.html">here</a>.
You’ll need the <code>DataKinds</code> extension to write types like <code>V 3 Float</code>.</p>
<p>That change is due to the fact linear is a mature library with a lot of interesting functions and
types everyone might use when doing graphics. Its <a href="http://hackage.haskell.org/package/linear-1.20.2/docs/Linear-V.html"><code>V</code></a>
type has several interesting instances – <code>Storable</code>, <code>Ord</code>, etc. – that are required in luminance.
Because it’s not simple to build such <code>V</code>, luminance provides you with three functions to build the
1D, 2D and 3D versions – <a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Vertex.html#v:vec2"><code>vec2</code></a>,
<a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Vertex.html#v:vec3"><code>vec3</code></a>
and <a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Vertex.html#v:vec2"><code>vec4</code></a>.
Currently, that type is the only one you can use to build <em>vertex components</em>. I might add <code>V2</code>,
<code>V3</code> and <code>V4</code> as well later.</p>
<p>An interesting change: the <code>Uniform</code> typeclass has <strong>a lot</strong> of new instances! Basically, all vector
types from linear, their array version and the 4x4 floating matrix – <code>M44 Float</code>. You can find the
list of all instances <a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Shader-Uniform.html#t:Uniform">here</a>.</p>
<p>A new function was added to the <a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Geometry.html"><code>Graphics.Lumimance.Geometry</code></a>
module called <a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Geometry.html#v:nubDirect"><code>nubDirect</code></a>.
That function performs in linear logarithmic time and is used to turn a <em>direct</em> representation of
vertices into a pair of data used to represent <em>indexed</em> vertices. The new list of vertices
stores only unique vertices and the list of integral values stores the indices. You can then use
both the information to build <em>indexed geometries</em> – see
<a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Geometry.html#v:createGeometry"><code>createGeometry</code></a>
for further details.</p>
<p>The interface to transfer texels to textures has changed. It doesn’t depend on <code>Foldable</code> anymore
but on <code>Data.Vector.Storable.Vector</code>. That change is due to the fact that the <code>Foldable</code> solution
uses <code>toList</code> behind the hood, which causes bad performance for the simple reason that we send the
list to the GPU through the FFI. It’s then more efficient to use a <code>Storable</code> version. Furthermore,
th  most known package for textures loading – <a href="http://hackage.haskell.org/package/JuicyPixels">JuicyPixels</a>
– already uses that type of <code>Vector</code>. So you just have to enjoy the new performance boost! ;)</p>
<p>About bugs… I fixed a few ones. First, the implementation of the <code>Storable</code> instance for
<a href="http://hackage.haskell.org/package/luminance-0.5.1/docs/Graphics-Luminance-Core-Tuple.html#t::."><code>(:.)</code></a>
had an error for <code>sizeOf</code>. The implementation must be lazy in its argument, and the old one was not,
causing <code>undefined</code> crashes when using that type. The strictness was removed and now everything
works just fine!</p>
<p>Two bugs that were also fixed: the indexed render and the render of geometries with several vertex
components. Those bugs were easy to fix and now you won’t experience those issues anymore.</p>
<h1>Interfacing luminance with wavefront to render geometries from artists!</h1>
<p>I thought it would be a hard task but I’m pretty proud of how easy it was to interface both the
packages! The idea was to provide a function that would turn a <code>WavefrontOBJ</code> into a <em>direct
representation</em> of luminance vertices. Here’s the function that implements such a conversion:</p>
<pre><code class="language-haskell">type Vtx = V 3 Float :. V 3 Float -- location :. normal

objToDirect :: WavefrontOBJ -&gt; Maybe [Vtx]
objToDirect obj = traverse faceToVtx (toList faces)
  where
    locations = objLocations obj
    normals = objNormals obj
    faces = objFaces obj
    faceToVtx face = do
      let face' = elValue face
      vni &lt;- faceNorIndex face'
      v &lt;- locations !? (faceLocIndex face' - 1)
      vn &lt;- normals !? (vni - 1)
      let loc = vec3 (locX v) (locY v) (locZ v)
          nor = vec3 (norX vn) (norY vn) (norZ vn)
      pure (loc :. nor)
</code></pre>
<p>As you can see, that function is pure and will eventually turn a <code>WavefrontOBJ</code> into a list of
<code>Vtx</code>. <code>Vtx</code> is our own vertex type, encoding the location and the normal of the vertex. You can
add texture coordinates if you want to. The function fails if a face’s index has no normal
associated with or if an index is out-of-bound.</p>
<p>And… and that’s all! You can already have your <code>Geometry</code> with that – <em>direct</em> one:</p>
<pre><code class="language-haskell">  x &lt;- fmap (fmap objToDirect) (fromFile "./ubercool-mesh.obj")
  case x of
    Right (Just vertices) -&gt; createGeometry vertices Nothing Triangle
    _ -&gt; throwError {- whatever you need as error there -}
</code></pre>
<p>You want an indexed version? Well, you already have everything to do that:</p>
<pre><code class="language-haskell">  x &lt;- fmap (fmap (nubDirect . objToDirect) (fromFile "./ubercool-mesh.obj")
  case x of
    Right (Just (vertices,indices)) -&gt; createGeometry vertices (Just indices) Triangle
    _ -&gt; throwError {- whatever you need as error there -}
</code></pre>
<p>Even though the <code>nubDirect</code> performs in a pretty good complexity, it takes time. Don’t be surprised
to see the “loading” time longer then.</p>
<p>I might package those snippets and helpers around them into a <code>luminance-wavefront</code> package, but
that’s not trivial as the vertex format should be free.</p>
<h1>Future directions and thank you</h1>
<p>I received a lot of warm feedback from people about what I do in the Haskell community, and I’m just
amazed. I’d like to thank each and everyone of you for your support – I even got support from
non-Haskellers!</p>
<p>What’s next then… Well, I need to add a few more textures to luminance – texture arrays are not
supported yet, and the framebuffers have to be altered to support all kind of textures. I will also
try to write a <a href="">cheddar</a> interpreter directly into luminance to dump the <code>String</code> type of shader
stages and replace it with cheddar’s whatever will be. For the long terms, I’ll add UBO and SSBO to
luminance, and… compatibility with older OpenGL versions.</p>
<p>Once again, thank you, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 18 Oct 2015 00:00:00 GMT</pubDate></item><item><title>luminance-0.6.0 sample</title><link>https://strongly-typed-thoughts.net/blog/luminance_sample</link><description><![CDATA[<p>It’s been two weeks <a href="https://crates.io/crates/luminance">luminance</a> is at version 0.6.0. I’m very
busy currently but I decided to put some effort into making a very minimalistic yet usable sample.
The sample uses <a href="https://crates.io/crates/luminance">luminance</a> and
<a href="https://crates.io/crates/luminance-gl">luminance-gl</a> (the <strong>OpenGL 3.3</strong> backend being the single
one available for now).</p>
<p>You’ll find it <a href="https://github.com/phaazon/luminance-samples-rs/blob/master/src/main.rs">here</a>.
The code is heavily commented and you can of course clone the repository and and run the executable
with cargo.</p>
<p>I’ll post a more detailed blog post about the application I’m building with luminance right now
later on.</p>
<p>Keep the vibe! :)</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 25 Jul 2016 00:00:00 GMT</pubDate></item><item><title>Existential quantification and GADT in luminance-0.8</title><link>https://strongly-typed-thoughts.net/blog/luminance-0.8</link><description><![CDATA[<h1>luminance 0.8 and existential quantification</h1>
<p>It’s been a while I haven’t released anything on my blog. I just wrote a few changes for the latest
version of luminance, <a href="https://hackage.haskell.org/package/luminance-0.8.2">luminance-0.8.2</a> and I
decided to write about it because I think those changes are interesting on a Haskell level.</p>
<h2>The problem</h2>
<p>If you haven’t read the <a href="https://hackage.haskell.org/package/luminance-0.8.2/changelog">changelog</a>
yet, I changed the <code>createProgram</code> function and the way it handles uniform interfaces. In
luminance &lt; 0.8, you were provided with as many functions as there are uniform kinds. Up to now,
luminance supports two uniform kinds:</p>
<ul>
<li>simple uniforms;
<ul>
<li>uniform block (UBO).</li>
</ul>
</li>
</ul>
<p>So you had two rank-2 functions like <code>forall a. (Uniform a) =&gt; Either String Natural -&gt; UniformInterface m (U a)</code> and <code>forall a. (UniformBlock a) =&gt; String -&gt; UniformInterface m (U (Region rw (UB a)))</code> to map whichever uniforms you wanted to.</p>
<p>The issue with that is that it requires to break the interface of <code>createProgram</code> each time we want
to add a new kind of uniform, and it’s also <a href="https://hackage.haskell.org/package/luminance-0.7.2/docs/Graphics-Luminance-Shader-Program.html#v:createProgram">a pretty hard to read function signature</a>!</p>
<p>So… how does luminance-0.8 solve that?</p>
<h2>(Generalized) Algebraic data types, rank-2 and existential quantification</h2>
<p>What is the only way we have to select uniforms? Names. Names can either be a <code>String</code> or a
<code>Natural</code> for explicit semantics. We could encode such a name using an algebraic data type:</p>
<pre><code class="language-haskell">data UniformName
  = UniformName String
	| UniformSemantic Natural
	  deriving (Eq,Show)
</code></pre>
<p>That’s a good start. Though, we still have the problem of choosing the kind of uniform because we
still have several functions – one per kind. We could encode the kind of the uniform directly into
the name. After all, when we ask for a uniform mapping through a name, we require to know the kind.
So that kind of makes sense. Let’s change our <code>UniformName</code> type:</p>
<pre><code class="language-haskell">data UniformName :: * -&gt; * where
  UniformName :: String -&gt; UniformName a
	UniformSemantic :: Natural -&gt; UniformName a
	UniformBlockName :: String -&gt; UniformName (Region rw (UB a))
</code></pre>
<p>That’s neat, but with that definition, we won’t go anywhere, because we’re too polymorphic. Indeed,
<code>UniformName "foo" :: UniformName a</code> can have any <code>a</code>. We need to put constraints on <code>a</code>. And that’s
where <em>GADTs</em> come in so handy! We can hide the constraints in the constructors and bring them into
scope when pattern matching. That’s a very neat feature of <em>GADTs</em>. So now, let’s add some
constraints to our constructors:</p>
<pre><code class="language-haskell">data UniformName :: * -&gt; * where
  UniformName :: (Uniform a) =&gt; String -&gt; UniformName a
	UniformSemantic :: (Uniform a) =&gt; Natural -&gt; UniformName a
	UniformBlockName :: (UniformBlock a) =&gt; String -&gt; UniformName (Region rw (UB a))
</code></pre>
<p>Yes! Now, we can write a function that takes a <code>UniformName a</code>, pattern matches it and call the
appropriate function regarding the infered shape of <code>a</code>!</p>
<p>However, how do we forward the error? In older version of luminance, we were using <code>ProgramError</code>
and more especially two of its constructors: <code>InactiveUniform</code> and <code>InactiveUniformBlock</code>. We
need to shrink that to a single <code>InactiveUniform</code> constructor and find a way to store our
<code>UniformName</code>… But we can’t yet, because of the <code>a</code> parameter! So the idea is to hide it through
existential quantification!</p>
<pre><code class="language-haskell">data SomeUniformName = forall a. SomeUniformName (UniformName a)

instance Eq SomeUniformName where
  -- …

instance Show SomeUniformName where
  -- …
</code></pre>
<p>And now we can store <code>SomeUniformName</code> in <code>InactiveUniform</code>. We won’t need to recover the type, we
just need the constructor and the carried name. By pattern matching, we can recover both those
information!</p>
<h2>Conclusion</h2>
<p>Feel free to have a look at the new <a href="https://hackage.haskell.org/package/luminance-0.8.1/docs/Graphics-Luminance-Shader-Program.html#v:createProgram"><code>createProgram</code> function</a>.
As you will see, the type signature is easier to read and to work with! :)</p>
<p>Have fun, and keep the vibe!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 09 Dec 2015 00:00:00 GMT</pubDate></item><item><title>Let’s talk about C++ constructors</title><link>https://strongly-typed-thoughts.net/blog/c++-constructors</link><description><![CDATA[<p>Lately, I had an interesting talk with colleagues of mine about C++. I told them I needed a safe
<code>std::unique_ptr</code> abstraction. I needed such a type because I was handling scarce resources that
needed to be uniquely owned in our code base, but at the same time, checking every time whether
a value is present (<code>nullptr</code> being the problem here) yields large numbers of bugs in production.</p>
<p>They asked <em>“What’s a safest <code>std::unique_ptr</code>?”</em> My main problem was (still is) that
<code>std::unique_ptr</code> can be initialized with <code>nullptr</code>. The problem with this is that you cannot
assume that you <em>always</em> own a <code>T</code> if you are given a <code>std::unique_ptr&lt;T&gt;</code>. References and wrapped
references cannot be used either because we really want to own the data here.</p>
<p>The rest of the discussion is driven by that initial motivation.</p>
<p>As a (mainly) Haskell and Rust developer, I’m also pretty good at writing C and C++ — after all,
I’ve been doing C and C++ since I’m 11 and today, I’m 28; I’ll let you do the math. I especially
developed 3D stuff, ranging from toy experiments to demoscene productions, and lots of other
low-levels things I won’t detail here.</p>
<p>See, I’ve failed a lot while trying things when I was younger — especially around the age of
15 / 16. That’s basically the age where I started to be <em>very</em> frustrated at C++ and, without even
knowing it, was asking to find a <em>better way to design code</em>. Yes, I failed a lot, I have no shame
stating it. But to me, failing has been a building block to my experience. The more I fail, the less
likely I’ll fail again the same way. By definition, if you fail a lot on different things… you now
have very good hints and ideas about how <em>not to fail again</em>. This is like <em>learn the hard way</em>, but
I truly think it’s important, especially to get <em>why</em> something doesn’t work. Reading books and
being told is important, but failing is even more.</p>
<p>Yes, today, with all the hindsight I’ve been through, all the languages I’ve been using – ranging
from C/C++, D, Python, Haskell, Idris, OCaml, Rust, Java, JavaScript, Perl, C#, GLSL and several
others — I think I have a pretty good idea about what’s out there in terms of language ideas.</p>
<p>The goal of this article is to explain <em>exactly why</em> I don’t like that much C++, <em>why</em> I think it
has a lot of bad ideas, and to provide people with material to think and meditate. I truly think
that a good developer needs to call themselves into question regularly and question their process
and established knowledge. I think that, decades ago, C and C++ were the right way to do things.
Today, I think those languages and especially <em>what has been accepted as ground and established
truth</em> about how to do things has become obsolete.</p>
<p>I’m going to explain why in a series of blog articles, starting with this one. Let’s go.</p>
<!-- vim-markdown-toc GFM -->
<ul>
<li><a href="#c-and-the-wrong-idea-of-constructors">C++ and the wrong idea of constructors</a>
<ul>
<li><a href="#hidden-costs">Hidden costs</a></li>
<li><a href="#the-c-broken-initialization-design">The C++ broken initialization design</a></li>
<li><a href="#construction--constructors">Construction ≠ constructors</a></li>
</ul>
</li>
<li><a href="#c-and-its-bad-standard-library">C++ and its bad standard library</a></li>
<li><a href="#how-would-you-do-that-in-rust">How would you do that in Rust?</a></li>
</ul>
<!-- vim-markdown-toc -->
<blockquote>
<p><a href="https://phaazon.net/blog/c++-exceptions">Next article in the series</a>.</p>
</blockquote>
<h1>C++ and the wrong idea of constructors</h1>
<p>I want to start with a very good example of something that has been accepted as pretty nice when
it was introduced and that is <em>completely</em> obsolete to me. Let’s talk about C++’s <em>constructors</em>.
But not <em>constructors</em> by themselves. I want to focus on <em>how most people</em> use them, especially
around the concept of <em>fallible construction</em> — we’re going to talk about exceptions, too!</p>
<p>For those who are not familiar with constructors in C++, let me explain a bit. In C, when you have
a data type (like a <code>struct</code>), you can create an object with such a type by allocating memory for it
(either on the stack or the heap) and then initialize its fields. Since C99, you also have
the <em>designated initializer</em> syntax to initialize a <code>struct</code> directly while you’re declaring it.
Some examples:</p>
<pre><code class="language-c">struct Foo {
  int x;
  char const* y;
};

int main() {
  Foo foo;
  foo.x = 3;
  foo.y = "Hello, world!";

  // designated initializer syntax
  Foo foo2 = {
    .x = 3,
    .y = "Hello, world!"
  };
}
</code></pre>
<p>Even though that code seems pretty <em>harmless</em>, to me, it’s very easy to misuse C’s construction
syntax. I will not explain exactly why here because we’re interested in C++, but you’ll understand
what’s the problem by reading on. It relates to being possible to leave a state or part of a state
undefined without having your compiler prevent you from doing that.</p>
<h2>Hidden costs</h2>
<p>In C++, you cannot go with any of those way to create a value without a mandatory C++ concept:
<em>constructors</em>. Constructors are C++’s way to initialize objects by using the same kind of syntax.
C++ has several constructors:</p>
<ul>
<li><em>Default constructor</em>. This one is invoked when you want to create an object without constructing
it with any parameter. It’s an important constructor in C++, as several containers and standard
algorithms rely on its presence.</li>
<li><em>Parameterized constructors</em>: you can create as many parameterized constructors as you want and
they will take parameters to initialize your objects based on those parameters.</li>
<li><em>Copy constructors</em>: special constructors called when you want to copy objects. In C++, a
copy constructor is automatically called when you pass an object <code>foo</code> which type is <code>Foo</code> to a
function that expects a <code>Foo</code>, for instance.</li>
<li><em>Move constructors</em>: special constructors called when you <em>move</em> objects around. In C++, a move
constructor is automatically called when you pass a rvalue reference to something (<code>Foo &amp;&amp;</code>).
You can obtain such references by using <code>std::move</code> or <code>std::forward</code>.</li>
</ul>
<blockquote>
<p>There is a slightly exception: <a href="https://en.cppreference.com/w/cpp/language/aggregate_initialization">aggregate initialization</a>, which is a bit similar to the C’s
designated initialization, but requires that you have not declared any constructor – among other
restrictions — and is then always available, either for public or private code. I will then not
talk about it. I also have to admit I never completely recall all the rules about POD designated
initialization.</p>
</blockquote>
<p>Let’s demonstrate some of the constructors from above in the following program:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct Foo {
  // default constuctor
  Foo() {
    std::cout &lt;&lt; "default ctor called" &lt;&lt; std::endl;
  }

  // copy constructor
  Foo(Foo const &amp;) {
    std::cout &lt;&lt; "copy ctor called" &lt;&lt; std::endl;
  }

  // move constructor
  Foo(Foo &amp;&amp;) {
    std::cout &lt;&lt; "move ctor called" &lt;&lt; std::endl;
  }

  // assignment operator by copy
  Foo &amp; operator=(Foo const &amp;) {
    std::cout &lt;&lt; "operator= called by copy" &lt;&lt; std::endl;
    return *this;
  }

  // assignment operator by move
  Foo &amp; operator=(Foo &amp;&amp;) {
    std::cout &lt;&lt; "operator= called by move" &lt;&lt; std::endl;
    return *this;
  }
};

Foo get_foo() {
  return Foo();
}

int main() {
  Foo foo;
  auto foo2 = foo;
  auto foo3 = get_foo();

  return 0;
}
</code></pre>
<p>In the <code>main</code>, what constructors do you think each line is going to call? :) The answer is… it
depends on your compiler. Yes, you heard me right. With my compiler, this is what I get:</p>
<ul>
<li><code>Foo foo;</code>: default constructor, which should be the case for every compiler here.</li>
<li><code>auto foo2 = foo;</code>: copy constructor.</li>
<li><code>auto foo3 = get_foo();</code>: default constructor. This is due to something called <a href="https://en.cppreference.com/w/cpp/language/copy_elision">(N)RVO</a>.</li>
</ul>
<p>This is already pretty bad, for several reasons:</p>
<ul>
<li>It’s hard to tell when we are going to copy something. Think of a more complex data structure,
such as a <code>std::vector&lt;Something&gt;</code>, which might contain thousands of millions of <code>Something</code>.
Because C++ copies by default with the <em>value semantics</em>, you might end up with several copies
while your code doesn’t explicitly copy anything.</li>
<li>Because copy constructors and move constructors are subject to compiler optimizations, we don’t
really know when the <a href="https://en.cppreference.com/w/cpp/language/copy_elision">(N)RVO</a> optimization is going to kick in — we can have good confidence
but we might be wrong. I know some people never really know what <code>auto foo = something;</code> does —
and I feel them, I often spend some time trying to figure out as well.</li>
</ul>
<p>The existence of copy constructors and the value semantics by default makes C++ hard to reason
about when we are making copies. Other languages, such as Rust, mitigate that risk by using a
move semantics by default. If you want to copy (i.e. clone) something, you have to explicitly ask
for it. For instance, consider:</p>
<pre><code class="language-cpp">// C++
void foo(std::vector&lt;Foo&gt; foos) {
  // …
}

int main() {
  std::vector&lt;Foo&gt; foos;

  // …

  foo(foos);

  return 0;
</code></pre>
<p>vs.</p>
<pre><code class="language-rust">fn foo(foos: Vec&lt;Foo&gt;) {
  // …
}

fn main {
  let foos = Vec::new();

  // …

  foo(foos);
}
</code></pre>
<p>The C++ program will copy the content of <code>foos</code> when calling <code>foo</code>. The copy here is due to the
copy constructor of <code>std::vector</code> being called. The effect of is that the whole content of the
heap-allocated region owns by the vector will be copied. I typically call that a <em>deep copy</em>, or
simply a <em>clone</em>.</p>
<p>The Rust program will not make a copy of <code>foos</code>. Instead, it will move ownership from the caller
to the calle. <code>foos</code> won’t be available anymore in <code>main</code> after the call to <code>foo</code>. So no heap
allocation will occur here. If we wanted the same behavior as the C++ one, we would do this:</p>
<pre><code class="language-rust">  foo(foos.clone());
</code></pre>
<p>That requires that <code>Foo</code> implement the <code>Clone</code> trait, but it’s off topic here. However, there a
few places where Rust can still do <em>hidden</em> copies that can hurt your performance. By default,
everything is moved. However, if your type implements the <code>Copy</code> trait, it means that it can be
safely copied. Such a copy is always performed bit-wise. What’s interesting is that there is, in
theory, nothing different at runtime between <em>copying</em> and <em>moving</em> in Rust. Moving might allow
more optimizations to prevent actually copying data, but imagine you don’t have optimizations.
Both copy and move semantics, in Rust, could be implemented with a <code>memcpy</code>. The main difference
is that copying doesn’t lose ownership. So, because the original value, is not moved, can still be
used after copy. Rust has implementations of <code>Copy</code> for arrays, which, thus, can yield bad
performances if passed directly to a function, for instance:</p>
<pre><code class="language-rust">fn bar(_: [u32; 1000000]) {}
</code></pre>
<p>Calling <code>bar</code> will make a copy of the array you pass as argument, which is bad news here. However,
I think it’s pretty unlikely that you allocate such an array on your stack. Most non-primitive
types from the standard library don’t have an implementation for <code>Copy</code>, which makes move follow
the move semantics.</p>
<p>Let’s get back to C++, shall we. We’ve established that C++ makes it <em>very easy</em> to write code
which has hidden copies, because of the existence of copy constructors and that value semantics
depends on it. Let’s go on with a problem I have with constructors that is as bad to me.</p>
<h2>The C++ broken initialization design</h2>
<p>Imagine that you want to be able to manipulate a kind of integer. That integer cannot be negative
and it cannot be equal to <code>0</code>. Also, we want it on 32-bit precision. There are several ways to do
that. Let’s enumerate a few of them (non-exhaustive list):</p>
<ol>
<li>Just use the <code>int32_t</code> type from the standard library and every time we try to create or modify
it, forbid to put values which are less or equal to <code>0</code>.</li>
<li>Use the <code>uint32_t</code> type from the standard library and prevent using <code>0</code>. We don’t need to check
for negative values because <code>uint32_t</code> cannot encode them.</li>
<li>Create a wrapper type that prevents from constructing values that violate the invariant (the
invariant being that it cannot be less than <code>1</code>.</li>
</ol>
<p>Obviously, both (1.) and (2.) have a big drawback: you’re going to spend a lot of runtime to check
that the invariant is not violated. Worse, the invariant <em>leaks in the API</em>. They have an API
contract flaw: they don’t say to the programmers they cannot be equal to <code>0</code>. For instance:</p>
<pre><code class="language-cpp">void foo(uint32_t non_zero) {
  // …
}
</code></pre>
<p>Even though the name of the variable is <code>non_zero</code>, what prevents a user from calling <code>foo</code> with
<code>0</code>? Nothing. They can just call <code>foo(0)</code> and the code will still compile. They will then get a
really bad runtime undefined behavior, or maybe nothing for several runs and a sudden bug… or you
will have to handle the invariant at every call, which doesn’t seem like a lot of fun — and it’s
dangerous because you can forget to check it.</p>
<p>Instead, we want this:</p>
<pre><code class="language-cpp">void foo(NonZero non_zero) {
}
</code></pre>
<p>Here, the function will clearly not be callable with a <code>uint32_t</code>. So the provided value <em>must</em> be
statically verified to be non-zero. The <code>NonZero</code> wrapper type must then ensure the invariant is
non-violated. I now have a question: do you think <code>NonZero</code> has to check the invariant every time
it’s used, or we can do better?</p>
<p>C++ states that <code>NonZero</code> should be constructed with a <em>constructor</em>, and my point is that
constructors are broken, because not all values can be constructed via C++ constructors. But they
are not broken the way you think. In theory, and actually, there is a way to use C++ constructors
in sound ways. Try to spend a few minutes and think about it. Can you implement a <code>struct NonZero</code>
with an API exposing <strong>only constructors</strong> to create and initialize non-zero values? There are two
possibilities:</p>
<ul>
<li>Either you, indeed, use a constructor, but because you want to be able to prevent people from
building non-zero values, you will have to <em>fail</em> in your constructor. And failure in constructors
is possible by throwing an <em>exception</em>. I don’t accept that answer because exceptions should
<strong>only be used in exceptional failure situations</strong>, and this is not one of them. I’ll explain
why.</li>
<li>By still allowing construction of the object but putting it in an invalid state. I consider this
a non-answer as well, as you will have to keep track of the invariant every time you want to use
the <code>NonZero</code>, and because it’s very easy to build a <code>NonZero</code> with a violated invariant with your
code compiled correctly: <code>auto foo = NonZero(0);</code>. So, it’s a <em>no</em>.</li>
</ul>
<p>I want to be very very crystal clear here. Exceptions in C++ are heavy. They require unwinding most
of the time when some code throws them. They are needed to handle exceptional errors, such as an
<em>out of memory</em> error, or a dead thread. People have been abusing exceptions for too long. Really.
Stop using exceptions. Calling <code>NonZero(x)</code> with <code>x</code> being <code>0</code> is an error that originate from
several places, because <code>NonZero</code> is a pretty abstract concept. You shouldn’t assume it’s
exceptional that someone tries to build one with <code>0</code>. It’s part of its API: it must fail to
construct if built with <code>0</code>. The API must convey the fact it can handle that kind of error. So
it’s an error that is covered by the scope of the type and its carried invariant.</p>
<p>An example of exceptional error here would be that, when you construct your <code>NonZero</code>, you’re out
of space on the stack. Throwing an error here seems okay to me. The problem, here, is not that we
have a constuctor that fails. The problem is that we used a constructor to encode fallible
construction. That’s the problem. My point here is that constructors must <strong>not</strong> be used for
fallible constructions.</p>
<p>So… what are we left with? If you really want to <em>only</em> use constructors without exceptions,
you have no other choices but to accept to have a dangling invariant. The invariant here is that
the wrapped <code>uint32_t</code> must not be <code>0</code>.</p>
<blockquote>
<p>A small note on invariants: they must be held <em>before</em> and <em>after</em> you call a public function on
your type. We can violate them inside our private code for performance and optimization purposes,
but don’t forget that you must rebalance everything so that the invariant is held when you give
control back to the user. This is especially <strong>hard</strong> to do in C++ because of exception safety, so
a rule of thumb: try to never violate invariants, even in private code.</p>
</blockquote>
<pre><code class="language-cpp">struct NonZero {
  // prevent people from creating default non-zero values; it makes no sense
  NonZero() = delete;

  NonZero(uint32_t value): _wrapped(value) {
    // that would break your application at runtime on debug and do nothing on release… :(
    assert(_wrapped != 0);
  }

  // let C++ automatically implement some stuff for us, which won’t break the invariant
  NonZero(NonZero const &amp;) = default;
  NonZero(NonZero &amp;&amp;) = default;

  NonZero &amp; operator=(NonZero const &amp;) = default;
  NonZero &amp; operator=(NonZero &amp;&amp;) = default;

private:
  // the invariant must be held on this value
  uint32_t _wrapped;
};
</code></pre>
<p>As you can see, having a <code>NonZero</code> with this definition doesn’t mean the value cannot be <code>0</code>.
Example:</p>
<pre><code class="language-cpp">int main() {
  auto nz = NonZero(0); // meh
  return 0;
}
</code></pre>
<p>This code compiles fine and will either abort at runtime on an assert or just silently do something
very wrong on release code.</p>
<p>So what can we do to this situation?</p>
<h2>Construction ≠ constructors</h2>
<p>The thing is that, if you’ve only done C++ in your life, you’re likely to think that the best thing
to do is to give up and use exceptions. After all, they’re pretty nice.</p>
<pre><code class="language-cpp">#include &lt;stdexcept&gt; // you’ll need that for the exception type

struct NonZero {
  // prevent people from creating default non-zero values; it makes no sense
  NonZero() = delete;

  NonZero(uint32_t value): _wrapped(value) {
    if (_wrapped == 0) {
      throw std::invalid_argument("cannot create a NonZery with 0");
    }
  }

  // let C++ automatically implement some stuff for us, which won’t break the invariant
  NonZero(NonZero const &amp;) = default;
  NonZero(NonZero &amp;&amp;) = default;

  NonZero &amp; operator=(NonZero const &amp;) = default;
  NonZero &amp; operator=(NonZero &amp;&amp;) = default;

private:
  // the invariant must be held on this value
  uint32_t _wrapped;
};
</code></pre>
<p>Now, when you call <code>NonZero(0)</code>, an exception is thrown. You can catch it with a <code>try</code> / <code>catch</code>
block. But please wait a minute. Several points:</p>
<ul>
<li>Again, exceptions should <strong>not</strong> be used for such errors. I get it you’ve given up on that rule,
but you shouldn’t. Save exceptions for exceptional cases. This is <em>not</em> exceptional. This is
<a href="https://phaazon.net/media/uploads/what_a_story_mark.gif">naaaaaaaaaaht</a>.</li>
<li>When you look at the constructor definition, nothing tells you it can fail. You have to read the
body of the constructor to check for <code>throw</code> — and again, it could quickly get hard because you
might call functions that throw too…</li>
</ul>
<p>That last point is what makes C++ a very hard language what it comes to error handling to me.
Error handling, most of the time, is based on exceptions, which are invisible at the type-level.
I got some remarks stating that, nowadays, modern C++ code bases use the <code>noexcept</code> keyword when
something cannot fail, and the rest of the time, we must assume something can fail. Okay, but
then, fail <em>how</em>? What kind of error? How do I know that? Rely on whether it’s written in a
Doxygen documentation, that, most of the time, doesn’t even exist? Also, just have a look at how
people handle failures in C++. Do you see those <code>try </code> / <code>catch</code> <em>everywhere</em> <code>noexcept</code> is not
annotated? No, because people don’t use <code>noexcept</code> and when they use library, they don’t even
apply that advice to themselves either. The problem with exceptions not being visible in types and
function signatures is that you have to read the code (or the missing Doxygen documentation :)) to
<em>find out</em> what exceptions you need to catch. The other problem is that they can be ignored and
implicitely and automatically propagated upwards in the call stack. There is no way to force a
user to either handle the error or explicitly pass it to their caller.</p>
<p>All that to say: don’t do this. Don’t think constructors are a good idea to construct types in all
possible situations. Failible constructions shouldn’t be done with public constructors and
exceptions. However, it doesn’t mean constructors cannot be used to implement fallible constructions.
That seems tricky, and you’re right. You might wonder:</p>
<blockquote>
<p><em>“Okay okay, you’ve advanced points but… still, how would you do it?”</em></p>
</blockquote>
<p>My implementation — and today, I would only accept that one — to this problem is this: since we
cannot create a value without using a constructor, and since constructors fail to encode fallible
constructions without using exceptions — which I don’t accept either, I’m going to use a private
constructor to build my <code>NonZero</code> and <em>not</em> check the invariant. Then, using a <code>static</code> method, I
can return a more typed object to encode a possible failure, by checking the invariant <em>before</em>
creating the object.</p>
<p>Consider:</p>
<pre><code class="language-cpp">#include &lt;optional&gt;

struct NonZero {
  // prevent people from creating default non-zero values; it makes no sense
  NonZero() = delete;

  // let C++ automatically implement some stuff for us, which won’t break the invariant
  NonZero(NonZero const &amp;) = default;
  NonZero(NonZero &amp;&amp;) = default;

  NonZero &amp; operator=(NonZero const &amp;) = default;
  NonZero &amp; operator=(NonZero &amp;&amp;) = default;

  // static method used to create a NonZero; the only way to create one
  // using the public interface
  std::optional&lt;NonZero&gt; from_u32(uint32_t value) {
    std::optional&lt;NonZero&gt; r;

    if (value != 0) {
      // call to the private ctor; the invariant is already checked in this branch
      r = NonZero(value);
    }

    return r;
  }

private:
  // private constructor that doesn’t check the invariant
  NonZero(uint32_t value): _wrapped(value) {}

  // the invariant must be held on this value
  uint32_t _wrapped;
};
</code></pre>
<p>So let me explain a bit all this code. First, the <code>NonZero(uint32_t)</code> constructor is private. It
has to be private because it doesn’t enforce the invariant. It allows to construct any <code>NonZero</code>.
We need it because C++ requires you to use a constructor to create a value of type <code>NonZero</code>.
Because I don’t want exceptions, I just don’t check the invariant here so that constructor
cannot fail.</p>
<p>The <code>static</code> method called <code>from_u32</code> is the only entry-point to create a value of type
<code>NonZero</code>. As you can see, it returns a <code>std::optional&lt;NonZero&gt;</code>, which means that it can fail.
If you call that function with <code>0</code>, you will get an empty optional value. That function is
implemented by creating an optional value, allocated on the stack with the default constructor —
which makes it empty. Then, we check the invariant and if it’s not violated, we allocate and
initialize the <code>NonZero</code>, and return the whole thing.</p>
<h1>C++ and its bad standard library</h1>
<p>For the rest of the article, consider we add the following method to <code>NonZero</code>:</p>
<pre><code class="language-cpp">  uint32_t value() const {
    return _wrapped;
  }
</code></pre>
<p>If you’ve followed carefully what we’ve been doing here, a call to <code>NonZero::value()</code> will <em>never</em>
return <code>0</code>, because construction statically disallows building such non-zero values. You then don’t
have to check for it!</p>
<p>However, we’re not completely done. We’ve used the <code>std::optional</code> standard type. How are we
supposed to use that type? Looking at the official documentation, we get use the <a href="https://en.cppreference.com/w/cpp/utility/optional/operator*"><code>operator*</code> or
<code>operator-&gt;</code></a> to access the
underlying object. We have the
<a href="https://en.cppreference.com/w/cpp/utility/optional/operator_bool"><code>has_value()</code></a> method to check
whether a value is present.</p>
<pre><code class="language-cpp">auto nz = NonZero::from_u32(0);

if (nz.has_value()) {
  std::cout &lt;&lt; "We have a value! " &lt;&lt; nz-&gt;value() &lt;&lt; std::endl;
}
</code></pre>
<p>That seems nice, right? Well, not really. C++ doesn’t have <em>exhaustive pattern matching</em>, which
makes it impossible to statically ensure you exhaustively check a <code>std::optional</code>. What it means
is that, given the current contract based on <code>has_value()</code> and the dereference operators, we
can still break our program and bring <em>undefined behaviors</em> without having our compiler complain.</p>
<pre><code class="language-cpp">auto nz = NonZero::from_u32(0);

std::cout &lt;&lt; "We have a value… right? " &lt;&lt; nz-&gt;value() &lt;&lt; std::endl;
</code></pre>
<p>That program is perfectly fine and valid. However it’s not. It’s completely wrong, because
<code>std::optional</code>’s API allows developers to access a value that might not be there.</p>
<p>This is the same situation as doing this:</p>
<pre><code class="language-cpp">int * ptr = nullptr;
int x = *ptr;
</code></pre>
<p>Statically fine. Dynamically pretty bad, right? C++’s standard library’s <code>std::optional</code> could
have been done in a much safer way, by, for instance, providing you with some combinators to
work with the underlying type. C++ doesn’t have <em>exhaustive pattern matching</em> but it still has
higher-order functions. We could then have something like:</p>
<pre><code class="language-cpp">auto nz = NonZero::from_u32(0);

nz.maybe(
  // called if no value is present
  []() {
    std::cout &lt;&lt; "No value there." &lt;&lt; std::endl;
  },
  // called if a value is present
  [](NonZero const &amp;v) {
    std::cout &lt;&lt; "We have a value: " &lt;&lt; v.value() &lt;&lt; std::endl;
  }
);
</code></pre>
<p>For most C++ developers, that code will look like very verbose and wrong, and I agree. Without
pattern matching, it’s very hard to safely and statically use a <code>std::optional</code>.</p>
<h1>How would you do that in Rust?</h1>
<p>In Rust, we have <em>exhaustive static dispatch</em>. <code>std::optional</code> is called <code>Option</code> and Rust
doesn’t have constructors <em>at all</em>.</p>
<p>Values can be constructed by providing <em>all</em> fields, if they are all available (i.e. that’s
always the case in the module the type is defined in; for the rest, you need access to the fields
by marking them all <code>pub</code>).</p>
<p>This is my <code>NonZero</code> wrapper in Rust:</p>
<pre><code class="language-rust">struct NonZero {
  // we don’t make that value pub so no one can directly construct a NonZero
  // outside of our module
  wrapped: u32,
}

impl NonZero {
  // yes, new is nothing special in Rust, so we can use it as a method!
  // Self references the current type we’re adding an impl for (here, NonZero)
  pub new(wrapped: u32) -&gt; Option&lt;Self&gt; {
    if wrapped == 0 {
      None
    } else {
      Some(NonZery { wrapped })
    }
  }
}

fn main() {
  match NonZero::new(0) {
    Some(value) =&gt; println!("we have a value! {}", value),
    None =&gt; println!("we don’t have a value… :("),
  }
}
</code></pre>
<p>That’s all. The program is statically safe, as the compiler can make sure we’re correctly using
the <code>Option&lt;NonZero&gt;</code>. We could use combinators that do the pattern matching for us. For
instance, imagine that you want the value <code>8080</code> if it’s not correctly constructed, or what it
contains otherwise:</p>
<pre><code class="language-rust">let port = nz.unwrap_or(8080);
</code></pre>
<p>Here, the type of <code>port</code> is <code>NonZero</code>. <code>Option::unwrap_or</code> might be implemented like this:</p>
<pre><code class="language-rust">impl&lt;T&gt; Option&lt;T&gt; {
  pub fn unwrap_or(self, default_value: T) -&gt; T {
    match self {
      Some(v) =&gt; v,
      None =&gt; default_value,
    }
  }
}
</code></pre>
<p>There are a lot of similar combinators, like <code>Option::map</code>, <code>Option::or</code>, <code>Option::unwrap_or_else</code>,
etc. I’ll let you dig by yourselves. However, there is an important point to make here. In C++,
there is the <code>std::optional::value()</code> method that returns the value if it’s present or just throws
an error (which I dislike). In Rust, there is the <code>std::Option::unwrap()</code> method, that returns the
value if it’s present or <em>panics</em> otherwise… which I also dislike. <code>unwrap()</code> can be dangerous in
Rust and I think it should be marked <code>unsafe</code>. But a more detailed article is needed about why
I want it to be marked <code>unsafe</code> and it’s off topic anyways.</p>
<p>So, we’re hitting the end of the article. It might be a lot of information; sorry. To sum up, in
C++:</p>
<ul>
<li>Construction must not be assumed to be done exclusively via constructors.</li>
<li><em>Pure</em> construction (as in infallible) can use constructors if you want.</li>
<li><em>Fallible construction</em> can use:
<ul>
<li>A static function that returns a typed object via, for instance, <code>std::optional</code> or similar,
and enforce invariants / pre-conditions.</li>
<li>A private constructor to build the result value <em>after</em> that invariant and pre-conditions get
checked.</li>
</ul>
</li>
<li>We see that we can make C++ much easier to reason about by following those rules. It will not be
as handy as in Rust where no constructor are needed, but it’s still a good improvement to me.</li>
</ul>
<p>Next time, I’ll bring you on a tour with me around the concept of <em>inclusion polymorphism</em>, which
is most of the time called <em>inheritance</em> in the world of OOP languages. But we’ll still stick to
C++. ;)</p>
<p><a href="https://phaazon.net/media/uploads/the_golden_smile.gif">Keep the vibes!</a></p>
<blockquote>
<p><a href="https://www.reddit.com/r/rust/comments/f8j304/lets_talk_about_c_constructors">Discussion on the article here.</a></p>
</blockquote>
<blockquote>
<p><a href="https://phaazon.net/blog/c++-exceptions">Keep on reading</a>.</p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 24 Feb 2020 00:35:00 GMT</pubDate></item><item><title>Abstracting shader – Environment</title><link>https://strongly-typed-thoughts.net/blog/abstracting_over_shader_environment</link><description><![CDATA[<h1>In the previous episode…</h1>
<p>This blog entry directly follows the one in which I introduced <strong>Ash</strong>, a <em>shading language</em> embedded in <strong>Haskell</strong>. Feel free to <a href="http://phaazon.blogspot.co.uk/2014/11/foreword-abstracting-what-shaders-are.html">read it here</a> before going on.</p>
<h1>Controlling behavior</h1>
<p>A <em>shader</em> is commonly a function. However, it’s a bit more than a simple function. If you’re a haskeller, you might already know the <code>MonadReader</code> typeclass or simply <code>Reader</code> (or its transformer version <code>ReaderT</code>). Well, a shader is kind of a function in a reader monad.</p>
<blockquote>
<p><em>So… that implies a shader runs in… an environment?</em></p>
</blockquote>
<p>Yeah, exactly! And you define that environment. The environment is guaranteed not to change between two invocations of a shader for the same render (e.g. between two vertices in the same render). This is interesting, because it enables you to use nice variables, such as time, screen resolution, matrices and whatever your imagination brings on ;)</p>
<p>The environment, however, can be changed between two renders, so that you can update the time value passed to the shader, the new resolution if the window resizes, the updated matrices since your camera’s moved, and so on and so forth.</p>
<p>Let’s see a few example in GLSL first.</p>
<h2>Shader environment in GLSL</h2>
<p>To control the environment of a shader in GLSL, we use <em>uniform</em> variables. Those are special, global variables and shared between all stages of a shader chain<sup class="footnote-reference"><a href="#shader_chain">1</a></sup>.</p>
<p>Let’s see how to introduce a few <em>uniforms</em> in our shader:</p>
<pre><code>uniform float time;       // time of the host application
uniform vec2 resolution;  // (w,h)
uniform vec2 iresolution; // (1 / w, 1 / h), really useful in a lot of cases ;)
uniform mat4 proj;        // projection matrix
uniform int seed;         // a seed for whatever purpose (perlin noise?)
uniform ivec2 gridSize;   // size of a perlin noise grid!
</code></pre>
<p>You got it. Nothing fancy. Those uniforms are shared between all stages so that we can use <code>time</code> in all our shaders, which is pretty cool. You use them as any kind of other variables.</p>
<p>Ok, let’s write an expression that takes a time, a bias value, and multiply them between each other:</p>
<pre><code>uniform float time;
uniform float bias;

// this is not a valid shader, just the expression using it
time * bias;
</code></pre>
<h2>Shader environment in HLSL</h2>
<p><strong>HLSL</strong> uses the term <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ff476896(v=vs.85).aspx">constant buffers</a> to introduce the concept of environment. I don’t have any examples right now, sorry for the inconvenience.</p>
<h2>Shader environment in Ash</h2>
<p>In Ash, environment variables are not called uniforms nor constant buffers. They’re called… <em>CPU variables</em>. That might be weird at first, but let’s think of it. Those values are handled through your application, which lives CPU-side. The environment is like a bridge between the CPU world and the GPU one. A CPU variable refers to a constant value GPU-side, but varying CPU-side.</p>
<p>Create a <em>CPU variable</em> is pretty straight-forward. You have to use a function called <code>cpu</code>. That function is a monadic function working in the EDSL monad. I won’t describe that type since it’s still a work in progress, but it’s a monad for sure.</p>
<blockquote>
<p><strong>Note: If you’ve read the <a href="http://phaazon.blogspot.co.uk/2014/11/foreword-abstracting-what-shaders-are.html">previous blog entry</a>, you might have come across the <code>Ash</code> type, describing a HOAST. That type is no more a HOAST. The “new Ash” – the type describing the HOAST – is now Expr.</strong></p>
</blockquote>
<p>This is <code>cpu</code>:</p>
<pre><code>cpu :: (CPU a) =&gt; Chain (Expr a)
</code></pre>
<p><code>CPU</code> is a typeclass that enables a type to be injected in the environment of a shader chain. The instances are provided by Ash and you can’t make your own – do you really want to make <code>instance CPU String</code>, or <code>instance (CPU a) =&gt; CPU (Maybe a)</code>? Don’t think so ;)</p>
<p>Let’s implement the same time–bias example as the GLSL one:</p>
<pre><code>foo :: Chain (Expr Float)
foo = liftA2 (*) cpu cpu
</code></pre>
<p>That example is ridiculous, since in normal code, you’d actually want to pass the CPU variables to nested expressions, in shaders. So you could do that:</p>
<pre><code>foo :: Chain ()
foo = do
  time &lt;- cpu
  bias &lt;- cpu

  -- do whatever you want with time and bias
  return ()
</code></pre>
<h1>You said Chain?</h1>
<p><code>Chain</code> is a new type I introduce in this paper. The idea came up from a discussion I had with Edward Kmett when I discovered that the EDSL <strong>needed</strong> a way to bind the CPU variables. I spotted two ways to go:</p>
<ul>
<li>using a name, like <code>String</code>, passed to <code>cpu</code>; that would result in writing the name in every shader using it, so that’s not ideal;</li>
<li>introducing the environment and providing a monad instance so that we could bind the CPU variables and use them in shaders inside the monad.</li>
</ul>
<p>The latter also provides a nice hidden feature. A chain of shaders might imply varying<sup class="footnote-reference"><a href="#varying">2</a></sup> values. Those varying values have information attached. If you mistake them, that results in catastrophic situations. Using a higher monadic type to capture that information – along with the environment – is in my opinion pretty great because it can prevent you from going into the wall ;).</p>
<p>To sum up, <code>Chain</code> provides a clear way to describe the relation between shaders.</p>
<h1>What’s next?</h1>
<p>I’m still building and enhancing Ash. In the next post, I’ll try to detail the interface to build functions, but I still need to find how to represent them the best possible way.</p>
<div class="footnote-definition" id="shader_chain"><sup class="footnote-definition-label">1</sup>
<p>You can imagine a shader chain as an explicit composition of functions (i.e. shaders). For instance, a vertex shader followed by geometry shader, itself followed by a fragment shader.</p>
</div>
<div class="footnote-definition" id="varying"><sup class="footnote-definition-label">2</sup>
<p>Varying values are values that travel between shaders. When a shader outputs a value, it can go to the input of another shader. That is call a varying value.</p>
</div>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Mon, 17 Nov 2014 00:00:00 GMT</pubDate></item><item><title>Rust and features discoverability</title><link>https://strongly-typed-thoughts.net/blog/rust-features-documentation</link><description><![CDATA[<p>I’ve been working on several projects lately and writing a bit about them on my blog – especially,
specific technical problems. This very blog entry is on a different level though: it’s about our
tooling in Rust.</p>
<p>See, most tools in Rust are wonderful:</p>
<ul>
<li><code>rustc</code>, the official compiler, is pretty easy to use and even though it’s an advanced piece of
software, it’s pretty unlikely you’ll have to use it directly (unless you’re doing something
very exotic, or working on it directly).</li>
<li><code>cargo</code>, by far the most appealing tool in the Rust ecosystem to me, is a Rust build system and
package manager. It downloads and checks your projects’ dependencies, using https://crates.io as
a default mirror for your crates (but you can
<a href="https://doc.rust-lang.org/cargo/reference/unstable.html#alternate-registries">use other mirrors if you want and are brave enough</a>,
even yours), (cross) compiles, runs library, binary and even documentation tests, etc.</li>
<li><code>cargo</code> also comes with a plugin system, allowing people to write cargo <em>subcommands</em> that can
be installed like with <code>cargo install cargo-tree</code> and run with <code>cargo tree</code>. Most common plugins
are:
<ul>
<li><code>bloat</code>.</li>
<li><code>watch</code>.</li>
<li><code>tree</code>.</li>
<li><code>clippy</code>.</li>
<li><code>fuzz</code>.</li>
<li><code>expand</code>.</li>
<li><code>outdated</code>.</li>
<li>List <a href="https://github.com/rust-lang/cargo/wiki/Third-party-cargo-subcommands">here</a>.</li>
</ul>
</li>
<li><code>rustup</code>, used to select, switch, install and update Rust compilers, toolchains, support
cross-compilation and select targets, etc.</li>
<li><a href="https://github.com/rust-lang-nursery/rls">RLS</a>.</li>
<li>Plus some additional, non-local tools such as
<ul>
<li><a href="https://play.rust-lang.org">Rust Playground</a>.</li>
<li><a href="https://crates.io">crates.io</a>, obviously.</li>
<li>The <a href="https://doc.rust-lang.org/std">excellent Rust documentation</a>, giving you information and
documentation about the standard library, the procedural macro, the allocation system, etc.</li>
<li>The <a href="https://docs.rs">docs.rs</a> documentation host, hosting ALL crates’ documentation for free,
automatically and with fuzzy searching.</li>
</ul>
</li>
<li>And many others.</li>
</ul>
<p>Clearly, you can see we have lots of wonderful tools made by talented people and you can clearly
feel how easy it is to start writing a crate, test it and publish it along with its documentation.
It feels seamless. The documentation is a very important thing in any tech ecosystem for several
reasons. And this blog is going to be about doc tooling and some specific parts of Rust.</p>
<h1>Documentation is key to achieve good quality… but not only</h1>
<p>Whatever the project you work on, you <sub>should</sub> <strong>must</strong> document your code. There are several
situations – let’s call this the <strong>First Hypothesis</strong>:</p>
<p>If you write code that is intended to be released publicly, you must document every public
symbols:</p>
<ul>
<li>Structs, enums and type aliases.</li>
<li>Any public field.</li>
<li>Constants.</li>
<li>Functions and macros.</li>
<li>Traits along with their associated types / constants and methods.</li>
<li>Trait impls (think of how useful the documentation of an <code>impl Default</code> can be!).</li>
<li>Modules.</li>
</ul>
<p>Now, if you are writing something that is private to a crate that you plan to release, think about
people who will want to contribute to help you with the crate. They will have to get to read your
code. They will have to go through the (sometimes painful) process of adapting to someone else’s
way to write code. So do them a favor: document your code.</p>
<p>Finally, what about things you don’t plan to release? Like, for instance, a binary? The same rule
actually applies: what happens if someone tries to contribute? What if you save the project aside
for some weeks and get back to it afterwards?</p>
<p>Those three paragraphs just show you that the <strong>First Hypothesis</strong> was wrong: there are not several
situations. Only one: as soon as you write code, whatever whom it’s aimed at, just document it.
You’ll be thanked and you might even thank yourself in a near future for doing so. Documenting your
code enables you to run <code>cargo doc --open</code> and immediately start <em>exploring</em>. Exploring is when you
need to catch up with a project’s ideas, concepts, or whenever you join a team as a new job and need
to get used to the codebase.</p>
<p>I want to share my experience here: most people are <em>bad</em> at this – it’s not necessarily their
faults, don’t blame them. Most teams I worked in were under high pressure, with business deadlines
to meet – I’ve been there and still am. This is more about a political discussion to have with “the
ones who give you such deadlines” but really, <strong>developing a project doesn’t stop at writing code
and testing.</strong> Onboarding and discoverability should be considered of a massive importance, because
it helps preventing the project from burying itself and getting too bloated for newcomers or even
long-running team members to understand <em>the actual heck is happening there</em>. When someone new joins
your team, you should help them to get accustomed to the codebase… but you also have to work. They
should have <em>leads</em> or <em>hints</em> to get their feet wet. Several solutions exist to document that in
<em>kind of</em> standardized ways:</p>
<ul>
<li>Write a <code>README.md</code> in all the projects your team has. This should be mandatory. Describe what
the project is about, how to build it as a developer, how to run it as a dev-ops. Describe the
architecture, etc.</li>
<li>Write a <code>CONTRIBUTING.md</code> to help onboarding newcomers with your team guidelines, conventions,
engineering processes, etc.</li>
<li>Be consistent: it’s not because a small project consists in only a single shell script that you
shouldn’t write a <code>README.md</code>: <strong>write it</strong>!</li>
<li>Something that I love doing, especially at work: have some kind of a cron / task that builds the
documentation for all your projects across the scope of your team… and have it host it all
on a server documented in the top-level <code>README.md</code> of your team. This is a real plus: people
will just go to that documentation link to search for things <em>“that have already been done by
the team”</em> instead of reinventing the wheels – trust me, even the ones who wrote the features
might have forgotten writing them already.</li>
</ul>
<p>We’re now reaching the point of this blog entry: documentation can (should?) be used to explore what
a project is about, how to use it, what are the public variants and invariants, etc. But there is
however a problem in the current Rust ecosystem, preventing us from completely have a comfortable
exploration of a crate or set of crates. A very, very important kind of public symbol is missing
from the list of documented Rust symbols. Do you think you can figure out which one?</p>
<h1>The missing piece to exploring a crate</h1>
<p>Have you ever worked with a customized crate? If you don’t know what I’m referring to, I’m thinking
about crates that can be configured with <em>features</em>. For instance, as a good example of this, I’ll
talk about a crate of mine: <a href="https://crates.io/crates/splines">splines</a>. The crate has several features you can set to achieve
different effects:</p>
<ul>
<li><code>"serialization"</code>: this feature will turn on the <a href="https://crates.io/crates/serde">serde</a> serialization code (both <code>Serialize</code>
and <code>Deserialize</code>) for all the public types exposed by <a href="https://crates.io/crates/splines">splines</a>.</li>
<li><code>"impl-cgmath"</code>: turn this on to have some <a href="https://crates.io/crates/cgmath">cgmath</a>’s types be usable directly within <a href="https://crates.io/crates/splines">splines</a>
by implementing the appropriate traits from <a href="https://crates.io/crates/splines">splines</a>.</li>
<li><code>"impl-nalgebra"</code>: same thing as <code>"impl-cgmath"</code> but for <a href="https://crates.io/crates/nalgebra">nalgebra</a>.</li>
<li><code>"std"</code>: automatically enabled, you can disable it with <code>default-features = false</code> or
<code>--no-default-features</code>. When disabled, the code behaves compiled with <code>no_std</code>.</li>
</ul>
<p>All of this is currently documented in the <code>README.md</code> of the project and the top-level
documentation of the project (for instance, <code>splines-0.2.3</code> has <a href="https://docs.rs/splines/0.2.3/splines/#features-and-customization">this paragraph about features and
what they do</a>).</p>
<p>We have two problems here:</p>
<ul>
<li>It’s hard for a newcomer to <em>discover</em> and <em>explore</em> features of <a href="https://crates.io/crates/splines">splines</a> (or any crate using
features) because they’re not documented like a Rust symbol is (a function, a type, etc.), while
they truly <strong>are</strong> recognized by <code>rustc</code> and <code>cargo</code>!</li>
<li>We can <em>“bypass”</em> this problem by documenting and having a list of features like I do with
<a href="https://crates.io/crates/splines">splines</a>, but that requires the project contributors to ensure to update the list whenever they
add, remove or alter the features set.</li>
</ul>
<p>You can see that the situation is not really comfortable. I looked into the list of PRs and issues
about that topic on <a href="https://github.com/rust-lang/rfcs">rust-lang/rfcs</a> and didn’t find anything.
So I might start to write some ideas here and maybe, depending on the feedback, write a new RFC to
fix that problem.</p>
<h1>Pre-RFC: documenting and exploring crate features</h1>
<p>In order to know what modification we can do without introducing breaking changes, we need to have
a look at how features are defined. This is done in a <code>Cargo.toml</code> manifest, such as (from
<a href="https://crates.io/crates/splines">splines</a>):</p>
<pre><code>[features]
default = ["std", "impl-cgmath"]
serialization = ["serde", "serde_derive"]
std = []
impl-cgmath = ["cgmath"]
impl-nalgebra = ["nalgebra"]
</code></pre>
<p>A feature is a key-value object where the key is the name of the feature and the value is a list of
dependencies (crate names, crate cluster, a feature of another crate, etc. – more about that
<a href="https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section">here</a>).</p>
<p>We could document the features here. After all, that’s what Haskell does with <code>cabal</code> and <em>flags</em>:</p>
<pre><code>Flag Debug
  Description: Enable debug support
  Default:     False
</code></pre>
<p>So why not simply do the exact same in Rust within a <code>Cargo.toml</code> manifest? Like so:</p>
<pre><code>[features]
default = ["std", "impl-cgmath"]

[features.serialization]
description = "Set to automatically derive Serialize and Deserialize from serde."
dependencies = ["serde", "serde_derive"]

[features.std]
description = "Compile with the standard library. Disable to compile with no_std."
dependencies = []

[features.impl-cgmath]
description = "Implement splines’ traits for some cgmath public types."
dependencies = ["cgmath"]

[features.impl-nalgebra]
description = "Implement splines’ traits for some nalgebra public types."
dependencies = ["nalgebra"]
</code></pre>
<p>In order to introduce this feature in a retro-compatible way, using the <em>“old”</em> syntax would just
behave the same way but without a description, making them optional.</p>
<p>The whole purpose of this addition would be that the rustdoc team could assume the existence of an
optional <code>description</code> field on features and present them when browsing a crate’s documentation,
enriching the exploration and discovering experiences of developers. Instead of having to look at
both <code>lib.rs</code> and <code>Cargo.toml</code> to look for features, one could just simply go on https://docs.rs,
look for the crate they want to get features information <em>et voila</em>!</p>
<h2>Features consequences</h2>
<p>Some people might think of the <em>consequences</em> of features. Those are, after all, used for
conditional compilation. What that means is that part of the code might be hidden from the
documentation if it’s feature-gated and that the feature wasn’t set when generating the
documentation. An example with <a href="https://crates.io/crates/splines">splines</a> <a href="https://docs.rs/splines/0.2.3/splines/trait.Interpolate.html#foreign-impls">here</a>.
You can see a few implementors of the <code>Interpolate</code> trait (some basic types and some from <a href="https://crates.io/crates/cgmath">cgmath</a>
because the <code>"impl-cgmath"</code> feature is enabled by default) but not the potential ones for
<a href="https://crates.io/crates/nalgebra">nalgebra</a>, which is somehow misleading – people don’t know they can actually use <a href="https://crates.io/crates/nalgebra">nalgebra</a> with
<a href="https://crates.io/crates/splines">splines</a>!</p>
<p>A workaround to this problem is to get a local copy of the documentation, generating it with the
wished features. However, this will not fix the problem of the discoverability.</p>
<p>A solution to this could be to simply <em>“ignore”</em> the features while generating the documentation –
and only while generating it <strong>but annotate the documented symbols with the features.</strong> That would
enable something like this (generated with <code>cargo doc --open --no-default-features</code> and retouched
to mock the idea up):</p>
<p><img src="https://phaazon.net/media/uploads/documented_features.png" alt="" /></p>
<p>If several features are required, they would just be presented as a list above the annotated item.</p>
<p>That’s all for me today. Please provide your feedback about that idea. If you like it, I’ll write an
RFC because I really think it’s a missing thing in the documentation world of Rust.</p>
<p>Keep the vibes!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 13 Oct 2018 23:37:00 GMT</pubDate></item><item><title>On impl blocks, injection and surjection in Rust</title><link>https://strongly-typed-thoughts.net/blog/on-rust-impl-block</link><description><![CDATA[<p>Rust has this cool feature called <em><code>impl</code> block</em>. An <code>impl</code> block is just a scope that introduces a
way to augment a type with methods – do not confuse <code>impl</code> blocks with <em>trait</em> <code>impl</code>s, used to
implement a given trait.</p>
<p>The syntax is the following:</p>
<pre><code class="language-rust">pub struct MyId(u32);

impl MyId {
  /// A method that creates a null identifier.
  pub fn nil() -&gt; Self {
    MyType(0)
  }

  /// A method that checks whether an ID is odd (why not?).
  pub fn is_odd(&amp;self) -&gt; bool {
    self.0 % 2 != 0
  }

  // some other methods…
}
</code></pre>
<p>This syntax gives you the <em>dot notation</em> and the <em>static method</em> call syntax:</p>
<pre><code class="language-rust">let x = MyId::nil(); // static method call

println!("is it odd? {}", x.is_odd()); // dot notation
</code></pre>
<p>Most of the time, you’ll want <code>impl</code> blocks for:</p>
<ul>
<li>The <em>dot notation</em>.</li>
<li>Sharing type variables.</li>
</ul>
<h1>Type variables in impl blocks</h1>
<p>A <em>type variable</em> is a variable that holds a type. People coming from languages like C++, Java,
C#, Python, D, etc. are used to name those <em>template parameters</em> or <em>type parameters</em>.</p>
<pre><code class="language-rust">pub struct List&lt;T&gt; {
  // hidden
}

impl&lt;T&gt; List&lt;T&gt; {
  // hidden
}
</code></pre>
<p><code>T</code> here is a type variable, and you can see that you can have an <code>impl</code> block over <code>List&lt;T&gt;</code>. What
that means is that any declared method will have a hidden type <code>Self = List&lt;T&gt;</code>. If you declare a
static method, <code>Self</code> will be set as <code>List&lt;T&gt;</code> and if you declare a regular method, <code>self</code>, <code>&amp;self</code>
or <code>&amp;mut self</code> will have types – respectively – <code>List&lt;T&gt;</code>, <code>&amp;List&lt;T&gt;</code> and <code>&amp;mut List&lt;T&gt;</code>.</p>
<p>That sharing is even more pronounced when you start to deal with more complex data types and want to
restrain their type variables with <em>trait bounds</em>. For instance, imagine that you want an ordered
list:</p>
<pre><code class="language-rust">pub struct OrderedList&lt;T&gt; {
  // hidden
}
</code></pre>
<p>You could implement <code>OrderedList&lt;T&gt;</code> for any <code>T</code>, but it’s very likely that for <em>a lot</em> of methods,
you’ll need <code>T</code> to be comparable. So you can do this, for example:</p>
<pre><code class="language-rust">impl&lt;T&gt; OrderedList&lt;T&gt; where T: PartialOrd {
  // hidden
}
</code></pre>
<p>All the methods and static methods will have <code>Self</code> an alias to <code>List&lt;T&gt; where T: PartialOrd</code>. This
will drastically help you when writing methods that all require the trait bound.</p>
<p>Note that you can have several <code>impl</code> blocks for any kind of refactoring. For instance, if you
<em>also</em> want to have two methods that don’t need the trait bound, you can go and add another <code>impl</code>
block that looks like:</p>
<pre><code class="language-rust">impl&lt;T&gt; OrderedList&lt;T&gt; {
  pub fn new() -&gt; Self {
    // hidden
  }

  pub fn singleton(sole: T) -&gt; Self {
    // hidden
  }
}
</code></pre>
<p>Now let’s try something a bit more complex. Let’s try this:</p>
<pre><code class="language-rust">trait Foo&lt;E&gt; {
  fn foo(&amp;self, x: E);
}

impl&lt;T, E&gt; OrderedList&lt;T&gt; where T: Foo&lt;E&gt; {
  fn invoke_foo(&amp;self, e: E) {
    for t in self {
      t.foo(e);
    }
  }
}
</code></pre>
<p>This won’t work and rustc will complain that <code>E</code> is not contrained enough (it doesn’t appear in
<code>Self</code>, it’s only used in a trait bound on <code>T</code>). So what’s the problem?</p>
<blockquote>
<p>You can test <a href="https://play.rust-lang.org/?gist=993db37f3544929db973861075650158&amp;version=stable&amp;mode=debug&amp;edition=2015">here</a></p>
</blockquote>
<h1>Enter surjection and injection</h1>
<p>The problem with that last block is that <code>E</code> is not constrained on <code>OrderedList&lt;T&gt;</code>. If you
don’t know what it means, don’t freak out: I’m going to introduce every concepts you need to know.
Hang on, it’s gonna be a bit math–ish, but that is worth it.</p>
<h2>Surjection</h2>
<p>In math, a <em>surjection</em> is a property about set morphisms. For a pair of sets <code>C</code> and <code>D</code> and a
morphism <code>f : C -&gt; D</code>, we say that this morphism is <em>surjective</em> if all elements from <code>D</code> appear in
a <code>f</code> application. This is weirdly said, so let’s say it another way: if for all the elements
<code>y</code> in <code>D</code>, you can find <em>at least one</em> element <code>x</code> in <code>C</code> so that <code>f(x) = y</code>. Another way to
swallow that down if you’re still choking is by drawing the <code>C</code> and <code>D</code> sets as big bags with a few
elements in there. The morphism <code>f</code> applications are just lines from objects from <code>C</code> to <code>D</code>. If all
elements from <code>D</code> have at least one arriving arrow, the morphism is surjective.</p>
<p>We write surjection this way:</p>
<pre><code>Ɐy ∈ D, ∃x ∈ C, f : C -&gt; D / f(x) = y
</code></pre>
<p>And we read it this way:</p>
<ul>
<li><code>Ɐy</code> means <em>for all</em> <code>y</code>.</li>
<li><code>∈ D</code> means <em>is an element of</em> <code>D</code>. So <code>Ɐy ∈ D</code> reads <em>for all y that is an element of D</em>.</li>
<li><code>,</code> often reads as juxtaposition.</li>
<li><code>∃x</code> means <em>there exists</em> <code>x</code>.</li>
<li><code>f : C -&gt; D</code> is just the type definition of the <code>f</code> morphism.</li>
<li><code>/</code> is often used instead of <code>,</code> to delimit the equation and the hypothesis; read it as
<code>so that</code>.</li>
</ul>
<p>So this whole math stuff reads:</p>
<blockquote>
<p>For all <code>y</code> in <code>D</code>, there exists <code>x</code> in <code>C</code> so that <code>f(x) = y</code>.</p>
</blockquote>
<p>It’s quite a non-intuitive notation when you don’t know about it but you can see the concepts are
pretty simple to understand.</p>
<p>You might ask, what is the point of knowing that in our case? Here, <code>Foo</code> is a type-level function.
Rust has decided to go with the <em>mainstream</em> way to note that (which is another topic and should be
the topic of another blog post, to be honest), so yeah, it’s a bit uglier than in Haskell, but it’s
a type-level function. You can picture it as <code>Foo : E -&gt; Foo&lt;E&gt;</code>. You can easily see that if you
are writing the body of a function and that you have a bound like <code>T: Foo&lt;E&gt;</code> available, you know
that you have type <code>T</code> that implements <code>Foo&lt;E&gt;</code>, so there’s definitely one type to substitute <code>E</code>
with (otherwise your function cannot be called), but there could be several ones. This means that
traits are surjective.</p>
<p>Another way to see why it’s surjective: if I give you <code>Foo&lt;u32&gt;</code>, you know that there’s at least one
type implementing it, but you don’t know which one, since it’s ambiguous.</p>
<pre><code class="language-rust">struct A;
struct B;

impl Foo&lt;u32&gt; for A {
  // hidden
}

impl Foo&lt;u32&gt; for B {
  // hidden
}
</code></pre>
<p>If I give you <code>Foo&lt;u32&gt;</code> and tell you <em>“It’s in a bound position of a function”</em>, you know that
if the function gets called, there’s at least one type implementing <code>Foo&lt;u32&gt;</code>… But which one is it?
<code>A</code>? <code>B</code>?</p>
<h2>Injection</h2>
<p>Injection is the <em>reversed</em> version of surjection. It tells you that all the elements from
<code>C</code> have a departing arrow (i.e. morphism application) ending in <code>D</code> <strong>and</strong> the elements in <code>D</code>
have zero or one arriving arrow. Another way to say it is that for all pairs of <code>x</code> and <code>y</code>
in <code>C</code>, <code>f(a) = f(b)</code> implies <code>a = b</code>, which means that you cannot have two inputs mapping to the
same result since if two applications of the morphism yield the same result, it means that the
inputs have to be the same.</p>
<pre><code>Ɐ(x, y) ∈ C, f : C -&gt; D / f(x) = f(y) =&gt; x = y
</code></pre>
<ul>
<li><code>=&gt;</code> is the math notation to state <em>implication</em>.</li>
</ul>
<p>Two interesting properties of injections:</p>
<ul>
<li>Some elements from <code>D</code> (which is called the <em>codomain</em> of the morphism while <code>C</code> is its
<em>domain</em>) might not have any arriving arrow.</li>
<li>If you have an arriving arrow in the codomain, then you can easily reverse its direction and
move backwards to the element in the domain because it’s not possible that two elements map to
the same element.</li>
</ul>
<p>The contrapositive is also interesting:</p>
<pre><code>Ɐ(x, y) ∈ C, f : C -&gt; D /  x ≠ y =&gt; f(x) ≠ f(y)
</code></pre>
<p>It’s easy to imagine an injection in Rust. For instance, the function transforming a boolean into
an integer is an injection. Every value in the domain (<code>bool</code>) have an arrow into the codomain
(<code>u8</code> for instance):</p>
<ul>
<li><code>f(false) = 0</code>.</li>
<li><code>f(true) = 1</code>.</li>
</ul>
<p>You can also see <code>0</code> has a single arriving arrow, and if you take it backwards, you end up on
<code>false</code>. Same thing happens for <code>1</code> and <code>true</code>. Finally, you can see that <code>3</code>, <code>64</code> or <code>42</code>, which
are definitely in the codomain, have no arriving arrow. We are dealing with an injection.</p>
<p>What’s interesting with our specific typesystem problem is that if we had injective traits, that
would mean that given <code>Foo&lt;T&gt;</code>, there’s only one type that can implement this trait. You would then
be able to take the bound backwards and recover the type, removing the ambiguity.</p>
<blockquote>
<p>This is not correctly possible in Rust. If you’re interested, Haskell can do it via
<a href="https://ghc.haskell.org/trac/ghc/wiki/InjectiveTypeFamilies">injective type families</a> or
functional dependencies, for instance.</p>
</blockquote>
<h3>A small digression on bijection</h3>
<p>For curiosity only, bijection is just the superposition of both the properties. If you have a
morphism that is both surjective and injective, it is bijective, and any element in the domain
gives you one element in the codomain and any element in the codomain gives you one element in the
domain an element in the codomain must have exactly one arriving arrow.</p>
<h1>Back to our impl block problem</h1>
<pre><code class="language-rust">trait Foo&lt;E&gt; {
  fn foo(&amp;self, x: E);
}

impl&lt;T, E&gt; OrderedList&lt;T&gt; where T: Foo&lt;E&gt; {
  fn invoke_foo(&amp;self, e: E) {
    for t in self {
      t.foo(e);
    }
  }
}
</code></pre>
<p>This is our initial problem. As you can see here, the <code>impl</code> block has two variables: <code>T</code> and <code>E</code>.
<code>T</code> here means that we’re implementing methods <em>for all</em> the <code>T</code>s – i.e. <code>Ɐ</code>. However, we’re not
implementing it for all the <code>E</code>. We would like to state that <em>we need a <code>E</code> to exist</em>, which means
that <em>there exists</em> an <code>E</code> – i.e. <code>∃</code>. The current syntax doesn’t support this.</p>
<p>However, what happens if we do this:</p>
<pre><code class="language-rust">impl&lt;T&gt; OrderedList&lt;T&gt; {
  fn invoke_foo&lt;E&gt;(&amp;self, e: E) where T: Foo&lt;E&gt; {
    for t in self {
      t.foo(e);
    }
  }
}
</code></pre>
<p>Here, you can see that since we can provide <code>E</code> by the caller of the <code>invoke_foo</code> function, we don’t
require an injective bound: we narrow all the possible types to one provided by the caller of the
function.</p>
<blockquote>
<p>You can <a href="https://play.rust-lang.org/?gist=d3c84c52f211736cff956c22ba72a741&amp;version=stable&amp;mode=debug&amp;edition=2015">test it by yourself here</a>.
The <code>Clone</code> trait was added so that we can actually loop with the input argument.</p>
</blockquote>
<h1>Intuition</h1>
<p>All of this made me think about why do we even use <code>impl</code> blocks. I mean, Haskell has been around
for roughly 30 years and we never seen the need for it arise. Rust has a very special way to express
typeclasses (Rust’s traits have an implicit type parameter, <code>Self</code>, which is a sort of limitation
that might get fixed in the future). If we forget about the <em>dot notation</em>, I truly think that I
would drop <code>impl</code> blocks for several reasons:</p>
<ul>
<li>No solution currently exists to the problem this blog entry discusses with <code>impl</code> blocks so far.</li>
<li>Documentation might become very hard to read because it makes every methods dependent on an
<code>impl</code> block in the documentation. This refactoring might be interesting for code readers and
writers but is, to me, truly a nightmare to people reading the documentation – it kind of makes
reading the documentation <em>stateful</em>, which is ridiculous. However, this point might be a very
interesting <code>rustdoc</code> feature request! ;)</li>
<li>On a general note, I’m not really adding any constraint to the <code>impl</code> blocks because those
depend mostly on the functions / methods and I find it utterly stupid to create <code>n</code> blocks for
<code>n</code> functions. So I basically just spawn the most general form of <code>impl</code> block and put the
constraints next to the methods.</li>
<li>I often confuse them with <code>impl</code> trait implementors when reading the code from someone else.</li>
</ul>
<p>What would be really great would be to allow people to create function with a <code>self</code> argument
<em>without <code>impl</code> block</em>. You could still have them around for compatibility purposes and for people
who actually enjoy them, but to me those are a bit useless and boilerplate.</p>
<blockquote>
<p>Again, I’m only talking about <code>impl</code> blocks. <code>impl</code> for trait are actually quite nice since you
can see them and spot them easily (as the <code>instance</code> keyword in Haskell).</p>
</blockquote>
<p>That’s all for today. Thank you for having read me. Keep the vibes, and write RFCs!</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sun, 22 Jul 2018 22:05:00 GMT</pubDate></item><item><title>Neovim plugins stability</title><link>https://strongly-typed-thoughts.net/blog/neovim-plugins-stability</link><description><![CDATA[<p>Have you ever installed a Neovim plugin and realized a few days / weeks later, after updating the
plugin, that something broke? That you now have have annoying error messages at startup? If so, then
this blog article should ring a bell. If not, it will probably open your eyes on a problem that can
bite you at any moment.</p>
<h1>Neovim and plugins</h1>
<p>I have been in that situation where I struggle with plugins breaking every time I update, and not
only in Neovim. The problem can occur pretty much anywhere. The main reason to this is simple:
<em>breaking changes</em> happen, and must happen. Plugins maintainers might decide at some point that
something must be removed, or changed. A configuration option you were using disappears or its
name changes. That happens and it’s part of the how software works.</p>
<p>However, there is a difference between breaking changes in plugins and breaking changes in i.e.
libraries / binaries we write in languages supporting package managers, such as Rust and <code>cargo</code>,
Haskell and <code>cabal</code>, Python and <code>poetry</code>, etc. etc. Actually, there are two main differences,
depending on the perspective you take to look at the problem:</p>
<ul>
<li>From a user perspective, software is not versioned and « they want the last updated thing. »
Obviously, if you are a programmer, you know that it’s likely something is versioned, but most
of the time, when you install something like a video game you like, an editor, a plugin or
anything, do you really ask for a specific version? There are obviously situations where you
need to ensure the versions that you use, but most of the time, <em>people do not care</em>.</li>
<li>From a plugin author perspective, there is no tooling to ensure that you can ship a feature
without breaking your users.</li>
</ul>
<p>About the last point, when I work on a given software project, I specify the dependencies of my
project, and I specify the versions of those dependencies. Using <a href="https://semver.org">SemVer</a>, I’m sure that if the
those dependencies are correctly written, I should be able to release my software (lib or bin)
without any issues on my users. There are exceptions, but those are rare and most of the time
related to human errors when implementing <a href="https://semver.org">SemVer</a> incorrectly.</p>
<p>When you write a plugin, you will most likely write it in a language and ecosystem that doesn’t even
support versioning. Which brings me to the main point of this article: how do you prevent breaking
your users?</p>
<h1>The git channel and the « latest » problem</h1>
<p>Neovim plugins are often implemented in package managers like
<a href="https://github.com/wbthomason/packer.nvim">packer</a> via Git URLs. Most of the time, <code>foo/bar</code> will be installed as the <code>bar</code> plugin, part of
the org / user <code>foo</code> on GitHub. You can also install local plugins but that’s off topic here. When
you provide the description of the plugin to install, you will most of the time provide nothing more.
This will default to install and synchronize the plugin using the <code>master</code> branch (or whatever your
local <code>git</code> says, like <code>main</code>). As a Software Engineer, to me, this is a really bad habit we all
have, and I’ll explain why.</p>
<p>When you depend on the <em>latest</em> version of something, you are basically signing a contract between
your local system and the remote system to keep your local version in sync with remote. If the
remote gets updated, you get the changes the next time you update. The problem with this is that
<code>master</code> contains zero information about whether an update is a breaking change or not. It’s the
same reason why defaulting API calls to your API service to the latest version of the API is <em>wrong</em>
and that you should default them to <code>v1</code>: because it will never change, so people have to <em>opt-in</em>
for breaking changes instead, which is a much saner way of dealing with breaking changes.</p>
<p>So I’ve been thinking about this problem for quite a while now. In ecosystem like Rust, crates have
to implement <a href="https://semver.org">SemVer</a>, so that <code>cargo</code>, the package manager and build system, knows how to resolve
the dependency graph given your version requirements. But we don’t have that with Neovim plugins… or
do we?</p>
<h1>The solution is (partially) already there</h1>
<p>In <a href="https://github.com/wbthomason/packer.nvim">packer</a> (and probably others), we can provide additional information to download (and update)
plugins. For instance, <a href="https://github.com/wbthomason/packer.nvim">packer</a> supports the <code>branch = …</code>, <code>tag = …</code> and <code>commit = …</code> configuration
options. So this gave me an idea. And this idea is something I would like to turn into a proposal
for the Neovim community. The idea is to use both branches and tags to encode <a href="https://semver.org">SemVer</a> information
and allow users to specify plugins a bit like you would specify dependencies in tools like <code>cargo</code>.
In order to do that, plugin authors must accept and implement <a href="https://semver.org">SemVer</a>. The current way I see things
is actually pretty simple:</p>
<ul>
<li><code>master</code>, <code>main</code>, etc. can be used as a <em>nightly</em> channel. Users who don’t want to deal with this
<a href="https://semver.org">SemVer</a> idea can just ignore the whole thing and still use their plugins the way they’ve always
had.</li>
<li>A triplet git tag, <code>vM.m.p</code>, represents a full, immutable version that cannot be changed. This
would allow a user to depend on <code>vM.m.p</code> and be sure that the plugin would be pinned to that
version. Here, <code>M</code> is the major version, <code>m</code> is the minor version and <code>p</code> is the patch version.
Every time the plugin is modified in a way that doesn’t show on the interface (no breaking change
and no non-breaking change, only internal patches), the <code>p</code> version is incremented.</li>
<li>A pair git branch <code>vM.m</code>, with <code>M</code> the major and <code>m</code> the minor version. The idea is that every
time a plugin gets a new non-breaking change, the <code>m</code> version is incremented. That would allow
users to depend on <code>vM.m</code> to be sticky to that minor version, but still receive patches updates.
More on that below.</li>
<li>A single version git branch, <code>vM</code>, with <code>M</code> the major version. Every time the plugin gets a new
breaking change, <code>M</code> is incremented. This would probably be the most interesting options for
users, as it would allow them to benefit from new features without having their plugin break on an
update.</li>
</ul>
<p>In order to implement this scheme using git branches and git tags, there is one trick to implement.
One a plugin author decide to make a new release, they have to do several things:</p>
<ul>
<li>Create a git tag. If the previous version was <code>v1.2.3</code>, they will either create <code>v1.2.4</code>, <code>v1.3.0</code>
or <code>v2.0.0</code>, depending on the kind of change.</li>
<li>If they have created a new patch version:
<ul>
<li>They need to create the tag <code>v1.2.4</code>.</li>
<li>They need to update the branch <code>v1.2</code> so that it now points to the same commit as <code>v1.2.4</code>.</li>
<li>They need to update the branch <code>v1</code> so that it now points to the same commit as <code>v1.2.4</code>.</li>
</ul>
</li>
<li>If they have created a new minor version:
<ul>
<li>They need to create the tag <code>v1.3.0</code>.</li>
<li>They need to create the branch <code>v1.3</code> and make it point to the same commit as <code>v1.3.0</code>.</li>
<li>They need to update the branch <code>v1</code> so that it now points to the same commit as <code>v1.3.0</code>.</li>
</ul>
</li>
<li>IF they have created a new major version:
<ul>
<li>They need to create the tag <code>v2.0.0</code>.</li>
<li>They need to create the branch <code>v2.0</code> and make it point to the same commit as <code>v2.0.0</code>.</li>
<li>They need to create the branch <code>v2</code> and make it point to the same commit as <code>v2.0.0</code>.</li>
</ul>
</li>
</ul>
<p>With this approach, a user can now depend on a version and be sure not to get breaking-changes. An
example for my config for <a href="https://github.com/phaazon/hop.nvim">hop.nvim</a>:</p>
<pre><code class="language-lua">use {
  'phaazon/hop.nvim',
  branch = "v1",
  config = function()
    require'hop'.setup {
      keys = 'etovxqpdygéèfblzhckisuran',
    }
  end
}
</code></pre>
<h1>Tooling</h1>
<p>I’m currently writing a (small) plugin, branching on package managers such as <a href="https://github.com/wbthomason/packer.nvim">packer</a>, to be able,
from a user perspective, to get a listing of packages that can be upgraded. For instance, if you
depend on <code>telescope.nvim-0.4</code>, if <code>telescope.nvim-0.4.9</code> is released, <a href="https://github.com/wbthomason/packer.nvim">packer</a> should pick it up
because you would have <code>branch = v0.4</code> in your config (and that branch would be updated to point to
git tag <code>v0.4.9</code>). However, if <code>telescope-nvim-0.5</code> is released, that tool will provide you with a
hint that a new version is available and that you need to manually select it, because it might break
your configuration.</p>
<h1>Pros. &amp; cons.</h1>
<p>The obvious advantage (and incentive) here is that if plugin authors accept to implement something
like this, plugin stability will be an old bitter memory. People who don’t care and want to live on
the bleeding edge can completely ignore this proposal and still use the <code>master</code> / <code>main</code> branch
(not providing the branch unfortunately often defaults to the <code>master</code> branch). The other incentive
here is that plugins (like the one I’m writing for the tooling) can now do more things with git tags
and branches to display more information, like the number of releases, git tag annotations to show
proper release notes inside Neovim, etc.</p>
<p>The drawbacks are not negligible: implementing such a <a href="https://semver.org">SemVer</a> proposal using git tags and branches
is going to require a bit more work, and the tooling is clearly lacking. This is very similar to how
dynamic relocatable objects (<code>.so</code>) are handled on most Linux systems: the actual <code>.so</code> file is
versioned somewhere on your file system, like <code>/usr/lib/foo.1.2.3.so</code>, and applications / other
libraries can depend on major and minor versions by using <em>symlinks</em>: <code>/usr/lib/foo.1.so</code> points to
<code>/usr/lib/foo.1.2.so</code>, which in turns points to <code>/usr/lib/foo1.2.3.so</code>. <code>git</code> doesn’t support sticky
branches (i.e. you can’t ask to make a branch reference another one, it has to reference a commit,
which is always immutable), so it means that updating a version requires you to update the whole
hierarchy of branches, which is a bit annoying. A tool (like a shell tool in the plugin I’m writing)
could probably help with that.</p>
<p>So what do you think? Worth it? I think that in terms of stability, it is a missing piece of pretty
much any editor supporting dynamic plugins. The « ideal » situation would be to support semantic
versioning directly at the package level (which is not <a href="https://github.com/wbthomason/packer.nvim">packer</a>, but directly how Neovim represents
package), and the actual encoding of the package versions would probably be hard to implement using
only a centralized system like GitHub as there is no index (besides git tags and branches).</p>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Wed, 03 Nov 2021 00:24:00 GMT</pubDate></item><item><title>A Rust shading language EDSL</title><link>https://strongly-typed-thoughts.net/blog/shades-edsl</link><description><![CDATA[<p>This blog article is about a crate I have been working on for a while now: <a href="https://crates.io/crates/shades">shades</a>. That library is a <em>shading
library</em>, that is, it provides you with types, traits and functions to create <a href="https://en.wikipedia.org/wiki/Shader">shaders</a>. A shader is just a piece of
<em>code</em> that typically runs on your GPU, and is written in a language such as <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">GLSL</a> — or anything that can target
<a href="https://www.khronos.org/spir">SPIR-V</a> these days.</p>
<h1>Context &amp; problem</h1>
<p>The way it typically works is by writing the GLSL code in a hard-coded string, or a file that is dynamically loaded at
runtime by your application (some people even pre-compile GLSL into a binary form, but it doesn’t change the fact that
the binary has to be brought to the GPU at runtime). The overall implication of all this is that the shader is
<em>represented</em> via an opaque <code>String</code> (or binary like <code>Vec&lt;u8&gt;</code>) in your application.</p>
<p>A couple of examples of interfaces to handle shaders, using <a href="https://crates.io/crates/wgpu">wgpu</a> and a crate of mine, <a href="https://crates.io/crates/luminance">luminance</a>:</p>
<ul>
<li><a href="https://docs.rs/wgpu/0.12.0/wgpu/enum.ShaderSource.html"><code>wgpu::ShaderSource</code></a></li>
<li><a href="https://docs.rs/luminance/0.47.0/luminance/shader/struct.ProgramBuilder.html#method.from_strings"><code>luminance::shader::ProgramBuilder::from_strings</code></a></li>
</ul>
<p>As you can see, dealing with shader sources implies a string interface, because that’s basically code that needs to be
compiled by your GPU driver (done at runtime). That has several issues:</p>
<ul>
<li>There is no way to <em>inspect</em> the content of the shader to use its inputs, outputs, environment variables, etc. That
leads to a lot of code duplication, which is just <code>String</code> duplication all over the place.</li>
<li>Your application might compile and run, you are not sure that your shaders are correctly written, so you have to write
a runtime checker for your shader, that needs to go through all the shaders in use in your application. Tedious, and
error-prone.</li>
<li>You have to learn a shading language. Most of the time, <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">GLSL</a> should be enough, but <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">GLSL</a> is old, and not very fun
to work with.</li>
<li>You will have to write the same code over and over and over. For instance, in <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">GLSL</a>, there is no definition of π,
so you will have to write its definition whenever you need it. That will quickly lead you to write some combinator
code to inject sub strings containing such definitions and it will be a nightmare.</li>
<li>It’s very weakly typed. A <code>String</code> doesn’t allow you to be sure that shader is compatible with the graphics pipeline.
Being the author of <a href="https://crates.io/crates/luminance">luminance</a>, that is a massive flaw to me. As you might know, the typing in <a href="https://crates.io/crates/luminance">luminance</a> is pretty
advanced (phantom typing, type families, type states, etc.), so being stuck with <code>String</code> is not a good situation.</li>
</ul>
<h1>Introducing <code>shades</code></h1>
<p>I introduced <a href="https://crates.io/crates/shades">shades</a> a couple of years ago without going public about it. The reason for that was that it was not
completely ready for people to use. Of course you could have given a try (and fundamentally, it hasn’t changed much),
but the syntax was going to drive people off right away. The idea of <a href="https://crates.io/crates/shades">shades</a> is to write shaders directly in Rust.
Instead of using <code>String</code> to represent a shader stage, you use <code>Stage&lt;Inputs, Outputs, Environment&gt;</code>. That gives you a
first visible advantage: typing. In <a href="https://crates.io/crates/shades">shades</a>, you don’t have to declare your inputs, outputs nor environment. For
instance, compare the following GLSL code with its <a href="https://crates.io/crates/shades">shades</a> counterpart:</p>
<pre><code class="language-glsl">uniform float time;
uniform vec2 resolution;
</code></pre>
<p>Whenever a shader stage has to use <code>time</code> or <code>resolution</code>, they will have to declare those two lines at the top of their
sources. With <a href="https://crates.io/crates/shades">shades</a>:</p>
<pre><code class="language-rust">#[derive(Debug)]
struct Env {
  time: f32,
  resolution: V2&lt;f32&gt;,
}

#[derive(Debug)]
struct EnvExpr {
  time: Expr&lt;f32&gt;,
  resolution: Expr&lt;V2&lt;f32&gt;&gt;,
}

impl Environment for Env {
  type Env = EnvExpr;

  fn env() -&gt; Self::Env {
    EnvExpr {
      time: Expr::new_env(0),
      resolution: Expr::new_env(1),
    }
  }

  fn env_set() -&gt; Vec&lt;(u16, Type)&gt; {
    vec![
      (0, &lt;f32 as ToType&gt;::to_type()),
      (1, &lt;V2&lt;f32&gt; as ToType&gt;::to_type()),
    ]
  }
}
</code></pre>
<p>When using a <code>Stage&lt;_, _, Env&gt;</code>, <code>time</code> and <code>resolution</code> will automatically be available, whenever you use them or not.
This is even more interesting with procedural macro support in <a href="https://crates.io/crates/luminance">luminance</a> for instance:</p>
<pre><code class="language-rust">// the Environment and Semantics derive macro will generate all the above code for you, as well as the type family
#[derive(Debug, Environment, Semantics)]
struct Env {
  time: f32,
  resolution: V2&lt;f32&gt;,
}
</code></pre>
<p>So yes, <a href="https://crates.io/crates/shades">shades</a> is a bit more verbose at the Rust level, but I think it’s worth it because shaders are checked at
compile-time (via <code>rustc</code>) and not at runtime anymore. Typing also allows a better composition and reusability.</p>
<p>Just to give you an idea, this is an old and outdated example taken from the repository to show you what it used to be:</p>
<pre><code class="language-rust">let vertex_shader = StageBuilder::new_vertex_shader(
  |mut shader: StageBuilder&lt;MyVertex, (), ()&gt;, input, output| {
    let increment = shader.fun(|_: &amp;mut Scope&lt;Expr&lt;f32&gt;&gt;, a: Expr&lt;f32&gt;| a + lit!(1.));

    shader.fun(|_: &amp;mut Scope&lt;()&gt;, _: Expr&lt;[[V2&lt;f32&gt;; 2]; 15]&gt;| ());

    shader.main_fun(|s: &amp;mut Scope&lt;()&gt;| {
      let x = s.var(1.);
      let _ = s.var([1, 2]);
      s.set(output.clip_distance.at(0), increment(x.clone()));
      s.set(
        &amp;output.position,
        vec4!(input.pos, 1.) * lit![0., 0.1, 1., -1.],
      );

      s.loop_while(true, |s| {
        s.when(x.clone().eq(1.), |s| {
          s.loop_break();
          s.abort();
        });
      });
    })
  },
);

let output = shades::writer::glsl::write_shader_to_str(&amp;vertex_shader).unwrap();
</code></pre>
<p>That snippet doesn’t do anything really logical; it’s just there to show you how things look like.</p>
<p>I wrote a couple of things with <a href="https://crates.io/crates/shades">shades</a>, and even though I like it, I have to admit that the Rust API is pretty hard to
read, use and understand. I needed something better.</p>
<h1>The new age of <code>shades</code>: <code>shades-edsl</code></h1>
<p>Then it struck me: why would <em>that</em> API need to be the one people use? It can remain public, but maybe we can build
something <em>above</em> it to make it much easier to use. The initial idea of that API was to be an <a href="https://en.wikipedia.org/wiki/Domain-specific_language">EDSL</a>, but Rust has a lot
of limitations (for instance, <code>Eq</code> methods, <code>Ord</code> methods, etc. return <code>bool</code>, while I would need them to return
<code>Expr&lt;bool&gt;</code>). So… I came to the realization that the right way to do this would be to write a <em>real</em> EDSL: <a href="https://crates.io/crates/shades-edsl">shades-edsl</a>.</p>
<p>The idea of <a href="https://crates.io/crates/shades-edsl">shades-edsl</a> is super simple: remove everything that would make <a href="https://crates.io/crates/shades">shades</a> usable directly by a human, and
build a procedural macro that transforms regular Rust code into the API from <a href="https://crates.io/crates/shades">shades</a>. Basically, generate the <a href="https://crates.io/crates/shades">shades</a>
code inside the code of a procedural macro, which is what humans will use.</p>
<p>The way it works is pretty straight-forward to understand: you write your shading code in a <code>shades! { … }</code> block.
Everything in that block is reinterpreted and replaced by <a href="https://crates.io/crates/shades">shades</a> symbols: builders, method calls, etc. etc.</p>
<p>For instance, the following:</p>
<pre><code class="language-rust">let stage = shades! { VS |_input, _output, _env| {
  fn add(a: i32, b: i32) -&gt; i32 {
    a + b
  }

  fn main() {
    // do nothing
  }
}};
</code></pre>
<p>Will be reinterpreted as:</p>
<pre><code class="language-rust">let stage = shades::stage::StageBuilder::&lt;_, _, _&gt;::new_vertex_shader(…);
</code></pre>
<p>Where <code>…</code> is generated code (a lambda here). A couple of more example:</p>
<pre><code class="language-rust">  // in a shades! block
  if cond {
    body_if
  } else {
    body_else
  }
</code></pre>
<p>is transformed into:</p>
<pre><code class="language-rust">  __scope.when(cond, |__scope| {
    #body_if // body_if is also transformed
  }).or_else(|__scope| {
    #body_else
  });
</code></pre>
<h1>Feature list</h1>
<ul>
<li>Constant declarations: the regular Rust <code>const</code> declarations work and declare constant items in the shading code.</li>
<li>Function declarations: two kind of declarations are supported:
<ul>
<li>Any kind of functions declared with <code>fn</code> will create function declarations and return a function handle (so that you
can call the function).</li>
<li>A function called <code>main</code> will not return a function handle, but instead will inject the function declaration and
will return the <code>Stage</code>. It is the only way to produce a <code>Stage</code> so you have the certainty that a <code>Stage</code> contains
the <code>main</code> function.</li>
</ul>
</li>
</ul>
<h1>How it works</h1>
<p><a href="https://crates.io/crates/shades-edsl">shades-edsl</a> is implemented as a procedural macros, <code>shades!</code>, which defines a <em>shader stage</em>. It’s basically a
monadic computation (using <code>shades::stage::StageBuilder</code> behind the scene). Once you write your <code>fn main()</code> function,
the builder simply generates the <code>Stage</code> object for you (using <code>StageBuilder::main_fun</code>). In terms of architecture, it’s
a pretty simple one:</p>
<ol>
<li>I use the <a href="https://crates.io/crates/syn">syn</a> crate to parse the content of the <code>shades!</code> invocation (<code>TokenStream</code>).</li>
<li>I then <em>mutate</em> the generated AST to optimize and inject some logic in your code.</li>
<li>I marshal the AST back to Rust by generating the calls to the <a href="https://crates.io/crates/shades">shades</a> API, and write the Rust code back to the
<code>TokenStream</code></li>
</ol>
<h2>Parsing the Rust code</h2>
<p>Parsing the Rust code uses <a href="https://crates.io/crates/syn">syn</a>, a famous parsing crate used a lot by procedural macros. It was made to parse Rust-like
code, but it has some interesting use cases. I’m used to writing procedural macros in <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> for instance
(<code>#[derive()]</code> annotations).</p>
<p>The idea is to take a Rust item and represent it as a data type. The important thing to know with <a href="https://crates.io/crates/syn">syn</a> is that it
supports nice error message reporting, so you are advised to store non-meaningful information, like braces, parens,
tokens, etc.</p>
<p>For instance:</p>
<pre><code class="language-rust">let x: Type = 3;
</code></pre>
<p>We can represent such an item (a <code>let</code> declaration) with the following <code>struct</code>:</p>
<pre><code class="language-rust">#[derive(Debug)]
pub struct VarDeclItem {
  let_token: syn::Token![let],
  name: syn::Ident,
  ty: Option&lt;(syn::Token![:], syn::Type)&gt;,
  assign_token: syn::Token![=],
  expr: syn::Expr,
  semi_token: syn::Token![;],
}
</code></pre>
<blockquote>
<p><code>syn::Token!</code> is a type macro that resolves to the right type name for the given symbol.</p>
</blockquote>
<p>Parsing in <a href="https://crates.io/crates/syn">syn</a> uses the <code>Parse</code> trait, and is more intimidating than hard to use. This is the whole parser for
<code>VarDeclItem</code>:</p>
<pre><code class="language-rust">impl Parse for VarDeclItem {
  fn parse(input: syn::parse::ParseStream) -&gt; Result&lt;Self, syn::Error&gt; {
    let let_token = input.parse()?;
    let name = input.parse()?;

    let ty = if input.peek(Token![:]) {
      let colon_token = input.parse()?;
      let ty = input.parse()?;
      Some((colon_token, ty))
    } else {
      None
    };
    let assign_token = input.parse()?;
    let expr = input.parse()?;
    let semi_token = input.parse()?;

    Ok(Self {
      let_token,
      name,
      ty,
      assign_token,
      expr,
      semi_token,
    })
  }
}
</code></pre>
<p><code>ParseStream</code> is the current state of the input stream. As you can see with the type of <code>parse()</code>, it will return the
<code>Self</code> type or an error, so we can call <code>input.parse()?</code> inside the implementation if type inference knows what the type
is, and that the type implements <code>Parse</code>. In our case, <code>syn::Token![let]</code> and <code>syn::Ident</code> do implement <code>Parse</code>, so we
can parse them in two simple lines:</p>
<pre><code class="language-rust">    let let_token = input.parse()?;
    let name = input.parse()?;
</code></pre>
<p>Then, we need to look ahead to check whether there is a semicolon (<code>:</code>). If there is, then we can parse <code>syn::Token![:]</code>
and <code>syn::Type</code>, as both already implement <code>Parse</code>. Otherwise, we simply assume <code>None</code> as type ascription:</p>
<pre><code class="language-rust">    let ty = if input.peek(Token![:]) {
      let colon_token = input.parse()?;
      let ty = input.parse()?;
      Some((colon_token, ty))
    } else {
      None
    };
</code></pre>
<p>The rest is as simple as everything implements <code>Parse</code> as well:</p>
<pre><code class="language-rust">    let assign_token = input.parse()?;
    let expr = input.parse()?;
    let semi_token = input.parse()?;
</code></pre>
<p>We have everything we need to return the parsed declaration:</p>
<pre><code class="language-rust">    Ok(Self {
      let_token,
      name,
      ty,
      assign_token,
      expr,
      semi_token,
    })
</code></pre>
<p>Every Rust items are parsed like that. There are a couple of harder things. For instance, input can be split by parsing
matched delimiters, resulting in two disjoint inputs, or some items will have to be delimited with punctuation (a
token).</p>
<p>An example of disjoint input parsing while parsing <code>if</code> statements:</p>
<pre><code class="language-rust">#[derive(Debug)]
pub struct IfItem {
  if_token: Token![if],
  paren_token: Paren,
  cond_expr: Expr,
  brace_token: Brace,
  body: ScopeInstrItems,
  else_item: Option&lt;ElseItem&gt;,
}

// …

impl Parse for IfItem {
  fn parse(input: syn::parse::ParseStream) -&gt; Result&lt;Self, syn::Error&gt; {
    let if_token = input.parse()?;

    let cond_input;
    let paren_token = parenthesized!(cond_input in input);
    let cond_expr = cond_input.parse()?;

    let body_input;
    let brace_token = braced!(body_input in input);
    let body = body_input.parse()?;

    let else_item = if input.peek(Token![else]) {
      Some(input.parse()?)
    } else {
      None
    };

    Ok(Self {
      if_token,
      paren_token,
      cond_expr,
      brace_token,
      body,
      else_item,
    })
  }
}
</code></pre>
<p>Here, we use the <code>parenthesized!</code> macro that parses a matching pair of <code>()</code>. Once it has finished, the <code>input</code> has
advanced after the closing delimiter, but the second input (<code>cond_input</code> in our case) is scoped right after the opening
delimiter and right before the closing one. That allows us to parse “inside” the parenthesis, which is a weird interface
at first, but once you get used to it, it’s actually pretty smart and pretty fast. Notice how we parse the conditional
expression using <code>cond_input</code> and not <code>input</code> here:</p>
<pre><code class="language-rust">    let cond_input;
    let paren_token = parenthesized!(cond_input in input);
    let cond_expr = cond_input.parse()?;
</code></pre>
<p>Same thing with <code>{}</code> and the <code>braced!</code> macro:</p>
<pre><code class="language-rust">    let body_input;
    let brace_token = braced!(body_input in input);
    let body = body_input.parse()?;
</code></pre>
<p>An even more interesting example: parsing function definitions:</p>
<pre><code class="language-rust">#[derive(Debug)]
pub struct FunDefItem {
  fn_token: Token![fn],
  name: Ident,
  paren_token: Paren,
  args: Punctuated&lt;FnArgItem, Token![,]&gt;,
  ret_ty: Option&lt;(Token![-&gt;], Type)&gt;,
  brace_token: Brace,
  body: ScopeInstrItems,
}
</code></pre>
<p>Here, <code>args</code> is a punctuated sequence of <code>FnArgItem</code>, delimited with <code>,</code>. The parser is as follows:</p>
<pre><code class="language-rust">impl Parse for FunDefItem {
  fn parse(input: syn::parse::ParseStream) -&gt; Result&lt;Self, syn::Error&gt; {
    let fn_token = input.parse()?;
    let name = input.parse()?;

    let args_input;
    let paren_token = parenthesized!(args_input in input);
    let args = Punctuated::parse_terminated(&amp;args_input)?;

    let ret_ty = if input.peek(Token![-&gt;]) {
      let arrow_token = input.parse()?;
      let ret_ty = input.parse()?;
      Some((arrow_token, ret_ty))
    } else {
      None
    };

    let body_input;
    let brace_token = braced!(body_input in input);
    let body = body_input.parse()?;

    Ok(Self {
      fn_token,
      name,
      paren_token,
      args,
      ret_ty,
      brace_token,
      body,
    })
  }
}
</code></pre>
<p>The argument part uses <code>parenthesized!</code> to parse the all arguments definitions, and then, with a single argument parser
(identifier plus type: <code>ident: Type</code>), we can simply ask to parse many by using comma punctuation. This is done with
<code>parse_terminated</code>:</p>
<pre><code class="language-rust">    let args_input;
    let paren_token = parenthesized!(args_input in input);
    let args = Punctuated::parse_terminated(&amp;args_input)?;
</code></pre>
<p>Parsing with <a href="https://crates.io/crates/syn">syn</a> is a bit weird at first (I’m used to <a href="https://crates.io/crates/nom">nom</a> because of <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">glsl</a>), but in the end, I think it’s a pretty
funny and interesting interface. There is no combinator such as alternative parser selection, but that’s probably for
the best, because the <em>peek</em> / <em>look ahead</em> interface forces you to write fast parsers. I really like it.</p>
<h2>Mutating the AST</h2>
<p>Mutating the AST is a top-down operation that consists in transforming types and expressions. The goal is to <em>lift</em>
items up the shades EDSL. A function such as:</p>
<pre><code class="language-rust">fn add(a: i32, b: i32) -&gt; i32
</code></pre>
<p>Needs to be <em>representable</em> in the <code>shades</code> AST. For this reason, <code>a</code>, <code>b</code> and the return type have to change types.
Changing the type implies going from <code>T</code> to <code>Expr&lt;T&gt;</code>. That operation is done in a <code>mutate(&amp;mut self)</code> method on each
syntax items parsed from the previous stage, and each of <code>mutate</code> invocation might call <code>mutate</code> on items they contain
(such as punctuated items, collections of instructions / statements, etc.).</p>
<p>The only interesting part to discuss here is probably how we can lift values in complex expressions such as
<code>a + (b * c)</code>. This is done with the concept of mutable visitors, defined in <a href="https://crates.io/crates/syn">syn</a> with the <code>visit-mut</code> feature gate.
A mutable visitor is simply an object that implements a trait (<code>VisitMut</code>), which defines one method for each type of
items in the AST that might exist. For instance, there is <code>visit_expr_mut(&amp;mut self, e: &amp;mut syn::Expr)</code> and a
<code>visit_type_mut(&amp;mut self, e: &amp;mut syn::Type)</code>. All <code>visit_*</code> methods have a default implementation doing nothing, so
that you are left with implementing only the ones that matter for you.</p>
<p>An example of what I do with types:</p>
<pre><code class="language-rust">struct ExprVisitor;

impl VisitMut for ExprVisitor {
  fn visit_type_mut(&amp;mut self, ty: &amp;mut Type) {
    *ty = parse_quote! { shades::expr::Expr&lt;#ty&gt; };
  }
}
</code></pre>
<blockquote>
<p><code>parse_quote!</code> is a <a href="https://crates.io/crates/syn">syn</a> macro that is akin to <code>quote!</code> from <a href="https://crates.io/crates/quote">quote</a>, but can infer the return type to parse to the
right type. Here, the returned type is inferred to be <code>syn::Type</code>.</p>
</blockquote>
<p>An interesting thing with <a href="https://crates.io/crates/shades-edsl">shades-edsl</a>: because users will provide their inputs, outputs and environment types in the
types of <code>Stage</code>, we do not want to wrap those in <code>Expr</code>, because they already contain <code>Expr</code> values, as seen earlier.
For this reason, the syntax of types of arguments was augmented with the possibility to add a dash (<code>#</code>) in behind the
types. If such a symbol is found, the type is assumed <em>unlifted</em> and shouldn’t be lift. For instance:</p>
<pre><code class="language-rust">let a: i32 = …; // will become Expr&lt;i32&gt;
let b: #Foo = …; // will remain Foo
</code></pre>
<h2>Generating the <code>shades</code> code</h2>
<p>Generating the <a href="https://crates.io/crates/shades">shades</a> AST is just a matter of traversing the parsed and mutated AST and using the <a href="https://crates.io/crates/quote">quote</a> crate and
its <code>quote!</code> macro to write the Rust code we want. The only missing part is how to bootstrap a <code>StageBuilder</code>, which is
done with some more parsing. Invocation looks like this:</p>
<pre><code class="language-rust">  let stage = shades! { vertex |input: TestInput, output: TestOutput, env: TestEnv| {
</code></pre>
<p>The first keyword is <code>vertex</code> here, but it could be <code>fragment</code>, <code>geometry</code> or any kind of shader stage (that needs to be
documented). Then, you have a lambda. I chose that syntax because a shader stage can be imagined as a function of its
input, output and environment. I hesitated with returning the output, but it would make things harder for some stages
(i.e. geometry shaders), where they can write to the same output several times. In that lambda, every type is
automatically marked as unlifted (so <code>input: TestInput</code> and <code>input: #TestInput</code> are the same).</p>
<p>Once transformed, this single line is akin to something like this:</p>
<pre><code class="language-rust">shades::stage::StageBuilder::new_vertex_shader(|mut __builder: shades::stage::StageBuilder::&lt;#user_in_ty, #user_out_ty, #user_env_ty&gt;, #input, #output| {
</code></pre>
<p>Everything else works the same way. Some fun example:</p>
<p>The generated code when the <code>main</code> function is found:</p>
<pre><code class="language-rust">    if name.to_string() == "main" {
      let q = quote! {
        // scope everything to prevent leaking them out
        {
          // function scope
          let mut __scope: shades::scope::Scope&lt;()&gt; = shades::scope::Scope::new(0);

          // roll down the statements
          #body

          // create the function definition
          use shades::erased::Erased as _;
          let erased = shades::fun::ErasedFun::new(Vec::new(), None, __scope.to_erased());
          let fundef = shades::fun::FunDef::&lt;(), ()&gt;::new(erased);
          __builder.main_fun(fundef)
        }
      };

      q.to_tokens(tokens);
      return;
    }
</code></pre>
<p>If items:</p>
<pre><code class="language-rust">impl ToTokens for IfItem {
  fn to_tokens(&amp;self, tokens: &amp;mut proc_macro2::TokenStream) {
    let cond = &amp;self.cond_expr;
    let body = &amp;self.body;

    let q = quote! {
    __scope.when(#cond, |__scope| {
      #body
    }) };

    q.to_tokens(tokens);

    match self.else_item {
      Some(ref else_item) =&gt; {
        let else_tail = &amp;else_item.else_tail;
        quote! { #else_tail }.to_tokens(tokens);
      }

      None =&gt; {
        quote! { ; }.to_tokens(tokens);
      }
    }
  }
</code></pre>
<p>Else tail items, which allows to add an <code>if</code> again (<code>else if</code>) or stop to <code>else</code> only. You will notice in <code>ToTokens</code>
implementation of <code>IfItem</code> that there is no <code>;</code> added at the end of the <code>.when()</code> call unless there is no <code>else</code>
following, and that rule is kept enforced in <code>ToTokens for ElseTailItem</code> as well. See, static analysis at compile-time!:</p>
<pre><code class="language-rust">impl ToTokens for ElseTailItem {
  fn to_tokens(&amp;self, tokens: &amp;mut proc_macro2::TokenStream) {
    match self {
      ElseTailItem::If(if_item) =&gt; {
        if_item.to_tokens_as_part_of_else(tokens);
      }

      ElseTailItem::Else { body, .. } =&gt; {
        let q = quote! {
          .or(|__scope| { #body });
        };

        q.to_tokens(tokens);
      }
    }
  }
}
</code></pre>
<h1>Writers</h1>
<p>Once you get a <code>Stage</code>, you can use a <em>writer</em> to output a <code>String</code>, so that <a href="https://crates.io/crates/shades">shades</a> is compatible with any kind of
library / framework / engine that expects a string-based shader source.</p>
<p>For example, for GLSL:</p>
<pre><code class="language-rust">  let output = shades::writer::glsl::write_shader_to_str(&amp;vertex_shader).unwrap();
</code></pre>
<h1>Interaction with <code>luminance</code></h1>
<p>Eh, I wrote both crates, so obviously <a href="https://crates.io/crates/luminance">luminance</a> has / will have a special support of <a href="https://crates.io/crates/shades">shades</a>. Remember the definition
of <code>Environment</code> above? It’s similar to <code>Inputs</code> and <code>Outputs</code>, too. <a href="https://crates.io/crates/luminance-derive">luminance-derive</a> already provides some derive
macros to automatically implement some traits, and it will provide a support to implement <code>Inputs</code>, <code>Outputs</code> and
<code>Environment</code> as well, without having to think about the indexes.</p>
<h1>Caveats and alternative work</h1>
<p>Of course, if something is not implemented correctly (a bug in <a href="https://crates.io/crates/shades-edsl">shades-edsl</a>, a bug in <a href="https://crates.io/crates/shades">shades</a>), debugging the output
shaders will be harder, because everything is generated (names included) on the target side. It’s something to keep in
mind.</p>
<p>You should also have a look at some similar yet different approaches:</p>
<ul>
<li><a href="https://github.com/EmbarkStudios/rust-gpu">rust-gpu</a> from EmbarkStudios. It’s a similar project but instead of
parsing Rust code, they wrote a compile chain (that you need to install via <code>rustup</code>) and compile your code with. It’s
an interesting point but I wanted something more typed with a more seamless integration to the rest of my ecosystem
(i.e. <a href="https://crates.io/crates/luminance">luminance</a>).</li>
<li><a href="https://www.w3.org/TR/WGSL">wgsl</a>, a new shading language for the WebGPU.</li>
<li><a href="https://crates.io/crates/glsl-quasiquote">glsl-quasiquote</a>: you can write GLSL code in it and get it parsed at compile-time. However, it will use the <a href="https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language">glsl</a>
crate, which is currently a parser only, and it will not do any kind of semantic analysis. Another crate has to be
written for that, and it doesn’t exist to my knowledge (and I won’t write it because it’s a lot of work and I don’t
think it’s the right approach, unless you want to write a GLSL compiler, which I don’t).</li>
</ul>
<p>Keep the vibes!</p>
<blockquote>
<p><a href="https://www.reddit.com/r/rust_gamedev/comments/wbxpp4/a_shading_language_edsl/">Reddit discussion</a></p>
</blockquote>
]]></description><author>Dimitri Sabadie &lt;dimitri.sabadie@gmail.com&gt;</author><pubDate>Sat, 30 Jul 2022 13:30:00 GMT</pubDate></item></channel></rss>