krauss@19564: (* Title: HOL/Tools/function_package/fundef_package.ML krauss@19564: ID: $Id$ krauss@19564: Author: Alexander Krauss, TU Muenchen krauss@19564: wenzelm@20363: A package for general recursive function definitions. krauss@19564: Isar commands. krauss@19564: krauss@19564: *) krauss@19564: wenzelm@20363: signature FUNDEF_PACKAGE = krauss@19564: sig wenzelm@20877: val add_fundef : (string * string option * mixfix) list krauss@22498: -> ((bstring * Attrib.src list) * (string * bool)) list wenzelm@20877: -> FundefCommon.fundef_config wenzelm@20877: -> local_theory krauss@21211: -> string * Proof.state krauss@20523: wenzelm@20877: val add_fundef_i: (string * typ option * mixfix) list krauss@22498: -> ((bstring * Attrib.src list) * (term * bool)) list krauss@22170: -> FundefCommon.fundef_config wenzelm@20877: -> local_theory krauss@21211: -> string * Proof.state krauss@21211: krauss@21211: val setup_termination_proof : string option -> local_theory -> Proof.state krauss@19564: krauss@19564: val cong_add: attribute krauss@19564: val cong_del: attribute wenzelm@20363: krauss@19564: val setup : theory -> theory krauss@21235: val setup_case_cong_hook : theory -> theory krauss@19770: val get_congs : theory -> thm list krauss@19564: end krauss@19564: krauss@19564: krauss@22170: structure FundefPackage : FUNDEF_PACKAGE = krauss@19564: struct krauss@19564: krauss@21051: open FundefLib krauss@19564: open FundefCommon krauss@19564: wenzelm@21435: val note_theorem = LocalTheory.note Thm.theoremK wenzelm@21435: krauss@22166: fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" krauss@22166: krauss@20523: fun burrow_snd f ps = (* ('a list -> 'b list) -> ('c * 'a) list -> ('c * 'b) list *) krauss@20523: let val (xs, ys) = split_list ps krauss@20523: in xs ~~ f ys end krauss@20270: krauss@20523: fun restore_spec_structure reps spec = krauss@22498: (burrow_snd o burrow o K) reps spec wenzelm@20363: krauss@22166: fun add_simps fixes spec sort label moreatts simps lthy = krauss@19564: let krauss@20523: val fnames = map (fst o fst) fixes krauss@21255: krauss@22166: val (saved_spec_simps, lthy) = krauss@22498: fold_map note_theorem (restore_spec_structure simps spec) lthy krauss@22498: val saved_simps = flat (map snd saved_spec_simps) krauss@21255: krauss@22166: val simps_by_f = sort saved_simps krauss@19564: krauss@22166: fun add_for_f fname simps = wenzelm@21435: note_theorem wenzelm@21658: ((NameSpace.qualified fname label, Attrib.internal (K Simplifier.simp_add) :: moreatts), krauss@22166: simps) #> snd krauss@19564: in krauss@22166: (saved_simps, krauss@22166: fold2 add_for_f fnames simps_by_f lthy) krauss@19564: end krauss@19564: krauss@22166: fun fundef_afterqed config fixes spec defname cont sort_cont [[proof]] lthy = krauss@20523: let krauss@22166: val FundefResult {f, R, psimps, trsimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = krauss@22166: cont (Goal.close_result proof) krauss@22166: wenzelm@21391: val qualify = NameSpace.qualified defname krauss@22170: val addsmps = add_simps fixes spec sort_cont wenzelm@21602: wenzelm@21602: val (((psimps', pinducts'), (_, [termination'])), lthy) = krauss@21255: lthy krauss@22170: |> addsmps "psimps" [] psimps krauss@22170: ||> fold_option (snd oo addsmps "simps" []) trsimps krauss@22166: ||>> note_theorem ((qualify "pinduct", krauss@22166: [Attrib.internal (K (InductAttrib.induct_set ""))]), simple_pinducts) krauss@22166: ||>> note_theorem ((qualify "termination", []), [termination]) krauss@22166: ||> (snd o note_theorem ((qualify "cases", []), [cases])) krauss@22166: ||> fold_option (snd oo curry note_theorem (qualify "domintros", [])) domintros krauss@21255: krauss@22170: val cdata = FundefCtxData { add_simps=addsmps, psimps=psimps', krauss@21255: pinducts=snd pinducts', termination=termination', f=f, R=R } krauss@22623: |> morph_fundef_data (LocalTheory.target_morphism lthy) krauss@20523: in krauss@22623: lthy krauss@22635: |> LocalTheory.declaration (fn phi => add_fundef_data defname (morph_fundef_data phi cdata)) (* save in target *) krauss@21255: |> Context.proof_map (add_fundef_data defname cdata) (* also save in local context *) krauss@20523: end (* FIXME: Add cases for induct and cases thm *) krauss@20523: krauss@20523: krauss@20523: krauss@20523: fun prep_with_flags prep fixspec eqnss_flags global_flag lthy = krauss@19564: let krauss@22498: val flags = map (fn x => global_flag orelse (snd (snd x))) eqnss_flags krauss@22498: val eqns = map (apsnd (single o fst)) eqnss_flags krauss@20523: krauss@20523: val ((fixes, _), ctxt') = prep fixspec [] lthy krauss@22498: krauss@22498: fun prep_eqn e = the_single (snd (fst (prep [] [e] ctxt'))) krauss@22498: |> apsnd the_single krauss@22498: krauss@22498: val spec = map prep_eqn eqns krauss@22498: |> map (apsnd (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t)) (* Add quantifiers *) krauss@22498: |> burrow_snd (fn ts => FundefSplit.split_some_equations ctxt' (flags ~~ ts)) krauss@20523: in krauss@20523: ((fixes, spec), ctxt') krauss@20523: end krauss@20523: krauss@20654: fun gen_add_fundef prep_spec fixspec eqnss_flags config lthy = krauss@20523: let krauss@22166: val FundefConfig {sequential, default, tailrec, ...} = config krauss@20654: krauss@20654: val ((fixes, spec), ctxt') = prep_with_flags prep_spec fixspec eqnss_flags sequential lthy krauss@22166: krauss@22166: val defname = mk_defname fixes krauss@22166: krauss@22498: val t_eqns = spec |> map snd |> flat (* flatten external structure *) krauss@20523: krauss@22166: val ((goalstate, cont, sort_cont), lthy) = krauss@22166: FundefMutual.prepare_fundef_mutual config defname fixes t_eqns default lthy krauss@20523: krauss@22166: val afterqed = fundef_afterqed config fixes spec defname cont sort_cont krauss@20523: in krauss@22166: (defname, lthy krauss@22166: |> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]] krauss@22166: |> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd) krauss@20523: end krauss@20523: krauss@20523: krauss@20523: fun total_termination_afterqed defname data [[totality]] lthy = krauss@20523: let krauss@22166: val FundefCtxData { add_simps, psimps, pinducts, ... } = data krauss@21255: krauss@22166: val totality = Goal.close_result totality krauss@19564: krauss@21255: val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals]) krauss@19564: krauss@22166: val tsimps = map remove_domain_condition psimps krauss@22166: val tinduct = map remove_domain_condition pinducts krauss@19770: krauss@21511: (* FIXME: How to generate code from (possibly) local contexts*) krauss@21511: val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps wenzelm@21658: val allatts = if has_guards then [] else [Attrib.internal (K (RecfunCodegen.add NONE))] wenzelm@21602: krauss@21511: val qualify = NameSpace.qualified defname; krauss@19564: in krauss@21511: lthy krauss@22166: |> add_simps "simps" allatts tsimps |> snd krauss@22166: |> note_theorem ((qualify "induct", []), tinduct) |> snd wenzelm@20363: end krauss@19564: krauss@19564: krauss@21211: fun setup_termination_proof name_opt lthy = krauss@20523: let krauss@22166: val defname = the_default (get_last_fundef (Context.Proof lthy)) name_opt krauss@22166: val data = the (get_fundef_data defname (Context.Proof lthy)) krauss@22166: handle Option.Option => raise ERROR ("No such function definition: " ^ defname) krauss@20523: krauss@22166: val FundefCtxData {termination, R, ...} = data krauss@22325: val domT = domain_type (fastype_of R) krauss@22325: val goal = HOLogic.mk_Trueprop (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT))) krauss@20523: in wenzelm@20877: lthy krauss@22325: |> ProofContext.note_thmss_i "" [(("", [ContextRules.rule_del]), [([allI], [])])] |> snd krauss@22325: |> ProofContext.note_thmss_i "" [(("", [ContextRules.intro_bang (SOME 1)]), [([allI], [])])] |> snd wenzelm@21602: |> ProofContext.note_thmss_i "" krauss@22325: [(("termination", [ContextRules.intro_bang (SOME 0)]), wenzelm@21602: [([Goal.norm_result termination], [])])] |> snd krauss@21319: |> set_termination_rule termination krauss@22166: |> Proof.theorem_i NONE (total_termination_afterqed defname data) [[(goal, [])]] krauss@20523: end krauss@20523: krauss@20523: krauss@20523: val add_fundef = gen_add_fundef Specification.read_specification krauss@20523: val add_fundef_i = gen_add_fundef Specification.cert_specification krauss@19611: krauss@19564: krauss@19564: krauss@19564: (* congruence rules *) krauss@19564: wenzelm@19617: val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq); wenzelm@19617: val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq); krauss@19564: krauss@21235: (* Datatype hook to declare datatype congs as "fundef_congs" *) krauss@21235: krauss@21235: wenzelm@21602: fun add_case_cong n thy = wenzelm@21602: Context.theory_map (map_fundef_congs (Drule.add_rule krauss@21235: (DatatypePackage.get_datatype thy n |> the krauss@21235: |> #case_cong wenzelm@21602: |> safe_mk_meta_eq))) krauss@21235: thy krauss@21235: krauss@21235: val case_cong_hook = fold add_case_cong krauss@21235: wenzelm@21602: val setup_case_cong_hook = krauss@21235: DatatypeHooks.add case_cong_hook krauss@21235: #> (fn thy => case_cong_hook (Symtab.keys (DatatypePackage.get_datatypes thy)) thy) krauss@19564: krauss@19564: (* setup *) krauss@19564: wenzelm@21602: val setup = wenzelm@21602: FundefData.init krauss@21235: #> FundefCongs.init krauss@21319: #> TerminationRule.init krauss@21235: #> Attrib.add_attributes krauss@21235: [("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")] krauss@21235: #> setup_case_cong_hook krauss@21319: #> FundefRelation.setup krauss@19564: krauss@19770: val get_congs = FundefCommon.get_fundef_congs o Context.Theory krauss@19770: krauss@19770: krauss@21235: krauss@19564: (* outer syntax *) krauss@19564: krauss@19564: local structure P = OuterParse and K = OuterKeyword in krauss@19564: krauss@19564: val functionP = krauss@19564: OuterSyntax.command "function" "define general recursive functions" K.thy_goal krauss@22498: (fundef_parser default_config krauss@22498: >> (fn ((config, fixes), statements) => krauss@22498: Toplevel.local_theory_to_proof (target_of config) (add_fundef fixes statements config #> snd) krauss@21211: #> Toplevel.print)); krauss@20523: krauss@19564: val terminationP = krauss@19564: OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal wenzelm@22102: (P.opt_target -- Scan.option P.name wenzelm@21000: >> (fn (target, name) => wenzelm@21000: Toplevel.print o krauss@21211: Toplevel.local_theory_to_proof target (setup_termination_proof name))); krauss@19564: krauss@19564: val _ = OuterSyntax.add_parsers [functionP]; krauss@19564: val _ = OuterSyntax.add_parsers [terminationP]; krauss@20654: val _ = OuterSyntax.add_keywords ["sequential", "otherwise"]; krauss@19564: krauss@19564: end; krauss@19564: krauss@19564: wenzelm@19585: end