section \Solutions for Session 3\ theory Solutions04 imports Demo04 begin subsection \Exercise 1\ text \ Prove that @{const Demo04.add} is associative and commutative. \ lemma add_Suc_left [simp]: "add (Suc x) y = Suc (add x y)" by (induction y) (simp_all only: add.simps) lemma add_assoc [simp]: "add (add x y) z = add x (add y z)" by (induction y) simp_all lemma add_commute: "add x y = add y x" by (induction y) simp_all subsection \Exercise 2\ text \ Prove that @{const Demo04.mul} can also be computed via recursion on its first argument. \ lemma mul0_left [simp]: "mul 0 y = 0" by (induction y) simp_all lemma mul_Suc_left [simp]: "mul (Suc x) y = add y (mul x y)" by (induction y) simp_all subsection \Exercise 3\ text \ Define a recursive function \div2\ that computes "division by 2" and prove that \m + n/2 = (2m + n)/2\ \ fun div2 :: "nat \ nat" where "div2 0 = 0" | "div2 (Suc 0) = 0" | "div2 (Suc (Suc x)) = Suc (div2 x)" lemma div2_add_self [simp]: "div2 (add m m) = m" by (induction m) simp_all lemma div2_Suc_add_self [simp]: "div2 (Suc (add m m)) = m" by (induction m) simp_all lemma add_div2: "add m (div2 n) = div2 (add (mul (Suc (Suc 0)) m) n)" by (induction m) simp_all subsection \Exercise 4\ text \ Only using @{const Demo04.add} and @{const Demo04.mul}, define a function \sum\ such that \sum n = 0 + 1 + 2 + \ + n\ and prove Gauss's equation. \ fun sum :: "nat \ nat" where "sum 0 = 0" | "sum (Suc n) = add (Suc n) (sum n)" lemma "sum n = div2 (mul n (Suc n))" proof (induction n) case 0 have "sum 0 = 0" unfolding sum.simps .. also have "\ = div2 (mul 0 (Suc 0))" unfolding mul0_left and div2.simps .. finally show ?case . next case IH: (Suc n) have "sum (Suc n) = add (Suc n) (sum n)" unfolding sum.simps .. also have "\ = add (Suc n) (div2 (mul n (Suc n)))" unfolding IH .. also have "\ = add (Suc n) (div2 (add (mul n n) n))" unfolding mul.simps .. also have "\ = div2 (add (mul (Suc (Suc 0)) (Suc n)) (add (mul n n) n))" unfolding add_div2 .. finally show ?case by (simp add: add_commute) qed end