26 lemma "sum n + sum n = n*(Suc n)" |
26 lemma "sum n + sum n = n*(Suc n)" |
27 apply(induct_tac n) |
27 apply(induct_tac n) |
28 apply(auto) |
28 apply(auto) |
29 done |
29 done |
30 |
30 |
31 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n" |
31 text{* |
|
32 Some examples of linear arithmetic: |
|
33 *} |
|
34 |
|
35 lemma "\<lbrakk> \<not> m < n; m < n+(1::int) \<rbrakk> \<Longrightarrow> m = n" |
32 by(auto) |
36 by(auto) |
33 |
37 |
34 lemma "min i (max j k) = max (min k i) (min i (j::nat))" |
38 lemma "min i (max j k) = max (min k i) (min i (j::nat))" |
35 by(arith) |
39 by(arith) |
36 |
40 |
37 lemma "n*n = n \<Longrightarrow> n=0 \<or> n=1" |
41 text{* |
38 oops |
42 Not proved automatically because it involves multiplication: |
|
43 *} |
|
44 |
|
45 lemma "n*n = n \<Longrightarrow> n=0 \<or> n=(1::int)" |
|
46 (*<*)oops(*>*) |
39 |
47 |
40 |
48 |
41 subsubsection{*Pairs*} |
49 subsubsection{*Pairs*} |
42 |
50 |
43 lemma "fst(x,y) = snd(z,x)" |
51 lemma "fst(x,y) = snd(z,x)" |
65 subsubsection{*Simplification Rules*} |
73 subsubsection{*Simplification Rules*} |
66 |
74 |
67 lemma fst_conv[simp]: "fst(x,y) = x" |
75 lemma fst_conv[simp]: "fst(x,y) = x" |
68 by auto |
76 by auto |
69 |
77 |
|
78 text{* |
|
79 Setting and resetting the @{text simp} attribute: |
|
80 *} |
|
81 |
70 declare fst_conv[simp] |
82 declare fst_conv[simp] |
71 declare fst_conv[simp del] |
83 declare fst_conv[simp del] |
72 |
84 |
73 |
85 |
74 subsubsection{*The Simplification Method*} |
86 subsubsection{*The Simplification Method*} |
75 |
87 |
76 lemma "x*(y+1) = y*(x+1)" |
88 lemma "x*(y+1) = y*(x+1::nat)" |
77 apply simp |
89 apply simp |
78 oops |
90 (*<*)oops(*>*) |
79 |
91 |
80 |
92 |
81 subsubsection{*Adding and Deleting Simplification Rules*} |
93 subsubsection{*Adding and Deleting Simplification Rules*} |
82 |
94 |
83 lemma "\<forall>x::nat. x*(y+z) = r" |
95 lemma "\<forall>x::nat. x*(y+z) = r" |
84 apply (simp add: add_mult_distrib2) |
96 apply (simp add: add_mult_distrib2) |
85 oops |
97 (*<*)oops(*>*)text_raw {* \isanewline\isanewline *} |
86 |
98 |
87 lemma "rev(rev(xs @ [])) = xs" |
99 lemma "rev(rev(xs @ [])) = xs" |
88 apply (simp del: rev_rev_ident) |
100 apply (simp del: rev_rev_ident) |
89 oops |
101 (*<*)oops(*>*) |
90 |
|
91 |
102 |
92 subsubsection{*Assumptions*} |
103 subsubsection{*Assumptions*} |
93 |
104 |
94 lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs" |
105 lemma "\<lbrakk> xs @ zs = ys @ xs; [] @ xs = [] @ [] \<rbrakk> \<Longrightarrow> ys = zs" |
95 apply simp |
106 by simp |
96 done |
|
97 |
107 |
98 lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []" |
108 lemma "\<forall>x. f x = g (f (g x)) \<Longrightarrow> f [] = f [] @ []" |
99 apply(simp (no_asm)) |
109 by(simp (no_asm)) |
100 done |
|
101 |
|
102 |
110 |
103 subsubsection{*Rewriting with Definitions*} |
111 subsubsection{*Rewriting with Definitions*} |
104 |
112 |
105 lemma "xor A (\<not>A)" |
113 lemma "xor A (\<not>A)" |
106 apply(simp only: xor_def) |
114 apply(simp only: xor_def) |
|
115 apply simp |
|
116 done |
|
117 |
|
118 |
|
119 subsubsection{*Conditional Equations*} |
|
120 |
|
121 lemma hd_Cons_tl[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs" |
|
122 by(case_tac xs, simp_all) |
|
123 |
|
124 lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs" |
107 by simp |
125 by simp |
108 |
126 |
109 |
127 |
110 subsubsection{*Conditional Equations*} |
|
111 |
|
112 lemma hd_Cons_tl[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs" |
|
113 apply(case_tac xs, simp, simp) |
|
114 done |
|
115 |
|
116 lemma "xs \<noteq> [] \<Longrightarrow> hd(rev xs) # tl(rev xs) = rev xs" |
|
117 by(simp) |
|
118 |
|
119 |
|
120 subsubsection{*Automatic Case Splits*} |
128 subsubsection{*Automatic Case Splits*} |
121 |
129 |
122 lemma "\<forall>xs. if xs = [] then A else B" |
130 lemma "\<forall>xs. if xs = [] then A else B" |
123 apply simp |
131 apply simp |
124 oops |
132 (*<*)oops(*>*)text_raw {* \isanewline\isanewline *} |
125 |
133 |
126 lemma "case xs @ [] of [] \<Rightarrow> P | y#ys \<Rightarrow> Q ys y" |
134 lemma "case xs @ [] of [] \<Rightarrow> P | y#ys \<Rightarrow> Q ys y" |
127 apply simp |
135 apply simp |
128 apply(simp split: list.split) |
136 apply(simp split: list.split) |
129 oops |
137 (*<*)oops(*>*) |
130 |
138 |
131 |
139 |
132 subsubsection{*Arithmetic*} |
140 subsubsection{*Arithmetic*} |
133 |
141 |
134 lemma "\<lbrakk> \<not> m < n; m < n+1 \<rbrakk> \<Longrightarrow> m = n" |
142 text{* |
|
143 Is simple enough for the default arithmetic: |
|
144 *} |
|
145 lemma "\<lbrakk> \<not> m < n; m < n+(1::nat) \<rbrakk> \<Longrightarrow> m = n" |
135 by simp |
146 by simp |
136 |
147 |
137 lemma "\<not> m < n \<and> m < n+1 \<Longrightarrow> m = n" |
148 text{* |
|
149 Contains boolean combinations and needs full arithmetic: |
|
150 *} |
|
151 lemma "\<not> m < n \<and> m < n+(1::nat) \<Longrightarrow> m = n" |
138 apply simp |
152 apply simp |
139 by(arith) |
153 by(arith) |
140 |
154 |
141 |
155 (*<*) |
142 subsubsection{*Tracing*} |
156 subsubsection{*Tracing*} |
143 |
157 |
144 lemma "rev [a] = []" |
158 lemma "rev [a] = []" |
145 apply(simp) |
159 apply(simp) |
146 oops |
160 oops |
|
161 (*>*) |
147 |
162 |
148 |
163 |
149 |
164 |
150 subsection{*Case Study: Compiling Expressions*} |
165 subsection{*Case Study: Compiling Expressions*} |
151 |
166 |