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