haftmann@24219: (* Title: Pure/Isar/code.ML haftmann@24219: ID: $Id$ haftmann@24219: Author: Florian Haftmann, TU Muenchen haftmann@24219: haftmann@24219: Abstract executable content of theory. Management of data dependent on haftmann@25485: executable content. Cache assumes non-concurrent processing of a single theory. haftmann@24219: *) haftmann@24219: haftmann@24219: signature CODE = haftmann@24219: sig haftmann@24624: val add_func: thm -> theory -> theory haftmann@28350: val add_nonlinear_func: thm -> theory -> theory haftmann@24624: val add_liberal_func: thm -> theory -> theory haftmann@24624: val add_default_func: thm -> theory -> theory haftmann@24624: val add_default_func_attr: Attrib.src haftmann@24219: val del_func: thm -> theory -> theory haftmann@26021: val del_funcs: string -> theory -> theory haftmann@28350: val add_funcl: string * (thm * bool) list Susp.T -> theory -> theory haftmann@27557: val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory haftmann@27557: val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory haftmann@24219: val add_inline: thm -> theory -> theory haftmann@24219: val del_inline: thm -> theory -> theory haftmann@24219: val add_post: thm -> theory -> theory haftmann@24219: val del_post: thm -> theory -> theory haftmann@27609: val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory haftmann@27557: val del_functrans: string -> theory -> theory haftmann@24423: val add_datatype: (string * typ) list -> theory -> theory haftmann@24423: val add_datatype_cmd: string list -> theory -> theory haftmann@25485: val type_interpretation: haftmann@25485: (string * ((string * sort) list * (string * typ list) list) haftmann@25485: -> theory -> theory) -> theory -> theory haftmann@24844: val add_case: thm -> theory -> theory haftmann@24844: val add_undefined: string -> theory -> theory haftmann@27675: val purge_data: theory -> theory haftmann@24219: haftmann@24219: val coregular_algebra: theory -> Sorts.algebra haftmann@24219: val operational_algebra: theory -> (sort -> sort) * Sorts.algebra haftmann@28350: val these_funcs: theory -> string -> (thm * bool) list haftmann@24219: val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list) haftmann@24423: val get_datatype_of_constr: theory -> string -> string option haftmann@24844: val get_case_data: theory -> string -> (int * string list) option haftmann@24844: val is_undefined: theory -> string -> bool haftmann@26970: val default_typ: theory -> string -> (string * sort) list * typ haftmann@24219: haftmann@24219: val preprocess_conv: cterm -> thm haftmann@24837: val preprocess_term: theory -> term -> term haftmann@24219: val postprocess_conv: cterm -> thm haftmann@24837: val postprocess_term: theory -> term -> term haftmann@24219: haftmann@24219: val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory haftmann@24219: haftmann@24219: val print_codesetup: theory -> unit haftmann@24219: end; haftmann@24219: haftmann@24219: signature CODE_DATA_ARGS = haftmann@24219: sig haftmann@24219: type T haftmann@24219: val empty: T haftmann@27609: val purge: theory -> string list -> T -> T haftmann@24219: end; haftmann@24219: haftmann@24219: signature CODE_DATA = haftmann@24219: sig haftmann@24219: type T haftmann@24219: val get: theory -> T haftmann@24219: val change: theory -> (T -> T) -> T haftmann@24219: val change_yield: theory -> (T -> 'a * T) -> 'a * T haftmann@24219: end; haftmann@24219: haftmann@24219: signature PRIVATE_CODE = haftmann@24219: sig haftmann@24219: include CODE haftmann@27609: val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T) haftmann@27609: -> serial haftmann@24219: val get_data: serial * ('a -> Object.T) * (Object.T -> 'a) haftmann@24219: -> theory -> 'a haftmann@24219: val change_data: serial * ('a -> Object.T) * (Object.T -> 'a) haftmann@24219: -> theory -> ('a -> 'a) -> 'a haftmann@24219: val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a) haftmann@24219: -> theory -> ('a -> 'b * 'a) -> 'b * 'a haftmann@24219: end; haftmann@24219: haftmann@24219: structure Code : PRIVATE_CODE = haftmann@24219: struct haftmann@24219: haftmann@25312: (** code attributes **) haftmann@25312: haftmann@25312: structure CodeAttr = TheoryDataFun ( haftmann@25312: type T = (string * (Args.T list -> attribute * Args.T list)) list; haftmann@25312: val empty = []; haftmann@25312: val copy = I; haftmann@25312: val extend = I; haftmann@26970: fun merge _ = AList.merge (op = : string * string -> bool) (K true); haftmann@25312: ); haftmann@24219: haftmann@25312: fun add_attribute (attr as (name, _)) = haftmann@25312: let haftmann@25312: fun add_parser ("", parser) attrs = attrs @ [("", parser)] haftmann@25312: | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs; haftmann@25312: fun error "" = error ("Code attribute already declared") haftmann@25312: | error name = error ("Code attribute " ^ name ^ " already declared") haftmann@25312: in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name haftmann@25312: then error name else add_parser attr attrs) haftmann@25312: end; haftmann@25312: haftmann@25312: val _ = haftmann@25312: let haftmann@25312: val code_attr = Attrib.syntax (Scan.peek (fn context => haftmann@25312: List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context))))); haftmann@25312: in wenzelm@26463: Context.>> (Context.map_theory wenzelm@26463: (Attrib.add_attributes wenzelm@26463: [("code", code_attr, "declare theorems for code generation")])) haftmann@25312: end; haftmann@25312: haftmann@25312: haftmann@28143: (** logical and syntactical specification of executable code **) haftmann@24219: haftmann@28350: (* defining equations with linear flag, default flag and lazy theorems *) haftmann@24219: haftmann@24219: fun pretty_lthms ctxt r = case Susp.peek r haftmann@28350: of SOME thms => map (ProofContext.pretty_thm ctxt o fst) thms haftmann@24219: | NONE => [Pretty.str "[...]"]; haftmann@24219: haftmann@24219: fun certificate thy f r = haftmann@24219: case Susp.peek r haftmann@28350: of SOME thms => (Susp.value o burrow_fst (f thy)) thms haftmann@24844: | NONE => let haftmann@24844: val thy_ref = Theory.check_thy thy; haftmann@28350: in Susp.delay (fn () => (burrow_fst (f (Theory.deref thy_ref)) o Susp.force) r) end; haftmann@24219: haftmann@28350: fun add_drop_redundant (thm, linear) thms = haftmann@24219: let haftmann@24219: val thy = Thm.theory_of_thm thm; haftmann@24219: val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; haftmann@24219: val args = args_of thm; haftmann@28350: fun matches_args args' = length args <= length args' andalso haftmann@28350: Pattern.matchess thy (args, curry Library.take (length args) args'); haftmann@28359: fun drop (thm', linear') = if (linear orelse not linear') haftmann@28359: andalso matches_args (args_of thm') then haftmann@28359: (warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); true) haftmann@28350: else false; haftmann@28350: in (thm, linear) :: filter_out drop thms end; haftmann@24219: haftmann@28350: fun add_thm _ thm (false, thms) = (false, Susp.map_force (add_drop_redundant thm) thms) haftmann@28350: | add_thm true thm (true, thms) = (true, Susp.map_force (fn thms => thms @ [thm]) thms) haftmann@28143: | add_thm false thm (true, thms) = (false, Susp.value [thm]); haftmann@24219: haftmann@28143: fun add_lthms lthms _ = (false, lthms); haftmann@25312: haftmann@28350: fun del_thm thm = (apsnd o Susp.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true)); haftmann@25312: haftmann@28143: fun merge_defthms ((true, _), defthms2) = defthms2 haftmann@28143: | merge_defthms (defthms1 as (false, _), (true, _)) = defthms1 haftmann@28143: | merge_defthms ((false, _), defthms2 as (false, _)) = defthms2; haftmann@24219: haftmann@24219: haftmann@28143: (* syntactic datatypes *) haftmann@24219: haftmann@24219: val eq_string = op = : string * string -> bool; haftmann@28143: haftmann@24219: fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = haftmann@24219: gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2) haftmann@24423: andalso gen_eq_set (eq_fst eq_string) (cs1, cs2); haftmann@28143: haftmann@27582: fun merge_dtyps (tabs as (tab1, tab2)) = haftmann@24219: let haftmann@27582: fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2; haftmann@27582: in Symtab.join join tabs end; haftmann@24219: haftmann@28143: haftmann@28143: (* specification data *) haftmann@28143: haftmann@24219: datatype spec = Spec of { haftmann@28350: funcs: (bool * (thm * bool) list Susp.T) Symtab.table, haftmann@24844: dtyps: ((string * sort) list * (string * typ list) list) Symtab.table, haftmann@24844: cases: (int * string list) Symtab.table * unit Symtab.table haftmann@24219: }; haftmann@24219: haftmann@24844: fun mk_spec (funcs, (dtyps, cases)) = haftmann@24844: Spec { funcs = funcs, dtyps = dtyps, cases = cases }; haftmann@24844: fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) = haftmann@24844: mk_spec (f (funcs, (dtyps, cases))); haftmann@27609: fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = (cases1, undefs1) }, haftmann@27609: Spec { funcs = funcs2, dtyps = dtyps2, cases = (cases2, undefs2) }) = haftmann@24219: let haftmann@28143: val funcs = Symtab.join (K merge_defthms) (funcs1, funcs2); haftmann@27582: val dtyps = merge_dtyps (dtyps1, dtyps2); haftmann@27609: val cases = (Symtab.merge (K true) (cases1, cases2), haftmann@27609: Symtab.merge (K true) (undefs1, undefs2)); haftmann@27557: in mk_spec (funcs, (dtyps, cases)) end; haftmann@24219: haftmann@25312: haftmann@25312: (* pre- and postprocessor *) haftmann@25312: haftmann@25312: datatype thmproc = Thmproc of { haftmann@27557: pre: MetaSimplifier.simpset, haftmann@27557: post: MetaSimplifier.simpset, haftmann@27609: functrans: (string * (serial * (theory -> thm list -> thm list option))) list haftmann@25312: }; haftmann@25312: haftmann@27557: fun mk_thmproc ((pre, post), functrans) = haftmann@27557: Thmproc { pre = pre, post = post, functrans = functrans }; haftmann@27557: fun map_thmproc f (Thmproc { pre, post, functrans }) = haftmann@27557: mk_thmproc (f ((pre, post), functrans)); haftmann@27557: fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 }, haftmann@27557: Thmproc { pre = pre2, post = post2, functrans = functrans2 }) = haftmann@25312: let haftmann@27557: val pre = MetaSimplifier.merge_ss (pre1, pre2); haftmann@27557: val post = MetaSimplifier.merge_ss (post1, post2); haftmann@27557: val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2); haftmann@27557: in mk_thmproc ((pre, post), functrans) end; haftmann@25312: haftmann@24219: datatype exec = Exec of { haftmann@24219: thmproc: thmproc, haftmann@24219: spec: spec haftmann@24219: }; haftmann@24219: haftmann@28143: haftmann@28143: (* code setup data *) haftmann@28143: haftmann@24219: fun mk_exec (thmproc, spec) = haftmann@24219: Exec { thmproc = thmproc, spec = spec }; haftmann@24219: fun map_exec f (Exec { thmproc = thmproc, spec = spec }) = haftmann@24219: mk_exec (f (thmproc, spec)); haftmann@27557: fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 }, haftmann@24219: Exec { thmproc = thmproc2, spec = spec2 }) = haftmann@24219: let haftmann@27557: val thmproc = merge_thmproc (thmproc1, thmproc2); haftmann@27557: val spec = merge_spec (spec1, spec2); haftmann@27557: in mk_exec (thmproc, spec) end; haftmann@27557: val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []), haftmann@24844: mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty)))); haftmann@24219: haftmann@25312: fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x; haftmann@24219: fun the_spec (Exec { spec = Spec x, ...}) = x; haftmann@24219: val the_funcs = #funcs o the_spec; haftmann@24219: val the_dtyps = #dtyps o the_spec; haftmann@24844: val the_cases = #cases o the_spec; haftmann@24219: val map_thmproc = map_exec o apfst o map_thmproc; haftmann@24219: val map_funcs = map_exec o apsnd o map_spec o apfst; haftmann@24844: val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst; haftmann@24844: val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd; haftmann@24219: haftmann@24219: haftmann@24219: (* data slots dependent on executable content *) haftmann@24219: haftmann@24219: (*private copy avoids potential conflict of table exceptions*) haftmann@24219: structure Datatab = TableFun(type key = int val ord = int_ord); haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: type kind = { haftmann@24219: empty: Object.T, haftmann@27609: purge: theory -> string list -> Object.T -> Object.T haftmann@24219: }; haftmann@24219: haftmann@24219: val kinds = ref (Datatab.empty: kind Datatab.table); haftmann@24219: val kind_keys = ref ([]: serial list); haftmann@24219: haftmann@24219: fun invoke f k = case Datatab.lookup (! kinds) k haftmann@24219: of SOME kind => f kind haftmann@24219: | NONE => sys_error "Invalid code data identifier"; haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@27609: fun declare_data empty purge = haftmann@24219: let haftmann@24219: val k = serial (); haftmann@27609: val kind = {empty = empty, purge = purge}; haftmann@24219: val _ = change kinds (Datatab.update (k, kind)); haftmann@24219: val _ = change kind_keys (cons k); haftmann@24219: in k end; haftmann@24219: haftmann@27609: fun invoke_init k = invoke (fn kind => #empty kind) k; haftmann@24219: haftmann@27609: fun invoke_purge_all thy cs = haftmann@24219: fold (fn k => Datatab.map_entry k haftmann@27609: (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys); haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: haftmann@25312: (** theory store **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: type data = Object.T Datatab.table; haftmann@27609: val empty_data = Datatab.empty : data; haftmann@24219: haftmann@24219: structure CodeData = TheoryDataFun haftmann@24219: ( haftmann@24219: type T = exec * data ref; haftmann@27609: val empty = (empty_exec, ref empty_data); haftmann@24219: fun copy (exec, data) = (exec, ref (! data)); haftmann@24219: val extend = copy; haftmann@24219: fun merge pp ((exec1, data1), (exec2, data2)) = haftmann@27609: (merge_exec (exec1, exec2), ref empty_data); haftmann@24219: ); haftmann@24219: wenzelm@26463: val _ = Context.>> (Context.map_theory CodeData.init); haftmann@24219: haftmann@24219: fun thy_data f thy = f ((snd o CodeData.get) thy); haftmann@24219: haftmann@24219: fun get_ensure_init kind data_ref = haftmann@24219: case Datatab.lookup (! data_ref) kind haftmann@24219: of SOME x => x haftmann@27609: | NONE => let val y = invoke_init kind haftmann@24219: in (change data_ref (Datatab.update (kind, y)); y) end; haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24219: (* access to executable content *) haftmann@24219: haftmann@24844: val the_exec = fst o CodeData.get; haftmann@24219: haftmann@27983: fun complete_class_params thy cs = haftmann@27983: fold (fn c => case AxClass.inst_of_param thy c haftmann@27983: of NONE => insert (op =) c haftmann@27983: | SOME (c', _) => insert (op =) c' #> insert (op =) c) cs []; haftmann@27983: haftmann@24219: fun map_exec_purge touched f thy = haftmann@27609: CodeData.map (fn (exec, data) => (f exec, ref (case touched haftmann@27983: of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data) haftmann@27609: | NONE => empty_data))) thy; haftmann@24219: haftmann@27675: val purge_data = (CodeData.map o apsnd) (K (ref empty_data)); haftmann@27675: haftmann@24219: haftmann@24219: (* access to data dependent on abstract executable content *) haftmann@24219: haftmann@24219: fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest); haftmann@24219: haftmann@24219: fun change_data (kind, mk, dest) = haftmann@24219: let haftmann@24219: fun chnge data_ref f = haftmann@24219: let haftmann@24219: val data = get_ensure_init kind data_ref; haftmann@24219: val data' = f (dest data); haftmann@24219: in (change data_ref (Datatab.update (kind, mk data')); data') end; haftmann@24219: in thy_data chnge end; haftmann@24219: haftmann@24219: fun change_yield_data (kind, mk, dest) = haftmann@24219: let haftmann@24219: fun chnge data_ref f = haftmann@24219: let haftmann@24219: val data = get_ensure_init kind data_ref; haftmann@24219: val (x, data') = f (dest data); haftmann@24219: in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end; haftmann@24219: in thy_data chnge end; haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: haftmann@24219: (* print executable content *) haftmann@24219: haftmann@24219: fun print_codesetup thy = haftmann@24219: let haftmann@24219: val ctxt = ProofContext.init thy; haftmann@24844: val exec = the_exec thy; haftmann@28143: fun pretty_func (s, (_, lthms)) = haftmann@24219: (Pretty.block o Pretty.fbreaks) ( haftmann@28143: Pretty.str s :: pretty_lthms ctxt lthms haftmann@24219: ); haftmann@24219: fun pretty_dtyp (s, []) = haftmann@24219: Pretty.str s haftmann@24219: | pretty_dtyp (s, cos) = haftmann@24219: (Pretty.block o Pretty.breaks) ( haftmann@24219: Pretty.str s haftmann@24219: :: Pretty.str "=" haftmann@24219: :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c haftmann@24219: | (c, tys) => haftmann@24219: (Pretty.block o Pretty.breaks) haftmann@28054: (Pretty.str (Code_Unit.string_of_const thy c) wenzelm@26947: :: Pretty.str "of" wenzelm@26947: :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos) haftmann@24219: ); haftmann@27557: val pre = (#pre o the_thmproc) exec; haftmann@27557: val post = (#post o the_thmproc) exec; haftmann@27557: val functrans = (map fst o #functrans o the_thmproc) exec; haftmann@27609: val funcs = the_funcs exec haftmann@24423: |> Symtab.dest haftmann@28054: |> (map o apfst) (Code_Unit.string_of_const thy) haftmann@24219: |> sort (string_ord o pairself fst); haftmann@24219: val dtyps = the_dtyps exec haftmann@24219: |> Symtab.dest wenzelm@26947: |> map (fn (dtco, (vs, cos)) => wenzelm@26947: (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos)) haftmann@24219: |> sort (string_ord o pairself fst) haftmann@24219: in haftmann@24219: (Pretty.writeln o Pretty.chunks) [ haftmann@24219: Pretty.block ( haftmann@24219: Pretty.str "defining equations:" haftmann@24219: :: Pretty.fbrk haftmann@27609: :: (Pretty.fbreaks o map pretty_func) funcs haftmann@24219: ), haftmann@27557: Pretty.block [ haftmann@27557: Pretty.str "preprocessing simpset:", haftmann@27557: Pretty.fbrk, haftmann@27557: MetaSimplifier.pretty_ss pre haftmann@27557: ], haftmann@27557: Pretty.block [ haftmann@27557: Pretty.str "postprocessing simpset:", haftmann@27557: Pretty.fbrk, haftmann@27557: MetaSimplifier.pretty_ss post haftmann@27557: ], haftmann@24219: Pretty.block ( haftmann@27609: Pretty.str "function transformers:" haftmann@24219: :: Pretty.fbrk haftmann@27557: :: (Pretty.fbreaks o map Pretty.str) functrans haftmann@25968: ), haftmann@25968: Pretty.block ( haftmann@24219: Pretty.str "datatypes:" haftmann@24219: :: Pretty.fbrk haftmann@24219: :: (Pretty.fbreaks o map pretty_dtyp) dtyps haftmann@24219: ) haftmann@24219: ] haftmann@24219: end; haftmann@24219: haftmann@24219: haftmann@24219: haftmann@24219: (** theorem transformation and certification **) haftmann@24219: haftmann@26970: fun const_of thy = dest_Const o fst o strip_comb o fst o Logic.dest_equals haftmann@26970: o ObjectLogic.drop_judgment thy o Thm.plain_prop_of; haftmann@26970: haftmann@26970: fun const_of_func thy = AxClass.unoverload_const thy o const_of thy; haftmann@26970: haftmann@24219: fun common_typ_funcs [] = [] haftmann@24219: | common_typ_funcs [thm] = [thm] haftmann@26970: | common_typ_funcs (thms as thm :: _) = (*FIXME is too general*) haftmann@24219: let haftmann@24219: val thy = Thm.theory_of_thm thm; haftmann@24219: fun incr_thm thm max = haftmann@24219: let haftmann@24219: val thm' = incr_indexes max thm; haftmann@24219: val max' = Thm.maxidx_of thm' + 1; haftmann@24219: in (thm', max') end; haftmann@24219: val (thms', maxidx) = fold_map incr_thm thms 0; haftmann@26970: val ty1 :: tys = map (snd o const_of thy) thms'; haftmann@24219: fun unify ty env = Sign.typ_unify thy (ty1, ty) env haftmann@24219: handle Type.TUNIFY => haftmann@24219: error ("Type unificaton failed, while unifying defining equations\n" haftmann@24219: ^ (cat_lines o map Display.string_of_thm) thms haftmann@24219: ^ "\nwith types\n" haftmann@28054: ^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys)); haftmann@24219: val (env, _) = fold unify tys (Vartab.empty, maxidx) haftmann@24219: val instT = Vartab.fold (fn (x_i, (sort, ty)) => haftmann@24219: cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; haftmann@24219: in map (Thm.instantiate (instT, [])) thms' end; haftmann@24219: haftmann@24219: fun certify_const thy const thms = haftmann@24219: let haftmann@24423: fun cert thm = if const = const_of_func thy thm haftmann@24219: then thm else error ("Wrong head of defining equation,\nexpected constant " haftmann@28054: ^ Code_Unit.string_of_const thy const ^ "\n" ^ Display.string_of_thm thm) haftmann@24219: in map cert thms end; haftmann@24219: haftmann@24219: haftmann@24219: haftmann@24219: (** operational sort algebra and class discipline **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: fun aggr_neutr f y [] = y haftmann@24219: | aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs; haftmann@24219: haftmann@24219: fun aggregate f [] = NONE haftmann@24219: | aggregate f (x::xs) = SOME (aggr_neutr f x xs); haftmann@24219: haftmann@25462: fun inter_sorts algebra = haftmann@25462: aggregate (map2 (curry (Sorts.inter_sort algebra))); haftmann@24219: haftmann@24219: fun specific_constraints thy (class, tyco) = haftmann@24219: let haftmann@24219: val vs = Name.invents Name.context "" (Sign.arity_number thy tyco); wenzelm@24969: val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class; haftmann@24837: val funcs = classparams haftmann@25597: |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco)) haftmann@24844: |> map (Symtab.lookup ((the_funcs o the_exec) thy)) haftmann@28350: |> (map o Option.map) (map fst o Susp.force o snd) haftmann@24219: |> maps these haftmann@28143: |> map (Thm.transfer thy); haftmann@24423: fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys haftmann@24423: | sorts_of tys = map (snd o dest_TVar) tys; haftmann@26970: val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs; haftmann@24219: in sorts end; haftmann@24219: haftmann@25462: fun weakest_constraints thy algebra (class, tyco) = haftmann@24219: let haftmann@25462: val all_superclasses = Sorts.complete_sort algebra [class]; haftmann@25462: in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses) haftmann@24219: of SOME sorts => sorts haftmann@25462: | NONE => Sorts.mg_domain algebra tyco [class] haftmann@24219: end; haftmann@24219: haftmann@25462: fun strongest_constraints thy algebra (class, tyco) = haftmann@24219: let haftmann@24219: val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class]; haftmann@24219: val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses; haftmann@25462: in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses) haftmann@24219: of SOME sorts => sorts haftmann@24219: | NONE => replicate haftmann@25462: (Sign.arity_number thy tyco) (Sorts.minimize_sort algebra (Sorts.all_classes algebra)) haftmann@25462: end; haftmann@25462: haftmann@25462: fun get_algebra thy (class, tyco) = haftmann@25462: let haftmann@25462: val base_algebra = Sign.classes_of thy; haftmann@25462: in if can (Sorts.mg_domain base_algebra tyco) [class] haftmann@25462: then base_algebra haftmann@25462: else let haftmann@25462: val superclasses = Sorts.super_classes base_algebra class; haftmann@25462: val sorts = inter_sorts base_algebra haftmann@25462: (map_filter (fn class => try (Sorts.mg_domain base_algebra tyco) [class]) superclasses) haftmann@25462: |> the_default (replicate (Sign.arity_number thy tyco) []) haftmann@25462: in haftmann@25462: base_algebra wenzelm@26947: |> Sorts.add_arities (Syntax.pp_global thy) (tyco, [(class, sorts)]) haftmann@25462: end haftmann@24219: end; haftmann@24219: haftmann@24837: fun gen_classparam_typ constr thy class (c, tyco) = haftmann@24219: let haftmann@25462: val algebra = get_algebra thy (class, tyco); wenzelm@24969: val cs = these (try (#params o AxClass.get_info thy) class); haftmann@25462: val SOME ty = AList.lookup (op =) cs c; wenzelm@24969: val sort_args = Name.names (Name.declare Name.aT Name.context) Name.aT haftmann@25462: (constr thy algebra (class, tyco)); haftmann@24219: val ty_inst = Type (tyco, map TFree sort_args); haftmann@24219: in Logic.varifyT (map_type_tfree (K ty_inst) ty) end; haftmann@24219: haftmann@24219: fun retrieve_algebra thy operational = wenzelm@26947: Sorts.subalgebra (Syntax.pp_global thy) operational haftmann@25462: (weakest_constraints thy (Sign.classes_of thy)) haftmann@24219: (Sign.classes_of thy); haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24219: fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd; haftmann@24219: fun operational_algebra thy = haftmann@24219: let haftmann@24219: fun add_iff_operational class = wenzelm@24928: can (AxClass.get_info thy) class ? cons class; haftmann@24219: val operational_classes = fold add_iff_operational (Sign.all_classes thy) [] haftmann@24219: in retrieve_algebra thy (member (op =) operational_classes) end; haftmann@24219: haftmann@24837: val classparam_weakest_typ = gen_classparam_typ weakest_constraints; haftmann@24837: val classparam_strongest_typ = gen_classparam_typ strongest_constraints; haftmann@24219: haftmann@24219: fun assert_func_typ thm = haftmann@24219: let haftmann@24219: val thy = Thm.theory_of_thm thm; haftmann@24837: fun check_typ_classparam tyco (c, thm) = haftmann@24219: let haftmann@24423: val SOME class = AxClass.class_of_param thy c; haftmann@26970: val (_, ty) = const_of thy thm; haftmann@24837: val ty_decl = classparam_weakest_typ thy class (c, tyco); haftmann@24837: val ty_strongest = classparam_strongest_typ thy class (c, tyco); haftmann@24219: fun constrain thm = haftmann@24219: let haftmann@24219: val max = Thm.maxidx_of thm + 1; haftmann@24219: val ty_decl' = Logic.incr_tvar max ty_decl; haftmann@26970: val (_, ty') = const_of thy thm; haftmann@24219: val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max); haftmann@24219: val instT = Vartab.fold (fn (x_i, (sort, ty)) => haftmann@24219: cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; haftmann@24219: in Thm.instantiate (instT, []) thm end; haftmann@24219: in if Sign.typ_instance thy (ty_strongest, ty) haftmann@24219: then if Sign.typ_instance thy (ty, ty_decl) haftmann@24219: then thm haftmann@28054: else (warning ("Constraining type\n" ^ Code_Unit.string_of_typ thy ty haftmann@24219: ^ "\nof defining equation\n" wenzelm@26928: ^ Display.string_of_thm thm haftmann@24219: ^ "\nto permitted most general type\n" haftmann@28054: ^ Code_Unit.string_of_typ thy ty_decl); haftmann@24219: constrain thm) haftmann@28054: else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty haftmann@24219: ^ "\nof defining equation\n" wenzelm@26928: ^ Display.string_of_thm thm haftmann@24219: ^ "\nis incompatible with permitted least general type\n" haftmann@28054: ^ Code_Unit.string_of_typ thy ty_strongest) haftmann@24423: end; haftmann@24423: fun check_typ_fun (c, thm) = haftmann@24219: let haftmann@26970: val (_, ty) = const_of thy thm; haftmann@24219: val ty_decl = Sign.the_const_type thy c; haftmann@24219: in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) haftmann@24219: then thm haftmann@28054: else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty haftmann@24219: ^ "\nof defining equation\n" wenzelm@26928: ^ Display.string_of_thm thm haftmann@24219: ^ "\nis incompatible with declared function type\n" haftmann@28054: ^ Code_Unit.string_of_typ thy ty_decl) haftmann@24219: end; haftmann@24423: fun check_typ (c, thm) = haftmann@25597: case AxClass.inst_of_param thy c haftmann@24837: of SOME (c, tyco) => check_typ_classparam tyco (c, thm) haftmann@24423: | NONE => check_typ_fun (c, thm); haftmann@24423: in check_typ (const_of_func thy thm, thm) end; haftmann@24219: haftmann@28350: fun mk_func linear = Code_Unit.error_thm (assert_func_typ o Code_Unit.mk_func linear); haftmann@28350: val mk_liberal_func = Code_Unit.warning_thm (assert_func_typ o Code_Unit.mk_func true); haftmann@28350: val mk_syntactic_func = Code_Unit.warning_thm (assert_func_typ o Code_Unit.mk_func false); haftmann@28350: val mk_default_func = Code_Unit.try_thm (assert_func_typ o Code_Unit.mk_func true); haftmann@24219: haftmann@28143: end; (*local*) haftmann@24219: haftmann@24219: haftmann@24219: (** interfaces and attributes **) haftmann@24219: haftmann@24624: fun delete_force msg key xs = haftmann@24624: if AList.defined (op =) xs key then AList.delete (op =) key xs haftmann@24624: else error ("No such " ^ msg ^ ": " ^ quote key); haftmann@24624: haftmann@24423: fun get_datatype thy tyco = haftmann@24844: case Symtab.lookup ((the_dtyps o the_exec) thy) tyco haftmann@24423: of SOME spec => spec haftmann@24423: | NONE => Sign.arity_number thy tyco wenzelm@24848: |> Name.invents Name.context Name.aT haftmann@24423: |> map (rpair []) haftmann@24423: |> rpair []; haftmann@24423: haftmann@24423: fun get_datatype_of_constr thy c = haftmann@24423: case (snd o strip_type o Sign.the_const_type thy) c haftmann@24423: of Type (tyco, _) => if member (op =) haftmann@24844: ((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c haftmann@24423: then SOME tyco else NONE haftmann@24423: | _ => NONE; haftmann@24423: haftmann@24423: fun get_constr_typ thy c = haftmann@24423: case get_datatype_of_constr thy c haftmann@24423: of SOME tyco => let haftmann@24423: val (vs, cos) = get_datatype thy tyco; haftmann@24423: val SOME tys = AList.lookup (op =) cos c; haftmann@24423: val ty = tys ---> Type (tyco, map TFree vs); haftmann@24423: in SOME (Logic.varifyT ty) end haftmann@24423: | NONE => NONE; haftmann@24423: haftmann@24844: val get_case_data = Symtab.lookup o fst o the_cases o the_exec; haftmann@24844: haftmann@24844: val is_undefined = Symtab.defined o snd o the_cases o the_exec; haftmann@24844: haftmann@28350: fun gen_add_func linear strict default thm thy = haftmann@28350: case (if strict then SOME o mk_func linear else mk_liberal_func) thm haftmann@28143: of SOME func => haftmann@28143: let haftmann@28143: val c = const_of_func thy func; haftmann@28143: val _ = if strict andalso (is_some o AxClass.class_of_param thy) c haftmann@28143: then error ("Rejected polymorphic equation for overloaded constant:\n" haftmann@28143: ^ Display.string_of_thm thm) haftmann@28143: else (); haftmann@28143: val _ = if strict andalso (is_some o get_datatype_of_constr thy) c haftmann@28143: then error ("Rejected equation for datatype constructor:\n" haftmann@28143: ^ Display.string_of_thm func) haftmann@28143: else (); haftmann@28143: in haftmann@28143: (map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default haftmann@28350: (c, (true, Susp.value [])) (add_thm default (func, linear))) thy haftmann@24624: end haftmann@24624: | NONE => thy; haftmann@24624: haftmann@28350: val add_func = gen_add_func true true false; haftmann@28350: val add_liberal_func = gen_add_func true false false; haftmann@28350: val add_default_func = gen_add_func true false true; haftmann@28350: val add_nonlinear_func = gen_add_func false true false; haftmann@24219: haftmann@28350: fun del_func thm thy = case mk_syntactic_func thm haftmann@28143: of SOME func => let haftmann@28143: val c = const_of_func thy func; haftmann@28143: in map_exec_purge (SOME [c]) (map_funcs haftmann@28143: (Symtab.map_entry c (del_thm func))) thy haftmann@28143: end haftmann@28143: | NONE => thy; haftmann@24219: haftmann@28143: fun del_funcs c = map_exec_purge (SOME [c]) haftmann@28143: (map_funcs (Symtab.map_entry c (K (false, Susp.value [])))); haftmann@26021: haftmann@28143: fun add_funcl (c, lthms) thy = haftmann@24219: let haftmann@28143: val lthms' = certificate thy (fn thy => certify_const thy c) lthms; haftmann@24219: (*FIXME must check compatibility with sort algebra; haftmann@24219: alas, naive checking results in non-termination!*) haftmann@24219: in haftmann@28143: map_exec_purge (SOME [c]) haftmann@28143: (map_funcs (Symtab.map_default (c, (true, Susp.value [])) haftmann@28143: (add_lthms lthms'))) thy haftmann@24219: end; haftmann@24219: haftmann@24624: val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute haftmann@24624: (fn thm => Context.mapping (add_default_func thm) I)); haftmann@24219: haftmann@25485: structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool); haftmann@25462: haftmann@24423: fun add_datatype raw_cs thy = haftmann@24219: let haftmann@25597: val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs; haftmann@28054: val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs; haftmann@25462: val cs' = map fst (snd vs_cos); haftmann@25462: val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco haftmann@25462: of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos) haftmann@24423: | NONE => NONE; haftmann@24219: in haftmann@24219: thy haftmann@25462: |> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos)) haftmann@24423: #> map_funcs (fold (Symtab.delete_safe o fst) cs)) haftmann@25485: |> TypeInterpretation.data (tyco, serial ()) haftmann@24219: end; haftmann@24219: haftmann@25485: fun type_interpretation f = TypeInterpretation.interpretation haftmann@25485: (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy); haftmann@25485: haftmann@24423: fun add_datatype_cmd raw_cs thy = haftmann@24423: let haftmann@28054: val cs = map (Code_Unit.read_bare_const thy) raw_cs; haftmann@24423: in add_datatype cs thy end; haftmann@24219: haftmann@24844: fun add_case thm thy = haftmann@24844: let haftmann@28054: val entry as (c, _) = Code_Unit.case_cert thm; haftmann@24844: in haftmann@24844: (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update entry) thy haftmann@24844: end; haftmann@24844: haftmann@24844: fun add_undefined c thy = haftmann@24844: (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy; haftmann@24844: haftmann@27557: val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst; haftmann@27557: val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd; haftmann@27557: haftmann@27557: fun add_inline thm thy = (map_pre o MetaSimplifier.add_simp) haftmann@28054: (Code_Unit.error_thm Code_Unit.mk_rew thm) thy; haftmann@27557: (*fully applied in order to get right context for mk_rew!*) haftmann@27557: haftmann@27557: fun del_inline thm thy = (map_pre o MetaSimplifier.del_simp) haftmann@28054: (Code_Unit.error_thm Code_Unit.mk_rew thm) thy; haftmann@27557: (*fully applied in order to get right context for mk_rew!*) haftmann@24219: haftmann@27557: fun add_post thm thy = (map_post o MetaSimplifier.add_simp) haftmann@28054: (Code_Unit.error_thm Code_Unit.mk_rew thm) thy; haftmann@27557: (*fully applied in order to get right context for mk_rew!*) haftmann@24219: haftmann@27557: fun del_post thm thy = (map_post o MetaSimplifier.del_simp) haftmann@28054: (Code_Unit.error_thm Code_Unit.mk_rew thm) thy; haftmann@27557: (*fully applied in order to get right context for mk_rew!*) haftmann@27557: haftmann@27557: fun add_functrans (name, f) = haftmann@27557: (map_exec_purge NONE o map_thmproc o apsnd) haftmann@24219: (AList.update (op =) (name, (serial (), f))); haftmann@24219: haftmann@27557: fun del_functrans name = haftmann@24219: (map_exec_purge NONE o map_thmproc o apsnd) haftmann@27609: (delete_force "function transformer" name); haftmann@24219: wenzelm@26463: val _ = Context.>> (Context.map_theory haftmann@24219: (let haftmann@24219: fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); haftmann@24219: fun add_simple_attribute (name, f) = haftmann@24219: add_attribute (name, Scan.succeed (mk_attribute f)); haftmann@24219: fun add_del_attribute (name, (add, del)) = haftmann@24219: add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del) haftmann@24219: || Scan.succeed (mk_attribute add)) haftmann@24219: in haftmann@25462: TypeInterpretation.init haftmann@25462: #> add_del_attribute ("func", (add_func, del_func)) haftmann@28350: #> add_simple_attribute ("nbe", add_nonlinear_func) haftmann@24219: #> add_del_attribute ("inline", (add_inline, del_inline)) haftmann@24219: #> add_del_attribute ("post", (add_post, del_post)) wenzelm@26463: end)); haftmann@24219: haftmann@24219: haftmann@24219: (** post- and preprocessing **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@27609: fun apply_functrans thy [] = [] haftmann@28353: | apply_functrans thy (thms as (thm, _) :: _) = haftmann@24219: let haftmann@24423: val const = const_of_func thy thm; haftmann@27609: val functrans = (map (fn (_, (_, f)) => f thy) o #functrans haftmann@27609: o the_thmproc o the_exec) thy; haftmann@28353: val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) (map fst thms); haftmann@28353: val thms'' = certify_const thy const thms'; haftmann@28353: val linears = map snd thms; haftmann@28353: in (*FIXME temporary workaround*) if length thms'' = length linears haftmann@28353: then thms'' ~~ linears haftmann@28353: else map (rpair true) thms'' haftmann@28353: end; haftmann@24219: haftmann@24219: fun rhs_conv conv thm = haftmann@24219: let haftmann@24219: val thm' = (conv o Thm.rhs_of) thm; haftmann@24219: in Thm.transitive thm thm' end haftmann@24219: haftmann@24837: fun term_of_conv thy f = haftmann@24837: Thm.cterm_of thy haftmann@24837: #> f haftmann@24837: #> Thm.prop_of haftmann@24837: #> Logic.dest_equals haftmann@24837: #> snd; haftmann@24837: haftmann@24219: in haftmann@24219: haftmann@24219: fun preprocess thy thms = haftmann@27582: let haftmann@27582: val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; haftmann@27582: in haftmann@27582: thms haftmann@27609: |> apply_functrans thy haftmann@28353: |> (map o apfst) (Code_Unit.rewrite_func pre) haftmann@28350: (*FIXME - must check here: rewrite rule, defining equation, proper constant *) haftmann@28353: |> (map o apfst) (AxClass.unoverload thy) haftmann@28353: |> burrow_fst common_typ_funcs haftmann@27582: end; haftmann@26970: haftmann@24219: haftmann@24219: fun preprocess_conv ct = haftmann@24219: let haftmann@24219: val thy = Thm.theory_of_cterm ct; haftmann@27582: val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; haftmann@24219: in haftmann@24219: ct haftmann@27582: |> Simplifier.rewrite pre haftmann@25597: |> rhs_conv (AxClass.unoverload_conv thy) haftmann@24219: end; haftmann@24219: haftmann@24837: fun preprocess_term thy = term_of_conv thy preprocess_conv; haftmann@24837: haftmann@24219: fun postprocess_conv ct = haftmann@24219: let haftmann@24219: val thy = Thm.theory_of_cterm ct; haftmann@27582: val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy; haftmann@24219: in haftmann@24219: ct haftmann@25597: |> AxClass.overload_conv thy haftmann@27582: |> rhs_conv (Simplifier.rewrite post) haftmann@24219: end; haftmann@24219: haftmann@24837: fun postprocess_term thy = term_of_conv thy postprocess_conv; haftmann@24837: haftmann@24219: end; (*local*) haftmann@24219: haftmann@25597: fun default_typ_proto thy c = case AxClass.inst_of_param thy c haftmann@24837: of SOME (c, tyco) => classparam_weakest_typ thy ((the o AxClass.class_of_param thy) c) haftmann@24423: (c, tyco) |> SOME haftmann@24423: | NONE => (case AxClass.class_of_param thy c haftmann@24423: of SOME class => SOME (Term.map_type_tvar wenzelm@24848: (K (TVar ((Name.aT, 0), [class]))) (Sign.the_const_type thy c)) haftmann@24423: | NONE => get_constr_typ thy c); haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: fun get_funcs thy const = haftmann@24844: Symtab.lookup ((the_funcs o the_exec) thy) const haftmann@28143: |> Option.map (Susp.force o snd) haftmann@24219: |> these haftmann@28350: |> (map o apfst) (Thm.transfer thy); haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24219: fun these_funcs thy const = haftmann@24219: let haftmann@24219: fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals haftmann@28350: o ObjectLogic.drop_judgment thy o Thm.plain_prop_of o fst); haftmann@24219: in haftmann@24219: get_funcs thy const haftmann@28353: |> preprocess thy haftmann@24219: |> drop_refl thy haftmann@24219: end; haftmann@24219: haftmann@24423: fun default_typ thy c = case default_typ_proto thy c haftmann@28054: of SOME ty => Code_Unit.typscheme thy (c, ty) haftmann@24423: | NONE => (case get_funcs thy c haftmann@28350: of (thm, _) :: _ => snd (Code_Unit.head_func (AxClass.unoverload thy thm)) haftmann@28054: | [] => Code_Unit.typscheme thy (c, Sign.the_const_type thy c)); haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: end; (*struct*) haftmann@24219: haftmann@24219: haftmann@24219: (** type-safe interfaces for data depedent on executable content **) haftmann@24219: haftmann@24219: functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = haftmann@24219: struct haftmann@24219: haftmann@24219: type T = Data.T; haftmann@24219: exception Data of T; haftmann@24219: fun dest (Data x) = x haftmann@24219: haftmann@24219: val kind = Code.declare_data (Data Data.empty) haftmann@27609: (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x)); haftmann@24219: haftmann@24219: val data_op = (kind, Data, dest); haftmann@24219: haftmann@24219: val get = Code.get_data data_op; haftmann@24219: val change = Code.change_data data_op; haftmann@24219: fun change_yield thy = Code.change_yield_data data_op thy; haftmann@24219: haftmann@24219: end; haftmann@24219: haftmann@28143: structure Code : CODE = struct open Code; end;