src/HOL/Tools/function_package/fundef_package.ML
author krauss
Wed Sep 20 12:05:31 2006 +0200 (2006-09-20)
changeset 20638 241792a4634e
parent 20523 36a59e5d0039
child 20654 d80502f0d701
permissions -rw-r--r--
Removed "induct set" attribute from total induction rules
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
krauss@20523
    12
    val add_fundef :  (string * string option * mixfix) list 
krauss@20523
    13
                      -> ((bstring * Attrib.src list) * string list) list list
krauss@20523
    14
                      -> bool 
krauss@20523
    15
                      -> local_theory 
krauss@20523
    16
                      -> Proof.state
krauss@20523
    17
krauss@20523
    18
    val add_fundef_i:  (string * typ option * mixfix) list 
krauss@20523
    19
                       -> ((bstring * Attrib.src list) * term list) list list
krauss@20523
    20
                       -> bool 
krauss@20523
    21
                       -> local_theory 
krauss@20523
    22
                       -> Proof.state
krauss@19564
    23
krauss@19564
    24
    val cong_add: attribute
krauss@19564
    25
    val cong_del: attribute
wenzelm@20363
    26
krauss@19564
    27
    val setup : theory -> theory
krauss@19770
    28
    val get_congs : theory -> thm list
krauss@19564
    29
end
krauss@19564
    30
krauss@19564
    31
krauss@20523
    32
structure FundefPackage  =
krauss@19564
    33
struct
krauss@19564
    34
krauss@19564
    35
open FundefCommon
krauss@19564
    36
krauss@19564
    37
krauss@20523
    38
fun burrow_snd f ps = (* ('a list -> 'b list) -> ('c * 'a) list -> ('c * 'b) list *)
krauss@20523
    39
    let val (xs, ys) = split_list ps
krauss@20523
    40
    in xs ~~ f ys end
krauss@20270
    41
krauss@20523
    42
fun restore_spec_structure reps spec =
krauss@20523
    43
    (burrow o burrow_snd o burrow o K) reps spec
wenzelm@20363
    44
krauss@20523
    45
fun with_local_path path f lthy =
krauss@20523
    46
    let 
krauss@20523
    47
      val restore = Theory.restore_naming (ProofContext.theory_of lthy)
krauss@19770
    48
    in
krauss@20523
    49
      lthy
krauss@20523
    50
        |> LocalTheory.theory (Theory.add_path path)
krauss@20523
    51
        |> f
krauss@20523
    52
        |> LocalTheory.theory restore
krauss@19770
    53
    end
wenzelm@20363
    54
krauss@20523
    55
fun add_simps label moreatts mutual_info fixes psimps spec lthy =
krauss@19564
    56
    let
krauss@20523
    57
      val fnames = map (fst o fst) fixes
krauss@20523
    58
      val psimps_by_f = FundefMutual.sort_by_function mutual_info fnames psimps
krauss@19564
    59
krauss@20523
    60
      fun add_for_f fname psimps =
krauss@20523
    61
          with_local_path fname
krauss@20523
    62
                          (LocalTheory.note ((label, Attrib.internal Simplifier.simp_add :: moreatts), psimps) #> snd)
krauss@19564
    63
    in
krauss@20523
    64
      lthy
krauss@20523
    65
        |> fold_map (fold_map LocalTheory.note) (restore_spec_structure psimps spec) |> snd
krauss@20523
    66
        |> fold2 add_for_f fnames psimps_by_f
krauss@19564
    67
    end
krauss@19564
    68
krauss@19564
    69
krauss@20523
    70
fun fundef_afterqed fixes spec mutual_info defname data [[result]] lthy =
krauss@20523
    71
    let
krauss@20523
    72
        val fundef_data = FundefMutual.mk_partial_rules_mutual lthy mutual_info data result
krauss@20523
    73
        val FundefMResult {psimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = fundef_data
krauss@20523
    74
    in
krauss@20523
    75
      lthy
krauss@20523
    76
        |> add_simps "psimps" [] mutual_info fixes psimps spec
krauss@20523
    77
        |> with_local_path defname
krauss@20523
    78
                  (LocalTheory.note (("domintros", []), domintros) #> snd
krauss@20523
    79
                   #> LocalTheory.note (("termination", []), [termination]) #> snd
krauss@20523
    80
                   #> LocalTheory.note (("cases", []), [cases]) #> snd
krauss@20523
    81
                   #> LocalTheory.note (("pinduct", [Attrib.internal (InductAttrib.induct_set "")]), simple_pinducts) #> snd)
krauss@20523
    82
        |> LocalTheory.theory (Context.theory_map (add_fundef_data defname (fundef_data, mutual_info, (fixes,spec))))
krauss@20523
    83
    end (* FIXME: Add cases for induct and cases thm *)
krauss@20523
    84
krauss@20523
    85
krauss@20523
    86
krauss@20523
    87
fun prep_with_flags prep fixspec eqnss_flags global_flag lthy =
krauss@19564
    88
    let
krauss@20523
    89
      val eqnss = map (map (apsnd (map fst))) eqnss_flags
krauss@20523
    90
      val flags = map (map (map (fn (_, f) => global_flag orelse f) o snd)) eqnss_flags
krauss@20523
    91
krauss@20523
    92
      val ((fixes, _), ctxt') = prep fixspec [] lthy
krauss@20523
    93
      val spec = map (fn eqns => snd (fst (prep [] eqns ctxt'))) eqnss
krauss@20523
    94
                     |> map (map (apsnd (map (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t)))) (* Add quantifiers *)
krauss@20523
    95
                     |> map2 (map2 (fn fs => fn (r, thms) => (r, fs ~~ thms))) flags
krauss@20523
    96
                     |> (burrow o burrow_snd o burrow) 
krauss@20523
    97
                          (FundefSplit.split_some_equations lthy)
krauss@20523
    98
                     |> map (map (apsnd flat))
krauss@20523
    99
    in
krauss@20523
   100
      ((fixes, spec), ctxt')
krauss@20523
   101
    end
krauss@20523
   102
krauss@19564
   103
krauss@20523
   104
fun gen_add_fundef prep_spec fixspec eqnss_flags preprocess lthy =
krauss@20523
   105
    let
krauss@20523
   106
      val ((fixes, spec), ctxt') = prep_with_flags prep_spec fixspec eqnss_flags preprocess lthy
krauss@20523
   107
      val t_eqns = spec
krauss@20523
   108
                     |> flat |> map snd |> flat (* flatten external structure *)
krauss@20523
   109
krauss@20523
   110
      val ((mutual_info, name, prep_result as Prep {goal, goalI, ...}), lthy) = 
krauss@20523
   111
          FundefMutual.prepare_fundef_mutual fixes t_eqns lthy
krauss@20523
   112
krauss@20523
   113
      val afterqed = fundef_afterqed fixes spec mutual_info name prep_result
krauss@20523
   114
    in
krauss@20523
   115
        lthy
krauss@20523
   116
          |> Proof.theorem_i PureThy.internalK NONE afterqed NONE ("", []) [(("", []), [(goal, [])])]
krauss@20523
   117
          |> Proof.refine (Method.primitive_text (fn _ => goalI)) |> Seq.hd
krauss@20523
   118
    end
krauss@20523
   119
krauss@20523
   120
krauss@20523
   121
fun total_termination_afterqed defname data [[totality]] lthy =
krauss@20523
   122
    let
krauss@20523
   123
        val (FundefMResult {psimps, simple_pinducts, ... }, mutual_info, (fixes, stmts)) = data
krauss@19564
   124
wenzelm@20363
   125
        val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
krauss@19564
   126
krauss@20523
   127
        val tsimps = map remove_domain_condition psimps
wenzelm@20363
   128
        val tinduct = map remove_domain_condition simple_pinducts
krauss@19770
   129
krauss@20523
   130
        (* FIXME: How to generate code from (possibly) local contexts 
krauss@20523
   131
        val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps
krauss@20523
   132
        val allatts = if has_guards then [] else [Attrib.internal (RecfunCodegen.add NONE)]
krauss@20523
   133
        *)
krauss@19564
   134
    in
krauss@20523
   135
        lthy
krauss@20523
   136
          |> add_simps "simps" [] mutual_info fixes tsimps stmts
krauss@20523
   137
          |> with_local_path defname
krauss@20638
   138
                (LocalTheory.note (("induct", []), tinduct) #> snd)
wenzelm@20363
   139
    end
krauss@19564
   140
krauss@19564
   141
krauss@20523
   142
fun fundef_setup_termination_proof name_opt lthy =
krauss@20523
   143
    let
krauss@20523
   144
        val name = the_default (get_last_fundef (Context.Proof lthy)) name_opt
krauss@20523
   145
        val data = the (get_fundef_data name (Context.Proof lthy))
krauss@20523
   146
                   handle Option.Option => raise ERROR ("No such function definition: " ^ name)
krauss@20523
   147
krauss@20523
   148
        val (res as FundefMResult {termination, ...}, _, _) = data
krauss@20523
   149
        val goal = FundefTermination.mk_total_termination_goal data
krauss@20523
   150
    in
krauss@20523
   151
      lthy 
krauss@20523
   152
        |> ProofContext.note_thmss_i [(("termination",
krauss@20523
   153
                                 [ContextRules.intro_query NONE]), [(ProofContext.export_standard lthy lthy [termination], [])])] |> snd
krauss@20523
   154
        |> Proof.theorem_i PureThy.internalK NONE
krauss@20523
   155
                           (total_termination_afterqed name data) NONE ("", [])
krauss@20523
   156
                           [(("", []), [(goal, [])])]
krauss@20523
   157
    end
krauss@20523
   158
krauss@20523
   159
krauss@20523
   160
val add_fundef = gen_add_fundef Specification.read_specification
krauss@20523
   161
val add_fundef_i = gen_add_fundef Specification.cert_specification
krauss@19611
   162
krauss@19564
   163
krauss@19564
   164
krauss@19564
   165
(* congruence rules *)
krauss@19564
   166
wenzelm@19617
   167
val cong_add = Thm.declaration_attribute (map_fundef_congs o Drule.add_rule o safe_mk_meta_eq);
wenzelm@19617
   168
val cong_del = Thm.declaration_attribute (map_fundef_congs o Drule.del_rule o safe_mk_meta_eq);
krauss@19564
   169
krauss@19564
   170
krauss@19564
   171
(* setup *)
krauss@19564
   172
wenzelm@20363
   173
val setup = FundefData.init #> FundefCongs.init
wenzelm@20363
   174
        #>  Attrib.add_attributes
wenzelm@20363
   175
                [("fundef_cong", Attrib.add_del_args cong_add cong_del, "declaration of congruence rule for function definitions")]
krauss@19564
   176
krauss@19564
   177
krauss@19770
   178
val get_congs = FundefCommon.get_fundef_congs o Context.Theory
krauss@19770
   179
krauss@19770
   180
krauss@19564
   181
(* outer syntax *)
krauss@19564
   182
krauss@19564
   183
local structure P = OuterParse and K = OuterKeyword in
krauss@19564
   184
krauss@20270
   185
krauss@20270
   186
krauss@20523
   187
fun or_list1 s = P.enum1 "|" s
krauss@20523
   188
krauss@20523
   189
val opt_sequential = Scan.optional ((P.$$$ "(" |-- P.$$$ "sequential" --| P.$$$ ")") >> K true) false
krauss@20523
   190
krauss@20523
   191
val otherwise = P.$$$ "(" |-- P.$$$ "otherwise" --| P.$$$ ")"
krauss@20523
   192
val statement_ow = P.and_list1 (P.opt_thm_name ":" -- Scan.repeat1 (P.prop -- Scan.optional (otherwise >> K true) false))
krauss@20523
   193
val statements_ow = or_list1 statement_ow
krauss@20270
   194
krauss@20270
   195
krauss@20523
   196
fun local_theory_to_proof f = 
krauss@20523
   197
    Toplevel.theory_to_proof (f o LocalTheory.init NONE)
krauss@19564
   198
krauss@19564
   199
val functionP =
krauss@19564
   200
  OuterSyntax.command "function" "define general recursive functions" K.thy_goal
krauss@20523
   201
  ((opt_sequential -- P.opt_locale_target -- P.fixes --| P.$$$ "where" -- statements_ow)
krauss@20523
   202
     >> (fn (((sequential, target), fixes), statements) =>
krauss@20523
   203
            Toplevel.print o local_theory_to_proof (add_fundef fixes statements sequential)));
krauss@20523
   204
krauss@19564
   205
krauss@19564
   206
val terminationP =
krauss@19564
   207
  OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal
krauss@20523
   208
  (Scan.option P.name 
krauss@20523
   209
    >> (fn name => Toplevel.print o local_theory_to_proof (fundef_setup_termination_proof name)));
krauss@19564
   210
krauss@20338
   211
krauss@19564
   212
val _ = OuterSyntax.add_parsers [functionP];
krauss@19564
   213
val _ = OuterSyntax.add_parsers [terminationP];
krauss@20523
   214
val _ = OuterSyntax.add_keywords ["sequential", "otherwise"]; (* currently unused *)
krauss@19564
   215
krauss@19564
   216
end;
krauss@19564
   217
krauss@19564
   218
wenzelm@19585
   219
end