src/Pure/Tools/nbe.ML
author nipkow
Fri, 09 Jun 2006 12:17:58 +0200
changeset 19830 b81d803dfaa4
parent 19795 746274ca400b
child 19962 016ba2d907a7
permissions -rw-r--r--
renamed command
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     1
(*  ID:         $Id$
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     2
    Author:     Tobias Nipkow, Florian Haftmann, TU Muenchen
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
     3
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
     4
Toplevel theory interface for "normalization by evaluation"
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
     5
Preconditions: no Vars
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
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    10
  val norm_by_eval_i: term -> theory -> term * theory;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    11
  val lookup: string -> NBE_Eval.Univ;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    12
  val update: string * NBE_Eval.Univ -> unit
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    13
  val trace_nbe: bool ref
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    14
end;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    15
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    16
structure NBE: NBE =
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    17
struct
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    18
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    19
structure NBE_Data = TheoryDataFun
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    20
(struct
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    21
  val name = "Pure/NBE";
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    22
  type T = NBE_Eval.Univ Symtab.table;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    23
  val empty = Symtab.empty;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    24
  val copy = I;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    25
  val extend = I;
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    26
  fun merge _ = Symtab.merge (K true);
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    27
  fun print _ _ = ();
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    28
end);
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    29
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    30
val trace_nbe = ref false;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    31
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    32
fun nbe_trace fs = if !trace_nbe then tracing(fs()) else ();
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    33
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    34
val _ = Context.add_setup NBE_Data.init;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    35
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    36
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    37
fun lookup s = (the o Symtab.lookup (!tab)) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    38
fun update sx = (tab := Symtab.update sx (!tab));
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    39
fun defined s = Symtab.defined (!tab) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    40
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    41
fun use_show "" = ()
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    42
  | use_show s =
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    43
 (if !trace_nbe then tracing ("\n---generated code:\n"^ s) else ();
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    44
  use_text(tracing o enclose "\n---compiler echo:\n" "\n---\n",
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    45
           tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    46
      (!trace_nbe) s);
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    47
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    48
(* FIXME move to term.ML *)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    49
fun subst_Frees [] tm = tm
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    50
  | subst_Frees inst tm =
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    51
      let
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    52
        fun subst (t as Free(s, _)) = the_default t (AList.lookup (op =) inst s)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    53
          | subst (Abs (a, T, t)) = Abs (a, T, subst t)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    54
          | subst (t $ u) = subst t $ subst u
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    55
          | subst t = t;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    56
      in subst tm end;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    57
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    58
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
    59
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    60
fun anno_vars (Ftab,Vtab) =
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    61
  subst_Vars  (map (fn (ixn,T) => (ixn, Var(ixn,T))) Vtab) o
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    62
  subst_Frees (map (fn (s,T) =>   (s,   Free(s,T)))  Ftab)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    63
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    64
(* FIXME try to use isar_cmd/print_term to take care of context *)
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    65
fun norm_by_eval_i t thy =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19147
diff changeset
    66
  let
1457d810b408 class package and codegen refinements
haftmann
parents: 19147
diff changeset
    67
    val nbe_tab = NBE_Data.get thy;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19181
diff changeset
    68
    val modl_old = CodegenThingol.project_module (Symtab.keys nbe_tab)
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19147
diff changeset
    69
      (CodegenPackage.get_root_module thy);
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    70
    val (t', thy') = CodegenPackage.codegen_term t thy;
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    71
    val modl_new = CodegenPackage.get_root_module thy';
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
    72
    val diff = CodegenThingol.diff_module (modl_new, modl_old);
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    73
    val _ = nbe_trace (fn() => "new definitions: " ^ (commas o map fst) diff);
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19147
diff changeset
    74
    val _ = (tab := nbe_tab;
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    75
             Library.seq (use_show o NBE_Codegen.generate defined) diff);
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    76
    val thy'' = NBE_Data.put (!tab) thy';
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    77
    val nt' = NBE_Eval.nbe (!tab) t';
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    78
    val _ = nbe_trace (fn() => "Input:\n" ^ Display.raw_string_of_term t)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    79
    val _ =  nbe_trace (fn()=> "Normalized:\n" ^ NBE_Eval.string_of_nterm nt');
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    80
    val t' = NBE_Codegen.nterm_to_term thy'' nt';
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    81
    val _ = nbe_trace (fn()=>"Converted back:\n" ^ Display.raw_string_of_term t');
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    82
    val t'' = anno_vars (var_tab t) t';
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    83
    val _ = nbe_trace (fn()=>"Vars typed:\n" ^ Display.raw_string_of_term t'');
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    84
    val ty = type_of t;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    85
    val (t''', _) =
19181
5c9f58562602 minor changes
nipkow
parents: 19177
diff changeset
    86
      Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy) (K NONE) (K NONE)
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    87
        [] false ([t''], ty);
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    88
    val s = Pretty.string_of
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    89
      (Pretty.block [Pretty.quote (Sign.pretty_term thy'' t'''), Pretty.fbrk,
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    90
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy'' ty)])
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    91
    val _ = writeln s
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    92
  in  (t''', thy'')  end;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    93
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    94
fun norm_by_eval raw_t thy = norm_by_eval_i (Sign.read_term thy raw_t) thy;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    95
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    96
structure P = OuterParse and K = OuterKeyword;
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    97
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    98
val nbeP =
19830
b81d803dfaa4 renamed command
nipkow
parents: 19795
diff changeset
    99
  OuterSyntax.command "normal_form" "normalization by evaluation" K.thy_decl
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   100
    (P.term >> (fn t => Toplevel.theory (snd o norm_by_eval t)));
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   101
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   102
val _ = OuterSyntax.add_parsers [nbeP];
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   103
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   104
end;