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