src/HOL/Tools/Function/function.ML
author wenzelm
Fri, 28 Oct 2011 17:15:52 +0200
changeset 46161 f599ac41e7f5
parent 45110 47ecd30e018d
child 46162 57cd50f98fdc
permissions -rw-r--r--
tuned signature -- refined terminology;
haftmann@37743
     1
(*  Title:      HOL/Tools/Function/function.ML
krauss@33088
     2
    Author:     Alexander Krauss, TU Muenchen
krauss@33088
     3
krauss@41362
     4
Main entry points to the function package.
krauss@33088
     5
*)
krauss@33088
     6
krauss@33088
     7
signature FUNCTION =
krauss@33088
     8
sig
krauss@34232
     9
  include FUNCTION_DATA
krauss@34230
    10
krauss@36513
    11
  val add_function: (binding * typ option * mixfix) list ->
krauss@36513
    12
    (Attrib.binding * term) list -> Function_Common.function_config ->
krauss@36515
    13
    (Proof.context -> tactic) -> local_theory -> info * local_theory
krauss@36513
    14
krauss@36513
    15
  val add_function_cmd: (binding * string option * mixfix) list ->
krauss@36513
    16
    (Attrib.binding * string) list -> Function_Common.function_config ->
wenzelm@45110
    17
    (Proof.context -> tactic) -> bool -> local_theory -> info * local_theory
krauss@36513
    18
krauss@36512
    19
  val function: (binding * typ option * mixfix) list ->
krauss@34232
    20
    (Attrib.binding * term) list -> Function_Common.function_config ->
krauss@34232
    21
    local_theory -> Proof.state
krauss@33088
    22
krauss@36512
    23
  val function_cmd: (binding * string option * mixfix) list ->
krauss@34232
    24
    (Attrib.binding * string) list -> Function_Common.function_config ->
wenzelm@45110
    25
    bool -> local_theory -> Proof.state
krauss@33088
    26
krauss@36545
    27
  val prove_termination: term option -> tactic -> local_theory -> 
krauss@36545
    28
    info * local_theory
krauss@36545
    29
  val prove_termination_cmd: string option -> tactic -> local_theory ->
krauss@36545
    30
    info * local_theory
krauss@36513
    31
krauss@36512
    32
  val termination : term option -> local_theory -> Proof.state
krauss@36512
    33
  val termination_cmd : string option -> local_theory -> Proof.state
krauss@34230
    34
krauss@34232
    35
  val setup : theory -> theory
krauss@34232
    36
  val get_congs : Proof.context -> thm list
krauss@34232
    37
krauss@34232
    38
  val get_info : Proof.context -> term -> info
krauss@33088
    39
end
krauss@33088
    40
krauss@33088
    41
krauss@33088
    42
structure Function : FUNCTION =
krauss@33088
    43
struct
krauss@33088
    44
krauss@33088
    45
open Function_Lib
krauss@33088
    46
open Function_Common
krauss@33088
    47
krauss@33088
    48
val simp_attribs = map (Attrib.internal o K)
krauss@34232
    49
  [Simplifier.simp_add,
krauss@34232
    50
   Code.add_default_eqn_attribute,
krauss@34232
    51
   Nitpick_Simps.add]
krauss@33088
    52
krauss@33088
    53
val psimp_attribs = map (Attrib.internal o K)
krauss@39987
    54
  [Nitpick_Psimps.add]
krauss@33088
    55
krauss@33088
    56
fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_"
krauss@33088
    57
krauss@34232
    58
fun add_simps fnames post sort extra_qualify label mod_binding moreatts
krauss@34232
    59
  simps lthy =
krauss@34232
    60
  let
krauss@34232
    61
    val spec = post simps
krauss@34232
    62
      |> map (apfst (apsnd (fn ats => moreatts @ ats)))
krauss@34232
    63
      |> map (apfst (apfst extra_qualify))
krauss@33088
    64
krauss@34232
    65
    val (saved_spec_simps, lthy) =
krauss@34232
    66
      fold_map Local_Theory.note spec lthy
krauss@33088
    67
krauss@34232
    68
    val saved_simps = maps snd saved_spec_simps
krauss@34232
    69
    val simps_by_f = sort saved_simps
krauss@33088
    70
krauss@34232
    71
    fun add_for_f fname simps =
krauss@34232
    72
      Local_Theory.note
krauss@34232
    73
        ((mod_binding (Binding.qualify true fname (Binding.name label)), []), simps)
krauss@34232
    74
      #> snd
krauss@34232
    75
  in
krauss@34232
    76
    (saved_simps, fold2 add_for_f fnames simps_by_f lthy)
krauss@34232
    77
  end
krauss@33088
    78
wenzelm@45110
    79
fun prepare_function do_print prep default_constraint fixspec eqns config lthy =
krauss@34232
    80
  let
krauss@34232
    81
    val constrn_fxs = map (fn (b, T, mx) => (b, SOME (the_default default_constraint T), mx))
krauss@34232
    82
    val ((fixes0, spec0), ctxt') = prep (constrn_fxs fixspec) eqns lthy
krauss@34232
    83
    val fixes = map (apfst (apfst Binding.name_of)) fixes0;
krauss@34232
    84
    val spec = map (fn (bnd, prop) => (bnd, [prop])) spec0;
krauss@34232
    85
    val (eqs, post, sort_cont, cnames) = get_preproc lthy config ctxt' fixes spec
krauss@33088
    86
krauss@34232
    87
    val defname = mk_defname fixes
krauss@42717
    88
    val FunctionConfig {partials, default, ...} = config
krauss@41663
    89
    val _ =
wenzelm@44923
    90
      if is_some default
wenzelm@44923
    91
      then legacy_feature "\"function (default)\" -- use 'partial_function' instead"
krauss@41663
    92
      else ()
krauss@33088
    93
krauss@36512
    94
    val ((goal_state, cont), lthy') =
krauss@34232
    95
      Function_Mutual.prepare_function_mutual config defname fixes eqs lthy
krauss@33088
    96
krauss@34232
    97
    fun afterqed [[proof]] lthy =
krauss@34232
    98
      let
krauss@42717
    99
        val FunctionResult {fs, R, psimps, simple_pinducts,
krauss@34232
   100
          termination, domintros, cases, ...} =
krauss@33088
   101
          cont (Thm.close_derivation proof)
krauss@33088
   102
krauss@34232
   103
        val fnames = map (fst o fst) fixes
krauss@34232
   104
        fun qualify n = Binding.name n
krauss@34232
   105
          |> Binding.qualify true defname
krauss@34232
   106
        val conceal_partial = if partials then I else Binding.conceal
krauss@33394
   107
krauss@34232
   108
        val addsmps = add_simps fnames post sort_cont
krauss@33088
   109
krauss@34232
   110
        val (((psimps', pinducts'), (_, [termination'])), lthy) =
krauss@34232
   111
          lthy
krauss@34232
   112
          |> addsmps (conceal_partial o Binding.qualify false "partial")
krauss@34232
   113
               "psimps" conceal_partial psimp_attribs psimps
krauss@34232
   114
          ||>> Local_Theory.note ((conceal_partial (qualify "pinduct"),
krauss@34232
   115
                 [Attrib.internal (K (Rule_Cases.case_names cnames)),
krauss@34232
   116
                  Attrib.internal (K (Rule_Cases.consumes 1)),
krauss@34232
   117
                  Attrib.internal (K (Induct.induct_pred ""))]), simple_pinducts)
krauss@34232
   118
          ||>> Local_Theory.note ((Binding.conceal (qualify "termination"), []), [termination])
krauss@34232
   119
          ||> (snd o Local_Theory.note ((qualify "cases",
krauss@34232
   120
                 [Attrib.internal (K (Rule_Cases.case_names cnames))]), [cases]))
krauss@40257
   121
          ||> (case domintros of NONE => I | SOME thms => 
krauss@40257
   122
                   Local_Theory.note ((qualify "domintros", []), thms) #> snd)
krauss@33088
   123
krauss@34232
   124
        val info = { add_simps=addsmps, case_names=cnames, psimps=psimps',
krauss@34232
   125
          pinducts=snd pinducts', simps=NONE, inducts=NONE, termination=termination',
krauss@34232
   126
          fs=fs, R=R, defname=defname, is_partial=true }
krauss@34230
   127
wenzelm@45110
   128
        val _ = Proof_Display.print_consts do_print lthy (K false) (map fst fixes)
krauss@34232
   129
      in
wenzelm@45110
   130
        (info,
wenzelm@46161
   131
         lthy |> Local_Theory.declaration false (add_function_data o transform_function_data info))
krauss@34232
   132
      end
krauss@36513
   133
  in
krauss@36512
   134
    ((goal_state, afterqed), lthy')
krauss@36511
   135
  end
krauss@36511
   136
wenzelm@45110
   137
fun gen_add_function do_print prep default_constraint fixspec eqns config tac lthy =
krauss@36513
   138
  let
krauss@36513
   139
    val ((goal_state, afterqed), lthy') =
wenzelm@45110
   140
      prepare_function do_print prep default_constraint fixspec eqns config lthy
krauss@36513
   141
    val pattern_thm =
krauss@36513
   142
      case SINGLE (tac lthy') goal_state of
krauss@36513
   143
        NONE => error "pattern completeness and compatibility proof failed"
krauss@36513
   144
      | SOME st => Goal.finish lthy' st
krauss@36513
   145
  in
krauss@36513
   146
    lthy'
krauss@36513
   147
    |> afterqed [[pattern_thm]]
krauss@36513
   148
  end
krauss@36513
   149
krauss@36513
   150
val add_function =
wenzelm@37153
   151
  gen_add_function false Specification.check_spec (Type_Infer.anyT HOLogic.typeS)
wenzelm@45110
   152
fun add_function_cmd a b c d int = gen_add_function int Specification.read_spec "_::type" a b c d
krauss@36513
   153
wenzelm@45110
   154
fun gen_function do_print prep default_constraint fixspec eqns config lthy =
krauss@36511
   155
  let
krauss@36513
   156
    val ((goal_state, afterqed), lthy') =
wenzelm@45110
   157
      prepare_function do_print prep default_constraint fixspec eqns config lthy
krauss@34232
   158
  in
krauss@36511
   159
    lthy'
krauss@36515
   160
    |> Proof.theorem NONE (snd oo afterqed) [[(Logic.unprotect (concl_of goal_state), [])]]
krauss@36512
   161
    |> Proof.refine (Method.primitive_text (K goal_state)) |> Seq.hd
krauss@34232
   162
  end
krauss@33088
   163
krauss@36512
   164
val function =
wenzelm@37153
   165
  gen_function false Specification.check_spec (Type_Infer.anyT HOLogic.typeS)
wenzelm@45110
   166
fun function_cmd a b c int = gen_function int Specification.read_spec "_::type" a b c
krauss@36513
   167
krauss@36511
   168
fun prepare_termination_proof prep_term raw_term_opt lthy =
krauss@34232
   169
  let
krauss@34232
   170
    val term_opt = Option.map (prep_term lthy) raw_term_opt
krauss@34232
   171
    val info = the (case term_opt of
krauss@34232
   172
                      SOME t => (import_function_data t lthy
krauss@34232
   173
                        handle Option.Option =>
krauss@34232
   174
                          error ("Not a function: " ^ quote (Syntax.string_of_term lthy t)))
krauss@34232
   175
                    | NONE => (import_last_function lthy handle Option.Option => error "Not a function"))
krauss@33088
   176
krauss@34232
   177
      val { termination, fs, R, add_simps, case_names, psimps,
krauss@34232
   178
        pinducts, defname, ...} = info
krauss@34232
   179
      val domT = domain_type (fastype_of R)
krauss@34232
   180
      val goal = HOLogic.mk_Trueprop
krauss@34232
   181
                   (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
krauss@34232
   182
      fun afterqed [[totality]] lthy =
krauss@34232
   183
        let
krauss@34232
   184
          val totality = Thm.close_derivation totality
krauss@34232
   185
          val remove_domain_condition =
wenzelm@35410
   186
            full_simplify (HOL_basic_ss addsimps [totality, @{thm True_implies_equals}])
krauss@34232
   187
          val tsimps = map remove_domain_condition psimps
krauss@34232
   188
          val tinduct = map remove_domain_condition pinducts
krauss@34230
   189
krauss@34232
   190
          fun qualify n = Binding.name n
krauss@34232
   191
            |> Binding.qualify true defname
krauss@34232
   192
        in
krauss@34232
   193
          lthy
krauss@34232
   194
          |> add_simps I "simps" I simp_attribs tsimps
krauss@34232
   195
          ||>> Local_Theory.note
krauss@34232
   196
             ((qualify "induct",
krauss@34232
   197
               [Attrib.internal (K (Rule_Cases.case_names case_names))]),
krauss@34232
   198
              tinduct)
krauss@36545
   199
          |-> (fn (simps, (_, inducts)) => fn lthy =>
krauss@34232
   200
            let val info' = { is_partial=false, defname=defname, add_simps=add_simps,
krauss@34232
   201
              case_names=case_names, fs=fs, R=R, psimps=psimps, pinducts=pinducts,
krauss@34232
   202
              simps=SOME simps, inducts=SOME inducts, termination=termination }
krauss@34232
   203
            in
krauss@36545
   204
              (info',
krauss@36545
   205
               lthy 
wenzelm@46161
   206
               |> Local_Theory.declaration false (add_function_data o transform_function_data info')
krauss@36545
   207
               |> Spec_Rules.add Spec_Rules.Equational (fs, tsimps))
krauss@34232
   208
            end)
krauss@34232
   209
        end
krauss@34232
   210
  in
krauss@36511
   211
    (goal, afterqed, termination)
krauss@36511
   212
  end
krauss@36511
   213
krauss@36513
   214
fun gen_prove_termination prep_term raw_term_opt tac lthy =
krauss@36513
   215
  let
krauss@36513
   216
    val (goal, afterqed, termination) =
krauss@36513
   217
      prepare_termination_proof prep_term raw_term_opt lthy
krauss@36513
   218
krauss@36513
   219
    val totality = Goal.prove lthy [] [] goal (K tac)
krauss@36513
   220
  in
krauss@36513
   221
    afterqed [[totality]] lthy
krauss@36513
   222
end
krauss@36513
   223
krauss@36513
   224
val prove_termination = gen_prove_termination Syntax.check_term
krauss@36513
   225
val prove_termination_cmd = gen_prove_termination Syntax.read_term
krauss@36513
   226
krauss@36512
   227
fun gen_termination prep_term raw_term_opt lthy =
krauss@36511
   228
  let
krauss@36511
   229
    val (goal, afterqed, termination) = prepare_termination_proof prep_term raw_term_opt lthy
krauss@36511
   230
  in
krauss@34232
   231
    lthy
wenzelm@43232
   232
    |> Proof_Context.note_thmss ""
krauss@34232
   233
       [((Binding.empty, [Context_Rules.rule_del]), [([allI], [])])] |> snd
wenzelm@43232
   234
    |> Proof_Context.note_thmss ""
krauss@34232
   235
       [((Binding.empty, [Context_Rules.intro_bang (SOME 1)]), [([allI], [])])] |> snd
wenzelm@43232
   236
    |> Proof_Context.note_thmss ""
krauss@34232
   237
       [((Binding.name "termination", [Context_Rules.intro_bang (SOME 0)]),
krauss@34232
   238
         [([Goal.norm_result termination], [])])] |> snd
krauss@36545
   239
    |> Proof.theorem NONE (snd oo afterqed) [[(goal, [])]]
krauss@34232
   240
  end
krauss@33088
   241
krauss@36512
   242
val termination = gen_termination Syntax.check_term
krauss@36512
   243
val termination_cmd = gen_termination Syntax.read_term
krauss@33088
   244
krauss@33088
   245
krauss@33088
   246
(* Datatype hook to declare datatype congs as "function_congs" *)
krauss@33088
   247
krauss@33088
   248
krauss@33088
   249
fun add_case_cong n thy =
krauss@34232
   250
  let
krauss@34232
   251
    val cong = #case_cong (Datatype.the_info thy n)
krauss@34232
   252
      |> safe_mk_meta_eq
krauss@34232
   253
  in
krauss@34232
   254
    Context.theory_map
krauss@34232
   255
      (Function_Ctx_Tree.map_function_congs (Thm.add_thm cong)) thy
krauss@34232
   256
  end
krauss@33088
   257
krauss@33088
   258
val setup_case_cong = Datatype.interpretation (K (fold add_case_cong))
krauss@33088
   259
krauss@33088
   260
krauss@33088
   261
(* setup *)
krauss@33088
   262
krauss@33088
   263
val setup =
krauss@33088
   264
  Attrib.setup @{binding fundef_cong}
krauss@33088
   265
    (Attrib.add_del Function_Ctx_Tree.cong_add Function_Ctx_Tree.cong_del)
krauss@33088
   266
    "declaration of congruence rule for function definitions"
krauss@33088
   267
  #> setup_case_cong
krauss@33088
   268
  #> Function_Relation.setup
krauss@33088
   269
  #> Function_Common.Termination_Simps.setup
krauss@33088
   270
krauss@33088
   271
val get_congs = Function_Ctx_Tree.get_function_congs
krauss@33088
   272
krauss@34230
   273
fun get_info ctxt t = Item_Net.retrieve (get_function ctxt) t
krauss@34230
   274
  |> the_single |> snd
krauss@33088
   275
wenzelm@36970
   276
krauss@33088
   277
(* outer syntax *)
krauss@33088
   278
krauss@33088
   279
val _ =
wenzelm@45110
   280
  Outer_Syntax.local_theory_to_proof' "function" "define general recursive functions"
wenzelm@36970
   281
  Keyword.thy_goal
krauss@33088
   282
  (function_parser default_config
krauss@36512
   283
     >> (fn ((config, fixes), statements) => function_cmd fixes statements config))
krauss@33088
   284
krauss@33088
   285
val _ =
wenzelm@36970
   286
  Outer_Syntax.local_theory_to_proof "termination" "prove termination of a recursive function"
wenzelm@36970
   287
  Keyword.thy_goal
wenzelm@36970
   288
  (Scan.option Parse.term >> termination_cmd)
wenzelm@36970
   289
krauss@33088
   290
krauss@33088
   291
end