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