src/Pure/Tools/nbe.ML
author haftmann
Fri, 13 Oct 2006 16:52:47 +0200
changeset 21012 f08574148b7a
parent 21002 c879f0150db9
child 21068 a6f47c0e7dbb
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20856
9f7f0bf89e7d cleaned up some mess
haftmann
parents: 20846
diff changeset
     1
(*  Title:      Pure/nbe.ML
9f7f0bf89e7d cleaned up some mess
haftmann
parents: 20846
diff changeset
     2
    ID:         $Id$
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     4
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
     5
Toplevel theory interface for "normalization by evaluation"
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     6
*)
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     7
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
     8
signature NBE =
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     9
sig
20856
9f7f0bf89e7d cleaned up some mess
haftmann
parents: 20846
diff changeset
    10
  (*preconditions: no Vars/TVars in term*)
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    11
  val norm_term: theory -> term -> term
20602
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
    12
  val normalization_conv: cterm -> thm
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    13
  val lookup: string -> NBE_Eval.Univ
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    14
  val update: string * NBE_Eval.Univ -> unit
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    15
  val trace: bool ref
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    16
end;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    17
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    18
structure NBE: NBE =
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    19
struct
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    20
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    21
val trace = ref false;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    22
fun tracing f = if !trace then Output.tracing (f ()) else ();
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    23
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    24
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    25
(* theory data setup *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    26
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    27
structure NBE_Rewrite = TheoryDataFun
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    28
(struct
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    29
  val name = "Pure/nbe";
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    30
  type T = thm list * thm list
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    31
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    32
  val empty = ([],[])
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    33
  val copy = I;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    34
  val extend = I;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    35
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    36
  fun merge _ ((pres1,posts1), (pres2,posts2)) =
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    37
    (Library.merge eq_thm (pres1,pres2), Library.merge eq_thm (posts1,posts2))
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    38
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    39
  fun print _ _ = ()
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    40
end);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    41
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    42
val _ = Context.add_setup NBE_Rewrite.init;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    43
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    44
fun consts_of_pres thy = 
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    45
  let val pres = fst(NBE_Rewrite.get thy);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    46
      val rhss = map (snd o Logic.dest_equals o prop_of) pres;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    47
  in (fold o fold_aterms)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    48
        (fn Const c => insert (op =) (CodegenConsts.norm_of_typ thy c) | _ => I)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    49
        rhss []
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    50
  end
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    51
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    52
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    53
local
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    54
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    55
fun attr_pre (thy,thm) =
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    56
 ((Context.map_theory o NBE_Rewrite.map o apfst) (insert eq_thm thm) thy, thm)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    57
fun attr_post (thy,thm) = 
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    58
 ((Context.map_theory o NBE_Rewrite.map o apsnd) (insert eq_thm thm) thy, thm)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    59
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    60
in
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    61
val _ = Context.add_setup
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    62
  (Attrib.add_attributes
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    63
     [("normal_pre", Attrib.no_args attr_pre, "declare pre-theorems for normalization"),
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    64
      ("normal_post", Attrib.no_args attr_post, "declare posy-theorems for normalization")]);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    65
end;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    66
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    67
fun apply_pres thy =
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    68
  let val pres = fst(NBE_Rewrite.get thy)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    69
  in map (CodegenData.rewrite_func pres) end
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    70
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    71
fun apply_posts thy =
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    72
  let val posts = snd(NBE_Rewrite.get thy)
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    73
  in Tactic.rewrite false posts end
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    74
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    75
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    76
structure NBE_Data = CodeDataFun
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    77
(struct
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    78
  val name = "Pure/NBE"
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    79
  type T = NBE_Eval.Univ Symtab.table
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    80
  val empty = Symtab.empty
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    81
  fun merge _ = Symtab.merge (K true)
20937
4297a44e26ae generalized purge
haftmann
parents: 20920
diff changeset
    82
  fun purge _ _ _ = Symtab.empty
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    83
end);
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    84
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    85
val _ = Context.add_setup NBE_Data.init;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    86
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    87
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    88
(* sandbox communication *)
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    89
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    90
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    91
fun lookup s = (the o Symtab.lookup (!tab)) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    92
fun update sx = (tab := Symtab.update sx (!tab));
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    93
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    94
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    95
(* norm by eval *)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    96
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    97
local
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    98
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    99
(* FIXME better turn this into a function
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   100
    NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   101
    with implicit side effect *)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   102
fun use_code NONE = ()
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   103
  | use_code (SOME s) =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   104
      (tracing (fn () => "\n---generated code:\n" ^ s);
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   105
       use_text (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   106
            Output.tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   107
        (!trace) s);
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   108
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   109
fun generate thy funs =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   110
  let
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   111
    val _ = tab := NBE_Data.get thy;;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   112
    val _ = Library.seq (use_code o NBE_Codegen.generate thy
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   113
      (fn s => Symtab.defined (!tab) s)) funs;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   114
  in NBE_Data.change thy (K (!tab)) end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   115
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   116
fun compile_term thy t =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   117
  let
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   118
    (*FIXME: proper interfaces in codegen_*)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   119
    val (consts, cs) = CodegenConsts.consts_of thy t;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   120
    val funcgr = CodegenFuncgr.mk_funcgr thy consts cs;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   121
    (*FIXME: proper interfaces in codegen_*)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   122
    fun const_typ (c, ty) =
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   123
      let
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   124
        val const = CodegenConsts.norm_of_typ thy (c, ty);
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   125
      in case CodegenFuncgr.get_funcs funcgr const
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   126
       of (thm :: _) => CodegenData.typ_func thy thm
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   127
        | [] => Sign.the_const_type thy c
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   128
      end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   129
    val (_, ct) = CodegenData.preprocess_cterm thy const_typ (Thm.cterm_of thy t)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   130
    val t' = Thm.term_of ct;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   131
    val (consts, cs) = CodegenConsts.consts_of thy t';
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   132
    val pre_consts = consts_of_pres thy;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   133
    val consts' = pre_consts @ consts;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   134
    val funcgr = CodegenFuncgr.mk_funcgr thy consts' cs;
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   135
    val nbe_tab = NBE_Data.get thy;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   136
    val all_consts =
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   137
      (pre_consts :: CodegenFuncgr.all_deps_of funcgr consts')
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   138
      |> (map o filter_out) (Symtab.defined nbe_tab o CodegenNames.const thy)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   139
      |> filter_out null;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   140
    val funs = (map o map)
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   141
      (fn c => (CodegenNames.const thy c, apply_pres thy (CodegenFuncgr.get_funcs funcgr c))) all_consts;
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   142
    val _ = tracing (fn () => "new definitions: " ^ (commas o maps (map fst)) funs);
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   143
    val _ = generate thy funs;
20856
9f7f0bf89e7d cleaned up some mess
haftmann
parents: 20846
diff changeset
   144
    val nt = NBE_Eval.eval thy (!tab) t';
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   145
  in nt end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   146
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   147
fun subst_Frees [] = I
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   148
  | subst_Frees inst =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   149
      Term.map_aterms (fn (t as Free(s, _)) => the_default t (AList.lookup (op =) inst s)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   150
                  | t => t);
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   151
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   152
fun var_tab t = (Term.add_frees t [], Term.add_vars t []);
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   153
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   154
fun anno_vars (Ftab, Vtab) =
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   155
  subst_Vars  (map (fn (ixn, T) => (ixn, Var(ixn,T))) Vtab) o
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   156
  subst_Frees (map (fn (s, T) =>   (s,   Free(s,T)))  Ftab)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   157
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   158
in
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   159
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   160
fun norm_term thy t =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   161
  let
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   162
    val _ = tracing (fn () => "Input:\n" ^ Display.raw_string_of_term t);
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   163
    val nt = compile_term thy t;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   164
    val vtab = var_tab t;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   165
    val ty = type_of t;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   166
    fun constrain ty t = Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   167
        (K NONE) (K NONE) Name.context false ([t], ty) |> fst;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   168
    val _ = tracing (fn () => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt);
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   169
    val t1 = NBE_Codegen.nterm_to_term thy nt;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   170
    val _ = tracing (fn () =>"Converted back:\n" ^ Display.raw_string_of_term t1);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   171
    val t2 = anno_vars vtab t1;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   172
    val _ = tracing (fn () =>"Vars typed:\n" ^ Display.raw_string_of_term t2);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   173
    val t3 = constrain ty t2;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   174
    val _ = if null (Term.term_tvars t3) then () else
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   175
      error ("Illegal schematic type variables in normalized term: "
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   176
        ^ setmp show_types true (Sign.string_of_term thy) t3);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   177
    val eq = apply_posts thy (Thm.cterm_of thy t3);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   178
    val t4 = snd(Logic.dest_equals(prop_of eq))
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   179
  in t4 end;
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   180
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   181
fun norm_print_term ctxt modes t =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   182
  let
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   183
    val thy = ProofContext.theory_of ctxt;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   184
    val t' = norm_term thy t;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   185
    val ty = Term.type_of t';
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   186
    val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   187
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   188
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   189
  in Pretty.writeln p end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   190
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   191
fun norm_print_term_e (modes, raw_t) state =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   192
  let
21002
c879f0150db9 print_evaluated_term: Toplevel.context_of;
wenzelm
parents: 20937
diff changeset
   193
    val ctxt = Context.proof_of (Toplevel.context_of state);
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   194
  in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   195
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   196
end; (*local*)
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   197
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   198
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   199
(* normalization oracle *)
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
   200
20602
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   201
exception Normalization of term;
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   202
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   203
fun normalization_oracle (thy, Normalization t) =
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   204
  Logic.mk_equals (t, norm_term thy t);
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   205
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   206
fun normalization_conv ct =
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   207
  let val {sign, t, ...} = rep_cterm ct
20677
5ce43b38a175 tuned oracle name;
wenzelm
parents: 20602
diff changeset
   208
  in Thm.invoke_oracle_i sign "Pure.normalization" (sign, Normalization t) end;
20602
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   209
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   210
val _ = Context.add_setup
20677
5ce43b38a175 tuned oracle name;
wenzelm
parents: 20602
diff changeset
   211
  (Theory.add_oracle ("normalization", normalization_oracle));
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   212
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   213
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   214
(* Isar setup *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   215
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   216
local structure P = OuterParse and K = OuterKeyword in
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   217
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   218
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   219
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   220
val nbeP =
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   221
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   222
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_e));
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   223
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   224
end;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   225
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   226
val _ = OuterSyntax.add_parsers [nbeP];
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   227
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   228
end;