Last active
January 16, 2024 10:41
-
-
Save thelissimus/fbaeb2d01f64c60279c9363ff362dd4c to your computer and use it in GitHub Desktop.
Hiccup renderer in Lean 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inductive Hiccup : Type | |
| tag : String -> List (String × String) -> List Hiccup -> Hiccup | |
| value : String -> Hiccup | |
deriving Repr | |
open Hiccup | |
partial def render : Hiccup -> String | |
| tag name attrs rest => s!"<{name} {buildAttrs attrs}>{rest.map render |> String.join}</{name}>" | |
| value s => s | |
where | |
buildAttrs (kvs : List (String × String)) : String := | |
kvs.map (λ (k, v) => s!"\"{k}\"=\"{v}\"") |> String.intercalate " " | |
#eval render $ tag "div" [("class", "foo")] [tag "span" [] [value "hello"], value "world"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment