src/Pure/Tools/nbe.ML
author haftmann
Fri, 30 Jun 2006 12:04:17 +0200
changeset 19968 2180f0f443af
parent 19962 016ba2d907a7
child 20155 da0505518e69
permissions -rw-r--r--
fixed stale theory bug
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
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    10
  val norm_term: theory -> term -> term
016ba2d907a7 new function norm_term
nipkow
parents: 19830
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
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    19
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    20
(* theory data setup *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    21
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    22
structure NBE_Data = TheoryDataFun
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    23
(struct
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    24
  val name = "Pure/NBE"
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    25
  type T = NBE_Eval.Univ Symtab.table
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    26
  val empty = Symtab.empty
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    27
  val copy = I
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    28
  val extend = I
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    29
  fun merge _ = Symtab.merge (K true)
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
    30
  fun print _ _ = ()
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    31
end);
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    32
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    33
val _ = Context.add_setup NBE_Data.init;
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    34
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    35
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    36
(* sandbox communication *)
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    37
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    38
val tab : NBE_Eval.Univ Symtab.table ref = ref Symtab.empty;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    39
fun lookup s = (the o Symtab.lookup (!tab)) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    40
fun update sx = (tab := Symtab.update sx (!tab));
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    41
fun defined s = Symtab.defined (!tab) s;
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
    42
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
    43
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    44
(* FIXME replace by Term.map_aterms *)
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    45
fun subst_Frees [] tm = tm
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    46
  | subst_Frees inst tm =
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    47
      let
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    48
        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
    49
          | subst (Abs (a, T, t)) = Abs (a, T, subst t)
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    50
          | subst (t $ u) = subst t $ subst u
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    51
          | subst t = t;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    52
      in subst tm end;
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    53
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    54
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
    55
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    56
fun anno_vars (Ftab, Vtab) =
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    57
  subst_Vars  (map (fn (ixn, T) => (ixn, Var(ixn,T))) Vtab) o
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    58
  subst_Frees (map (fn (s, T) =>   (s,   Free(s,T)))  Ftab)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    59
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    60
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    61
(* debugging *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    62
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    63
val trace_nbe = ref false;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    64
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    65
fun trace f = if !trace_nbe then tracing (f ()) else ();
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    66
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    67
(* FIXME better turn this into a function
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    68
    NBE_Eval.Univ Symtab.table -> NBE_Eval.Univ Symtab.table
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    69
    with implicit side effect *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    70
fun use_code "" = ()
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    71
  | use_code s =
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    72
      (if !trace_nbe then tracing ("\n---generated code:\n"^ s) else ();
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    73
       use_text(tracing o enclose "\n---compiler echo:\n" "\n---\n",
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    74
            tracing o enclose "\n--- compiler echo (with error!):\n" "\n---\n")
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    75
        (!trace_nbe) s);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    76
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    77
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    78
(* norm by eval *)
19795
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    79
746274ca400b added type inference at the end of normalization
nipkow
parents: 19341
diff changeset
    80
(* FIXME try to use isar_cmd/print_term to take care of context *)
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    81
fun norm_print t thy =
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19147
diff changeset
    82
  let
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    83
    val _ = trace (fn () => "Input:\n" ^ Display.raw_string_of_term t);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    84
    fun compile_term t thy =
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    85
      let
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    86
        val (modl_this, thy') = CodegenPackage.get_root_module thy;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    87
        val nbe_tab = NBE_Data.get thy';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    88
        val modl_old = CodegenThingol.project_module (Symtab.keys nbe_tab) modl_this;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    89
        val (t', thy'') = CodegenPackage.codegen_term t thy';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    90
        val (modl_new, thy''') = CodegenPackage.get_root_module thy'';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    91
        val diff = CodegenThingol.diff_module (modl_new, modl_old);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    92
        val _ = trace (fn () => "new definitions: " ^ (commas o map fst) diff);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    93
        val _ = (tab := nbe_tab;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    94
             Library.seq (use_code o NBE_Codegen.generate defined) diff);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    95
        val thy'''' = NBE_Data.put (!tab) thy''';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    96
        val nt' = NBE_Eval.nbe (!tab) t';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    97
      in (nt', thy'''') end;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    98
    fun eval_term t nt thy =
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
    99
      let
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   100
        val vtab = var_tab t;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   101
        val ty = type_of t;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   102
        fun restrain ty t = Sign.infer_types (Sign.pp thy) thy (Sign.consts_of thy)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   103
            (K NONE) (K NONE)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   104
            [] false ([t], ty) |> fst;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   105
        val _ = trace (fn () => "Normalized:\n" ^ NBE_Eval.string_of_nterm nt);
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   106
        val t' = NBE_Codegen.nterm_to_term thy nt;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   107
        val _ = trace (fn () =>"Converted back:\n" ^ Display.raw_string_of_term t');
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   108
        val t'' = anno_vars vtab t';
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   109
        val _ = trace (fn () =>"Vars typed:\n" ^ Display.raw_string_of_term t'');
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   110
        val t''' = restrain ty t''
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   111
        val s = Pretty.string_of
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   112
            (Pretty.block [Pretty.quote (Sign.pretty_term thy t'''), Pretty.fbrk,
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   113
                Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ thy ty)])
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   114
        val _ = writeln s
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   115
      in (t''', thy) end;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   116
  in
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   117
    thy
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   118
    |> compile_term t
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   119
    |-> (fn nt => eval_term t nt)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   120
  end;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   121
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   122
fun norm_print' s thy = norm_print (Sign.read_term thy s) thy;
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   123
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   124
fun norm_term thy t = fst (norm_print t (Theory.copy thy));
19962
016ba2d907a7 new function norm_term
nipkow
parents: 19830
diff changeset
   125
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   126
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   127
(* Isar setup *)
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   128
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   129
local structure P = OuterParse and K = OuterKeyword in
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   130
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   131
val nbeP =
19830
b81d803dfaa4 renamed command
nipkow
parents: 19795
diff changeset
   132
  OuterSyntax.command "normal_form" "normalization by evaluation" K.thy_decl
19968
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   133
    (P.term >> (fn s => Toplevel.theory (snd o norm_print' s)));
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   134
2180f0f443af fixed stale theory bug
haftmann
parents: 19962
diff changeset
   135
end;
19147
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   136
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   137
val _ = OuterSyntax.add_parsers [nbeP];
0f584853b6a4 added nbe, updated neb_*
nipkow
parents:
diff changeset
   138
19177
68c6824d8bb6 improvements for nbe
haftmann
parents: 19167
diff changeset
   139
end;