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