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