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