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