10 |
10 |
11 text \<open>This theory presents two methods: reify and reflection\<close> |
11 text \<open>This theory presents two methods: reify and reflection\<close> |
12 |
12 |
13 text \<open> |
13 text \<open> |
14 Consider an HOL type \<open>\<sigma>\<close>, the structure of which is not recongnisable |
14 Consider an HOL type \<open>\<sigma>\<close>, the structure of which is not recongnisable |
15 on the theory level. This is the case of @{typ bool}, arithmetical terms such as @{typ int}, |
15 on the theory level. This is the case of \<^typ>\<open>bool\<close>, arithmetical terms such as \<^typ>\<open>int\<close>, |
16 @{typ real} etc \dots In order to implement a simplification on terms of type \<open>\<sigma>\<close> we |
16 \<^typ>\<open>real\<close> etc \dots In order to implement a simplification on terms of type \<open>\<sigma>\<close> we |
17 often need its structure. Traditionnaly such simplifications are written in ML, |
17 often need its structure. Traditionnaly such simplifications are written in ML, |
18 proofs are synthesized. |
18 proofs are synthesized. |
19 |
19 |
20 An other strategy is to declare an HOL datatype \<open>\<tau>\<close> and an HOL function (the |
20 An other strategy is to declare an HOL datatype \<open>\<tau>\<close> and an HOL function (the |
21 interpretation) that maps elements of \<open>\<tau>\<close> to elements of \<open>\<sigma>\<close>. |
21 interpretation) that maps elements of \<open>\<tau>\<close> to elements of \<open>\<sigma>\<close>. |
33 subgoal. |
33 subgoal. |
34 |
34 |
35 The method \<open>reflection\<close> uses \<open>reify\<close> and has a very similar signature: |
35 The method \<open>reflection\<close> uses \<open>reify\<close> and has a very similar signature: |
36 \<open>reflection corr_thm eqs (t)\<close>. Here again \<open>eqs\<close> and \<open>(t)\<close> |
36 \<open>reflection corr_thm eqs (t)\<close>. Here again \<open>eqs\<close> and \<open>(t)\<close> |
37 are as described above and \<open>corr_thm\<close> is a theorem proving |
37 are as described above and \<open>corr_thm\<close> is a theorem proving |
38 @{prop "I vs (f t) = I vs t"}. We assume that \<open>I\<close> is the interpretation |
38 \<^prop>\<open>I vs (f t) = I vs t\<close>. We assume that \<open>I\<close> is the interpretation |
39 and \<open>f\<close> is some useful and executable simplification of type \<open>\<tau> \<Rightarrow> \<tau>\<close>. |
39 and \<open>f\<close> is some useful and executable simplification of type \<open>\<tau> \<Rightarrow> \<tau>\<close>. |
40 The method \<open>reflection\<close> applies reification and hence the theorem @{prop "t = I xs s"} |
40 The method \<open>reflection\<close> applies reification and hence the theorem \<^prop>\<open>t = I xs s\<close> |
41 and hence using \<open>corr_thm\<close> derives @{prop "t = I xs (f s)"}. It then uses |
41 and hence using \<open>corr_thm\<close> derives \<^prop>\<open>t = I xs (f s)\<close>. It then uses |
42 normalization by equational rewriting to prove @{prop "f s = s'"} which almost finishes |
42 normalization by equational rewriting to prove \<^prop>\<open>f s = s'\<close> which almost finishes |
43 the proof of @{prop "t = t'"} where @{prop "I xs s' = t'"}. |
43 the proof of \<^prop>\<open>t = t'\<close> where \<^prop>\<open>I xs s' = t'\<close>. |
44 \<close> |
44 \<close> |
45 |
45 |
46 text \<open>Example 1 : Propositional formulae and NNF.\<close> |
46 text \<open>Example 1 : Propositional formulae and NNF.\<close> |
47 text \<open>The type \<open>fm\<close> represents simple propositional formulae:\<close> |
47 text \<open>The type \<open>fm\<close> represents simple propositional formulae:\<close> |
48 |
48 |
79 |
79 |
80 lemma "Q \<longrightarrow> (D \<and> F \<and> ((\<not> D) \<and> (\<not> F)))" |
80 lemma "Q \<longrightarrow> (D \<and> F \<and> ((\<not> D) \<and> (\<not> F)))" |
81 apply (reify Ifm.simps) |
81 apply (reify Ifm.simps) |
82 oops |
82 oops |
83 |
83 |
84 text \<open>Method \<open>reify\<close> maps a @{typ bool} to an @{typ fm}. For this it needs the |
84 text \<open>Method \<open>reify\<close> maps a \<^typ>\<open>bool\<close> to an \<^typ>\<open>fm\<close>. For this it needs the |
85 semantics of \<open>fm\<close>, i.e.\ the rewrite rules in \<open>Ifm.simps\<close>.\<close> |
85 semantics of \<open>fm\<close>, i.e.\ the rewrite rules in \<open>Ifm.simps\<close>.\<close> |
86 |
86 |
87 text \<open>You can also just pick up a subterm to reify.\<close> |
87 text \<open>You can also just pick up a subterm to reify.\<close> |
88 lemma "Q \<longrightarrow> (D \<and> F \<and> ((\<not> D) \<and> (\<not> F)))" |
88 lemma "Q \<longrightarrow> (D \<and> F \<and> ((\<not> D) \<and> (\<not> F)))" |
89 apply (reify Ifm.simps ("((\<not> D) \<and> (\<not> F))")) |
89 apply (reify Ifm.simps ("((\<not> D) \<and> (\<not> F))")) |
113 | "nnf (NOT (Imp p q)) = And (nnf p) (nnf (NOT q))" |
113 | "nnf (NOT (Imp p q)) = And (nnf p) (nnf (NOT q))" |
114 | "nnf (NOT (Iff p q)) = Or (And (nnf p) (nnf (NOT q))) (And (nnf (NOT p)) (nnf q))" |
114 | "nnf (NOT (Iff p q)) = Or (And (nnf p) (nnf (NOT q))) (And (nnf (NOT p)) (nnf q))" |
115 | "nnf (NOT (NOT p)) = nnf p" |
115 | "nnf (NOT (NOT p)) = nnf p" |
116 | "nnf (NOT p) = NOT p" |
116 | "nnf (NOT p) = NOT p" |
117 |
117 |
118 text \<open>The correctness theorem of @{const nnf}: it preserves the semantics of @{typ fm}\<close> |
118 text \<open>The correctness theorem of \<^const>\<open>nnf\<close>: it preserves the semantics of \<^typ>\<open>fm\<close>\<close> |
119 lemma nnf [reflection]: |
119 lemma nnf [reflection]: |
120 "Ifm (nnf p) vs = Ifm p vs" |
120 "Ifm (nnf p) vs = Ifm p vs" |
121 by (induct p rule: nnf.induct) auto |
121 by (induct p rule: nnf.induct) auto |
122 |
122 |
123 text \<open>Now let's perform NNF using our @{const nnf} function defined above. First to the |
123 text \<open>Now let's perform NNF using our \<^const>\<open>nnf\<close> function defined above. First to the |
124 whole subgoal.\<close> |
124 whole subgoal.\<close> |
125 lemma "A \<noteq> B \<and> (B \<longrightarrow> A \<noteq> (B \<or> C \<and> (B \<longrightarrow> A \<or> D))) \<longrightarrow> A \<or> B \<and> D" |
125 lemma "A \<noteq> B \<and> (B \<longrightarrow> A \<noteq> (B \<or> C \<and> (B \<longrightarrow> A \<or> D))) \<longrightarrow> A \<or> B \<and> D" |
126 apply (reflection Ifm.simps) |
126 apply (reflection Ifm.simps) |
127 oops |
127 oops |
128 |
128 |
197 |
197 |
198 lemma "1 * (2 * x + (y::nat) + 0 + 1) \<noteq> 0" |
198 lemma "1 * (2 * x + (y::nat) + 0 + 1) \<noteq> 0" |
199 apply (reify Inum_eqs' ("1 * (2 * x + (y::nat) + 0 + 1)")) |
199 apply (reify Inum_eqs' ("1 * (2 * x + (y::nat) + 0 + 1)")) |
200 oops |
200 oops |
201 |
201 |
202 text \<open>Okay, let's try reflection. Some simplifications on @{typ num} follow. You can |
202 text \<open>Okay, let's try reflection. Some simplifications on \<^typ>\<open>num\<close> follow. You can |
203 skim until the main theorem \<open>linum\<close>.\<close> |
203 skim until the main theorem \<open>linum\<close>.\<close> |
204 |
204 |
205 fun lin_add :: "num \<Rightarrow> num \<Rightarrow> num" |
205 fun lin_add :: "num \<Rightarrow> num \<Rightarrow> num" |
206 where |
206 where |
207 "lin_add (CN n1 c1 r1) (CN n2 c2 r2) = |
207 "lin_add (CN n1 c1 r1) (CN n2 c2 r2) = |