src/Pure/Tools/am_interpreter.ML
author haftmann
Mon, 02 Oct 2006 23:00:51 +0200
changeset 20835 27d049062b56
parent 17795 5b18c3343028
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     1
(*  Title:      Pure/Tools/am_interpreter.ML
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     2
    ID:         $Id$
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     3
    Author:     Steven Obua
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     4
*)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     5
16842
wenzelm
parents: 16782
diff changeset
     6
signature ABSTRACT_MACHINE =
wenzelm
parents: 16782
diff changeset
     7
sig
wenzelm
parents: 16782
diff changeset
     8
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     9
datatype term = Var of int | Const of int | App of term * term | Abs of term
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    10
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    11
datatype pattern = PVar | PConst of int * (pattern list)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    12
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    13
type program
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    14
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    15
exception Compile of string;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    16
val compile : (pattern * term) list -> program
16842
wenzelm
parents: 16782
diff changeset
    17
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    18
exception Run of string;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    19
val run : program -> term -> term
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    20
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    21
end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    22
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    23
structure AM_Interpreter : ABSTRACT_MACHINE = struct
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    24
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    25
datatype term = Var of int | Const of int | App of term * term | Abs of term
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    26
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    27
datatype pattern = PVar | PConst of int * (pattern list)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    28
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    29
datatype closure = CVar of int | CConst of int
16842
wenzelm
parents: 16782
diff changeset
    30
                 | CApp of closure * closure | CAbs of closure
wenzelm
parents: 16782
diff changeset
    31
                 | Closure of (closure list) * closure
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    32
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents: 16781
diff changeset
    33
structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    34
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    35
datatype program = Program of ((pattern * closure) list) prog_struct.table
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    36
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    37
datatype stack = SEmpty | SAppL of closure * stack | SAppR of closure * stack | SAbs of stack
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    38
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    39
exception Compile of string;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    40
exception Run of string;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    41
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    42
fun clos_of_term (Var x) = CVar x
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    43
  | clos_of_term (Const c) = CConst c
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    44
  | clos_of_term (App (u, v)) = CApp (clos_of_term u, clos_of_term v)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    45
  | clos_of_term (Abs u) = CAbs (clos_of_term u)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    46
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    47
fun term_of_clos (CVar x) = Var x
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    48
  | term_of_clos (CConst c) = Const c
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    49
  | term_of_clos (CApp (u, v)) = App (term_of_clos u, term_of_clos v)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    50
  | term_of_clos (CAbs u) = Abs (term_of_clos u)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    51
  | term_of_clos (Closure (e, u)) = raise (Run "internal error: closure in normalized term found")
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    52
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    53
fun strip_closure args (CApp (a,b)) = strip_closure (b::args) a
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    54
  | strip_closure args x = (x, args)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    55
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    56
fun len_head_of_closure n (CApp (a,b)) = len_head_of_closure (n+1) a
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    57
  | len_head_of_closure n x = (n, x)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    58
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    59
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    60
(* earlier occurrence of PVar corresponds to higher de Bruijn index *)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    61
fun pattern_match args PVar clos = SOME (clos::args)
16842
wenzelm
parents: 16782
diff changeset
    62
  | pattern_match args (PConst (c, patterns)) clos =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    63
    let
16842
wenzelm
parents: 16782
diff changeset
    64
        val (f, closargs) = strip_closure [] clos
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    65
    in
16842
wenzelm
parents: 16782
diff changeset
    66
        case f of
wenzelm
parents: 16782
diff changeset
    67
            CConst d =>
wenzelm
parents: 16782
diff changeset
    68
            if c = d then
wenzelm
parents: 16782
diff changeset
    69
                pattern_match_list args patterns closargs
wenzelm
parents: 16782
diff changeset
    70
            else
wenzelm
parents: 16782
diff changeset
    71
                NONE
wenzelm
parents: 16782
diff changeset
    72
          | _ => NONE
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    73
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    74
and pattern_match_list args [] [] = SOME args
16842
wenzelm
parents: 16782
diff changeset
    75
  | pattern_match_list args (p::ps) (c::cs) =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    76
    (case pattern_match args p c of
16842
wenzelm
parents: 16782
diff changeset
    77
        NONE => NONE
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    78
      | SOME args => pattern_match_list args ps cs)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    79
  | pattern_match_list _ _ _ = NONE
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    80
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    81
(* Returns true iff at most 0 .. (free-1) occur unbound. therefore check_freevars 0 t iff t is closed *)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    82
fun check_freevars free (Var x) = x < free
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    83
  | check_freevars free (Const c) = true
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    84
  | check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    85
  | check_freevars free (Abs m) = check_freevars (free+1) m
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    86
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    87
fun count_patternvars PVar = 1
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    88
  | count_patternvars (PConst (_, ps)) = List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    89
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    90
fun pattern_key (PConst (c, ps)) = (c, length ps)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    91
  | pattern_key _ = raise (Compile "pattern reduces to variable")
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    92
16842
wenzelm
parents: 16782
diff changeset
    93
fun compile eqs =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    94
    let
16842
wenzelm
parents: 16782
diff changeset
    95
        val eqs = map (fn (p, r) => (check_freevars (count_patternvars p) r;
wenzelm
parents: 16782
diff changeset
    96
                                     (pattern_key p, (p, clos_of_term r)))) eqs
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    97
    in
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    98
        Program (prog_struct.make (map (fn (k, a) => (k, [a])) eqs))
16842
wenzelm
parents: 16782
diff changeset
    99
    end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   100
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   101
fun match_rules n [] clos = NONE
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   102
  | match_rules n ((p,eq)::rs) clos =
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   103
    case pattern_match [] p clos of
16842
wenzelm
parents: 16782
diff changeset
   104
        NONE => match_rules (n+1) rs clos
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   105
      | SOME args => SOME (Closure (args, eq))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   106
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   107
fun match_closure (Program prog) clos =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   108
    case len_head_of_closure 0 clos of
16842
wenzelm
parents: 16782
diff changeset
   109
        (len, CConst c) =>
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 16842
diff changeset
   110
        (case prog_struct.lookup prog (c, len) of
16842
wenzelm
parents: 16782
diff changeset
   111
            NONE => NONE
wenzelm
parents: 16782
diff changeset
   112
          | SOME rules => match_rules 0 rules clos)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   113
      | _ => NONE
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   114
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   115
fun lift n (c as (CConst _)) = c
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   116
  | lift n (v as CVar m) = if m < n then v else CVar (m+1)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   117
  | lift n (CAbs t) = CAbs (lift (n+1) t)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   118
  | lift n (CApp (a,b)) = CApp (lift n a, lift n b)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   119
  | lift n (Closure (e, a)) = Closure (lift_env n e, lift (n+(length e)) a)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   120
and lift_env n e = map (lift n) e
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   121
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   122
fun weak prog stack (Closure (e, CApp (a, b))) = weak prog (SAppL (Closure (e, b), stack)) (Closure (e, a))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   123
  | weak prog (SAppL (b, stack)) (Closure (e, CAbs m)) = weak prog stack (Closure (b::e, m))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   124
  | weak prog stack (Closure (e, CVar n)) = weak prog stack (List.nth (e, n) handle Subscript => (CVar (n-(length e))))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   125
  | weak prog stack (Closure (e, c as CConst _)) = weak prog stack c
16842
wenzelm
parents: 16782
diff changeset
   126
  | weak prog stack clos =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   127
    case match_closure prog clos of
16842
wenzelm
parents: 16782
diff changeset
   128
        NONE => weak_last prog stack clos
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   129
      | SOME r => weak prog stack r
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   130
and weak_last prog (SAppR (a, stack)) b = weak prog stack (CApp (a,b))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   131
  | weak_last prog (s as (SAppL (b, stack))) a = weak prog (SAppR (a, stack)) b
16842
wenzelm
parents: 16782
diff changeset
   132
  | weak_last prog stack c = (stack, c)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   133
16842
wenzelm
parents: 16782
diff changeset
   134
fun strong prog stack (Closure (e, CAbs m)) =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   135
    let
16842
wenzelm
parents: 16782
diff changeset
   136
        val (stack', wnf) = weak prog SEmpty (Closure ((CVar 0)::(lift_env 0 e), m))
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   137
    in
16842
wenzelm
parents: 16782
diff changeset
   138
        case stack' of
wenzelm
parents: 16782
diff changeset
   139
            SEmpty => strong prog (SAbs stack) wnf
wenzelm
parents: 16782
diff changeset
   140
          | _ => raise (Run "internal error in strong: weak failed")
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   141
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   142
  | strong prog stack (clos as (CApp (u, v))) = strong prog (SAppL (v, stack)) u
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   143
  | strong prog stack clos = strong_last prog stack clos
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   144
and strong_last prog (SAbs stack) m = strong prog stack (CAbs m)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   145
  | strong_last prog (SAppL (b, stack)) a = strong prog (SAppR (a, stack)) b
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   146
  | strong_last prog (SAppR (a, stack)) b = strong_last prog stack (CApp (a, b))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   147
  | strong_last prog stack clos = (stack, clos)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   148
16842
wenzelm
parents: 16782
diff changeset
   149
fun run prog t =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   150
    let
16842
wenzelm
parents: 16782
diff changeset
   151
        val (stack, wnf) = weak prog SEmpty (Closure ([], clos_of_term t))
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   152
    in
16842
wenzelm
parents: 16782
diff changeset
   153
        case stack of
wenzelm
parents: 16782
diff changeset
   154
            SEmpty => (case strong prog SEmpty wnf of
wenzelm
parents: 16782
diff changeset
   155
                           (SEmpty, snf) => term_of_clos snf
wenzelm
parents: 16782
diff changeset
   156
                         | _ => raise (Run "internal error in run: strong failed"))
wenzelm
parents: 16782
diff changeset
   157
          | _ => raise (Run "internal error in run: weak failed")
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   158
    end
16842
wenzelm
parents: 16782
diff changeset
   159
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   160
end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   161
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   162
structure AbstractMachine = AM_Interpreter