src/Tools/nbe.ML
author haftmann
Tue, 22 Apr 2008 22:00:25 +0200
changeset 26739 947b6013e863
parent 26064 65585de05a66
child 26747 f32fa5f5bdd1
permissions -rw-r--r--
different handling of eq class for nbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24590
733120d04233 delayed evaluation
haftmann
parents: 24508
diff changeset
     1
(*  Title:      Tools/nbe.ML
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     3
    Authors:    Klaus Aehlig, LMU Muenchen; Tobias Nipkow, Florian Haftmann, TU Muenchen
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     4
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
     5
Normalization by evaluation, based on generic code generator.
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     6
*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     7
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     8
signature NBE =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
     9
sig
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
    10
  val norm_conv: cterm -> thm
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
    11
  val norm_term: theory -> term -> term
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
    12
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25190
diff changeset
    13
  datatype Univ =
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
    14
      Const of int * Univ list               (*named (uninterpreted) constants*)
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    15
    | Free of string * Univ list             (*free (uninterpreted) variables*)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    16
    | DFree of string * int                  (*free (uninterpreted) dictionary parameters*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    17
    | BVar of int * Univ list
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    18
    | Abs of (int * (Univ list -> Univ)) * Univ list;
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    19
  val apps: Univ -> Univ list -> Univ        (*explicit applications*)
25944
haftmann
parents: 25935
diff changeset
    20
  val abss: int -> (Univ list -> Univ) -> Univ
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    21
                                            (*abstractions as closures*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    22
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25190
diff changeset
    23
  val univs_ref: (unit -> Univ list -> Univ list) option ref
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    24
  val trace: bool ref
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    25
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
    26
  val setup: class list -> (string * string) list -> theory -> theory
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    27
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    28
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    29
structure Nbe: NBE =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    30
struct
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    31
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    32
(* generic non-sense *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    33
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    34
val trace = ref false;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    35
fun tracing f x = if !trace then (Output.tracing (f x); x) else x;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    36
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    37
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    38
(** the semantical universe **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    39
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    40
(*
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    41
   Functions are given by their semantical function value. To avoid
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    42
   trouble with the ML-type system, these functions have the most
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    43
   generic type, that is "Univ list -> Univ". The calling convention is
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    44
   that the arguments come as a list, the last argument first. In
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    45
   other words, a function call that usually would look like
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    46
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    47
   f x_1 x_2 ... x_n   or   f(x_1,x_2, ..., x_n)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    48
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    49
   would be in our convention called as
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    50
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    51
              f [x_n,..,x_2,x_1]
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    52
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    53
   Moreover, to handle functions that are still waiting for some
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    54
   arguments we have additionally a list of arguments collected to far
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    55
   and the number of arguments we're still waiting for.
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    56
*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    57
25204
36cf92f63a44 replaced Secure.evaluate by ML_Context.evaluate;
wenzelm
parents: 25190
diff changeset
    58
datatype Univ =
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
    59
    Const of int * Univ list           (*named (uninterpreted) constants*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    60
  | Free of string * Univ list         (*free variables*)
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    61
  | DFree of string * int              (*free (uninterpreted) dictionary parameters*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    62
  | BVar of int * Univ list            (*bound named variables*)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    63
  | Abs of (int * (Univ list -> Univ)) * Univ list
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24347
diff changeset
    64
                                      (*abstractions as closures*);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    65
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    66
(* constructor functions *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    67
25944
haftmann
parents: 25935
diff changeset
    68
fun abss n f = Abs ((n, f), []);
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    69
fun apps (Abs ((n, f), xs)) ys = let val k = n - length ys in
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    70
      if k = 0 then f (ys @ xs)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    71
      else if k < 0 then
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    72
        let val (zs, ws) = chop (~ k) ys
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    73
        in apps (f (ws @ xs)) zs end
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    74
      else Abs ((k, f), ys @ xs) end (*note: reverse convention also for apps!*)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    75
  | apps (Const (name, xs)) ys = Const (name, ys @ xs)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    76
  | apps (Free (name, xs)) ys = Free (name, ys @ xs)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
    77
  | apps (BVar (name, xs)) ys = BVar (name, ys @ xs);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    78
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    79
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    80
(** assembling and compiling ML code from terms **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    81
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    82
(* abstract ML syntax *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    83
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    84
infix 9 `$` `$$`;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    85
fun e1 `$` e2 = "(" ^ e1 ^ " " ^ e2 ^ ")";
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
    86
fun e `$$` [] = e
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
    87
  | e `$$` es = "(" ^ e ^ " " ^ space_implode " " es ^ ")";
24590
733120d04233 delayed evaluation
haftmann
parents: 24508
diff changeset
    88
fun ml_abs v e = "(fn " ^ v ^ " => " ^ e ^ ")";
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    89
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    90
fun ml_cases t cs =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    91
  "(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
25944
haftmann
parents: 25935
diff changeset
    92
fun ml_Let d e = "let\n" ^ d ^ " in " ^ e ^ " end";
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    93
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    94
fun ml_list es = "[" ^ commas es ^ "]";
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    95
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    96
fun ml_fundefs ([(name, [([], e)])]) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    97
      "val " ^ name ^ " = " ^ e ^ "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    98
  | ml_fundefs (eqs :: eqss) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
    99
      let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   100
        fun fundef (name, eqs) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   101
          let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   102
            fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   103
          in space_implode "\n  | " (map eqn eqs) end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   104
      in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   105
        (prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   106
        |> space_implode "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   107
        |> suffix "\n"
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   108
      end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   109
25944
haftmann
parents: 25935
diff changeset
   110
(* nbe specific syntax and sandbox communication *)
haftmann
parents: 25935
diff changeset
   111
haftmann
parents: 25935
diff changeset
   112
val univs_ref = ref (NONE : (unit -> Univ list -> Univ list) option);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   113
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   114
local
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   115
  val prefix =          "Nbe.";
25944
haftmann
parents: 25935
diff changeset
   116
  val name_ref =        prefix ^ "univs_ref";
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   117
  val name_const =      prefix ^ "Const";
25944
haftmann
parents: 25935
diff changeset
   118
  val name_abss =       prefix ^ "abss";
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   119
  val name_apps =       prefix ^ "apps";
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   120
in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   121
25944
haftmann
parents: 25935
diff changeset
   122
val univs_cookie = (name_ref, univs_ref);
haftmann
parents: 25935
diff changeset
   123
25935
haftmann
parents: 25924
diff changeset
   124
fun nbe_fun "" = "nbe_value"
haftmann
parents: 25924
diff changeset
   125
  | nbe_fun c = "c_" ^ translate_string (fn "." => "_" | c => c) c;
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   126
fun nbe_dict v n = "d_" ^ v ^ "_" ^ string_of_int n;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   127
fun nbe_bound v = "v_" ^ v;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   128
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   129
(*note: these three are the "turning spots" where proper argument order is established!*)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   130
fun nbe_apps t [] = t
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   131
  | nbe_apps t ts = name_apps `$$` [t, ml_list (rev ts)];
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   132
fun nbe_apps_local c ts = nbe_fun c `$` ml_list (rev ts);
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   133
fun nbe_apps_constr idx ts =
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   134
  name_const `$` ("(" ^ string_of_int idx ^ ", " ^ ml_list (rev ts) ^ ")");
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   135
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   136
fun nbe_abss 0 f = f `$` ml_list []
25944
haftmann
parents: 25935
diff changeset
   137
  | nbe_abss n f = name_abss `$$` [string_of_int n, f];
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   138
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   139
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   140
24219
e558fe311376 new structure for code generator modules
haftmann
parents: 24166
diff changeset
   141
open BasicCodeThingol;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   142
25865
a141d6bfd398 tuned comment
haftmann
parents: 25204
diff changeset
   143
(* code generation *)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   144
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   145
fun assemble_eqnss idx_of deps eqnss =
25944
haftmann
parents: 25935
diff changeset
   146
  let
haftmann
parents: 25935
diff changeset
   147
    fun prep_eqns (c, (vs, eqns)) =
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   148
      let
25944
haftmann
parents: 25935
diff changeset
   149
        val dicts = maps (fn (v, sort) => map_index (nbe_dict v o fst) sort) vs;
haftmann
parents: 25935
diff changeset
   150
        val num_args = length dicts + (length o fst o hd) eqns;
haftmann
parents: 25935
diff changeset
   151
      in (c, (num_args, (dicts, eqns))) end;
haftmann
parents: 25935
diff changeset
   152
    val eqnss' = map prep_eqns eqnss;
haftmann
parents: 25935
diff changeset
   153
haftmann
parents: 25935
diff changeset
   154
    fun assemble_constapp c dss ts = 
haftmann
parents: 25935
diff changeset
   155
      let
haftmann
parents: 25935
diff changeset
   156
        val ts' = (maps o map) assemble_idict dss @ ts;
haftmann
parents: 25935
diff changeset
   157
      in case AList.lookup (op =) eqnss' c
haftmann
parents: 25935
diff changeset
   158
       of SOME (n, _) => if n <= length ts'
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   159
            then let val (ts1, ts2) = chop n ts'
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   160
            in nbe_apps (nbe_apps_local c ts1) ts2
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   161
            end else nbe_apps (nbe_abss n (nbe_fun c)) ts'
25944
haftmann
parents: 25935
diff changeset
   162
        | NONE => if member (op =) deps c
haftmann
parents: 25935
diff changeset
   163
            then nbe_apps (nbe_fun c) ts'
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   164
            else nbe_apps_constr (idx_of c) ts'
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   165
      end
25944
haftmann
parents: 25935
diff changeset
   166
    and assemble_idict (DictConst (inst, dss)) =
haftmann
parents: 25935
diff changeset
   167
          assemble_constapp inst dss []
haftmann
parents: 25935
diff changeset
   168
      | assemble_idict (DictVar (supers, (v, (n, _)))) =
haftmann
parents: 25935
diff changeset
   169
          fold_rev (fn super => assemble_constapp super [] o single) supers (nbe_dict v n);
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   170
25944
haftmann
parents: 25935
diff changeset
   171
    fun assemble_iterm constapp =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   172
      let
25944
haftmann
parents: 25935
diff changeset
   173
        fun of_iterm t =
haftmann
parents: 25935
diff changeset
   174
          let
haftmann
parents: 25935
diff changeset
   175
            val (t', ts) = CodeThingol.unfold_app t
haftmann
parents: 25935
diff changeset
   176
          in of_iapp t' (fold_rev (cons o of_iterm) ts []) end
haftmann
parents: 25935
diff changeset
   177
        and of_iapp (IConst (c, (dss, _))) ts = constapp c dss ts
haftmann
parents: 25935
diff changeset
   178
          | of_iapp (IVar v) ts = nbe_apps (nbe_bound v) ts
haftmann
parents: 25935
diff changeset
   179
          | of_iapp ((v, _) `|-> t) ts =
haftmann
parents: 25935
diff changeset
   180
              nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound v]) (of_iterm t))) ts
haftmann
parents: 25935
diff changeset
   181
          | of_iapp (ICase (((t, _), cs), t0)) ts =
haftmann
parents: 25935
diff changeset
   182
              nbe_apps (ml_cases (of_iterm t) (map (pairself of_iterm) cs
haftmann
parents: 25935
diff changeset
   183
                @ [("_", of_iterm t0)])) ts
haftmann
parents: 25935
diff changeset
   184
      in of_iterm end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   185
25944
haftmann
parents: 25935
diff changeset
   186
    fun assemble_eqns (c, (num_args, (dicts, eqns))) =
haftmann
parents: 25935
diff changeset
   187
      let
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   188
        val assemble_arg = assemble_iterm
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   189
          (fn c => fn _ => fn ts => nbe_apps_constr (idx_of c) ts);
25944
haftmann
parents: 25935
diff changeset
   190
        val assemble_rhs = assemble_iterm assemble_constapp;
haftmann
parents: 25935
diff changeset
   191
        fun assemble_eqn (args, rhs) =
haftmann
parents: 25935
diff changeset
   192
          ([ml_list (rev (dicts @ map assemble_arg args))], assemble_rhs rhs);
haftmann
parents: 25935
diff changeset
   193
        val default_args = map nbe_bound (Name.invent_list [] "a" num_args);
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   194
        val default_eqn = if c = "" then NONE
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   195
          else SOME ([ml_list (rev default_args)],
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   196
            nbe_apps_constr (idx_of c) default_args);
25944
haftmann
parents: 25935
diff changeset
   197
      in
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   198
        ((nbe_fun c, map assemble_eqn eqns @ the_list default_eqn),
25944
haftmann
parents: 25935
diff changeset
   199
          nbe_abss num_args (nbe_fun c))
haftmann
parents: 25935
diff changeset
   200
      end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   201
25944
haftmann
parents: 25935
diff changeset
   202
    val (fun_vars, fun_vals) = map_split assemble_eqns eqnss';
haftmann
parents: 25935
diff changeset
   203
    val deps_vars = ml_list (map nbe_fun deps);
haftmann
parents: 25935
diff changeset
   204
  in ml_abs deps_vars (ml_Let (ml_fundefs fun_vars) (ml_list fun_vals)) end;
haftmann
parents: 25935
diff changeset
   205
haftmann
parents: 25935
diff changeset
   206
(* code compilation *)
haftmann
parents: 25935
diff changeset
   207
haftmann
parents: 25935
diff changeset
   208
fun compile_eqnss gr raw_deps [] = []
haftmann
parents: 25935
diff changeset
   209
  | compile_eqnss gr raw_deps eqnss = 
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   210
      let
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   211
        val (deps, deps_vals) = split_list (map_filter
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   212
          (fn dep => Option.map (fn univ => (dep, univ)) (fst ((Graph.get_node gr dep)))) raw_deps);
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   213
        val idx_of = raw_deps
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   214
          |> map (fn dep => (dep, snd (Graph.get_node gr dep)))
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   215
          |> AList.lookup (op =)
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   216
          |> (fn f => the o f);
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   217
        val s = assemble_eqnss idx_of deps eqnss;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   218
        val cs = map fst eqnss;
25944
haftmann
parents: 25935
diff changeset
   219
      in
haftmann
parents: 25935
diff changeset
   220
        s
haftmann
parents: 25935
diff changeset
   221
        |> tracing (fn s => "\n--- code to be evaluated:\n" ^ s)
haftmann
parents: 25935
diff changeset
   222
        |> ML_Context.evaluate
haftmann
parents: 25935
diff changeset
   223
            (Output.tracing o enclose "\n---compiler echo:\n" "\n---\n",
haftmann
parents: 25935
diff changeset
   224
            Output.tracing o enclose "\n--- compiler echo (with error):\n" "\n---\n")
haftmann
parents: 25935
diff changeset
   225
            (!trace) univs_cookie
haftmann
parents: 25935
diff changeset
   226
        |> (fn f => f deps_vals)
haftmann
parents: 25935
diff changeset
   227
        |> (fn univs => cs ~~ univs)
haftmann
parents: 25935
diff changeset
   228
      end;
25190
5cd8486c5a4f clarified implementation
haftmann
parents: 25167
diff changeset
   229
25944
haftmann
parents: 25935
diff changeset
   230
(* preparing function equations *)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   231
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   232
fun eqns_of_stmt (_, CodeThingol.Fun (_, [])) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   233
      []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   234
  | eqns_of_stmt (const, CodeThingol.Fun ((vs, _), eqns)) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   235
      [(const, (vs, map fst eqns))]
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   236
  | eqns_of_stmt (_, CodeThingol.Datatypecons _) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   237
      []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   238
  | eqns_of_stmt (_, CodeThingol.Datatype _) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   239
      []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   240
  | eqns_of_stmt (class, CodeThingol.Class (v, (superclasses, classops))) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   241
      let
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   242
        val names = map snd superclasses @ map fst classops;
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   243
        val params = Name.invent_list [] "d" (length names);
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   244
        fun mk (k, name) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   245
          (name, ([(v, [])],
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   246
            [([IConst (class, ([], [])) `$$ map IVar params], IVar (nth params k))]));
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   247
      in map_index mk names end
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   248
  | eqns_of_stmt (_, CodeThingol.Classrel _) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   249
      []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   250
  | eqns_of_stmt (_, CodeThingol.Classparam _) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   251
      []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   252
  | eqns_of_stmt (inst, CodeThingol.Classinst ((class, (_, arities)), (superinsts, instops))) =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   253
      [(inst, (arities, [([], IConst (class, ([], [])) `$$
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   254
        map (fn (_, (_, (inst, dicts))) => IConst (inst, (dicts, []))) superinsts
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   255
        @ map (IConst o snd o fst) instops)]))];
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   256
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   257
fun compile_stmts stmts_deps =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   258
  let
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   259
    val names = map (fst o fst) stmts_deps;
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   260
    val names_deps = map (fn ((name, _), deps) => (name, deps)) stmts_deps;
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   261
    val eqnss = maps (eqns_of_stmt o fst) stmts_deps;
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   262
    val refl_deps = names_deps
25190
5cd8486c5a4f clarified implementation
haftmann
parents: 25167
diff changeset
   263
      |> maps snd
5cd8486c5a4f clarified implementation
haftmann
parents: 25167
diff changeset
   264
      |> distinct (op =)
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   265
      |> fold (insert (op =)) names;
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   266
    fun new_node name (gr, (maxidx, idx_tab)) = if can (Graph.get_node gr) name
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   267
      then (gr, (maxidx, idx_tab))
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   268
      else (Graph.new_node (name, (NONE, maxidx)) gr,
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   269
        (maxidx + 1, Inttab.update_new (maxidx, name) idx_tab));
25190
5cd8486c5a4f clarified implementation
haftmann
parents: 25167
diff changeset
   270
    fun compile gr = eqnss
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   271
      |> compile_eqnss gr refl_deps
25190
5cd8486c5a4f clarified implementation
haftmann
parents: 25167
diff changeset
   272
      |> rpair gr;
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   273
  in
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   274
    fold new_node refl_deps
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   275
    #> apfst (fold (fn (name, deps) => fold (curry Graph.add_edge name) deps) names_deps
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   276
      #> compile
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   277
      #-> fold (fn (name, univ) => (Graph.map_node name o apfst) (K (SOME univ))))
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   278
  end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   279
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   280
fun ensure_stmts code =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   281
  let
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   282
    fun add_stmts names (gr, (maxidx, idx_tab)) = if exists ((can o Graph.get_node) gr) names
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   283
      then (gr, (maxidx, idx_tab))
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   284
      else (gr, (maxidx, idx_tab))
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   285
        |> compile_stmts (map (fn name => ((name, Graph.get_node code name),
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   286
          Graph.imm_succs code name)) names);
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   287
  in fold_rev add_stmts (Graph.strong_conn code) end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   288
25944
haftmann
parents: 25935
diff changeset
   289
haftmann
parents: 25935
diff changeset
   290
(** evaluation **)
haftmann
parents: 25935
diff changeset
   291
haftmann
parents: 25935
diff changeset
   292
(* term evaluation *)
haftmann
parents: 25935
diff changeset
   293
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   294
fun eval_term gr deps ((vs, ty), t) =
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   295
  let 
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   296
    val frees = CodeThingol.fold_unbound_varnames (insert (op =)) t []
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   297
    val frees' = map (fn v => Free (v, [])) frees;
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   298
    val dict_frees = maps (fn (v, sort) => map_index (curry DFree v o fst) sort) vs;
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   299
  in
25935
haftmann
parents: 25924
diff changeset
   300
    ("", (vs, [(map IVar frees, t)]))
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   301
    |> singleton (compile_eqnss gr deps)
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   302
    |> snd
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   303
    |> (fn t => apps t (rev (dict_frees @ frees')))
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   304
  end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   305
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   306
(* reification *)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   307
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   308
fun term_of_univ thy idx_tab t =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   309
  let
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   310
    fun take_until f [] = []
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   311
      | take_until f (x::xs) = if f x then [] else x :: take_until f xs;
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   312
    fun is_dict (Const (idx, _)) =
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   313
          let
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   314
            val c = the (Inttab.lookup idx_tab idx);
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   315
          in
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   316
            (is_some o CodeName.class_rev thy) c
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   317
            orelse (is_some o CodeName.classrel_rev thy) c
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   318
            orelse (is_some o CodeName.instance_rev thy) c
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   319
          end
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   320
      | is_dict (DFree _) = true
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   321
      | is_dict _ = false;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   322
    fun of_apps bounds (t, ts) =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   323
      fold_map (of_univ bounds) ts
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   324
      #>> (fn ts' => list_comb (t, rev ts'))
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   325
    and of_univ bounds (Const (idx, ts)) typidx =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   326
          let
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   327
            val ts' = take_until is_dict ts;
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   328
            val c = (the o CodeName.const_rev thy o the o Inttab.lookup idx_tab) idx;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
   329
            val T = Code.default_typ thy c;
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   330
            val T' = map_type_tvar (fn ((v, i), S) => TypeInfer.param (typidx + i) (v, [])) T;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   331
            val typidx' = typidx + maxidx_of_typ T' + 1;
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   332
          in of_apps bounds (Term.Const (c, T'), ts') typidx' end
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   333
      | of_univ bounds (Free (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   334
          of_apps bounds (Term.Free (name, dummyT), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   335
      | of_univ bounds (BVar (name, ts)) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   336
          of_apps bounds (Bound (bounds - name - 1), ts) typidx
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   337
      | of_univ bounds (t as Abs _) typidx =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   338
          typidx
25924
f974a1c64348 improved implementation
haftmann
parents: 25865
diff changeset
   339
          |> of_univ (bounds + 1) (apps t [BVar (bounds, [])])
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   340
          |-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   341
  in of_univ 0 t 0 |> fst end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   342
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   343
(* function store *)
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   344
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   345
structure Nbe_Functions = CodeDataFun
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   346
(
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   347
  type T = (Univ option * int) Graph.T * (int * string Inttab.table);
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   348
  val empty = (Graph.empty, (0, Inttab.empty));
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   349
  fun merge _ ((gr1, (maxidx1, idx_tab1)), (gr2, (maxidx2, idx_tab2))) =
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   350
    (Graph.merge (K true) (gr1, gr2), (IntInf.max (maxidx1, maxidx2),
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   351
      Inttab.merge (K true) (idx_tab1, idx_tab2)));
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   352
  fun purge _ NONE _ = empty
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   353
    | purge NONE _ _ = empty
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   354
    | purge (SOME thy) (SOME cs) (gr, (maxidx, idx_tab)) =
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   355
        let
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   356
          val cs_exisiting =
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   357
            map_filter (CodeName.const_rev thy) (Graph.keys gr);
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   358
          val dels = (Graph.all_preds gr
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   359
              o map (CodeName.const thy)
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   360
              o filter (member (op =) cs_exisiting)
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   361
            ) cs;
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   362
        in (Graph.del_nodes dels gr, (maxidx, idx_tab)) end;
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   363
);
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   364
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   365
(* compilation, evaluation and reification *)
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   366
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   367
fun compile_eval thy code vs_ty_t deps =
26064
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   368
  let
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   369
    val (gr, (_, idx_tab)) = Nbe_Functions.change thy (ensure_stmts code);
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   370
  in
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   371
    vs_ty_t
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   372
    |> eval_term gr deps
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   373
    |> term_of_univ thy idx_tab
65585de05a66 using integers for pattern matching
haftmann
parents: 26011
diff changeset
   374
  end;
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   375
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   376
(* trivial type classes *)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   377
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   378
structure Nbe_Triv_Classes = TheoryDataFun
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   379
(
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   380
  type T = class list * (string * string) list;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   381
  val empty = ([], []);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   382
  val copy = I;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   383
  val extend = I;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   384
  fun merge _ ((classes1, consts1), (classes2, consts2)) =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   385
    (Library.merge (op =) (classes1, classes2), Library.merge (op =) (consts1, consts2));
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   386
)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   387
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   388
fun add_triv_classes thy =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   389
  let
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   390
    val (trivs, _) = Nbe_Triv_Classes.get thy;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   391
    val inters = curry (Sorts.inter_sort (Sign.classes_of thy)) trivs;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   392
    fun map_sorts f = (map_types o map_atyps)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   393
      (fn TVar (v, sort) => TVar (v, f sort)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   394
        | TFree (v, sort) => TFree (v, f sort));
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   395
  in map_sorts inters end;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   396
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   397
fun subst_triv_consts thy =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   398
  let
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   399
    fun subst_const f = map_aterms (fn t as Term.Const (c, ty) => (case f c
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   400
         of SOME c' => Term.Const (c', ty)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   401
          | NONE => t)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   402
      | t => t);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   403
    val (_, consts) = Nbe_Triv_Classes.get thy;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   404
    val subst_inst = perhaps (Option.map fst o AxClass.inst_of_param thy);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   405
  in map_aterms (subst_const (AList.lookup (op =) consts o subst_inst)) end;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   406
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   407
(* evaluation with type reconstruction *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   408
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   409
fun eval thy t code vs_ty_t deps =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   410
  let
24347
245ff8661b8c explizit dependencies
haftmann
parents: 24292
diff changeset
   411
    val ty = type_of t;
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   412
    val type_free = AList.lookup (op =)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   413
      (map (fn (s, T) => (s, Term.Free (s, T))) (Term.add_frees t []));
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   414
    val type_frees = Term.map_aterms
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   415
      (fn (t as Term.Free (s, _)) => the_default t (type_free s) | t => t);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   416
    fun type_infer t = [(t, ty)]
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   417
      |> TypeInfer.infer_types (Sign.pp thy) (Sign.tsig_of thy) I
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   418
           (try (Type.strip_sorts o Sign.the_const_type thy)) (K NONE)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   419
           Name.context 0 NONE
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   420
      |> fst |> the_single |> fst;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   421
    fun check_tvars t = if null (Term.term_tvars t) then t else
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   422
      error ("Illegal schematic type variables in normalized term: "
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   423
        ^ setmp show_types true (Sign.string_of_term thy) t);
25167
0fd59d8e2bad fixed typo
haftmann
parents: 25101
diff changeset
   424
    val string_of_term = setmp show_types true (Sign.string_of_term thy);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   425
  in
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   426
    compile_eval thy code vs_ty_t deps
25167
0fd59d8e2bad fixed typo
haftmann
parents: 25101
diff changeset
   427
    |> tracing (fn t => "Normalized:\n" ^ string_of_term t)
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   428
    |> subst_triv_consts thy
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   429
    |> type_frees
25167
0fd59d8e2bad fixed typo
haftmann
parents: 25101
diff changeset
   430
    |> tracing (fn t => "Vars typed:\n" ^ string_of_term t)
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   431
    |> type_infer
25167
0fd59d8e2bad fixed typo
haftmann
parents: 25101
diff changeset
   432
    |> tracing (fn t => "Types inferred:\n" ^ string_of_term t)
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   433
    |> check_tvars
25101
cae0f68b693b now employing dictionaries
haftmann
parents: 25098
diff changeset
   434
    |> tracing (fn t => "---\n")
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   435
  end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   436
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   437
(* evaluation oracle *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   438
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   439
exception Norm of term * CodeThingol.code
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24347
diff changeset
   440
  * (CodeThingol.typscheme * CodeThingol.iterm) * string list;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   441
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   442
fun norm_oracle (thy, Norm (t, code, vs_ty_t, deps)) =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   443
  Logic.mk_equals (t, eval thy t code vs_ty_t deps);
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   444
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   445
fun norm_invoke thy t code vs_ty_t deps =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   446
  Thm.invoke_oracle_i thy "HOL.norm" (thy, Norm (t, code, vs_ty_t, deps));
24283
haftmann
parents: 24219
diff changeset
   447
  (*FIXME get rid of hardwired theory name*)
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   448
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   449
fun norm_conv ct =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   450
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   451
    val thy = Thm.theory_of_cterm ct;
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   452
    fun evaluator' t code vs_ty_t deps = norm_invoke thy t code vs_ty_t deps;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   453
    fun evaluator t = (add_triv_classes thy t, evaluator' t);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   454
  in CodePackage.evaluate_conv thy evaluator ct end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   455
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   456
fun norm_term thy t =
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   457
  let
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   458
    fun evaluator' t code vs_ty_t deps = eval thy t code vs_ty_t deps;
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   459
    fun evaluator t = (add_triv_classes thy t, evaluator' t);
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   460
  in (Code.postprocess_term thy o CodePackage.evaluate_term thy evaluator) t end;
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   461
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   462
(* evaluation command *)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   463
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   464
fun norm_print_term ctxt modes t =
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   465
  let
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   466
    val thy = ProofContext.theory_of ctxt;
24839
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   467
    val t' = norm_term thy t;
199c48ec5a09 step towards proper purge operation
haftmann
parents: 24713
diff changeset
   468
    val ty' = Term.type_of t';
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24612
diff changeset
   469
    val p = PrintMode.with_modes modes (fn () =>
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24867
diff changeset
   470
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'), Pretty.fbrk,
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24867
diff changeset
   471
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt ty')]) ();
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   472
  in Pretty.writeln p end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   473
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   474
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   475
(** Isar setup **)
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   476
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24493
diff changeset
   477
fun norm_print_term_cmd (modes, s) state =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   478
  let val ctxt = Toplevel.context_of state
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24493
diff changeset
   479
  in norm_print_term ctxt modes (Syntax.read_term ctxt s) end;
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   480
26739
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   481
fun setup nbe_classes nbe_consts =
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   482
  Theory.add_oracle ("norm", norm_oracle)
947b6013e863 different handling of eq class for nbe
haftmann
parents: 26064
diff changeset
   483
  #> Nbe_Triv_Classes.map (K (nbe_classes, nbe_consts));
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   484
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   485
local structure P = OuterParse and K = OuterKeyword in
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   486
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   487
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   488
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24839
diff changeset
   489
val _ =
24155
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   490
  OuterSyntax.improper_command "normal_form" "normalize term by evaluation" K.diag
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   491
    (opt_modes -- P.typ >> (Toplevel.keep o norm_print_term_cmd));
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   492
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   493
end;
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   494
d86867645f4f nbe improved
haftmann
parents:
diff changeset
   495
end;