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