<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title></title>
    <link rel="self" type="application/atom+xml" href="https://glocq.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://glocq.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-08-27T00:00:00+00:00</updated>
    <id>https://glocq.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Animation in Bevy: The Big Picture</title>
        <published>2026-08-27T00:00:00+00:00</published>
        <updated>2026-08-27T00:00:00+00:00</updated>
        
        <author>
          <name>Grégoire Locqueville</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://glocq.com/en/blog/20260827/"/>
        <id>https://glocq.com/en/blog/20260827/</id>
        
        <content type="html" xml:base="https://glocq.com/en/blog/20260827/">&lt;p&gt;&lt;em&gt;August 27, 2026&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So you just downloaded an animated 3D character &lt;code&gt;.glb&lt;/code&gt; file on a free assets website. You have a basic Bevy application running. Now you want to spawn your character in your Bevy app. Easy enough, there&#39;s probably a function for that. You look up the basic &lt;a rel=&quot;external&quot; href=&quot;https://bevy.org/examples/animation/animated-mesh/&quot;&gt;Animated Mesh example&lt;/a&gt; on Bevy&#39;s website... and it quickly becomes apparent that things are not as simple as you&#39;ve hoped. There&#39;s a lot of setting things up, animation-related types whose exact purpose is not obvious... You might be able to tweak the examples to fit your needs reasonably fast, but actually forming a mental model of how animation works in Bevy, based on the examples, is going to take some serious pondering.&lt;/p&gt;
&lt;p&gt;This post is what I wish I had when trying to understand animation in Bevy a couple weeks ago. I start by building up, step by step, enough of a mental model to deal with basic animation in Bevy. Then I walk you through the official example, explaining how it relates to that mental model, and also holding your hand a bit when encountering methods you might not have encountered yet.&lt;/p&gt;
&lt;p&gt;If you are comfortable with the basics of Bevy’s ECS, you’re in this post’s target audience :)&lt;/p&gt;
&lt;h2 id=&quot;first-intuition&quot;&gt;First Intuition&lt;/h2&gt;
&lt;p&gt;Let’s forget about Bevy and ECS for a second, and think (very abstractly) about what it takes to get an animated 3D model to move. You need two ingredients:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A spawned &lt;strong&gt;3D model&lt;/strong&gt; (or some kind of reference to one),&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;animation&lt;/strong&gt; to be played (or some kind of reference to one).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So at first, the way you might expect to play an animation is something like that:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;my_model&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;play_animation&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;my_animation&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s what our mental model looks like so far:&lt;/p&gt;
&lt;img src=&quot;/images/animation_intuition.svg&quot; alt=&quot;A diagram with a blue box titled 3D Model on the left, a red box titled animation data on the right, and a yellow oval titled play connecting both boxes&quot; width=&quot;370&quot;/&gt; 
&lt;p&gt;Now this is actually somewhat close to the way an actual Bevy function works, namely, the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/animation/struct.AnimationPlayer.html#method.play&quot;&gt;&lt;code&gt;play&lt;/code&gt;&lt;/a&gt; method from the &lt;code&gt;AnimationPlayer&lt;/code&gt; type… or at least it would be, if its &lt;code&gt;self&lt;/code&gt; actually referred to a 3D model, and its &lt;code&gt;animation&lt;/code&gt; argument actually referred to an animation. Right now, though, it’s not clear that they do. But let’s look in more detail into the way our two ingredients are represented in Bevy, and maybe we can reconcile our intuition with the way the &lt;code&gt;play&lt;/code&gt; function works.&lt;/p&gt;
&lt;h2 id=&quot;animationplayer-a-way-to-control-a-3d-model-s-animations&quot;&gt;AnimationPlayer: A way to control a 3D model’s animations&lt;/h2&gt;
&lt;p&gt;Say you spawn a 3D model from a &lt;code&gt;.glb&lt;/code&gt; file. Ideally, you’d like to refer to it using the ID of the spawned entity, except 3D models are generally not spawned as a single entity, but rather as a hierarchy of entities:&lt;/p&gt;
&lt;img src=&quot;/images/model_hierarchy.svg&quot; alt=&quot;A diagram with a blue box titled 3D Model enclosing a tree of boxes representing entities, each containing a couple boxes representing components&quot; width=&quot;320&quot;/&gt; 
&lt;p&gt;To animate your model, you need to refer to it some other way.&lt;/p&gt;
&lt;p&gt;Bevy has a mechanism to do just that — refer to a 3D model for animation — , in the form of the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/animation/struct.AnimationPlayer.html&quot;&gt;&lt;code&gt;AnimationPlayer&lt;/code&gt;&lt;/a&gt; type. &lt;code&gt;AnimationPlayer&lt;/code&gt; is a &lt;code&gt;Component&lt;/code&gt; that is automatically inserted in an &lt;code&gt;Entity&lt;/code&gt;… &lt;em&gt;somewhere&lt;/em&gt; in the entity hierarchy corresponding to an animatable 3D model, when that model is spawned.&lt;/p&gt;
&lt;img src=&quot;/images/animation_player.svg&quot; alt=&quot;A diagram representing the same hierarchy of entities, except one of the components is yellow and represents the animation player component&quot; width=&quot;320&quot;/&gt; 
&lt;p&gt;Once you have gotten a hold of an &lt;code&gt;AnimationPlayer&lt;/code&gt;, you can tell it to play/pause an animation, access the one currently playing… but that’s assuming you have animations in the first place! Let’s focus on those now.&lt;/p&gt;
&lt;h2 id=&quot;animationgraph-a-way-to-store-and-combine-animations&quot;&gt;AnimationGraph: A way to store and combine animations&lt;/h2&gt;
&lt;p&gt;The way animations are represented in Bevy is a little complex, because what you’ll generally manipulate is not single animations, but instances of a data structure able to store several animations at once and combine them with one another. That structure is the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/animation/graph/struct.AnimationGraph.html&quot;&gt;&lt;code&gt;AnimationGraph&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I won’t get into the details of how &lt;code&gt;AnimationGraph&lt;/code&gt;s are used here; once you’re comfortable with simple animations, you can learn more about general animation graphs with the &lt;a rel=&quot;external&quot; href=&quot;https://bevy.org/examples/animation/animation-graph/&quot;&gt;the Animation Graph example&lt;/a&gt;. For now we’ll focus on cases where our graph contains &lt;strong&gt;only one animation&lt;/strong&gt; that we’re interested in. In those cases,  the data that identifies your animation will be:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;(A reference to) an &lt;code&gt;AnimationGraph&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;An identifier  for where your animation actually sits inside the graph — that&#39;s what the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/petgraph/0.8.3/petgraph/graph/struct.NodeIndex.html&quot;&gt;&lt;code&gt;NodeIndex&lt;/code&gt;&lt;/a&gt; type is for.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;putting-it-together&quot;&gt;Putting it Together&lt;/h2&gt;
&lt;p&gt;Ok, say you have an &lt;code&gt;AnimationPlayer&lt;/code&gt;, as well as an &lt;code&gt;AnimationGraph&lt;/code&gt; and a &lt;code&gt;NodeIndex&lt;/code&gt;. Here’s how you connect them to play your animation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Insert a reference to your &lt;code&gt;AnimationGraph&lt;/code&gt; as a component on the same entity as your &lt;code&gt;AnimationPlayer&lt;/code&gt;. That’s exactly what the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/animation/graph/struct.AnimationGraphHandle.html&quot;&gt;&lt;code&gt;AnimationGraphHandle&lt;/code&gt;&lt;/a&gt; is for: it holds a &lt;code&gt;Handle&lt;/code&gt; to an &lt;code&gt;AnimationGraph&lt;/code&gt;, and it implements the &lt;code&gt;Component&lt;/code&gt; trait.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;my_animation_player.play(my_node_index);&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That’s it! Upon that call, &lt;code&gt;my_animation_player&lt;/code&gt; knows to look for the &lt;code&gt;AnimationGraph&lt;/code&gt; that’s a component on the same entity as itself, it looks for the animation with index &lt;code&gt;NodeIndex&lt;/code&gt; in that graph, and animates the model it belongs to.&lt;/p&gt;
&lt;p&gt;Here&#39;s how I picture things at this point:&lt;/p&gt;
&lt;img src=&quot;/images/animation_full.svg&quot; alt=&quot;Same diagram representing an entity hierarchy enclosed in a blue box, with one of the components colored yellow to represent the AnimationPlayer component, but now there&#39;s an additional component in the same entity as the AnimationPlayer. That new component is colored red and stands for the AnimationGraphHandle component. On the right of all that, there&#39;s a red box titled Animation Data, containing a subbox titled AnimationGraph and a subbox named NodeIndex. A red arrow connects the AnimationGraphHandle to the AnimationGraph box. An oval titled play connects the AnimationPlayer component with the NodeIndex box.&quot; width=&quot;650&quot;/&gt; 
&lt;p&gt;Now let’s look at the example code again.&lt;/p&gt;
&lt;h2 id=&quot;a-walk-through-the-example-code&quot;&gt;A Walk through the Example Code&lt;/h2&gt;
&lt;p&gt;In this section, I’ll simply walk through &lt;a rel=&quot;external&quot; href=&quot;https://bevy.org/examples/animation/animated-mesh/&quot;&gt;&lt;code&gt;Animated Mesh&lt;/code&gt;&lt;/a&gt;, from Bevy’s official examples, explaining how it relates to what I said above along the way. I’ll mostly go in chronological order, though I may reorder or even skip over some sections. My aim is to provide an explanation that’s a little more in-depth than the existing inline comments (which are already pretty thorough as far as comments go!), and connect it to what we’ve talked about in the abstract until now.&lt;/p&gt;
&lt;p&gt;The first lines are standard Bevy stuff; just note that we add the system &lt;code&gt;setup_mesh_and_animation&lt;/code&gt;, where we’ll put the animation logic. Let’s look at that system now, which runs at startup. First thing we do is extract animation data from our &lt;code&gt;.glb&lt;/code&gt; file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span&gt;graph&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; index&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #7AA89F;&quot;&gt; AnimationGraph&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;from_clip&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    asset_server&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;load&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;GltfAssetLabel&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;Animation&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;from_asset&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;GLTF_PATH&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There’s some boilerplate involved to load the animation clip; you don’t need to understand the detail of each function call, however you can note a few things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/gltf/enum.GltfAssetLabel.html&quot;&gt;&lt;code&gt;GltfAssetLabel::Animation&lt;/code&gt;&lt;/a&gt; constructor takes an integer that refers to your animation inside the &lt;code&gt;.glb&lt;/code&gt; file. If you don’t know the exact structure of your file, you’ll have to guess: try putting in numbers like 0, 1, 2, and see if your animation shows up.&lt;/li&gt;
&lt;li&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/prelude/struct.AnimationGraph.html#method.from_clip&quot;&gt;&lt;code&gt;from_clip&lt;/code&gt;&lt;/a&gt; method returns both an &lt;code&gt;AnimationGraph&lt;/code&gt; and a &lt;code&gt;NodeIndex&lt;/code&gt; in that graph — exactly the data you need to refer to an animation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then we add the graph we get to the asset store, and keep a &lt;code&gt;Handle&lt;/code&gt; to it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;let&lt;/span&gt;&lt;span&gt; graph_handle&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; graphs&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;graph&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also load our mesh; like for the graph, there’s some boilerplate involved which you don’t need to completely understand right away:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;let&lt;/span&gt;&lt;span&gt; mesh_scene&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;    WorldAssetRoot&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;asset_server&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;load&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;GltfAssetLabel&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;Scene&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;from_asset&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;GLTF_PATH&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s enough to know that the &lt;code&gt;mesh_scene&lt;/code&gt; you get can be spawned right away with the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/prelude/struct.Commands.html#method.spawn&quot;&gt;&lt;code&gt;spawn&lt;/code&gt;&lt;/a&gt; method. Here, however, for convenience, we bundle it with a custom component, &lt;code&gt;animation_to_play&lt;/code&gt; (which has custom type &lt;code&gt;AnimationToPlay&lt;/code&gt;, defined earlier in the example). &lt;code&gt;animation_to_play&lt;/code&gt; contains the two pieces of animation data we will need, namely the animation graph handle and the graph index:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;commands&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;spawn&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;((&lt;/span&gt;&lt;span&gt;animation_to_play&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; mesh_scene&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we pass control to the &lt;code&gt;play_animation_when_ready&lt;/code&gt; system once things are properly spawned:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;observe&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;play_animation_when_ready&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because it is called on the result of a &lt;code&gt;spawn&lt;/code&gt; call, this &lt;code&gt;observe&lt;/code&gt; call injects the spawned entity to the system it is given as argument. In other words, the root entity of our mesh scene, which we just spawned, can be accessed, as we will see, from the body of &lt;code&gt;play_animation_when_ready&lt;/code&gt;, in the form of the &lt;code&gt;entity&lt;/code&gt; field of the &lt;code&gt;scene_ready&lt;/code&gt; argument.&lt;/p&gt;
&lt;p&gt;The remaining tasks are a little less straightforward than what we’ve done so far. We need to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Locate the entity that has the &lt;code&gt;AnimationPlayer&lt;/code&gt; component,&lt;/li&gt;
&lt;li&gt;Add a handle to our animation graph as a component to it,&lt;/li&gt;
&lt;li&gt;Tell the animation player to play the animation we want.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ve got a problem with step 1, though: we loaded our mesh scene from a &lt;code&gt;.glb&lt;/code&gt; file, which spawns a hierarchy of entities; the animation player will have been attached to &lt;em&gt;some&lt;/em&gt; entity in that hierarchy, but we don’t know which! Maybe if we knew very well how our &lt;code&gt;.glb&lt;/code&gt; file is structured, and how GLTFs are represented in Bevy’s ECS, we could infer what entity has the animation player…? Eh, forget that, we’ll just manually visit all descendants of the mesh scene root, and check whether they have an &lt;code&gt;AnimationPlayer&lt;/code&gt; component.&lt;/p&gt;
&lt;p&gt;We start by getting the root entity of our mesh scene hierarchy. As mentioned earlier, this is injected into this system as the &lt;code&gt;entity&lt;/code&gt; field of the &lt;code&gt;scene_ready&lt;/code&gt; argument. That entity should have a component of type &lt;code&gt;AnimationToPlay&lt;/code&gt; containing the animation data we need, so we can directly &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/ecs/prelude/struct.Query.html#method.get&quot;&gt;&lt;code&gt;get&lt;/code&gt;&lt;/a&gt; it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;if let&lt;/span&gt;&lt;span style=&quot;color: #7AA89F;&quot;&gt; Ok&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;animation_to_play&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; animations_to_play&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scene_ready&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span&gt;entity&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Don’t confuse &lt;code&gt;animations_to_play&lt;/code&gt;, a &lt;code&gt;Query&lt;/code&gt; for components of type &lt;code&gt;AnimationToPlay&lt;/code&gt;, and &lt;code&gt;animation_to_play&lt;/code&gt;, the actual &lt;code&gt;AnimationToPlay&lt;/code&gt; that we get by looking at our entity through the prism of that query using the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/ecs/prelude/struct.Query.html#method.get&quot;&gt;&lt;code&gt;get&lt;/code&gt;&lt;/a&gt; method.)&lt;/p&gt;
&lt;p&gt;We have the scene root (as well as our animation data), but the scene root is not where the &lt;code&gt;AnimationPlayer&lt;/code&gt; component lives: all we know is that it will have been attached to &lt;em&gt;some&lt;/em&gt; entity in our scene hierarchy, in a way that’s dependent on our &lt;code&gt;.glb&lt;/code&gt; file and the specifics of how Bevy instantiates those files in its ECS. We don’t need to know about all that, though: we can just go through all entities in the scene (that is, all descendants of the root entity), and check whether they have an &lt;code&gt;AnimationPlayer&lt;/code&gt; component:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;for&lt;/span&gt;&lt;span&gt; child&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; in&lt;/span&gt;&lt;span&gt; children&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;iter_descendants&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scene_ready&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span&gt;entity&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;    if let&lt;/span&gt;&lt;span style=&quot;color: #7AA89F;&quot;&gt; Ok&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;mut&lt;/span&gt;&lt;span&gt; player&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; players&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;get_mut&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;child&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(In case you’re not familiar with &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/prelude/struct.Query.html#method.iter_descendants&quot;&gt;&lt;code&gt;iter_descendants&lt;/code&gt;&lt;/a&gt;, it’s a method which you must call on a &lt;code&gt;Query&amp;lt;&amp;amp;Children&amp;gt;&lt;/code&gt; (though you can replace &lt;code&gt;Children&lt;/code&gt; with another &lt;code&gt;RelationshipTarget&lt;/code&gt; if you’re interested in another kind of hierarchy) and which does what you’d expect, that is, iterate over all entities that descend from the argument you give it.)&lt;/p&gt;
&lt;p&gt;Phew! We’ve done the hard part, find the entity with the animation player. Now we can add the animation graph to that entity (the example tells the player to play the animation before the graph is added, which I guess works, but makes less intuitive sense):&lt;/p&gt;
&lt;p&gt;First we get the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bevy/latest/bevy/prelude/struct.EntityCommands.html&quot;&gt;&lt;code&gt;EntityCommands&lt;/code&gt;&lt;/a&gt; corresponding to the animation player entity, which will allow us to do stuff like add a component to it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;commands&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;entity&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;child&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then we add to it, as a component, the handle to the animation graph contained in &lt;code&gt;animation_to_play&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;AnimationGraphHandle&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;animation_to_play&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span&gt;graph_handle&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;clone&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;()));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we can tell our player to start playing the animation at the index specified in &lt;code&gt;animation_to_play&lt;/code&gt;, and also tell it to loop:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;player&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;play&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;animation_to_play&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span&gt;index&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;repeat&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Easy Type-Level Flags In Haskell</title>
        <published>2025-12-14T00:00:00+00:00</published>
        <updated>2025-12-14T00:00:00+00:00</updated>
        
        <author>
          <name>Grégoire Locqueville</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://glocq.com/en/blog/20251214/"/>
        <id>https://glocq.com/en/blog/20251214/</id>
        
        <content type="html" xml:base="https://glocq.com/en/blog/20251214/">&lt;p&gt;&lt;em&gt;December 14, 2025&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Say you&#39;re implementing an interpreter for arithmetical expressions in Haskell. You define the following data type:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;data Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  =&lt;/span&gt;&lt;span&gt; Literal&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Plus&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Minus&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Times&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Div&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr Expr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now you and I know that allowing division is a little risky, because your user might attempt to divide by zero at some point, which is totally forbidden. There are plenty of ways to deal with this at evaluation time; typically, you&#39;ll wrap the result of the computation in a &lt;code&gt;Maybe&lt;/code&gt; or some other type constructor that encodes the fact that an error was encountered.&lt;/p&gt;
&lt;p&gt;But say you really want a way to prevent divisions by zero at compile time. Of course you won&#39;t know if a value is zero until you&#39;ve evaluated it, so there&#39;s no perfect solution. But you might decide to define another data type which forbids divisions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;data SafeExpr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  =&lt;/span&gt;&lt;span&gt; SafeLiteral&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; SafePlus&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; SafeExpr SafeExpr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; SafeMinus&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; SafeExpr SafeExpr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; SafeTimes&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; SafeExpr SafeExpr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, and now you don&#39;t have to worry about divisions by zero anymore. Even better, you can introduce unsafety in your codebase in a controlled way, by using &lt;code&gt;SafeExpr&lt;/code&gt; when you want a strict guarantee that no division by zero will occur, and &lt;code&gt;Expr&lt;/code&gt; when you&#39;re ok with dealing with division-by-zero errors. For instance, you may make an &quot;unsafe&quot; toggle available to your user. When the toggle is off, your code uses &lt;code&gt;SafeExpr&lt;/code&gt;, and when it&#39;s on, it uses &lt;code&gt;Expr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The issue with this is... you now have two data types! This means you have to duplicate code: not only the above &lt;code&gt;data&lt;/code&gt; declarations, but also any function that produces or consumes an expression tree will need to be written twice. We don&#39;t want that. How can we avoid it?&lt;/p&gt;
&lt;p&gt;Some ideas (which I haven&#39;t all explored fully, so some might not actually work!) might include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Template Haskell&lt;/strong&gt;: letting the compiler write the duplicated code for you;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Generics&lt;/strong&gt;: inferring information based on the shape of your data type;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Type-level wizardry&lt;/strong&gt;: you might want to tag your expression type with something like a type-level boolean (&lt;code&gt;&#39;Bool&lt;/code&gt;/&lt;code&gt;&#39;False&lt;/code&gt;), and make sure that the &lt;code&gt;Div&lt;/code&gt; constructor never gets used when your type is tagged with &lt;code&gt;&#39;False&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Writing an explicit converter between our data types&lt;/strong&gt;: This one I&#39;d say is the best of those suboptimal solutions. It requires no compiler extensions and whatnot, and not much boilerplate. That is... unless you want to conditionally exclude another branch based on another condition. Then you need 2x2=4 datatypes, and conversion between each compatible pair among those! And the problem only gets worse the more conditions you add...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All but the last of those solutions add complexity to your program, if only by requiring the use of compiler extensions. But what if I told you there&#39;s a much simpler solution that doesn&#39;t add any complexity? No code duplication, no boilerplate, no compiler extensions, no loss of portability!&lt;/p&gt;
&lt;h2 id=&quot;a-trivial-yet-magical-type&quot;&gt;A Trivial Yet Magical Type&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;We&#39;ll make use of the type &lt;code&gt;Void&lt;/code&gt;, so here&#39;s a refresher. If you&#39;re familiar with &lt;code&gt;Void&lt;/code&gt;, you may skip this section :)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Void&lt;/code&gt; (from the module &lt;code&gt;Data.Void&lt;/code&gt;) is a very simple type: it has no constructor, and only one destructor &lt;code&gt;absurd :: Void -&amp;gt; a&lt;/code&gt;. You may think of it as the empty type, the type that has no values.&lt;/p&gt;
&lt;p&gt;Think about how weird the fact that it has no constructor is: though you may use &lt;code&gt;Void&lt;/code&gt; in your type signatures, your program will never actually hold a value of type &lt;code&gt;Void&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;The destructor is also weird: look at how &lt;code&gt;a&lt;/code&gt; is a free type variable --- it may stand for any type you like! Consider, for instance, the type &lt;code&gt;Reason&lt;/code&gt; of strings that explain why your ex left you, or the type &lt;code&gt;WorldPeace&lt;/code&gt; of &lt;code&gt;IO&lt;/code&gt; actions that achieve world peace. With a value &lt;code&gt;v&lt;/code&gt; of type &lt;code&gt;Void&lt;/code&gt;, all you&#39;d have to do is write &lt;code&gt;main = putStrLn (absurd v :: Reason) &amp;gt;&amp;gt; (absurd v :: WorldPeace)&lt;/code&gt; in &lt;code&gt;Main.hs&lt;/code&gt;, and get both the reason why your ex left, and world peace, at the same time! The catch, obviously, is that you will never get hold of a value of type &lt;code&gt;Void&lt;/code&gt; like &lt;code&gt;v&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;(Note that this does not mean we can&#39;t achieve world peace! I&#39;m just saying if we can, it&#39;s not as easy as that. And we should not be deterred by the fact that it does look very difficult. Regarding your ex, though, you should probably just move on)&lt;/p&gt;
&lt;p&gt;In a way, &lt;code&gt;Void&lt;/code&gt; is like the reverse (or &lt;em&gt;dual&lt;/em&gt;) of &lt;code&gt;()&lt;/code&gt;: just like you can&#39;t construct a value of &lt;code&gt;Void&lt;/code&gt; unless you already essentially have a value of &lt;code&gt;Void&lt;/code&gt;, you can&#39;t consume a value of &lt;code&gt;()&lt;/code&gt;, that is, write a function that has &lt;code&gt;()&lt;/code&gt; as an input, unless your function essentially outputs &lt;code&gt;()&lt;/code&gt;. So &lt;code&gt;Void&lt;/code&gt; is just as trivial as &lt;code&gt;()&lt;/code&gt;, but it has that touch of magic that &lt;code&gt;()&lt;/code&gt; lacks.&lt;/p&gt;
&lt;p&gt;Now we&#39;re good to go on with our solution!&lt;/p&gt;
&lt;h2 id=&quot;a-laughingly-simple-type-class&quot;&gt;A Laughingly Simple Type Class&lt;/h2&gt;
&lt;p&gt;Consider the following type class (&lt;code&gt;TFlag&lt;/code&gt; stands for &quot;type flag&quot;):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;class TFlag&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;  whenFlag&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; f b&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how the &lt;code&gt;f&lt;/code&gt; type parameter does not have any constraints (except for it being of kind &lt;code&gt;* -&amp;gt; *&lt;/code&gt;): it does not need to be a functor or anything of the like. Let&#39;s not dwell on the meaning of this class and its method &lt;code&gt;whenFlag&lt;/code&gt;, though, before looking at its instances. Actually, there are only two! The two types that implement &lt;code&gt;TFlag&lt;/code&gt; are the unit type &lt;code&gt;()&lt;/code&gt; and the void type &lt;code&gt;Void&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;instance TFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ()&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  whenFlag w x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;instance TFlag Void where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  whenFlag w x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; absurd w&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both instances are trivial, but each is trivial in a different way. To implement &lt;code&gt;whenFlag&lt;/code&gt;, we need to construct a value of type &lt;code&gt;f ()&lt;/code&gt;. Let&#39;s see how it&#39;s done in each case:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the instance for &lt;code&gt;()&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; happens to be &lt;code&gt;()&lt;/code&gt; already. So the second argument (which has type &lt;code&gt;f b&lt;/code&gt;) actually has type &lt;code&gt;f ()&lt;/code&gt;, exactly what we need. How lucky! Surely we won&#39;t be as lucky in the other instance?&lt;/li&gt;
&lt;li&gt;In the instance for &lt;code&gt;Void&lt;/code&gt;, the second argument has type &lt;code&gt;f Void&lt;/code&gt;, which is indeed not what we need. Again, &lt;code&gt;f&lt;/code&gt; is not a functor or anything like that, so we don&#39;t have anything to work with it. We need to turn to the first argument &lt;code&gt;w&lt;/code&gt;... its type, &lt;code&gt;Void&lt;/code&gt;, has nothing to do with &lt;code&gt;f ()&lt;/code&gt;, but since &lt;code&gt;Void&lt;/code&gt; is this magical type, all we need is to apply &lt;code&gt;absurd&lt;/code&gt; and we get a value of whatever type we want! Of course this was all just a dream, we never had a value of type &lt;code&gt;Void&lt;/code&gt; in the first place.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Note: if you allow the use of compiler extensions, the &lt;code&gt;EmptyCase&lt;/code&gt; extension allows you to pattern match on &lt;code&gt;Void&lt;/code&gt;, by writing:&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;instance TFlag Void where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  whenFlag w x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; case&lt;/span&gt;&lt;span&gt; w &lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What &lt;code&gt;whenFlag&lt;/code&gt; is doing is basically telling you &quot;give me a &lt;em&gt;proof&lt;/em&gt;, or a &lt;em&gt;witness&lt;/em&gt; &lt;code&gt;w&lt;/code&gt;, that the value &lt;code&gt;x&lt;/code&gt; has type &lt;code&gt;f ()&lt;/code&gt;, and I&#39;ll let you use it&quot;. This is trivial! But it lets us write code that only works when &lt;code&gt;b&lt;/code&gt; is &lt;code&gt;()&lt;/code&gt; in contexts when &lt;code&gt;b&lt;/code&gt; is &lt;code&gt;Void&lt;/code&gt;. Let&#39;s see how to use it in practice!&lt;/p&gt;
&lt;h2 id=&quot;back-to-our-expression-data-type&quot;&gt;Back To Our Expression Data Type&lt;/h2&gt;
&lt;p&gt;Here&#39;s how we can use the &lt;code&gt;TFlag&lt;/code&gt; class to implement a type-safe optional type constructor:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;data Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  =&lt;/span&gt;&lt;span&gt; Literal&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Plus&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Minus&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Times&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; Div unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What&#39;s changed?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We added a new type parameter &lt;code&gt;unsafeFlag&lt;/code&gt;. &lt;code&gt;unsafeFlag&lt;/code&gt; stands for a type subject to &lt;code&gt;TFlag&lt;/code&gt;, that is, it is either &lt;code&gt;()&lt;/code&gt; or &lt;code&gt;Void&lt;/code&gt;. &lt;code&gt;()&lt;/code&gt; stands for a type-level &quot;true&quot; of sorts, and &lt;code&gt;Void&lt;/code&gt; stands for a type-level &quot;false&quot; --- you&#39;ll see why in a minute.&lt;/li&gt;
&lt;li&gt;The unsafe constructor &lt;code&gt;Div&lt;/code&gt; has been tagged with &lt;code&gt;unsafeFlag&lt;/code&gt;. As a result, it may only be used when &lt;code&gt;unsafeFlag&lt;/code&gt; is &lt;code&gt;()&lt;/code&gt;! If it were &lt;code&gt;Void&lt;/code&gt;, you wouldn&#39;t be able to produce the required argument of type &lt;code&gt;unsafeFlag&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All other branches are left untouched, so they can be used normally.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;How would we write a function that deals with expressions?&lt;/p&gt;
&lt;p&gt;Functions that produce expressions are easy to write. Either your function may produce an expression that involves divisions, and you need to be explicit about it by setting &lt;code&gt;unsafeFlag&lt;/code&gt; to be &lt;code&gt;()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;divExpr&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;divExpr x y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; Div&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; () (&lt;/span&gt;&lt;span&gt;Literal x&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span&gt;Literal y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or it can&#39;t, and you can get away with a generic &lt;code&gt;unsafeFlag&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;plusExpr&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; TFlag&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;plusExpr x y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; Plus&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span&gt;Literal x&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;) (&lt;/span&gt;&lt;span&gt;Literal y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Functions that consume expressions are where it gets interesting. Either your function needs the safe version, and you need to be explicit about it by setting &lt;code&gt;unsafeFlag&lt;/code&gt;. You can discard the unsafe branch &lt;code&gt;Div&lt;/code&gt; by using the magical function &lt;code&gt;absurd&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;safeEval&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr Void&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;safeEval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Literal x&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;safeEval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Plus x y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; safeEval x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;+&lt;/span&gt;&lt;span&gt; safeEval y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;safeEval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Div w x y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; absurd w&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or your function is able to deal with the unsafe version, in which case you will need to specify how to deal with the unsafe case. But thanks to the &lt;code&gt;whenFlag&lt;/code&gt; method, your code will also work in the safe case:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; TFlag&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Expr&lt;/span&gt;&lt;span&gt; unsafeFlag&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Maybe Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;eval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Literal x&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; Just x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;eval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Plus x y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;lt;$&amp;gt;&lt;/span&gt;&lt;span&gt; eval x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt;&lt;span&gt; eval y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;eval &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Div w x y&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; whenFlag w &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  if&lt;/span&gt;&lt;span&gt; eval y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;==&lt;/span&gt;&lt;span&gt; Just&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;    then&lt;/span&gt;&lt;span&gt; Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;lt;$&amp;gt;&lt;/span&gt;&lt;span&gt; eval x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt;&lt;span&gt; eval y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- Note that testing equality between floating-point values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- is generally not a great idea.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- This here is just for exposition purposes :)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s it, we&#39;ve found a way to portably exclude some constructor at compile-time without any boilerplate, and write functions that work with either or both of the versions!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Solving a Wooden Puzzle Using Haskell (Part II)</title>
        <published>2025-05-05T00:00:00+00:00</published>
        <updated>2025-05-05T00:00:00+00:00</updated>
        
        <author>
          <name>Grégoire Locqueville</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://glocq.com/en/blog/20250505/"/>
        <id>https://glocq.com/en/blog/20250505/</id>
        
        <content type="html" xml:base="https://glocq.com/en/blog/20250505/">&lt;p&gt;&lt;em&gt;May 5, 2025&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This post is the second of a two-part series that describes my computer-assisted
solution to a physical puzzle I got. You will probably want to read the beginning
of &lt;a href=&quot;https://glocq.com/en/blog/20250428/&quot;&gt;part I&lt;/a&gt; to understand what the puzzle is about, but other
than that this post is pretty self-contained :)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Last time we defined a list &lt;code&gt;allValidPieces&lt;/code&gt; of all the ways that a piece may
be fitted in the box. Its type is the nested list &lt;code&gt;[[V3 Int]]&lt;/code&gt;, because we
represent a single piece as a list of the voxels that compose it. Looks like
we&#39;re going to deal with lots of nested lists... Let&#39;s try to prevent confusion
by defining a type synonym:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;type Shape&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Shape&lt;/code&gt; will typically represent a puzzle piece, but it can represent any chunk
of voxels. We&#39;ll use it below to represent a set of pieces lumped together.&lt;/p&gt;
&lt;h2 id=&quot;compatibility-matters&quot;&gt;Compatibility Matters&lt;/h2&gt;
&lt;p&gt;Ok, moving on. Let&#39;s recall what we are after: we need to find a way to fit 25
pieces in the box simultaneously. In other words, we&#39;re looking for a
subcollection of &lt;code&gt;allValidPieces&lt;/code&gt; that has 25 elements, such that no two
elements interpenetrate one another. Which begs the question: what does it mean,
formally, for two shapes to interpenetrate one another?&lt;/p&gt;
&lt;p&gt;Recall that we represented a piece as the list of voxels that compose it. Two
shapes, then, are in conflict when they have at least one voxel in common — in
other words, when there is any voxel from the first that is also an element of
the second:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;conflict&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;conflict shape1 shape2 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; any &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;\&lt;/span&gt;&lt;span&gt;voxel &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; elem voxel shape2&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span&gt; shape1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point, I&#39;d say we&#39;re finally done modeling the problem and can start
looking for a solution!&lt;/p&gt;
&lt;p&gt;25 pieces is a lot, we can&#39;t just repeatedly pick lists of 25 pieces until we
come across one where pieces happen to not conflict with one another. We need to
build our solution step by step, and for that, we need a way to find out what
pieces are compatible with a collection of pieces already in place. Out of
performance concerns, we won&#39;t encode the collection of pieces already in place
using a list of &lt;code&gt;Shape&lt;/code&gt;s, but we&#39;ll lump them all together in one single
&lt;code&gt;Shape&lt;/code&gt; composed of the voxels that are already occupied by some piece:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;newCompatiblePieces&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   -- ^ Voxels to avoid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;                    -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt; -- ^ All pieces that do not conflict with the input shape&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;newCompatiblePieces occupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  filter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;\&lt;/span&gt;&lt;span&gt;candidatePiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; not &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span&gt; conflict candidatePiece occupiedVoxels&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    allValidPieces&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that in the above function, we are looking for all pieces that do not
conflict with the input, but the pieces in the output list will generally
conflict with one another. That&#39;s not a problem: the output list should not be
seen as a list of pieces to add, but as a list of &lt;em&gt;options&lt;/em&gt; which we can choose
from.&lt;/p&gt;
&lt;p&gt;Of course, some of the potentially compatible pieces we find, while they do not
directly conflict with any piece already in place, will lead to a dead-end
nevertheless: there is no solution compatible with both the pieces in place and
the new piece. That&#39;s actually kind of the point of our puzzle!: we can&#39;t solve
it just by adding piece after piece as long as they fit.&lt;/p&gt;
&lt;p&gt;With an imperative apprach, we&#39;d deal with this problem using explicit
&lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Backtracking&quot;&gt;backtracking&lt;/a&gt;, keeping track, at
each step, of the pieces we&#39;ve tried, and going back one step whenever we don&#39;t
find what we&#39;re looking for. This is hard to think about! In Haskell, we
have a cheat code, and that&#39;s called:&lt;/p&gt;
&lt;h2 id=&quot;parallel-universes-with-the-list-monad&quot;&gt;Parallel Universes with the &lt;code&gt;List&lt;/code&gt; Monad&lt;/h2&gt;
&lt;p&gt;I like to think of monads and &lt;code&gt;do&lt;/code&gt; notation as a way for the programmer to pretend
they have access to a pure value when they don&#39;t actually. In the &lt;code&gt;IO&lt;/code&gt; monad,
writing &lt;code&gt;userInput &amp;lt;- getLine&lt;/code&gt; is a way to pretend you have a value of type
&lt;code&gt;String&lt;/code&gt;, even though you need to wait for &lt;code&gt;getLine&lt;/code&gt; to be executed at runtime
for the value to actually be defined. In the &lt;code&gt;Maybe&lt;/code&gt; monad, writing
&lt;code&gt;result &amp;lt;- someComputation&lt;/code&gt; is a way to pretend that &lt;code&gt;someComputation&lt;/code&gt; yielded
a normal value, which you can access under the name &lt;code&gt;result&lt;/code&gt;, even though
&lt;code&gt;someComputation&lt;/code&gt; might be &lt;code&gt;Nothing&lt;/code&gt; — in which case the whole &lt;code&gt;do&lt;/code&gt; block becomes
&lt;code&gt;Nothing&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Enter the &lt;code&gt;List&lt;/code&gt; monad (also known as the &lt;code&gt;[]&lt;/code&gt; monad, but that&#39;s harder to
pronounce). If &lt;code&gt;someList&lt;/code&gt; is a list of type &lt;code&gt;[a]&lt;/code&gt;, you can write
&lt;code&gt;value &amp;lt;- someList&lt;/code&gt; and proceed as though &lt;code&gt;value&lt;/code&gt; were an actual value of type
&lt;code&gt;a&lt;/code&gt;, even though there may be zero or more than one elements in the list.
What mechanism makes that possible? When you write &lt;code&gt;value1 &amp;lt;- someList1&lt;/code&gt;, the
universe &lt;em&gt;splits into several parallel subuniverses&lt;/em&gt;, and everything you write
under that line in the do block is computed/executed as many times as there are
elements in &lt;code&gt;someList1&lt;/code&gt;. If you write &lt;code&gt;value2 &amp;lt;- someList2&lt;/code&gt; below that, &lt;em&gt;each
of those parallel universes splits into parallel universes again&lt;/em&gt;, one for each
element of &lt;code&gt;someList2&lt;/code&gt;. If you&#39;re not careful, the number of branches can explode
quickly!&lt;/p&gt;
&lt;p&gt;With that in mind, we can implement our function that recursively looks for
&lt;em&gt;subsolutions&lt;/em&gt; of the puzzle: lists of &lt;em&gt;n&lt;/em&gt; compatible pieces (for some input &lt;em&gt;n&lt;/em&gt;
between 0 and 25) that do not conflict with a collection of pieces (again,
lumped together into a single &lt;code&gt;Shape&lt;/code&gt;) assumed to already be in place:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;subsolutions&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Int&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   -- ^ How many pieces should be in the subsolution we&amp;#39;re looking for?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;             -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt; -- ^ Unavailable voxels, already occupied by some piece&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;             -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt; -- ^ List of subsolutions, each of which is itself a list of pieces&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;subsolutions &lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;0&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;subsolutions n occupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; filter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;                (&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;\&lt;/span&gt;&lt;span&gt;piece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; not &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;conflict occupiedVoxels piece&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                allValidPieces&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; updatedOccupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span&gt; occupiedVoxels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  otherPieces &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; subsolutions &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span&gt; updatedOccupiedVoxels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  return &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span&gt; newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;:&lt;/span&gt;&lt;span&gt; otherPieces&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The base case is &lt;em&gt;n&lt;/em&gt; = 0: there is exactly one solution with zero pieces, that&#39;s
the list with no piece in it. Now look at what happens in the other branch:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We look for candidate pieces (on the right of the first &lt;code&gt;&amp;lt;-&lt;/code&gt;), that is, pieces
that do not conflict with our list of occupied voxels;&lt;/li&gt;
&lt;li&gt;Using the magical &lt;code&gt;&amp;lt;-&lt;/code&gt; symbol, we split the universe into as many parallel
universes as there are candidate pieces; in each subuniverse, &lt;code&gt;newPiece&lt;/code&gt;
will be defined to be the corresponding candidate piece;&lt;/li&gt;
&lt;li&gt;In each parallel universe, we perform a recursive call, looking for the list
of subsolutions that are compatible with both the original list of occupied
voxels and our new piece;&lt;/li&gt;
&lt;li&gt;Again, we split each universe into many parallel subuniverses, one for each
of those subsolutions;&lt;/li&gt;
&lt;li&gt;For each universe, we return the resulting subsolution. The actual result is
a list with one value for each universe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now you might think &quot;gosh, that&#39;s &lt;em&gt;a lot&lt;/em&gt; of universes to split into, how is my
computer ever going to deal with those?&quot;, and you&#39;d be right. There are two
reasons for hope, though:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Some combinations of pieces will not allow for any new piece to be fit into
the box. In that case, the corresponding list of subsolutions will be empty,
which means that instead of splitting the corresponding universe into
subuniverses, the whole branch will die off. In other words, sometimes the
total number of universes will decrease.&lt;/li&gt;
&lt;li&gt;Non-strict evaluation! This is all implemented with Haskell&#39;s lazy list type.
This means that as long as you&#39;re not trying to print the whole list of
solutions, not all parallel universes will actually be explored. Typically,
you&#39;ll want to print only the first element of the list of solutions, in
which case your program will stop as soon as it&#39;s found one.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Anyway, the final list of solutions (there may be more than one!) to our problem
is then:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;allSolutions&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;allSolutions &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; subsolutions &lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and we get an arbitrary solution by taking the first element of the list with
&lt;code&gt;head&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Alas, my computer hangs and never returns a value. It seems that we were too
careless splitting the universe many times, and the computer can&#39;t handle the
resulting combinatorial explosion. Honestly, this isn&#39;t too surprising. Our
approach is as bruteforce as it gets — which is actually good for a first
approach: as Donald Knuth puts it,
&lt;a rel=&quot;external&quot; href=&quot;https://en.wikiquote.org/wiki/Donald_Knuth&quot;&gt;&quot;premature optimization is the root of all evil (or at least most of it) in programming&quot;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Ok, but what now? I guess we&#39;ll need to dig into optimization literature, or
maybe turn the problem into a SAT formula and fire up a SAT solver... Just
kidding! We&#39;re still going to bruteforce our problem, we just need to be smarter
about it.&lt;/p&gt;
&lt;h2 id=&quot;a-slightly-smarter-solution&quot;&gt;A Slightly Smarter Solution&lt;/h2&gt;
&lt;p&gt;We don&#39;t need to look far, in the way we built our final list of solutions, to
find sources of redundancy — (sub)solutions that are equivalent to one another,
but are nevertheless registered separately. For two given compatible piece
dispositions &lt;code&gt;piece1&lt;/code&gt; and &lt;code&gt;piece2&lt;/code&gt;, for instance, &lt;code&gt;[piece1, piece2]&lt;/code&gt; and
&lt;code&gt;[piece2, piece1]&lt;/code&gt; count as two subsolutions, even though they obviously
represent the same situation. The search will be performed once for each, and
if it leads nowhere, well... too bad, you will have wasted twice as much time.
And this will be true for any combination of compatible pieces, at any recursive
call. No wonder the algorithm is inefficient!&lt;/p&gt;
&lt;p&gt;So, how are we to reduce the search space? Well, assume that you have a
subsolution and you&#39;re looking for a piece to add to it in order to get closer
to the full solution. Now take any free voxel &lt;code&gt;freeVoxel&lt;/code&gt;, that is, any voxel
that is not part of any of the subsolution&#39;s pieces. Since we ultimately want
to fill the 5x5x5 cube, we know for sure that the full solution will feature a
piece that contains &lt;code&gt;freeVoxel&lt;/code&gt;. So we can restrict our search to only pieces
that do!&lt;/p&gt;
&lt;p&gt;Let&#39;s first implement a function that picks any free voxel in the box. We just
define a list composed of all of the box&#39;s voxels, filter out those that are
contained in one of the pieces in place, and take the first element of the
filtered list using &lt;code&gt;head&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;pickFreeVoxel&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; V3 Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pickFreeVoxel alreadyOccupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  head &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  filter &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;\&lt;/span&gt;&lt;span&gt;voxel &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; not &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span&gt; elem voxel alreadyOccupiedVoxels&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; $&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  [&lt;/span&gt;&lt;span&gt;V3 x y z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It isn&#39;t hard, now, to modify our &lt;code&gt;subsolutions&lt;/code&gt; function so that it implements
our &quot;smarter&quot; bruteforce algorithm:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;subsolutionsSmart&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Int&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   -- ^ How many pieces should be in the subsolution we&amp;#39;re looking for?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;                  -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt; -- ^ Unavailable voxels, already occupied by some piece&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;                  -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;subsolutionsSmart &lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;0&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;subsolutionsSmart n occupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; freeVoxel &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; pickFreeVoxel occupiedVoxels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; filter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;                  (&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;\&lt;/span&gt;&lt;span&gt;piece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; elem freeVoxel piece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                             not &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;conflict occupiedVoxels piece&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;                  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  allValidPieces&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; updatedOccupiedVoxels &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span&gt; occupiedVoxels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  otherPieces &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; subsolutionsSmart &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span&gt; updatedOccupiedVoxels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  return &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span&gt; newPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;:&lt;/span&gt;&lt;span&gt; otherPieces&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only change is that each time we&#39;re looking for a new piece, we pick a
&lt;code&gt;freeVoxel&lt;/code&gt; and filter out pieces that don&#39;t contain it. Note that while
&lt;code&gt;newPiece&lt;/code&gt; is on the left of a left arrow &lt;code&gt;&amp;lt;-&lt;/code&gt;, because we want to be
considering &lt;em&gt;all&lt;/em&gt; pieces in the list, &lt;code&gt;freeVoxel&lt;/code&gt; is defined using a normal
&lt;code&gt;let&lt;/code&gt; assignment. That&#39;s because, even though &lt;em&gt;any&lt;/em&gt; free voxel will do, we only
want to consider one.&lt;/p&gt;
&lt;p&gt;And finally, to get the full list of solutions, we do exactly the same as above:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;allSolutionsSmart&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;allSolutionsSmart &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; subsolutionsSmart &lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We only want one solution, so we print the first element (or &lt;code&gt;head&lt;/code&gt;) of that
list, and we measure the time it takes:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; IO&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  startTime &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; getCurrentTime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  print &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span&gt; head allSolutionsSmart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  endTime &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; getCurrentTime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  putStrLn &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color: #98BB6C;&quot;&gt; &amp;quot;Found in &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             show &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;realToFrac &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;diffUTCTime endTime startTime&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Double&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #98BB6C;&quot;&gt;             &amp;quot; seconds.&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And my computer does find a solution, in a little more than 30 minutes!&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I don&#39;t have a moral to this story, it was just a fun case study. But here are
three suggestions of ways the program could be improved.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Optimization. On the one hand, the fact that a bruteforcing approach was able
to find us a solution at all is pretty nice. On the other hand, 30 minutes is
still quite a long time! The algorithm could probably be optimized, for instance
by favoring subsolutions that are more &quot;compact&quot;, for some definition of &quot;compact&quot;...&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you run the program at home, the output you&#39;ll get is a frankly illegible
list of lists of vector values that look like &lt;code&gt;V3 2 4 1&lt;/code&gt;. I was able to test my
solution in practice by manually drawing the corresponding pieces, and then packing the pieces
according to my drawing, but I won&#39;t lie, the process was pretty tedious. So
another possible improvement could be to find a way to pretty-print the solution(s)
the algorithm finds. It&#39;s not obvious how to do so, especially with text output!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, you might want to get all possible solutions, and for that,
to get rid of solutions that are rotated and/or mirrored versions of other
solutions. Better yet, you could try to avoid generating them in the first
place (in the same   way that we found a trick to avoid generating solutions
that were the same up to a rearrangement of the piece order).&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Feel free to tell me if you make progress on either front!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Solving a Wooden Puzzle Using Haskell (Part I)</title>
        <published>2025-04-28T00:00:00+00:00</published>
        <updated>2025-04-28T00:00:00+00:00</updated>
        
        <author>
          <name>Grégoire Locqueville</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://glocq.com/en/blog/20250428/"/>
        <id>https://glocq.com/en/blog/20250428/</id>
        
        <content type="html" xml:base="https://glocq.com/en/blog/20250428/">&lt;p&gt;&lt;em&gt;April 28, 2025&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This post is the first of a two-part series that describes my computer-assisted
solution to a physical puzzle I got. In this first part, I describe the problem
and model it in Haskell. This post is a little verbose, so feel free to skip
directly to &lt;a href=&quot;https://glocq.com/en/blog/20250505/&quot;&gt;part II&lt;/a&gt;, where we&#39;ll see how to tell our computer to
actually solve the puzzle.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I got gifted a puzzle recently, allegedly a &quot;very hard one&quot;. After receiving it,
I spent a couple minutes trying to solve it, but it quickly became clear that,
unless there was a trick I&#39;d missed, I didn&#39;t have the right combination of
patience and brainpower for that. I had some family and friends try to solve it
too, and although some of them displayed an impressive amount of persistence,
none was persistent enough to crack the puzzle either.&lt;/p&gt;
&lt;p&gt;You know who else is incredibly persistent? My computer. It&#39;s not nearly as smart
as the humans around me and I, but it makes up for that in speed and tirelessness.
So if I manage to tell it exactly what to do, it might be able to find a solution.
Let&#39;s try!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href=&quot;/documents/wooden_puzzle.lhs&quot;&gt;The source for this post and the next&lt;/a&gt; is a literate Haskell file. This means
you can run the code by running &lt;code&gt;cabal run wooden_puzzle.lhs&lt;/code&gt;. You can also
experiment with the values defined in it by running &lt;code&gt;cabal repl wooden_puzzle.lhs&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The following pieces of code are here to tell the Haskell toolchain how exactly
to interpret the file, and what our dependencies and imports are. I&#39;ll also
update them based on what I import in the second post.&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;{- cabal:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   build-depends:      base ^&amp;gt;= 4.17, linear ^&amp;gt;= 1.23, time ^&amp;gt;= 1.14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   default-language:   Haskell2010&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   build-tool-depends: markdown-unlit:markdown-unlit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;   ghc-options:        -pgmL markdown-unlit -Wall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; Main&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; Data.Time.Clock&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;getCurrentTime&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt; diffUTCTime&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; Linear.Matrix&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;M33&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt; identity&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt; transpose&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt; det33&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;!*!&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;!*&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; Linear.V3&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-puzzle&quot;&gt;The Puzzle&lt;/h2&gt;
&lt;p&gt;Here&#39;s what the puzzle looks like:&lt;/p&gt;
&lt;img src=&quot;/images/puzzle.jpg&quot; alt=&quot;A wooden box, with a bunch of wooden pieces scattered inside it and next to it&quot; width=&quot;320&quot;/&gt; 
&lt;p&gt;Its principle is fairly simple: it&#39;s composed of 25 identical wooden
pieces, and a 5x5x5 cubic box. To solve the puzzle, one needs to pack the pieces
into a 5x5x5 cube, without holes nor chunks dangling out, so that it fits into
the box.&lt;/p&gt;
&lt;p&gt;Here&#39;s what a single piece looks like: it&#39;s a 4x1x1 trunk, with a 1x1x1 cubic
notch poking out from one side:&lt;/p&gt;
&lt;img src=&quot;/images/puzzle_piece.svg&quot; alt=&quot;Simple rendering of a piece composed of a few cubes put together&quot; width=&quot;320&quot;/&gt; 
&lt;p&gt;The puzzle is easy enough to understand, and the fact that it&#39;s basically all
composed of cubes hints at the discrete nature of the problem, suggesting that
it would lend itself well to an algorithm search. Exactly how to model the
situation may not be completely obvious, though. We&#39;ll start setting the scene
by modeling space itself, then we&#39;ll talk about individual pieces, first in
isolation, then in the context of the whole puzzle. Along the way, we will need
a bit of math; I&#39;ll do my best to explain theoretical notions as we encounter them.&lt;/p&gt;
&lt;h2 id=&quot;first-steps-modeling-our-problem&quot;&gt;First Steps Modeling our Problem&lt;/h2&gt;
&lt;p&gt;Since we&#39;re dealing with things that all ultimately decompose into 1x1x1 cubes,
we can think of space as a grid of voxels (the 3D equivalent to a pixel) with
integer coordinates. Mathematically, a position in that space is a vector with
three integer coordinates&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;linear&lt;/code&gt; library provides the &lt;code&gt;V3&lt;/code&gt; type constructor for vectors of three
coordinates; the type of positions will thus be &lt;code&gt;V3 Int&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since the box has dimensions 5x5x5 voxels, we&#39;ll say it contains all voxels whose
coordinates are of the form &lt;code&gt;V3 x y z&lt;/code&gt;, where &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; range from 1 to 5.&lt;/p&gt;
&lt;p&gt;Since all pieces are identical, we can get away with modeling just one &quot;generic
piece&quot;, with arbitrary position and orientation. Every other piece can then be
recovered simply by moving this &quot;blueprint&quot; around. We&#39;ll pick the disposition
such that the piece&#39;s trunk extends from coordinates (0, 0, 0) to (0, 0, 3), and
its notch is located at (0, 1, 2). We model that generic piece by simply
providing the list of coordinates of the voxels that compose it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;genericPiece&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;genericPiece &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span&gt; V3&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0 0 0&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; V3&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0 0 1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; V3&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0 0 2&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; V3&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0 0 3&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;,&lt;/span&gt;&lt;span&gt; V3&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 0 1 2&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s tackle the hard part: describing the ways a piece can be arranged
inside the box.&lt;/p&gt;
&lt;p&gt;Mathematically, the ways that an object may be moved around in space can be
described by specifying a rotation around the origin, and a translation.
A 3D rotation around the origin can be encoded by a 3x3 matrix, and a
translation, by a 3-component vector:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;data Disposition&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; Disposition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  {&lt;/span&gt;&lt;span&gt; rotation&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;    ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; M33 Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  ,&lt;/span&gt;&lt;span&gt; translation&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; V3 Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; deriving Show&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, for a given disposition of a piece, we need a way to get the coordinates
of the cubes that compose the piece. To do that, we just apply the rotation and
the translation to each cube of the generic piece.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;dispositionCoordinates&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Disposition&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dispositionCoordinates disposition &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; fmap applyTransform genericPiece&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- Note that we are applying the translation after the rotation. We could&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- technically apply the the translation first, but I think it makes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- more intuitive sense to first choose our piece&amp;#39;s orientation and then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- translate it. In the end, what matters is that we stick to our convention.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;  applyTransform&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; V3 Int&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; V3 Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  applyTransform vector &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    translation disposition &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span&gt;rotation disposition &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;!*&lt;/span&gt;&lt;span&gt; vector&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;our-action-plan-to-enumerate-all-dispositions&quot;&gt;Our Action Plan to Enumerate All Dispositions&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;Disposition&lt;/code&gt; datatype is able to represent any possible disposition of a
piece in the box. What we&#39;ll need to solve the puzzle is to enumerate all
those dispositions, but if we try to do so, we&#39;ll run into two problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Not all values of type &lt;code&gt;Disposition&lt;/code&gt; actually correspond to a valid disposition:
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;rotation&lt;/code&gt; field is of type &lt;code&gt;M33 Int&lt;/code&gt;, but not all &lt;code&gt;M33 Int&lt;/code&gt; matrices
correspond to rotations: some of them encode transforms that mirrors their
input, others, to transforms that inflate it, and others do weirder stuff yet.&lt;/li&gt;
&lt;li&gt;Even in cases where the &lt;code&gt;rotation&lt;/code&gt; field actually encodes a rotation, the
specific disposition encoded by a given &lt;code&gt;Disposition&lt;/code&gt; might not fit in the
box.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Disposition&lt;/code&gt; datatype is infinite (ignoring technicalities): it has fields
of type &lt;code&gt;V3 Int&lt;/code&gt; and &lt;code&gt;M33 Int&lt;/code&gt;, which both have &lt;code&gt;Int&lt;/code&gt; coefficients, and &lt;code&gt;Int&lt;/code&gt;
is infinite. So even if we wanted to list all possible &lt;code&gt;Disposition&lt;/code&gt;s, we couldn&#39;t.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Fortunately, it turns out that there are only finitely many possible dispositions,
so there&#39;s still hope that we might be able to list them all. Directly enumerating
them would be too difficult, though, so we will decompose the task:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First, we&#39;ll make up a list of &lt;em&gt;candidate dispositions&lt;/em&gt;. This list will have two
important properties:
&lt;ul&gt;
&lt;li&gt;Any &lt;em&gt;valid&lt;/em&gt; disposition will appear in the list, even though the list might
still (and will) feature invalid ones.&lt;/li&gt;
&lt;li&gt;The list will be finite, so we&#39;ll be able to enumerate it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Then we&#39;ll define a predicate on dispositions, that is, a function of type
&lt;code&gt;Disposition -&amp;gt; Bool&lt;/code&gt;, which will tell us — this time with certainty —
whether a given disposition is valid.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To get the full, exclusive list of valid dispositions, we&#39;ll then just need to
filter out invalid dispositions from the list of candidate ones.&lt;/p&gt;
&lt;img src=&quot;/images/puzzle_venn.svg&quot; alt=&quot;Simple rendering of a piece composed of a few cubes put together&quot; width=&quot;320&quot;/&gt; 
&lt;h2 id=&quot;enumerating-candidate-dispositions&quot;&gt;Enumerating Candidate Dispositions&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;Disposition&lt;/code&gt; has two fields; let&#39;s look at each of those in detail. First,
the &lt;code&gt;rotation&lt;/code&gt; field. &lt;code&gt;rotation&lt;/code&gt;s are encoded by a value of type &lt;code&gt;M33 Int&lt;/code&gt;.
As we saw earlier, though, not all values of that type encode a rotation. Is
there a simple way to narrow &lt;code&gt;M33 Int&lt;/code&gt; down to an easily enumerated collection?
There is!&lt;/p&gt;
&lt;p&gt;To do that, we first need to talk a bit about what the coefficients of a 3x3
matrix represent. Say you have a 3D linear transform, and you want to write down
the matrix &lt;em&gt;M&lt;/em&gt; that corresponds to it (in the standard coordinate system with axes
X, Y and Z). The first thing you do is apply the transform to the X axis. You
end up with a vector that is no longer necessarily aligned with the X axis;
instead, it has components along the X, Y and Z axis. The coefficients on the
first row of the &lt;em&gt;M&lt;/em&gt; correspond to how much of each of the three axes appear in
our transformed X axis. Then the second row correspond to the same, but starting
with the Y axis, and the third row, to the Z axis.&lt;/p&gt;
&lt;p&gt;That&#39;s for a general linear transform. But we&#39;re dealing with the much more
specific case of rotations in a voxel world. If you think a bit about what happens
in you rotate one of the three axes when you rotate it in that context, you&#39;ll
find that it either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Remains unchanged.&lt;/li&gt;
&lt;li&gt;Gets flipped.&lt;/li&gt;
&lt;li&gt;Turns into another axis.&lt;/li&gt;
&lt;li&gt;Turns into the flipped version of another axis.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In all four cases, the corresponding matrix row will contain a 1 or a -1
coefficient for the resulting axis, and a 0 coefficient elsewhere. This means
that if we enumerate all possible matrices with coefficients -1, 0 and 1, we&#39;ll
get all valid rotation matrices! We do so using Haskell&#39;s handy syntactic sugar
for &lt;a rel=&quot;external&quot; href=&quot;https://wiki.haskell.org/List_comprehension&quot;&gt;list comprehension&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;candidateRotations&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;M33 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;candidateRotations &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #727169;&quot;&gt;  -- The `M33` type is actually just two nested `V3` in a trench coat:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  V3&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; (&lt;/span&gt;&lt;span&gt;V3 m11 m12 m13&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;     (&lt;/span&gt;&lt;span&gt;V3 m21 m22 m23&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;     (&lt;/span&gt;&lt;span&gt;V3 m31 m32 m33&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;  |&lt;/span&gt;&lt;span&gt; m11 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m12 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m13 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  ,&lt;/span&gt;&lt;span&gt; m21 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m22 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m23 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  ,&lt;/span&gt;&lt;span&gt; m31 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m32 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; m33 &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, keep in mind that this list contains many matrices that do &lt;em&gt;not&lt;/em&gt;
correspond to valid rotations. But all valid rotations do appear in it.&lt;/p&gt;
&lt;p&gt;Now we&#39;ll need to narrow the possible translations down into a finite list too.
The following paragraph is my attempt at explaining somewhat rigorously how we
do that; it&#39;s a little convoluted, but the general idea is fairly intuitive:
we care only about dispositions that fit in the box, so we can rule out any
disposition that translates the &lt;code&gt;genericPiece&lt;/code&gt; too far away.&lt;/p&gt;
&lt;p&gt;Ok, now here&#39;s the &quot;rigorous&quot; explanation: the &lt;code&gt;V3 Int&lt;/code&gt; type is able to encode
arbitrarily long translations, but we&#39;re only interested in those that keep
the piece in our 5x5x5 box. Taking a look at the &lt;code&gt;genericPiece&lt;/code&gt; above, remark
that it contains the cube at the origin, &lt;code&gt;V3 0 0 0&lt;/code&gt;. Regardless of the rotation
matrix we apply, it will leave that cube unchanged. This means that only
the &lt;code&gt;translation&lt;/code&gt; field impacts the origin cube. In other words, in any piece
given by a value of type &lt;code&gt;Disposition&lt;/code&gt;, the coordinates of the cube
corresponding to the &lt;code&gt;genericPiece&lt;/code&gt;&#39;s cube at the origin are exactly the
&lt;code&gt;translation&lt;/code&gt;&#39;s field coordinates. Since we want all the cubes in our piece to
fit in the box, all coefficients must be between 1 and 5. So we can rule out
any disposition whose &lt;code&gt;translation&lt;/code&gt; field features coefficients outside that
range, and we can finitely enumerate a list of candidate translations:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;candidateTranslations&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;candidateTranslations &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span&gt; V3 x y z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;],&lt;/span&gt;&lt;span&gt; z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;..&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;] ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have an enumeration of candidates for each of &lt;code&gt;Disposition&lt;/code&gt;&#39;s fields,
we can put them together to get a list of candidate &lt;code&gt;Disposition&lt;/code&gt;s:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;candidateDispositions&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Disposition&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;candidateDispositions &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;  [&lt;/span&gt;&lt;span&gt; Disposition rot trans &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;|&lt;/span&gt;&lt;span&gt; rot   &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; candidateRotations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;                          ,&lt;/span&gt;&lt;span&gt; trans &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; candidateTranslations &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;filtering-out-remaining-invalid-dispositions&quot;&gt;Filtering Out Remaining Invalid Dispositions&lt;/h2&gt;
&lt;p&gt;Now on to filtering out the remaining invalid dispositions! First we&#39;ll weed out
invalid rotations. We don&#39;t need to get into the details here, but it turns out
there&#39;s an easy way to tell whether a given 3x3 matrix encodes a rotation: a 3x3
matrix &lt;em&gt;M&lt;/em&gt; encodes a rotation exactly when it is part both of the
&lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Orthogonal_group&quot;&gt;orthogonal linear group&lt;/a&gt; and the
&lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Special_linear_group&quot;&gt;special linear group&lt;/a&gt;.
In more concrete terms, this means &lt;em&gt;M&lt;/em&gt; fulfils the following two conditions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;M&lt;/em&gt; multiplied by its transpose is the identity matrix.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;M&lt;/em&gt;&#39;s determinant is 1.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In Haskell, this translates to:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;-- |&lt;/span&gt;&lt;span style=&quot;color: #727169;&quot;&gt; Does the given matrix encode a rotation?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;isRotation&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; M33 Int&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;isRotation matrix &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; ((&lt;/span&gt;&lt;span&gt;transpose matrix &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;!*!&lt;/span&gt;&lt;span&gt; matrix&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ==&lt;/span&gt;&lt;span&gt; identity&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;                    (&lt;/span&gt;&lt;span&gt;det33 matrix &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;re almost done describing all possible dispositions! We know what a valid
rotation looks like, now the one problem we might still have is a piece arranged
in such a way that it doesn&#39;t fit in the box. That&#39;s easy, we just need to check
that each cube of the piece fits in the box:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;pieceFitsTheBox&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Disposition&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pieceFitsTheBox disposition &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  all cubeFitsTheBox &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;dispositionCoordinates disposition&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;  where&lt;/span&gt;&lt;span&gt; cubeFitsTheBox &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; V3 Int&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        cubeFitsTheBox &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;V3 x y z&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                    y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; y &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                    z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; z &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #D27E99;&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And a valid disposition is one whose &lt;code&gt;rotation&lt;/code&gt; field is actually a rotation,
and which results in a piece that fits in the box:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;isValidDisposition&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Disposition&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;isValidDisposition disposition &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; isRotation &lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;(&lt;/span&gt;&lt;span&gt;rotation disposition&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                 pieceFitsTheBox disposition&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Putting it all together, we get a list of all valid dispositions by starting from
the list of candidate dispositions and filtering out invalid ones:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;allValidDispositions&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;Disposition&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;allValidDispositions &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; filter isValidDisposition candidateDispositions&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;re pretty much done for this first blog post, but I&#39;d like to prepare for the
next one with one last step: our &lt;code&gt;Disposition&lt;/code&gt; datatype served us well, but all
we will need now to solve the puzzle is the actual coordinates of the cubes that
compose the pieces. So we&#39;ll convert it all to coordinates:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #DCD7BA; background-color: #1F1F28;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7E9CD8;&quot;&gt;allValidPieces&lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt; ::&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt; [[&lt;/span&gt;&lt;span style=&quot;color: #957FB8;&quot;&gt;V3 Int&lt;/span&gt;&lt;span style=&quot;color: #9CABCA;&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;allValidPieces &lt;/span&gt;&lt;span style=&quot;color: #E6C384;&quot;&gt;=&lt;/span&gt;&lt;span&gt; fmap dispositionCoordinates allValidDispositions&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Phew! That was a little tedious, but now everything&#39;s in place for next part,
where we&#39;ll leverage Haskell&#39;s conciseness to actually solve the puzzle in a very
elegant way. See you then!&lt;/p&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;Pedantic note: Mathematically speaking, a vector cannot have integer
coordinates. The coordinates of a vector must take values in a field,
and integers do not form a field, only a ring. The technical term for
&quot;vector space&quot;-like objects over something that&#39;s only a ring is
&lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Module_(mathematics)&quot;&gt;module&lt;/a&gt;.
Of course this lexical matter has absolutely no consequence on the rest,
so we&#39;ll keep talking about &quot;vectors&quot;.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tricks and Conventions</title>
        <published>2025-03-24T00:00:00+00:00</published>
        <updated>2025-03-24T00:00:00+00:00</updated>
        
        <author>
          <name>Grégoire Locqueville</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://glocq.com/en/blog/20250324/"/>
        <id>https://glocq.com/en/blog/20250324/</id>
        
        <content type="html" xml:base="https://glocq.com/en/blog/20250324/">&lt;p&gt;&lt;em&gt;March 24, 2025&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I want to talk about an equation that sometimes comes up in online pop sci math expositions, namely 0.999... = 1.&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Any time it is the subject of a blog post or an online video, this equation sparks a heated debate in the comments. Things generally go as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The original post/video provides a proof that the equation is true,&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Skeptics&lt;/em&gt; start protesting in the comments, invoking all sorts of informal arguments, trying to articulate the fact that the equation is &lt;em&gt;obviously false&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;Then &lt;em&gt;believers&lt;/em&gt; tell them off, mocking them for not agreeing with math&#39;s objectively true conclusions,&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;and the whole argument generally goes as well as you would expect from a public online discussion.&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-clever-proof&quot;&gt;A Clever Proof&lt;/h2&gt;
&lt;p&gt;What does a typical one of those proofs of 0.999... = 1 look like? There&#39;s a common one that goes like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start with the number 1.&lt;/li&gt;
&lt;li&gt;Divide it by 3. You get 0.333...&lt;/li&gt;
&lt;li&gt;Multiply that number back by 3, and you get 0.999...&lt;/li&gt;
&lt;li&gt;But since all you did was divide and multiply 1 by the same number 3, you must end up with the same number you started with, namely 1! Hence 0.999... is the same as 1. QED!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I don&#39;t know about you, but I think this proof feels like some kind of magic trick. If you&#39;ve ever followed math lessons, chances are you&#39;ve been exposed to fallacious proofs that &lt;em&gt;e.g.&lt;/em&gt; 1 = 0, where reasonable-looking algebraic manipulations hide forbidden divisions by zero; and this might have taught you to be wary of fishy-looking proofs you&#39;ve never talked to before. And this proof sure looks fishy!&lt;/p&gt;
&lt;p&gt;And yet I can guarantee you there&#39;s nothing &lt;em&gt;technically&lt;/em&gt; wrong with it. Sure, each step could be spelt out in more detail, but there&#39;s no actual cheating involved. Is that it then, do we just have to accept what the ✨math✨ tells us without questioning it? — Not only do I believe that you don&#39;t &lt;em&gt;have to&lt;/em&gt;, I believe that you &lt;em&gt;shouldn&#39;t&lt;/em&gt; blindly accept it, lest you miss something important.&lt;/p&gt;
&lt;h2 id=&quot;what-s-in-a-decimal-number&quot;&gt;What&#39;s in a Decimal Number?&lt;/h2&gt;
&lt;p&gt;By &quot;decimal numbers&quot;, I mean &quot;numbers that can be written with &lt;em&gt;finitely&lt;/em&gt; many digits after the decimal dot&quot;, like 1234.5 or 1.203. In this section, I&#39;d just like to make sure that you and I are on the same page about what those represent exactly before we go on to talk about numbers with &lt;em&gt;infinite&lt;/em&gt; decimal expansion.&lt;/p&gt;
&lt;p&gt;Let&#39;s try to write those numbers using only &lt;em&gt;whole numbers&lt;/em&gt; and basic operations (addition, multiplication, division). Let&#39;s take 1.203 as an example. We can start by laying out each digit like so:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1&lt;/li&gt;
&lt;li&gt;2&lt;/li&gt;
&lt;li&gt;0&lt;/li&gt;
&lt;li&gt;3&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first digit &quot;1&quot; we can leave alone, since it stands for exactly what it looks like. The second one &quot;2&quot; stands for the number of tenths, so let&#39;s divide it by 10. The third one we&#39;ll divide by 10x10 = 100, and so on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1&lt;/li&gt;
&lt;li&gt;2/10&lt;/li&gt;
&lt;li&gt;0/(10x10)&lt;/li&gt;
&lt;li&gt;3/(10x10x10)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;then we can just add all that together, and that&#39;s what mathematicians have agreed the string &quot;1.203&quot; should &lt;em&gt;mean&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;1.203 = 1+2/10+0/(10x10)+3/(10x10x10)&lt;/p&gt;
&lt;p&gt;Easy enough, right? Not much to get excited about here. The real fun begins when there&#39;s an infinite number of digits after the dot. Before we get into what it &lt;em&gt;means&lt;/em&gt; to write infinitely many digits after the dot, though, we&#39;ll need to get acquainted with the notion of a &lt;em&gt;limit&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;limits&quot;&gt;Limits&lt;/h2&gt;
&lt;p&gt;Forget about decimal dots, and consider the following sequence of numbers:&lt;/p&gt;
&lt;p&gt;1/1 (also known as 1), 1/2, 1/3, 1/4, ...&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Clearly, no single one of those numbers is equal to 0. Yet you can probably sense that the sequence as a whole is related to the number 0. How so? If you wanted to make this idea precise, you might say something along the lines of:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;No matter how close I want to get to 0, there&#39;s always some number in the sequence after which all numbers are at least that close to 0.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Say you&#39;re only interested in numbers that are at most 0.1 units away from 0. Well in our infinite sequence, all numbers to the right of 1/10 are less than 0.1 units away from 0! Want to be more strict, and only accept numbers at most 0.000001 units away from 0? No problem, just start at 1/1000000 in the sequence: all the numbers to the right of that are close enough to 0 according to your new criterion.&lt;/p&gt;
&lt;p&gt;When an infinite sequence of numbers gets close to some number in this way, mathematicians say that the sequence has it as a &lt;strong&gt;limit&lt;/strong&gt;.&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Now on to digits!&lt;/p&gt;
&lt;h2 id=&quot;non-necessarily-decimal-numbers&quot;&gt;Non-(necessarily) Decimal Numbers&lt;/h2&gt;
&lt;p&gt;Consider some number with infinitely many digits after the dot, say pi. As a reminder, pi is a number whose decimal expansion starts with 3.14159... Let&#39;s construct a sequence of numbers in the following way: we start with 3, and at each step, we add one digit after the decimal dot:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;3&lt;/li&gt;
&lt;li&gt;3.1&lt;/li&gt;
&lt;li&gt;3.14&lt;/li&gt;
&lt;li&gt;3.141&lt;/li&gt;
&lt;li&gt;3.1415&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now taken individually, each of those numbers is a decimal number, and we&#39;ve seen how to deal with those in the previous section — nothing controversial there. But now we have an &lt;em&gt;infinite sequence&lt;/em&gt; of such numbers, and there&#39;s something we can do about infinite sequences: ask whether they have a limit! Does the sequence above have a limit? It sure does — and the limit is, of course, pi.&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In general, sequences built from a decimal string in this way always have exactly one limit. And so mathematicians have agreed that &lt;em&gt;the number represented by that string is said limit&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Now we have all we need to understand our original issue!&lt;/p&gt;
&lt;p&gt;Let&#39;s recap what we&#39;ve (re)learnt:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We&#39;ve clarified what we mean when we write a number with &lt;em&gt;finitely&lt;/em&gt; many digits, in terms of an expression involving only whole numbers;&lt;/li&gt;
&lt;li&gt;We&#39;ve defined the notion of a &lt;em&gt;limit&lt;/em&gt;;&lt;/li&gt;
&lt;li&gt;Using this notion, we&#39;ve also clarified what we mean when we write a number with &lt;em&gt;infinitely&lt;/em&gt; many digits.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now what happens if we apply this to the number 0.999...? We get a sequence&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;0 = 0&lt;/li&gt;
&lt;li&gt;0.9 = 0 + 1/10&lt;/li&gt;
&lt;li&gt;0.99 = 0 + 1/10 + 1/(10x10)&lt;/li&gt;
&lt;li&gt;0.999 = 0 + 1/10 + 1/(10x10) + 1/(10x10x10)&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And we need to determine its limit. To do so, we can observe that the sequence can be rewritten like so&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1 - 1&lt;/li&gt;
&lt;li&gt;1 - 1/10&lt;/li&gt;
&lt;li&gt;1 - 1/(10x10)&lt;/li&gt;
&lt;li&gt;1 - 1/(10x10x10)&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;which can be proven has 1 as a limit.&lt;/p&gt;
&lt;h2 id=&quot;slow-and-methodical-solves-the-case&quot;&gt;Slow and Methodical Solves the Case&lt;/h2&gt;
&lt;p&gt;So we&#39;re done: we proved the original claim that 0.999... = 1 — except we did so with a much lengthier proof than the &quot;clever proof&quot; laid out at the beginning of this post! What&#39;s the point then?&lt;/p&gt;
&lt;p&gt;My complaint about the &quot;clever proof&quot; was that it felt like a trick — a series of steps, admittedly simple, which can each be checked to be correct, but which doesn&#39;t offer much in the way of understanding what&#39;s actually going on. My aim with the above was to provide you with the knowledge required to be able to take a step back and ask yourself the right questions. From a bird&#39;s eye view, what have we essentially done?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We&#39;ve asked what it actually &lt;em&gt;means&lt;/em&gt; to write 0.999... Implicitly, this suggests that a string of digits&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#6&quot;&gt;6&lt;/a&gt;&lt;/sup&gt; is not exactly the same as a number, but that it is only a &lt;em&gt;representation&lt;/em&gt; of a number.&lt;/li&gt;
&lt;li&gt;While I did claim that a given string of digits⁶ may not represent more than one number, two distinct strings of digits may well represent the same number. This actually shouldn&#39;t come as a surprise, though: you&#39;re probably already aware that, say, 7.1900 and 7.19 stand for the same number!&lt;/li&gt;
&lt;li&gt;Equipped with this new perspective, faced with a limit to compute, we&#39;ve done the natural thing to do, namely, compute the limit.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The answer to our original question emerged naturally from a methodical effort, and along with it came a clearer understanding of the whole matter. I guess that&#39;s the first of two ideas I&#39;ve been trying to communicate in this blog post: when faced with intellectual problems, tricks and strokes of genius may make you feel smart, but patient and purposeful efforts will generally come with a longer-lasting reward.&lt;/p&gt;
&lt;h2 id=&quot;it-s-all-convention&quot;&gt;It&#39;s All Convention&lt;/h2&gt;
&lt;p&gt;The second idea I&#39;d like to communicate is more important in math than you might suspect; namely, the relativity of it all. We started by looking at a debate over whether two numbers that did not look equal actually were equal; but looking closer, we found out that what we were looking at was not exactly numbers, but &lt;em&gt;representations&lt;/em&gt; of numbers. This specific way of representing numbers using strings of digits⁶ is not inscribed in nature in the same way that, say, the law of universal gravitation is: humans invented it, and had to teach it to one another, enough so that it has now become ubiquitous. But as ubiquitous as it is, it&#39;s still just a convention!&lt;/p&gt;
&lt;p&gt;More generally, everything mathematicians do is based on conventions: they write down definitions of mathematical objects and make up names and notations to talk about them; and they use the definitions, names and notations that their peers have devised. As a result, they are acutely aware of the distinction between human conventions and the absolute, intangible ideas that lie behind.&lt;/p&gt;
&lt;p&gt;I feel like the mathematicians&#39; attitude of systematically questioning the absolute/relative quality of what you&#39;re talking about is second nature to them, and yet is often lost on the general public. Yet I believe it&#39;s good mental hygiene: next time you disagree with someone, try to evaluate how much of your conflict is a matter of convention — and maybe you&#39;ll realize you were just using the same word to talk about something different, or that the &quot;spelling error&quot; you were about to complain about wasn&#39;t actually a hindrance to clear and respectful communication!&lt;/p&gt;
&lt;p&gt;P.S. Before I leave you, here&#39;s some food for thought, in the form of one technical question, and one more &quot;philosophical&quot; one.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;So there are numbers that have several corresponding decimal representations. Are you able to tell which ones, exactly?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now that we understand better the relation between decimal representations and numbers, and that we&#39;ve learnt to ask what is absolute and what is convention, a new question might arise: &lt;em&gt;what even is a number, exactly?&lt;/em&gt; There are actually several answers to that question, none of which are obvious!&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#7&quot;&gt;7&lt;/a&gt;&lt;/sup&gt; But that&#39;s a story for another time...&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;I hope you will forgive me for not writing out the left-hand side of the equation in full, but just so we&#39;re clear: the &quot;...&quot; here is meant to stand for an infinite number of nines. Note that although the subject is controversial, there doesn&#39;t seem to be any disagreement about the meaning of those three dots, so we won&#39;t dwell on them.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;Ok, I might be exaggerating here, but I remember there being a clear disagreement in the exchanges I witnessed.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;Note that while previously, we were talking about one number (with infinitely many digits), here we are considering&lt;/em&gt; an infinite sequence &lt;em&gt;of numbers. It so happens that some of those numbers (like 1/2) can be written with finitely many digits (0.5), while others (1/3) have infinitely many (0.333...), but&lt;/em&gt; that&#39;s not the point: &lt;em&gt;for now, I want you to think about all those numbers without caring about how to write them.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;Note that a sequence of numbers does not necessarily have a limit: in the sequence 1, 2, 1, 2, ..., for instance, there is no number that gets approached by this sequence in the way described above. However, you can prove (which I&#39;m not going to do here) that a sequence cannot have more than one distinct limits.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;The way I talk about it makes it seem like it&#39;s a happy coincidence. Of course it&#39;s not, the decimal expansion 3.14159... was specifically constructed in such a way that the associated sequence has the number pi as a limit.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;6&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;I&#39;m being a little imprecise with the phrase &quot;string of digits&quot; here. To be perfectly rigorous, I should have said &quot;finite string of digits, possibly followed by a dot and another finite or infinite string of digits&quot;... but it just isn&#39;t as catchy, you know.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;7&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;7&lt;/sup&gt;
&lt;p&gt;&lt;em&gt;For a taste of what the answers look like, have a peek at &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Construction_of_the_real_numbers&quot;&gt;https://en.wikipedia.org/wiki/Construction_of_the_real_numbers&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;
</content>
        
    </entry>
</feed>
