src/Tools/nbe.ML
author haftmann
Mon, 06 Aug 2007 11:45:39 +0200
changeset 24155 d86867645f4f
child 24166 7b28dc69bdbb
permissions -rw-r--r--
nbe improved
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/Nbe/Nbe_Eval.ML
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     3
    Authors:    Klaus Aehlig, LMU Muenchen; Tobias Nipkow, Florian Haftmann, TU Muenchen
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     4
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     5
Evaluation mechanisms for normalization by evaluation.
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     6
*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     7
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     8
(*
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     9
FIXME:
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    10
- get rid of BVar (?) - it is only used for terms to be evaluated, not for functions
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    11
- proper purge operation - preliminary for...
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    12
- really incremental code generation
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    13
*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    14
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    15
signature NBE =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    16
sig
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    17
  datatype Univ = 
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    18
      Const of string * Univ list        (*named constructors*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    19
    | Free of string * Univ list
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    20
    | BVar of int * Univ list
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    21
    | Abs of (int * (Univ list -> Univ)) * Univ list;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    22
  val free: string -> Univ list -> Univ       (*free (uninterpreted) variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    23
  val abs: int -> (Univ list -> Univ) -> Univ list -> Univ
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    24
                                            (*abstractions as functions*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    25
  val app: Univ -> Univ -> Univ              (*explicit application*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    26
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    27
  val univs_ref: Univ list ref 
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    28
  val lookup_fun: CodegenNames.const -> Univ
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    29
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    30
  val normalization_conv: cterm -> thm
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    31
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    32
  val trace: bool ref
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    33
  val setup: theory -> theory
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    34
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    35
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    36
structure Nbe: NBE =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    37
struct
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    38
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    39
(* generic non-sense *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    40
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    41
val trace = ref false;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    42
fun tracing f x = if !trace then (Output.tracing (f x); x) else x;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    43
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    44
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    45
(** the semantical universe **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    46
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    47
(*
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    48
   Functions are given by their semantical function value. To avoid
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    49
   trouble with the ML-type system, these functions have the most
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    50
   generic type, that is "Univ list -> Univ". The calling convention is
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    51
   that the arguments come as a list, the last argument first. In
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    52
   other words, a function call that usually would look like
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    53
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    54
   f x_1 x_2 ... x_n   or   f(x_1,x_2, ..., x_n)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    55
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    56
   would be in our convention called as
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    57
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    58
              f [x_n,..,x_2,x_1]
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    59
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    60
   Moreover, to handle functions that are still waiting for some
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    61
   arguments we have additionally a list of arguments collected to far
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    62
   and the number of arguments we're still waiting for.
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    63
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    64
   (?) Finally, it might happen, that a function does not get all the
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    65
   arguments it needs.  In this case the function must provide means to
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    66
   present itself as a string. As this might be a heavy-wight
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    67
   operation, we delay it. (?) 
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    68
*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    69
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    70
datatype Univ = 
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    71
    Const of string * Univ list        (*named constructors*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    72
  | Free of string * Univ list         (*free variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    73
  | BVar of int * Univ list            (*bound named variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    74
  | Abs of (int * (Univ list -> Univ)) * Univ list
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    75
                                      (*functions*);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    76
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    77
(* constructor functions *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    78
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    79
val free = curry Free;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    80
fun abs n f ts = Abs ((n, f), ts);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    81
fun app (Abs ((1, f), xs)) x = f (x :: xs)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    82
  | app (Abs ((n, f), xs)) x = Abs ((n - 1, f), x :: xs)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    83
  | app (Const (name, args)) x = Const (name, x :: args)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    84
  | app (Free (name, args)) x = Free (name, x :: args)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    85
  | app (BVar (name, args)) x = BVar (name, x :: args);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    86
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    87
(* global functions store *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    88
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    89
structure Nbe_Functions = CodeDataFun
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    90
(struct
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    91
  type T = Univ Symtab.table;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    92
  val empty = Symtab.empty;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    93
  fun merge _ = Symtab.merge (K true);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    94
  fun purge _ _ _ = Symtab.empty;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    95
end);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    96
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    97
(* sandbox communication *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    98
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    99
val univs_ref = ref [] : Univ list ref;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   100
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   101
local
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   102
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   103
val tab_ref = ref NONE : Univ Symtab.table option ref;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   104
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   105
in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   106
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   107
fun lookup_fun s = case ! tab_ref
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   108
 of NONE => error "compile_univs"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   109
  | SOME tab => (the o Symtab.lookup tab) s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   110
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   111
fun compile_univs tab ([], _) = []
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   112
  | compile_univs tab (cs, raw_s) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   113
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   114
        val _ = univs_ref := [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   115
        val s = "Nbe.univs_ref := " ^ raw_s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   116
        val _ = tracing (fn () => "\n---generated code:\n" ^ s) ();
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   117
        val _ = tab_ref := SOME tab;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   118
        val _ = use_text "" (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   119
          Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   120
          (!trace) s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   121
        val _ = tab_ref := NONE;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   122
        val univs = case !univs_ref of [] => error "compile_univs" | univs => univs;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   123
      in cs ~~ univs end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   124
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   125
end; (*local*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   126
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   127
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   128
(** assembling and compiling ML code from terms **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   129
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   130
(* abstract ML syntax *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   131
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   132
infix 9 `$` `$$`;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   133
fun e1 `$` e2 = "(" ^ e1 ^ " " ^ e2 ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   134
fun e `$$` es = "(" ^ e ^ " " ^ space_implode " " es ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   135
fun ml_abs v e = "(fn" ^ v ^ " => " ^ e ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   136
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   137
fun ml_Val v s = "val " ^ v ^ " = " ^ s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   138
fun ml_cases t cs =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   139
  "(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   140
fun ml_Let ds e = "let\n" ^ space_implode "\n" ds ^ " in " ^ e ^ " end";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   141
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   142
fun ml_list es = "[" ^ commas es ^ "]";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   143
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   144
fun ml_fundefs ([(name, [([], e)])]) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   145
      "val " ^ name ^ " = " ^ e ^ "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   146
  | ml_fundefs (eqs :: eqss) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   147
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   148
        fun fundef (name, eqs) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   149
          let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   150
            fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   151
          in space_implode "\n  | " (map eqn eqs) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   152
      in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   153
        (prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   154
        |> space_implode "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   155
        |> suffix "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   156
      end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   157
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   158
(* nbe specific syntax *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   159
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   160
local
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   161
  val prefix =          "Nbe.";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   162
  val name_const =      prefix ^ "Const";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   163
  val name_free =       prefix ^ "free";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   164
  val name_abs =        prefix ^ "abs";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   165
  val name_app =        prefix ^ "app";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   166
  val name_lookup_fun = prefix ^ "lookup_fun";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   167
in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   168
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   169
fun nbe_const c ts = name_const `$` ("(" ^ ML_Syntax.print_string c ^ ", " ^ ml_list ts ^ ")");
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   170
fun nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   171
fun nbe_free v = name_free `$$` [ML_Syntax.print_string v, ml_list []];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   172
fun nbe_bound v = "v_" ^ v;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   173
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   174
fun nbe_apps e es =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   175
  Library.foldr (fn (s, e) => name_app `$$` [e, s]) (es, e);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   176
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   177
fun nbe_abss 0 f = f `$` ml_list []
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   178
  | nbe_abss n f = name_abs `$$` [string_of_int n, f, ml_list []];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   179
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   180
fun nbe_lookup c = ml_Val (nbe_fun c) (name_lookup_fun `$` ML_Syntax.print_string c);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   181
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   182
val nbe_value = "value";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   183
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   184
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   185
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   186
open BasicCodegenThingol;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   187
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   188
(* greetings to Tarski *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   189
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   190
fun assemble_iterm thy is_fun num_args =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   191
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   192
    fun of_iterm t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   193
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   194
        val (t', ts) = CodegenThingol.unfold_app t
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   195
      in of_itermapp t' (fold (cons o of_iterm) ts []) end
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   196
    and of_itermapp (IConst (c, (dss, _))) ts =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   197
          (case num_args c
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   198
           of SOME n => if n <= length ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   199
                then let val (args2, args1) = chop (length ts - n) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   200
                in nbe_apps (nbe_fun c `$` ml_list args1) args2
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   201
                end else nbe_const c ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   202
            | NONE => if is_fun c then nbe_apps (nbe_fun c) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   203
                else nbe_const c ts)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   204
      | of_itermapp (IVar v) ts = nbe_apps (nbe_bound v) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   205
      | of_itermapp ((v, _) `|-> t) ts =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   206
          nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   207
      | of_itermapp (ICase (((t, _), cs), t0)) ts =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   208
          nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   209
            @ [("_", of_iterm t0)])) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   210
  in of_iterm end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   211
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   212
fun assemble_fun thy is_fun num_args (c, eqns) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   213
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   214
    val assemble_arg = assemble_iterm thy (K false) (K NONE);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   215
    val assemble_rhs = assemble_iterm thy is_fun num_args;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   216
    fun assemble_eqn (args, rhs) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   217
      ([ml_list (map assemble_arg (rev args))], assemble_rhs rhs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   218
    val default_params = map nbe_bound
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   219
      (Name.invent_list [] "a" ((the o num_args) c));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   220
    val default_eqn = ([ml_list default_params], nbe_const c default_params);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   221
  in map assemble_eqn eqns @ [default_eqn] end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   222
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   223
fun assemble_eqnss thy is_fun [] = ([], "")
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   224
  | assemble_eqnss thy is_fun eqnss =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   225
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   226
        val cs = map fst eqnss;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   227
        val num_args = cs ~~ map (fn (_, (args, rhs) :: _) => length args) eqnss;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   228
        val funs = fold (fold (CodegenThingol.fold_constnames
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   229
          (insert (op =))) o map snd o snd) eqnss [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   230
        val bind_funs = map nbe_lookup (filter is_fun funs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   231
        val bind_locals = ml_fundefs (map nbe_fun cs ~~ map
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   232
          (assemble_fun thy is_fun (AList.lookup (op =) num_args)) eqnss);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   233
        val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   234
      in (cs, ml_Let (bind_funs @ [bind_locals]) result) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   235
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   236
fun assemble_eval thy is_fun t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   237
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   238
    val funs = CodegenThingol.fold_constnames (insert (op =)) t [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   239
    val frees = CodegenThingol.fold_unbound_varnames (insert (op =)) t [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   240
    val bind_funs = map nbe_lookup (filter is_fun funs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   241
    val bind_value = ml_fundefs [(nbe_value, [([ml_list (map nbe_bound frees)],
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   242
      assemble_iterm thy is_fun (K NONE) t)])];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   243
    val result = ml_list [nbe_value `$` ml_list (map nbe_free frees)];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   244
  in ([nbe_value], ml_Let (bind_funs @ [bind_value]) result) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   245
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   246
fun eqns_of_stmt (name, CodegenThingol.Fun ([], _)) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   247
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   248
  | eqns_of_stmt (name, CodegenThingol.Fun (eqns, _)) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   249
      SOME (name, eqns)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   250
  | eqns_of_stmt (_, CodegenThingol.Datatypecons _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   251
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   252
  | eqns_of_stmt (_, CodegenThingol.Datatype _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   253
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   254
  | eqns_of_stmt (_, CodegenThingol.Class _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   255
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   256
  | eqns_of_stmt (_, CodegenThingol.Classrel _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   257
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   258
  | eqns_of_stmt (_, CodegenThingol.Classop _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   259
      NONE
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   260
  | eqns_of_stmt (_, CodegenThingol.Classinst _) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   261
      NONE;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   262
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   263
fun compile_stmts thy is_fun =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   264
  map_filter eqns_of_stmt
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   265
  #> assemble_eqnss thy is_fun
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   266
  #> compile_univs (Nbe_Functions.get thy);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   267
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   268
fun eval_term thy is_fun =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   269
  assemble_eval thy is_fun
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   270
  #> compile_univs (Nbe_Functions.get thy)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   271
  #> the_single
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   272
  #> snd;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   273
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   274
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   275
(** compilation and evaluation **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   276
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   277
(* ensure global functions *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   278
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   279
fun ensure_funs thy code =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   280
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   281
    fun compile' stmts tab =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   282
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   283
        val compiled = compile_stmts thy (Symtab.defined tab) stmts;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   284
      in Nbe_Functions.change thy (fold Symtab.update compiled) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   285
    val nbe_tab = Nbe_Functions.get thy;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   286
    val stmtss =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   287
      map (AList.make (Graph.get_node code)) (rev (Graph.strong_conn code))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   288
      |> (map o filter_out) (Symtab.defined nbe_tab o fst)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   289
  in fold compile' stmtss nbe_tab end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   290
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   291
(* re-conversion *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   292
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   293
fun term_of_univ thy t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   294
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   295
    fun of_apps bounds (t, ts) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   296
      fold_map (of_univ bounds) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   297
      #>> (fn ts' => list_comb (t, rev ts'))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   298
    and of_univ bounds (Const (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   299
          let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   300
            val SOME (const as (c, _)) = CodegenNames.const_rev thy name;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   301
            val T = CodegenData.default_typ thy const;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   302
            val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, S)) T;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   303
            val typidx' = typidx + maxidx_of_typ T' + 1;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   304
          in of_apps bounds (Term.Const (c, T'), ts) typidx' end
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   305
      | of_univ bounds (Free (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   306
          of_apps bounds (Term.Free (name, dummyT), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   307
      | of_univ bounds (BVar (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   308
          of_apps bounds (Bound (bounds - name - 1), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   309
      | of_univ bounds (t as Abs _) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   310
          typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   311
          |> of_univ (bounds + 1) (app t (BVar (bounds, [])))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   312
          |-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   313
  in of_univ 0 t 0 |> fst end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   314
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   315
(* evaluation with type reconstruction *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   316
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   317
fun eval thy code t t' =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   318
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   319
    fun subst_Frees [] = I
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   320
      | subst_Frees inst =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   321
          Term.map_aterms (fn (t as Term.Free (s, _)) => the_default t (AList.lookup (op =) inst s)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   322
                            | t => t);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   323
    val anno_vars =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   324
      subst_Frees (map (fn (s, T) => (s, Term.Free (s, T))) (Term.add_frees t []))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   325
      #> subst_Vars (map (fn (ixn, T) => (ixn, Var (ixn, T))) (Term.add_vars t []))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   326
    fun check_tvars t = if null (Term.term_tvars t) then t else
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   327
      error ("Illegal schematic type variables in normalized term: "
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   328
        ^ setmp show_types true (Sign.string_of_term thy) t);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   329
    val ty = type_of t;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   330
    fun constrain t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   331
      singleton (ProofContext.infer_types_pats (ProofContext.init thy)) (TypeInfer.constrain t ty);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   332
  in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   333
    t'
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   334
    |> eval_term thy (Symtab.defined (ensure_funs thy code))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   335
    |> term_of_univ thy
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   336
    |> tracing (fn t => "Normalized:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   337
    |> tracing (fn _ => "Term type:\n" ^ Display.raw_string_of_typ ty)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   338
    |> anno_vars
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   339
    |> tracing (fn t => "Vars typed:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   340
    |> tracing (fn t => setmp show_types true (Sign.string_of_term thy) t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   341
    |> constrain
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   342
    |> tracing (fn t => "Types inferred:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   343
    |> check_tvars
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   344
  end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   345
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   346
(* evaluation oracle *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   347
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   348
exception Normalization of CodegenThingol.code * term * CodegenThingol.iterm;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   349
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   350
fun normalization_oracle (thy, Normalization (code, t, t')) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   351
  Logic.mk_equals (t, eval thy code t t');
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   352
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   353
fun normalization_invoke thy code t t' =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   354
  Thm.invoke_oracle_i thy "Nbe.normalization" (thy, Normalization (code, t, t'));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   355
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   356
fun normalization_conv ct =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   357
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   358
    val thy = Thm.theory_of_cterm ct;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   359
    fun conv code t' ct =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   360
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   361
        val t = Thm.term_of ct;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   362
      in normalization_invoke thy code t t' end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   363
  in CodegenPackage.eval_conv thy conv ct end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   364
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   365
(* evaluation command *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   366
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   367
fun norm_print_term ctxt modes t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   368
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   369
    val thy = ProofContext.theory_of ctxt;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   370
    val ct = Thm.cterm_of thy t;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   371
    val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   372
    val ty = Term.type_of t';
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   373
    val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   374
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   375
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   376
  in Pretty.writeln p end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   377
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   378
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   379
(** Isar setup **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   380
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   381
fun norm_print_term_cmd (modes, raw_t) state =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   382
  let val ctxt = Toplevel.context_of state
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   383
  in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   384
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   385
val setup = Theory.add_oracle ("normalization", normalization_oracle)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   386
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   387
local structure P = OuterParse and K = OuterKeyword in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   388
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   389
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   390
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   391
val nbeP =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   392
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   393
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_cmd));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   394
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   395
val _ = OuterSyntax.add_parsers [nbeP];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   396
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   397
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   398
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   399
end;