src/Tools/nbe.ML
author wenzelm
Sun Sep 23 22:23:27 2007 +0200 (2007-09-23)
changeset 24680 0d355aa59e67
parent 24634 38db11874724
child 24713 8b3b6d09ef40
permissions -rw-r--r--
TypeInfer.constrain: canonical argument order;
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@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@24381
    18
      Const of string * Univ list            (*named (uninterpreted) constants*)
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@24590
    27
  val univs_ref: (unit -> Univ list) ref 
haftmann@24423
    28
  val lookup_fun: string -> 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@24381
    66
    Const of string * Univ list        (*named (uninterpreted) constants*)
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@24381
    70
                                      (*abstractions as closures*);
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@24590
    94
val univs_ref = ref (fn () => [] : Univ list);
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@24590
   109
        val _ = univs_ref := (fn () => []);
haftmann@24155
   110
        val s = "Nbe.univs_ref := " ^ raw_s;
haftmann@24381
   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@24590
   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@24590
   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@24590
   139
val ml_delay = ml_abs "()"
haftmann@24590
   140
haftmann@24155
   141
fun ml_fundefs ([(name, [([], e)])]) =
haftmann@24155
   142
      "val " ^ name ^ " = " ^ e ^ "\n"
haftmann@24155
   143
  | ml_fundefs (eqs :: eqss) =
haftmann@24155
   144
      let
haftmann@24155
   145
        fun fundef (name, eqs) =
haftmann@24155
   146
          let
haftmann@24155
   147
            fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
haftmann@24155
   148
          in space_implode "\n  | " (map eqn eqs) end;
haftmann@24155
   149
      in
haftmann@24155
   150
        (prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
haftmann@24155
   151
        |> space_implode "\n"
haftmann@24155
   152
        |> suffix "\n"
haftmann@24155
   153
      end;
haftmann@24155
   154
haftmann@24155
   155
(* nbe specific syntax *)
haftmann@24155
   156
haftmann@24155
   157
local
haftmann@24155
   158
  val prefix =          "Nbe.";
haftmann@24155
   159
  val name_const =      prefix ^ "Const";
haftmann@24155
   160
  val name_free =       prefix ^ "free";
haftmann@24155
   161
  val name_abs =        prefix ^ "abs";
haftmann@24155
   162
  val name_app =        prefix ^ "app";
haftmann@24155
   163
  val name_lookup_fun = prefix ^ "lookup_fun";
haftmann@24155
   164
in
haftmann@24155
   165
haftmann@24155
   166
fun nbe_const c ts = name_const `$` ("(" ^ ML_Syntax.print_string c ^ ", " ^ ml_list ts ^ ")");
haftmann@24155
   167
fun nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
haftmann@24155
   168
fun nbe_free v = name_free `$$` [ML_Syntax.print_string v, ml_list []];
haftmann@24155
   169
fun nbe_bound v = "v_" ^ v;
haftmann@24155
   170
haftmann@24155
   171
fun nbe_apps e es =
haftmann@24155
   172
  Library.foldr (fn (s, e) => name_app `$$` [e, s]) (es, e);
haftmann@24155
   173
haftmann@24155
   174
fun nbe_abss 0 f = f `$` ml_list []
haftmann@24155
   175
  | nbe_abss n f = name_abs `$$` [string_of_int n, f, ml_list []];
haftmann@24155
   176
haftmann@24155
   177
fun nbe_lookup c = ml_Val (nbe_fun c) (name_lookup_fun `$` ML_Syntax.print_string c);
haftmann@24155
   178
haftmann@24155
   179
val nbe_value = "value";
haftmann@24155
   180
haftmann@24155
   181
end;
haftmann@24155
   182
haftmann@24219
   183
open BasicCodeThingol;
haftmann@24155
   184
haftmann@24155
   185
(* greetings to Tarski *)
haftmann@24155
   186
haftmann@24155
   187
fun assemble_iterm thy is_fun num_args =
haftmann@24155
   188
  let
haftmann@24155
   189
    fun of_iterm t =
haftmann@24155
   190
      let
haftmann@24219
   191
        val (t', ts) = CodeThingol.unfold_app t
haftmann@24347
   192
      in of_iapp t' (fold (cons o of_iterm) ts []) end
haftmann@24347
   193
    and of_iconst c ts = case num_args c
haftmann@24347
   194
     of SOME n => if n <= length ts
haftmann@24347
   195
          then let val (args2, args1) = chop (length ts - n) ts
haftmann@24347
   196
          in nbe_apps (nbe_fun c `$` ml_list args1) args2
haftmann@24347
   197
          end else nbe_const c ts
haftmann@24347
   198
      | NONE => if is_fun c then nbe_apps (nbe_fun c) ts
haftmann@24347
   199
          else nbe_const c ts
haftmann@24347
   200
    and of_iapp (IConst (c, (dss, _))) ts = of_iconst c ts
haftmann@24347
   201
      | of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts
haftmann@24347
   202
      | of_iapp ((v, _) `|-> t) ts =
haftmann@24155
   203
          nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
haftmann@24347
   204
      | of_iapp (ICase (((t, _), cs), t0)) ts =
haftmann@24155
   205
          nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
haftmann@24155
   206
            @ [("_", of_iterm t0)])) ts
haftmann@24155
   207
  in of_iterm end;
haftmann@24155
   208
haftmann@24155
   209
fun assemble_fun thy is_fun num_args (c, eqns) =
haftmann@24155
   210
  let
haftmann@24155
   211
    val assemble_arg = assemble_iterm thy (K false) (K NONE);
haftmann@24155
   212
    val assemble_rhs = assemble_iterm thy is_fun num_args;
haftmann@24155
   213
    fun assemble_eqn (args, rhs) =
haftmann@24155
   214
      ([ml_list (map assemble_arg (rev args))], assemble_rhs rhs);
haftmann@24155
   215
    val default_params = map nbe_bound
haftmann@24155
   216
      (Name.invent_list [] "a" ((the o num_args) c));
haftmann@24155
   217
    val default_eqn = ([ml_list default_params], nbe_const c default_params);
haftmann@24155
   218
  in map assemble_eqn eqns @ [default_eqn] end;
haftmann@24155
   219
haftmann@24347
   220
fun assemble_eqnss thy is_fun ([], deps) = ([], "")
haftmann@24347
   221
  | assemble_eqnss thy is_fun (eqnss, deps) =
haftmann@24155
   222
      let
haftmann@24155
   223
        val cs = map fst eqnss;
haftmann@24155
   224
        val num_args = cs ~~ map (fn (_, (args, rhs) :: _) => length args) eqnss;
haftmann@24219
   225
        val funs = fold (fold (CodeThingol.fold_constnames
haftmann@24155
   226
          (insert (op =))) o map snd o snd) eqnss [];
haftmann@24155
   227
        val bind_funs = map nbe_lookup (filter is_fun funs);
haftmann@24155
   228
        val bind_locals = ml_fundefs (map nbe_fun cs ~~ map
haftmann@24155
   229
          (assemble_fun thy is_fun (AList.lookup (op =) num_args)) eqnss);
haftmann@24590
   230
        val result = ml_list (map (fn (c, n) => nbe_abss n (nbe_fun c)) num_args)
haftmann@24590
   231
          |> ml_delay;
haftmann@24155
   232
      in (cs, ml_Let (bind_funs @ [bind_locals]) result) end;
haftmann@24155
   233
haftmann@24381
   234
fun assemble_eval thy is_fun (((vs, ty), t), deps) =
haftmann@24155
   235
  let
haftmann@24219
   236
    val funs = CodeThingol.fold_constnames (insert (op =)) t [];
haftmann@24219
   237
    val frees = CodeThingol.fold_unbound_varnames (insert (op =)) t [];
haftmann@24155
   238
    val bind_funs = map nbe_lookup (filter is_fun funs);
haftmann@24155
   239
    val bind_value = ml_fundefs [(nbe_value, [([ml_list (map nbe_bound frees)],
haftmann@24155
   240
      assemble_iterm thy is_fun (K NONE) t)])];
haftmann@24590
   241
    val result = ml_list [nbe_value `$` ml_list (map nbe_free frees)]
haftmann@24590
   242
      |> ml_delay;
haftmann@24155
   243
  in ([nbe_value], ml_Let (bind_funs @ [bind_value]) result) end;
haftmann@24155
   244
haftmann@24381
   245
fun eqns_of_stmt ((_, CodeThingol.Fun (_, [])), _) =
haftmann@24155
   246
      NONE
haftmann@24381
   247
  | eqns_of_stmt ((name, CodeThingol.Fun (_, eqns)), deps) =
haftmann@24590
   248
      SOME ((name, map fst eqns), deps)
haftmann@24347
   249
  | eqns_of_stmt ((_, CodeThingol.Datatypecons _), _) =
haftmann@24155
   250
      NONE
haftmann@24347
   251
  | eqns_of_stmt ((_, CodeThingol.Datatype _), _) =
haftmann@24155
   252
      NONE
haftmann@24347
   253
  | eqns_of_stmt ((_, CodeThingol.Class _), _) =
haftmann@24155
   254
      NONE
haftmann@24347
   255
  | eqns_of_stmt ((_, CodeThingol.Classrel _), _) =
haftmann@24155
   256
      NONE
haftmann@24347
   257
  | eqns_of_stmt ((_, CodeThingol.Classop _), _) =
haftmann@24155
   258
      NONE
haftmann@24347
   259
  | eqns_of_stmt ((_, CodeThingol.Classinst _), _) =
haftmann@24155
   260
      NONE;
haftmann@24155
   261
haftmann@24155
   262
fun compile_stmts thy is_fun =
haftmann@24155
   263
  map_filter eqns_of_stmt
haftmann@24347
   264
  #> split_list
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@24347
   286
    val stmtss = rev (Graph.strong_conn code)
haftmann@24347
   287
      |> (map o map_filter) (fn name => if Symtab.defined nbe_tab name
haftmann@24347
   288
           then NONE
haftmann@24347
   289
           else SOME ((name, Graph.get_node code name), Graph.imm_succs code name))
haftmann@24347
   290
      |> filter_out null
haftmann@24155
   291
  in fold compile' stmtss nbe_tab end;
haftmann@24155
   292
haftmann@24155
   293
(* re-conversion *)
haftmann@24155
   294
haftmann@24155
   295
fun term_of_univ thy t =
haftmann@24155
   296
  let
haftmann@24155
   297
    fun of_apps bounds (t, ts) =
haftmann@24155
   298
      fold_map (of_univ bounds) ts
haftmann@24155
   299
      #>> (fn ts' => list_comb (t, rev ts'))
haftmann@24155
   300
    and of_univ bounds (Const (name, ts)) typidx =
haftmann@24155
   301
          let
haftmann@24423
   302
            val SOME c = CodeName.const_rev thy name;
haftmann@24423
   303
            val T = Code.default_typ thy c;
haftmann@24155
   304
            val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, S)) T;
haftmann@24155
   305
            val typidx' = typidx + maxidx_of_typ T' + 1;
haftmann@24155
   306
          in of_apps bounds (Term.Const (c, T'), ts) typidx' end
haftmann@24155
   307
      | of_univ bounds (Free (name, ts)) typidx =
haftmann@24155
   308
          of_apps bounds (Term.Free (name, dummyT), ts) typidx
haftmann@24155
   309
      | of_univ bounds (BVar (name, ts)) typidx =
haftmann@24155
   310
          of_apps bounds (Bound (bounds - name - 1), ts) typidx
haftmann@24155
   311
      | of_univ bounds (t as Abs _) typidx =
haftmann@24155
   312
          typidx
haftmann@24155
   313
          |> of_univ (bounds + 1) (app t (BVar (bounds, [])))
haftmann@24155
   314
          |-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
haftmann@24155
   315
  in of_univ 0 t 0 |> fst end;
haftmann@24155
   316
haftmann@24155
   317
(* evaluation with type reconstruction *)
haftmann@24155
   318
haftmann@24381
   319
fun eval thy code t vs_ty_t deps =
haftmann@24155
   320
  let
haftmann@24347
   321
    val ty = type_of t;
haftmann@24155
   322
    fun subst_Frees [] = I
haftmann@24155
   323
      | subst_Frees inst =
haftmann@24155
   324
          Term.map_aterms (fn (t as Term.Free (s, _)) => the_default t (AList.lookup (op =) inst s)
haftmann@24155
   325
                            | t => t);
haftmann@24155
   326
    val anno_vars =
haftmann@24155
   327
      subst_Frees (map (fn (s, T) => (s, Term.Free (s, T))) (Term.add_frees t []))
haftmann@24155
   328
      #> subst_Vars (map (fn (ixn, T) => (ixn, Var (ixn, T))) (Term.add_vars t []))
haftmann@24347
   329
    fun constrain t =
wenzelm@24680
   330
      singleton (Syntax.check_terms (ProofContext.init thy)) (TypeInfer.constrain ty t);
haftmann@24155
   331
    fun check_tvars t = if null (Term.term_tvars t) then t else
haftmann@24155
   332
      error ("Illegal schematic type variables in normalized term: "
haftmann@24155
   333
        ^ setmp show_types true (Sign.string_of_term thy) t);
haftmann@24155
   334
  in
haftmann@24381
   335
    (vs_ty_t, deps)
haftmann@24155
   336
    |> eval_term thy (Symtab.defined (ensure_funs thy code))
haftmann@24155
   337
    |> term_of_univ thy
haftmann@24155
   338
    |> tracing (fn t => "Normalized:\n" ^ setmp show_types true Display.raw_string_of_term t)
haftmann@24155
   339
    |> anno_vars
haftmann@24155
   340
    |> tracing (fn t => "Vars typed:\n" ^ setmp show_types true Display.raw_string_of_term 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@24381
   344
    |> tracing (fn _ => "---\n")
haftmann@24155
   345
  end;
haftmann@24155
   346
haftmann@24155
   347
(* evaluation oracle *)
haftmann@24155
   348
haftmann@24381
   349
exception Normalization of CodeThingol.code * term
haftmann@24381
   350
  * (CodeThingol.typscheme * CodeThingol.iterm) * string list;
haftmann@24155
   351
haftmann@24381
   352
fun normalization_oracle (thy, Normalization (code, t, vs_ty_t, deps)) =
haftmann@24381
   353
  Logic.mk_equals (t, eval thy code t vs_ty_t deps);
haftmann@24155
   354
haftmann@24381
   355
fun normalization_invoke thy code t vs_ty_t deps =
haftmann@24381
   356
  Thm.invoke_oracle_i thy "HOL.normalization" (thy, Normalization (code, t, vs_ty_t, deps));
haftmann@24283
   357
  (*FIXME get rid of hardwired theory name*)
haftmann@24155
   358
haftmann@24155
   359
fun normalization_conv ct =
haftmann@24155
   360
  let
haftmann@24155
   361
    val thy = Thm.theory_of_cterm ct;
haftmann@24381
   362
    fun conv code vs_ty_t deps ct =
haftmann@24155
   363
      let
haftmann@24155
   364
        val t = Thm.term_of ct;
haftmann@24381
   365
      in normalization_invoke thy code t vs_ty_t deps end;
haftmann@24219
   366
  in CodePackage.eval_conv thy conv ct end;
haftmann@24155
   367
haftmann@24155
   368
(* evaluation command *)
haftmann@24155
   369
haftmann@24155
   370
fun norm_print_term ctxt modes t =
haftmann@24155
   371
  let
haftmann@24155
   372
    val thy = ProofContext.theory_of ctxt;
haftmann@24155
   373
    val ct = Thm.cterm_of thy t;
haftmann@24155
   374
    val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct;
haftmann@24155
   375
    val ty = Term.type_of t';
wenzelm@24634
   376
    val p = PrintMode.with_modes modes (fn () =>
haftmann@24155
   377
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
haftmann@24155
   378
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
haftmann@24155
   379
  in Pretty.writeln p end;
haftmann@24155
   380
haftmann@24155
   381
haftmann@24155
   382
(** Isar setup **)
haftmann@24155
   383
wenzelm@24508
   384
fun norm_print_term_cmd (modes, s) state =
haftmann@24155
   385
  let val ctxt = Toplevel.context_of state
wenzelm@24508
   386
  in norm_print_term ctxt modes (Syntax.read_term ctxt s) end;
haftmann@24155
   387
haftmann@24155
   388
val setup = Theory.add_oracle ("normalization", normalization_oracle)
haftmann@24155
   389
haftmann@24155
   390
local structure P = OuterParse and K = OuterKeyword in
haftmann@24155
   391
haftmann@24155
   392
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
haftmann@24155
   393
haftmann@24155
   394
val nbeP =
haftmann@24155
   395
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
haftmann@24155
   396
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_cmd));
haftmann@24155
   397
haftmann@24155
   398
val _ = OuterSyntax.add_parsers [nbeP];
haftmann@24155
   399
haftmann@24155
   400
end;
haftmann@24155
   401
haftmann@24155
   402
end;