src/HOL/ex/NormalForm.thy
author nipkow
Mon Oct 09 12:51:31 2006 +0200 (2006-10-09)
changeset 20922 14873e42659c
parent 20921 24b8536dcf93
child 21046 fe1db2f991a7
permissions -rw-r--r--
added nbe_post for delayed_if
nipkow@19829
     1
(*  ID:         $Id$
nipkow@19829
     2
    Authors:    Klaus Aehlig, Tobias Nipkow
wenzelm@20807
     3
*)
nipkow@19829
     4
wenzelm@20807
     5
header "Test of normalization function"
nipkow@19829
     6
nipkow@19829
     7
theory NormalForm
nipkow@19829
     8
imports Main
nipkow@19829
     9
begin
nipkow@19829
    10
nipkow@19971
    11
lemma "p \<longrightarrow> True" by normalization
krauss@20523
    12
declare disj_assoc [code func]
haftmann@20595
    13
lemma "((P | Q) | R) = (P | (Q | R))" by normalization
nipkow@19971
    14
lemma "0 + (n::nat) = n" by normalization
haftmann@20595
    15
lemma "0 + Suc n = Suc n" by normalization
haftmann@20595
    16
lemma "Suc n + Suc m = n + Suc (Suc m)" by normalization
nipkow@19971
    17
lemma "~((0::nat) < (0::nat))" by normalization
nipkow@19971
    18
nipkow@19829
    19
datatype n = Z | S n
nipkow@19829
    20
consts
haftmann@20842
    21
  add :: "n \<Rightarrow> n \<Rightarrow> n"
haftmann@20842
    22
  add2 :: "n \<Rightarrow> n \<Rightarrow> n"
haftmann@20842
    23
  mul :: "n \<Rightarrow> n \<Rightarrow> n"
haftmann@20842
    24
  mul2 :: "n \<Rightarrow> n \<Rightarrow> n"
haftmann@20842
    25
  exp :: "n \<Rightarrow> n \<Rightarrow> n"
nipkow@19829
    26
primrec
haftmann@20842
    27
  "add Z = id"
haftmann@20842
    28
  "add (S m) = S o add m"
nipkow@19829
    29
primrec
haftmann@20842
    30
  "add2 Z n = n"
haftmann@20842
    31
  "add2 (S m) n = S(add2 m n)"
nipkow@19829
    32
nipkow@19829
    33
lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)"
haftmann@20842
    34
  by(induct n) auto
haftmann@20842
    35
lemma [code]: "add2 n (S m) =  S (add2 n m)"
haftmann@20842
    36
  by(induct n) auto
nipkow@19829
    37
lemma [code]: "add2 n Z = n"
haftmann@20842
    38
  by(induct n) auto
nipkow@19971
    39
nipkow@19971
    40
lemma "add2 (add2 n m) k = add2 n (add2 m k)" by normalization
nipkow@19971
    41
lemma "add2 (add2 (S n) (S m)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
nipkow@19971
    42
lemma "add2 (add2 (S n) (add2 (S m) Z)) (S k) = S(S(S(add2 n (add2 m k))))" by normalization
nipkow@19829
    43
nipkow@19829
    44
primrec
haftmann@20842
    45
  "mul Z = (%n. Z)"
haftmann@20842
    46
  "mul (S m) = (%n. add (mul m n) n)"
nipkow@19829
    47
primrec
haftmann@20842
    48
  "mul2 Z n = Z"
haftmann@20842
    49
  "mul2 (S m) n = add2 n (mul2 m n)"
nipkow@19829
    50
primrec
haftmann@20842
    51
  "exp m Z = S Z"
haftmann@20842
    52
  "exp m (S n) = mul (exp m n) m"
nipkow@19829
    53
nipkow@19971
    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
nipkow@19971
    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
nipkow@19971
    56
lemma "exp (S(S Z)) (S(S(S(S Z)))) = exp (S(S(S(S Z)))) (S(S Z))" by normalization
nipkow@19971
    57
nipkow@19971
    58
lemma "(let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)) = Z" by normalization
haftmann@20842
    59
lemma "split (%x y. x) (a, b) = a" by normalization
nipkow@19971
    60
lemma "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z)) = Z" by normalization
nipkow@19971
    61
nipkow@19971
    62
lemma "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" by normalization
nipkow@19829
    63
haftmann@20842
    64
lemma "[] @ [] = []" by normalization
haftmann@20842
    65
lemma "[] @ xs = xs" by normalization
haftmann@20842
    66
lemma "[a \<Colon> 'd, b, c] @ xs = a # b # c # xs" by normalization
haftmann@20842
    67
lemma "[%a::'x. a, %b. b, c] @ xs = (%x. x) # (%x. x) # c # xs" by normalization
haftmann@20842
    68
lemma "[%a::'x. a, %b. b, c] @ [u,v] = [%x. x, %x. x, c, u, v]" by normalization
haftmann@20842
    69
lemma "map f [x,y,z::'x] = [f x, f y, f z]" by normalization
nipkow@19829
    70
normal_form "map (%f. f True) [id,g,Not]"
nipkow@19829
    71
normal_form "map (%f. f True) ([id,g,Not] @ fs)"
haftmann@20842
    72
lemma "rev[a,b,c] = [c, b, a]" by normalization
nipkow@19829
    73
normal_form "rev(a#b#cs)"
haftmann@20842
    74
lemma "map map [f,g,h] = [map f, map g, map h]" by normalization
nipkow@19829
    75
normal_form "map (%F. F [a,b,c::'x]) (map map [f,g,h])"
nipkow@19829
    76
normal_form "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))"
nipkow@19829
    77
normal_form "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])"
nipkow@19829
    78
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]"
nipkow@19829
    79
normal_form "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False"
nipkow@19829
    80
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs"
nipkow@19829
    81
normal_form "let x = y::'x in [x,x]"
nipkow@19829
    82
normal_form "Let y (%x. [x,x])"
nipkow@19829
    83
normal_form "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
nipkow@19829
    84
normal_form "(%(x,y). add x y) (S z,S z)"
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@20842
    88
lemma "[x,y,z] @ [a,b,c] = [x, y, z, a, b ,c]" by normalization
nipkow@19829
    89
normal_form "%(xs, ys). xs @ ys"
nipkow@19829
    90
normal_form "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f])"
nipkow@19829
    91
normal_form "%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True"
nipkow@19829
    92
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]"
nipkow@19829
    93
haftmann@20842
    94
lemma "last [a, b, c] = c"
haftmann@20842
    95
  by normalization
haftmann@20842
    96
lemma "last ([a, b, c] @ xs) = (if null xs then c else last xs)"
haftmann@20842
    97
  by normalization
nipkow@19829
    98
haftmann@20842
    99
lemma "(2::int) + 3 - 1 + (- k) * 2 = 4 + - k * 2" by normalization
haftmann@20842
   100
lemma "(-4::int) * 2 = -8" by normalization
haftmann@20842
   101
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
haftmann@20842
   102
lemma "(2::int) + 3 = 5" by normalization
haftmann@20842
   103
lemma "(2::int) + 3 * (- 4) * (- 1) = 14" by normalization
haftmann@20842
   104
lemma "(2::int) + 3 * (- 4) * 1 + 0 = -10" by normalization
haftmann@20842
   105
lemma "(2::int) < 3" by normalization
haftmann@20842
   106
lemma "(2::int) <= 3" by normalization
haftmann@20842
   107
lemma "abs ((-4::int) + 2 * 1) = 2" by normalization
haftmann@20842
   108
lemma "4 - 42 * abs (3 + (-7\<Colon>int)) = -164" by normalization
haftmann@20352
   109
normal_form "min 0 x"
haftmann@20352
   110
normal_form "min 0 (x::nat)"
haftmann@20842
   111
lemma "(if (0\<Colon>nat) \<le> (x\<Colon>nat) then 0\<Colon>nat else x) = 0" by normalization
nipkow@19829
   112
nipkow@20921
   113
(* Delaying if: FIXME move to HOL.thy(?) *)
nipkow@20921
   114
nipkow@20921
   115
definition delayed_if :: "bool \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> (unit \<Rightarrow> 'a) \<Rightarrow> 'a"
nipkow@20921
   116
"delayed_if b f g = (if b then f() else g())"
nipkow@20921
   117
nipkow@20921
   118
lemma [normal_pre]: "(if b then x else y) == delayed_if b (%u. x) (%u. y)"
nipkow@20921
   119
unfolding delayed_if_def by simp
nipkow@20921
   120
nipkow@20922
   121
lemma [normal_post]: "delayed_if b f g == (if b then f() else g())"
nipkow@20922
   122
unfolding delayed_if_def by simp
nipkow@20922
   123
nipkow@20921
   124
lemma [code func]:
nipkow@20921
   125
 shows "delayed_if True f g = f()" and "delayed_if False f g = g()"
nipkow@20921
   126
by (auto simp:delayed_if_def)
nipkow@20921
   127
nipkow@20921
   128
hide (open) const delayed_if
nipkow@20921
   129
nipkow@20921
   130
normal_form "OperationalEquality.eq [2..<4] [2,3]"
nipkow@20921
   131
(*lemma "OperationalEquality.eq [2..<4] [2,3]" by normalization*)
nipkow@20921
   132
nipkow@20922
   133
definition
nipkow@20922
   134
 andalso :: "bool \<Rightarrow> bool \<Rightarrow> bool"
nipkow@20922
   135
"andalso x y = (if x then y else False)"
nipkow@20922
   136
nipkow@20922
   137
lemma "andalso a b = (if a then b else False)" by normalization
nipkow@20922
   138
nipkow@19829
   139
end