src/HOL/ex/NormalForm.thy
author nipkow
Mon, 09 Oct 2006 12:20:39 +0200
changeset 20921 24b8536dcf93
parent 20842 f5f69a1059f4
child 20922 14873e42659c
permissions -rw-r--r--
added delayed_if
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
20807
wenzelm
parents: 20595
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
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    11
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
    12
declare disj_assoc [code func]
20595
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
diff changeset
    13
lemma "((P | Q) | R) = (P | (Q | R))" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    14
lemma "0 + (n::nat) = n" by normalization
20595
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
diff changeset
    15
lemma "0 + Suc n = Suc n" by normalization
db6bedfba498 improved numeral handling for nbe
haftmann
parents: 20523
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
d909e782e247 renamed file
nipkow
parents:
diff changeset
    20
consts
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    21
  add :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    22
  add2 :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    23
  mul :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    24
  mul2 :: "n \<Rightarrow> n \<Rightarrow> n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    25
  exp :: "n \<Rightarrow> n \<Rightarrow> n"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    26
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    27
  "add Z = id"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    28
  "add (S m) = S o add m"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    29
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    30
  "add2 Z n = n"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    31
  "add2 (S m) n = S(add2 m n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    32
d909e782e247 renamed file
nipkow
parents:
diff changeset
    33
lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    34
  by(induct n) auto
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    35
lemma [code]: "add2 n (S m) =  S (add2 n m)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    36
  by(induct n) auto
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    37
lemma [code]: "add2 n Z = n"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    38
  by(induct n) auto
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    39
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    40
lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    41
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
    42
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
    43
d909e782e247 renamed file
nipkow
parents:
diff changeset
    44
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    45
  "mul Z = (%n. Z)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    46
  "mul (S m) = (%n. add (mul m n) n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    47
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    48
  "mul2 Z n = Z"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    49
  "mul2 (S m) n = add2 n (mul2 m n)"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    50
primrec
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    51
  "exp m Z = S Z"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    52
  "exp m (S n) = mul (exp m n) m"
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    53
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    54
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
    55
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
    56
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
    57
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    58
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
    59
lemma "split (%x y. x) (a, b) = a" by normalization
19971
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    60
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
    61
ddf69abaffa8 normal_form to lemma test
nipkow
parents: 19829
diff changeset
    62
lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    63
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    64
lemma "[] @ [] = []" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    65
lemma "[] @ xs = xs" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    66
lemma "[a \<Colon> 'd, b, c] @ xs = a # b # c # xs" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    67
lemma "[%a::'x. a, %b. b, c] @ xs = (%x. x) # (%x. x) # c # xs" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    68
lemma "[%a::'x. a, %b. b, c] @ [u,v] = [%x. x, %x. x, c, u, v]" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    69
lemma "map f [x,y,z::'x] = [f x, f y, f z]" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    70
normal_form "map (%f. f True) [id,g,Not]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    71
normal_form "map (%f. f True) ([id,g,Not] @ fs)"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    72
lemma "rev[a,b,c] = [c, b, a]" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    73
normal_form "rev(a#b#cs)"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    74
lemma "map map [f,g,h] = [map f, map g, map h]" by normalization
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
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    88
lemma "[x,y,z] @ [a,b,c] = [x, y, z, a, b ,c]" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    89
normal_form "%(xs, ys). xs @ ys"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    90
normal_form "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    91
normal_form "%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    92
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    93
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    94
lemma "last [a, b, c] = c"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    95
  by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    96
lemma "last ([a, b, c] @ xs) = (if null xs then c else last xs)"
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    97
  by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
    98
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
    99
lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   100
lemma "(-4::int) * 2 = -8" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   101
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   102
lemma "(2::int) + 3 = 5" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   103
lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   104
lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   105
lemma "(2::int) < 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   106
lemma "(2::int) <= 3" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   107
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   108
lemma "4 - 42 * abs (3 + (-7\<Colon>int)) = -164" by normalization
20352
bb56a6cbacac adding code lemma now works as expected
haftmann
parents: 20191
diff changeset
   109
normal_form "min 0 x"
bb56a6cbacac adding code lemma now works as expected
haftmann
parents: 20191
diff changeset
   110
normal_form "min 0 (x::nat)"
20842
f5f69a1059f4 cleaned and extended
haftmann
parents: 20807
diff changeset
   111
lemma "(if (0\<Colon>nat) \<le> (x\<Colon>nat) then 0\<Colon>nat else x) = 0" by normalization
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
   112
20921
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   113
(* Delaying if: FIXME move to HOL.thy(?) *)
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   114
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   115
definition delayed_if :: "bool \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> 'a"
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   116
"delayed_if b f g = (if b then f() else g())"
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   117
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   118
lemma [normal_pre]: "(if b then x else y) == delayed_if b (%u. x) (%u. y)"
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   119
unfolding delayed_if_def by simp
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   120
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   121
lemma [code func]:
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   122
 shows "delayed_if True f g = f()" and "delayed_if False f g = g()"
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   123
by (auto simp:delayed_if_def)
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   124
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   125
hide (open) const delayed_if
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   126
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   127
normal_form "OperationalEquality.eq [2..<4] [2,3]"
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   128
(*lemma "OperationalEquality.eq [2..<4] [2,3]" by normalization*)
24b8536dcf93 added delayed_if
nipkow
parents: 20842
diff changeset
   129
19829
d909e782e247 renamed file
nipkow
parents:
diff changeset
   130
end