src/Pure/Tools/nbe.ML
author aspinall
Tue, 19 Dec 2006 17:35:33 +0100
changeset 21886 f1790ca921e1
parent 21708 45e7491bea47
child 21991 04528ce9ded5
permissions -rw-r--r--
Missing elements from doc_markup_elements
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*)
20602
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
    11
  val normalization_conv: cterm -> thm
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    12
  val lookup: string -> NBE_Eval.Univ
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    13
  val update: string * NBE_Eval.Univ -> unit
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    14
  val trace: bool ref
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    15
end;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    16
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    17
structure NBE: NBE =
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    18
struct
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    19
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    20
val trace = ref false;
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
    21
fun tracing f x = if !trace then (Output.tracing (f x); x) else x;
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    22
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    23
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    24
(** data setup **)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    25
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    26
(* preproc and postproc attributes *)
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    27
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    28
structure NBE_Rewrite = TheoryDataFun
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    29
(struct
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    30
  val name = "Pure/nbe";
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    31
  type T = thm list * thm list
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    32
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    33
  val empty = ([],[])
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    34
  val copy = I;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    35
  val extend = I;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    36
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    37
  fun merge _ ((pres1,posts1), (pres2,posts2)) =
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    38
    (Library.merge eq_thm (pres1,pres2), Library.merge eq_thm (posts1,posts2))
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    39
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    40
  fun print _ _ = ()
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    41
end);
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    42
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    43
val _ =
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    44
  let
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    45
    fun map_data f = Context.mapping (NBE_Rewrite.map f) I;
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    46
    fun attr_pre (thy, thm) =
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    47
      ((map_data o apfst) (insert eq_thm thm) thy, thm)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    48
    fun attr_post (thy, thm) = 
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    49
      ((map_data o apsnd) (insert eq_thm thm) thy, thm)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    50
    val attr = Attrib.syntax (Scan.lift (Args.$$$ "pre" >> K attr_pre
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    51
      || Args.$$$ "post" >> K attr_post));
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    52
  in
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    53
    Context.add_setup (
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    54
      NBE_Rewrite.init
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    55
      #> Attrib.add_attributes
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    56
          [("normal", attr, "declare rewrite theorems for normalization")]
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    57
    )
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    58
  end;
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    59
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    60
fun consts_of_pres thy = 
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    61
  let
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    62
    val ctxt = ProofContext.init thy;
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    63
    val pres = fst (NBE_Rewrite.get thy);
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    64
    val rhss = map (snd o Logic.dest_equals o prop_of o LocalDefs.meta_rewrite_rule ctxt) pres;
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    65
  in
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    66
    (fold o fold_aterms)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    67
      (fn Const c => insert (op =) (CodegenConsts.norm_of_typ thy c) | _ => I)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    68
      rhss []
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    69
  end;
20920
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_pres thy =
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    72
  let
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    73
    val ctxt = ProofContext.init thy;
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    74
    val pres = (map (LocalDefs.meta_rewrite_rule ctxt) o fst) (NBE_Rewrite.get thy)
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    75
  in map (CodegenData.rewrite_func pres) end
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    76
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    77
fun apply_posts thy =
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    78
  let
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    79
    val ctxt = ProofContext.init thy;
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
    80
    val posts = (map (LocalDefs.meta_rewrite_rule ctxt) o snd) (NBE_Rewrite.get thy)
21708
45e7491bea47 reorganized structure Tactic vs. MetaSimplifier;
wenzelm
parents: 21506
diff changeset
    81
  in MetaSimplifier.rewrite false posts end
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    82
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
    83
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    84
(* code store *)
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    85
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
    86
structure NBE_Data = CodeDataFun
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    87
(struct
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    88
  val name = "Pure/NBE"
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    89
  type T = NBE_Eval.Univ Symtab.table
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    90
  val empty = Symtab.empty
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    91
  fun merge _ = Symtab.merge (K true)
20937
4297a44e26ae generalized purge
haftmann
parents: 20920
diff changeset
    92
  fun purge _ _ _ = Symtab.empty
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    93
end);
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    94
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    95
val _ = Context.add_setup NBE_Data.init;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    96
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    97
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
    98
(** norm by eval **)
21068
a6f47c0e7dbb cleanup
haftmann
parents: 21002
diff changeset
    99
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   100
(* sandbox communication *)
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   101
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   102
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   103
fun lookup s = (the o Symtab.lookup (!tab)) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   104
fun update sx = (tab := Symtab.update sx (!tab));
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   105
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   106
local
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   107
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   108
(* function generation *)
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   109
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   110
fun generate thy funs =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   111
  let
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   112
    (* FIXME better turn this into a function
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   113
        NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   114
        with implicit side effect *)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   115
    fun use_code NONE = ()
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   116
      | use_code (SOME s) =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   117
          (tracing (fn () => "\n---generated code:\n" ^ s);
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   118
           use_text (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   119
                Output.tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   120
            (!trace) s);
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   121
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   122
    val _ = tracing (fn () => "new definitions: " ^ (commas o maps (map fst)) funs);
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   123
    val _ = tab := NBE_Data.get thy;;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   124
    val _ = Library.seq (use_code o NBE_Codegen.generate thy
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   125
      (fn s => Symtab.defined (!tab) s)) funs;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   126
  in NBE_Data.change thy (K (!tab)) end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   127
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   128
fun ensure_funs thy t =
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   129
  let
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   130
    val consts = CodegenConsts.consts_of thy t;
20920
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   131
    val pre_consts = consts_of_pres thy;
07f279940664 added pre/post-processor equations
nipkow
parents: 20856
diff changeset
   132
    val consts' = pre_consts @ consts;
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   133
    val funcgr = CodegenFuncgr.make thy consts';
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   134
    val nbe_tab = NBE_Data.get thy;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   135
    val all_consts =
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   136
      (pre_consts :: CodegenFuncgr.deps funcgr consts')
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   137
      |> (map o filter_out) (Symtab.defined nbe_tab o CodegenNames.const thy)
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   138
      |> filter_out null;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   139
    val funs = (map o map)
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   140
      (fn c => (CodegenNames.const thy c, apply_pres thy (CodegenFuncgr.funcs funcgr c))) all_consts;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   141
  in generate thy funs end;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   142
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   143
(* term evaluation *)
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   144
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   145
fun eval_term thy t =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   146
  let
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   147
    fun subst_Frees [] = I
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   148
      | subst_Frees inst =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   149
          Term.map_aterms (fn (t as Free(s, _)) => the_default t (AList.lookup (op =) inst s)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   150
                      | t => t);
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   151
    val anno_vars =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   152
      subst_Frees (map (fn (s, T) => (s, Free (s, T))) (Term.add_frees t []))
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   153
      #> subst_Vars  (map (fn (ixn, T) => (ixn, Var(ixn,T))) (Term.add_vars t []))
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   154
    fun check_tvars t = if null (Term.term_tvars t) then t else
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   155
      error ("Illegal schematic type variables in normalized term: "
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   156
        ^ setmp show_types true (Sign.string_of_term thy) t);
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   157
    val ty = type_of t;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   158
    fun constrain t = Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   159
      (K NONE) (K NONE) Name.context false ([t], ty) |> fst;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   160
    val _ = ensure_funs thy t;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   161
  in
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   162
    t
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   163
    |> tracing (fn t => "Input:\n" ^ Display.raw_string_of_term t)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   164
    |> NBE_Eval.eval thy (!tab)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   165
    |> tracing (fn nt => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   166
    |> NBE_Codegen.nterm_to_term thy
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   167
    |> tracing (fn t =>"Converted back:\n" ^ Display.raw_string_of_term t)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   168
    |> anno_vars
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   169
    |> tracing (fn t =>"Vars typed:\n" ^ Display.raw_string_of_term t)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   170
    |> constrain
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   171
    |> check_tvars
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   172
  end;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   173
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   174
(* evaluation oracle *)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   175
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   176
exception Normalization of term;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
   177
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   178
fun normalization_oracle (thy, Normalization t) =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   179
  Logic.mk_equals (t, eval_term thy t);
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   180
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   181
fun normalization_invoke thy t =
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   182
  Thm.invoke_oracle_i thy "Pure.normalization" (thy, Normalization t);
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   183
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   184
in
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   185
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   186
(* interface *)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   187
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   188
fun normalization_conv ct =
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   189
  let
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   190
    val thy = Thm.theory_of_cterm ct;
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   191
    fun mk drop_classes ct thm1 =
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   192
      let
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   193
        val t = Thm.term_of ct;
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   194
        val thm2 = normalization_invoke thy t;
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   195
        val thm3 = apply_posts thy ((snd o Drule.dest_equals o Thm.cprop_of) thm2);
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   196
        val thm23 = drop_classes (Thm.transitive thm2 thm3);
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   197
      in
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   198
        Thm.transitive thm1 thm23 handle THM _ =>
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   199
          error ("normalization_conv - could not construct proof:\n"
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   200
          ^ (cat_lines o map string_of_thm) [thm1, thm2, thm3])
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   201
      end;
8e621232a865 clarified make_term interface
haftmann
parents: 21124
diff changeset
   202
  in fst (CodegenFuncgr.make_term thy mk ct) end;
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   203
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   204
fun norm_print_term ctxt modes t =
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   205
  let
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   206
    val thy = ProofContext.theory_of ctxt;
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   207
    val ct = Thm.cterm_of thy t;
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   208
    val (_, t') = (Logic.dest_equals o Thm.prop_of o normalization_conv) ct;
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   209
    val ty = Term.type_of t';
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   210
    val p = Library.setmp print_mode (modes @ ! print_mode) (fn () =>
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   211
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   212
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)]) ();
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   213
  in Pretty.writeln p end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   214
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   215
fun norm_print_term_e (modes, raw_t) state =
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 21129
diff changeset
   216
  let val ctxt = Toplevel.context_of state
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   217
  in norm_print_term ctxt modes (ProofContext.read_term ctxt raw_t) end;
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   218
20602
96fa2cf465f5 moved part of normalization oracle here
haftmann
parents: 20191
diff changeset
   219
val _ = Context.add_setup
20677
5ce43b38a175 tuned oracle name;
wenzelm
parents: 20602
diff changeset
   220
  (Theory.add_oracle ("normalization", normalization_oracle));
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   221
21124
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   222
end; (*local*)
8648b5dd6a87 constructing proof
haftmann
parents: 21068
diff changeset
   223
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   224
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   225
(* Isar setup *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   226
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   227
local structure P = OuterParse and K = OuterKeyword in
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   228
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   229
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   230
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   231
val nbeP =
20846
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   232
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
5fde744176d7 various code refinements
haftmann
parents: 20677
diff changeset
   233
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_e));
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   234
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   235
end;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   236
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   237
val _ = OuterSyntax.add_parsers [nbeP];
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   238
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   239
end;