author | nipkow |
Tue, 06 Jun 2006 19:16:42 +0200 | |
changeset 19794 | 100ba10eee64 |
parent 19238 | a2a4e6838bfc |
permissions | -rw-r--r-- |
19148 | 1 |
(* ID: $Id$ |
2 |
||
3 |
Temporary test of nbe module. |
|
4 |
*) |
|
5 |
||
6 |
theory nbe |
|
7 |
imports Main |
|
8 |
begin |
|
19202 | 9 |
|
19237 | 10 |
ML "reset quick_and_dirty" |
19148 | 11 |
|
19794 | 12 |
norm_by_eval "True \<longrightarrow> p" |
13 |
||
14 |
(* FIXME Eventually the code generator should be able to handle this |
|
15 |
by re-generating the existing code for "or": |
|
16 |
||
19180 | 17 |
declare disj_assoc[code] |
18 |
||
19 |
norm_by_eval "(P | Q) | R" |
|
20 |
||
19794 | 21 |
lemma[code]: "(P \<longrightarrow> P) = True" by blast |
19180 | 22 |
norm_by_eval "P \<longrightarrow> P" |
19794 | 23 |
norm_by_eval "True \<longrightarrow> P" |
24 |
*) |
|
19180 | 25 |
|
26 |
||
27 |
datatype n = Z | S n |
|
28 |
consts |
|
29 |
add :: "n \<Rightarrow> n \<Rightarrow> n" |
|
30 |
add2 :: "n \<Rightarrow> n \<Rightarrow> n" |
|
31 |
mul :: "n \<Rightarrow> n \<Rightarrow> n" |
|
32 |
mul2 :: "n \<Rightarrow> n \<Rightarrow> n" |
|
33 |
exp :: "n \<Rightarrow> n \<Rightarrow> n" |
|
34 |
primrec |
|
35 |
"add Z = id" |
|
36 |
"add (S m) = S o add m" |
|
37 |
primrec |
|
38 |
"add2 Z n = n" |
|
39 |
"add2 (S m) n = S(add2 m n)" |
|
40 |
||
41 |
lemma [code]: "add2 (add2 n m) k = add2 n (add2 m k)" |
|
42 |
by(induct n, auto) |
|
43 |
lemma [code]: "add2 n (S m) = S(add2 n m)" |
|
44 |
by(induct n, auto) |
|
45 |
lemma [code]: "add2 n Z = n" |
|
46 |
by(induct n, auto) |
|
47 |
||
48 |
norm_by_eval "add2 (add2 n m) k" |
|
49 |
norm_by_eval "add2 (add2 (S n) (S m)) (S k)" |
|
50 |
norm_by_eval "add2 (add2 (S n)(add2 (S m) Z)) (S k)" |
|
51 |
||
52 |
primrec |
|
53 |
"mul Z = (%n. Z)" |
|
54 |
"mul (S m) = (%n. add (mul m n) n)" |
|
55 |
primrec |
|
56 |
"mul2 Z n = Z" |
|
57 |
"mul2 (S m) n = add2 n (mul2 m n)" |
|
58 |
primrec |
|
59 |
"exp m Z = S Z" |
|
60 |
"exp m (S n) = mul (exp m n) m" |
|
61 |
||
62 |
norm_by_eval "mul2 (S(S(S(S(S(S(S Z))))))) (S(S(S(S(S Z)))))" |
|
63 |
norm_by_eval "mul (S(S(S(S(S(S(S Z))))))) (S(S(S(S(S Z)))))" |
|
19794 | 64 |
norm_by_eval "exp (S(S Z)) (S(S(S(S(S Z)))))" |
19180 | 65 |
|
66 |
norm_by_eval "[] @ []" |
|
67 |
norm_by_eval "[] @ xs" |
|
19794 | 68 |
norm_by_eval "[] @ (xs:: 'b list)" |
69 |
norm_by_eval "[a::'d,b,c] @ xs" |
|
70 |
norm_by_eval "[%a::'x. a, %b. b, c] @ xs" |
|
71 |
norm_by_eval "[%a::'x. a, %b. b, c] @ [u,v]" |
|
72 |
norm_by_eval "map f (xs::'c list)" |
|
73 |
norm_by_eval "map f [x,y,z::'x]" |
|
19180 | 74 |
norm_by_eval "map (%f. f True) [id,g,Not]" |
75 |
norm_by_eval "map (%f. f True) ([id,g,Not] @ fs)" |
|
76 |
norm_by_eval "rev[a,b,c]" |
|
77 |
norm_by_eval "rev(a#b#cs)" |
|
78 |
norm_by_eval "map map [f,g,h]" |
|
19794 | 79 |
norm_by_eval "map (%F. F [a,b,c::'x]) (map map [f,g,h])" |
19180 | 80 |
norm_by_eval "map (%F. F ([a,b,c] @ ds)) (map map ([f,g,h]@fs))" |
81 |
norm_by_eval "map (%F. F [Z,S Z,S(S Z)]) (map map [S,add (S Z),mul (S(S Z)),id])" |
|
82 |
norm_by_eval "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]" |
|
83 |
norm_by_eval "case xs of [] \<Rightarrow> True | x#xs \<Rightarrow> False" |
|
84 |
norm_by_eval "case Z of Z \<Rightarrow> True | S x \<Rightarrow> False" |
|
85 |
norm_by_eval "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) xs" |
|
19794 | 86 |
norm_by_eval "let x = y::'x in [x,x]" |
19180 | 87 |
norm_by_eval "Let y (%x. [x,x])" |
88 |
norm_by_eval "case n of Z \<Rightarrow> True | S x \<Rightarrow> False" |
|
89 |
norm_by_eval "(%(x,y). add x y) (S z,S z)" |
|
90 |
norm_by_eval "filter (%x. x) ([True,False,x]@xs)" |
|
91 |
norm_by_eval "filter Not ([True,False,x]@xs)" |
|
92 |
||
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
93 |
norm_by_eval "0 + (n::nat)" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
94 |
norm_by_eval "0 + Suc(n)" |
19794 | 95 |
norm_by_eval "0::nat" |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
96 |
norm_by_eval "Suc(n) + Suc m" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
97 |
norm_by_eval "[] @ xs" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
98 |
norm_by_eval "(x#xs) @ ys" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
99 |
norm_by_eval "[x,y,z] @ [a,b,c]" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
100 |
norm_by_eval "%(xs, ys). xs @ ys" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
101 |
norm_by_eval "(%(xs, ys). xs @ ys) ([a, b, c], [d, e, f])" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
102 |
norm_by_eval "%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True" |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19148
diff
changeset
|
103 |
norm_by_eval "map (%x. case x of None \<Rightarrow> False | Some y \<Rightarrow> True) [None, Some ()]" |
19148 | 104 |
|
19180 | 105 |
norm_by_eval "case n of None \<Rightarrow> True | Some((x,y),(u,v)) \<Rightarrow> False" |
106 |
norm_by_eval "let ((x,y),(u,v)) = ((Z,Z),(Z,Z)) in add (add x y) (add u v)" |
|
107 |
norm_by_eval "(%((x,y),(u,v)). add (add x y) (add u v)) ((Z,Z),(Z,Z))" |
|
108 |
norm_by_eval "last[a,b,c]" |
|
19794 | 109 |
norm_by_eval "last([a,b,c]@xs)" |
110 |
norm_by_eval " (0::nat) < (0::nat)" |
|
19202 | 111 |
|
19794 | 112 |
(* FIXME |
19202 | 113 |
won't work since it relies on |
114 |
polymorphically used ad-hoc overloaded function: |
|
115 |
norm_by_eval "max 0 (0::nat)" |
|
19235 | 116 |
*) |
19180 | 117 |
|
19238 | 118 |
text {* |
19202 | 119 |
Numerals still take their time\<dots> |
19238 | 120 |
*} |
19202 | 121 |
|
122 |
end |