src/Pure/Tools/am_interpreter.ML
changeset 16842 5979c46853d1
parent 16782 b214f21ae396
child 17412 e26cb20ef0cc
equal deleted inserted replaced
16841:228d663cc9b3 16842:5979c46853d1
     1 (*  Title:      Pure/Tools/am_interpreter.ML
     1 (*  Title:      Pure/Tools/am_interpreter.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Steven Obua
     3     Author:     Steven Obua
     4 *)
     4 *)
     5 
     5 
     6 signature ABSTRACT_MACHINE = sig							     
     6 signature ABSTRACT_MACHINE =
       
     7 sig
       
     8 
     7 datatype term = Var of int | Const of int | App of term * term | Abs of term
     9 datatype term = Var of int | Const of int | App of term * term | Abs of term
     8 
    10 
     9 datatype pattern = PVar | PConst of int * (pattern list)
    11 datatype pattern = PVar | PConst of int * (pattern list)
    10 
    12 
    11 type program
    13 type program
    12 
    14 
    13 exception Compile of string;
    15 exception Compile of string;
    14 val compile : (pattern * term) list -> program
    16 val compile : (pattern * term) list -> program
    15  
    17 
    16 exception Run of string;
    18 exception Run of string;
    17 val run : program -> term -> term
    19 val run : program -> term -> term
    18 
    20 
    19 end
    21 end
    20 
    22 
    23 datatype term = Var of int | Const of int | App of term * term | Abs of term
    25 datatype term = Var of int | Const of int | App of term * term | Abs of term
    24 
    26 
    25 datatype pattern = PVar | PConst of int * (pattern list)
    27 datatype pattern = PVar | PConst of int * (pattern list)
    26 
    28 
    27 datatype closure = CVar of int | CConst of int
    29 datatype closure = CVar of int | CConst of int
    28 		 | CApp of closure * closure | CAbs of closure 
    30                  | CApp of closure * closure | CAbs of closure
    29 		 | Closure of (closure list) * closure 
    31                  | Closure of (closure list) * closure
    30 
    32 
    31 structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
    33 structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
    32 
    34 
    33 type program = ((pattern * closure) list) prog_struct.table
    35 type program = ((pattern * closure) list) prog_struct.table
    34 
    36 
    55   | len_head_of_closure n x = (n, x)
    57   | len_head_of_closure n x = (n, x)
    56 
    58 
    57 
    59 
    58 (* earlier occurrence of PVar corresponds to higher de Bruijn index *)
    60 (* earlier occurrence of PVar corresponds to higher de Bruijn index *)
    59 fun pattern_match args PVar clos = SOME (clos::args)
    61 fun pattern_match args PVar clos = SOME (clos::args)
    60   | pattern_match args (PConst (c, patterns)) clos = 
    62   | pattern_match args (PConst (c, patterns)) clos =
    61     let
    63     let
    62 	val (f, closargs) = strip_closure [] clos
    64         val (f, closargs) = strip_closure [] clos
    63     in
    65     in
    64 	case f of 
    66         case f of
    65 	    CConst d => 
    67             CConst d =>
    66 	    if c = d then 
    68             if c = d then
    67 		pattern_match_list args patterns closargs
    69                 pattern_match_list args patterns closargs
    68 	    else 
    70             else
    69 		NONE
    71                 NONE
    70 	  | _ => NONE
    72           | _ => NONE
    71     end
    73     end
    72 and pattern_match_list args [] [] = SOME args
    74 and pattern_match_list args [] [] = SOME args
    73   | pattern_match_list args (p::ps) (c::cs) = 
    75   | pattern_match_list args (p::ps) (c::cs) =
    74     (case pattern_match args p c of
    76     (case pattern_match args p c of
    75 	NONE => NONE
    77         NONE => NONE
    76       | SOME args => pattern_match_list args ps cs)
    78       | SOME args => pattern_match_list args ps cs)
    77   | pattern_match_list _ _ _ = NONE
    79   | pattern_match_list _ _ _ = NONE
    78 
    80 
    79 (* Returns true iff at most 0 .. (free-1) occur unbound. therefore check_freevars 0 t iff t is closed *)
    81 (* Returns true iff at most 0 .. (free-1) occur unbound. therefore check_freevars 0 t iff t is closed *)
    80 fun check_freevars free (Var x) = x < free
    82 fun check_freevars free (Var x) = x < free
    86   | count_patternvars (PConst (_, ps)) = List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
    88   | count_patternvars (PConst (_, ps)) = List.foldl (fn (p, count) => (count_patternvars p)+count) 0 ps
    87 
    89 
    88 fun pattern_key (PConst (c, ps)) = (c, length ps)
    90 fun pattern_key (PConst (c, ps)) = (c, length ps)
    89   | pattern_key _ = raise (Compile "pattern reduces to variable")
    91   | pattern_key _ = raise (Compile "pattern reduces to variable")
    90 
    92 
    91 fun compile eqs = 
    93 fun compile eqs =
    92     let
    94     let
    93 	val eqs = map (fn (p, r) => (check_freevars (count_patternvars p) r; 
    95         val eqs = map (fn (p, r) => (check_freevars (count_patternvars p) r;
    94 				     (pattern_key p, (p, clos_of_term r)))) eqs
    96                                      (pattern_key p, (p, clos_of_term r)))) eqs
    95     in
    97     in
    96 	prog_struct.make (map (fn (k, a) => (k, [a])) eqs)
    98         prog_struct.make (map (fn (k, a) => (k, [a])) eqs)
    97     end	
    99     end
    98 
   100 
    99 fun match_rules n [] clos = NONE
   101 fun match_rules n [] clos = NONE
   100   | match_rules n ((p,eq)::rs) clos =
   102   | match_rules n ((p,eq)::rs) clos =
   101     case pattern_match [] p clos of
   103     case pattern_match [] p clos of
   102 	NONE => match_rules (n+1) rs clos
   104         NONE => match_rules (n+1) rs clos
   103       | SOME args => SOME (Closure (args, eq))
   105       | SOME args => SOME (Closure (args, eq))
   104 
   106 
   105 fun match_closure prog clos = 
   107 fun match_closure prog clos =
   106     case len_head_of_closure 0 clos of
   108     case len_head_of_closure 0 clos of
   107 	(len, CConst c) =>
   109         (len, CConst c) =>
   108 	(case prog_struct.lookup (prog, (c, len)) of
   110         (case prog_struct.lookup (prog, (c, len)) of
   109 	    NONE => NONE
   111             NONE => NONE
   110 	  | SOME rules => match_rules 0 rules clos)
   112           | SOME rules => match_rules 0 rules clos)
   111       | _ => NONE
   113       | _ => NONE
   112 
   114 
   113 fun lift n (c as (CConst _)) = c
   115 fun lift n (c as (CConst _)) = c
   114   | lift n (v as CVar m) = if m < n then v else CVar (m+1)
   116   | lift n (v as CVar m) = if m < n then v else CVar (m+1)
   115   | lift n (CAbs t) = CAbs (lift (n+1) t)
   117   | lift n (CAbs t) = CAbs (lift (n+1) t)
   119 
   121 
   120 fun weak prog stack (Closure (e, CApp (a, b))) = weak prog (SAppL (Closure (e, b), stack)) (Closure (e, a))
   122 fun weak prog stack (Closure (e, CApp (a, b))) = weak prog (SAppL (Closure (e, b), stack)) (Closure (e, a))
   121   | weak prog (SAppL (b, stack)) (Closure (e, CAbs m)) = weak prog stack (Closure (b::e, m))
   123   | weak prog (SAppL (b, stack)) (Closure (e, CAbs m)) = weak prog stack (Closure (b::e, m))
   122   | weak prog stack (Closure (e, CVar n)) = weak prog stack (List.nth (e, n) handle Subscript => (CVar (n-(length e))))
   124   | weak prog stack (Closure (e, CVar n)) = weak prog stack (List.nth (e, n) handle Subscript => (CVar (n-(length e))))
   123   | weak prog stack (Closure (e, c as CConst _)) = weak prog stack c
   125   | weak prog stack (Closure (e, c as CConst _)) = weak prog stack c
   124   | weak prog stack clos =    
   126   | weak prog stack clos =
   125     case match_closure prog clos of
   127     case match_closure prog clos of
   126 	NONE => weak_last prog stack clos
   128         NONE => weak_last prog stack clos
   127       | SOME r => weak prog stack r
   129       | SOME r => weak prog stack r
   128 and weak_last prog (SAppR (a, stack)) b = weak prog stack (CApp (a,b))
   130 and weak_last prog (SAppR (a, stack)) b = weak prog stack (CApp (a,b))
   129   | weak_last prog (s as (SAppL (b, stack))) a = weak prog (SAppR (a, stack)) b
   131   | weak_last prog (s as (SAppL (b, stack))) a = weak prog (SAppR (a, stack)) b
   130   | weak_last prog stack c = (stack, c) 
   132   | weak_last prog stack c = (stack, c)
   131 
   133 
   132 fun strong prog stack (Closure (e, CAbs m)) = 
   134 fun strong prog stack (Closure (e, CAbs m)) =
   133     let
   135     let
   134 	val (stack', wnf) = weak prog SEmpty (Closure ((CVar 0)::(lift_env 0 e), m))
   136         val (stack', wnf) = weak prog SEmpty (Closure ((CVar 0)::(lift_env 0 e), m))
   135     in
   137     in
   136 	case stack' of
   138         case stack' of
   137 	    SEmpty => strong prog (SAbs stack) wnf
   139             SEmpty => strong prog (SAbs stack) wnf
   138 	  | _ => raise (Run "internal error in strong: weak failed")
   140           | _ => raise (Run "internal error in strong: weak failed")
   139     end
   141     end
   140   | strong prog stack (clos as (CApp (u, v))) = strong prog (SAppL (v, stack)) u
   142   | strong prog stack (clos as (CApp (u, v))) = strong prog (SAppL (v, stack)) u
   141   | strong prog stack clos = strong_last prog stack clos
   143   | strong prog stack clos = strong_last prog stack clos
   142 and strong_last prog (SAbs stack) m = strong prog stack (CAbs m)
   144 and strong_last prog (SAbs stack) m = strong prog stack (CAbs m)
   143   | strong_last prog (SAppL (b, stack)) a = strong prog (SAppR (a, stack)) b
   145   | strong_last prog (SAppL (b, stack)) a = strong prog (SAppR (a, stack)) b
   144   | strong_last prog (SAppR (a, stack)) b = strong_last prog stack (CApp (a, b))
   146   | strong_last prog (SAppR (a, stack)) b = strong_last prog stack (CApp (a, b))
   145   | strong_last prog stack clos = (stack, clos)
   147   | strong_last prog stack clos = (stack, clos)
   146 
   148 
   147 fun run prog t = 
   149 fun run prog t =
   148     let
   150     let
   149 	val (stack, wnf) = weak prog SEmpty (Closure ([], clos_of_term t))
   151         val (stack, wnf) = weak prog SEmpty (Closure ([], clos_of_term t))
   150     in
   152     in
   151 	case stack of 
   153         case stack of
   152 	    SEmpty => (case strong prog SEmpty wnf of
   154             SEmpty => (case strong prog SEmpty wnf of
   153 			   (SEmpty, snf) => term_of_clos snf
   155                            (SEmpty, snf) => term_of_clos snf
   154 			 | _ => raise (Run "internal error in run: strong failed"))
   156                          | _ => raise (Run "internal error in run: strong failed"))
   155 	  | _ => raise (Run "internal error in run: weak failed")
   157           | _ => raise (Run "internal error in run: weak failed")
   156     end
   158     end
   157 	  
   159 
   158 end
   160 end
   159 
   161 
   160 structure AbstractMachine = AM_Interpreter
   162 structure AbstractMachine = AM_Interpreter