src/HOL/Tools/function_package/fundef_package.ML
author wenzelm
Wed Jun 25 17:38:32 2008 +0200 (2008-06-25)
changeset 27353 71c4dd53d4cb
parent 27187 17b63e145986
child 28083 103d9282a946
permissions -rw-r--r--
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
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@23203
    13
                      -> ((bstring * Attrib.src list) * string) list 
wenzelm@20877
    14
                      -> FundefCommon.fundef_config
krauss@23203
    15
                      -> bool list
wenzelm@20877
    16
                      -> local_theory
krauss@22733
    17
                      -> Proof.state
krauss@20523
    18
wenzelm@20877
    19
    val add_fundef_i:  (string * typ option * mixfix) list
krauss@23203
    20
                       -> ((bstring * Attrib.src list) * term) list
krauss@22170
    21
                       -> FundefCommon.fundef_config
krauss@23203
    22
                       -> bool list
wenzelm@20877
    23
                       -> local_theory
krauss@22733
    24
                       -> Proof.state
krauss@21211
    25
krauss@21211
    26
    val setup_termination_proof : string option -> local_theory -> Proof.state
krauss@19564
    27
krauss@19564
    28
    val setup : theory -> theory
krauss@19770
    29
    val get_congs : theory -> thm list
krauss@19564
    30
end
krauss@19564
    31
krauss@19564
    32
krauss@23203
    33
structure FundefPackage : FUNDEF_PACKAGE =
krauss@19564
    34
struct
krauss@19564
    35
krauss@21051
    36
open FundefLib
krauss@19564
    37
open FundefCommon
krauss@19564
    38
wenzelm@21435
    39
val note_theorem = LocalTheory.note Thm.theoremK
wenzelm@21435
    40
krauss@22166
    41
fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" 
krauss@22166
    42
krauss@26529
    43
fun add_simps fnames post sort extra_qualify label moreatts simps lthy =
krauss@19564
    44
    let
krauss@23819
    45
      val atts = Attrib.internal (K Simplifier.simp_add) :: moreatts
krauss@23819
    46
      val spec = post simps
krauss@23819
    47
                   |> map (apfst (apsnd (append atts)))
krauss@26529
    48
                   |> map (apfst (apfst extra_qualify))
krauss@21255
    49
krauss@22166
    50
      val (saved_spec_simps, lthy) =
krauss@23819
    51
        fold_map note_theorem spec lthy
krauss@23819
    52
krauss@22498
    53
      val saved_simps = flat (map snd saved_spec_simps)
krauss@22166
    54
      val simps_by_f = sort saved_simps
krauss@19564
    55
krauss@22166
    56
      fun add_for_f fname simps =
krauss@23819
    57
        note_theorem ((NameSpace.qualified fname label, []), simps) #> snd
krauss@19564
    58
    in
krauss@22166
    59
      (saved_simps,
krauss@22166
    60
       fold2 add_for_f fnames simps_by_f lthy)
krauss@19564
    61
    end
krauss@19564
    62
wenzelm@26594
    63
fun fundef_afterqed do_print fixes post defname cont sort_cont cnames [[proof]] lthy =
krauss@20523
    64
    let
krauss@26529
    65
      val FundefResult {fs, R, psimps, trsimps,  simple_pinducts, termination, domintros, cases, ...} = 
wenzelm@26628
    66
          cont (Thm.close_derivation proof)
krauss@22166
    67
krauss@23819
    68
      val fnames = map (fst o fst) fixes
wenzelm@21391
    69
      val qualify = NameSpace.qualified defname
krauss@23819
    70
      val addsmps = add_simps fnames post sort_cont
wenzelm@21602
    71
wenzelm@21602
    72
      val (((psimps', pinducts'), (_, [termination'])), lthy) =
krauss@21255
    73
          lthy
krauss@26529
    74
            |> addsmps (NameSpace.qualified "partial") "psimps" [] psimps
krauss@26529
    75
            ||> fold_option (snd oo addsmps I "simps" []) trsimps
krauss@22166
    76
            ||>> note_theorem ((qualify "pinduct",
krauss@25222
    77
                   [Attrib.internal (K (RuleCases.case_names cnames)),
krauss@25222
    78
                    Attrib.internal (K (RuleCases.consumes 1)),
krauss@25222
    79
                    Attrib.internal (K (Induct.induct_pred ""))]), simple_pinducts)
krauss@22166
    80
            ||>> note_theorem ((qualify "termination", []), [termination])
krauss@25222
    81
            ||> (snd o note_theorem ((qualify "cases", 
krauss@25222
    82
                   [Attrib.internal (K (RuleCases.case_names cnames))]), [cases]))
krauss@22166
    83
            ||> fold_option (snd oo curry note_theorem (qualify "domintros", [])) domintros
krauss@21255
    84
krauss@25222
    85
      val cdata = FundefCtxData { add_simps=addsmps, case_names=cnames, psimps=psimps',
krauss@22733
    86
                                  pinducts=snd pinducts', termination=termination', fs=fs, R=R, defname=defname }
wenzelm@26594
    87
      val _ =
wenzelm@26594
    88
        if not do_print then ()
wenzelm@26594
    89
        else Specification.print_consts lthy (K false) (map fst fixes)
krauss@20523
    90
    in
krauss@22623
    91
      lthy 
krauss@25201
    92
        |> LocalTheory.declaration (add_fundef_data o morph_fundef_data cdata)
krauss@25222
    93
    end
krauss@20523
    94
krauss@20523
    95
wenzelm@26594
    96
fun gen_add_fundef is_external prep fixspec eqnss config flags lthy =
krauss@20523
    97
    let
krauss@25088
    98
      val ((fixes, spec), ctxt') = prep fixspec (map (fn (n_a, eq) => [(n_a, [eq])]) eqnss) lthy
krauss@25222
    99
      val (eqs, post, sort_cont, cnames) = FundefCommon.get_preproc lthy config flags ctxt' fixes spec
krauss@22166
   100
krauss@22166
   101
      val defname = mk_defname fixes
krauss@22166
   102
krauss@23819
   103
      val ((goalstate, cont), lthy) =
krauss@23203
   104
          FundefMutual.prepare_fundef_mutual config defname fixes eqs lthy
krauss@20523
   105
wenzelm@26594
   106
      val afterqed = fundef_afterqed is_external fixes post defname cont sort_cont cnames
krauss@20523
   107
    in
krauss@22733
   108
      lthy
wenzelm@26594
   109
        |> is_external ? LocalTheory.set_group (serial_string ())
krauss@22733
   110
        |> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]]
krauss@22733
   111
        |> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd
krauss@20523
   112
    end
krauss@20523
   113
krauss@22733
   114
fun total_termination_afterqed data [[totality]] lthy =
krauss@20523
   115
    let
krauss@25222
   116
      val FundefCtxData { add_simps, case_names, psimps, pinducts, defname, ... } = data
krauss@21255
   117
wenzelm@26628
   118
      val totality = Thm.close_derivation totality
krauss@19564
   119
krauss@21255
   120
      val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
krauss@19564
   121
krauss@22166
   122
      val tsimps = map remove_domain_condition psimps
krauss@22166
   123
      val tinduct = map remove_domain_condition pinducts
krauss@19770
   124
krauss@21511
   125
      val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps
haftmann@24624
   126
      val allatts = if has_guards then [] else [Attrib.internal (K RecfunCodegen.add_default)]
wenzelm@21602
   127
krauss@21511
   128
      val qualify = NameSpace.qualified defname;
krauss@19564
   129
    in
krauss@21511
   130
      lthy
krauss@26529
   131
        |> add_simps I "simps" allatts tsimps |> snd
krauss@25222
   132
        |> note_theorem ((qualify "induct", [Attrib.internal (K (RuleCases.case_names case_names))]), tinduct) |> snd
wenzelm@20363
   133
    end
krauss@19564
   134
krauss@19564
   135
krauss@22733
   136
fun setup_termination_proof term_opt lthy =
krauss@20523
   137
    let
krauss@22733
   138
      val data = the (case term_opt of
wenzelm@24508
   139
                        SOME t => import_fundef_data (Syntax.read_term lthy t) (Context.Proof lthy)
krauss@22733
   140
                      | NONE => import_last_fundef (Context.Proof lthy))
wenzelm@27187
   141
          handle Option.Option => error ("Not a function: " ^ quote (the_default "" term_opt))
krauss@20523
   142
krauss@22166
   143
        val FundefCtxData {termination, R, ...} = data
krauss@22325
   144
        val domT = domain_type (fastype_of R)
krauss@22325
   145
        val goal = HOLogic.mk_Trueprop (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
krauss@20523
   146
    in
wenzelm@20877
   147
      lthy
krauss@22325
   148
        |> ProofContext.note_thmss_i "" [(("", [ContextRules.rule_del]), [([allI], [])])] |> snd
krauss@22325
   149
        |> ProofContext.note_thmss_i "" [(("", [ContextRules.intro_bang (SOME 1)]), [([allI], [])])] |> snd
wenzelm@21602
   150
        |> ProofContext.note_thmss_i ""
krauss@22325
   151
          [(("termination", [ContextRules.intro_bang (SOME 0)]),
wenzelm@21602
   152
            [([Goal.norm_result termination], [])])] |> snd
krauss@22733
   153
        |> Proof.theorem_i NONE (total_termination_afterqed data) [[(goal, [])]]
krauss@20523
   154
    end
krauss@20523
   155
wenzelm@26171
   156
fun termination_cmd term_opt lthy =
wenzelm@26171
   157
  lthy
wenzelm@26171
   158
  |> LocalTheory.set_group (serial_string ())
wenzelm@26171
   159
  |> setup_termination_proof term_opt;
krauss@20523
   160
wenzelm@26129
   161
val add_fundef = gen_add_fundef true Specification.read_specification
wenzelm@26129
   162
val add_fundef_i = gen_add_fundef false Specification.check_specification
krauss@19611
   163
krauss@19564
   164
krauss@21235
   165
(* Datatype hook to declare datatype congs as "fundef_congs" *)
krauss@21235
   166
krauss@21235
   167
wenzelm@21602
   168
fun add_case_cong n thy =
krauss@24168
   169
    Context.theory_map (FundefCtxTree.map_fundef_congs (Thm.add_thm
krauss@21235
   170
                          (DatatypePackage.get_datatype thy n |> the
krauss@21235
   171
                           |> #case_cong
wenzelm@21602
   172
                           |> safe_mk_meta_eq)))
krauss@21235
   173
                       thy
krauss@21235
   174
haftmann@24626
   175
val case_cong = fold add_case_cong
krauss@21235
   176
wenzelm@24711
   177
val setup_case_cong = DatatypePackage.interpretation case_cong
haftmann@24626
   178
krauss@19564
   179
(* setup *)
krauss@19564
   180
wenzelm@21602
   181
val setup =
wenzelm@22846
   182
  Attrib.add_attributes
krauss@24168
   183
    [("fundef_cong", Attrib.add_del_args FundefCtxTree.cong_add FundefCtxTree.cong_del,
wenzelm@22846
   184
      "declaration of congruence rule for function definitions")]
haftmann@24626
   185
  #> setup_case_cong
wenzelm@22846
   186
  #> FundefRelation.setup
krauss@26749
   187
  #> FundefCommon.TerminationSimps.setup
krauss@19564
   188
krauss@24168
   189
val get_congs = FundefCtxTree.get_fundef_congs o Context.Theory
krauss@19770
   190
krauss@21235
   191
krauss@19564
   192
(* outer syntax *)
krauss@19564
   193
krauss@19564
   194
local structure P = OuterParse and K = OuterKeyword in
krauss@19564
   195
wenzelm@27353
   196
val _ = OuterKeyword.keyword "otherwise";
wenzelm@24867
   197
wenzelm@24867
   198
val _ =
wenzelm@26989
   199
  OuterSyntax.local_theory_to_proof "function" "define general recursive functions" K.thy_goal
krauss@22498
   200
  (fundef_parser default_config
wenzelm@26989
   201
     >> (fn ((config, fixes), (flags, statements)) => add_fundef fixes statements config flags));
krauss@20523
   202
wenzelm@24867
   203
val _ =
wenzelm@26989
   204
  OuterSyntax.local_theory_to_proof "termination" "prove termination of a recursive function" K.thy_goal
wenzelm@26989
   205
  (Scan.option P.term >> termination_cmd);
krauss@19564
   206
krauss@19564
   207
end;
krauss@19564
   208
krauss@19564
   209
wenzelm@19585
   210
end