src/HOL/Tools/function_package/fundef_package.ML
author wenzelm
Tue Nov 14 15:29:52 2006 +0100 (2006-11-14)
changeset 21359 072e83a0b5bb
parent 21319 cf814e36f788
child 21391 a8809f46bd7f
permissions -rw-r--r--
simplified Proof.theorem(_i) interface;
krauss@19564
     1
(*  Title:      HOL/Tools/function_package/fundef_package.ML
krauss@19564
     2
    ID:         $Id$
krauss@19564
     3
    Author:     Alexander Krauss, TU Muenchen
krauss@19564
     4
wenzelm@20363
     5
A package for general recursive function definitions.
krauss@19564
     6
Isar commands.
krauss@19564
     7
krauss@19564
     8
*)
krauss@19564
     9
wenzelm@20363
    10
signature FUNDEF_PACKAGE =
krauss@19564
    11
sig
wenzelm@20877
    12
    val add_fundef :  (string * string option * mixfix) list
krauss@20523
    13
                      -> ((bstring * Attrib.src list) * string list) list list
wenzelm@20877
    14
                      -> FundefCommon.fundef_config
wenzelm@20877
    15
                      -> local_theory
krauss@21211
    16
                      -> string * Proof.state
krauss@20523
    17
wenzelm@20877
    18
    val add_fundef_i:  (string * typ option * mixfix) list
krauss@20523
    19
                       -> ((bstring * Attrib.src list) * term list) list list
wenzelm@20877
    20
                       -> bool
wenzelm@20877
    21
                       -> local_theory
krauss@21211
    22
                       -> string * Proof.state
krauss@21211
    23
krauss@21211
    24
    val setup_termination_proof : string option -> local_theory -> Proof.state
krauss@19564
    25
krauss@19564
    26
    val cong_add: attribute
krauss@19564
    27
    val cong_del: attribute
wenzelm@20363
    28
krauss@19564
    29
    val setup : theory -> theory
krauss@21235
    30
    val setup_case_cong_hook : theory -> theory
krauss@19770
    31
    val get_congs : theory -> thm list
krauss@19564
    32
end
krauss@19564
    33
krauss@19564
    34
krauss@20523
    35
structure FundefPackage  =
krauss@19564
    36
struct
krauss@19564
    37
krauss@21051
    38
open FundefLib
krauss@19564
    39
open FundefCommon
krauss@19564
    40
krauss@20523
    41
fun burrow_snd f ps = (* ('a list -> 'b list) -> ('c * 'a) list -> ('c * 'b) list *)
krauss@20523
    42
    let val (xs, ys) = split_list ps
krauss@20523
    43
    in xs ~~ f ys end
krauss@20270
    44
krauss@20523
    45
fun restore_spec_structure reps spec =
krauss@20523
    46
    (burrow o burrow_snd o burrow o K) reps spec
wenzelm@20363
    47
krauss@20523
    48
fun add_simps label moreatts mutual_info fixes psimps spec lthy =
krauss@19564
    49
    let
krauss@20523
    50
      val fnames = map (fst o fst) fixes
krauss@21255
    51
krauss@21255
    52
      val (saved_spec_psimps, lthy) = fold_map (fold_map LocalTheory.note) (restore_spec_structure psimps spec) lthy
krauss@21255
    53
      val saved_psimps = flat (map snd (flat saved_spec_psimps))
krauss@21255
    54
krauss@21255
    55
      val psimps_by_f = FundefMutual.sort_by_function mutual_info fnames saved_psimps
krauss@19564
    56
krauss@20523
    57
      fun add_for_f fname psimps =
wenzelm@20877
    58
          LocalTheory.note ((NameSpace.append fname label, Attrib.internal Simplifier.simp_add :: moreatts), psimps) #> snd
krauss@19564
    59
    in
krauss@21255
    60
      (saved_psimps,
krauss@21255
    61
       fold2 add_for_f fnames psimps_by_f lthy)
krauss@19564
    62
    end
krauss@19564
    63
krauss@19564
    64
krauss@21319
    65
fun fundef_afterqed config fixes spec mutual_info defname data [[result]] lthy =
krauss@20523
    66
    let
krauss@21319
    67
      val FundefConfig { domintros, ...} = config
krauss@21255
    68
      val Prep {f, R, ...} = data
krauss@21255
    69
      val fundef_data = FundefMutual.mk_partial_rules_mutual lthy mutual_info data result
krauss@21255
    70
      val FundefMResult {psimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = fundef_data
krauss@21255
    71
      val qualify = NameSpace.append defname
krauss@21255
    72
          
krauss@21255
    73
      val (((psimps', pinducts'), (_, [termination'])), lthy) = 
krauss@21255
    74
          lthy
krauss@21255
    75
            |> PROFILE "adding_psimps" (add_simps "psimps" [] mutual_info fixes psimps spec)
krauss@21255
    76
            ||>> PROFILE "adding pinduct" (LocalTheory.note ((qualify "pinduct", [Attrib.internal (InductAttrib.induct_set "")]), simple_pinducts))
krauss@21255
    77
            ||>> PROFILE "adding termination" (LocalTheory.note ((qualify "termination", []), [termination]))
krauss@21255
    78
            ||> (snd o PROFILE "adding cases" (LocalTheory.note ((qualify "cases", []), [cases])))
krauss@21255
    79
            ||> (snd o PROFILE "adding domintros" (LocalTheory.note ((qualify "domintros", []), domintros)))
krauss@21255
    80
krauss@21255
    81
      val cdata = FundefCtxData { fixes=fixes, spec=spec, mutual=mutual_info, psimps=psimps',
krauss@21255
    82
                                  pinducts=snd pinducts', termination=termination', f=f, R=R }
krauss@21255
    83
krauss@21255
    84
      
krauss@20523
    85
    in
krauss@20523
    86
      lthy
krauss@21255
    87
        |> LocalTheory.declaration (add_fundef_data defname cdata) (* save in target *)
krauss@21255
    88
        |> Context.proof_map (add_fundef_data defname cdata) (* also save in local context *)
krauss@20523
    89
    end (* FIXME: Add cases for induct and cases thm *)
krauss@20523
    90
krauss@20523
    91
krauss@20523
    92
krauss@20523
    93
fun prep_with_flags prep fixspec eqnss_flags global_flag lthy =
krauss@19564
    94
    let
krauss@20523
    95
      val eqnss = map (map (apsnd (map fst))) eqnss_flags
krauss@20523
    96
      val flags = map (map (map (fn (_, f) => global_flag orelse f) o snd)) eqnss_flags
krauss@20523
    97
krauss@20523
    98
      val ((fixes, _), ctxt') = prep fixspec [] lthy
krauss@20523
    99
      val spec = map (fn eqns => snd (fst (prep [] eqns ctxt'))) eqnss
krauss@20523
   100
                     |> map (map (apsnd (map (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t)))) (* Add quantifiers *)
krauss@20523
   101
                     |> map2 (map2 (fn fs => fn (r, thms) => (r, fs ~~ thms))) flags
wenzelm@20877
   102
                     |> (burrow o burrow_snd o burrow)
krauss@20523
   103
                          (FundefSplit.split_some_equations lthy)
krauss@20523
   104
                     |> map (map (apsnd flat))
krauss@20523
   105
    in
krauss@20523
   106
      ((fixes, spec), ctxt')
krauss@20523
   107
    end
krauss@20523
   108
krauss@19564
   109
krauss@20654
   110
fun gen_add_fundef prep_spec fixspec eqnss_flags config lthy =
krauss@20523
   111
    let
krauss@20654
   112
      val FundefConfig {sequential, default, ...} = config
krauss@20654
   113
krauss@20654
   114
      val ((fixes, spec), ctxt') = prep_with_flags prep_spec fixspec eqnss_flags sequential lthy
krauss@20523
   115
      val t_eqns = spec
krauss@20523
   116
                     |> flat |> map snd |> flat (* flatten external structure *)
krauss@20523
   117
wenzelm@20877
   118
      val ((mutual_info, name, prep_result as Prep {goal, goalI, ...}), lthy) =
krauss@20654
   119
          FundefMutual.prepare_fundef_mutual fixes t_eqns default lthy
krauss@20523
   120
krauss@21319
   121
      val afterqed = fundef_afterqed config fixes spec mutual_info name prep_result
krauss@20523
   122
    in
krauss@21211
   123
      (name, lthy
wenzelm@21359
   124
         |> Proof.theorem_i PureThy.internalK NONE afterqed [[(goal, [])]]
krauss@21211
   125
         |> Proof.refine (Method.primitive_text (fn _ => goalI)) |> Seq.hd)
krauss@20523
   126
    end
krauss@20523
   127
krauss@20523
   128
krauss@20523
   129
fun total_termination_afterqed defname data [[totality]] lthy =
krauss@20523
   130
    let
krauss@21255
   131
      val FundefCtxData { fixes, spec, mutual, psimps, pinducts, ... } = data
krauss@21255
   132
krauss@21255
   133
      val totality = PROFILE "closing" Drule.close_derivation totality
krauss@19564
   134
krauss@21255
   135
      val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
krauss@19564
   136
krauss@21255
   137
      val tsimps = PROFILE "making tsimps" (map remove_domain_condition) psimps
krauss@21255
   138
      val tinduct = PROFILE "making tinduct" (map remove_domain_condition) pinducts
krauss@19770
   139
wenzelm@20877
   140
        (* FIXME: How to generate code from (possibly) local contexts
krauss@20523
   141
        val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps
krauss@20523
   142
        val allatts = if has_guards then [] else [Attrib.internal (RecfunCodegen.add NONE)]
krauss@20523
   143
        *)
wenzelm@20877
   144
        val qualify = NameSpace.append defname;
krauss@19564
   145
    in
krauss@20523
   146
        lthy
krauss@21255
   147
          |> PROFILE "adding tsimps" (add_simps "simps" [] mutual fixes tsimps spec) |> snd
krauss@21255
   148
          |> PROFILE "adding tinduct" (LocalTheory.note ((qualify "induct", []), tinduct)) |> snd
wenzelm@20363
   149
    end
krauss@19564
   150
krauss@19564
   151
krauss@21211
   152
fun setup_termination_proof name_opt lthy =
krauss@20523
   153
    let
krauss@20523
   154
        val name = the_default (get_last_fundef (Context.Proof lthy)) name_opt
krauss@20523
   155
        val data = the (get_fundef_data name (Context.Proof lthy))
krauss@20523
   156
                   handle Option.Option => raise ERROR ("No such function definition: " ^ name)
krauss@20523
   157
krauss@21255
   158
        val FundefCtxData {termination, f, R, ...} = data
krauss@21255
   159
        val goal = FundefTermination.mk_total_termination_goal f R
krauss@20523
   160
    in
wenzelm@20877
   161
      lthy
krauss@20523
   162
        |> ProofContext.note_thmss_i [(("termination",
krauss@21237
   163
                                        [ContextRules.intro_query NONE]), [(ProofContext.standard lthy [termination], [])])] |> snd
krauss@21319
   164
        |> set_termination_rule termination
krauss@20523
   165
        |> Proof.theorem_i PureThy.internalK NONE
wenzelm@21359
   166
                           (total_termination_afterqed name data) [[(goal, [])]]
krauss@20523
   167
    end
krauss@20523
   168
krauss@20523
   169
krauss@20523
   170
val add_fundef = gen_add_fundef Specification.read_specification
krauss@20523
   171
val add_fundef_i = gen_add_fundef Specification.cert_specification
krauss@19611
   172
krauss@19564
   173
krauss@19564
   174
krauss@19564
   175
(* congruence rules *)
krauss@19564
   176
wenzelm@19617
   177
val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq);
wenzelm@19617
   178
val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq);
krauss@19564
   179
krauss@21235
   180
(* Datatype hook to declare datatype congs as "fundef_congs" *)
krauss@21235
   181
krauss@21235
   182
krauss@21235
   183
fun add_case_cong n thy = 
krauss@21235
   184
    Context.theory_map (map_fundef_congs (Drule.add_rule 
krauss@21235
   185
                          (DatatypePackage.get_datatype thy n |> the
krauss@21235
   186
                           |> #case_cong
krauss@21235
   187
                           |> safe_mk_meta_eq))) 
krauss@21235
   188
                       thy
krauss@21235
   189
krauss@21235
   190
val case_cong_hook = fold add_case_cong
krauss@21235
   191
krauss@21235
   192
val setup_case_cong_hook = 
krauss@21235
   193
    DatatypeHooks.add case_cong_hook
krauss@21235
   194
    #> (fn thy => case_cong_hook (Symtab.keys (DatatypePackage.get_datatypes thy)) thy)
krauss@19564
   195
krauss@19564
   196
(* setup *)
krauss@19564
   197
krauss@21235
   198
val setup = 
krauss@21235
   199
    FundefData.init 
krauss@21235
   200
      #> FundefCongs.init
krauss@21319
   201
      #> TerminationRule.init
krauss@21235
   202
      #>  Attrib.add_attributes
krauss@21235
   203
            [("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
krauss@21235
   204
      #> setup_case_cong_hook
krauss@21319
   205
      #> FundefRelation.setup
krauss@19564
   206
krauss@19770
   207
val get_congs = FundefCommon.get_fundef_congs o Context.Theory
krauss@19770
   208
krauss@19770
   209
krauss@21235
   210
krauss@19564
   211
(* outer syntax *)
krauss@19564
   212
krauss@19564
   213
local structure P = OuterParse and K = OuterKeyword in
krauss@19564
   214
krauss@20270
   215
krauss@20270
   216
krauss@20523
   217
fun or_list1 s = P.enum1 "|" s
krauss@20523
   218
krauss@20523
   219
val opt_sequential = Scan.optional ((P.$$$ "(" |-- P.$$$ "sequential" --| P.$$$ ")") >> K true) false
krauss@20523
   220
krauss@20523
   221
val otherwise = P.$$$ "(" |-- P.$$$ "otherwise" --| P.$$$ ")"
krauss@20523
   222
val statement_ow = P.and_list1 (P.opt_thm_name ":" -- Scan.repeat1 (P.prop -- Scan.optional (otherwise >> K true) false))
krauss@20523
   223
val statements_ow = or_list1 statement_ow
krauss@20270
   224
krauss@20270
   225
krauss@19564
   226
val functionP =
krauss@19564
   227
  OuterSyntax.command "function" "define general recursive functions" K.thy_goal
krauss@21051
   228
  ((config_parser default_config -- P.opt_locale_target -- P.fixes --| P.$$$ "where" -- statements_ow)
krauss@20654
   229
     >> (fn (((config, target), fixes), statements) =>
krauss@21211
   230
            Toplevel.local_theory_to_proof target (add_fundef fixes statements config #> snd)
krauss@21211
   231
            #> Toplevel.print));
krauss@20523
   232
krauss@19564
   233
krauss@21051
   234
krauss@19564
   235
val terminationP =
krauss@19564
   236
  OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
wenzelm@21000
   237
  (P.opt_locale_target -- Scan.option P.name
wenzelm@21000
   238
    >> (fn (target, name) =>
wenzelm@21000
   239
           Toplevel.print o
krauss@21211
   240
           Toplevel.local_theory_to_proof target (setup_termination_proof name)));
krauss@19564
   241
krauss@20338
   242
krauss@19564
   243
val _ = OuterSyntax.add_parsers [functionP];
krauss@19564
   244
val _ = OuterSyntax.add_parsers [terminationP];
krauss@20654
   245
val _ = OuterSyntax.add_keywords ["sequential", "otherwise"];
krauss@19564
   246
krauss@19564
   247
end;
krauss@19564
   248
krauss@19564
   249
wenzelm@19585
   250
end