theory RulesOfTheGame2InClass
imports Main
begin
(* case analysis using moreover/ultimately *)
theorem "(p \ q) \ (p \ r) \ (q \ s) \ r \ s"
proof (rule impI)
assume 1: "(p \ q) \ (p \ r) \ (q \ s)"
thm disjE
from 1 have "p \ q" by blast
moreover { assume 2: "p"
have 3: "r"
proof -
from 1 have 3: "p \ r" by blast
from 3 2 show "r" by (rule mp)
qed
from 3 have "r \ s" by (rule disjI1)
} moreover { assume 3: "q"
from 1 3 have 4: "s" by blast
from 4 have "r \ s" by (rule disjI2)
} ultimately show "r \ s" by (rule disjE)
qed
definition even :: "nat \ bool" where
"even n == \ m. n = 2 * m"
definition odd :: "nat \ bool" where
"odd n == \ m. n = 2 * m + 1"
theorem "\ odd n; odd m \ \ even (n + m)"
apply (simp add: odd_def even_def)
apply (erule exE)+
apply (rule_tac x="ma + maa + 1" in exI)
apply simp
done
theorem assumes 1: "odd n" and 2: "odd m"
shows "even (n + m)"
proof -
from 1 have 3: "\ m. n = 2 * m + 1" unfolding odd_def by assumption
from 3 obtain k where 4: "n = 2 * k + 1" by (rule exE)
from 2 have 5: "\ p. m = 2 * p + 1" unfolding odd_def by assumption
from 5 obtain p where 6: "m = 2 * p + 1" by (rule exE)
from 4 6 have 7: "n + m = (2 * k + 1) + (2 * p + 1)" by simp
also have "\ = 2 * (k + p + 1)" by simp
finally have "n + m = 2 * (k + p + 1)" by simp
from this have "\ q. n + m = 2 * q" by (rule exI)
from this show "even (n + m)" unfolding even_def by assumption
qed
datatype tree = Tip | Node tree nat tree
primrec flatten :: "tree \ nat list" where
"flatten Tip = []" |
"flatten (Node l x r) = (flatten l @ [x] @ flatten r)"
primrec flatten_iter :: "tree \ nat list \ nat list" where
"flatten_iter Tip ls = ls" |
"flatten_iter (Node l x r) ls = flatten_iter l (x # (flatten_iter r ls))"
lemma helper: "flatten_iter t ls = flatten t @ ls"
proof (induct t arbitrary: ls)
fix t1 x t2 ls
assume IH1: "\ls. flatten_iter t1 ls = flatten t1 @ ls"
and IH2: "\ls. flatten_iter t2 ls = flatten t2 @ ls"
have "flatten_iter (Node t1 x t2) ls = flatten_iter t1 (x # (flatten_iter t2 ls))"
by simp
also from IH2 have "\ = flatten_iter t1 (x # (flatten t2 @ ls))" by simp
also from IH1 have "\ = flatten t1 @ (x # (flatten t2 @ ls))" by simp
finally
show "flatten_iter (Node t1 x t2) ls = flatten (Node t1 x t2) @ ls" by simp
next
fix ls
show "flatten_iter Tip ls = flatten Tip @ ls" by simp
qed
theorem "flatten_iter t [] = flatten t"
by (simp add: helper)
end