src/Tools/nbe.ML
author haftmann
Wed Feb 26 11:57:52 2014 +0100 (2014-02-26)
changeset 55757 9fc71814b8c1
parent 55167 f3ac344284ff
child 56245 84fc7dfa3cd4
permissions -rw-r--r--
prefer proof context over background theory
     1 (*  Title:      Tools/nbe.ML
     2     Authors:    Klaus Aehlig, LMU Muenchen; Tobias Nipkow, Florian Haftmann, TU Muenchen
     3 
     4 Normalization by evaluation, based on generic code generator.
     5 *)
     6 
     7 signature NBE =
     8 sig
     9   val dynamic_conv: Proof.context -> conv
    10   val dynamic_value: Proof.context -> term -> term
    11   val static_conv: Proof.context -> string list -> Proof.context -> conv
    12   val static_value: Proof.context -> string list -> Proof.context -> term -> term
    13 
    14   datatype Univ =
    15       Const of int * Univ list               (*named (uninterpreted) constants*)
    16     | DFree of string * int                  (*free (uninterpreted) dictionary parameters*)
    17     | BVar of int * Univ list
    18     | Abs of (int * (Univ list -> Univ)) * Univ list
    19   val apps: Univ -> Univ list -> Univ        (*explicit applications*)
    20   val abss: int -> (Univ list -> Univ) -> Univ
    21                                              (*abstractions as closures*)
    22   val same: Univ * Univ -> bool
    23 
    24   val put_result: (unit -> Univ list -> Univ list) -> Proof.context -> Proof.context
    25   val trace: bool Unsynchronized.ref
    26 
    27   val setup: theory -> theory
    28   val add_const_alias: thm -> theory -> theory
    29 end;
    30 
    31 structure Nbe: NBE =
    32 struct
    33 
    34 (* generic non-sense *)
    35 
    36 val trace = Unsynchronized.ref false;
    37 fun traced f x = if !trace then (tracing (f x); x) else x;
    38 
    39 
    40 (** certificates and oracle for "trivial type classes" **)
    41 
    42 structure Triv_Class_Data = Theory_Data
    43 (
    44   type T = (class * thm) list;
    45   val empty = [];
    46   val extend = I;
    47   fun merge data : T = AList.merge (op =) (K true) data;
    48 );
    49 
    50 fun add_const_alias thm thy =
    51   let
    52     val (ofclass, eqn) = case try Logic.dest_equals (Thm.prop_of thm)
    53      of SOME ofclass_eq => ofclass_eq
    54       | _ => error ("Bad certificate: " ^ Display.string_of_thm_global thy thm);
    55     val (T, class) = case try Logic.dest_of_class ofclass
    56      of SOME T_class => T_class
    57       | _ => error ("Bad certificate: " ^ Display.string_of_thm_global thy thm);
    58     val tvar = case try Term.dest_TVar T
    59      of SOME (tvar as (_, sort)) => if null (filter (can (Axclass.get_info thy)) sort)
    60           then tvar
    61           else error ("Bad sort: " ^ Display.string_of_thm_global thy thm)
    62       | _ => error ("Bad type: " ^ Display.string_of_thm_global thy thm);
    63     val _ = if Term.add_tvars eqn [] = [tvar] then ()
    64       else error ("Inconsistent type: " ^ Display.string_of_thm_global thy thm);
    65     val lhs_rhs = case try Logic.dest_equals eqn
    66      of SOME lhs_rhs => lhs_rhs
    67       | _ => error ("Not an equation: " ^ Syntax.string_of_term_global thy eqn);
    68     val c_c' = case try (pairself (Axclass.unoverload_const thy o dest_Const)) lhs_rhs
    69      of SOME c_c' => c_c'
    70       | _ => error ("Not an equation with two constants: "
    71           ^ Syntax.string_of_term_global thy eqn);
    72     val _ = if the_list (Axclass.class_of_param thy (snd c_c')) = [class] then ()
    73       else error ("Inconsistent class: " ^ Display.string_of_thm_global thy thm);
    74   in Triv_Class_Data.map (AList.update (op =) (class, thm)) thy end;
    75 
    76 local
    77 
    78 val get_triv_classes = map fst o Triv_Class_Data.get;
    79 
    80 val (_, triv_of_class) = Context.>>> (Context.map_theory_result
    81   (Thm.add_oracle (@{binding triv_of_class}, fn (thy, T, class) =>
    82     Thm.cterm_of thy (Logic.mk_of_class (T, class)))));
    83 
    84 in
    85 
    86 fun lift_triv_classes_conv ctxt conv ct =
    87   let
    88     val thy = Proof_Context.theory_of ctxt;
    89     val algebra = Sign.classes_of thy;
    90     val certT = Thm.ctyp_of thy;
    91     val triv_classes = get_triv_classes thy;
    92     fun additional_classes sort = filter_out (fn class => Sorts.sort_le algebra (sort, [class])) triv_classes;
    93     fun mk_entry (v, sort) =
    94       let
    95         val T = TFree (v, sort);
    96         val cT = certT T;
    97         val triv_sort = additional_classes sort;
    98       in
    99         (v, (Sorts.inter_sort algebra (sort, triv_sort),
   100           (cT, AList.make (fn class => Thm.of_class (cT, class)) sort
   101             @ AList.make (fn class => triv_of_class (thy, T, class)) triv_sort)))
   102       end;
   103     val vs_tab = map mk_entry (Term.add_tfrees (Thm.term_of ct) []);
   104     fun instantiate thm =
   105       let
   106         val cert_tvars = map (certT o TVar) (Term.add_tvars
   107           ((fst o Logic.dest_equals o Logic.strip_imp_concl o Thm.prop_of) thm) []);
   108         val instantiation =
   109           map2 (fn cert_tvar => fn (_, (_, (cT, _))) => (cert_tvar, cT)) cert_tvars vs_tab;
   110       in Thm.instantiate (instantiation, []) thm end;
   111     fun of_class (TFree (v, _), class) =
   112           the (AList.lookup (op =) ((snd o snd o the o AList.lookup (op =) vs_tab) v) class)
   113       | of_class (T, _) = error ("Bad type " ^ Syntax.string_of_typ_global thy T);
   114     fun strip_of_class thm =
   115       let
   116         val prems_of_class = Thm.prop_of thm
   117           |> Logic.strip_imp_prems
   118           |> map (Logic.dest_of_class #> of_class);
   119       in fold Thm.elim_implies prems_of_class thm end;
   120   in
   121     ct
   122     |> (Drule.cterm_fun o map_types o map_type_tfree)
   123         (fn (v, _) => TFree (v, (fst o the o AList.lookup (op =) vs_tab) v))
   124     |> conv
   125     |> Thm.strip_shyps
   126     |> Thm.varifyT_global
   127     |> Thm.unconstrainT
   128     |> instantiate
   129     |> strip_of_class
   130   end;
   131 
   132 fun lift_triv_classes_rew ctxt rew t =
   133   let
   134     val thy = Proof_Context.theory_of ctxt;
   135     val algebra = Sign.classes_of thy;
   136     val triv_classes = get_triv_classes thy;
   137     val vs = Term.add_tfrees t [];
   138   in
   139     t
   140     |> (map_types o map_type_tfree)
   141         (fn (v, sort) => TFree (v, Sorts.inter_sort algebra (sort, triv_classes)))
   142     |> rew
   143     |> (map_types o map_type_tfree)
   144         (fn (v, sort) => TFree (v, the_default sort (AList.lookup (op =) vs v)))
   145   end;
   146 
   147 end;
   148 
   149 
   150 (** the semantic universe **)
   151 
   152 (*
   153    Functions are given by their semantical function value. To avoid
   154    trouble with the ML-type system, these functions have the most
   155    generic type, that is "Univ list -> Univ". The calling convention is
   156    that the arguments come as a list, the last argument first. In
   157    other words, a function call that usually would look like
   158 
   159    f x_1 x_2 ... x_n   or   f(x_1,x_2, ..., x_n)
   160 
   161    would be in our convention called as
   162 
   163               f [x_n,..,x_2,x_1]
   164 
   165    Moreover, to handle functions that are still waiting for some
   166    arguments we have additionally a list of arguments collected to far
   167    and the number of arguments we're still waiting for.
   168 *)
   169 
   170 datatype Univ =
   171     Const of int * Univ list           (*named (uninterpreted) constants*)
   172   | DFree of string * int              (*free (uninterpreted) dictionary parameters*)
   173   | BVar of int * Univ list            (*bound variables, named*)
   174   | Abs of (int * (Univ list -> Univ)) * Univ list
   175                                        (*abstractions as closures*);
   176 
   177 
   178 (* constructor functions *)
   179 
   180 fun abss n f = Abs ((n, f), []);
   181 fun apps (Abs ((n, f), xs)) ys = let val k = n - length ys in
   182       case int_ord (k, 0)
   183        of EQUAL => f (ys @ xs)
   184         | LESS => let val (zs, ws) = chop (~ k) ys in apps (f (ws @ xs)) zs end
   185         | GREATER => Abs ((k, f), ys @ xs) (*note: reverse convention also for apps!*)
   186       end
   187   | apps (Const (name, xs)) ys = Const (name, ys @ xs)
   188   | apps (BVar (n, xs)) ys = BVar (n, ys @ xs);
   189 
   190 fun same (Const (k, xs), Const (l, ys)) = k = l andalso eq_list same (xs, ys)
   191   | same (DFree (s, k), DFree (t, l)) = s = t andalso k = l
   192   | same (BVar (k, xs), BVar (l, ys)) = k = l andalso eq_list same (xs, ys)
   193   | same _ = false;
   194 
   195 
   196 (** assembling and compiling ML code from terms **)
   197 
   198 (* abstract ML syntax *)
   199 
   200 infix 9 `$` `$$`;
   201 fun e1 `$` e2 = "(" ^ e1 ^ " " ^ e2 ^ ")";
   202 fun e `$$` [] = e
   203   | e `$$` es = "(" ^ e ^ " " ^ space_implode " " es ^ ")";
   204 fun ml_abs v e = "(fn " ^ v ^ " => " ^ e ^ ")";
   205 
   206 fun ml_cases t cs =
   207   "(case " ^ t ^ " of " ^ space_implode " | " (map (fn (p, t) => p ^ " => " ^ t) cs) ^ ")";
   208 fun ml_Let d e = "let\n" ^ d ^ " in " ^ e ^ " end";
   209 fun ml_as v t = "(" ^ v ^ " as " ^ t ^ ")";
   210 
   211 fun ml_and [] = "true"
   212   | ml_and [x] = x
   213   | ml_and xs = "(" ^ space_implode " andalso " xs ^ ")";
   214 fun ml_if b x y = "(if " ^ b ^ " then " ^ x ^ " else " ^ y ^ ")";
   215 
   216 fun ml_list es = "[" ^ commas es ^ "]";
   217 
   218 fun ml_fundefs ([(name, [([], e)])]) =
   219       "val " ^ name ^ " = " ^ e ^ "\n"
   220   | ml_fundefs (eqs :: eqss) =
   221       let
   222         fun fundef (name, eqs) =
   223           let
   224             fun eqn (es, e) = name ^ " " ^ space_implode " " es ^ " = " ^ e
   225           in space_implode "\n  | " (map eqn eqs) end;
   226       in
   227         (prefix "fun " o fundef) eqs :: map (prefix "and " o fundef) eqss
   228         |> cat_lines
   229         |> suffix "\n"
   230       end;
   231 
   232 
   233 (* nbe specific syntax and sandbox communication *)
   234 
   235 structure Univs = Proof_Data
   236 (
   237   type T = unit -> Univ list -> Univ list
   238   (* FIXME avoid user error with non-user text *)
   239   fun init _ () = error "Univs"
   240 );
   241 val put_result = Univs.put;
   242 
   243 local
   244   val prefix =     "Nbe.";
   245   val name_put =   prefix ^ "put_result";
   246   val name_const = prefix ^ "Const";
   247   val name_abss =  prefix ^ "abss";
   248   val name_apps =  prefix ^ "apps";
   249   val name_same =  prefix ^ "same";
   250 in
   251 
   252 val univs_cookie = (Univs.get, put_result, name_put);
   253 
   254 fun nbe_fun idx_of 0 (Code_Symbol.Constant "") = "nbe_value"
   255   | nbe_fun idx_of i sym = "c_" ^ string_of_int (idx_of sym)
   256       ^ "_" ^ Code_Symbol.default_base sym ^ "_" ^ string_of_int i;
   257 fun nbe_dict v n = "d_" ^ v ^ "_" ^ string_of_int n;
   258 fun nbe_bound v = "v_" ^ v;
   259 fun nbe_bound_optional NONE = "_"
   260   | nbe_bound_optional (SOME v) = nbe_bound v;
   261 fun nbe_default v = "w_" ^ v;
   262 
   263 (*note: these three are the "turning spots" where proper argument order is established!*)
   264 fun nbe_apps t [] = t
   265   | nbe_apps t ts = name_apps `$$` [t, ml_list (rev ts)];
   266 fun nbe_apps_local idx_of i c ts = nbe_fun idx_of i c `$` ml_list (rev ts);
   267 fun nbe_apps_constr idx_of c ts =
   268   let
   269     val c' = if !trace then string_of_int (idx_of c) ^ " (*" ^ Code_Symbol.default_base c ^ "*)"
   270       else string_of_int (idx_of c);
   271   in name_const `$` ("(" ^ c' ^ ", " ^ ml_list (rev ts) ^ ")") end;
   272 
   273 fun nbe_abss 0 f = f `$` ml_list []
   274   | nbe_abss n f = name_abss `$$` [string_of_int n, f];
   275 
   276 fun nbe_same (v1, v2) = "(" ^ name_same ^ " (" ^ nbe_bound v1 ^ ", " ^ nbe_bound v2 ^ "))";
   277 
   278 end;
   279 
   280 open Basic_Code_Symbol;
   281 open Basic_Code_Thingol;
   282 
   283 
   284 (* code generation *)
   285 
   286 fun assemble_eqnss idx_of deps eqnss =
   287   let
   288     fun prep_eqns (c, (vs, eqns)) =
   289       let
   290         val dicts = maps (fn (v, sort) => map_index (nbe_dict v o fst) sort) vs;
   291         val num_args = length dicts + ((length o fst o hd) eqns);
   292       in (c, (num_args, (dicts, eqns))) end;
   293     val eqnss' = map prep_eqns eqnss;
   294 
   295     fun assemble_constapp sym dss ts = 
   296       let
   297         val ts' = (maps o map) assemble_dict dss @ ts;
   298       in case AList.lookup (op =) eqnss' sym
   299        of SOME (num_args, _) => if num_args <= length ts'
   300             then let val (ts1, ts2) = chop num_args ts'
   301             in nbe_apps (nbe_apps_local idx_of 0 sym ts1) ts2
   302             end else nbe_apps (nbe_abss num_args (nbe_fun idx_of 0 sym)) ts'
   303         | NONE => if member (op =) deps sym
   304             then nbe_apps (nbe_fun idx_of 0 sym) ts'
   305             else nbe_apps_constr idx_of sym ts'
   306       end
   307     and assemble_classrels classrels =
   308       fold_rev (fn classrel => assemble_constapp (Class_Relation classrel) [] o single) classrels
   309     and assemble_dict (Dict (classrels, x)) =
   310           assemble_classrels classrels (assemble_plain_dict x)
   311     and assemble_plain_dict (Dict_Const (inst, dss)) =
   312           assemble_constapp (Class_Instance inst) dss []
   313       | assemble_plain_dict (Dict_Var (v, (n, _))) =
   314           nbe_dict v n
   315 
   316     fun assemble_iterm constapp =
   317       let
   318         fun of_iterm match_cont t =
   319           let
   320             val (t', ts) = Code_Thingol.unfold_app t
   321           in of_iapp match_cont t' (fold_rev (cons o of_iterm NONE) ts []) end
   322         and of_iapp match_cont (IConst { sym, dicts = dss, ... }) ts = constapp sym dss ts
   323           | of_iapp match_cont (IVar v) ts = nbe_apps (nbe_bound_optional v) ts
   324           | of_iapp match_cont ((v, _) `|=> t) ts =
   325               nbe_apps (nbe_abss 1 (ml_abs (ml_list [nbe_bound_optional v]) (of_iterm NONE t))) ts
   326           | of_iapp match_cont (ICase { term = t, clauses = clauses, primitive = t0, ... }) ts =
   327               nbe_apps (ml_cases (of_iterm NONE t)
   328                 (map (fn (p, t) => (of_iterm NONE p, of_iterm match_cont t)) clauses
   329                   @ [("_", case match_cont of SOME s => s | NONE => of_iterm NONE t0)])) ts
   330       in of_iterm end;
   331 
   332     fun subst_nonlin_vars args =
   333       let
   334         val vs = (fold o Code_Thingol.fold_varnames)
   335           (fn v => AList.map_default (op =) (v, 0) (Integer.add 1)) args [];
   336         val names = Name.make_context (map fst vs);
   337         fun declare v k ctxt =
   338           let val vs = Name.invent ctxt v k
   339           in (vs, fold Name.declare vs ctxt) end;
   340         val (vs_renames, _) = fold_map (fn (v, k) => if k > 1
   341           then declare v (k - 1) #>> (fn vs => (v, vs))
   342           else pair (v, [])) vs names;
   343         val samepairs = maps (fn (v, vs) => map (pair v) vs) vs_renames;
   344         fun subst_vars (t as IConst _) samepairs = (t, samepairs)
   345           | subst_vars (t as IVar NONE) samepairs = (t, samepairs)
   346           | subst_vars (t as IVar (SOME v)) samepairs = (case AList.lookup (op =) samepairs v
   347              of SOME v' => (IVar (SOME v'), AList.delete (op =) v samepairs)
   348               | NONE => (t, samepairs))
   349           | subst_vars (t1 `$ t2) samepairs = samepairs
   350               |> subst_vars t1
   351               ||>> subst_vars t2
   352               |>> (op `$)
   353           | subst_vars (ICase { primitive = t, ... }) samepairs = subst_vars t samepairs;
   354         val (args', _) = fold_map subst_vars args samepairs;
   355       in (samepairs, args') end;
   356 
   357     fun assemble_eqn sym dicts default_args (i, (args, rhs)) =
   358       let
   359         val match_cont = if Code_Symbol.is_value sym then NONE
   360           else SOME (nbe_apps_local idx_of (i + 1) sym (dicts @ default_args));
   361         val assemble_arg = assemble_iterm
   362           (fn sym' => fn dss => fn ts => nbe_apps_constr idx_of sym' ((maps o map) (K "_") dss @ ts)) NONE;
   363         val assemble_rhs = assemble_iterm assemble_constapp match_cont;
   364         val (samepairs, args') = subst_nonlin_vars args;
   365         val s_args = map assemble_arg args';
   366         val s_rhs = if null samepairs then assemble_rhs rhs
   367           else ml_if (ml_and (map nbe_same samepairs))
   368             (assemble_rhs rhs) (the match_cont);
   369         val eqns = case match_cont
   370          of NONE => [([ml_list (rev (dicts @ s_args))], s_rhs)]
   371           | SOME default_rhs =>
   372               [([ml_list (rev (dicts @ map2 ml_as default_args s_args))], s_rhs),
   373                 ([ml_list (rev (dicts @ default_args))], default_rhs)]
   374       in (nbe_fun idx_of i sym, eqns) end;
   375 
   376     fun assemble_eqns (sym, (num_args, (dicts, eqns))) =
   377       let
   378         val default_args = map nbe_default
   379           (Name.invent Name.context "a" (num_args - length dicts));
   380         val eqns' = map_index (assemble_eqn sym dicts default_args) eqns
   381           @ (if Code_Symbol.is_value sym then [] else [(nbe_fun idx_of (length eqns) sym,
   382             [([ml_list (rev (dicts @ default_args))],
   383               nbe_apps_constr idx_of sym (dicts @ default_args))])]);
   384       in (eqns', nbe_abss num_args (nbe_fun idx_of 0 sym)) end;
   385 
   386     val (fun_vars, fun_vals) = map_split assemble_eqns eqnss';
   387     val deps_vars = ml_list (map (nbe_fun idx_of 0) deps);
   388   in ml_abs deps_vars (ml_Let (ml_fundefs (flat fun_vars)) (ml_list fun_vals)) end;
   389 
   390 
   391 (* compile equations *)
   392 
   393 fun compile_eqnss ctxt nbe_program raw_deps [] = []
   394   | compile_eqnss ctxt nbe_program raw_deps eqnss =
   395       let
   396         val (deps, deps_vals) = split_list (map_filter
   397           (fn dep => Option.map (fn univ => (dep, univ)) (fst ((Code_Symbol.Graph.get_node nbe_program dep)))) raw_deps);
   398         val idx_of = raw_deps
   399           |> map (fn dep => (dep, snd (Code_Symbol.Graph.get_node nbe_program dep)))
   400           |> AList.lookup (op =)
   401           |> (fn f => the o f);
   402         val s = assemble_eqnss idx_of deps eqnss;
   403         val cs = map fst eqnss;
   404       in
   405         s
   406         |> traced (fn s => "\n--- code to be evaluated:\n" ^ s)
   407         |> pair ""
   408         |> Code_Runtime.value ctxt univs_cookie
   409         |> (fn f => f deps_vals)
   410         |> (fn univs => cs ~~ univs)
   411       end;
   412 
   413 
   414 (* extract equations from statements *)
   415 
   416 fun dummy_const sym dss =
   417   IConst { sym = sym, typargs = [], dicts = dss,
   418     dom = [], range = ITyVar "", annotate = false };
   419 
   420 fun eqns_of_stmt (_, Code_Thingol.NoStmt) =
   421       []
   422   | eqns_of_stmt (_, Code_Thingol.Fun ((_, []), _)) =
   423       []
   424   | eqns_of_stmt (sym_const, Code_Thingol.Fun (((vs, _), eqns), _)) =
   425       [(sym_const, (vs, map fst eqns))]
   426   | eqns_of_stmt (_, Code_Thingol.Datatypecons _) =
   427       []
   428   | eqns_of_stmt (_, Code_Thingol.Datatype _) =
   429       []
   430   | eqns_of_stmt (sym_class, Code_Thingol.Class (v, (classrels, classparams))) =
   431       let
   432         val syms = map Class_Relation classrels @ map (Constant o fst) classparams;
   433         val params = Name.invent Name.context "d" (length syms);
   434         fun mk (k, sym) =
   435           (sym, ([(v, [])],
   436             [([dummy_const sym_class [] `$$ map (IVar o SOME) params],
   437               IVar (SOME (nth params k)))]));
   438       in map_index mk syms end
   439   | eqns_of_stmt (_, Code_Thingol.Classrel _) =
   440       []
   441   | eqns_of_stmt (_, Code_Thingol.Classparam _) =
   442       []
   443   | eqns_of_stmt (sym_inst, Code_Thingol.Classinst { class, tyco, vs, superinsts, inst_params, ... }) =
   444       [(sym_inst, (vs, [([], dummy_const (Type_Class class) [] `$$
   445         map (fn (class, dss) => dummy_const (Class_Instance (tyco, class)) dss) superinsts
   446         @ map (IConst o fst o snd o fst) inst_params)]))];
   447 
   448 
   449 (* compile whole programs *)
   450 
   451 fun ensure_const_idx name (nbe_program, (maxidx, idx_tab)) =
   452   if can (Code_Symbol.Graph.get_node nbe_program) name
   453   then (nbe_program, (maxidx, idx_tab))
   454   else (Code_Symbol.Graph.new_node (name, (NONE, maxidx)) nbe_program,
   455     (maxidx + 1, Inttab.update_new (maxidx, name) idx_tab));
   456 
   457 fun compile_stmts ctxt stmts_deps =
   458   let
   459     val names = map (fst o fst) stmts_deps;
   460     val names_deps = map (fn ((name, _), deps) => (name, deps)) stmts_deps;
   461     val eqnss = maps (eqns_of_stmt o fst) stmts_deps;
   462     val refl_deps = names_deps
   463       |> maps snd
   464       |> distinct (op =)
   465       |> fold (insert (op =)) names;
   466     fun compile nbe_program = eqnss
   467       |> compile_eqnss ctxt nbe_program refl_deps
   468       |> rpair nbe_program;
   469   in
   470     fold ensure_const_idx refl_deps
   471     #> apfst (fold (fn (name, deps) => fold (curry Code_Symbol.Graph.add_edge name) deps) names_deps
   472       #> compile
   473       #-> fold (fn (name, univ) => (Code_Symbol.Graph.map_node name o apfst) (K (SOME univ))))
   474   end;
   475 
   476 fun compile_program ctxt program =
   477   let
   478     fun add_stmts names (nbe_program, (maxidx, idx_tab)) = if exists ((can o Code_Symbol.Graph.get_node) nbe_program) names
   479       then (nbe_program, (maxidx, idx_tab))
   480       else (nbe_program, (maxidx, idx_tab))
   481         |> compile_stmts ctxt (map (fn name => ((name, Code_Symbol.Graph.get_node program name),
   482           Code_Symbol.Graph.immediate_succs program name)) names);
   483   in
   484     fold_rev add_stmts (Code_Symbol.Graph.strong_conn program)
   485   end;
   486 
   487 
   488 (** evaluation **)
   489 
   490 (* term evaluation by compilation *)
   491 
   492 fun compile_term ctxt nbe_program deps (vs : (string * sort) list, t) =
   493   let 
   494     val dict_frees = maps (fn (v, sort) => map_index (curry DFree v o fst) sort) vs;
   495   in
   496     (Code_Symbol.value, (vs, [([], t)]))
   497     |> singleton (compile_eqnss ctxt nbe_program deps)
   498     |> snd
   499     |> (fn t => apps t (rev dict_frees))
   500   end;
   501 
   502 
   503 (* reconstruction *)
   504 
   505 fun typ_of_itype vs (tyco `%% itys) =
   506       Type (tyco, map (typ_of_itype vs) itys)
   507   | typ_of_itype vs (ITyVar v) =
   508       TFree ("'" ^ v, (the o AList.lookup (op =) vs) v);
   509 
   510 fun term_of_univ ctxt (idx_tab : Code_Symbol.T Inttab.table) t =
   511   let
   512     fun take_until f [] = []
   513       | take_until f (x :: xs) = if f x then [] else x :: take_until f xs;
   514     fun is_dict (Const (idx, _)) =
   515           (case Inttab.lookup idx_tab idx of
   516             SOME (Constant _) => false
   517           | _ => true)
   518       | is_dict (DFree _) = true
   519       | is_dict _ = false;
   520     fun const_of_idx idx =
   521       case Inttab.lookup idx_tab idx of SOME (Constant const) => const;
   522     fun of_apps bounds (t, ts) =
   523       fold_map (of_univ bounds) ts
   524       #>> (fn ts' => list_comb (t, rev ts'))
   525     and of_univ bounds (Const (idx, ts)) typidx =
   526           let
   527             val ts' = take_until is_dict ts;
   528             val const = const_of_idx idx;
   529             val T = map_type_tvar (fn ((v, i), _) =>
   530               Type_Infer.param typidx (v ^ string_of_int i, []))
   531                 (Sign.the_const_type (Proof_Context.theory_of ctxt) const);
   532             val typidx' = typidx + 1;
   533           in of_apps bounds (Term.Const (const, T), ts') typidx' end
   534       | of_univ bounds (BVar (n, ts)) typidx =
   535           of_apps bounds (Bound (bounds - n - 1), ts) typidx
   536       | of_univ bounds (t as Abs _) typidx =
   537           typidx
   538           |> of_univ (bounds + 1) (apps t [BVar (bounds, [])])
   539           |-> (fn t' => pair (Term.Abs ("u", dummyT, t')))
   540   in of_univ 0 t 0 |> fst end;
   541 
   542 
   543 (* evaluation with type reconstruction *)
   544 
   545 fun eval_term raw_ctxt (nbe_program, idx_tab) ((vs0, (vs, ty)), t) deps =
   546   let
   547     val ctxt = Syntax.init_pretty_global (Proof_Context.theory_of raw_ctxt);
   548     val string_of_term = Syntax.string_of_term (Config.put show_types true ctxt);
   549     val ty' = typ_of_itype vs0 ty;
   550     fun type_infer t =
   551       Syntax.check_term (Config.put Type_Infer_Context.const_sorts false ctxt)
   552         (Type.constraint ty' t);
   553     fun check_tvars t =
   554       if null (Term.add_tvars t []) then t
   555       else error ("Illegal schematic type variables in normalized term: " ^ string_of_term t);
   556   in
   557     compile_term ctxt nbe_program deps (vs, t)
   558     |> term_of_univ ctxt idx_tab
   559     |> traced (fn t => "Normalized:\n" ^ string_of_term t)
   560     |> type_infer
   561     |> traced (fn t => "Types inferred:\n" ^ string_of_term t)
   562     |> check_tvars
   563     |> traced (fn _ => "---\n")
   564   end;
   565 
   566 
   567 (* function store *)
   568 
   569 structure Nbe_Functions = Code_Data
   570 (
   571   type T = (Univ option * int) Code_Symbol.Graph.T * (int * Code_Symbol.T Inttab.table);
   572   val empty = (Code_Symbol.Graph.empty, (0, Inttab.empty));
   573 );
   574 
   575 fun compile ignore_cache ctxt program =
   576   let
   577     val (nbe_program, (_, idx_tab)) =
   578       Nbe_Functions.change (if ignore_cache then NONE else SOME (Proof_Context.theory_of ctxt))
   579         (compile_program ctxt program);
   580   in (nbe_program, idx_tab) end;
   581 
   582 
   583 (* evaluation oracle *)
   584 
   585 fun mk_equals ctxt lhs raw_rhs =
   586   let
   587     val thy = Proof_Context.theory_of ctxt;
   588     val ty = Thm.typ_of (Thm.ctyp_of_term lhs);
   589     val eq = Thm.cterm_of thy (Term.Const ("==", ty --> ty --> propT));
   590     val rhs = Thm.cterm_of thy raw_rhs;
   591   in Thm.mk_binop eq lhs rhs end;
   592 
   593 val (_, raw_oracle) = Context.>>> (Context.map_theory_result
   594   (Thm.add_oracle (@{binding normalization_by_evaluation},
   595     fn (ctxt, nbe_program_idx_tab, vsp_ty_t, deps, ct) =>
   596       mk_equals ctxt ct (eval_term ctxt nbe_program_idx_tab vsp_ty_t deps))));
   597 
   598 fun oracle ctxt nbe_program_idx_tab vsp_ty_t deps ct =
   599   raw_oracle (ctxt, nbe_program_idx_tab, vsp_ty_t, deps, ct);
   600 
   601 fun dynamic_conv ctxt = lift_triv_classes_conv ctxt
   602   (Code_Thingol.dynamic_conv ctxt (oracle ctxt o compile false ctxt));
   603 
   604 fun dynamic_value ctxt = lift_triv_classes_rew ctxt
   605   (Code_Thingol.dynamic_value ctxt I (eval_term ctxt o compile false ctxt));
   606 
   607 fun static_conv ctxt consts =
   608   let
   609     val evaluator = Code_Thingol.static_conv ctxt consts
   610       (fn program => fn _ => K (oracle ctxt (compile true ctxt program)));
   611   in fn ctxt' => lift_triv_classes_conv ctxt' (evaluator ctxt') end;
   612 
   613 fun static_value ctxt consts =
   614   let
   615     val evaluator = Code_Thingol.static_value ctxt I consts
   616       (fn program => fn _ => K (eval_term ctxt (compile true ctxt program)));
   617   in fn ctxt' => lift_triv_classes_rew ctxt' (evaluator ctxt') end;
   618 
   619 
   620 (** setup **)
   621 
   622 val setup = Value.add_evaluator ("nbe", dynamic_value);
   623 
   624 end;