src/Tools/Compute_Oracle/am_interpreter.ML
author haftmann
Sat, 15 Sep 2007 19:27:35 +0200
changeset 24584 01e83ffa6c54
parent 23663 84b5c89b8b49
child 24654 329f1b4d9d16
permissions -rw-r--r--
fixed title
haftmann@24584
     1
(*  Title:      Tools/Compute_Oracle/am_interpreter.ML
wenzelm@23174
     2
    ID:         $Id$
wenzelm@23174
     3
    Author:     Steven Obua
wenzelm@23174
     4
*)
wenzelm@23174
     5
wenzelm@23174
     6
structure AM_Interpreter : ABSTRACT_MACHINE = struct
wenzelm@23174
     7
obua@23663
     8
open AbstractMachine;
wenzelm@23174
     9
obua@23663
    10
datatype closure = CDummy | CVar of int | CConst of int
wenzelm@23174
    11
                 | CApp of closure * closure | CAbs of closure
wenzelm@23174
    12
                 | Closure of (closure list) * closure
wenzelm@23174
    13
wenzelm@23174
    14
structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
wenzelm@23174
    15
wenzelm@23174
    16
datatype program = Program of ((pattern * closure) list) prog_struct.table
wenzelm@23174
    17
wenzelm@23174
    18
datatype stack = SEmpty | SAppL of closure * stack | SAppR of closure * stack | SAbs of stack
wenzelm@23174
    19
wenzelm@23174
    20
fun clos_of_term (Var x) = CVar x
wenzelm@23174
    21
  | clos_of_term (Const c) = CConst c
wenzelm@23174
    22
  | clos_of_term (App (u, v)) = CApp (clos_of_term u, clos_of_term v)
wenzelm@23174
    23
  | clos_of_term (Abs u) = CAbs (clos_of_term u)
wenzelm@23174
    24
wenzelm@23174
    25
fun term_of_clos (CVar x) = Var x
wenzelm@23174
    26
  | term_of_clos (CConst c) = Const c
wenzelm@23174
    27
  | term_of_clos (CApp (u, v)) = App (term_of_clos u, term_of_clos v)
wenzelm@23174
    28
  | term_of_clos (CAbs u) = Abs (term_of_clos u)
wenzelm@23174
    29
  | term_of_clos (Closure (e, u)) = raise (Run "internal error: closure in normalized term found")
obua@23663
    30
  | term_of_clos CDummy = raise (Run "internal error: dummy in normalized term found")
wenzelm@23174
    31
wenzelm@23174
    32
fun strip_closure args (CApp (a,b)) = strip_closure (b::args) a
wenzelm@23174
    33
  | strip_closure args x = (x, args)
wenzelm@23174
    34
wenzelm@23174
    35
fun len_head_of_closure n (CApp (a,b)) = len_head_of_closure (n+1) a
wenzelm@23174
    36
  | len_head_of_closure n x = (n, x)
wenzelm@23174
    37
wenzelm@23174
    38
wenzelm@23174
    39
(* earlier occurrence of PVar corresponds to higher de Bruijn index *)
wenzelm@23174
    40
fun pattern_match args PVar clos = SOME (clos::args)
wenzelm@23174
    41
  | pattern_match args (PConst (c, patterns)) clos =
wenzelm@23174
    42
    let
wenzelm@23174
    43
        val (f, closargs) = strip_closure [] clos
wenzelm@23174
    44
    in
wenzelm@23174
    45
        case f of
wenzelm@23174
    46
            CConst d =>
wenzelm@23174
    47
            if c = d then
wenzelm@23174
    48
                pattern_match_list args patterns closargs
wenzelm@23174
    49
            else
wenzelm@23174
    50
                NONE
wenzelm@23174
    51
          | _ => NONE
wenzelm@23174
    52
    end
wenzelm@23174
    53
and pattern_match_list args [] [] = SOME args
wenzelm@23174
    54
  | pattern_match_list args (p::ps) (c::cs) =
wenzelm@23174
    55
    (case pattern_match args p c of
wenzelm@23174
    56
        NONE => NONE
wenzelm@23174
    57
      | SOME args => pattern_match_list args ps cs)
wenzelm@23174
    58
  | pattern_match_list _ _ _ = NONE
wenzelm@23174
    59
wenzelm@23174
    60
fun count_patternvars PVar = 1
wenzelm@23174
    61
  | count_patternvars (PConst (_, ps)) = List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
wenzelm@23174
    62
wenzelm@23174
    63
fun pattern_key (PConst (c, ps)) = (c, length ps)
wenzelm@23174
    64
  | pattern_key _ = raise (Compile "pattern reduces to variable")
wenzelm@23174
    65
obua@23663
    66
(*Returns true iff at most 0 .. (free-1) occur unbound. therefore
obua@23663
    67
  check_freevars 0 t iff t is closed*)
obua@23663
    68
fun check_freevars free (Var x) = x < free
obua@23663
    69
  | check_freevars free (Const c) = true
obua@23663
    70
  | check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v
obua@23663
    71
  | check_freevars free (Abs m) = check_freevars (free+1) m
obua@23663
    72
wenzelm@23174
    73
fun compile eqs =
wenzelm@23174
    74
    let
obua@23663
    75
	val _ = if exists (fn (a,b,c) => not (null a)) eqs then raise Compile ("cannot deal with guards") else ()
obua@23663
    76
	val eqs = map (fn (a,b,c) => (b,c)) eqs
obua@23663
    77
	fun check (p, r) = if check_freevars (count_patternvars p) r then () else raise Compile ("unbound variables in rule") 
obua@23663
    78
        val eqs = map (fn (p, r) => (check (p,r); (pattern_key p, (p, clos_of_term r)))) eqs
obua@23663
    79
	fun merge (k, a) table = prog_struct.update (k, case prog_struct.lookup table k of NONE => [a] | SOME l => a::l) table
obua@23663
    80
	val p = fold merge eqs prog_struct.empty 
wenzelm@23174
    81
    in
obua@23663
    82
        Program p
wenzelm@23174
    83
    end
wenzelm@23174
    84
wenzelm@23174
    85
fun match_rules n [] clos = NONE
wenzelm@23174
    86
  | match_rules n ((p,eq)::rs) clos =
wenzelm@23174
    87
    case pattern_match [] p clos of
wenzelm@23174
    88
        NONE => match_rules (n+1) rs clos
wenzelm@23174
    89
      | SOME args => SOME (Closure (args, eq))
wenzelm@23174
    90
wenzelm@23174
    91
fun match_closure (Program prog) clos =
wenzelm@23174
    92
    case len_head_of_closure 0 clos of
wenzelm@23174
    93
        (len, CConst c) =>
wenzelm@23174
    94
        (case prog_struct.lookup prog (c, len) of
wenzelm@23174
    95
            NONE => NONE
wenzelm@23174
    96
          | SOME rules => match_rules 0 rules clos)
wenzelm@23174
    97
      | _ => NONE
wenzelm@23174
    98
wenzelm@23174
    99
obua@23663
   100
type state = bool * program * stack * closure
wenzelm@23174
   101
obua@23663
   102
datatype loopstate = Continue of state | Stop of stack * closure
obua@23663
   103
obua@23663
   104
fun proj_C (Continue s) = s
obua@23663
   105
  | proj_C _ = raise Match
obua@23663
   106
obua@23663
   107
fun proj_S (Stop s) = s
obua@23663
   108
  | proj_S _ = raise Match
obua@23663
   109
obua@23663
   110
fun cont (Continue _) = true
obua@23663
   111
  | cont _ = false
obua@23663
   112
obua@23663
   113
fun do_reduction reduce p =
wenzelm@23174
   114
    let
obua@23663
   115
	val s = ref (Continue p)
obua@23663
   116
	val _ = while cont (!s) do (s := reduce (proj_C (!s)))
obua@23663
   117
    in
obua@23663
   118
	proj_S (!s)
obua@23663
   119
    end
obua@23663
   120
obua@23663
   121
fun weak_reduce (false, prog, stack, Closure (e, CApp (a, b))) = Continue (false, prog, SAppL (Closure (e, b), stack), Closure (e, a))
obua@23663
   122
  | weak_reduce (false, prog, SAppL (b, stack), Closure (e, CAbs m)) = Continue (false, prog, stack, Closure (b::e, m))
obua@23663
   123
  | weak_reduce (false, prog, stack, Closure (e, CVar n)) = Continue (false, prog, stack, case List.nth (e, n) of CDummy => CVar n | r => r)
obua@23663
   124
  | weak_reduce (false, prog, stack, Closure (e, c as CConst _)) = Continue (false, prog, stack, c)
obua@23663
   125
  | weak_reduce (false, prog, stack, clos) =
obua@23663
   126
    (case match_closure prog clos of
obua@23663
   127
         NONE => Continue (true, prog, stack, clos)
obua@23663
   128
       | SOME r => Continue (false, prog, stack, r))
obua@23663
   129
  | weak_reduce (true, prog, SAppR (a, stack), b) = Continue (false, prog, stack, CApp (a,b))
obua@23663
   130
  | weak_reduce (true, prog, s as (SAppL (b, stack)), a) = Continue (false, prog, SAppR (a, stack), b)
obua@23663
   131
  | weak_reduce (true, prog, stack, c) = Stop (stack, c)
obua@23663
   132
obua@23663
   133
fun strong_reduce (false, prog, stack, Closure (e, CAbs m)) =
obua@23663
   134
    let
obua@23663
   135
        val (stack', wnf) = do_reduction weak_reduce (false, prog, SEmpty, Closure (CDummy::e, m))
wenzelm@23174
   136
    in
wenzelm@23174
   137
        case stack' of
obua@23663
   138
            SEmpty => Continue (false, prog, SAbs stack, wnf)
wenzelm@23174
   139
          | _ => raise (Run "internal error in strong: weak failed")
wenzelm@23174
   140
    end
obua@23663
   141
  | strong_reduce (false, prog, stack, clos as (CApp (u, v))) = Continue (false, prog, SAppL (v, stack), u)
obua@23663
   142
  | strong_reduce (false, prog, stack, clos) = Continue (true, prog, stack, clos)
obua@23663
   143
  | strong_reduce (true, prog, SAbs stack, m) = Continue (false, prog, stack, CAbs m)
obua@23663
   144
  | strong_reduce (true, prog, SAppL (b, stack), a) = Continue (false, prog, SAppR (a, stack), b)
obua@23663
   145
  | strong_reduce (true, prog, SAppR (a, stack), b) = Continue (true, prog, stack, CApp (a, b))
obua@23663
   146
  | strong_reduce (true, prog, stack, clos) = Stop (stack, clos)
wenzelm@23174
   147
wenzelm@23174
   148
fun run prog t =
wenzelm@23174
   149
    let
obua@23663
   150
        val (stack, wnf) = do_reduction weak_reduce (false, prog, SEmpty, Closure ([], clos_of_term t))
wenzelm@23174
   151
    in
wenzelm@23174
   152
        case stack of
obua@23663
   153
            SEmpty => (case do_reduction strong_reduce (false, prog, SEmpty, wnf) of
wenzelm@23174
   154
                           (SEmpty, snf) => term_of_clos snf
wenzelm@23174
   155
                         | _ => raise (Run "internal error in run: strong failed"))
wenzelm@23174
   156
          | _ => raise (Run "internal error in run: weak failed")
wenzelm@23174
   157
    end
wenzelm@23174
   158
obua@23663
   159
fun discard p = ()
obua@23663
   160
wenzelm@23174
   161
end