src/Pure/Tools/nbe_eval.ML
author haftmann
Fri Mar 03 08:52:39 2006 +0100 (2006-03-03)
changeset 19177 68c6824d8bb6
parent 19167 f237c0cb3882
child 19202 0b9eb4b0ad98
permissions -rwxr-xr-x
improvements for nbe
nipkow@19116
     1
(*  ID:         $Id$
haftmann@19177
     2
    Author:     Klaus Aehlig, LMU Muenchen; Tobias Nipkow, TU Muenchen
haftmann@19177
     3
haftmann@19177
     4
Evaluator infrastructure for "normalization by evaluation".
nipkow@19116
     5
*)
nipkow@19116
     6
nipkow@19116
     7
(* Optimizations:
nipkow@19116
     8
 int instead of string in Constr
nipkow@19116
     9
 treat int via ML ints
nipkow@19116
    10
*)
nipkow@19116
    11
nipkow@19116
    12
signature NBE_EVAL =
nipkow@19116
    13
sig
haftmann@19177
    14
  (*named terms used for output only*)
haftmann@19177
    15
  datatype nterm =
haftmann@19177
    16
      C of string         (*named constants*)
haftmann@19177
    17
    | V of string         (*free variable*)
haftmann@19177
    18
    | B of int            (*named(!!) bound variables*)
haftmann@19177
    19
    | A of nterm * nterm  (*application*)
haftmann@19177
    20
    | AbsN of int * nterm (*binding of named variables*);
haftmann@19177
    21
  datatype Univ = 
haftmann@19177
    22
      Constr of string*(Univ list)       (*named constructors*)
haftmann@19177
    23
    | Var of string*(Univ list)          (*free variables*)
haftmann@19177
    24
    | BVar of int*(Univ list)            (*bound named variables*)
haftmann@19177
    25
    | Fun of (Univ list -> Univ) * (Univ list) * int * (unit -> nterm)
haftmann@19177
    26
                                         (*functions*);
nipkow@19116
    27
haftmann@19177
    28
  val nbe: Univ Symtab.table -> CodegenThingol.iexpr -> nterm;
haftmann@19177
    29
  val apply: Univ -> Univ -> Univ;
haftmann@19177
    30
haftmann@19177
    31
  val to_term: Univ -> nterm;
nipkow@19147
    32
haftmann@19177
    33
  val mk_Fun: string * (Univ list -> Univ) * int -> string * Univ;
haftmann@19177
    34
  val new_name: unit -> int;
nipkow@19116
    35
haftmann@19177
    36
  (* For testing
haftmann@19177
    37
  val eval: (Univ list) -> term -> Univ
haftmann@19177
    38
  *)
nipkow@19116
    39
end;
nipkow@19116
    40
haftmann@19177
    41
structure NBE_Eval: NBE_EVAL =
nipkow@19116
    42
struct
nipkow@19116
    43
nipkow@19116
    44
datatype nterm =
haftmann@19177
    45
    C of string
haftmann@19177
    46
  | V of string
haftmann@19177
    47
  | B of int
haftmann@19177
    48
  | A of nterm * nterm
haftmann@19177
    49
  | AbsN of int * nterm;
nipkow@19116
    50
nipkow@19116
    51
fun apps t args = foldr (fn (y,x) => A(x,y)) t args;
nipkow@19116
    52
nipkow@19116
    53
(* ------------------------------ The semantical universe --------------------- *)
nipkow@19116
    54
nipkow@19116
    55
(*
nipkow@19116
    56
   Functions are given by their semantical function value. To avoid
nipkow@19116
    57
   trouble with the ML-type system, these functions have the most
nipkow@19116
    58
   generic type, that is "Univ list -> Univ". The calling convention is
nipkow@19116
    59
   that the arguments come as a list, the last argument first. In
nipkow@19116
    60
   other words, a function call that usually would look like
nipkow@19116
    61
nipkow@19116
    62
   f x_1 x_2 ... x_n   or   f(x_1,x_2, ..., x_n)
nipkow@19116
    63
nipkow@19116
    64
   would be in our convention called as
nipkow@19116
    65
nipkow@19116
    66
              f [x_n,..,x_2,x_1]
nipkow@19116
    67
nipkow@19116
    68
   Moreover, to handle functions that are still waiting for some
nipkow@19116
    69
   arguments we have additionally a list of arguments collected to far
nipkow@19116
    70
   and the number of arguments we're still waiting for.
nipkow@19116
    71
nipkow@19116
    72
   Finally, it might happen, that a function does not get all the
nipkow@19116
    73
   arguments it needs. In this case the function must provide means to
nipkow@19116
    74
   present itself as a string. As this might be a heavy-wight
nipkow@19116
    75
   operation, we delay it.
nipkow@19116
    76
*)
nipkow@19116
    77
nipkow@19116
    78
datatype Univ = 
nipkow@19116
    79
    Constr of string*(Univ list)       (* Named Constructors *)
nipkow@19116
    80
  | Var of string*(Univ list)          (* Free variables *)
nipkow@19116
    81
  | BVar of int*(Univ list)         (* Bound named variables *)
nipkow@19116
    82
  | Fun of (Univ list -> Univ) * (Univ list) * int * (unit -> nterm);
nipkow@19116
    83
nipkow@19116
    84
fun to_term (Constr(name, args))    = apps (C name) (map to_term args)
nipkow@19116
    85
  |	to_term (Var   (name, args))    = apps (V name) (map to_term args)
nipkow@19116
    86
  |	to_term (BVar  (name, args))    = apps (B name) (map to_term args)
nipkow@19116
    87
  |	to_term (Fun(_,args,_,name_thunk)) = apps (name_thunk ()) (map to_term args);
nipkow@19116
    88
nipkow@19116
    89
(*
nipkow@19116
    90
   A typical operation, why functions might be good for, is
nipkow@19116
    91
   application. Moreover, functions are the only values that can
nipkow@19116
    92
   reasonably we applied in a semantical way.
nipkow@19116
    93
*)
nipkow@19116
    94
haftmann@19177
    95
fun apply (Fun(f,xs,1,name))  x = f (x::xs)
haftmann@19177
    96
  | apply (Fun(f,xs,n,name))  x = Fun(f,x::xs,n-1,name)
haftmann@19177
    97
  | apply (Constr(name,args)) x = Constr(name,x::args)
haftmann@19177
    98
  | apply (Var(name,args))    x = Var(name,x::args)
haftmann@19177
    99
  | apply (BVar(name,args))   x = BVar(name,x::args);
nipkow@19116
   100
nipkow@19147
   101
fun mk_Fun(name,v,0) = (name,v [])
nipkow@19147
   102
  | mk_Fun(name,v,n) = (name,Fun(v,[],n, fn () => C name));
nipkow@19147
   103
nipkow@19116
   104
(* ---------------- table with the meaning of constants -------------------- *)
nipkow@19116
   105
nipkow@19147
   106
val xfun_tab: Univ Symtab.table ref = ref Symtab.empty;
nipkow@19116
   107
nipkow@19147
   108
fun lookup s = case Symtab.lookup (!xfun_tab) s of
nipkow@19116
   109
    SOME x => x
nipkow@19116
   110
  | NONE   => Constr(s,[]);
nipkow@19116
   111
nipkow@19116
   112
(* ------------------ evaluation with greetings to Tarski ------------------ *)
nipkow@19116
   113
nipkow@19116
   114
(* generation of fresh names *)
nipkow@19116
   115
nipkow@19116
   116
val counter = ref 0;
nipkow@19116
   117
haftmann@19177
   118
fun new_name () = (counter := !counter +1; !counter);
nipkow@19116
   119
nipkow@19116
   120
(* greetings to Tarski *)
nipkow@19116
   121
haftmann@19167
   122
open BasicCodegenThingol;
nipkow@19147
   123
haftmann@19167
   124
fun eval xs (IConst ((f, _), _)) = lookup f
haftmann@19167
   125
  | eval xs (IVar n) =
haftmann@19167
   126
      AList.lookup (op =) xs n
haftmann@19167
   127
      |> the_default (Var (n, []))
haftmann@19167
   128
  | eval xs (s `$ t) = apply (eval xs s) (eval xs t)
haftmann@19167
   129
  | eval xs ((n, _) `|-> t) =
haftmann@19167
   130
      Fun (fn [x] => eval (AList.update (op =) (n, x) xs) t,
haftmann@19167
   131
             [], 1,
haftmann@19167
   132
             fn () => let val var = new_name() in
haftmann@19167
   133
                 AbsN (var, to_term (eval (AList.update (op =) (n, BVar (var, [])) xs) t)) end)
haftmann@19167
   134
  | eval xs (IAbs (_, t)) = eval xs t
haftmann@19167
   135
  | eval xs (ICase (_, t)) = eval xs t;
nipkow@19116
   136
nipkow@19116
   137
(* finally... *)
nipkow@19116
   138
haftmann@19167
   139
fun nbe tab t = (counter :=0; xfun_tab := tab; to_term(eval [] t));
nipkow@19116
   140
nipkow@19116
   141
end;