src/HOL/ex/ReflectionEx.thy
changeset 23547 cb1203d8897c
parent 23477 f4b83f03cac9
child 23608 e65e36dbe0d2
equal deleted inserted replaced
23546:c8a1bd9585a0 23547:cb1203d8897c
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Amine Chaieb, TU Muenchen
     3     Author:     Amine Chaieb, TU Muenchen
     4 *)
     4 *)
     5 
     5 
     6 header {* Examples for generic reflection and reification *}
     6 header {* Examples for generic reflection and reification *}
     7 
       
     8 theory ReflectionEx
     7 theory ReflectionEx
     9 imports Reflection
     8 imports Reflection
    10 begin
     9 begin
    11 
    10 
    12 text{* This theory presents two methods: reify and reflection *}
    11 text{* This theory presents two methods: reify and reflection *}
    23 The method reflection uses @{text reify} and has a very similar signature: @{text "reflection corr_thm eqs (t)"}. Here again @{text eqs} and @{text "(t)"} are as described above and @{text corr_thm} is a thorem proving @{term "I vs (f t) = I vs t"}. We assume that @{text I} is the interpretation and @{text f} is some useful and executable simplification of type @{text "tau \<Rightarrow> tau"}. The method @{text reflection} applies reification and hence the theorem @{term "t = I xs s"} and hence using @{text corr_thm} derives @{term "t = I xs (f s)"}. It then uses normalization by evaluation to prove @{term "f s = s'"} which almost finishes the proof of @{term "t = t'"} where @{term "I xs s' = t'"}.
    22 The method reflection uses @{text reify} and has a very similar signature: @{text "reflection corr_thm eqs (t)"}. Here again @{text eqs} and @{text "(t)"} are as described above and @{text corr_thm} is a thorem proving @{term "I vs (f t) = I vs t"}. We assume that @{text I} is the interpretation and @{text f} is some useful and executable simplification of type @{text "tau \<Rightarrow> tau"}. The method @{text reflection} applies reification and hence the theorem @{term "t = I xs s"} and hence using @{text corr_thm} derives @{term "t = I xs (f s)"}. It then uses normalization by evaluation to prove @{term "f s = s'"} which almost finishes the proof of @{term "t = t'"} where @{term "I xs s' = t'"}.
    24 *}
    23 *}
    25 
    24 
    26 text{* Example 1 : Propositional formulae and NNF.*}
    25 text{* Example 1 : Propositional formulae and NNF.*}
    27 text{* The type @{text fm} represents simple propositional formulae: *}
    26 text{* The type @{text fm} represents simple propositional formulae: *}
       
    27 
       
    28 datatype form = TrueF | FalseF | Less nat nat |
       
    29                 And form form | Or form form | Neg form | ExQ form
       
    30 
       
    31 fun interp :: "('a::ord) list \<Rightarrow> form \<Rightarrow> bool" where
       
    32 "interp e TrueF = True" |
       
    33 "interp e FalseF = False" |
       
    34 "interp e (Less i j) = (e!i < e!j)" |
       
    35 "interp e (And f1 f2) = (interp e f1 & interp e f2)" |
       
    36 "interp e (Or f1 f2) = (interp e f1 | interp e f2)" |
       
    37 "interp e (Neg f) = (~ interp e f)" |
       
    38 "interp e (ExQ f) = (EX x. interp (x#e) f)"
       
    39 
       
    40 lemmas interp_reify_eqs = interp.simps[where ?'b = int]
       
    41 declare interp_reify_eqs(1)[reify add: interp_reify_eqs]
       
    42 
       
    43 lemma "EX x::int. y < x & x < z"
       
    44   apply reify
       
    45 oops
    28 
    46 
    29 datatype fm = And fm fm | Or fm fm | Imp fm fm | Iff fm fm | NOT fm | At nat
    47 datatype fm = And fm fm | Or fm fm | Imp fm fm | Iff fm fm | NOT fm | At nat
    30 
    48 
    31 consts Ifm :: "bool list \<Rightarrow> fm \<Rightarrow> bool"
    49 consts Ifm :: "bool list \<Rightarrow> fm \<Rightarrow> bool"
    32 primrec
    50 primrec
    35   "Ifm vs (Or p q) = (Ifm vs p \<or> Ifm vs q)"
    53   "Ifm vs (Or p q) = (Ifm vs p \<or> Ifm vs q)"
    36   "Ifm vs (Imp p q) = (Ifm vs p \<longrightarrow> Ifm vs q)"
    54   "Ifm vs (Imp p q) = (Ifm vs p \<longrightarrow> Ifm vs q)"
    37   "Ifm vs (Iff p q) = (Ifm vs p = Ifm vs q)"
    55   "Ifm vs (Iff p q) = (Ifm vs p = Ifm vs q)"
    38   "Ifm vs (NOT p) = (\<not> (Ifm vs p))"
    56   "Ifm vs (NOT p) = (\<not> (Ifm vs p))"
    39 
    57 
       
    58 lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
       
    59 apply (reify Ifm.simps)
       
    60 oops
       
    61 
       
    62   text{* Method @{text reify} maps a bool to an fm. For this it needs the 
       
    63   semantics of fm, i.e.\ the rewrite rules in @{text Ifm.simps}. *}
       
    64 
       
    65 
       
    66   (* You can also just pick up a subterm to reify \<dots> *)
       
    67 lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
       
    68 apply (reify Ifm.simps ("((~ D) & (~ F))"))
       
    69 oops
       
    70 
       
    71   text{* Let's perform NNF. This is a version that tends to generate disjunctions *}
    40 consts fmsize :: "fm \<Rightarrow> nat"
    72 consts fmsize :: "fm \<Rightarrow> nat"
    41 primrec
    73 primrec
    42   "fmsize (At n) = 1"
    74   "fmsize (At n) = 1"
    43   "fmsize (NOT p) = 1 + fmsize p"
    75   "fmsize (NOT p) = 1 + fmsize p"
    44   "fmsize (And p q) = 1 + fmsize p + fmsize q"
    76   "fmsize (And p q) = 1 + fmsize p + fmsize q"
    45   "fmsize (Or p q) = 1 + fmsize p + fmsize q"
    77   "fmsize (Or p q) = 1 + fmsize p + fmsize q"
    46   "fmsize (Imp p q) = 2 + fmsize p + fmsize q"
    78   "fmsize (Imp p q) = 2 + fmsize p + fmsize q"
    47   "fmsize (Iff p q) = 2 + 2* fmsize p + 2* fmsize q"
    79   "fmsize (Iff p q) = 2 + 2* fmsize p + 2* fmsize q"
    48 
    80 
    49 
       
    50 
       
    51   text{* Method @{text reify} maps a bool to an fm. For this it needs the 
       
    52   semantics of fm, i.e.\ the rewrite rules in @{text Ifm.simps}. *}
       
    53 lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
       
    54 apply (reify Ifm.simps)
       
    55 oops
       
    56 
       
    57   (* You can also just pick up a subterm to reify \<dots> *)
       
    58 lemma "Q \<longrightarrow> (D & F & ((~ D) & (~ F)))"
       
    59 apply (reify Ifm.simps ("((~ D) & (~ F))"))
       
    60 oops
       
    61 
       
    62   text{* Let's perform NNF. This is a version that tends to generate disjunctions *}
       
    63 consts nnf :: "fm \<Rightarrow> fm"
    81 consts nnf :: "fm \<Rightarrow> fm"
    64 recdef nnf "measure fmsize"
    82 recdef nnf "measure fmsize"
    65   "nnf (At n) = At n"
    83   "nnf (At n) = At n"
    66   "nnf (And p q) = And (nnf p) (nnf q)"
    84   "nnf (And p q) = And (nnf p) (nnf q)"
    67   "nnf (Or p q) = Or (nnf p) (nnf q)"
    85   "nnf (Or p q) = Or (nnf p) (nnf q)"
   382   "Isgn vs (And p q) = (Isgn vs p \<and> Isgn vs q)"
   400   "Isgn vs (And p q) = (Isgn vs p \<and> Isgn vs q)"
   383   "Isgn vs (Or p q) = (Isgn vs p \<or> Isgn vs q)"
   401   "Isgn vs (Or p q) = (Isgn vs p \<or> Isgn vs q)"
   384 
   402 
   385 lemmas eqs = Isgn.simps Iprod.simps
   403 lemmas eqs = Isgn.simps Iprod.simps
   386 
   404 
   387 lemma "(x::int)^4 * y * z * y^2 * z^23 > 0"
   405 lemma "(x::'a::{ordered_idom, recpower})^4 * y * z * y^2 * z^23 > 0"
   388   apply (reify eqs)
   406   apply (reify eqs)
   389   oops
   407   oops
   390 
   408 
   391 
       
   392 end
   409 end