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