fun resolve (stack, closure) = let val _ = writeln "start resolving" val t = resolve_stack (resolve_closure' closure) stack val _ = writeln "finished resolving" in
t end
fun strip_closure args (CApp (a,b)) = strip_closure (b::args) a
| strip_closure args x = (x, args)
fun len_head_of_closure n (CApp (a, _)) = len_head_of_closure (n+1) a
| len_head_of_closure n x = (n, x)
(* earlier occurrence of PVar corresponds to higher de Bruijn index *) fun pattern_match args PVar clos = SOME (clos::args)
| pattern_match args (PConst (c, patterns)) clos = let val (f, closargs) = strip_closure [] clos in case f of
CConst d => if c = d then
pattern_match_list args patterns closargs else
NONE
| _ => NONE end and pattern_match_list args [] [] = SOME args
| pattern_match_list args (p::ps) (c::cs) =
(case pattern_match args p c of
NONE => NONE
| SOME args => pattern_match_list args ps cs)
| pattern_match_list _ _ _ = NONE
fun pattern_key (PConst (c, ps)) = (c, length ps)
| pattern_key _ = raise (Compile "pattern reduces to variable")
(*Returns true iff at most 0 .. (free-1) occur unbound. therefore
check_freevars 0 t iff t is closed*) fun check_freevars free (Var x) = x < free
| check_freevars free (Const _) = true
| check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v
| check_freevars free (Abs m) = check_freevars (free+1) m
| check_freevars free (Computed t) = check_freevars free t
fun compile eqs = let fun check p r = if check_freevars p r then () elseraise Compile ("unbound variables in rule") fun check_guard p (Guard (a,b)) = (check p a; check p b) fun clos_of_guard (Guard (a,b)) = (clos_of_term a, clos_of_term b) val eqs = map (fn (guards, p, r) => letval pcount = count_patternvars p val _ = map (check_guard pcount) (guards) val _ = check pcount r in
(pattern_key p, (p, clos_of_term r, map clos_of_guard guards)) end) eqs fun merge (k, a) table = prog_struct.update (k, case prog_struct.lookup table k of NONE => [a] | SOME l => a::l) table val p = fold merge eqs prog_struct.empty in
Program p end
type state = bool * program * stack * closure
datatype loopstate = Continue of state | Stop of stack * closure
fun proj_C (Continue s) = s
| proj_C _ = raiseMatch
exception InterruptedExecution of stack * closure
fun proj_S (Stop s) = s
| proj_S (Continue (_,_,s,c)) = (s,c)
fun cont (Continue _) = true
| cont _ = false
val max_reductions = Unsynchronized.ref (NONE : int option)
fun do_reduction reduce p = let val s = Unsynchronized.ref (Continue p) val counter = Unsynchronized.ref 0 val _ = case !max_reductions of
NONE => while cont (!s) do (s := reduce (proj_C (!s)))
| SOME m => while cont (!s) andalso (!counter < m) do (s := reduce (proj_C (!s)); counter := (!counter) + 1) in case !max_reductions of
SOME m => if !counter >= m thenraise InterruptedExecution (proj_S (!s)) else proj_S (!s)
| NONE => proj_S (!s) end
fun match_rules prog n [] clos = NONE
| match_rules prog n ((p,eq,guards)::rs) clos = case pattern_match [] p clos of
NONE => match_rules prog (n+1) rs clos
| SOME args => if forall (guard_checks prog args) guards then SOME (Closure (args, eq)) else match_rules prog (n+1) rs clos and guard_checks prog args (a,b) = (simp prog (Closure (args, a)) = simp prog (Closure (args, b))) and match_closure (p as (Program prog)) clos = case len_head_of_closure 0 clos of
(len, CConst c) =>
(case prog_struct.lookup prog (c, len) of
NONE => NONE
| SOME rules => match_rules p 0 rules clos)
| _ => NONE
and weak_reduce (false, prog, stack, Closure (e, CApp (a, b))) = Continue (false, prog, SAppL (Closure (e, b), stack), Closure (e, a))
| weak_reduce (false, prog, SAppL (b, stack), Closure (e, CAbs m)) = Continue (false, prog, stack, Closure (b::e, m))
| weak_reduce (false, prog, stack, Closure (e, CVar n)) = Continue (false, prog, stack, case nth e n of CDummy => CVar n | r => r)
| weak_reduce (false, prog, stack, Closure (_, c as CConst _)) = Continue (false, prog, stack, c)
| weak_reduce (false, prog, stack, clos) =
(case match_closure prog clos of
NONE => Continue (true, prog, stack, clos)
| SOME r => Continue (false, prog, stack, r))
| weak_reduce (true, prog, SAppR (a, stack), b) = Continue (false, prog, stack, CApp (a,b))
| weak_reduce (true, prog, SAppL (b, stack), a) = Continue (false, prog, SAppR (a, stack), b)
| weak_reduce (true, prog, stack, c) = Stop (stack, c)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.