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