src/HOL/Tools/function_package/fundef_package.ML
author krauss
Sun May 07 00:00:13 2006 +0200 (2006-05-07)
changeset 19583 c5fa77b03442
parent 19564 d3e2f532459a
child 19585 70a1ce3b23ae
permissions -rw-r--r--
function-package: Changed record usage to make sml/nj happy...
     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 -> 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 end
    19 
    20 
    21 structure FundefPackage : FUNDEF_PACKAGE =
    22 struct
    23 
    24 open FundefCommon
    25 
    26 val True_implies = thm "True_implies"
    27 
    28 
    29 fun fundef_afterqed congs curry_info name data natts thmss thy =
    30     let
    31 	val (complete_thm :: compat_thms) = map hd thmss
    32 	val fundef_data = FundefProof.mk_partial_rules_curried thy congs curry_info data (freezeT complete_thm) (map freezeT compat_thms)
    33 	val FundefResult {psimps, subset_pinduct, simple_pinduct, total_intro, dom_intros, ...} = fundef_data
    34 
    35 	val (names, attsrcs) = split_list natts
    36 	val atts = map (map (Attrib.attribute thy)) attsrcs
    37 
    38 	val Prep {names = Names {acc_R=accR, ...}, ...} = data
    39 	val dom_abbrev = Logic.mk_equals (Free (name ^ "_dom", fastype_of accR), accR)
    40 	val (_, thy) = LocalTheory.mapping NONE (Specification.abbreviation_i ("", false) [(NONE, dom_abbrev)]) thy
    41 
    42 	val thy = thy |> Theory.add_path name 
    43 
    44 	val thy = thy |> Theory.add_path "psimps"
    45 	val (_, thy) = PureThy.add_thms ((names ~~ psimps) ~~ atts) thy;
    46 	val thy = thy |> Theory.parent_path
    47 
    48 	val (_, thy) = PureThy.add_thms [(("cases", complete_thm), [RuleCases.case_names names])] thy
    49 	val (_, thy) = PureThy.add_thmss [(("domintros", dom_intros), [])] thy
    50 	val (_, thy) = PureThy.add_thms [(("termination", total_intro), [])] thy
    51 	val (_,thy) = PureThy.add_thms [(("pinduct", simple_pinduct), [RuleCases.case_names names, InductAttrib.induct_set ""])] thy
    52 	val (_, thy) = PureThy.add_thmss [(("psimps", psimps), [Simplifier.simp_add])] thy
    53 	val thy = thy |> Theory.parent_path
    54     in
    55 	add_fundef_data name fundef_data thy
    56     end
    57 
    58 fun add_fundef eqns_atts thy =
    59     let
    60 	val (natts, eqns) = split_list eqns_atts
    61 
    62 	val congs = get_fundef_congs (Context.Theory thy)
    63 
    64 	val (curry_info, name, (data, thy)) = FundefPrep.prepare_fundef_curried congs (map (Sign.read_prop thy) eqns) thy
    65 	val Prep {complete, compat, ...} = data
    66 
    67 	val props = (complete :: compat) (*(complete :: fst (chop 110 compat))*)
    68     in
    69 	thy |> ProofContext.init
    70 	    |> Proof.theorem_i PureThy.internalK NONE (fundef_afterqed congs curry_info name data natts) NONE ("", [])
    71 	    (map (fn t => (("", []), [(t, ([], []))])) props)
    72     end
    73 
    74 
    75 fun total_termination_afterqed name thmss thy =
    76     let
    77 	val totality = hd (hd thmss)
    78 
    79 	val FundefResult {psimps, simple_pinduct, ... }
    80 	  = the (get_fundef_data name thy)
    81 
    82 	val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies])
    83 
    84 	val tsimps = map remove_domain_condition psimps
    85 	val tinduct = remove_domain_condition simple_pinduct
    86 
    87 	val thy = Theory.add_path name thy
    88 		  
    89 		  (* Need the names and attributes. Apply the attributes again? *)
    90 (*	val thy = thy |> Theory.add_path "simps"
    91 	val (_, thy) = PureThy.add_thms ((names ~~ tsimps) ~~ atts) thy;
    92 	val thy = thy |> Theory.parent_path*)
    93 
    94 	val (_, thy) = PureThy.add_thms [(("induct", tinduct), [])] thy 
    95 	val (_, thy) = PureThy.add_thmss [(("simps", tsimps), [Simplifier.simp_add, RecfunCodegen.add NONE])] thy
    96 	val thy = Theory.parent_path thy
    97     in
    98 	thy
    99     end
   100 
   101 (*
   102 fun mk_partial_rules name D_name D domT idomT thmss thy =
   103     let
   104 	val [subs, dcl] = (hd thmss)
   105 
   106 	val {f_const, f_curried_const, G_const, R_const, G_elims, completeness, f_simps, names_attrs, subset_induct, ... }
   107 	  = the (Symtab.lookup (FundefData.get thy) name)
   108 
   109 	val D_implies_dom = subs COMP (instantiate' [SOME (ctyp_of thy idomT)] 
   110 						    [SOME (cterm_of thy D)]
   111 						    subsetD)
   112 
   113 	val D_simps = map (curry op RS D_implies_dom) f_simps
   114 
   115 	val D_induct = subset_induct
   116 			   |> cterm_instantiate [(cterm_of thy (Var (("D",0), fastype_of D)) ,cterm_of thy D)]
   117 			   |> curry op COMP subs
   118 			   |> curry op COMP (dcl |> forall_intr (cterm_of thy (Var (("z",0), idomT)))
   119 						 |> forall_intr (cterm_of thy (Var (("x",0), idomT))))
   120 
   121 	val ([tinduct'], thy2) = PureThy.add_thms [((name ^ "_" ^ D_name ^ "_induct", D_induct), [])] thy
   122 	val ([tsimps'], thy3) = PureThy.add_thmss [((name ^ "_" ^ D_name ^ "_simps", D_simps), [])] thy2
   123     in
   124 	thy3
   125     end
   126 *)
   127  
   128 
   129 fun fundef_setup_termination_proof name NONE thy = 
   130     let
   131 	val name = if name = "" then get_last_fundef thy else name
   132 	val data = the (get_fundef_data name thy)
   133 
   134 	val FundefResult {total_intro, ...} = data
   135 	val goal = FundefTermination.mk_total_termination_goal data
   136     in
   137 	thy |> ProofContext.init
   138 	    |> ProofContext.note_thmss_i [(("termination_intro", 
   139 					    [ContextRules.intro_query NONE]), [([total_intro], [])])] |> snd
   140 	    |> Proof.theorem_i PureThy.internalK NONE (total_termination_afterqed name) NONE ("", [])
   141 	    [(("", []), [(goal, ([], []))])]
   142     end	
   143   | fundef_setup_termination_proof name (SOME (dom_name, dom)) thy =
   144     let
   145 	val name = if name = "" then get_last_fundef thy else name
   146 	val data = the (get_fundef_data name thy)
   147 	val (subs, dcl) = FundefTermination.mk_partial_termination_goal thy data dom
   148     in
   149 	thy |> ProofContext.init
   150 	    |> Proof.theorem_i PureThy.internalK NONE (K I) NONE ("", [])
   151 	    [(("", []), [(subs, ([], [])), (dcl, ([], []))])]
   152     end	
   153 
   154 
   155 
   156 
   157 (* congruence rules *)
   158 
   159 val cong_add = Thm.declaration_attribute (map_fundef_congs o cons o safe_mk_meta_eq);
   160 val cong_del = Thm.declaration_attribute (map_fundef_congs o remove (op =) o safe_mk_meta_eq);
   161 
   162 
   163 (* setup *)
   164 
   165 val setup = FundefData.init #> FundefCongs.init 
   166 	#>  Attrib.add_attributes
   167 		[("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
   168 
   169 
   170 (* outer syntax *)
   171 
   172 local structure P = OuterParse and K = OuterKeyword in
   173 
   174 val function_decl =
   175     Scan.repeat1 (P.opt_thm_name ":" -- P.prop);
   176 
   177 val functionP =
   178   OuterSyntax.command "function" "define general recursive functions" K.thy_goal
   179     (function_decl >> (fn eqns =>
   180       Toplevel.print o Toplevel.theory_to_proof (add_fundef eqns)));
   181 
   182 val terminationP =
   183   OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
   184   ((Scan.optional P.name "" -- Scan.option (P.$$$ "(" |-- Scan.optional (P.name --| P.$$$ ":") "dom" -- P.term --| P.$$$ ")"))
   185        >> (fn (name,dom) =>
   186 	      Toplevel.print o Toplevel.theory_to_proof (fundef_setup_termination_proof name dom)));
   187 
   188 val _ = OuterSyntax.add_parsers [functionP];
   189 val _ = OuterSyntax.add_parsers [terminationP];
   190 
   191 
   192 end;
   193 
   194 
   195 end