src/HOL/ex/NormalForm.thy
author haftmann
Fri, 17 Apr 2009 14:29:55 +0200
changeset 30946 585c3f2622ea
parent 28952 15a4b2cf8c34
child 31062 878e00798148
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30946
haftmann
parents: 28952
diff changeset
     1
(*  Authors:  Klaus Aehlig, Tobias Nipkow *)
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     2
30946
haftmann
parents: 28952
diff changeset
     3
header {* Testing implementation of normalization by evaluation *}
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     4
d909e782e247 renamed file
nipkow
parents:
diff changeset
     5
theory NormalForm
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28709
diff changeset
     6
imports Main Rational
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     7
begin
d909e782e247 renamed file
nipkow
parents:
diff changeset
     8
21117
e8657a20a52f *** empty log message ***
haftmann
parents: 21059
diff changeset
     9
lemma "True" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    10
lemma "p \<longrightarrow> True" by normalization
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    11
declare disj_assoc [code nbe]
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    12
lemma "((P | Q) | R) = (P | (Q | R))" by normalization
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28422
diff changeset
    13
declare disj_assoc [code del]
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    14
lemma "0 + (n::nat) = n" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    15
lemma "0 + Suc n = Suc n" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    16
lemma "Suc n + Suc m = n + Suc (Suc m)" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    17
lemma "~((0::nat) < (0::nat))" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    18
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    19
datatype n = Z | S n
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    20
30946
haftmann
parents: 28952
diff changeset
    21
primrec add :: "n \<Rightarrow> n \<Rightarrow> n" where
haftmann
parents: 28952
diff changeset
    22
   "add Z = id"
haftmann
parents: 28952
diff changeset
    23
 | "add (S m) = S o add m"
haftmann
parents: 28952
diff changeset
    24
haftmann
parents: 28952
diff changeset
    25
primrec add2 :: "n \<Rightarrow> n \<Rightarrow> n" where
haftmann
parents: 28952
diff changeset
    26
   "add2 Z n = n"
haftmann
parents: 28952
diff changeset
    27
 | "add2 (S m) n = S(add2 m n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    28
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 26739
diff changeset
    29
declare add2.simps [code]
28709
6a5d214aaa82 adapted to strict pattern discipline
haftmann
parents: 28562
diff changeset
    30
lemma [code nbe]: "add2 (add2 n m) k = add2 n (add2 m k)"
28143
e5c6c4aac52c different bookkeeping for code equations
haftmann
parents: 26739
diff changeset
    31
  by (induct n) auto
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    32
lemma [code]: "add2 n (S m) =  S (add2 n m)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    33
  by(induct n) auto
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    34
lemma [code]: "add2 n Z = n"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    35
  by(induct n) auto
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    36
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    37
lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    38
lemma "add2 (add2 (S n) (S m)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    39
lemma "add2 (add2 (S n) (add2 (S m) Z)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    40
30946
haftmann
parents: 28952
diff changeset
    41
primrec mul :: "n \<Rightarrow> n \<Rightarrow> n" where
haftmann
parents: 28952
diff changeset
    42
   "mul Z = (%n. Z)"
haftmann
parents: 28952
diff changeset
    43
 | "mul (S m) = (%n. add (mul m n) n)"
haftmann
parents: 28952
diff changeset
    44
haftmann
parents: 28952
diff changeset
    45
primrec mul2 :: "n \<Rightarrow> n \<Rightarrow> n" where
haftmann
parents: 28952
diff changeset
    46
   "mul2 Z n = Z"
haftmann
parents: 28952
diff changeset
    47
 | "mul2 (S m) n = add2 n (mul2 m n)"
haftmann
parents: 28952
diff changeset
    48
haftmann
parents: 28952
diff changeset
    49
primrec exp :: "n \<Rightarrow> n \<Rightarrow> n" where
haftmann
parents: 28952
diff changeset
    50
   "exp m Z = S Z"
haftmann
parents: 28952
diff changeset
    51
 | "exp m (S n) = mul (exp m n) m"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    52
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    53
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
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    54
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
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    55
lemma "exp (S(S Z)) (S(S(S(S Z)))) = exp (S(S(S(S Z)))) (S(S Z))" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    56
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    57
lemma "(let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)) = Z" by normalization
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    58
lemma "split (%x y. x) (a, b) = a" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    59
lemma "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z)) = Z" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    60
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    61
lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    62
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    63
lemma "[] @ [] = []" by normalization
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    64
lemma "map f [x,y,z::'x] = [f x, f y, f z]" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    65
lemma "[a, b, c] @ xs = a # b # c # xs" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    66
lemma "[] @ xs = xs" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    67
lemma "map (%f. f True) [id, g, Not] = [True, g True, False]" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    68
28422
haftmann
parents: 28351
diff changeset
    69
lemma "map (%f. f True) ([id, g, Not] @ fs) = [True, g True, False] @ map (%f. f True) fs"
haftmann
parents: 28351
diff changeset
    70
  by normalization rule+
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    71
lemma "rev [a, b, c] = [c, b, a]" by normalization
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26555
diff changeset
    72
normal_form "rev (a#b#cs) = rev cs @ [b, a]"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    73
normal_form "map (%F. F [a,b,c::'x]) (map map [f,g,h])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    74
normal_form "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    75
normal_form "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])"
25934
7b8f3a9efa03 more lemmas
haftmann
parents: 25866
diff changeset
    76
lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]" 
7b8f3a9efa03 more lemmas
haftmann
parents: 25866
diff changeset
    77
  by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    78
normal_form "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False"
25934
7b8f3a9efa03 more lemmas
haftmann
parents: 25866
diff changeset
    79
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs = P"
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    80
lemma "let x = y in [x, x] = [y, y]" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    81
lemma "Let y (%x. [x,x]) = [y, y]" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    82
normal_form "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    83
lemma "(%(x,y). add x y) (S z,S z) = S (add z (S z))" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    84
normal_form "filter (%x. x) ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    85
normal_form "filter Not ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    86
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    87
lemma "[x,y,z] @ [a,b,c] = [x, y, z, a, b, c]" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    88
lemma "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f]) = [a, b, c, d, e, f]" by normalization
25100
fe9632d914c7 added examples
haftmann
parents: 23396
diff changeset
    89
lemma "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    90
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    91
lemma "last [a, b, c] = c" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    92
lemma "last ([a, b, c] @ xs) = last (c # xs)" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    93
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
    94
lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    95
lemma "(-4::int) * 2 = -8" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    96
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    97
lemma "(2::int) + 3 = 5" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    98
lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    99
lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   100
lemma "(2::int) < 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   101
lemma "(2::int) <= 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   102
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   103
lemma "4 - 42 * abs (3 + (-7\<Colon>int)) = -164" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   104
lemma "(if (0\<Colon>nat) \<le> (x\<Colon>nat) then 0\<Colon>nat else x) = 0" by normalization
22394
54ea68b5a92f tuned code theorems for ord on integers
haftmann
parents: 21987
diff changeset
   105
lemma "4 = Suc (Suc (Suc (Suc 0)))" by normalization
54ea68b5a92f tuned code theorems for ord on integers
haftmann
parents: 21987
diff changeset
   106
lemma "nat 4 = Suc (Suc (Suc (Suc 0)))" by normalization
25100
fe9632d914c7 added examples
haftmann
parents: 23396
diff changeset
   107
lemma "[Suc 0, 0] = [Suc 0, 0]" by normalization
fe9632d914c7 added examples
haftmann
parents: 23396
diff changeset
   108
lemma "max (Suc 0) 0 = Suc 0" by normalization
25187
2d225c1c4b78 more computation with rationals
haftmann
parents: 25165
diff changeset
   109
lemma "(42::rat) / 1704 = 1 / 284 + 3 / 142" by normalization
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   110
normal_form "Suc 0 \<in> set ms"
20922
14873e42659c added nbe_post for delayed_if
nipkow
parents: 20921
diff changeset
   111
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
   112
lemma "f = f" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
   113
lemma "f x = f x" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
   114
lemma "(f o g) x = f (g x)" by normalization
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28337
diff changeset
   115
lemma "(f o id) x = f x" by normalization
25934
7b8f3a9efa03 more lemmas
haftmann
parents: 25866
diff changeset
   116
normal_form "(\<lambda>x. x)"
21987
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   117
23396
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   118
(* Church numerals: *)
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   119
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   120
normal_form "(%m n f x. m f (n f x)) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   121
normal_form "(%m n f x. m (n f) x) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   122
normal_form "(%m n. n m) (%f x. f(f(f(x)))) (%f x. f(f(f(x))))"
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   123
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
   124
end