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