<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
      <id>https://wolfgirl.dev/blog/</id>
      <title>PolyWolf's Blog - #lean4</title>
      <subtitle
        >all posts tagged "lean4" on a blog written by PolyWolf</subtitle
      >
      <icon>/apple-touch-icon.png</icon>
      <link href="/tags/lean4/rss.xml" rel="self" />
      <link href="https://wolfgirl.dev/blog/" />
      <updated>2024-09-03T12:39:21Z</updated>
      
    <entry>
      <title><![CDATA[OK Nevermind Lean 4 Is Kinda Bad Actually]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/7534408/" />
      <id>https://wolfgirl.dev/blog/7534408/</id>
      <published>2024-09-03T12:39:21Z</published>
      <updated>2024-09-03T12:39:21Z</updated>
      <summary><![CDATA[In which I express my disappointment that the new programming language I'm learning isn't so good after all...]]></summary>
      <content type="html"><![CDATA[<p>Originally posted on Cohost. I had planned to write <a href=/tags/pwcc>pwcc</a> in Lean 4, but after these discoveries & roadblocks, I gave up on it.</p><hr><h3 id=heading-the-binary-sizes>The Binary Sizes<a aria-label="Link to heading 'The Binary Sizes'" data-heading-content="The Binary Sizes" class=anchor href=#the-binary-sizes></a></h3><p>My "hello world but it parses command line arguments" binary was a whopping 78 megabytes, and after adding a regex library (which I haven't yet ended up using in any codepaths that can be run) it's 83 megabytes now. Talking to my friend this is because "no optimizations are done" and "all symbols from all transitive dependencies are exported" and "mathlib (the largest one & sort of the reason for Lean's existence in the first place) is usually included somewhere in that chain"</p><h3 id=heading-the-error-messages>The Error Messages<a aria-label="Link to heading 'The Error Messages'" data-heading-content="The Error Messages" class=anchor href=#the-error-messages></a></h3><p>Because of the whole "type universe" thing it has going on, you get some very strange errors:</p><ul><li>returning the wrong type of <code>Unit</code> (there is also a universe-polymorphic <code>PUnit</code>)</li><li>using the wrong level of <code>Option.none</code> inside an <code>Option</code> monad (when the return type of the outer function and the inner function belong to different type universes)</li></ul><p>Let alone all the error messages that come up when trying to do metaprogramming stuff..</p><p>A lot of this I think is because Lean is wayyy too flexible, so errors are reported far away from where the "wrong" thing actually happened. Writing <code>Unit -> T</code> when it really wanted <code>fun () => ({}: T)</code> is not necessarily easy to debug.</p><h3 id=heading-the-metaprogramming>The Metaprogramming<a aria-label="Link to heading 'The Metaprogramming'" data-heading-content="The Metaprogramming" class=anchor href=#the-metaprogramming></a></h3><p>Lean 4 is supposed to have the most advanced metaprogramming in existence, and to some extent that's true! However, because it's so advanced, it only operates at the AST level & lacks the capabilities to insert all the ASTs it knows about. For example, I was trying to make a custom inductive datatype. In Rust, this looks something like <a href="https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=370a6c198e6da266529a592c5916a5c1">this</a>, and translating that to Lean syntax, you'll get something like</p><pre class=syntax-highlighting><code class=language-lean4>macro t:ident "::=" (ids:ident),* : command => `(
  inductive $t where $[| $ids : $t]*
)
</code></pre><p>The problems with this you'll run into:</p><ol><li><code>macro</code> doesn't like arguments inside repeaters, you need to desugar to <code>syntax</code> + <code>macro_rules</code></li><li>the part that the <code>$[]*</code> replaces isn't a syntax category Lean recognizes</li></ol><p>Both of these generate a similar error message ("unexpected token") but (2) is the much more fundamental problem. If you try to desugar even more, you'll find that you're expected to make a syntax node of type <code>TSyntax `Lean.Parser.Command.ctor</code>. How do you get a syntax node of this type? Who knows!! If you fake it and try to case something that might look like it, you get bizarre error messages.</p><p>There's another thing you can do, which is to make an <code>elab</code> instead of a macro, but I also could not for the life of me figure out the magic incantation to make <code>Lean.Elab.Command.Declaration.inductDecl</code> stop giving me errors when I tried to <code>elabCommand</code> one with a single constructor.</p><p>In any case, I think I will go back to Rust metaprogramming, where we operate on plain-ol' token streams & the error messages make more sense to me</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    <entry>
      <title><![CDATA[Lean 4 Is A Cool Language]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/7506895/" />
      <id>https://wolfgirl.dev/blog/7506895/</id>
      <published>2024-09-01T03:05:22Z</published>
      <updated>2024-09-01T03:05:22Z</updated>
      <summary><![CDATA[In which I express my positive feelings about a new programming language I'm learning!]]></summary>
      <content type="html"><![CDATA[<p>Originally published on Cohost. Initially, I had planned on writing <a href=/tags/pwcc/>pwcc</a> in Lean 4, and this post was part of that exploratory process.</p><hr><p>Following along with <a href=https://lean-lang.org/lean4/doc/examples/bintree.lean.html>https://lean-lang.org/lean4/doc/examples/bintree.lean.html</a>, because i Just Want To Start Writing Code and reading examples is the best way to get acquainted.</p><p>Starting off, we have just a simple polymorphic tree definition:</p><pre class=syntax-highlighting><code class=language-lean><span class=keyword>inductive</span> <span class=variable>Tree</span> <span class=punctuation>(</span><span class=variable>β</span> <span class=punctuation>:</span> <span class=variable>Type</span> <span class=variable>v</span><span class=punctuation>)</span> <span class=keyword>where</span>
  <span class=operator>|</span> <span class=variable>leaf</span>
  <span class=operator>|</span> <span class=variable>node</span> <span class=punctuation>(</span><span class=variable>left</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span> <span class=punctuation>(</span><span class=variable>key</span> <span class=punctuation>:</span> <span class=variable>Nat</span><span class=punctuation>)</span> <span class=punctuation>(</span><span class=variable>value</span> <span class=punctuation>:</span> <span class=variable>β</span><span class=punctuation>)</span> <span class=punctuation>(</span><span class=variable>right</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span>
  <span class=keyword>deriving</span> <span class=variable>Repr</span></code></pre><p>And, skipping over a few things, both a naïve <code>toList</code> implementation and a tail-recursive one:</p><pre class=syntax-highlighting><code class=language-lean><span class=keyword>def</span> <span class=variable>Tree</span><span class=operator>.</span><span class=variable>toList</span> <span class=punctuation>(</span><span class=variable>t</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span> <span class=punctuation>:</span> <span class=variable>List</span> <span class=punctuation>(</span><span class=variable>Nat</span> × <span class=variable>β</span><span class=punctuation>)</span> <span class=operator>:=</span>
  <span class=keyword>match</span> <span class=variable>t</span> <span class=keyword>with</span>
  <span class=operator>|</span> <span class=variable>leaf</span> <span class=operator>=></span> <span class=punctuation>[]</span>
  <span class=operator>|</span> <span class=variable>node</span> <span class=variable>l</span> <span class=variable>k</span> <span class=variable>v</span> <span class=variable>r</span> <span class=operator>=></span> <span class=variable>l</span><span class=operator>.</span><span class=variable>toList</span> ++ <span class=punctuation>[(</span><span class=variable>k</span><span class=punctuation>,</span> <span class=variable>v</span><span class=punctuation>)]</span> ++ <span class=variable>r</span><span class=operator>.</span><span class=variable>toList</span>

<span class=keyword>def</span> <span class=variable>Tree</span><span class=operator>.</span><span class=variable>toListTR</span> <span class=punctuation>(</span><span class=variable>t</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span> <span class=punctuation>:</span> <span class=variable>List</span> <span class=punctuation>(</span><span class=variable>Nat</span> × <span class=variable>β</span><span class=punctuation>)</span> <span class=operator>:=</span>
  <span class=variable>go</span> <span class=variable>t</span> <span class=punctuation>[]</span>
<span class=variable>where</span>
  <span class=variable>go</span> <span class=punctuation>(</span><span class=variable>t</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span> <span class=punctuation>(</span><span class=variable>acc</span> <span class=punctuation>:</span> <span class=variable>List</span> <span class=punctuation>(</span><span class=variable>Nat</span> × <span class=variable>β</span><span class=punctuation>))</span> <span class=punctuation>:</span> <span class=variable>List</span> <span class=punctuation>(</span><span class=variable>Nat</span> × <span class=variable>β</span><span class=punctuation>)</span> <span class=operator>:=</span>
    <span class=keyword>match</span> <span class=variable>t</span> <span class=keyword>with</span>
    <span class=operator>|</span> <span class=variable>leaf</span> <span class=operator>=></span> <span class=variable>acc</span>
    <span class=operator>|</span> <span class=variable>node</span> <span class=variable>l</span> <span class=variable>k</span> <span class=variable>v</span> <span class=variable>r</span> <span class=operator>=></span> <span class=variable>go</span> <span class=variable>l</span> <span class=punctuation>((</span><span class=variable>k</span><span class=punctuation>,</span> <span class=variable>v</span><span class=punctuation>)</span> :: <span class=variable>go</span> <span class=variable>r</span> <span class=variable>acc</span><span class=punctuation>)</span></code></pre><p>Neat! Good to see that a functional programming language can in fact do functional programming things. Wait, what's this next paragraph:</p><blockquote>We now prove that <code>t.toList</code> and <code>t.toListTR</code> return the same list. The proof is on induction, and as we used the auxiliary function <code>go</code> to define <code>Tree.toListTR</code>, we use the auxiliary theorem <code>go</code> to prove the theorem.</blockquote><p>oh...</p><pre class=syntax-highlighting><code class=language-lean><span class=keyword>theorem</span> <span class=variable>Tree</span><span class=operator>.</span><span class=variable>toList_eq_toListTR</span> <span class=punctuation>(</span><span class=variable>t</span> <span class=punctuation>:</span> <span class=variable>Tree</span> <span class=variable>β</span><span class=punctuation>)</span>
        <span class=punctuation>:</span> <span class=variable>t</span><span class=operator>.</span><span class=variable>toList</span> <span class=operator>=</span> <span class=variable>t</span><span class=operator>.</span><span class=variable>toListTR</span> <span class=operator>:=</span> <span class=function>by
  simp </span><span class=punctuation>[</span><span class=variable>toListTR</span><span class=punctuation>,</span><span class=function> </span><span class=variable>go</span><span class=function> </span><span class=variable>t</span><span class=function> </span><span class=punctuation>[]]</span><span class=function>
</span><span class=variable>where</span><span class=function>
  </span><span class=variable>go</span><span class=function> </span><span class=punctuation>(</span><span class=variable>t</span><span class=function> </span><span class=punctuation>:</span><span class=function> </span><span class=variable>Tree</span><span class=function> </span><span class=variable>β</span><span class=punctuation>)</span><span class=function> </span><span class=punctuation>(</span><span class=variable>acc</span><span class=function> </span><span class=punctuation>:</span><span class=function> </span><span class=variable>List</span><span class=function> </span><span class=punctuation>(</span><span class=variable>Nat</span><span class=function> × </span><span class=variable>β</span><span class=punctuation>))</span><span class=function>
     </span><span class=punctuation>:</span><span class=function> </span><span class=variable>toListTR</span><span class=operator>.</span><span class=variable>go</span><span class=function> </span><span class=variable>t</span><span class=function> </span><span class=variable>acc</span> <span class=operator>=</span> <span class=variable>t</span><span class=operator>.</span><span class=variable>toList</span> ++ <span class=variable>acc</span> <span class=operator>:=</span> <span class=function>by
    </span><span class=variable>induction</span> <span class=variable>t</span> <span class=variable>generalizing</span> <span class=variable>acc</span> <span class=operator>&lt;</span><span class=punctuation>;</span><span class=operator>></span>
      <span class=variable>simp</span> <span class=punctuation>[</span><span class=variable>toListTR</span><span class=operator>.</span><span class=variable>go</span><span class=punctuation>,</span> <span class=variable>toList</span><span class=punctuation>,</span> <span class=operator>*</span><span class=punctuation>,</span> <span class=variable>List</span><span class=operator>.</span><span class=variable>append_assoc</span><span class=punctuation>]</span></code></pre><blockquote>The <code>[csimp]</code> annotation instructs the Lean code generator to replace any <code>Tree.toList</code> with <code>Tree.toListTR</code> when generating code.</blockquote><p>oh???</p><pre class=syntax-highlighting><code class=language-lean><span class=attribute>@[</span><span class=variable>csimp</span><span class=punctuation>]</span> <span class=keyword>theorem</span> <span class=variable>Tree</span><span class=operator>.</span><span class=variable>toList_eq_toListTR_csimp</span>
                 <span class=punctuation>:</span> @<span class=variable>Tree</span><span class=operator>.</span><span class=variable>toList</span> <span class=operator>=</span> @<span class=variable>Tree</span><span class=operator>.</span><span class=variable>toListTR</span> <span class=operator>:=</span> <span class=function>by
  </span><span class=variable>funext</span><span class=function> </span><span class=variable>β</span><span class=function> </span><span class=variable>t</span><span class=function>
  </span><span class=function>apply </span><span class=variable>toList_eq_toListTR</span></code></pre><p>Like i knew that this was the "functional programming used for proving things" language, but such a tight integration w/ the compiler itself is very cool. Formal verification ftw!</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    </feed>
  