fun print_rule (p, t) = let fun str x = string_of_int x fun print_pattern n PVar = (n+1, "x"^(str n))
| print_pattern n (PConst (c, [])) = (n, "c"^(str c))
| print_pattern n (PConst (c, args)) = let val h = print_pattern n (PConst (c,[])) in
print_pattern_list h args end and print_pattern_list r [] = r
| print_pattern_list (n, p) (t::ts) = let val (n, t) = print_pattern n t in
print_pattern_list (n, "App ("^p^", "^t^")") ts end
val (n, pattern) = print_pattern 0 p val pattern = if exists_string Symbol.is_ascii_blank pattern then"(" ^ pattern ^")" else pattern
fun print_term d (Var x) = "Var " ^ str x
| print_term d (Const c) = "c" ^ str c
| print_term d (App (a,b)) = "App (" ^ print_term d a ^ ", " ^ print_term d b ^ ")"
| print_term d (Abs c) = "Abs (" ^ print_term (d + 1) c ^ ")"
| print_term d (Computed c) = print_term d c
fun listvars n = if n = 0 then"x0"else"x"^(str n)^", "^(listvars (n-1))
val term = print_term 0 t val term = if n > 0 then"Closure (["^(listvars (n-1))^"], "^term^")" else"Closure ([], "^term^")"
in " | weak_reduce (false, stack, "^pattern^") = Continue (false, stack, "^term^")" end
fun constants_of PVar = []
| constants_of (PConst (c, ps)) = c :: maps constants_of ps
fun constants_of_term (Var _) = []
| constants_of_term (Abs m) = constants_of_term m
| constants_of_term (App (a,b)) = (constants_of_term a)@(constants_of_term b)
| constants_of_term (Const c) = [c]
| constants_of_term (Computed c) = constants_of_term c
fun load_rules sname name prog = let val buffer = Unsynchronized.ref"" fun write s = (buffer := (!buffer)^s) fun writeln s = (write s; write "\n") fun writelist [] = ()
| writelist (s::ss) = (writeln s; writelist ss) fun str i = string_of_int i val _ = writelist [ "structure "^name^" = struct", "", "datatype term = Dummy | App of term * term | Abs of term | Var of int | Const of int | Closure of term list * term"] val constants = distinct (op =) (maps (fn (p, r) => ((constants_of p)@(constants_of_term r))) prog) val _ = map (fn x => write (" | c"^(str x))) constants val _ = writelist [ "", "datatype stack = SEmpty | SAppL of term * stack | SAppR of term * stack | SAbs of stack", "", "type state = bool * stack * term", "", "datatype loopstate = Continue of state | Stop of stack * term", "", "fun proj_C (Continue s) = s", " | proj_C _ = raise Match", "", "fun proj_S (Stop s) = s", " | proj_S _ = raise Match", "", "fun cont (Continue _) = true", " | cont _ = false", "", "fun do_reduction reduce p =", " let", " val s = Unsynchronized.ref (Continue p)", " val _ = while cont (!s) do (s := reduce (proj_C (!s)))", " in", " proj_S (!s)", " end", ""]
in
compiled_rewriter := NONE;
ML_Compiler0.ML ML_Env.context
{line = 1, file = "", verbose = false, debug = false} (!buffer); case !compiled_rewriter of
NONE => raise (Compile "cannot communicate with compiled function")
| SOME r => (compiled_rewriter := NONE; r) end
fun compile eqs = let val _ = ifexists (fn (a,_,_) => not (null a)) eqs thenraise Compile ("cannot deal with guards") else () val eqs = map (fn (_,b,c) => (b,c)) eqs fun check (p, r) = if check_freevars (count_patternvars p) r then () elseraise Compile ("unbound variables in rule") val _ = map (fn (p, r) =>
(check (p, r); case p of PVar => raise (Compile "pattern is just a variable") | _ => ())) eqs in
load_rules "AM_Compiler""AM_compiled_code" eqs end
fun run prog t = prog t
end
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
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.