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