src/HOL/Tools/function_package/fundef_package.ML
author krauss
Tue Jun 06 09:28:24 2006 +0200 (2006-06-06)
changeset 19782 48c4632e2c28
parent 19770 be5c23ebe1eb
child 19784 fa5080577da9
permissions -rw-r--r--
HOL/Tools/function_package: imporoved handling of guards, added an example
     1 (*  Title:      HOL/Tools/function_package/fundef_package.ML
     2     ID:         $Id$
     3     Author:     Alexander Krauss, TU Muenchen
     4 
     5 A package for general recursive function definitions. 
     6 Isar commands.
     7 
     8 *)
     9 
    10 signature FUNDEF_PACKAGE = 
    11 sig
    12     val add_fundef : ((bstring * Attrib.src list) * string) list list -> theory -> Proof.state (* Need an _i variant *)
    13 
    14     val cong_add: attribute
    15     val cong_del: attribute
    16 							 
    17     val setup : theory -> theory
    18     val get_congs : theory -> thm list
    19 end
    20 
    21 
    22 structure FundefPackage : FUNDEF_PACKAGE =
    23 struct
    24 
    25 open FundefCommon
    26 
    27 val True_implies = thm "True_implies"
    28 
    29 
    30 fun add_simps label moreatts (MutualPart {f_name, ...}, psimps) (names, attss) thy =
    31     let 
    32       val thy = thy |> Theory.add_path f_name 
    33                 
    34       val thy = thy |> Theory.add_path label
    35       val spsimps = map standard psimps
    36       val add_list = (names ~~ spsimps) ~~ attss
    37       val (_, thy) = PureThy.add_thms add_list thy
    38       val thy = thy |> Theory.parent_path
    39                 
    40       val (_, thy) = PureThy.add_thmss [((label, spsimps), Simplifier.simp_add :: moreatts)] thy
    41       val thy = thy |> Theory.parent_path
    42     in
    43       thy
    44     end
    45     
    46 
    47 
    48 
    49 
    50 
    51 fun fundef_afterqed congs curry_info name data names atts thmss thy =
    52     let
    53 	val (complete_thm :: compat_thms) = map hd thmss
    54 	val fundef_data = FundefMutual.mk_partial_rules_mutual thy congs curry_info data (freezeT complete_thm) (map freezeT compat_thms)
    55 	val FundefMResult {psimps, subset_pinducts, simple_pinducts, termination, domintros, ...} = fundef_data
    56         val Mutual {parts, ...} = curry_info
    57 
    58 	val Prep {names = Names {acc_R=accR, ...}, ...} = data
    59 	val dom_abbrev = Logic.mk_equals (Free (name ^ "_dom", fastype_of accR), accR)
    60 	val (_, thy) = LocalTheory.mapping NONE (Specification.abbreviation_i ("", false) [(NONE, dom_abbrev)]) thy
    61 
    62         val thy = fold2 (add_simps "psimps" []) (parts ~~ psimps) (names ~~ atts) thy
    63 
    64 	val thy = thy |> Theory.add_path name
    65 	val (_, thy) = PureThy.add_thms [(("cases", complete_thm), [RuleCases.case_names (flat names)])] thy
    66 	val (_, thy) = PureThy.add_thmss [(("domintros", domintros), [])] thy
    67 	val (_, thy) = PureThy.add_thms [(("termination", termination), [])] thy
    68 	val (_,thy) = PureThy.add_thmss [(("pinduct", map standard simple_pinducts), [RuleCases.case_names (flat names), InductAttrib.induct_set ""])] thy
    69 	val thy = thy |> Theory.parent_path
    70     in
    71 	add_fundef_data name (fundef_data, curry_info, names, atts) thy
    72     end
    73 
    74 fun gen_add_fundef prep_att eqns_attss thy =
    75     let
    76 	fun split eqns_atts =
    77 	    let 
    78 		val (natts, eqns) = split_list eqns_atts
    79 		val (names, raw_atts) = split_list natts
    80 		val atts = map (map (prep_att thy)) raw_atts
    81 	    in
    82 		((names, atts), eqns)
    83 	    end
    84 
    85 
    86 	val (natts, eqns) = split_list (map split_list eqns_attss)
    87 	val (names, raw_atts) = split_list (map split_list natts)
    88 
    89 	val atts = map (map (map (prep_att thy))) raw_atts
    90 
    91 	val congs = get_fundef_congs (Context.Theory thy)
    92 
    93 	val t_eqns = map (map (Sign.read_prop thy)) eqns
    94 			 |> map (map (term_of o cterm_of thy)) (* HACK to prevent strange things from happening with abbreviations *)
    95 
    96 	val (curry_info, name, (data, thy)) = FundefMutual.prepare_fundef_mutual congs t_eqns thy
    97 	val Prep {complete, compat, ...} = data
    98 
    99 	val props = (complete :: compat) (*(complete :: fst (chop 110 compat))*)
   100     in
   101 	thy |> ProofContext.init
   102 	    |> Proof.theorem_i PureThy.internalK NONE (fundef_afterqed congs curry_info name data names atts) NONE ("", [])
   103 	    (map (fn t => (("", []), [(t, [])])) props)
   104     end
   105 
   106 
   107 fun total_termination_afterqed name (Mutual {parts, ...}) thmss thy =
   108     let
   109 	val totality = hd (hd thmss)
   110 
   111 	val (FundefMResult {psimps, simple_pinducts, ... }, Mutual {parts, ...}, names, atts)
   112 	  = the (get_fundef_data name thy)
   113 
   114 	val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies])
   115 
   116 	val tsimps = map (map remove_domain_condition) psimps
   117 	val tinduct = map remove_domain_condition simple_pinducts
   118 
   119         val thy = fold2 (add_simps "simps" [(*RecfunCodegen.add NONE*)]) (parts ~~ tsimps) (names ~~ atts) thy
   120 
   121 	val thy = Theory.add_path name thy
   122 		  
   123 	val (_, thy) = PureThy.add_thmss [(("induct", map standard tinduct), [])] thy 
   124 	val thy = Theory.parent_path thy
   125     in
   126 	thy
   127     end
   128 
   129 (*
   130 fun mk_partial_rules name D_name D domT idomT thmss thy =
   131     let
   132 	val [subs, dcl] = (hd thmss)
   133 
   134 	val {f_const, f_curried_const, G_const, R_const, G_elims, completeness, f_simps, names_attrs, subset_induct, ... }
   135 	  = the (Symtab.lookup (FundefData.get thy) name)
   136 
   137 	val D_implies_dom = subs COMP (instantiate' [SOME (ctyp_of thy idomT)] 
   138 						    [SOME (cterm_of thy D)]
   139 						    subsetD)
   140 
   141 	val D_simps = map (curry op RS D_implies_dom) f_simps
   142 
   143 	val D_induct = subset_induct
   144 			   |> cterm_instantiate [(cterm_of thy (Var (("D",0), fastype_of D)) ,cterm_of thy D)]
   145 			   |> curry op COMP subs
   146 			   |> curry op COMP (dcl |> forall_intr (cterm_of thy (Var (("z",0), idomT)))
   147 						 |> forall_intr (cterm_of thy (Var (("x",0), idomT))))
   148 
   149 	val ([tinduct'], thy2) = PureThy.add_thms [((name ^ "_" ^ D_name ^ "_induct", D_induct), [])] thy
   150 	val ([tsimps'], thy3) = PureThy.add_thmss [((name ^ "_" ^ D_name ^ "_simps", D_simps), [])] thy2
   151     in
   152 	thy3
   153     end
   154 *)
   155  
   156 
   157 fun fundef_setup_termination_proof name NONE thy = 
   158     let
   159 	val name = if name = "" then get_last_fundef thy else name
   160 	val data = the (get_fundef_data name thy)
   161 
   162 	val (res as FundefMResult {termination, ...}, mutual, _, _) = data
   163 	val goal = FundefTermination.mk_total_termination_goal data
   164     in
   165 	thy |> ProofContext.init
   166 	    |> ProofContext.note_thmss_i [(("termination", 
   167 					    [ContextRules.intro_query NONE]), [([termination], [])])] |> snd
   168 	    |> Proof.theorem_i PureThy.internalK NONE (total_termination_afterqed name mutual) NONE ("", [])
   169 	    [(("", []), [(goal, [])])]
   170     end	
   171   | fundef_setup_termination_proof name (SOME (dom_name, dom)) thy =
   172     let
   173 	val name = if name = "" then get_last_fundef thy else name
   174 	val data = the (get_fundef_data name thy)
   175 	val (subs, dcl) = FundefTermination.mk_partial_termination_goal thy data dom
   176     in
   177 	thy |> ProofContext.init
   178 	    |> Proof.theorem_i PureThy.internalK NONE (K I) NONE ("", [])
   179 	    [(("", []), [(subs, []), (dcl, [])])]
   180     end	
   181 
   182 
   183 val add_fundef = gen_add_fundef Attrib.attribute
   184 
   185 
   186 
   187 (* congruence rules *)
   188 
   189 val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq);
   190 val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq);
   191 
   192 
   193 (* setup *)
   194 
   195 val setup = FundefData.init #> FundefCongs.init 
   196 	#>  Attrib.add_attributes
   197 		[("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
   198 
   199 
   200 val get_congs = FundefCommon.get_fundef_congs o Context.Theory
   201 
   202 
   203 (* outer syntax *)
   204 
   205 local structure P = OuterParse and K = OuterKeyword in
   206 
   207 val function_decl =
   208     Scan.repeat1 (P.opt_thm_name ":" -- P.prop);
   209 
   210 val functionP =
   211   OuterSyntax.command "function" "define general recursive functions" K.thy_goal
   212     (P.and_list1 function_decl >> (fn eqnss =>
   213       Toplevel.print o Toplevel.theory_to_proof (add_fundef eqnss)));
   214 
   215 val terminationP =
   216   OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
   217   ((Scan.optional P.name "" -- Scan.option (P.$$$ "(" |-- Scan.optional (P.name --| P.$$$ ":") "dom" -- P.term --| P.$$$ ")"))
   218        >> (fn (name,dom) =>
   219 	      Toplevel.print o Toplevel.theory_to_proof (fundef_setup_termination_proof name dom)));
   220 
   221 val _ = OuterSyntax.add_parsers [functionP];
   222 val _ = OuterSyntax.add_parsers [terminationP];
   223 
   224 
   225 end;
   226 
   227 
   228 end