krauss@19564: (* Title: HOL/Tools/function_package/fundef_package.ML krauss@19564: ID: $Id$ krauss@19564: Author: Alexander Krauss, TU Muenchen krauss@19564: krauss@19564: A package for general recursive function definitions. krauss@19564: Isar commands. krauss@19564: krauss@19564: *) krauss@19564: krauss@19564: signature FUNDEF_PACKAGE = krauss@19564: sig krauss@19564: val add_fundef : ((bstring * Attrib.src list) * string) list -> theory -> Proof.state (* Need an _i variant *) krauss@19564: krauss@19564: val cong_add: attribute krauss@19564: val cong_del: attribute krauss@19564: krauss@19564: val setup : theory -> theory krauss@19564: end krauss@19564: krauss@19564: krauss@19564: structure FundefPackage : FUNDEF_PACKAGE = krauss@19564: struct krauss@19564: krauss@19564: open FundefCommon krauss@19564: krauss@19564: val True_implies = thm "True_implies" krauss@19564: krauss@19564: krauss@19564: fun fundef_afterqed congs curry_info name data natts thmss thy = krauss@19564: let krauss@19564: val (complete_thm :: compat_thms) = map hd thmss krauss@19564: val fundef_data = FundefProof.mk_partial_rules_curried thy congs curry_info data (freezeT complete_thm) (map freezeT compat_thms) krauss@19583: val FundefResult {psimps, subset_pinduct, simple_pinduct, total_intro, dom_intros, ...} = fundef_data krauss@19564: krauss@19564: val (names, attsrcs) = split_list natts krauss@19564: val atts = map (map (Attrib.attribute thy)) attsrcs krauss@19564: krauss@19583: val Prep {names = Names {acc_R=accR, ...}, ...} = data krauss@19583: val dom_abbrev = Logic.mk_equals (Free (name ^ "_dom", fastype_of accR), accR) krauss@19583: val (_, thy) = LocalTheory.mapping NONE (Specification.abbreviation_i ("", false) [(NONE, dom_abbrev)]) thy krauss@19564: krauss@19564: val thy = thy |> Theory.add_path name krauss@19564: krauss@19564: val thy = thy |> Theory.add_path "psimps" krauss@19564: val (_, thy) = PureThy.add_thms ((names ~~ psimps) ~~ atts) thy; krauss@19564: val thy = thy |> Theory.parent_path krauss@19564: krauss@19564: val (_, thy) = PureThy.add_thms [(("cases", complete_thm), [RuleCases.case_names names])] thy krauss@19564: val (_, thy) = PureThy.add_thmss [(("domintros", dom_intros), [])] thy krauss@19564: val (_, thy) = PureThy.add_thms [(("termination", total_intro), [])] thy krauss@19564: val (_,thy) = PureThy.add_thms [(("pinduct", simple_pinduct), [RuleCases.case_names names, InductAttrib.induct_set ""])] thy krauss@19564: val (_, thy) = PureThy.add_thmss [(("psimps", psimps), [Simplifier.simp_add])] thy krauss@19564: val thy = thy |> Theory.parent_path krauss@19564: in krauss@19564: add_fundef_data name fundef_data thy krauss@19564: end krauss@19564: krauss@19564: fun add_fundef eqns_atts thy = krauss@19564: let krauss@19564: val (natts, eqns) = split_list eqns_atts krauss@19564: krauss@19564: val congs = get_fundef_congs (Context.Theory thy) krauss@19564: krauss@19564: val (curry_info, name, (data, thy)) = FundefPrep.prepare_fundef_curried congs (map (Sign.read_prop thy) eqns) thy krauss@19583: val Prep {complete, compat, ...} = data krauss@19564: krauss@19564: val props = (complete :: compat) (*(complete :: fst (chop 110 compat))*) krauss@19564: in krauss@19564: thy |> ProofContext.init krauss@19564: |> Proof.theorem_i PureThy.internalK NONE (fundef_afterqed congs curry_info name data natts) NONE ("", []) wenzelm@19585: (map (fn t => (("", []), [(t, [])])) props) krauss@19564: end krauss@19564: krauss@19564: krauss@19564: fun total_termination_afterqed name thmss thy = krauss@19564: let krauss@19564: val totality = hd (hd thmss) krauss@19564: krauss@19583: val FundefResult {psimps, simple_pinduct, ... } krauss@19564: = the (get_fundef_data name thy) krauss@19564: krauss@19564: val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies]) krauss@19564: krauss@19564: val tsimps = map remove_domain_condition psimps krauss@19564: val tinduct = remove_domain_condition simple_pinduct krauss@19564: krauss@19564: val thy = Theory.add_path name thy krauss@19564: krauss@19564: (* Need the names and attributes. Apply the attributes again? *) krauss@19564: (* val thy = thy |> Theory.add_path "simps" krauss@19564: val (_, thy) = PureThy.add_thms ((names ~~ tsimps) ~~ atts) thy; krauss@19564: val thy = thy |> Theory.parent_path*) krauss@19564: krauss@19564: val (_, thy) = PureThy.add_thms [(("induct", tinduct), [])] thy krauss@19564: val (_, thy) = PureThy.add_thmss [(("simps", tsimps), [Simplifier.simp_add, RecfunCodegen.add NONE])] thy krauss@19564: val thy = Theory.parent_path thy krauss@19564: in krauss@19564: thy krauss@19564: end krauss@19564: krauss@19564: (* krauss@19564: fun mk_partial_rules name D_name D domT idomT thmss thy = krauss@19564: let krauss@19564: val [subs, dcl] = (hd thmss) krauss@19564: krauss@19564: val {f_const, f_curried_const, G_const, R_const, G_elims, completeness, f_simps, names_attrs, subset_induct, ... } krauss@19564: = the (Symtab.lookup (FundefData.get thy) name) krauss@19564: krauss@19564: val D_implies_dom = subs COMP (instantiate' [SOME (ctyp_of thy idomT)] krauss@19564: [SOME (cterm_of thy D)] krauss@19564: subsetD) krauss@19564: krauss@19564: val D_simps = map (curry op RS D_implies_dom) f_simps krauss@19564: krauss@19564: val D_induct = subset_induct krauss@19564: |> cterm_instantiate [(cterm_of thy (Var (("D",0), fastype_of D)) ,cterm_of thy D)] krauss@19564: |> curry op COMP subs krauss@19564: |> curry op COMP (dcl |> forall_intr (cterm_of thy (Var (("z",0), idomT))) krauss@19564: |> forall_intr (cterm_of thy (Var (("x",0), idomT)))) krauss@19564: krauss@19564: val ([tinduct'], thy2) = PureThy.add_thms [((name ^ "_" ^ D_name ^ "_induct", D_induct), [])] thy krauss@19564: val ([tsimps'], thy3) = PureThy.add_thmss [((name ^ "_" ^ D_name ^ "_simps", D_simps), [])] thy2 krauss@19564: in krauss@19564: thy3 krauss@19564: end krauss@19564: *) krauss@19564: krauss@19564: krauss@19564: fun fundef_setup_termination_proof name NONE thy = krauss@19564: let krauss@19564: val name = if name = "" then get_last_fundef thy else name krauss@19564: val data = the (get_fundef_data name thy) krauss@19564: krauss@19583: val FundefResult {total_intro, ...} = data krauss@19564: val goal = FundefTermination.mk_total_termination_goal data krauss@19564: in krauss@19564: thy |> ProofContext.init krauss@19564: |> ProofContext.note_thmss_i [(("termination_intro", krauss@19564: [ContextRules.intro_query NONE]), [([total_intro], [])])] |> snd krauss@19564: |> Proof.theorem_i PureThy.internalK NONE (total_termination_afterqed name) NONE ("", []) wenzelm@19585: [(("", []), [(goal, [])])] krauss@19564: end krauss@19564: | fundef_setup_termination_proof name (SOME (dom_name, dom)) thy = krauss@19564: let krauss@19564: val name = if name = "" then get_last_fundef thy else name krauss@19564: val data = the (get_fundef_data name thy) krauss@19564: val (subs, dcl) = FundefTermination.mk_partial_termination_goal thy data dom krauss@19564: in krauss@19564: thy |> ProofContext.init krauss@19564: |> Proof.theorem_i PureThy.internalK NONE (K I) NONE ("", []) wenzelm@19585: [(("", []), [(subs, []), (dcl, [])])] krauss@19564: end krauss@19564: krauss@19564: krauss@19564: krauss@19564: krauss@19564: (* congruence rules *) krauss@19564: krauss@19564: val cong_add = Thm.declaration_attribute (map_fundef_congs o cons o safe_mk_meta_eq); krauss@19564: val cong_del = Thm.declaration_attribute (map_fundef_congs o remove (op =) o safe_mk_meta_eq); krauss@19564: krauss@19564: krauss@19564: (* setup *) krauss@19564: krauss@19564: val setup = FundefData.init #> FundefCongs.init krauss@19564: #> Attrib.add_attributes krauss@19564: [("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")] krauss@19564: krauss@19564: krauss@19564: (* outer syntax *) krauss@19564: krauss@19564: local structure P = OuterParse and K = OuterKeyword in krauss@19564: krauss@19564: val function_decl = krauss@19564: Scan.repeat1 (P.opt_thm_name ":" -- P.prop); krauss@19564: krauss@19564: val functionP = krauss@19564: OuterSyntax.command "function" "define general recursive functions" K.thy_goal krauss@19564: (function_decl >> (fn eqns => krauss@19564: Toplevel.print o Toplevel.theory_to_proof (add_fundef eqns))); krauss@19564: krauss@19564: val terminationP = krauss@19564: OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal krauss@19564: ((Scan.optional P.name "" -- Scan.option (P.$$$ "(" |-- Scan.optional (P.name --| P.$$$ ":") "dom" -- P.term --| P.$$$ ")")) krauss@19564: >> (fn (name,dom) => krauss@19564: Toplevel.print o Toplevel.theory_to_proof (fundef_setup_termination_proof name dom))); krauss@19564: krauss@19564: val _ = OuterSyntax.add_parsers [functionP]; krauss@19564: val _ = OuterSyntax.add_parsers [terminationP]; krauss@19564: krauss@19564: krauss@19564: end; krauss@19564: krauss@19564: wenzelm@19585: end