theory Overview_Demo imports Main begin section \A simple recursive function example\ fun add :: "nat \ nat \ nat" where "add 0 n = n" | "add (Suc m) n = Suc (add m n)" text \"Suc" is the successor constructor for natural numbers.\ text \Here's a simple lemma about this function, proved by induction:\ lemma add_02: "add m 0 = m" apply(induction m) apply (auto) done thm add_02 text \Executing the above function:\ value "add 5 3" text \... or generating code in a language like Haskell from it:\ export_code add in Haskell (* A simple comment *) text \This is also a comment but it generates nice \LaTeX-text!\ section \Types and terms\ term True text \Note that variables and constants (eg True, &) are displayed differently.\ term False term true term "True & False" (* compound terms and formulas need to enclosed in quotes *) term "True \ False" term "True \ false" value "True \ False" value "True \ P" value "if True then x else y" value "(if True then x else y) = x" (* if subterms need to enclosed in parentheses *) value "case n of 0 \ 0 | Suc n \ n" value "(case n of 0 \ 0 | Suc n \ n) \ n" (* same for case subterms *) value "A \ B = A \ B" (* equality has higher precedence than conjunction *) value "(A \ B) = (A \ B)" value "A \ B \ A \ B" (* conjunction has lower precedence than if-and-only-if *) (* To display types in jedit: press ctrl (Mac: cmd) and hover over text. To view the definition of a constant: press ctrl (Mac: cmd) and click on the text. *) text \Warning: "+" and numbers are overloaded:\ term "n + n" term "n + n = 0" term "(n::nat) + n = 0" text \Numbers need type annotations unless the type is clear from the context, e.g.:\ term "Suc n" (*Try this: term "True + False" Terms must be type correct!*) text \Type inference:\ term "f (g x) y" term "h (%x. f(g x))" term "\x. x + x" text \Currying of function arguments (e.g. @{typ "int \ int \ int"}} instead of tupling them (e.g. @{typ "(int \ int) \ int"}) allows for partial application\ term "add" term "add 3" term map term "map (add 3)" value "map (add 3) [0, 1, 2]" section \More on proofs\ text \General schema: \lemma name[attributes]: "..." apply (...) apply (...) done\ Lemma names and attributes are optional. Adding the \simp\ attribute to a lemma will make the simplifier use it as a rewrite rule by default. The \oops\ command can be used to abort a proof attempt. The \sorry\ command can be used to make Isabelle accept a lemma without a complete proof. This can be useful for top-down proof development: assume some lemmas using \sorry\ while working on a higher-level proof, then come back later to finish up the remaining lemmas. The \apply\ commands take proof methods like \auto\, \induction\, or \simp\.\ lemma "add m 0 = m" apply(induction m) apply (simp) (* unlike auto, simp only works on the first subgoal, leaving any others untouched *) (* proof state at this point: goal (1 subgoal): 1. \m. add m 0 = m \ add (Suc m) 0 = Suc m One remaining subgoal "add (Suc m) 0 = Suc m" with a locally fixed variable "m" and one assumption "add m 0 = m". Multiple assumptions are written using multiple arrows, e.g. "A1 \ A2 \ A3 \ B". *) by auto (* "by" can also finish a proof, taking one or two proof methods *) end