(*<*) theory Solution imports Main begin (*>*) section {* Two Grammars *} text{* The most natural definition of valid sequences of parentheses is this: \[ S \quad\to\quad \epsilon \quad\mid\quad '('~S~')' \quad\mid\quad S~S \] where $\epsilon$ is the empty word. A second, somewhat unusual grammar is the following one: \[ T \quad\to\quad \epsilon \quad\mid\quad T~'('~T~')' \] *} text {* $\rhd$ Make the definitions of sets @{term S} and @{term T} in Isabelle and give structured Isar proofs leading to the theorem @{term "S = T"}. Hint: use lists over a specific type to repesent words. *} text {* The alphabet: *} datatype alpha = A | B text {* Standard grammar: *} inductive_set S :: "alpha list set" where S1: "[] \ S" | S2: "w \ S \ [A] @ w @ [B] \ S" | S3: "v \ S \ w \ S \ v @ w \ S" text {* Nonstandard grammar: *} inductive_set T :: "alpha list set" where T1: "[] \ T" | T23: "v \ T \ w \ T \ v @ ([A] @ w @ [B]) \ T" text {* Equivalence proof *} lemma T_in_S: "w \ T \ w \ S" proof (induct set: T) case T1 show "[] \ S" by (rule S1) next case (T23 v w) have "v \ S" . moreover { have "w \ S" . then have "[A] @ w @ [B] \ S" by (rule S2) } ultimately show "v @ ([A] @ w @ [B]) \ S" by (rule S3) qed lemma T2: "w \ T \ [A] @ w @ [B] \ T" proof - have "[] \ T" by (rule T1) moreover assume "w \ T" ultimately have "[] @ ([A] @ w @ [B]) \ T" by (rule T23) then show ?thesis by simp qed lemma T3: assumes u: "u \ T" and v: "v \ T" shows "u @ v \ T" using v proof induct case T1 from u show "u @ [] \ T" by simp next case (T23 v w) have "u @ v \ T" . moreover have "w \ T" . ultimately have "(u @ v) @ ([A] @ w @ [B]) \ T" by (rule T.T23) then show "u @ (v @ [A] @ w @ [B]) \ T" by simp qed lemma S_in_T: "w \ S \ w \ T" proof (induct set: S) case S1 show "[] \ T" by (rule T1) next case (S2 w) have "w \ T" . then show "[A] @ w @ [B] \ T" by (rule T2) next case (S3 v w) have "v \ T" and "w \ T" . then show "v @ w \ T" by (rule T3) qed theorem "S = T" using S_in_T T_in_S by blast section {* Polynomial sums *} text {* $\rhd$ Produce structured proofs of the following theorems, using induction and calculational reasoning in Isar. Note that the given tactic scripts are of limited use in reconstructing structured proofs; nevertheless the hints of automated steps below can be re-used to finish sub-problems. The @{text "\"} symbol can be entered as ``\verb,\,''; note that numerals in Isabelle/HOL are polymorphic. *} theorem -- {* problem *} fixes n :: nat shows "2 * (\i=0..n. i) = n * (n + 1)" by (induct n) simp_all theorem -- {* solution *} fixes n :: nat shows "2 * (\i=0..n. i) = n * (n + 1)" proof (induct n) case 0 have "2 * (\i=0..0. i) = (0::nat)" by simp also have "(0::nat) = 0 * (0 + 1)" by simp finally show ?case . next case (Suc n) have "2 * (\i=0..Suc n. i) = 2 * (\i=0..n. i) + 2 * (n + 1)" by simp also have "2 * (\i=0..n. i) = n * (n + 1)" by (rule Suc.hyps) also have "n * (n + 1) + 2 * (n + 1) = Suc n * (Suc n + 1)" by simp finally show ?case . qed theorem -- {* problem *} fixes n :: nat shows "(\i=0.." by (induct n) (simp_all add: power_eq_if nat_distrib) theorem -- {* solution *} fixes n :: nat shows "(\i=0.." proof (induct n) case 0 have "(\i=0..<0. 2 * i + 1) = (0::nat)" by simp also have "(0::nat) = 0\" by simp finally show ?case . next case (Suc n) have "(\i=0..i=0..i=0.." by (rule Suc.hyps) also have "n^2 + 2 * n + 1 = (Suc n)\" by (simp add: power_eq_if nat_distrib) finally show ?case . qed theorem -- {* problem *} fixes n :: nat shows "(\i=0..i=0..i=0..<0. 2^i) = (0::nat)" by simp also have "(0::nat) = 2^0 - (1::nat)" by simp finally show ?case . next case (Suc n) have "(\i=0..i=0..i=0..i=0..i=0..i=0..<0. 3^i) = (0::nat)" by simp also have "(0::nat) = 3^0 - (1::nat)" by simp finally show ?case . next case (Suc n) have "(2::nat) * (\i=0..i=0..i=0..i=0..i=0..i=0..<0. k^i) = (0::nat)" by simp also have "(0::nat) = k^0 - (1::nat)" by simp finally show ?case . next case (Suc n) have "(k - 1) * (\i=0..i=0..i=0..*)