RosettaCodeData/Task/Flatten-a-list/Isabelle/flatten-a-list.isabelle
2023-07-01 13:44:08 -04:00

34 lines
855 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

theory Scratch
imports Main
begin
datatype 'a tree = Leaf 'a ("<_>")
| Node "'a tree list" ("⟦ _ ⟧")
textThe datatype introduces special pretty printing:
lemma "Leaf a = <a>" by simp
lemma "Node [] = ⟦ [] ⟧" by simp
definition "example ≡ ⟦[ ⟦[<1>]⟧, <2>, ⟦[ ⟦[<3>, <4>]⟧, <5>]⟧, ⟦[⟦[⟦[]⟧]⟧]⟧, ⟦[⟦[⟦[<6>]⟧]⟧]⟧, <7>, <8>, ⟦[]⟧ ]⟧"
lemma "example =
Node [
Node [Leaf 1],
Leaf 2,
Node [Node [Leaf 3, Leaf 4], Leaf 5],
Node [Node [ Node []]],
Node [Node [Node [Leaf 6]]],
Leaf 7,
Leaf 8,
Node []
]"
by(simp add: example_def)
fun flatten :: "'a tree ⇒ 'a list" where
"flatten (Leaf a) = [a]"
| "flatten (Node xs) = concat (map flatten xs)"
lemma "flatten example = [1, 2, 3, 4, 5, 6, 7, 8]"
by(simp add: example_def)
end