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