24584

1 
(* Title: Tools/Compute_Oracle/am_interpreter.ML

23174

2 
ID: $Id$


3 
Author: Steven Obua


4 
*)


5 

24654

6 
signature AM_BARRAS =


7 
sig


8 
include ABSTRACT_MACHINE


9 
val max_reductions : int option ref


10 
end


11 


12 
structure AM_Interpreter : AM_BARRAS = struct

23174

13 

23663

14 
open AbstractMachine;

23174

15 

23663

16 
datatype closure = CDummy  CVar of int  CConst of int

23174

17 
 CApp of closure * closure  CAbs of closure


18 
 Closure of (closure list) * closure


19 


20 
structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);


21 


22 
datatype program = Program of ((pattern * closure) list) prog_struct.table


23 


24 
datatype stack = SEmpty  SAppL of closure * stack  SAppR of closure * stack  SAbs of stack


25 


26 
fun clos_of_term (Var x) = CVar x


27 
 clos_of_term (Const c) = CConst c


28 
 clos_of_term (App (u, v)) = CApp (clos_of_term u, clos_of_term v)


29 
 clos_of_term (Abs u) = CAbs (clos_of_term u)


30 


31 
fun term_of_clos (CVar x) = Var x


32 
 term_of_clos (CConst c) = Const c


33 
 term_of_clos (CApp (u, v)) = App (term_of_clos u, term_of_clos v)


34 
 term_of_clos (CAbs u) = Abs (term_of_clos u)


35 
 term_of_clos (Closure (e, u)) = raise (Run "internal error: closure in normalized term found")

23663

36 
 term_of_clos CDummy = raise (Run "internal error: dummy in normalized term found")

23174

37 

24654

38 
fun resolve_closure closures (CVar x) = (case List.nth (closures, x) of CDummy => CVar x  r => r)


39 
 resolve_closure closures (CConst c) = CConst c


40 
 resolve_closure closures (CApp (u, v)) = CApp (resolve_closure closures u, resolve_closure closures v)


41 
 resolve_closure closures (CAbs u) = CAbs (resolve_closure (CDummy::closures) u)


42 
 resolve_closure closures (CDummy) = raise (Run "internal error: resolve_closure applied to CDummy")


43 
 resolve_closure closures (Closure (e, u)) = resolve_closure e u


44 


45 
fun resolve_closure' c = resolve_closure [] c


46 


47 
fun resolve_stack tm SEmpty = tm


48 
 resolve_stack tm (SAppL (c, s)) = resolve_stack (CApp (tm, resolve_closure' c)) s


49 
 resolve_stack tm (SAppR (c, s)) = resolve_stack (CApp (resolve_closure' c, tm)) s


50 
 resolve_stack tm (SAbs s) = resolve_stack (CAbs tm) s


51 


52 
fun resolve (stack, closure) =


53 
let


54 
val _ = writeln "start resolving"


55 
val t = resolve_stack (resolve_closure' closure) stack


56 
val _ = writeln "finished resolving"


57 
in


58 
t


59 
end


60 

23174

61 
fun strip_closure args (CApp (a,b)) = strip_closure (b::args) a


62 
 strip_closure args x = (x, args)


63 


64 
fun len_head_of_closure n (CApp (a,b)) = len_head_of_closure (n+1) a


65 
 len_head_of_closure n x = (n, x)


66 


67 


68 
(* earlier occurrence of PVar corresponds to higher de Bruijn index *)


69 
fun pattern_match args PVar clos = SOME (clos::args)


70 
 pattern_match args (PConst (c, patterns)) clos =


71 
let


72 
val (f, closargs) = strip_closure [] clos


73 
in


74 
case f of


75 
CConst d =>


76 
if c = d then


77 
pattern_match_list args patterns closargs


78 
else


79 
NONE


80 
 _ => NONE


81 
end


82 
and pattern_match_list args [] [] = SOME args


83 
 pattern_match_list args (p::ps) (c::cs) =


84 
(case pattern_match args p c of


85 
NONE => NONE


86 
 SOME args => pattern_match_list args ps cs)


87 
 pattern_match_list _ _ _ = NONE


88 


89 
fun count_patternvars PVar = 1


90 
 count_patternvars (PConst (_, ps)) = List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps


91 


92 
fun pattern_key (PConst (c, ps)) = (c, length ps)


93 
 pattern_key _ = raise (Compile "pattern reduces to variable")


94 

23663

95 
(*Returns true iff at most 0 .. (free1) occur unbound. therefore


96 
check_freevars 0 t iff t is closed*)


97 
fun check_freevars free (Var x) = x < free


98 
 check_freevars free (Const c) = true


99 
 check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v


100 
 check_freevars free (Abs m) = check_freevars (free+1) m


101 

23174

102 
fun compile eqs =


103 
let

23663

104 
val _ = if exists (fn (a,b,c) => not (null a)) eqs then raise Compile ("cannot deal with guards") else ()


105 
val eqs = map (fn (a,b,c) => (b,c)) eqs


106 
fun check (p, r) = if check_freevars (count_patternvars p) r then () else raise Compile ("unbound variables in rule")


107 
val eqs = map (fn (p, r) => (check (p,r); (pattern_key p, (p, clos_of_term r)))) eqs


108 
fun merge (k, a) table = prog_struct.update (k, case prog_struct.lookup table k of NONE => [a]  SOME l => a::l) table


109 
val p = fold merge eqs prog_struct.empty

23174

110 
in

23663

111 
Program p

23174

112 
end


113 


114 
fun match_rules n [] clos = NONE


115 
 match_rules n ((p,eq)::rs) clos =


116 
case pattern_match [] p clos of


117 
NONE => match_rules (n+1) rs clos


118 
 SOME args => SOME (Closure (args, eq))


119 


120 
fun match_closure (Program prog) clos =


121 
case len_head_of_closure 0 clos of


122 
(len, CConst c) =>


123 
(case prog_struct.lookup prog (c, len) of


124 
NONE => NONE


125 
 SOME rules => match_rules 0 rules clos)


126 
 _ => NONE


127 

23663

128 


129 
type state = bool * program * stack * closure


130 


131 
datatype loopstate = Continue of state  Stop of stack * closure


132 


133 
fun proj_C (Continue s) = s


134 
 proj_C _ = raise Match


135 

24654

136 
exception InterruptedExecution of stack * closure


137 

23663

138 
fun proj_S (Stop s) = s

24654

139 
 proj_S (Continue (_,_,s,c)) = (s,c)

23663

140 


141 
fun cont (Continue _) = true


142 
 cont _ = false

23174

143 

24654

144 
val max_reductions = ref (NONE : int option)


145 

23663

146 
fun do_reduction reduce p =


147 
let


148 
val s = ref (Continue p)

24654

149 
val counter = ref 0


150 
val _ = case !max_reductions of


151 
NONE => while cont (!s) do (s := reduce (proj_C (!s)))


152 
 SOME m => while cont (!s) andalso (!counter < m) do (s := reduce (proj_C (!s)); counter := (!counter) + 1)

23663

153 
in

24654

154 
case !max_reductions of


155 
SOME m => if !counter >= m then raise InterruptedExecution (proj_S (!s)) else proj_S (!s)


156 
 NONE => proj_S (!s)

23663

157 
end

23174

158 

23663

159 
fun weak_reduce (false, prog, stack, Closure (e, CApp (a, b))) = Continue (false, prog, SAppL (Closure (e, b), stack), Closure (e, a))


160 
 weak_reduce (false, prog, SAppL (b, stack), Closure (e, CAbs m)) = Continue (false, prog, stack, Closure (b::e, m))


161 
 weak_reduce (false, prog, stack, Closure (e, CVar n)) = Continue (false, prog, stack, case List.nth (e, n) of CDummy => CVar n  r => r)


162 
 weak_reduce (false, prog, stack, Closure (e, c as CConst _)) = Continue (false, prog, stack, c)


163 
 weak_reduce (false, prog, stack, clos) =


164 
(case match_closure prog clos of


165 
NONE => Continue (true, prog, stack, clos)


166 
 SOME r => Continue (false, prog, stack, r))


167 
 weak_reduce (true, prog, SAppR (a, stack), b) = Continue (false, prog, stack, CApp (a,b))


168 
 weak_reduce (true, prog, s as (SAppL (b, stack)), a) = Continue (false, prog, SAppR (a, stack), b)


169 
 weak_reduce (true, prog, stack, c) = Stop (stack, c)


170 


171 
fun strong_reduce (false, prog, stack, Closure (e, CAbs m)) =

24654

172 
(let


173 
val (stack', wnf) = do_reduction weak_reduce (false, prog, SEmpty, Closure (CDummy::e, m))


174 
in


175 
case stack' of


176 
SEmpty => Continue (false, prog, SAbs stack, wnf)


177 
 _ => raise (Run "internal error in strong: weak failed")


178 
end handle InterruptedExecution state => raise InterruptedExecution (stack, resolve state))

23663

179 
 strong_reduce (false, prog, stack, clos as (CApp (u, v))) = Continue (false, prog, SAppL (v, stack), u)


180 
 strong_reduce (false, prog, stack, clos) = Continue (true, prog, stack, clos)


181 
 strong_reduce (true, prog, SAbs stack, m) = Continue (false, prog, stack, CAbs m)


182 
 strong_reduce (true, prog, SAppL (b, stack), a) = Continue (false, prog, SAppR (a, stack), b)


183 
 strong_reduce (true, prog, SAppR (a, stack), b) = Continue (true, prog, stack, CApp (a, b))


184 
 strong_reduce (true, prog, stack, clos) = Stop (stack, clos)

23174

185 


186 
fun run prog t =

24654

187 
(let


188 
val (stack, wnf) = do_reduction weak_reduce (false, prog, SEmpty, Closure ([], clos_of_term t))


189 
in


190 
case stack of


191 
SEmpty => (case do_reduction strong_reduce (false, prog, SEmpty, wnf) of


192 
(SEmpty, snf) => term_of_clos snf


193 
 _ => raise (Run "internal error in run: strong failed"))


194 
 _ => raise (Run "internal error in run: weak failed")


195 
end handle InterruptedExecution state => term_of_clos (resolve state))

23174

196 

23663

197 
fun discard p = ()

23174

198 

23663

199 
end
