src/Tools/nbe.ML
author haftmann
Mon, 20 Aug 2007 18:07:31 +0200
changeset 24347 245ff8661b8c
parent 24292 26ac9fe0e80e
child 24381 560e8ecdf633
permissions -rw-r--r--
explizit dependencies
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 
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
    28
  val lookup_fun: CodeName.const -> Univ
24155
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
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    65
datatype Univ = 
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    66
    Const of string * Univ list        (*named constructors*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    67
  | Free of string * Univ list         (*free variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    68
  | BVar of int * Univ list            (*bound named variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    69
  | Abs of (int * (Univ list -> Univ)) * Univ list
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    70
                                      (*functions*);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    71
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    72
(* constructor functions *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    73
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    74
val free = curry Free;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    75
fun abs n f ts = Abs ((n, f), ts);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    76
fun app (Abs ((1, f), xs)) x = f (x :: xs)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    77
  | app (Abs ((n, f), xs)) x = Abs ((n - 1, f), x :: xs)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    78
  | app (Const (name, args)) x = Const (name, x :: args)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    79
  | app (Free (name, args)) x = Free (name, x :: args)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    80
  | app (BVar (name, args)) x = BVar (name, x :: args);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    81
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    82
(* global functions store *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    83
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    84
structure Nbe_Functions = CodeDataFun
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    85
(struct
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    86
  type T = Univ Symtab.table;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    87
  val empty = Symtab.empty;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    88
  fun merge _ = Symtab.merge (K true);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    89
  fun purge _ _ _ = Symtab.empty;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    90
end);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    91
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    92
(* sandbox communication *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    93
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    94
val univs_ref = ref [] : Univ list ref;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    95
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    96
local
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    97
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    98
val tab_ref = ref NONE : Univ Symtab.table option ref;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    99
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   100
in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   101
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   102
fun lookup_fun s = case ! tab_ref
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   103
 of NONE => error "compile_univs"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   104
  | SOME tab => (the o Symtab.lookup tab) s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   105
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   106
fun compile_univs tab ([], _) = []
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   107
  | compile_univs tab (cs, raw_s) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   108
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   109
        val _ = univs_ref := [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   110
        val s = "Nbe.univs_ref := " ^ raw_s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   111
        val _ = tracing (fn () => "\n---generated code:\n" ^ s) ();
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   112
        val _ = tab_ref := SOME tab;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   113
        val _ = use_text "" (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   114
          Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   115
          (!trace) s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   116
        val _ = tab_ref := NONE;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   117
        val univs = case !univs_ref of [] => error "compile_univs" | univs => univs;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   118
      in cs ~~ univs end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   119
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   120
end; (*local*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   121
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   122
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   123
(** assembling and compiling ML code from terms **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   124
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   125
(* abstract ML syntax *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   126
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   127
infix 9 `$` `$$`;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   128
fun e1 `$` e2 = "(" ^ e1 ^ " " ^ e2 ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   129
fun e `$$` es = "(" ^ e ^ " " ^ space_implode " " es ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   130
fun ml_abs v e = "(fn" ^ v ^ " => " ^ e ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   131
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   132
fun ml_Val v s = "val " ^ v ^ " = " ^ s;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   133
fun ml_cases t cs =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   134
  "(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   135
fun ml_Let ds e = "let\n" ^ space_implode "\n" ds ^ " in " ^ e ^ " end";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   136
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   137
fun ml_list es = "[" ^ commas es ^ "]";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   138
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   139
fun ml_fundefs ([(name, [([], e)])]) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   140
      "val " ^ name ^ " = " ^ e ^ "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   141
  | ml_fundefs (eqs :: eqss) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   142
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   143
        fun fundef (name, eqs) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   144
          let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   145
            fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   146
          in space_implode "\n  | " (map eqn eqs) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   147
      in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   148
        (prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   149
        |> space_implode "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   150
        |> suffix "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   151
      end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   152
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   153
(* nbe specific syntax *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   154
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   155
local
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   156
  val prefix =          "Nbe.";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   157
  val name_const =      prefix ^ "Const";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   158
  val name_free =       prefix ^ "free";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   159
  val name_abs =        prefix ^ "abs";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   160
  val name_app =        prefix ^ "app";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   161
  val name_lookup_fun = prefix ^ "lookup_fun";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   162
in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   163
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   164
fun nbe_const c ts = name_const `$` ("(" ^ ML_Syntax.print_string c ^ ", " ^ ml_list ts ^ ")");
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   165
fun nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   166
fun nbe_free v = name_free `$$` [ML_Syntax.print_string v, ml_list []];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   167
fun nbe_bound v = "v_" ^ v;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   168
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   169
fun nbe_apps e es =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   170
  Library.foldr (fn (s, e) => name_app `$$` [e, s]) (es, e);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   171
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   172
fun nbe_abss 0 f = f `$` ml_list []
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   173
  | nbe_abss n f = name_abs `$$` [string_of_int n, f, ml_list []];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   174
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   175
fun nbe_lookup c = ml_Val (nbe_fun c) (name_lookup_fun `$` ML_Syntax.print_string c);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   176
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   177
val nbe_value = "value";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   178
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   179
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   180
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   181
open BasicCodeThingol;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   182
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   183
(* greetings to Tarski *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   184
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   185
fun assemble_iterm thy is_fun num_args =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   186
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   187
    fun of_iterm t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   188
      let
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   189
        val (t', ts) = CodeThingol.unfold_app t
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   190
      in of_iapp t' (fold (cons o of_iterm) ts []) end
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   191
    and of_iconst c ts = case num_args c
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   192
     of SOME n => if n <= length ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   193
          then let val (args2, args1) = chop (length ts - n) ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   194
          in nbe_apps (nbe_fun c `$` ml_list args1) args2
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   195
          end else nbe_const c ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   196
      | NONE => if is_fun c then nbe_apps (nbe_fun c) ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   197
          else nbe_const c ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   198
    and of_iapp (IConst (c, (dss, _))) ts = of_iconst c ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   199
      | of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   200
      | of_iapp ((v, _) `|-> t) ts =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   201
          nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   202
      | of_iapp (ICase (((t, _), cs), t0)) ts =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   203
          nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   204
            @ [("_", of_iterm t0)])) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   205
  in of_iterm end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   206
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   207
fun assemble_fun thy is_fun num_args (c, eqns) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   208
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   209
    val assemble_arg = assemble_iterm thy (K false) (K NONE);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   210
    val assemble_rhs = assemble_iterm thy is_fun num_args;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   211
    fun assemble_eqn (args, rhs) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   212
      ([ml_list (map assemble_arg (rev args))], assemble_rhs rhs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   213
    val default_params = map nbe_bound
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   214
      (Name.invent_list [] "a" ((the o num_args) c));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   215
    val default_eqn = ([ml_list default_params], nbe_const c default_params);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   216
  in map assemble_eqn eqns @ [default_eqn] end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   217
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   218
fun assemble_eqnss thy is_fun ([], deps) = ([], "")
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   219
  | assemble_eqnss thy is_fun (eqnss, deps) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   220
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   221
        val cs = map fst eqnss;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   222
        val num_args = cs ~~ map (fn (_, (args, rhs) :: _) => length args) eqnss;
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   223
        val funs = fold (fold (CodeThingol.fold_constnames
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   224
          (insert (op =))) o map snd o snd) eqnss [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   225
        val bind_funs = map nbe_lookup (filter is_fun funs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   226
        val bind_locals = ml_fundefs (map nbe_fun cs ~~ map
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   227
          (assemble_fun thy is_fun (AList.lookup (op =) num_args)) eqnss);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   228
        val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   229
      in (cs, ml_Let (bind_funs @ [bind_locals]) result) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   230
24283
haftmann
parents: 24219
diff changeset
   231
fun assemble_eval thy is_fun (t, deps) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   232
  let
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   233
    val funs = CodeThingol.fold_constnames (insert (op =)) t [];
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   234
    val frees = CodeThingol.fold_unbound_varnames (insert (op =)) t [];
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   235
    val bind_funs = map nbe_lookup (filter is_fun funs);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   236
    val bind_value = ml_fundefs [(nbe_value, [([ml_list (map nbe_bound frees)],
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   237
      assemble_iterm thy is_fun (K NONE) t)])];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   238
    val result = ml_list [nbe_value `$` ml_list (map nbe_free frees)];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   239
  in ([nbe_value], ml_Let (bind_funs @ [bind_value]) result) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   240
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   241
fun eqns_of_stmt ((_, CodeThingol.Fun ([], _)), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   242
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   243
  | eqns_of_stmt ((name, CodeThingol.Fun (eqns, _)), deps) =
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   244
      SOME ((name, eqns), deps)
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   245
  | eqns_of_stmt ((_, CodeThingol.Datatypecons _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   246
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   247
  | eqns_of_stmt ((_, CodeThingol.Datatype _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   248
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   249
  | eqns_of_stmt ((_, CodeThingol.Class _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   250
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   251
  | eqns_of_stmt ((_, CodeThingol.Classrel _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   252
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   253
  | eqns_of_stmt ((_, CodeThingol.Classop _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   254
      NONE
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   255
  | eqns_of_stmt ((_, CodeThingol.Classinst _), _) =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   256
      NONE;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   257
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   258
fun compile_stmts thy is_fun =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   259
  map_filter eqns_of_stmt
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   260
  #> split_list
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   261
  #> assemble_eqnss thy is_fun
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   262
  #> compile_univs (Nbe_Functions.get thy);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   263
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   264
fun eval_term thy is_fun =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   265
  assemble_eval 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
  #> the_single
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   268
  #> snd;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   269
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   270
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   271
(** compilation and evaluation **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   272
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   273
(* ensure global functions *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   274
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   275
fun ensure_funs thy code =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   276
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   277
    fun compile' stmts tab =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   278
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   279
        val compiled = compile_stmts thy (Symtab.defined tab) stmts;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   280
      in Nbe_Functions.change thy (fold Symtab.update compiled) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   281
    val nbe_tab = Nbe_Functions.get thy;
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   282
    val stmtss = rev (Graph.strong_conn code)
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   283
      |> (map o map_filter) (fn name => if Symtab.defined nbe_tab name
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   284
           then NONE
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   285
           else SOME ((name, Graph.get_node code name), Graph.imm_succs code name))
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   286
      |> filter_out null
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   287
  in fold compile' stmtss nbe_tab end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   288
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   289
(* re-conversion *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   290
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   291
fun term_of_univ thy t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   292
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   293
    fun of_apps bounds (t, ts) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   294
      fold_map (of_univ bounds) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   295
      #>> (fn ts' => list_comb (t, rev ts'))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   296
    and of_univ bounds (Const (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   297
          let
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   298
            val SOME (const as (c, _)) = CodeName.const_rev thy name;
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   299
            val T = Code.default_typ thy const;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   300
            val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, S)) T;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   301
            val typidx' = typidx + maxidx_of_typ T' + 1;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   302
          in of_apps bounds (Term.Const (c, T'), ts) typidx' end
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   303
      | of_univ bounds (Free (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   304
          of_apps bounds (Term.Free (name, dummyT), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   305
      | of_univ bounds (BVar (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   306
          of_apps bounds (Bound (bounds - name - 1), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   307
      | of_univ bounds (t as Abs _) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   308
          typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   309
          |> of_univ (bounds + 1) (app t (BVar (bounds, [])))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   310
          |-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   311
  in of_univ 0 t 0 |> fst end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   312
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   313
(* evaluation with type reconstruction *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   314
24283
haftmann
parents: 24219
diff changeset
   315
fun eval thy code t t' deps =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   316
  let
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   317
    val ty = type_of t;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   318
    fun subst_Frees [] = I
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   319
      | subst_Frees inst =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   320
          Term.map_aterms (fn (t as Term.Free (s, _)) => the_default t (AList.lookup (op =) inst s)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   321
                            | t => t);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   322
    val anno_vars =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   323
      subst_Frees (map (fn (s, T) => (s, Term.Free (s, T))) (Term.add_frees t []))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   324
      #> subst_Vars (map (fn (ixn, T) => (ixn, Var (ixn, T))) (Term.add_vars t []))
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   325
    fun constrain t =
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   326
      singleton (ProofContext.infer_types_pats (ProofContext.init thy)) (TypeInfer.constrain t ty);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   327
    fun check_tvars t = if null (Term.term_tvars t) then t else
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   328
      error ("Illegal schematic type variables in normalized term: "
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   329
        ^ setmp show_types true (Sign.string_of_term thy) t);
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   330
  in
24283
haftmann
parents: 24219
diff changeset
   331
    (t', deps)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   332
    |> eval_term thy (Symtab.defined (ensure_funs thy code))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   333
    |> term_of_univ thy
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   334
    |> tracing (fn t => "Normalized:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   335
    |> tracing (fn _ => "Term type:\n" ^ Display.raw_string_of_typ ty)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   336
    |> anno_vars
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   337
    |> tracing (fn t => "Vars typed:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   338
    |> tracing (fn t => setmp show_types true (Sign.string_of_term thy) t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   339
    |> constrain
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   340
    |> tracing (fn t => "Types inferred:\n" ^ setmp show_types true Display.raw_string_of_term t)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   341
    |> check_tvars
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   342
  end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   343
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   344
(* evaluation oracle *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   345
24283
haftmann
parents: 24219
diff changeset
   346
exception Normalization of CodeThingol.code * term * CodeThingol.iterm * string list;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   347
24283
haftmann
parents: 24219
diff changeset
   348
fun normalization_oracle (thy, Normalization (code, t, t', deps)) =
haftmann
parents: 24219
diff changeset
   349
  Logic.mk_equals (t, eval thy code t t' deps);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   350
24283
haftmann
parents: 24219
diff changeset
   351
fun normalization_invoke thy code t t' deps =
24292
26ac9fe0e80e added evaluation examples
haftmann
parents: 24283
diff changeset
   352
  Thm.invoke_oracle_i thy "HOL.normalization" (thy, Normalization (code, t, t', deps));
24283
haftmann
parents: 24219
diff changeset
   353
  (*FIXME get rid of hardwired theory name*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   354
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   355
fun normalization_conv ct =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   356
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   357
    val thy = Thm.theory_of_cterm ct;
24283
haftmann
parents: 24219
diff changeset
   358
    fun conv code (t', ty') deps ct =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   359
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   360
        val t = Thm.term_of ct;
24283
haftmann
parents: 24219
diff changeset
   361
      in normalization_invoke thy code t t' deps end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   362
  in CodePackage.eval_conv thy conv ct end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   363
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   364
(* evaluation command *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   365
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   366
fun norm_print_term ctxt modes t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   367
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   368
    val thy = ProofContext.theory_of ctxt;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   369
    val ct = Thm.cterm_of thy t;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   370
    val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   371
    val ty = Term.type_of t';
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   372
    val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   373
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   374
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   375
  in Pretty.writeln p end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   376
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   377
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   378
(** Isar setup **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   379
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   380
fun norm_print_term_cmd (modes, raw_t) state =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   381
  let val ctxt = Toplevel.context_of state
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   382
  in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   383
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   384
val setup = Theory.add_oracle ("normalization", normalization_oracle)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   385
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   386
local structure P = OuterParse and K = OuterKeyword in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   387
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   388
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   389
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   390
val nbeP =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   391
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   392
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_cmd));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   393
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   394
val _ = OuterSyntax.add_parsers [nbeP];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   395
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   396
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   397
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   398
end;