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