src/HOL/ex/Reflection_Examples.thy
changeset 58249 180f1b3508ed
parent 51240 a7a04b449e8b
child 58305 57752a91eec4
equal deleted inserted replaced
58248:25af24e1f37b 58249:180f1b3508ed
    44 *}
    44 *}
    45 
    45 
    46 text {* Example 1 : Propositional formulae and NNF. *}
    46 text {* Example 1 : Propositional formulae and NNF. *}
    47 text {* The type @{text fm} represents simple propositional formulae: *}
    47 text {* The type @{text fm} represents simple propositional formulae: *}
    48 
    48 
    49 datatype form = TrueF | FalseF | Less nat nat
    49 datatype_new form = TrueF | FalseF | Less nat nat
    50   | And form form | Or form form | Neg form | ExQ form
    50   | And form form | Or form form | Neg form | ExQ form
    51 
    51 
    52 primrec interp :: "form \<Rightarrow> ('a::ord) list \<Rightarrow> bool"
    52 primrec interp :: "form \<Rightarrow> ('a::ord) list \<Rightarrow> bool"
    53 where
    53 where
    54   "interp TrueF vs \<longleftrightarrow> True"
    54   "interp TrueF vs \<longleftrightarrow> True"
    64 
    64 
    65 lemma "\<exists>x. x < y \<and> x < z"
    65 lemma "\<exists>x. x < y \<and> x < z"
    66   apply reify
    66   apply reify
    67   oops
    67   oops
    68 
    68 
    69 datatype fm = And fm fm | Or fm fm | Imp fm fm | Iff fm fm | NOT fm | At nat
    69 datatype_new fm = And fm fm | Or fm fm | Imp fm fm | Iff fm fm | NOT fm | At nat
    70 
    70 
    71 primrec Ifm :: "fm \<Rightarrow> bool list \<Rightarrow> bool"
    71 primrec Ifm :: "fm \<Rightarrow> bool list \<Rightarrow> bool"
    72 where
    72 where
    73   "Ifm (At n) vs \<longleftrightarrow> vs ! n"
    73   "Ifm (At n) vs \<longleftrightarrow> vs ! n"
    74 | "Ifm (And p q) vs \<longleftrightarrow> Ifm p vs \<and> Ifm q vs"
    74 | "Ifm (And p q) vs \<longleftrightarrow> Ifm p vs \<and> Ifm q vs"
   133 
   133 
   134 
   134 
   135 text {* Example 2: Simple arithmetic formulae *}
   135 text {* Example 2: Simple arithmetic formulae *}
   136 
   136 
   137 text {* The type @{text num} reflects linear expressions over natural number *}
   137 text {* The type @{text num} reflects linear expressions over natural number *}
   138 datatype num = C nat | Add num num | Mul nat num | Var nat | CN nat nat num
   138 datatype_new num = C nat | Add num num | Mul nat num | Var nat | CN nat nat num
   139 
   139 
   140 text {* This is just technical to make recursive definitions easier. *}
   140 text {* This is just technical to make recursive definitions easier. *}
   141 primrec num_size :: "num \<Rightarrow> nat" 
   141 primrec num_size :: "num \<Rightarrow> nat" 
   142 where
   142 where
   143   "num_size (C c) = 1"
   143   "num_size (C c) = 1"
   250   apply (reflection Inum_eqs' only: "Suc (Suc 1) * (x + Suc 1 * y)")
   250   apply (reflection Inum_eqs' only: "Suc (Suc 1) * (x + Suc 1 * y)")
   251 oops
   251 oops
   252 
   252 
   253 text {* Let's lift this to formulae and see what happens *}
   253 text {* Let's lift this to formulae and see what happens *}
   254 
   254 
   255 datatype aform = Lt num num  | Eq num num | Ge num num | NEq num num | 
   255 datatype_new aform = Lt num num  | Eq num num | Ge num num | NEq num num | 
   256   Conj aform aform | Disj aform aform | NEG aform | T | F
   256   Conj aform aform | Disj aform aform | NEG aform | T | F
   257 
   257 
   258 primrec linaformsize:: "aform \<Rightarrow> nat"
   258 primrec linaformsize:: "aform \<Rightarrow> nat"
   259 where
   259 where
   260   "linaformsize T = 1"
   260   "linaformsize T = 1"
   329 
   329 
   330 text {* We now give an example where interpretaions have zero or more than only
   330 text {* We now give an example where interpretaions have zero or more than only
   331   one envornement of different types and show that automatic reification also deals with
   331   one envornement of different types and show that automatic reification also deals with
   332   bindings *}
   332   bindings *}
   333   
   333   
   334 datatype rb = BC bool | BAnd rb rb | BOr rb rb
   334 datatype_new rb = BC bool | BAnd rb rb | BOr rb rb
   335 
   335 
   336 primrec Irb :: "rb \<Rightarrow> bool"
   336 primrec Irb :: "rb \<Rightarrow> bool"
   337 where
   337 where
   338   "Irb (BC p) \<longleftrightarrow> p"
   338   "Irb (BC p) \<longleftrightarrow> p"
   339 | "Irb (BAnd s t) \<longleftrightarrow> Irb s \<and> Irb t"
   339 | "Irb (BAnd s t) \<longleftrightarrow> Irb s \<and> Irb t"
   341 
   341 
   342 lemma "A \<and> (B \<or> D \<and> B) \<and> A \<and> (B \<or> D \<and> B) \<or> A \<and> (B \<or> D \<and> B) \<or> A \<and> (B \<or> D \<and> B)"
   342 lemma "A \<and> (B \<or> D \<and> B) \<and> A \<and> (B \<or> D \<and> B) \<or> A \<and> (B \<or> D \<and> B) \<or> A \<and> (B \<or> D \<and> B)"
   343   apply (reify Irb.simps)
   343   apply (reify Irb.simps)
   344 oops
   344 oops
   345 
   345 
   346 datatype rint = IC int | IVar nat | IAdd rint rint | IMult rint rint
   346 datatype_new rint = IC int | IVar nat | IAdd rint rint | IMult rint rint
   347   | INeg rint | ISub rint rint
   347   | INeg rint | ISub rint rint
   348 
   348 
   349 primrec Irint :: "rint \<Rightarrow> int list \<Rightarrow> int"
   349 primrec Irint :: "rint \<Rightarrow> int list \<Rightarrow> int"
   350 where
   350 where
   351   Irint_Var: "Irint (IVar n) vs = vs ! n"
   351   Irint_Var: "Irint (IVar n) vs = vs ! n"
   368 
   368 
   369 lemma "(3::int) * x + y * y - 9 + (- z) = 0"
   369 lemma "(3::int) * x + y * y - 9 + (- z) = 0"
   370   apply (reify Irint_simps ("(3::int) * x + y * y - 9 + (- z)"))
   370   apply (reify Irint_simps ("(3::int) * x + y * y - 9 + (- z)"))
   371   oops
   371   oops
   372 
   372 
   373 datatype rlist = LVar nat | LEmpty | LCons rint rlist | LAppend rlist rlist
   373 datatype_new rlist = LVar nat | LEmpty | LCons rint rlist | LAppend rlist rlist
   374 
   374 
   375 primrec Irlist :: "rlist \<Rightarrow> int list \<Rightarrow> int list list \<Rightarrow> int list"
   375 primrec Irlist :: "rlist \<Rightarrow> int list \<Rightarrow> int list list \<Rightarrow> int list"
   376 where
   376 where
   377   "Irlist (LEmpty) is vs = []"
   377   "Irlist (LEmpty) is vs = []"
   378 | "Irlist (LVar n) is vs = vs ! n"
   378 | "Irlist (LVar n) is vs = vs ! n"
   385 
   385 
   386 lemma "([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs = [y * y - z - 9 + (3::int) * x]"
   386 lemma "([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs = [y * y - z - 9 + (3::int) * x]"
   387   apply (reify Irlist.simps Irint_simps ("([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs"))
   387   apply (reify Irlist.simps Irint_simps ("([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs"))
   388   oops
   388   oops
   389 
   389 
   390 datatype rnat = NC nat| NVar nat| NSuc rnat | NAdd rnat rnat | NMult rnat rnat
   390 datatype_new rnat = NC nat| NVar nat| NSuc rnat | NAdd rnat rnat | NMult rnat rnat
   391   | NNeg rnat | NSub rnat rnat | Nlgth rlist
   391   | NNeg rnat | NSub rnat rnat | Nlgth rlist
   392 
   392 
   393 primrec Irnat :: "rnat \<Rightarrow> int list \<Rightarrow> int list list \<Rightarrow> nat list \<Rightarrow> nat"
   393 primrec Irnat :: "rnat \<Rightarrow> int list \<Rightarrow> int list list \<Rightarrow> nat list \<Rightarrow> nat"
   394 where
   394 where
   395   Irnat_Suc: "Irnat (NSuc t) is ls vs = Suc (Irnat t is ls vs)"
   395   Irnat_Suc: "Irnat (NSuc t) is ls vs = Suc (Irnat t is ls vs)"
   416 lemma "Suc n * length (([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs) = length xs"
   416 lemma "Suc n * length (([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs) = length xs"
   417   apply (reify Irnat_simps Irlist.simps Irint_simps
   417   apply (reify Irnat_simps Irlist.simps Irint_simps
   418      ("Suc n * length (([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs)"))
   418      ("Suc n * length (([(3::int) * x + y * y - 9 + (- z)] @ []) @ xs)"))
   419   oops
   419   oops
   420 
   420 
   421 datatype rifm = RT | RF | RVar nat
   421 datatype_new rifm = RT | RF | RVar nat
   422   | RNLT rnat rnat | RNILT rnat rint | RNEQ rnat rnat
   422   | RNLT rnat rnat | RNILT rnat rint | RNEQ rnat rnat
   423   | RAnd rifm rifm | ROr rifm rifm | RImp rifm rifm| RIff rifm rifm
   423   | RAnd rifm rifm | ROr rifm rifm | RImp rifm rifm| RIff rifm rifm
   424   | RNEX rifm | RIEX rifm | RLEX rifm | RNALL rifm | RIALL rifm | RLALL rifm
   424   | RNEX rifm | RIEX rifm | RLEX rifm | RNALL rifm | RIALL rifm | RLALL rifm
   425   | RBEX rifm | RBALL rifm
   425   | RBEX rifm | RBALL rifm
   426 
   426 
   449   apply (reify Irifm.simps Irnat_simps Irlist.simps Irint_simps)
   449   apply (reify Irifm.simps Irnat_simps Irlist.simps Irint_simps)
   450 oops
   450 oops
   451 
   451 
   452 text {* An example for equations containing type variables *}
   452 text {* An example for equations containing type variables *}
   453 
   453 
   454 datatype prod = Zero | One | Var nat | Mul prod prod 
   454 datatype_new prod = Zero | One | Var nat | Mul prod prod 
   455   | Pw prod nat | PNM nat nat prod
   455   | Pw prod nat | PNM nat nat prod
   456 
   456 
   457 primrec Iprod :: " prod \<Rightarrow> ('a::linordered_idom) list \<Rightarrow>'a" 
   457 primrec Iprod :: " prod \<Rightarrow> ('a::linordered_idom) list \<Rightarrow>'a" 
   458 where
   458 where
   459   "Iprod Zero vs = 0"
   459   "Iprod Zero vs = 0"
   461 | "Iprod (Var n) vs = vs ! n"
   461 | "Iprod (Var n) vs = vs ! n"
   462 | "Iprod (Mul a b) vs = Iprod a vs * Iprod b vs"
   462 | "Iprod (Mul a b) vs = Iprod a vs * Iprod b vs"
   463 | "Iprod (Pw a n) vs = Iprod a vs ^ n"
   463 | "Iprod (Pw a n) vs = Iprod a vs ^ n"
   464 | "Iprod (PNM n k t) vs = (vs ! n) ^ k * Iprod t vs"
   464 | "Iprod (PNM n k t) vs = (vs ! n) ^ k * Iprod t vs"
   465 
   465 
   466 datatype sgn = Pos prod | Neg prod | ZeroEq prod | NZeroEq prod | Tr | F 
   466 datatype_new sgn = Pos prod | Neg prod | ZeroEq prod | NZeroEq prod | Tr | F 
   467   | Or sgn sgn | And sgn sgn
   467   | Or sgn sgn | And sgn sgn
   468 
   468 
   469 primrec Isgn :: "sgn \<Rightarrow> ('a::linordered_idom) list \<Rightarrow> bool"
   469 primrec Isgn :: "sgn \<Rightarrow> ('a::linordered_idom) list \<Rightarrow> bool"
   470 where 
   470 where 
   471   "Isgn Tr vs \<longleftrightarrow> True"
   471   "Isgn Tr vs \<longleftrightarrow> True"