20319
|
1 |
(*
|
|
2 |
ID: $Id$
|
|
3 |
Author: Amine Chaieb, TU Muenchen
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Examples for generic reflection and reification *}
|
|
7 |
|
|
8 |
theory ReflectionEx
|
|
9 |
imports Reflection
|
|
10 |
begin
|
|
11 |
|
|
12 |
|
|
13 |
(* The type fm represents simple propositional formulae *)
|
|
14 |
datatype fm = And fm fm | Or fm fm | Imp fm fm | Iff fm fm | NOT fm | At nat
|
|
15 |
|
|
16 |
consts Ifm :: "bool list \<Rightarrow> fm \<Rightarrow> bool"
|
|
17 |
primrec
|
|
18 |
"Ifm vs (At n) = vs!n"
|
|
19 |
"Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
|
|
20 |
"Ifm vs (Or p q) = (Ifm vs p \<or> Ifm vs q)"
|
|
21 |
"Ifm vs (Imp p q) = (Ifm vs p \<longrightarrow> Ifm vs q)"
|
|
22 |
"Ifm vs (Iff p q) = (Ifm vs p = Ifm vs q)"
|
|
23 |
"Ifm vs (NOT p) = (\<not> (Ifm vs p))"
|
|
24 |
|
|
25 |
consts fmsize :: "fm \<Rightarrow> nat"
|
|
26 |
primrec
|
|
27 |
"fmsize (At n) = 1"
|
|
28 |
"fmsize (NOT p) = 1 + fmsize p"
|
|
29 |
"fmsize (And p q) = 1 + fmsize p + fmsize q"
|
|
30 |
"fmsize (Or p q) = 1 + fmsize p + fmsize q"
|
|
31 |
"fmsize (Imp p q) = 2 + fmsize p + fmsize q"
|
|
32 |
"fmsize (Iff p q) = 2 + 2* fmsize p + 2* fmsize q"
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
(* reify maps a bool to an fm, for this it needs the
|
|
37 |
semantics of fm (Ifm.simps)*)
|
|
38 |
lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
|
|
39 |
apply (reify Ifm.simps)
|
|
40 |
oops
|
|
41 |
|
|
42 |
(* You can also just pick up a subterm to reify \<dots> *)
|
|
43 |
lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
|
|
44 |
apply (reify Ifm.simps ("((~ D) & (~ F))"))
|
|
45 |
oops
|
|
46 |
|
|
47 |
(* Let's perform NNF, A version that tends to disjunctions *)
|
|
48 |
consts nnf :: "fm \<Rightarrow> fm"
|
|
49 |
recdef nnf "measure fmsize"
|
|
50 |
"nnf (At n) = At n"
|
|
51 |
"nnf (And p q) = And (nnf p) (nnf q)"
|
|
52 |
"nnf (Or p q) = Or (nnf p) (nnf q)"
|
|
53 |
"nnf (Imp p q) = Or (nnf (NOT p)) (nnf q)"
|
|
54 |
"nnf (Iff p q) = Or (And (nnf p) (nnf q)) (And (nnf (NOT p)) (nnf (NOT q)))"
|
|
55 |
"nnf (NOT (And p q)) = Or (nnf (NOT p)) (nnf (NOT q))"
|
|
56 |
"nnf (NOT (Or p q)) = And (nnf (NOT p)) (nnf (NOT q))"
|
|
57 |
"nnf (NOT (Imp p q)) = And (nnf p) (nnf (NOT q))"
|
|
58 |
"nnf (NOT (Iff p q)) = Or (And (nnf p) (nnf (NOT q))) (And (nnf (NOT p)) (nnf q))"
|
|
59 |
"nnf (NOT (NOT p)) = nnf p"
|
|
60 |
"nnf (NOT p) = NOT p"
|
|
61 |
|
|
62 |
(* nnf preserves the semantics of fm *)
|
|
63 |
lemma nnf: "Ifm vs (nnf p) = Ifm vs p"
|
|
64 |
by (induct p rule: nnf.induct) auto
|
|
65 |
|
|
66 |
(* Now let's perform NNF using our function defined above.
|
|
67 |
reflection takes the correctness theorem (nnf) the semantics of fm
|
|
68 |
and applies the function to the corresponding of the formula *)
|
|
69 |
lemma "(\<not> (A = B)) \<and> (B \<longrightarrow> (A \<noteq> (B | C \<and> (B \<longrightarrow> A | D)))) \<longrightarrow> A \<or> B \<and> D"
|
|
70 |
apply (reflection nnf Ifm.simps)
|
|
71 |
oops
|
|
72 |
|
|
73 |
(* Here also you can just pick up a subterm \<dots> *)
|
|
74 |
lemma "(\<not> (A = B)) \<and> (B \<longrightarrow> (A \<noteq> (B | C \<and> (B \<longrightarrow> A | D)))) \<longrightarrow> A \<or> B \<and> D"
|
|
75 |
apply (reflection nnf Ifm.simps ("(B | C \<and> (B \<longrightarrow> A | D))"))
|
|
76 |
oops
|
|
77 |
|
|
78 |
|
|
79 |
(* Example 2 : simple arithmetics formulae *)
|
|
80 |
|
|
81 |
(* num reflects linear expressions over natural number *)
|
|
82 |
datatype num = C nat | Add num num | Mul nat num | Var nat | CN nat nat num
|
|
83 |
|
|
84 |
consts num_size :: "num \<Rightarrow> nat"
|
|
85 |
primrec
|
|
86 |
"num_size (C c) = 1"
|
|
87 |
"num_size (Var n) = 1"
|
|
88 |
"num_size (Add a b) = 1 + num_size a + num_size b"
|
|
89 |
"num_size (Mul c a) = 1 + num_size a"
|
|
90 |
"num_size (CN n c a) = 4 + num_size a "
|
|
91 |
|
|
92 |
(* The semantics of num *)
|
|
93 |
consts Inum:: "nat list \<Rightarrow> num \<Rightarrow> nat"
|
|
94 |
primrec
|
|
95 |
Inum_C : "Inum vs (C i) = i"
|
|
96 |
Inum_Var: "Inum vs (Var n) = vs!n"
|
|
97 |
Inum_Add: "Inum vs (Add s t) = Inum vs s + Inum vs t"
|
|
98 |
Inum_Mul: "Inum vs (Mul c t) = c * Inum vs t"
|
|
99 |
Inum_CN : "Inum vs (CN n c t) = c*(vs!n) + Inum vs t"
|
|
100 |
|
|
101 |
(* Let's reify some nat expressions \<dots> *)
|
|
102 |
lemma "4 * (2*x + (y::nat)) \<noteq> 0"
|
|
103 |
apply (reify Inum.simps ("4 * (2*x + (y::nat))"))
|
|
104 |
(* We're in a bad situation!!*)
|
|
105 |
oops
|
|
106 |
(* So let's leave the Inum_C equation at the end and see what happens \<dots>*)
|
|
107 |
lemma "4 * (2*x + (y::nat)) \<noteq> 0"
|
|
108 |
apply (reify Inum_Var Inum_Add Inum_Mul Inum_CN Inum_C ("4 * (2*x + (y::nat))"))
|
|
109 |
(* We're still in a bad situation!!*)
|
|
110 |
(* BUT!! It's better\<dots> Note that the reification depends on the order of the equations\<dots> *)
|
|
111 |
(* The problem is that Inum_C has the form : Inum vs (C i) = i *)
|
|
112 |
(* So the right handside matches any term of type nat, which makes things bad. *)
|
|
113 |
(* We want only numerals \<dots> So let's specialize Inum_C with numerals.*)
|
|
114 |
oops
|
|
115 |
|
|
116 |
lemma Inum_number: "Inum vs (C (number_of t)) = number_of t" by simp
|
|
117 |
lemmas Inum_eqs = Inum_Var Inum_Add Inum_Mul Inum_CN Inum_number
|
|
118 |
|
|
119 |
(* Second trial *)
|
|
120 |
lemma "1 * (2*x + (y::nat)) \<noteq> 0"
|
|
121 |
apply (reify Inum_eqs ("1 * (2*x + (y::nat))"))
|
|
122 |
oops
|
|
123 |
(* That was fine, so let's try an other one\<dots> *)
|
|
124 |
|
|
125 |
lemma "1 * (2*x + (y::nat) + 0 + 1) \<noteq> 0"
|
|
126 |
apply (reify Inum_eqs ("1 * (2*x + (y::nat) + 0 + 1)"))
|
|
127 |
(* Oh!! 0 is not a variable \<dots> Oh! 0 is not a number_of .. thing *)
|
|
128 |
(* Tha same for 1, so let's add those equations too *)
|
|
129 |
oops
|
|
130 |
|
|
131 |
lemma Inum_01: "Inum vs (C 0) = 0" "Inum vs (C 1) = 1" "Inum vs (C(Suc n)) = Suc n"
|
|
132 |
by simp+
|
|
133 |
lemmas Inum_eqs'= Inum_eqs Inum_01
|
|
134 |
(* Third Trial *)
|
|
135 |
|
|
136 |
lemma "1 * (2*x + (y::nat) + 0 + 1) \<noteq> 0"
|
|
137 |
apply (reify Inum_eqs' ("1 * (2*x + (y::nat) + 0 + 1)"))
|
|
138 |
(* Okay *)
|
|
139 |
oops
|
|
140 |
|
|
141 |
(* Some simplifications for num terms, you can skimm untill the main theorem linum *)
|
|
142 |
consts lin_add :: "num \<times> num \<Rightarrow> num"
|
|
143 |
recdef lin_add "measure (\<lambda>(x,y). ((size x) + (size y)))"
|
|
144 |
"lin_add (CN n1 c1 r1,CN n2 c2 r2) =
|
|
145 |
(if n1=n2 then
|
|
146 |
(let c = c1 + c2
|
|
147 |
in (if c=0 then lin_add(r1,r2) else CN n1 c (lin_add (r1,r2))))
|
|
148 |
else if n1 \<le> n2 then (CN n1 c1 (lin_add (r1,CN n2 c2 r2)))
|
|
149 |
else (CN n2 c2 (lin_add (CN n1 c1 r1,r2))))"
|
|
150 |
"lin_add (CN n1 c1 r1,t) = CN n1 c1 (lin_add (r1, t))"
|
|
151 |
"lin_add (t,CN n2 c2 r2) = CN n2 c2 (lin_add (t,r2))"
|
|
152 |
"lin_add (C b1, C b2) = C (b1+b2)"
|
|
153 |
"lin_add (a,b) = Add a b"
|
|
154 |
lemma lin_add: "Inum bs (lin_add (t,s)) = Inum bs (Add t s)"
|
|
155 |
apply (induct t s rule: lin_add.induct, simp_all add: Let_def)
|
|
156 |
apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
|
|
157 |
by (case_tac "n1 = n2", simp_all add: ring_eq_simps)
|
|
158 |
|
|
159 |
consts lin_mul :: "num \<Rightarrow> nat \<Rightarrow> num"
|
|
160 |
recdef lin_mul "measure size "
|
|
161 |
"lin_mul (C j) = (\<lambda> i. C (i*j))"
|
|
162 |
"lin_mul (CN n c a) = (\<lambda> i. if i=0 then (C 0) else CN n (i*c) (lin_mul a i))"
|
|
163 |
"lin_mul t = (\<lambda> i. Mul i t)"
|
|
164 |
|
|
165 |
lemma lin_mul: "\<And> i. Inum bs (lin_mul t i) = Inum bs (Mul i t)"
|
|
166 |
by (induct t rule: lin_mul.induct, auto simp add: ring_eq_simps)
|
|
167 |
|
|
168 |
consts linum:: "num \<Rightarrow> num"
|
|
169 |
recdef linum "measure num_size"
|
|
170 |
"linum (C b) = C b"
|
|
171 |
"linum (Var n) = CN n 1 (C 0)"
|
|
172 |
"linum (Add t s) = lin_add (linum t, linum s)"
|
|
173 |
"linum (Mul c t) = lin_mul (linum t) c"
|
|
174 |
"linum (CN n c t) = lin_add (linum (Mul c (Var n)),linum t)"
|
|
175 |
|
|
176 |
lemma linum : "Inum vs (linum t) = Inum vs t"
|
|
177 |
by (induct t rule: linum.induct, simp_all add: lin_mul lin_add)
|
|
178 |
|
|
179 |
(* Now we can use linum to simplify nat terms using reflection *)
|
|
180 |
lemma "(Suc (Suc 1)) * ((x::nat) + (Suc 1)*y) = 3*x + 6*y"
|
|
181 |
apply (reflection linum Inum_eqs' ("(Suc (Suc 1)) * ((x::nat) + (Suc 1)*y)"))
|
|
182 |
oops
|
|
183 |
|
|
184 |
(* Let's list this to formulae \<dots> *)
|
|
185 |
|
|
186 |
datatype aform = Lt num num | Eq num num | Ge num num | NEq num num |
|
|
187 |
Conj aform aform | Disj aform aform | NEG aform | T | F
|
|
188 |
consts linaformsize:: "aform \<Rightarrow> nat"
|
|
189 |
recdef linaformsize "measure size"
|
|
190 |
"linaformsize T = 1"
|
|
191 |
"linaformsize F = 1"
|
|
192 |
"linaformsize (Lt a b) = 1"
|
|
193 |
"linaformsize (Ge a b) = 1"
|
|
194 |
"linaformsize (Eq a b) = 1"
|
|
195 |
"linaformsize (NEq a b) = 1"
|
|
196 |
"linaformsize (NEG p) = 2 + linaformsize p"
|
|
197 |
"linaformsize (Conj p q) = 1 + linaformsize p + linaformsize q"
|
|
198 |
"linaformsize (Disj p q) = 1 + linaformsize p + linaformsize q"
|
|
199 |
|
|
200 |
|
|
201 |
consts aform :: "nat list => aform => bool"
|
|
202 |
primrec
|
|
203 |
"aform vs T = True"
|
|
204 |
"aform vs F = False"
|
|
205 |
"aform vs (Lt a b) = (Inum vs a < Inum vs b)"
|
|
206 |
"aform vs (Eq a b) = (Inum vs a = Inum vs b)"
|
|
207 |
"aform vs (Ge a b) = (Inum vs a \<ge> Inum vs b)"
|
|
208 |
"aform vs (NEq a b) = (Inum vs a \<noteq> Inum vs b)"
|
|
209 |
"aform vs (NEG p) = (\<not> (aform vs p))"
|
|
210 |
"aform vs (Conj p q) = (aform vs p \<and> aform vs q)"
|
|
211 |
"aform vs (Disj p q) = (aform vs p \<or> aform vs q)"
|
|
212 |
|
|
213 |
(* Let's reify and do reflection .. *)
|
|
214 |
lemma "(3::nat)*x + t < 0 \<and> (2 * x + y \<noteq> 17)"
|
|
215 |
apply (reify Inum_eqs' aform.simps)
|
|
216 |
oops
|
|
217 |
|
|
218 |
lemma "(3::nat)*x + t < 0 & x*x + t*x + 3 + 1 = z*t*4*z | x + x + 1 < 0"
|
|
219 |
apply (reflection linum Inum_eqs' aform.simps ("x + x + 1"))
|
|
220 |
oops
|
|
221 |
|
|
222 |
(* We now define a simple transformation on aform: NNF + linum on atoms *)
|
|
223 |
consts linaform:: "aform \<Rightarrow> aform"
|
|
224 |
recdef linaform "measure linaformsize"
|
|
225 |
"linaform (Lt s t) = Lt (linum s) (linum t)"
|
|
226 |
"linaform (Eq s t) = Eq (linum s) (linum t)"
|
|
227 |
"linaform (Ge s t) = Ge (linum s) (linum t)"
|
|
228 |
"linaform (NEq s t) = NEq (linum s) (linum t)"
|
|
229 |
"linaform (Conj p q) = Conj (linaform p) (linaform q)"
|
|
230 |
"linaform (Disj p q) = Disj (linaform p) (linaform q)"
|
|
231 |
"linaform (NEG T) = F"
|
|
232 |
"linaform (NEG F) = T"
|
|
233 |
"linaform (NEG (Lt a b)) = Ge a b"
|
|
234 |
"linaform (NEG (Ge a b)) = Lt a b"
|
|
235 |
"linaform (NEG (Eq a b)) = NEq a b"
|
|
236 |
"linaform (NEG (NEq a b)) = Eq a b"
|
|
237 |
"linaform (NEG (NEG p)) = linaform p"
|
|
238 |
"linaform (NEG (Conj p q)) = Disj (linaform (NEG p)) (linaform (NEG q))"
|
|
239 |
"linaform (NEG (Disj p q)) = Conj (linaform (NEG p)) (linaform (NEG q))"
|
|
240 |
"linaform p = p"
|
|
241 |
|
|
242 |
lemma linaform: "aform vs (linaform p) = aform vs p"
|
|
243 |
by (induct p rule: linaform.induct, auto simp add: linum)
|
|
244 |
|
|
245 |
lemma "(((Suc(Suc (Suc 0)))*((x::nat) + (Suc (Suc 0)))) + (Suc (Suc (Suc 0))) * ((Suc(Suc (Suc 0)))*((x::nat) + (Suc (Suc 0))))< 0) \<and> (Suc 0 + Suc 0< 0)"
|
|
246 |
apply (reflection linaform Inum_eqs' aform.simps) (* ("Suc 0 + Suc 0< 0")) *)
|
|
247 |
oops
|
|
248 |
|
|
249 |
(* etc etc \<dots>*)
|
|
250 |
|
|
251 |
|
|
252 |
end
|