src/HOL/Tools/function_package/fundef_package.ML
author wenzelm
Thu Aug 03 15:03:07 2006 +0200 (2006-08-03)
changeset 20320 a5368278a72c
parent 20285 23f5cd23c1e1
child 20338 ecdfc96cf4d0
permissions -rw-r--r--
removed True_implies (cf. True_implies_equals);
     1 
     2 (*  Title:      HOL/Tools/function_package/fundef_package.ML
     3     ID:         $Id$
     4     Author:     Alexander Krauss, TU Muenchen
     5 
     6 A package for general recursive function definitions. 
     7 Isar commands.
     8 
     9 *)
    10 
    11 signature FUNDEF_PACKAGE = 
    12 sig
    13     val add_fundef : ((bstring * (Attrib.src list * bool)) * string) list list -> bool -> theory -> Proof.state (* Need an _i variant *)
    14 
    15     val cong_add: attribute
    16     val cong_del: attribute
    17 							 
    18     val setup : theory -> theory
    19     val get_congs : theory -> thm list
    20 end
    21 
    22 
    23 structure FundefPackage : FUNDEF_PACKAGE =
    24 struct
    25 
    26 open FundefCommon
    27 
    28 
    29 fun add_simps label moreatts (MutualPart {f_name, ...}, psimps) spec_part thy =
    30     let 
    31       val psimpss = Library.unflat (map snd spec_part) psimps
    32       val (names, attss) = split_list (map fst spec_part) 
    33 
    34       val thy = thy |> Theory.add_path f_name 
    35                 
    36       val thy = thy |> Theory.add_path label
    37       val spsimpss = map (map standard) psimpss (* FIXME *)
    38       val add_list = (names ~~ spsimpss) ~~ attss
    39       val (_, thy) = PureThy.add_thmss add_list thy
    40       val thy = thy |> Theory.parent_path
    41                 
    42       val (_, thy) = PureThy.add_thmss [((label, flat spsimpss), Simplifier.simp_add :: moreatts)] thy
    43       val thy = thy |> Theory.parent_path
    44     in
    45       thy
    46     end
    47     
    48 
    49 
    50 
    51 
    52 
    53 fun fundef_afterqed congs mutual_info name data spec [[result]] thy =
    54     let
    55 	val fundef_data = FundefMutual.mk_partial_rules_mutual thy mutual_info data result
    56 	val FundefMResult {psimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = fundef_data
    57         val Mutual {parts, ...} = mutual_info
    58 
    59 	val Prep {names = Names {acc_R=accR, ...}, ...} = data
    60 	val dom_abbrev = Logic.mk_equals (Free (name ^ "_dom", fastype_of accR), accR)
    61 	val (_, thy) = LocalTheory.mapping NONE (Specification.abbreviation_i ("", false) [(NONE, dom_abbrev)]) thy
    62 
    63         val thy = fold2 (add_simps "psimps" []) (parts ~~ psimps) spec thy
    64 
    65         val casenames = flat (map (map (fst o fst)) spec)
    66 
    67 	val thy = thy |> Theory.add_path name
    68 	val (_, thy) = PureThy.add_thms [(("cases", cases), [RuleCases.case_names casenames])] thy
    69 	val (_, thy) = PureThy.add_thmss [(("domintros", domintros), [])] thy
    70 	val (_, thy) = PureThy.add_thms [(("termination", standard termination), [])] thy
    71 	val (_,thy) = PureThy.add_thmss [(("pinduct", map standard simple_pinducts), [RuleCases.case_names casenames, InductAttrib.induct_set ""])] thy
    72 	val thy = thy |> Theory.parent_path
    73     in
    74       add_fundef_data name (fundef_data, mutual_info, spec) thy
    75     end
    76 
    77 fun gen_add_fundef prep_att eqns_attss (preprocess : bool) thy =
    78     let
    79       fun prep_eqns neqs =
    80           neqs
    81             |> map (apsnd (Sign.read_prop thy))    
    82             |> map (apfst (apsnd (apfst (map (prep_att thy)))))
    83             |> FundefSplit.split_some_equations (ProofContext.init thy)
    84       
    85       val spec = map prep_eqns eqns_attss
    86       val t_eqnss = map (flat o map snd) spec
    87 
    88 (*
    89  val t_eqns = if preprocess then map (FundefSplit.split_all_equations (ProofContext.init thy)) t_eqns
    90               else t_eqns
    91 *)
    92 
    93       val congs = get_fundef_congs (Context.Theory thy)
    94 
    95       val (mutual_info, name, (data, thy)) = FundefMutual.prepare_fundef_mutual congs t_eqnss thy
    96       val Prep {goal, goalI, ...} = data
    97     in
    98 	thy |> ProofContext.init
    99 	    |> Proof.theorem_i PureThy.internalK NONE (fundef_afterqed congs mutual_info name data spec) NONE ("", [])
   100 	    [(("", []), [(goal, [])])]
   101             |> Proof.refine (Method.primitive_text (fn _ => goalI))
   102             |> Seq.hd
   103     end
   104 
   105 
   106 fun total_termination_afterqed name (Mutual {parts, ...}) thmss thy =
   107     let
   108 	val totality = hd (hd thmss)
   109 
   110 	val (FundefMResult {psimps, simple_pinducts, ... }, Mutual {parts, ...}, spec)
   111 	  = the (get_fundef_data name thy)
   112 
   113 	val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
   114 
   115 	val tsimps = map (map remove_domain_condition) psimps
   116 	val tinduct = map remove_domain_condition simple_pinducts
   117 
   118         val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) (flat tsimps)
   119         val allatts = if has_guards then [] else [RecfunCodegen.add NONE]
   120 
   121         val thy = fold2 (add_simps "simps" allatts) (parts ~~ tsimps) spec thy
   122 
   123 	val thy = Theory.add_path name thy
   124 		  
   125 	val (_, thy) = PureThy.add_thmss [(("induct", map standard tinduct), [])] thy 
   126 	val thy = Theory.parent_path thy
   127     in
   128 	thy
   129     end
   130 
   131 (*
   132 fun mk_partial_rules name D_name D domT idomT thmss thy =
   133     let
   134 	val [subs, dcl] = (hd thmss)
   135 
   136 	val {f_const, f_curried_const, G_const, R_const, G_elims, completeness, f_simps, names_attrs, subset_induct, ... }
   137 	  = the (Symtab.lookup (FundefData.get thy) name)
   138 
   139 	val D_implies_dom = subs COMP (instantiate' [SOME (ctyp_of thy idomT)] 
   140 						    [SOME (cterm_of thy D)]
   141 						    subsetD)
   142 
   143 	val D_simps = map (curry op RS D_implies_dom) f_simps
   144 
   145 	val D_induct = subset_induct
   146 			   |> cterm_instantiate [(cterm_of thy (Var (("D",0), fastype_of D)) ,cterm_of thy D)]
   147 			   |> curry op COMP subs
   148 			   |> curry op COMP (dcl |> forall_intr (cterm_of thy (Var (("z",0), idomT)))
   149 						 |> forall_intr (cterm_of thy (Var (("x",0), idomT))))
   150 
   151 	val ([tinduct'], thy2) = PureThy.add_thms [((name ^ "_" ^ D_name ^ "_induct", D_induct), [])] thy
   152 	val ([tsimps'], thy3) = PureThy.add_thmss [((name ^ "_" ^ D_name ^ "_simps", D_simps), [])] thy2
   153     in
   154 	thy3
   155     end
   156 *)
   157  
   158 
   159 fun fundef_setup_termination_proof name NONE thy = 
   160     let
   161 	val name = if name = "" then get_last_fundef thy else name
   162 	val data = the (get_fundef_data name thy)
   163                    handle Option.Option => raise ERROR ("No such function definition: " ^ name)
   164 
   165 	val (res as FundefMResult {termination, ...}, mutual, _) = data
   166 	val goal = FundefTermination.mk_total_termination_goal data
   167     in
   168 	thy |> ProofContext.init
   169 	    |> ProofContext.note_thmss_i [(("termination", 
   170 					    [ContextRules.intro_query NONE]), [([standard termination], [])])] |> snd
   171 	    |> Proof.theorem_i PureThy.internalK NONE (total_termination_afterqed name mutual) NONE ("", [])
   172 	    [(("", []), [(goal, [])])]
   173     end	
   174   | fundef_setup_termination_proof name (SOME (dom_name, dom)) thy =
   175     let
   176 	val name = if name = "" then get_last_fundef thy else name
   177 	val data = the (get_fundef_data name thy)
   178 	val (subs, dcl) = FundefTermination.mk_partial_termination_goal thy data dom
   179     in
   180 	thy |> ProofContext.init
   181 	    |> Proof.theorem_i PureThy.internalK NONE (K I) NONE ("", [])
   182 	    [(("", []), [(subs, []), (dcl, [])])]
   183     end	
   184 
   185 
   186 val add_fundef = gen_add_fundef Attrib.attribute
   187 
   188 
   189 
   190 (* congruence rules *)
   191 
   192 val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq);
   193 val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq);
   194 
   195 
   196 (* setup *)
   197 
   198 val setup = FundefData.init #> FundefCongs.init 
   199 	#>  Attrib.add_attributes
   200 		[("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
   201 
   202 
   203 val get_congs = FundefCommon.get_fundef_congs o Context.Theory
   204 
   205 
   206 (* outer syntax *)
   207 
   208 local structure P = OuterParse and K = OuterKeyword in
   209 
   210 
   211 
   212 val star = Scan.one (fn t => (OuterLex.val_of t = "*"));
   213 
   214 
   215 val attribs_with_star = P.$$$ "[" |-- P.!!! ((P.list (star >> K NONE || P.attrib >> SOME)) 
   216                                                >> (fn x => (map_filter I x, exists is_none x)))
   217                               --| P.$$$ "]";
   218 
   219 val opt_attribs_with_star = Scan.optional attribs_with_star ([], false);
   220 
   221 fun opt_thm_name_star s =
   222   Scan.optional ((P.name -- opt_attribs_with_star || (attribs_with_star >> pair "")) --| P.$$$ s) ("", ([], false));
   223 
   224 
   225 val function_decl =
   226     Scan.repeat1 (opt_thm_name_star ":" -- P.prop);
   227 
   228 val functionP =
   229   OuterSyntax.command "function" "define general recursive functions" K.thy_goal
   230   (((Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "pre" -- P.$$$ ")") >> K true) false) --    
   231   P.and_list1 function_decl) >> (fn (prepr, eqnss) =>
   232                                     Toplevel.print o Toplevel.theory_to_proof (add_fundef eqnss prepr)));
   233 
   234 val terminationP =
   235   OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
   236   ((Scan.optional P.name "" -- Scan.option (P.$$$ "(" |-- Scan.optional (P.name --| P.$$$ ":") "dom" -- P.term --| P.$$$ ")"))
   237        >> (fn (name,dom) =>
   238 	      Toplevel.print o Toplevel.theory_to_proof (fundef_setup_termination_proof name dom)));
   239 
   240 val _ = OuterSyntax.add_parsers [functionP];
   241 val _ = OuterSyntax.add_parsers [terminationP];
   242 
   243 
   244 end;
   245 
   246 
   247 end