src/HOL/ex/NormalForm.thy
author nipkow
Fri, 09 Jun 2006 12:17:37 +0200
changeset 19829 d909e782e247
child 19971 ddf69abaffa8
permissions -rw-r--r--
renamed file
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
d909e782e247 renamed file
nipkow
parents:
diff changeset
     3
d909e782e247 renamed file
nipkow
parents:
diff changeset
     4
Test of normalization function
d909e782e247 renamed file
nipkow
parents:
diff changeset
     5
*)
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
d909e782e247 renamed file
nipkow
parents:
diff changeset
    11
normal_form "True \<longrightarrow> p"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    12
d909e782e247 renamed file
nipkow
parents:
diff changeset
    13
(* FIXME Eventually the code generator should be able to handle this
d909e782e247 renamed file
nipkow
parents:
diff changeset
    14
by re-generating the existing code for "or":
d909e782e247 renamed file
nipkow
parents:
diff changeset
    15
d909e782e247 renamed file
nipkow
parents:
diff changeset
    16
declare disj_assoc[code]
d909e782e247 renamed file
nipkow
parents:
diff changeset
    17
d909e782e247 renamed file
nipkow
parents:
diff changeset
    18
normal_form "(P | Q) | R"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    19
d909e782e247 renamed file
nipkow
parents:
diff changeset
    20
*)
d909e782e247 renamed file
nipkow
parents:
diff changeset
    21
d909e782e247 renamed file
nipkow
parents:
diff changeset
    22
d909e782e247 renamed file
nipkow
parents:
diff changeset
    23
datatype n = Z | S n
d909e782e247 renamed file
nipkow
parents:
diff changeset
    24
consts
d909e782e247 renamed file
nipkow
parents:
diff changeset
    25
 add :: "n \<Rightarrow> n \<Rightarrow> n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    26
 add2 :: "n \<Rightarrow> n \<Rightarrow> n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    27
 mul :: "n \<Rightarrow> n \<Rightarrow> n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    28
 mul2 :: "n \<Rightarrow> n \<Rightarrow> n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    29
 exp :: "n \<Rightarrow> n \<Rightarrow> n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    30
primrec
d909e782e247 renamed file
nipkow
parents:
diff changeset
    31
"add Z = id"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    32
"add (S m) = S o add m"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    33
primrec
d909e782e247 renamed file
nipkow
parents:
diff changeset
    34
"add2 Z n = n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    35
"add2 (S m) n = S(add2 m n)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    36
d909e782e247 renamed file
nipkow
parents:
diff changeset
    37
lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    38
by(induct n, auto)
d909e782e247 renamed file
nipkow
parents:
diff changeset
    39
lemma [code]: "add2 n (S m) =  S(add2 n m)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    40
by(induct n, auto)
d909e782e247 renamed file
nipkow
parents:
diff changeset
    41
lemma [code]: "add2 n Z = n"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    42
by(induct n, auto)
d909e782e247 renamed file
nipkow
parents:
diff changeset
    43
 
d909e782e247 renamed file
nipkow
parents:
diff changeset
    44
normal_form "add2 (add2 n m) k"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    45
normal_form "add2 (add2 (S n) (S m)) (S k)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    46
normal_form "add2 (add2 (S n)(add2 (S m) Z)) (S k)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    47
d909e782e247 renamed file
nipkow
parents:
diff changeset
    48
primrec
d909e782e247 renamed file
nipkow
parents:
diff changeset
    49
"mul Z = (%n. Z)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    50
"mul (S m) = (%n. add (mul m n) n)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    51
primrec
d909e782e247 renamed file
nipkow
parents:
diff changeset
    52
"mul2 Z n = Z"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    53
"mul2 (S m) n = add2 n (mul2 m n)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    54
primrec
d909e782e247 renamed file
nipkow
parents:
diff changeset
    55
"exp m Z = S Z"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    56
"exp m (S n) = mul (exp m n) m"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    57
d909e782e247 renamed file
nipkow
parents:
diff changeset
    58
normal_form "mul2 (S(S(S(S(S(S(S Z))))))) (S(S(S(S(S Z)))))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    59
normal_form "mul (S(S(S(S(S(S(S Z))))))) (S(S(S(S(S Z)))))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    60
normal_form "exp (S(S Z)) (S(S(S(S(S Z)))))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    61
d909e782e247 renamed file
nipkow
parents:
diff changeset
    62
normal_form "[] @ []"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    63
normal_form "[] @ xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    64
normal_form "[] @ (xs:: 'b list)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    65
normal_form "[a::'d,b,c] @ xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    66
normal_form "[%a::'x. a, %b. b, c] @ xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    67
normal_form "[%a::'x. a, %b. b, c] @ [u,v]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    68
normal_form "map f (xs::'c list)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    69
normal_form "map f [x,y,z::'x]"
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)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    72
normal_form "rev[a,b,c]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    73
normal_form "rev(a#b#cs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    74
normal_form "map map [f,g,h]"
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 "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    81
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    82
normal_form "let x = y::'x in [x,x]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    83
normal_form "Let y (%x. [x,x])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    84
normal_form "case n of Z \<Rightarrow> True | S x \<Rightarrow> False"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    85
normal_form "(%(x,y). add x y) (S z,S z)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    86
normal_form "filter (%x. x) ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    87
normal_form "filter Not ([True,False,x]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    88
d909e782e247 renamed file
nipkow
parents:
diff changeset
    89
normal_form "0 + (n::nat)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    90
normal_form "0 + Suc(n)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    91
normal_form "0::nat"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    92
normal_form "Suc(n) + Suc m"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    93
normal_form "[] @ xs"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    94
normal_form "(x#xs) @ ys"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    95
normal_form "[x,y,z] @ [a,b,c]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    96
normal_form "%(xs, ys). xs @ ys"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    97
normal_form "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f])"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    98
normal_form "%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True"
d909e782e247 renamed file
nipkow
parents:
diff changeset
    99
normal_form "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   100
d909e782e247 renamed file
nipkow
parents:
diff changeset
   101
normal_form "case n of None \<Rightarrow> True | Some((x,y),(u,v)) \<Rightarrow> False"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   102
normal_form "let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   103
normal_form "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z))"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   104
normal_form "last[a,b,c]"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   105
normal_form "last([a,b,c]@xs)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   106
normal_form " (0::nat) < (0::nat)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   107
d909e782e247 renamed file
nipkow
parents:
diff changeset
   108
(* FIXME
d909e782e247 renamed file
nipkow
parents:
diff changeset
   109
  won't work since it relies on 
d909e782e247 renamed file
nipkow
parents:
diff changeset
   110
  polymorphically used ad-hoc overloaded function:
d909e782e247 renamed file
nipkow
parents:
diff changeset
   111
  normal_form "max 0 (0::nat)"
d909e782e247 renamed file
nipkow
parents:
diff changeset
   112
*)
d909e782e247 renamed file
nipkow
parents:
diff changeset
   113
d909e782e247 renamed file
nipkow
parents:
diff changeset
   114
text {*
d909e782e247 renamed file
nipkow
parents:
diff changeset
   115
  Numerals still take their time\<dots>
d909e782e247 renamed file
nipkow
parents:
diff changeset
   116
*}
d909e782e247 renamed file
nipkow
parents:
diff changeset
   117
d909e782e247 renamed file
nipkow
parents:
diff changeset
   118
end