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