24584
|
1 |
(* Title: Tools/Compute_Oracle/am_compiler.ML
|
23174
|
2 |
ID: $Id$
|
|
3 |
Author: Steven Obua
|
|
4 |
*)
|
|
5 |
|
|
6 |
signature COMPILING_AM =
|
|
7 |
sig
|
|
8 |
include ABSTRACT_MACHINE
|
|
9 |
|
23663
|
10 |
val set_compiled_rewriter : (term -> term) -> unit
|
23174
|
11 |
val list_nth : 'a list * int -> 'a
|
|
12 |
val list_map : ('a -> 'b) -> 'a list -> 'b list
|
|
13 |
end
|
|
14 |
|
|
15 |
structure AM_Compiler : COMPILING_AM = struct
|
|
16 |
|
|
17 |
val list_nth = List.nth;
|
|
18 |
val list_map = map;
|
|
19 |
|
23663
|
20 |
open AbstractMachine;
|
23174
|
21 |
|
23663
|
22 |
val compiled_rewriter = ref (NONE:(term -> term)Option.option)
|
23174
|
23 |
|
|
24 |
fun set_compiled_rewriter r = (compiled_rewriter := SOME r)
|
|
25 |
|
|
26 |
type program = (term -> term)
|
|
27 |
|
|
28 |
|
|
29 |
(*Returns true iff at most 0 .. (free-1) occur unbound. therefore
|
|
30 |
check_freevars 0 t iff t is closed*)
|
|
31 |
fun check_freevars free (Var x) = x < free
|
|
32 |
| check_freevars free (Const c) = true
|
|
33 |
| check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v
|
|
34 |
| check_freevars free (Abs m) = check_freevars (free+1) m
|
|
35 |
|
|
36 |
fun count_patternvars PVar = 1
|
|
37 |
| count_patternvars (PConst (_, ps)) =
|
|
38 |
List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
|
|
39 |
|
|
40 |
fun print_rule (p, t) =
|
|
41 |
let
|
|
42 |
fun str x = Int.toString x
|
|
43 |
fun print_pattern n PVar = (n+1, "x"^(str n))
|
|
44 |
| print_pattern n (PConst (c, [])) = (n, "c"^(str c))
|
|
45 |
| print_pattern n (PConst (c, args)) =
|
|
46 |
let
|
|
47 |
val h = print_pattern n (PConst (c,[]))
|
|
48 |
in
|
|
49 |
print_pattern_list h args
|
|
50 |
end
|
|
51 |
and print_pattern_list r [] = r
|
|
52 |
| print_pattern_list (n, p) (t::ts) =
|
|
53 |
let
|
|
54 |
val (n, t) = print_pattern n t
|
|
55 |
in
|
|
56 |
print_pattern_list (n, "App ("^p^", "^t^")") ts
|
|
57 |
end
|
|
58 |
|
|
59 |
val (n, pattern) = print_pattern 0 p
|
|
60 |
val pattern =
|
|
61 |
if exists_string Symbol.is_ascii_blank pattern then "(" ^ pattern ^")"
|
|
62 |
else pattern
|
|
63 |
|
|
64 |
fun print_term d (Var x) = (*if x < d then "Var "^(str x) else "x"^(str (n-(x-d)-1))*)
|
|
65 |
"Var " ^ str x
|
|
66 |
| print_term d (Const c) = "c" ^ str c
|
|
67 |
| print_term d (App (a,b)) = "App (" ^ print_term d a ^ ", " ^ print_term d b ^ ")"
|
|
68 |
| print_term d (Abs c) = "Abs (" ^ print_term (d + 1) c ^ ")"
|
|
69 |
|
|
70 |
fun listvars n = if n = 0 then "x0" else "x"^(str n)^", "^(listvars (n-1))
|
|
71 |
|
|
72 |
val term = print_term 0 t
|
|
73 |
val term =
|
|
74 |
if n > 0 then "Closure (["^(listvars (n-1))^"], "^term^")"
|
|
75 |
else "Closure ([], "^term^")"
|
|
76 |
|
|
77 |
in
|
23663
|
78 |
" | weak_reduce (false, stack, "^pattern^") = Continue (false, stack, "^term^")"
|
23174
|
79 |
end
|
|
80 |
|
|
81 |
fun constants_of PVar = []
|
|
82 |
| constants_of (PConst (c, ps)) = c :: maps constants_of ps
|
|
83 |
|
|
84 |
fun constants_of_term (Var _) = []
|
|
85 |
| constants_of_term (Abs m) = constants_of_term m
|
|
86 |
| constants_of_term (App (a,b)) = (constants_of_term a)@(constants_of_term b)
|
|
87 |
| constants_of_term (Const c) = [c]
|
|
88 |
|
|
89 |
fun load_rules sname name prog =
|
|
90 |
let
|
|
91 |
val buffer = ref ""
|
|
92 |
fun write s = (buffer := (!buffer)^s)
|
|
93 |
fun writeln s = (write s; write "\n")
|
|
94 |
fun writelist [] = ()
|
|
95 |
| writelist (s::ss) = (writeln s; writelist ss)
|
|
96 |
fun str i = Int.toString i
|
|
97 |
val _ = writelist [
|
|
98 |
"structure "^name^" = struct",
|
|
99 |
"",
|
23663
|
100 |
"datatype term = Dummy | App of term * term | Abs of term | Var of int | Const of int | Closure of term list * term"]
|
23174
|
101 |
val constants = distinct (op =) (maps (fn (p, r) => ((constants_of p)@(constants_of_term r))) prog)
|
|
102 |
val _ = map (fn x => write (" | c"^(str x))) constants
|
|
103 |
val _ = writelist [
|
|
104 |
"",
|
|
105 |
"datatype stack = SEmpty | SAppL of term * stack | SAppR of term * stack | SAbs of stack",
|
23663
|
106 |
"",
|
|
107 |
"type state = bool * stack * term",
|
|
108 |
"",
|
|
109 |
"datatype loopstate = Continue of state | Stop of stack * term",
|
|
110 |
"",
|
|
111 |
"fun proj_C (Continue s) = s",
|
|
112 |
" | proj_C _ = raise Match",
|
|
113 |
"",
|
|
114 |
"fun proj_S (Stop s) = s",
|
|
115 |
" | proj_S _ = raise Match",
|
|
116 |
"",
|
|
117 |
"fun cont (Continue _) = true",
|
|
118 |
" | cont _ = false",
|
23174
|
119 |
"",
|
23663
|
120 |
"fun do_reduction reduce p =",
|
|
121 |
" let",
|
|
122 |
" val s = ref (Continue p)",
|
|
123 |
" val _ = while cont (!s) do (s := reduce (proj_C (!s)))",
|
|
124 |
" in",
|
|
125 |
" proj_S (!s)",
|
|
126 |
" end",
|
|
127 |
""]
|
|
128 |
|
|
129 |
val _ = writelist [
|
|
130 |
"fun weak_reduce (false, stack, Closure (e, App (a, b))) = Continue (false, SAppL (Closure (e, b), stack), Closure (e, a))",
|
|
131 |
" | weak_reduce (false, SAppL (b, stack), Closure (e, Abs m)) = Continue (false, stack, Closure (b::e, m))",
|
|
132 |
" | weak_reduce (false, stack, c as Closure (e, Abs m)) = Continue (true, stack, c)",
|
|
133 |
" | weak_reduce (false, stack, Closure (e, Var n)) = Continue (false, stack, case "^sname^".list_nth (e, n) of Dummy => Var n | r => r)",
|
|
134 |
" | weak_reduce (false, stack, Closure (e, c)) = Continue (false, stack, c)"]
|
|
135 |
val _ = writelist (map print_rule prog)
|
|
136 |
val _ = writelist [
|
|
137 |
" | weak_reduce (false, stack, clos) = Continue (true, stack, clos)",
|
|
138 |
" | weak_reduce (true, SAppR (a, stack), b) = Continue (false, stack, App (a,b))",
|
|
139 |
" | weak_reduce (true, s as (SAppL (b, stack)), a) = Continue (false, SAppR (a, stack), b)",
|
|
140 |
" | weak_reduce (true, stack, c) = Stop (stack, c)",
|
23174
|
141 |
"",
|
23663
|
142 |
"fun strong_reduce (false, stack, Closure (e, Abs m)) =",
|
23174
|
143 |
" let",
|
23663
|
144 |
" val (stack', wnf) = do_reduction weak_reduce (false, SEmpty, Closure (Dummy::e, m))",
|
23174
|
145 |
" in",
|
23663
|
146 |
" case stack' of",
|
|
147 |
" SEmpty => Continue (false, SAbs stack, wnf)",
|
|
148 |
" | _ => raise ("^sname^".Run \"internal error in strong: weak failed\")",
|
|
149 |
" end",
|
|
150 |
" | strong_reduce (false, stack, clos as (App (u, v))) = Continue (false, SAppL (v, stack), u)",
|
|
151 |
" | strong_reduce (false, stack, clos) = Continue (true, stack, clos)",
|
|
152 |
" | strong_reduce (true, SAbs stack, m) = Continue (false, stack, Abs m)",
|
|
153 |
" | strong_reduce (true, SAppL (b, stack), a) = Continue (false, SAppR (a, stack), b)",
|
|
154 |
" | strong_reduce (true, SAppR (a, stack), b) = Continue (true, stack, App (a, b))",
|
|
155 |
" | strong_reduce (true, stack, clos) = Stop (stack, clos)",
|
23174
|
156 |
""]
|
|
157 |
|
|
158 |
val ic = "(case c of "^(implode (map (fn c => (str c)^" => c"^(str c)^" | ") constants))^" _ => Const c)"
|
|
159 |
val _ = writelist [
|
|
160 |
"fun importTerm ("^sname^".Var x) = Var x",
|
|
161 |
" | importTerm ("^sname^".Const c) = "^ic,
|
|
162 |
" | importTerm ("^sname^".App (a, b)) = App (importTerm a, importTerm b)",
|
|
163 |
" | importTerm ("^sname^".Abs m) = Abs (importTerm m)",
|
|
164 |
""]
|
|
165 |
|
23663
|
166 |
fun ec c = " | exportTerm c"^(str c)^" = "^sname^".Const "^(str c)
|
23174
|
167 |
val _ = writelist [
|
23663
|
168 |
"fun exportTerm (Var x) = "^sname^".Var x",
|
|
169 |
" | exportTerm (Const c) = "^sname^".Const c",
|
|
170 |
" | exportTerm (App (a,b)) = "^sname^".App (exportTerm a, exportTerm b)",
|
|
171 |
" | exportTerm (Abs m) = "^sname^".Abs (exportTerm m)",
|
|
172 |
" | exportTerm (Closure (closlist, clos)) = raise ("^sname^".Run \"internal error, cannot export Closure\")",
|
|
173 |
" | exportTerm Dummy = raise ("^sname^".Run \"internal error, cannot export Dummy\")"]
|
23174
|
174 |
val _ = writelist (map ec constants)
|
|
175 |
|
|
176 |
val _ = writelist [
|
|
177 |
"",
|
|
178 |
"fun rewrite t = ",
|
|
179 |
" let",
|
23663
|
180 |
" val (stack, wnf) = do_reduction weak_reduce (false, SEmpty, Closure ([], importTerm t))",
|
23174
|
181 |
" in",
|
|
182 |
" case stack of ",
|
23663
|
183 |
" SEmpty => (case do_reduction strong_reduce (false, SEmpty, wnf) of",
|
23174
|
184 |
" (SEmpty, snf) => exportTerm snf",
|
|
185 |
" | _ => raise ("^sname^".Run \"internal error in rewrite: strong failed\"))",
|
|
186 |
" | _ => (raise ("^sname^".Run \"internal error in rewrite: weak failed\"))",
|
|
187 |
" end",
|
|
188 |
"",
|
|
189 |
"val _ = "^sname^".set_compiled_rewriter rewrite",
|
|
190 |
"",
|
|
191 |
"end;"]
|
|
192 |
|
|
193 |
in
|
|
194 |
compiled_rewriter := NONE;
|
|
195 |
use_text "" Output.ml_output false (!buffer);
|
|
196 |
case !compiled_rewriter of
|
|
197 |
NONE => raise (Compile "cannot communicate with compiled function")
|
23663
|
198 |
| SOME r => (compiled_rewriter := NONE; r)
|
23174
|
199 |
end
|
|
200 |
|
|
201 |
fun compile eqs =
|
|
202 |
let
|
23663
|
203 |
val _ = if exists (fn (a,b,c) => not (null a)) eqs then raise Compile ("cannot deal with guards") else ()
|
|
204 |
val eqs = map (fn (a,b,c) => (b,c)) eqs
|
|
205 |
fun check (p, r) = if check_freevars (count_patternvars p) r then () else raise Compile ("unbound variables in rule")
|
23174
|
206 |
val _ = map (fn (p, r) =>
|
23663
|
207 |
(check (p, r);
|
|
208 |
case p of PVar => raise (Compile "pattern is just a variable") | _ => ())) eqs
|
23174
|
209 |
in
|
|
210 |
load_rules "AM_Compiler" "AM_compiled_code" eqs
|
|
211 |
end
|
|
212 |
|
|
213 |
fun run prog t = (prog t)
|
23663
|
214 |
|
|
215 |
fun discard p = ()
|
23174
|
216 |
|
|
217 |
end
|
|
218 |
|