src/HOL/Tools/function_package/fundef_package.ML
author krauss
Fri Aug 04 18:01:45 2006 +0200 (2006-08-04)
changeset 20338 ecdfc96cf4d0
parent 20320 a5368278a72c
child 20363 f34c5dbe74d5
permissions -rw-r--r--
Added Keywords: "otherwise" and "sequential", needed for function package's
sequential mode.
     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       val congs = get_fundef_congs (Context.Theory thy)
    89 
    90       val (mutual_info, name, (data, thy)) = FundefMutual.prepare_fundef_mutual congs t_eqnss thy
    91       val Prep {goal, goalI, ...} = data
    92     in
    93 	thy |> ProofContext.init
    94 	    |> Proof.theorem_i PureThy.internalK NONE (fundef_afterqed congs mutual_info name data spec) NONE ("", [])
    95 	    [(("", []), [(goal, [])])]
    96             |> Proof.refine (Method.primitive_text (fn _ => goalI))
    97             |> Seq.hd
    98     end
    99 
   100 
   101 fun total_termination_afterqed name (Mutual {parts, ...}) thmss thy =
   102     let
   103 	val totality = hd (hd thmss)
   104 
   105 	val (FundefMResult {psimps, simple_pinducts, ... }, Mutual {parts, ...}, spec)
   106 	  = the (get_fundef_data name thy)
   107 
   108 	val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
   109 
   110 	val tsimps = map (map remove_domain_condition) psimps
   111 	val tinduct = map remove_domain_condition simple_pinducts
   112 
   113         val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) (flat tsimps)
   114         val allatts = if has_guards then [] else [RecfunCodegen.add NONE]
   115 
   116         val thy = fold2 (add_simps "simps" allatts) (parts ~~ tsimps) spec thy
   117 
   118 	val thy = Theory.add_path name thy
   119 		  
   120 	val (_, thy) = PureThy.add_thmss [(("induct", map standard tinduct), [])] thy 
   121 	val thy = Theory.parent_path thy
   122     in
   123 	thy
   124     end
   125 
   126 (*
   127 fun mk_partial_rules name D_name D domT idomT thmss thy =
   128     let
   129 	val [subs, dcl] = (hd thmss)
   130 
   131 	val {f_const, f_curried_const, G_const, R_const, G_elims, completeness, f_simps, names_attrs, subset_induct, ... }
   132 	  = the (Symtab.lookup (FundefData.get thy) name)
   133 
   134 	val D_implies_dom = subs COMP (instantiate' [SOME (ctyp_of thy idomT)] 
   135 						    [SOME (cterm_of thy D)]
   136 						    subsetD)
   137 
   138 	val D_simps = map (curry op RS D_implies_dom) f_simps
   139 
   140 	val D_induct = subset_induct
   141 			   |> cterm_instantiate [(cterm_of thy (Var (("D",0), fastype_of D)) ,cterm_of thy D)]
   142 			   |> curry op COMP subs
   143 			   |> curry op COMP (dcl |> forall_intr (cterm_of thy (Var (("z",0), idomT)))
   144 						 |> forall_intr (cterm_of thy (Var (("x",0), idomT))))
   145 
   146 	val ([tinduct'], thy2) = PureThy.add_thms [((name ^ "_" ^ D_name ^ "_induct", D_induct), [])] thy
   147 	val ([tsimps'], thy3) = PureThy.add_thmss [((name ^ "_" ^ D_name ^ "_simps", D_simps), [])] thy2
   148     in
   149 	thy3
   150     end
   151 *)
   152  
   153 
   154 fun fundef_setup_termination_proof name NONE thy = 
   155     let
   156 	val name = if name = "" then get_last_fundef thy else name
   157 	val data = the (get_fundef_data name thy)
   158                    handle Option.Option => raise ERROR ("No such function definition: " ^ name)
   159 
   160 	val (res as FundefMResult {termination, ...}, mutual, _) = data
   161 	val goal = FundefTermination.mk_total_termination_goal data
   162     in
   163 	thy |> ProofContext.init
   164 	    |> ProofContext.note_thmss_i [(("termination", 
   165 					    [ContextRules.intro_query NONE]), [([standard termination], [])])] |> snd
   166 	    |> Proof.theorem_i PureThy.internalK NONE (total_termination_afterqed name mutual) NONE ("", [])
   167 	    [(("", []), [(goal, [])])]
   168     end	
   169   | fundef_setup_termination_proof name (SOME (dom_name, dom)) thy =
   170     let
   171 	val name = if name = "" then get_last_fundef thy else name
   172 	val data = the (get_fundef_data name thy)
   173 	val (subs, dcl) = FundefTermination.mk_partial_termination_goal thy data dom
   174     in
   175 	thy |> ProofContext.init
   176 	    |> Proof.theorem_i PureThy.internalK NONE (K I) NONE ("", [])
   177 	    [(("", []), [(subs, []), (dcl, [])])]
   178     end	
   179 
   180 
   181 val add_fundef = gen_add_fundef Attrib.attribute
   182 
   183 
   184 
   185 (* congruence rules *)
   186 
   187 val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq);
   188 val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq);
   189 
   190 
   191 (* setup *)
   192 
   193 val setup = FundefData.init #> FundefCongs.init 
   194 	#>  Attrib.add_attributes
   195 		[("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
   196 
   197 
   198 val get_congs = FundefCommon.get_fundef_congs o Context.Theory
   199 
   200 
   201 (* outer syntax *)
   202 
   203 local structure P = OuterParse and K = OuterKeyword in
   204 
   205 
   206 
   207 val star = Scan.one (fn t => (OuterLex.val_of t = "*"));
   208 
   209 
   210 val attribs_with_star = P.$$$ "[" |-- P.!!! ((P.list (star >> K NONE || P.attrib >> SOME)) 
   211                                                >> (fn x => (map_filter I x, exists is_none x)))
   212                               --| P.$$$ "]";
   213 
   214 val opt_attribs_with_star = Scan.optional attribs_with_star ([], false);
   215 
   216 val opt_thm_name_star =
   217   Scan.optional ((P.name -- opt_attribs_with_star || (attribs_with_star >> pair "")) --| P.$$$ ":") ("", ([], false));
   218 
   219 
   220 val function_decl =
   221     Scan.repeat1 (opt_thm_name_star -- P.prop);
   222 
   223 val functionP =
   224   OuterSyntax.command "function" "define general recursive functions" K.thy_goal
   225   (((Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "sequential" -- P.$$$ ")") >> K true) false) --    
   226   P.and_list1 function_decl) >> (fn (prepr, eqnss) =>
   227                                     Toplevel.print o Toplevel.theory_to_proof (add_fundef eqnss prepr)));
   228 
   229 val terminationP =
   230   OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
   231   ((Scan.optional P.name "" -- Scan.option (P.$$$ "(" |-- Scan.optional (P.name --| P.$$$ ":") "dom" -- P.term --| P.$$$ ")"))
   232        >> (fn (name,dom) =>
   233 	      Toplevel.print o Toplevel.theory_to_proof (fundef_setup_termination_proof name dom)));
   234 
   235 val _ = OuterSyntax.add_keywords ["sequential", "otherwise"];
   236 
   237 val _ = OuterSyntax.add_parsers [functionP];
   238 val _ = OuterSyntax.add_parsers [terminationP];
   239 
   240 
   241 end;
   242 
   243 
   244 end