src/HOL/ex/NormalForm.thy
author haftmann
Wed Mar 12 19:38:14 2008 +0100 (2008-03-12)
changeset 26265 4b63b9e9b10d
parent 25934 7b8f3a9efa03
child 26513 6f306c8c2c54
permissions -rw-r--r--
separated Random.thy from Quickcheck.thy
     1 (*  ID:         $Id$
     2     Authors:    Klaus Aehlig, Tobias Nipkow
     3 *)
     4 
     5 header {* Test of normalization function *}
     6 
     7 theory NormalForm
     8 imports Main "~~/src/HOL/Real/Rational"
     9 begin
    10 
    11 lemma "True" by normalization
    12 lemma "p \<longrightarrow> True" by normalization
    13 declare disj_assoc [code func]
    14 lemma "((P | Q) | R) = (P | (Q | R))" by normalization rule
    15 declare disj_assoc [code func del]
    16 lemma "0 + (n::nat) = n" by normalization rule
    17 lemma "0 + Suc n = Suc n" by normalization rule
    18 lemma "Suc n + Suc m = n + Suc (Suc m)" by normalization rule
    19 lemma "~((0::nat) < (0::nat))" by normalization
    20 
    21 datatype n = Z | S n
    22 consts
    23   add :: "n \<Rightarrow> n \<Rightarrow> n"
    24   add2 :: "n \<Rightarrow> n \<Rightarrow> n"
    25   mul :: "n \<Rightarrow> n \<Rightarrow> n"
    26   mul2 :: "n \<Rightarrow> n \<Rightarrow> n"
    27   exp :: "n \<Rightarrow> n \<Rightarrow> n"
    28 primrec
    29   "add Z = id"
    30   "add (S m) = S o add m"
    31 primrec
    32   "add2 Z n = n"
    33   "add2 (S m) n = S(add2 m n)"
    34 
    35 lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)"
    36   by(induct n) auto
    37 lemma [code]: "add2 n (S m) =  S (add2 n m)"
    38   by(induct n) auto
    39 lemma [code]: "add2 n Z = n"
    40   by(induct n) auto
    41 
    42 lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization rule
    43 lemma "add2 (add2 (S n) (S m)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization rule
    44 lemma "add2 (add2 (S n) (add2 (S m) Z)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization rule
    45 
    46 primrec
    47   "mul Z = (%n. Z)"
    48   "mul (S m) = (%n. add (mul m n) n)"
    49 primrec
    50   "mul2 Z n = Z"
    51   "mul2 (S m) n = add2 n (mul2 m n)"
    52 primrec
    53   "exp m Z = S Z"
    54   "exp m (S n) = mul (exp m n) m"
    55 
    56 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
    57 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
    58 lemma "exp (S(S Z)) (S(S(S(S Z)))) = exp (S(S(S(S Z)))) (S(S Z))" by normalization
    59 
    60 lemma "(let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)) = Z" by normalization
    61 lemma "split (%x y. x) (a, b) = a" by normalization rule
    62 lemma "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z)) = Z" by normalization
    63 
    64 lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
    65 
    66 lemma "[] @ [] = []" by normalization
    67 lemma "map f [x,y,z::'x] = [f x, f y, f z]" by normalization rule+
    68 lemma "[a, b, c] @ xs = a # b # c # xs" by normalization rule+
    69 lemma "[] @ xs = xs" by normalization rule
    70 lemma "map (%f. f True) [id, g, Not] = [True, g True, False]" by normalization rule+
    71 lemma "map (%f. f True) ([id, g, Not] @ fs) = [True, g True, False] @ map (%f. f True) fs" by normalization rule+
    72 lemma "rev [a, b, c] = [c, b, a]" by normalization rule+
    73 normal_form "rev (a#b#cs) = rev cs @ [b, a]"
    74 normal_form "map (%F. F [a,b,c::'x]) (map map [f,g,h])"
    75 normal_form "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))"
    76 normal_form "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])"
    77 lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]" 
    78   by normalization
    79 normal_form "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False"
    80 normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs = P"
    81 lemma "let x = y in [x, x] = [y, y]" by normalization rule+
    82 lemma "Let y (%x. [x,x]) = [y, y]" by normalization rule+
    83 normal_form "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
    84 lemma "(%(x,y). add x y) (S z,S z) = S (add z (S z))" by normalization rule+
    85 normal_form "filter (%x. x) ([True,False,x]@xs)"
    86 normal_form "filter Not ([True,False,x]@xs)"
    87 
    88 lemma "[x,y,z] @ [a,b,c] = [x, y, z, a, b ,c]" by normalization rule+
    89 lemma "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f]) = [a, b, c, d, e, f]" by normalization rule+
    90 lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]" by normalization
    91 
    92 lemma "last [a, b, c] = c" by normalization rule
    93 lemma "last ([a, b, c] @ xs) = (if null xs then c else last xs)"
    94   by normalization rule
    95 
    96 lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization rule
    97 lemma "(-4::int) * 2 = -8" by normalization
    98 lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
    99 lemma "(2::int) + 3 = 5" by normalization
   100 lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
   101 lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
   102 lemma "(2::int) < 3" by normalization
   103 lemma "(2::int) <= 3" by normalization
   104 lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
   105 lemma "4 - 42 * abs (3 + (-7\<Colon>int)) = -164" by normalization
   106 lemma "(if (0\<Colon>nat) \<le> (x\<Colon>nat) then 0\<Colon>nat else x) = 0" by normalization
   107 lemma "4 = Suc (Suc (Suc (Suc 0)))" by normalization
   108 lemma "nat 4 = Suc (Suc (Suc (Suc 0)))" by normalization
   109 lemma "[Suc 0, 0] = [Suc 0, 0]" by normalization
   110 lemma "max (Suc 0) 0 = Suc 0" by normalization
   111 lemma "(42::rat) / 1704 = 1 / 284 + 3 / 142" by normalization
   112 normal_form "Suc 0 \<in> set ms"
   113 
   114 lemma "f = f" by normalization rule+
   115 lemma "f x = f x" by normalization rule+
   116 lemma "(f o g) x = f (g x)" by normalization rule+
   117 lemma "(f o id) x = f x" by normalization rule+
   118 normal_form "(\<lambda>x. x)"
   119 
   120 (* Church numerals: *)
   121 
   122 normal_form "(%m n f x. m f (n f x)) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
   123 normal_form "(%m n f x. m (n f) x) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
   124 normal_form "(%m n. n m) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
   125 
   126 end