src/HOL/ex/NormalForm.thy
author nipkow
Fri, 15 Jun 2007 09:09:06 +0200
changeset 23396 6d72ababc58f
parent 22845 5f9138bcb3d7
child 25100 fe9632d914c7
permissions -rw-r--r--
Church numerals added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     1
(*  ID:         $Id$
d909e782e247 renamed file
nipkow
parents:
diff changeset
     2
    Authors:    Klaus Aehlig, Tobias Nipkow
20807
wenzelm
parents: 20595
diff changeset
     3
*)
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     4
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
     5
header {* Test of normalization function *}
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
     6
d909e782e247 renamed file
nipkow
parents:
diff changeset
     7
theory NormalForm
d909e782e247 renamed file
nipkow
parents:
diff changeset
     8
imports Main
d909e782e247 renamed file
nipkow
parents:
diff changeset
     9
begin
d909e782e247 renamed file
nipkow
parents:
diff changeset
    10
21117
e8657a20a52f *** empty log message ***
haftmann
parents: 21059
diff changeset
    11
lemma "True" by normalization
e8657a20a52f *** empty log message ***
haftmann
parents: 21059
diff changeset
    12
lemma "x = x" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    13
lemma "p \<longrightarrow> True" by normalization
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20352
diff changeset
    14
declare disj_assoc [code func]
20595
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
diff changeset
    15
lemma "((P | Q) | R) = (P | (Q | R))" by normalization
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22394
diff changeset
    16
declare disj_assoc [code func del]
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    17
lemma "0 + (n::nat) = n" by normalization
20595
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
diff changeset
    18
lemma "0 + Suc n = Suc n" by normalization
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
diff changeset
    19
lemma "Suc n + Suc m = n + Suc (Suc m)" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    20
lemma "~((0::nat) < (0::nat))" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    21
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    22
datatype n = Z | S n
d909e782e247 renamed file
nipkow
parents:
diff changeset
    23
consts
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    24
  add :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    25
  add2 :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    26
  mul :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    27
  mul2 :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    28
  exp :: "n \<Rightarrow> n \<Rightarrow> n"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    29
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    30
  "add Z = id"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    31
  "add (S m) = S o add m"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    32
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    33
  "add2 Z n = n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    34
  "add2 (S m) n = S(add2 m n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    35
d909e782e247 renamed file
nipkow
parents:
diff changeset
    36
lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    37
  by(induct n) auto
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    38
lemma [code]: "add2 n (S m) =  S (add2 n m)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    39
  by(induct n) auto
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    40
lemma [code]: "add2 n Z = n"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    41
  by(induct n) auto
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    42
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    43
lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    44
lemma "add2 (add2 (S n) (S m)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    45
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
    46
d909e782e247 renamed file
nipkow
parents:
diff changeset
    47
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    48
  "mul Z = (%n. Z)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    49
  "mul (S m) = (%n. add (mul m n) n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    50
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    51
  "mul2 Z n = Z"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    52
  "mul2 (S m) n = add2 n (mul2 m n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    53
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    54
  "exp m Z = S Z"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    55
  "exp m (S n) = mul (exp m n) m"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    56
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    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
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    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
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    59
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
    60
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    61
lemma "(let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)) = Z" by normalization
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    62
lemma "split (%x y. x) (a, b) = a" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    63
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
    64
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    65
lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    66
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    67
lemma "[] @ [] = []" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    68
lemma "[] @ xs = xs" by normalization
21460
cda5cd8bfd16 example tuned
haftmann
parents: 21196
diff changeset
    69
normal_form "[a, b, c] @ xs = a # b # c # xs"
cda5cd8bfd16 example tuned
haftmann
parents: 21196
diff changeset
    70
normal_form "map f [x,y,z::'x] = [f x, f y, f z]"
21156
17f144c6e2f2 some example tweaking
haftmann
parents: 21117
diff changeset
    71
normal_form "map (%f. f True) [id, g, Not] = [True, g True, False]"
17f144c6e2f2 some example tweaking
haftmann
parents: 21117
diff changeset
    72
normal_form "map (%f. f True) ([id, g, Not] @ fs) = [True, g True, False] @ map (%f. f True) fs"
21460
cda5cd8bfd16 example tuned
haftmann
parents: 21196
diff changeset
    73
normal_form "rev [a, b, c] = [c, b, a]"
21156
17f144c6e2f2 some example tweaking
haftmann
parents: 21117
diff changeset
    74
normal_form "rev (a#b#cs) = rev cs @ [b, a]"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    75
normal_form "map (%F. F [a,b,c::'x]) (map map [f,g,h])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    76
normal_form "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    77
normal_form "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    78
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    79
normal_form "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    80
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    81
normal_form "let x = y::'x in [x,x]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    82
normal_form "Let y (%x. [x,x])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    83
normal_form "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    84
normal_form "(%(x,y). add x y) (S z,S z)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    85
normal_form "filter (%x. x) ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    86
normal_form "filter Not ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    87
21460
cda5cd8bfd16 example tuned
haftmann
parents: 21196
diff changeset
    88
normal_form "[x,y,z] @ [a,b,c] = [x, y, z, a, b ,c]"
cda5cd8bfd16 example tuned
haftmann
parents: 21196
diff changeset
    89
normal_form "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f]) = [a, b, c, d, e, f]"
21156
17f144c6e2f2 some example tweaking
haftmann
parents: 21117
diff changeset
    90
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()] = [False, True]"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    91
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    92
lemma "last [a, b, c] = c"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    93
  by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    94
lemma "last ([a, b, c] @ xs) = (if null xs then c else last xs)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    95
  by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    96
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    97
lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    98
lemma "(-4::int) * 2 = -8" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    99
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   100
lemma "(2::int) + 3 = 5" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   101
lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   102
lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   103
lemma "(2::int) < 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   104
lemma "(2::int) <= 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   105
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   106
lemma "4 - 42 * abs (3 + (-7\<Colon>int)) = -164" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   107
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
   108
lemma "4 = Suc (Suc (Suc (Suc 0)))" by normalization
54ea68b5a92f tuned code theorems for ord on integers
haftmann
parents: 21987
diff changeset
   109
lemma "nat 4 = Suc (Suc (Suc (Suc 0)))" by normalization
20922
14873e42659c added nbe_post for delayed_if
nipkow
parents: 20921
diff changeset
   110
21059
361e62500ab7 added if_delayed
haftmann
parents: 21046
diff changeset
   111
normal_form "Suc 0 \<in> set ms"
20922
14873e42659c added nbe_post for delayed_if
nipkow
parents: 20921
diff changeset
   112
21987
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   113
normal_form "f"
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   114
normal_form "f x"
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   115
normal_form "(f o g) x"
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   116
normal_form "(f o id) x"
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   117
normal_form "\<lambda>x. x"
29d5cdd1cc03 more term examples
haftmann
parents: 21460
diff changeset
   118
23396
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   119
(* Church numerals: *)
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   120
6d72ababc58f Church numerals added
nipkow
parents: 22845
diff changeset
   121
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
   122
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
   123
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
   124
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
   125
end