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