Skip to content

Instantly share code, notes, and snippets.

@thelissimus
Last active January 16, 2024 10:41
Show Gist options
  • Save thelissimus/fbaeb2d01f64c60279c9363ff362dd4c to your computer and use it in GitHub Desktop.
Save thelissimus/fbaeb2d01f64c60279c9363ff362dd4c to your computer and use it in GitHub Desktop.
Hiccup renderer in Lean 4
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