haftmann@37744: (* Title: Tools/Code/code_preproc.ML haftmann@30010: Author: Florian Haftmann, TU Muenchen haftmann@30010: haftmann@31125: Preprocessing code equations into a well-sorted system haftmann@31125: in a graph with explicit dependencies. haftmann@30010: *) haftmann@30010: haftmann@31125: signature CODE_PREPROC = haftmann@30010: sig wenzelm@51717: val map_pre: (Proof.context -> Proof.context) -> theory -> theory wenzelm@51717: val map_post: (Proof.context -> Proof.context) -> theory -> theory haftmann@55757: val add_functrans: string * (Proof.context -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory haftmann@31125: val del_functrans: string -> theory -> theory haftmann@55757: val simple_functrans: (Proof.context -> thm list -> thm list option) haftmann@55757: -> Proof.context -> (thm * bool) list -> (thm * bool) list option haftmann@55757: val print_codeproc: Proof.context -> unit haftmann@31125: haftmann@30947: type code_algebra haftmann@30947: type code_graph haftmann@34891: val cert: code_graph -> string -> Code.cert haftmann@32873: val sortargs: code_graph -> string -> sort list haftmann@30947: val all: code_graph -> string list haftmann@55757: val pretty: Proof.context -> code_graph -> Pretty.T wenzelm@60697: val obtain: bool -> Proof.context -> string list -> term list -> code_algebra * code_graph haftmann@55757: val dynamic_conv: Proof.context haftmann@56920: -> (code_algebra -> code_graph -> term -> conv) -> conv haftmann@56968: val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'b) haftmann@56968: -> (code_algebra -> code_graph -> term -> 'a) -> term -> 'b haftmann@56973: val static_conv: { ctxt: Proof.context, consts: string list } haftmann@56973: -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> conv) haftmann@55757: -> Proof.context -> conv haftmann@56973: val static_value: { ctxt: Proof.context, lift_postproc: ((term -> term) -> 'a -> 'b), consts: string list } haftmann@56973: -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> 'a) haftmann@56968: -> Proof.context -> term -> 'b haftmann@57430: haftmann@57430: val trace_none: Context.generic -> Context.generic haftmann@57430: val trace_all: Context.generic -> Context.generic haftmann@57430: val trace_only: string list -> Context.generic -> Context.generic haftmann@57430: val trace_only_ext: string list -> Context.generic -> Context.generic haftmann@30010: end haftmann@30010: haftmann@31125: structure Code_Preproc : CODE_PREPROC = haftmann@30010: struct haftmann@30010: haftmann@31125: (** preprocessor administration **) haftmann@31125: haftmann@31125: (* theory data *) haftmann@31125: haftmann@31125: datatype thmproc = Thmproc of { haftmann@31125: pre: simpset, haftmann@31125: post: simpset, haftmann@55757: functrans: (string * (serial * (Proof.context -> (thm * bool) list -> (thm * bool) list option))) list haftmann@31125: }; haftmann@31125: haftmann@31599: fun make_thmproc ((pre, post), functrans) = haftmann@31125: Thmproc { pre = pre, post = post, functrans = functrans }; haftmann@31125: fun map_thmproc f (Thmproc { pre, post, functrans }) = haftmann@31599: make_thmproc (f ((pre, post), functrans)); haftmann@31125: fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 }, haftmann@31125: Thmproc { pre = pre2, post = post2, functrans = functrans2 }) = haftmann@31125: let haftmann@31125: val pre = Simplifier.merge_ss (pre1, pre2); haftmann@31125: val post = Simplifier.merge_ss (post1, post2); haftmann@34891: val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2) haftmann@34891: handle AList.DUP => error ("Duplicate function transformer"); haftmann@31599: in make_thmproc ((pre, post), functrans) end; haftmann@31125: wenzelm@33522: structure Code_Preproc_Data = Theory_Data haftmann@31125: ( haftmann@31125: type T = thmproc; haftmann@31599: val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []); wenzelm@33522: val extend = I; wenzelm@33522: val merge = merge_thmproc; haftmann@31125: ); haftmann@31125: haftmann@31125: fun the_thmproc thy = case Code_Preproc_Data.get thy haftmann@31125: of Thmproc x => x; haftmann@31125: haftmann@31125: fun delete_force msg key xs = haftmann@31125: if AList.defined (op =) xs key then AList.delete (op =) key xs haftmann@31125: else error ("No such " ^ msg ^ ": " ^ quote key); haftmann@31125: haftmann@38669: val map_data = Code_Preproc_Data.map o map_thmproc; haftmann@31125: haftmann@32072: val map_pre_post = map_data o apfst; wenzelm@51717: wenzelm@51717: fun map_simpset which f thy = wenzelm@51717: map_pre_post (which (simpset_map (Proof_Context.init_global thy) f)) thy; wenzelm@51717: val map_pre = map_simpset apfst; wenzelm@51717: val map_post = map_simpset apsnd; haftmann@31125: haftmann@56929: fun process_unfold add_del = map_pre o add_del; haftmann@56929: fun process_post add_del = map_post o add_del; haftmann@32072: haftmann@56929: fun process_abbrev add_del raw_thm thy = haftmann@32072: let wenzelm@51717: val ctxt = Proof_Context.init_global thy; wenzelm@51717: val thm = Local_Defs.meta_rewrite_rule ctxt raw_thm; haftmann@32072: val thm_sym = Thm.symmetric thm; haftmann@32072: in haftmann@32072: thy |> map_pre_post (fn (pre, post) => haftmann@56929: (pre |> simpset_map ctxt (add_del thm_sym), haftmann@56929: post |> simpset_map ctxt (add_del thm))) haftmann@32072: end; haftmann@32072: haftmann@31125: fun add_functrans (name, f) = (map_data o apsnd) haftmann@31125: (AList.update (op =) (name, (serial (), f))); haftmann@31125: haftmann@31125: fun del_functrans name = (map_data o apsnd) haftmann@31125: (delete_force "function transformer" name); haftmann@31125: haftmann@31125: haftmann@56968: (* algebra of sandwiches: cterm transformations with pending postprocessors *) haftmann@56968: haftmann@56976: fun matches_transitive eq1 eq2 = Thm.rhs_of eq1 aconvc Thm.lhs_of eq2; haftmann@56976: haftmann@56968: fun trans_comb eq1 eq2 = haftmann@56976: (*explicit assertions: evaluation conversion stacks are error-prone*) haftmann@56976: if Thm.is_reflexive eq1 then (@{assert} (matches_transitive eq1 eq2); eq2) haftmann@56976: else if Thm.is_reflexive eq2 then (@{assert} (matches_transitive eq1 eq2); eq1) haftmann@56968: else Thm.transitive eq1 eq2; haftmann@56968: haftmann@56968: fun trans_conv_rule conv eq = trans_comb eq (conv (Thm.rhs_of eq)); haftmann@56968: haftmann@56976: structure Sandwich : sig haftmann@56976: type T = Proof.context -> cterm -> (thm -> thm) * cterm; haftmann@56976: val chain: T -> T -> T haftmann@56976: val lift: (Proof.context -> cterm -> (cterm -> thm) * thm) -> T haftmann@56976: val conversion: T -> (Proof.context -> term -> conv) -> Proof.context -> conv; haftmann@63157: val computation: T -> ((term -> term) -> 'a -> 'b) -> haftmann@56976: (Proof.context -> term -> 'a) -> Proof.context -> term -> 'b; haftmann@56976: end = struct haftmann@56976: haftmann@56976: type T = Proof.context -> cterm -> (thm -> thm) * cterm; haftmann@56968: haftmann@56968: fun chain sandwich2 sandwich1 ctxt = haftmann@56968: sandwich1 ctxt haftmann@56968: ##>> sandwich2 ctxt haftmann@56968: #>> (op o); haftmann@56968: haftmann@56976: fun lift conv_sandwhich ctxt ct = haftmann@56968: let haftmann@56968: val (postproc_conv, eq) = conv_sandwhich ctxt ct; haftmann@56976: fun potentail_trans_comb eq1 eq2 = haftmann@56976: if matches_transitive eq1 eq2 then trans_comb eq1 eq2 else eq2; haftmann@56976: (*weakened protocol for plain term evaluation*) haftmann@56976: in (trans_conv_rule postproc_conv o potentail_trans_comb eq, Thm.rhs_of eq) end; haftmann@56968: haftmann@56976: fun conversion sandwich conv ctxt ct = haftmann@56968: let haftmann@56968: val (postproc, ct') = sandwich ctxt ct; wenzelm@59582: in postproc (conv ctxt (Thm.term_of ct') ct') end; haftmann@56968: haftmann@63157: fun computation sandwich lift_postproc eval ctxt t = haftmann@56968: let wenzelm@59633: val (postproc, ct') = sandwich ctxt (Thm.cterm_of ctxt t); haftmann@56968: in wenzelm@59582: Thm.term_of ct' haftmann@56968: |> eval ctxt wenzelm@59633: |> lift_postproc (Thm.term_of o Thm.rhs_of o postproc o Thm.reflexive o Thm.cterm_of ctxt) haftmann@56968: end; haftmann@56968: haftmann@56976: end; haftmann@56976: haftmann@56968: haftmann@31125: (* post- and preprocessing *) haftmann@31125: haftmann@56968: fun normalized_tfrees_sandwich ctxt ct = haftmann@56967: let wenzelm@59582: val t = Thm.term_of ct; wenzelm@60805: val vs_original = wenzelm@60805: fold_term_types (K (fold_atyps (insert (eq_fst op =) o dest_TFree))) t []; haftmann@56967: val vs_normalized = Name.invent_names Name.context Name.aT (map snd vs_original); wenzelm@60805: val normalize = wenzelm@60805: map_type_tfree (TFree o the o AList.lookup (op =) (vs_original ~~ vs_normalized)); haftmann@56967: val normalization = wenzelm@60805: map2 (fn (v, sort) => fn (v', _) => (((v', 0), sort), Thm.ctyp_of ctxt (TFree (v, sort)))) wenzelm@60805: vs_original vs_normalized; haftmann@56967: in haftmann@56971: if eq_list (eq_fst (op =)) (vs_normalized, vs_original) haftmann@56971: then (I, ct) wenzelm@59633: else wenzelm@60805: (Thm.instantiate (normalization, []) o Thm.varifyT_global, wenzelm@59633: Thm.cterm_of ctxt (map_types normalize t)) haftmann@56967: end; haftmann@56967: haftmann@56968: fun no_variables_sandwich ctxt ct = haftmann@56968: let wenzelm@59633: val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (Thm.cterm_of ctxt t) wenzelm@59633: | t as Var _ => insert (op aconvc) (Thm.cterm_of ctxt t) haftmann@56968: | _ => I) (Thm.term_of ct) []; haftmann@56968: fun apply_beta var thm = Thm.combination thm (Thm.reflexive var) haftmann@56968: |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false))) haftmann@56968: |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false)); haftmann@56971: in haftmann@56971: if null all_vars haftmann@56971: then (I, ct) haftmann@56971: else (fold apply_beta all_vars, fold_rev Thm.lambda all_vars ct) haftmann@56971: end; haftmann@31125: haftmann@56968: fun simplifier_conv_sandwich ctxt = haftmann@31125: let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@56968: val pre = (#pre o the_thmproc) thy; haftmann@56968: val post = (#post o the_thmproc) thy; haftmann@56968: fun pre_conv ctxt' = haftmann@56968: Simplifier.rewrite (put_simpset pre ctxt') wenzelm@63073: #> trans_conv_rule (Axclass.unoverload_conv ctxt') haftmann@56968: fun post_conv ctxt' = wenzelm@63073: Axclass.overload_conv ctxt' haftmann@56968: #> trans_conv_rule (Simplifier.rewrite (put_simpset post ctxt')) haftmann@56968: in fn ctxt' => pre_conv ctxt' #> pair (post_conv ctxt') end; haftmann@55757: haftmann@56976: fun simplifier_sandwich ctxt = Sandwich.lift (simplifier_conv_sandwich ctxt); haftmann@31125: haftmann@56968: fun value_sandwich ctxt = haftmann@56968: normalized_tfrees_sandwich haftmann@56976: |> Sandwich.chain no_variables_sandwich haftmann@56976: |> Sandwich.chain (simplifier_sandwich ctxt); haftmann@31125: haftmann@55757: fun print_codeproc ctxt = haftmann@31125: let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@31125: val pre = (#pre o the_thmproc) thy; haftmann@31125: val post = (#post o the_thmproc) thy; haftmann@31125: val functrans = (map fst o #functrans o the_thmproc) thy; haftmann@31125: in wenzelm@56334: Pretty.writeln_chunks [ haftmann@31125: Pretty.block [ haftmann@31125: Pretty.str "preprocessing simpset:", haftmann@31125: Pretty.fbrk, wenzelm@59917: Simplifier.pretty_simpset true (put_simpset pre ctxt) haftmann@31125: ], haftmann@31125: Pretty.block [ haftmann@31125: Pretty.str "postprocessing simpset:", haftmann@31125: Pretty.fbrk, wenzelm@59917: Simplifier.pretty_simpset true (put_simpset post ctxt) haftmann@31125: ], haftmann@31125: Pretty.block ( haftmann@31125: Pretty.str "function transformers:" haftmann@31125: :: Pretty.fbrk haftmann@31125: :: (Pretty.fbreaks o map Pretty.str) functrans haftmann@31125: ) haftmann@31125: ] haftmann@31125: end; haftmann@31125: haftmann@55757: fun simple_functrans f ctxt eqns = case f ctxt (map fst eqns) haftmann@31125: of SOME thms' => SOME (map (rpair (forall snd eqns)) thms') haftmann@31125: | NONE => NONE; haftmann@31125: haftmann@31125: haftmann@31125: (** sort algebra and code equation graph types **) haftmann@30010: haftmann@30947: type code_algebra = (sort -> sort) * Sorts.algebra; haftmann@34891: type code_graph = ((string * sort) list * Code.cert) Graph.T; haftmann@30010: haftmann@39604: fun get_node eqngr const = Graph.get_node eqngr const haftmann@39604: handle Graph.UNDEF _ => error ("No such constant in code equation graph: " ^ quote const); haftmann@39604: haftmann@39604: fun cert eqngr = snd o get_node eqngr; haftmann@39604: fun sortargs eqngr = map snd o fst o get_node eqngr; haftmann@30010: fun all eqngr = Graph.keys eqngr; haftmann@30010: haftmann@55757: fun pretty ctxt eqngr = haftmann@55757: let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@55757: in haftmann@55757: AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr) haftmann@55757: |> (map o apfst) (Code.string_of_const thy) wenzelm@59058: |> sort (string_ord o apply2 fst) haftmann@55757: |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert)) haftmann@55757: |> Pretty.chunks haftmann@55757: end; haftmann@30010: haftmann@30010: haftmann@57430: (** simplifier tracing **) haftmann@57430: haftmann@57430: structure Trace_Switch = Generic_Data haftmann@57430: ( haftmann@57430: type T = string list option; haftmann@57430: val empty = SOME []; haftmann@57430: val extend = I; haftmann@59323: fun merge (NONE, _) = NONE haftmann@59323: | merge (_, NONE) = NONE haftmann@57430: | merge (SOME cs1, SOME cs2) = SOME (Library.merge (op =) (cs1, cs2)); haftmann@57430: ); haftmann@57430: haftmann@57430: val trace_none = Trace_Switch.put (SOME []); haftmann@57430: haftmann@57430: val trace_all = Trace_Switch.put NONE; haftmann@57430: haftmann@57430: fun gen_trace_only prep_const raw_cs context = haftmann@57430: let haftmann@57430: val cs = map (prep_const (Context.theory_of context)) raw_cs; haftmann@57430: in Trace_Switch.put (SOME cs) context end; haftmann@57430: haftmann@57430: val trace_only = gen_trace_only (K I); haftmann@57430: val trace_only_ext = gen_trace_only Code.read_const; haftmann@57430: haftmann@57430: fun switch_trace c ctxt = haftmann@57430: let haftmann@57430: val d = Trace_Switch.get (Context.Proof ctxt); haftmann@57430: val switch = case d of NONE => true | SOME cs => member (op =) cs c; haftmann@57430: val _ = if switch haftmann@57430: then tracing ("Preprocessing function equations for " haftmann@57430: ^ Code.string_of_const (Proof_Context.theory_of ctxt) c) haftmann@57430: else (); haftmann@57430: in Config.put simp_trace switch ctxt end; haftmann@57430: haftmann@57430: haftmann@30010: (** the Waisenhaus algorithm **) haftmann@30010: haftmann@30010: (* auxiliary *) haftmann@30010: wenzelm@51685: fun is_proper_class thy = can (Axclass.get_info thy); haftmann@30942: haftmann@30010: fun complete_proper_sort thy = haftmann@30942: Sign.complete_sort thy #> filter (is_proper_class thy); haftmann@30010: haftmann@30029: fun inst_params thy tyco = wenzelm@51685: map (fn (c, _) => Axclass.param_of_inst thy (c, tyco)) wenzelm@51685: o maps (#params o Axclass.get_info thy); haftmann@30010: haftmann@30010: haftmann@30010: (* data structures *) haftmann@30010: haftmann@30010: datatype const = Fun of string | Inst of class * string; haftmann@30010: haftmann@30010: fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2) haftmann@30010: | const_ord (Inst class_tyco1, Inst class_tyco2) = haftmann@30010: prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2) haftmann@30010: | const_ord (Fun _, Inst _) = LESS haftmann@30010: | const_ord (Inst _, Fun _) = GREATER; haftmann@30010: haftmann@30010: type var = const * int; haftmann@30010: haftmann@30010: structure Vargraph = wenzelm@31971: Graph(type key = var val ord = prod_ord const_ord int_ord); haftmann@30010: haftmann@30054: datatype styp = Tyco of string * styp list | Var of var | Free; haftmann@30054: haftmann@30054: fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys) haftmann@30054: | styp_of c_lhs (TFree (v, _)) = case c_lhs haftmann@30054: of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs) haftmann@30054: | NONE => Free; haftmann@30010: haftmann@30054: type vardeps_data = ((string * styp list) list * class list) Vargraph.T haftmann@34891: * (((string * sort) list * Code.cert) Symtab.table haftmann@30054: * (class * string) list); haftmann@30010: haftmann@30054: val empty_vardeps_data : vardeps_data = haftmann@30054: (Vargraph.empty, (Symtab.empty, [])); haftmann@30054: haftmann@30876: haftmann@30054: (* retrieving equations and instances from the background context *) haftmann@30010: haftmann@55364: fun obtain_eqns ctxt eqngr c = haftmann@30010: case try (Graph.get_node eqngr) c haftmann@34891: of SOME (lhs, cert) => ((lhs, []), cert) haftmann@30010: | NONE => let haftmann@55364: val thy = Proof_Context.theory_of ctxt; haftmann@55757: val functrans = (map (fn (_, (_, f)) => f ctxt) haftmann@48075: o #functrans o the_thmproc) thy; haftmann@57430: val cert = Code.get_cert (switch_trace c ctxt) functrans c; haftmann@55364: val (lhs, rhss) = haftmann@55364: Code.typargs_deps_of_cert thy cert; haftmann@34891: in ((lhs, rhss), cert) end; haftmann@30010: haftmann@55757: fun obtain_instance ctxt arities (inst as (class, tyco)) = haftmann@30010: case AList.lookup (op =) arities inst haftmann@30010: of SOME classess => (classess, ([], [])) haftmann@30010: | NONE => let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@30029: val all_classes = complete_proper_sort thy [class]; haftmann@37384: val super_classes = remove (op =) class all_classes; wenzelm@59840: val classess = wenzelm@59840: map (complete_proper_sort thy) wenzelm@59840: (Proof_Context.arity_sorts ctxt tyco [class]); haftmann@30029: val inst_params = inst_params thy tyco all_classes; haftmann@37384: in (classess, (super_classes, inst_params)) end; haftmann@30010: haftmann@30054: haftmann@30054: (* computing instantiations *) haftmann@30054: haftmann@55364: fun add_classes ctxt arities eqngr c_k new_classes vardeps_data = haftmann@30010: let haftmann@30010: val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; haftmann@30010: val diff_classes = new_classes |> subtract (op =) old_classes; haftmann@30010: in if null diff_classes then vardeps_data haftmann@30010: else let wenzelm@44338: val c_ks = Vargraph.immediate_succs (fst vardeps_data) c_k |> insert (op =) c_k; haftmann@30010: in haftmann@30010: vardeps_data haftmann@30010: |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) haftmann@55364: |> fold (fn styp => fold (ensure_typmatch_inst ctxt arities eqngr styp) new_classes) styps haftmann@55364: |> fold (fn c_k => add_classes ctxt arities eqngr c_k diff_classes) c_ks haftmann@30010: end end haftmann@55364: and add_styp ctxt arities eqngr c_k new_tyco_styps vardeps_data = haftmann@30010: let haftmann@37384: val (old_tyco_stypss, classes) = Vargraph.get_node (fst vardeps_data) c_k; haftmann@37384: in if member (op =) old_tyco_stypss new_tyco_styps then vardeps_data haftmann@30010: else haftmann@30010: vardeps_data haftmann@37384: |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps) haftmann@55364: |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes haftmann@30010: end haftmann@55364: and add_dep ctxt arities eqngr c_k c_k' vardeps_data = haftmann@30010: let haftmann@30010: val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; haftmann@30010: in haftmann@30010: vardeps_data haftmann@55364: |> add_classes ctxt arities eqngr c_k' classes haftmann@30010: |> apfst (Vargraph.add_edge (c_k, c_k')) haftmann@30010: end haftmann@55364: and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data = wenzelm@59840: if can (Proof_Context.arity_sorts ctxt tyco) [class] haftmann@30010: then vardeps_data haftmann@55364: |> ensure_inst ctxt arities eqngr (class, tyco) haftmann@30010: |> fold_index (fn (k, styp) => haftmann@55364: ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps haftmann@30010: else vardeps_data (*permissive!*) haftmann@55364: and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = haftmann@30054: if member (op =) insts inst then vardeps_data haftmann@30054: else let haftmann@37384: val (classess, (super_classes, inst_params)) = haftmann@55757: obtain_instance ctxt arities inst; haftmann@30010: in haftmann@30010: vardeps_data haftmann@30054: |> (apsnd o apsnd) (insert (op =) inst) haftmann@30083: |> fold_index (fn (k, _) => haftmann@30083: apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess haftmann@55364: |> fold (fn super_class => ensure_inst ctxt arities eqngr (super_class, tyco)) super_classes haftmann@55364: |> fold (ensure_fun ctxt arities eqngr) inst_params haftmann@30010: |> fold_index (fn (k, classes) => haftmann@55364: add_classes ctxt arities eqngr (Inst (class, tyco), k) classes haftmann@37384: #> fold (fn super_class => haftmann@55364: add_dep ctxt arities eqngr (Inst (super_class, tyco), k) haftmann@37384: (Inst (class, tyco), k)) super_classes haftmann@30010: #> fold (fn inst_param => haftmann@55364: add_dep ctxt arities eqngr (Fun inst_param, k) haftmann@30010: (Inst (class, tyco), k) haftmann@30010: ) inst_params haftmann@30010: ) classess haftmann@30010: end haftmann@55364: and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data = haftmann@30054: vardeps_data haftmann@55364: |> add_styp ctxt arities eqngr c_k tyco_styps haftmann@55364: | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data = haftmann@30054: vardeps_data haftmann@55364: |> add_dep ctxt arities eqngr c_k c_k' haftmann@55364: | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data = haftmann@30054: vardeps_data haftmann@55364: and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data = haftmann@30054: vardeps_data haftmann@55364: |> ensure_fun ctxt arities eqngr c' haftmann@30054: |> fold_index (fn (k, styp) => haftmann@55364: ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps haftmann@55364: and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) = haftmann@30054: if Symtab.defined eqntab c then vardeps_data haftmann@30054: else let haftmann@55364: val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c; haftmann@30054: val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss; haftmann@30010: in haftmann@30010: vardeps_data haftmann@30054: |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) haftmann@30083: |> fold_index (fn (k, _) => haftmann@30083: apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs haftmann@55364: |> fold_index (fn (k, (_, sort)) => add_classes ctxt arities eqngr (Fun c, k) haftmann@55364: (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs haftmann@55364: |> fold (ensure_rhs ctxt arities eqngr) rhss' haftmann@30054: end; haftmann@30010: haftmann@30010: haftmann@30010: (* applying instantiations *) haftmann@30010: haftmann@55757: fun dicts_of ctxt (proj_sort, algebra) (T, sort) = haftmann@30010: let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@62538: fun class_relation _ (x, _) _ = x; wenzelm@36102: fun type_constructor (tyco, _) xs class = haftmann@30054: inst_params thy tyco (Sorts.complete_sort algebra [class]) haftmann@30054: @ (maps o maps) fst xs; haftmann@30010: fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); haftmann@30010: in wenzelm@32795: flat (Sorts.of_sort_derivation algebra wenzelm@36102: { class_relation = K class_relation, type_constructor = type_constructor, haftmann@30010: type_variable = type_variable } (T, proj_sort sort) haftmann@30010: handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) haftmann@30010: end; haftmann@30010: haftmann@55757: fun add_arity ctxt vardeps (class, tyco) = haftmann@33063: AList.default (op =) ((class, tyco), haftmann@34891: map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) haftmann@55757: (Sign.arity_number (Proof_Context.theory_of ctxt) tyco)); haftmann@30010: haftmann@55757: fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) = haftmann@30058: if can (Graph.get_node eqngr) c then (rhss, eqngr) haftmann@30058: else let haftmann@55757: val thy = Proof_Context.theory_of ctxt; haftmann@30010: val lhs = map_index (fn (k, (v, _)) => haftmann@30010: (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; haftmann@49971: val cert = proto_cert haftmann@49971: |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs) haftmann@49971: |> Code.conclude_cert; haftmann@35224: val (vs, rhss') = Code.typargs_deps_of_cert thy cert; haftmann@34891: val eqngr' = Graph.new_node (c, (vs, cert)) eqngr; haftmann@30054: in (map (pair c) rhss' @ rhss, eqngr') end; haftmann@30010: haftmann@55757: fun extend_arities_eqngr raw_ctxt cs ts (arities, (eqngr : code_graph)) = haftmann@30010: let haftmann@55757: val thy = Proof_Context.theory_of raw_ctxt; haftmann@55364: val {pre, ...} = the_thmproc thy; haftmann@55757: val ctxt = put_simpset pre raw_ctxt; haftmann@30942: val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) => haftmann@30942: insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts []; haftmann@30054: val (vardeps, (eqntab, insts)) = empty_vardeps_data haftmann@55364: |> fold (ensure_fun ctxt arities eqngr) cs haftmann@55364: |> fold (ensure_rhs ctxt arities eqngr) cs_rhss; haftmann@55757: val arities' = fold (add_arity ctxt vardeps) insts arities; wenzelm@61262: val algebra = Sorts.subalgebra (Context.Theory thy) (is_proper_class thy) haftmann@30064: (AList.lookup (op =) arities') (Sign.classes_of thy); haftmann@55757: val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr); haftmann@55757: fun deps_of (c, rhs) = c :: maps (dicts_of ctxt algebra) haftmann@32873: (rhs ~~ sortargs eqngr' c); haftmann@30054: val eqngr'' = fold (fn (c, rhs) => fold haftmann@30054: (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr'; haftmann@30942: in (algebra, (arities', eqngr'')) end; haftmann@30010: haftmann@30010: haftmann@31125: (** store for preprocessed arities and code equations **) haftmann@30010: haftmann@34173: structure Wellsorted = Code_Data haftmann@30010: ( haftmann@30947: type T = ((string * class) * sort list) list * code_graph; haftmann@30010: val empty = ([], Graph.empty); haftmann@30010: ); haftmann@30010: haftmann@30010: haftmann@31125: (** retrieval and evaluation interfaces **) haftmann@30010: haftmann@63157: (* haftmann@63157: naming conventions haftmann@63157: * evaluator "eval" is either haftmann@63157: * conversion "conv" haftmann@63157: * value computation "comp" haftmann@63157: * "evaluation" is a lifting of an evaluator haftmann@63157: *) haftmann@63157: wenzelm@60697: fun obtain ignore_cache ctxt consts ts = apsnd snd wenzelm@60697: (Wellsorted.change_yield (if ignore_cache then NONE else SOME (Proof_Context.theory_of ctxt)) wenzelm@60697: (extend_arities_eqngr ctxt consts ts)); haftmann@38670: haftmann@63157: fun dynamic_evaluation eval ctxt t = haftmann@30942: let haftmann@30947: val consts = fold_aterms haftmann@56968: (fn Const (c, _) => insert (op =) c | _ => I) t []; wenzelm@60697: val (algebra, eqngr) = obtain false ctxt consts [t]; haftmann@56968: in eval algebra eqngr t end; haftmann@30947: haftmann@63157: fun static_evaluation ctxt consts eval = haftmann@63157: let haftmann@63157: val (algebra, eqngr) = obtain true ctxt consts []; haftmann@63157: in eval { algebra = algebra, eqngr = eqngr } end; haftmann@63157: haftmann@56968: fun dynamic_conv ctxt conv = haftmann@63157: Sandwich.conversion (value_sandwich ctxt) haftmann@63157: (dynamic_evaluation conv) ctxt; haftmann@56968: haftmann@56968: fun dynamic_value ctxt lift_postproc evaluator = haftmann@63157: Sandwich.computation (value_sandwich ctxt) lift_postproc haftmann@63157: (dynamic_evaluation evaluator) ctxt; haftmann@31125: haftmann@56973: fun static_conv { ctxt, consts } conv = haftmann@63157: Sandwich.conversion (value_sandwich ctxt) haftmann@63157: (static_evaluation ctxt consts conv); haftmann@38670: haftmann@63157: fun static_value { ctxt, lift_postproc, consts } comp = haftmann@63157: Sandwich.computation (value_sandwich ctxt) lift_postproc haftmann@63157: (static_evaluation ctxt consts comp); haftmann@39475: haftmann@31125: haftmann@31125: (** setup **) haftmann@31125: haftmann@59323: val _ = Theory.setup ( haftmann@31125: let haftmann@31125: fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); haftmann@56929: fun add_del_attribute_parser process = haftmann@56929: Attrib.add_del (mk_attribute (process Simplifier.add_simp)) haftmann@56929: (mk_attribute (process Simplifier.del_simp)); haftmann@31125: in haftmann@59323: Attrib.setup @{binding code_unfold} (add_del_attribute_parser process_unfold) haftmann@59323: "preprocessing equations for code generator" haftmann@59323: #> Attrib.setup @{binding code_post} (add_del_attribute_parser process_post) haftmann@59323: "postprocessing equations for code generator" haftmann@59323: #> Attrib.setup @{binding code_abbrev} (add_del_attribute_parser process_abbrev) haftmann@59323: "post- and preprocessing equations for code generator" haftmann@59323: #> Attrib.setup @{binding code_preproc_trace} haftmann@59323: ((Scan.lift (Args.$$$ "off" >> K trace_none) haftmann@59323: || (Scan.lift (Args.$$$ "only" |-- Args.colon |-- Scan.repeat1 Parse.term)) haftmann@59323: >> trace_only_ext haftmann@59323: || Scan.succeed trace_all) haftmann@59323: >> (Thm.declaration_attribute o K)) "tracing of the code generator preprocessor" haftmann@59323: end); haftmann@31125: haftmann@31125: val _ = wenzelm@59936: Outer_Syntax.command @{command_keyword print_codeproc} "print code preprocessor setup" wenzelm@60097: (Scan.succeed (Toplevel.keep (print_codeproc o Toplevel.context_of))); haftmann@30010: haftmann@30010: end; (*struct*)