src/HOL/ex/Normalization_by_Evaluation.thy
 author wenzelm Wed Jun 22 10:09:20 2016 +0200 (2016-06-22) changeset 63343 fb5d8a50c641 parent 61945 1135b8de26c3 child 66345 882abe912da9 permissions -rw-r--r--
bundle lifting_syntax;
```     1 (*  Authors:  Klaus Aehlig, Tobias Nipkow *)
```
```     2
```
```     3 section \<open>Testing implementation of normalization by evaluation\<close>
```
```     4
```
```     5 theory Normalization_by_Evaluation
```
```     6 imports Complex_Main
```
```     7 begin
```
```     8
```
```     9 lemma "True" by normalization
```
```    10 lemma "p \<longrightarrow> True" by normalization
```
```    11 declare disj_assoc [code nbe]
```
```    12 lemma "((P | Q) | R) = (P | (Q | R))" by normalization
```
```    13 lemma "0 + (n::nat) = n" by normalization
```
```    14 lemma "0 + Suc n = Suc n" by normalization
```
```    15 lemma "Suc n + Suc m = n + Suc (Suc m)" by normalization
```
```    16 lemma "~((0::nat) < (0::nat))" by normalization
```
```    17
```
```    18 datatype n = Z | S n
```
```    19
```
```    20 primrec add :: "n \<Rightarrow> n \<Rightarrow> n" where
```
```    21    "add Z = id"
```
```    22  | "add (S m) = S o add m"
```
```    23
```
```    24 primrec add2 :: "n \<Rightarrow> n \<Rightarrow> n" where
```
```    25    "add2 Z n = n"
```
```    26  | "add2 (S m) n = S(add2 m n)"
```
```    27
```
```    28 declare add2.simps [code]
```
```    29 lemma [code nbe]: "add2 (add2 n m) k = add2 n (add2 m k)"
```
```    30   by (induct n) auto
```
```    31 lemma [code]: "add2 n (S m) =  S (add2 n m)"
```
```    32   by(induct n) auto
```
```    33 lemma [code]: "add2 n Z = n"
```
```    34   by(induct n) auto
```
```    35
```
```    36 lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization
```
```    37 lemma "add2 (add2 (S n) (S m)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
```
```    38 lemma "add2 (add2 (S n) (add2 (S m) Z)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
```
```    39
```
```    40 primrec mul :: "n \<Rightarrow> n \<Rightarrow> n" where
```
```    41    "mul Z = (%n. Z)"
```
```    42  | "mul (S m) = (%n. add (mul m n) n)"
```
```    43
```
```    44 primrec mul2 :: "n \<Rightarrow> n \<Rightarrow> n" where
```
```    45    "mul2 Z n = Z"
```
```    46  | "mul2 (S m) n = add2 n (mul2 m n)"
```
```    47
```
```    48 primrec exp :: "n \<Rightarrow> n \<Rightarrow> n" where
```
```    49    "exp m Z = S Z"
```
```    50  | "exp m (S n) = mul (exp m n) m"
```
```    51
```
```    52 lemma "mul2 (S(S(S(S(S Z))))) (S(S(S Z))) = S(S(S(S(S(S(S(S(S(S(S(S(S(S(S Z))))))))))))))" by normalization
```
```    53 lemma "mul (S(S(S(S(S Z))))) (S(S(S Z))) = S(S(S(S(S(S(S(S(S(S(S(S(S(S(S Z))))))))))))))" by normalization
```
```    54 lemma "exp (S(S Z)) (S(S(S(S Z)))) = exp (S(S(S(S Z)))) (S(S Z))" by normalization
```
```    55
```
```    56 lemma "(let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)) = Z" by normalization
```
```    57 lemma "case_prod (%x y. x) (a, b) = a" by normalization
```
```    58 lemma "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z)) = Z" by normalization
```
```    59
```
```    60 lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
```
```    61
```
```    62 lemma "[] @ [] = []" by normalization
```
```    63 lemma "map f [x,y,z::'x] = [f x, f y, f z]" by normalization
```
```    64 lemma "[a, b, c] @ xs = a # b # c # xs" by normalization
```
```    65 lemma "[] @ xs = xs" by normalization
```
```    66 lemma "map (%f. f True) [id, g, Not] = [True, g True, False]" by normalization
```
```    67
```
```    68 lemma "map (%f. f True) ([id, g, Not] @ fs) = [True, g True, False] @ map (%f. f True) fs"
```
```    69   by normalization rule
```
```    70 lemma "rev [a, b, c] = [c, b, a]" by normalization
```
```    71 value "rev (a#b#cs) = rev cs @ [b, a]"
```
```    72 value "map (%F. F [a,b,c::'x]) (map map [f,g,h])"
```
```    73 value "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))"
```
```    74 value "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])"
```
```    75 lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]"
```
```    76   by normalization
```
```    77 value "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False"
```
```    78 value "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs = P"
```
```    79 lemma "let x = y in [x, x] = [y, y]" by normalization
```
```    80 lemma "Let y (%x. [x,x]) = [y, y]" by normalization
```
```    81 value "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
```
```    82 lemma "(%(x,y). add x y) (S z,S z) = S (add z (S z))" by normalization
```
```    83 value "filter (%x. x) ([True,False,x]@xs)"
```
```    84 value "filter Not ([True,False,x]@xs)"
```
```    85
```
```    86 lemma "[x,y,z] @ [a,b,c] = [x, y, z, a, b, c]" by normalization
```
```    87 lemma "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f]) = [a, b, c, d, e, f]" by normalization
```
```    88 lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]" by normalization
```
```    89
```
```    90 lemma "last [a, b, c] = c" by normalization
```
```    91 lemma "last ([a, b, c] @ xs) = last (c # xs)" by normalization
```
```    92
```
```    93 lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization
```
```    94 lemma "(-4::int) * 2 = -8" by normalization
```
```    95 lemma "\<bar>(-4::int) + 2 * 1\<bar> = 2" by normalization
```
```    96 lemma "(2::int) + 3 = 5" by normalization
```
```    97 lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
```
```    98 lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
```
```    99 lemma "(2::int) < 3" by normalization
```
```   100 lemma "(2::int) <= 3" by normalization
```
```   101 lemma "\<bar>(-4::int) + 2 * 1\<bar> = 2" by normalization
```
```   102 lemma "4 - 42 * \<bar>3 + (-7::int)\<bar> = -164" by normalization
```
```   103 lemma "(if (0::nat) \<le> (x::nat) then 0::nat else x) = 0" by normalization
```
```   104 lemma "4 = Suc (Suc (Suc (Suc 0)))" by normalization
```
```   105 lemma "nat 4 = Suc (Suc (Suc (Suc 0)))" by normalization
```
```   106 lemma "[Suc 0, 0] = [Suc 0, 0]" by normalization
```
```   107 lemma "max (Suc 0) 0 = Suc 0" by normalization
```
```   108 lemma "(42::rat) / 1704 = 1 / 284 + 3 / 142" by normalization
```
```   109 value "Suc 0 \<in> set ms"
```
```   110
```
```   111 (* non-left-linear patterns, equality by extensionality *)
```
```   112
```
```   113 lemma "f = f" by normalization
```
```   114 lemma "f x = f x" by normalization
```
```   115 lemma "(f o g) x = f (g x)" by normalization
```
```   116 lemma "(f o id) x = f x" by normalization
```
```   117 lemma "(id :: bool \<Rightarrow> bool) = id" by normalization
```
```   118 value "(\<lambda>x. x)"
```
```   119
```
```   120 (* Church numerals: *)
```
```   121
```
```   122 value "(%m n f x. m f (n f x)) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
```
```   123 value "(%m n f x. m (n f) x) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
```
```   124 value "(%m n. n m) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
```
```   125
```
```   126 (* handling of type classes in connection with equality *)
```
```   127
```
```   128 lemma "map f [x, y] = [f x, f y]" by normalization
```
```   129 lemma "(map f [x, y], w) = ([f x, f y], w)" by normalization
```
```   130 lemma "map f [x, y] = [f x :: 'a::semigroup_add, f y]" by normalization
```
```   131 lemma "map f [x :: 'a::semigroup_add, y] = [f x, f y]" by normalization
```
```   132 lemma "(map f [x :: 'a::semigroup_add, y], w :: 'b::finite) = ([f x, f y], w)" by normalization
```
```   133
```
```   134 end
```