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