src/HOL/Tools/Function/function_common.ML
changeset 67634 9225bb0d1327
parent 67149 e61557884799
child 67651 6dd41193a72a
equal deleted inserted replaced
67633:9a1212f4393e 67634:9225bb0d1327
     2     Author:     Alexander Krauss, TU Muenchen
     2     Author:     Alexander Krauss, TU Muenchen
     3 
     3 
     4 Common definitions and other infrastructure for the function package.
     4 Common definitions and other infrastructure for the function package.
     5 *)
     5 *)
     6 
     6 
     7 signature FUNCTION_DATA =
     7 signature FUNCTION_COMMON =
     8 sig
     8 sig
     9   type info =
     9   type info =
    10    {is_partial : bool,
    10    {is_partial : bool,
    11     defname : binding,
    11     defname : binding,
    12       (*contains no logical entities: invariant under morphisms:*)
    12       (*contains no logical entities: invariant under morphisms:*)
    24     termination : thm,
    24     termination : thm,
    25     totality : thm option,
    25     totality : thm option,
    26     cases : thm list,
    26     cases : thm list,
    27     pelims: thm list list,
    27     pelims: thm list list,
    28     elims: thm list list option}
    28     elims: thm list list option}
    29 end
       
    30 
       
    31 structure Function_Data : FUNCTION_DATA =
       
    32 struct
       
    33 
       
    34 type info =
       
    35  {is_partial : bool,
       
    36   defname : binding,
       
    37     (*contains no logical entities: invariant under morphisms:*)
       
    38   add_simps : (binding -> binding) -> string -> (binding -> binding) ->
       
    39     Token.src list -> thm list -> local_theory -> thm list * local_theory,
       
    40   fnames : binding list,
       
    41   case_names : string list,
       
    42   fs : term list,
       
    43   R : term,
       
    44   dom: term,
       
    45   psimps: thm list,
       
    46   pinducts: thm list,
       
    47   simps : thm list option,
       
    48   inducts : thm list option,
       
    49   termination : thm,
       
    50   totality : thm option,
       
    51   cases : thm list,
       
    52   pelims : thm list list,
       
    53   elims : thm list list option}
       
    54 
       
    55 end
       
    56 
       
    57 signature FUNCTION_COMMON =
       
    58 sig
       
    59   include FUNCTION_DATA
       
    60   val profile : bool Unsynchronized.ref
    29   val profile : bool Unsynchronized.ref
    61   val PROFILE : string -> ('a -> 'b) -> 'a -> 'b
    30   val PROFILE : string -> ('a -> 'b) -> 'a -> 'b
    62   val mk_acc : typ -> term -> term
    31   val mk_acc : typ -> term -> term
    63   val split_def : Proof.context -> (string -> 'a) -> term ->
    32   val split_def : Proof.context -> (string -> 'a) -> term ->
    64     string * (string * typ) list * term list * term list * term
    33     string * (string * typ) list * term list * term list * term
    76   val mk_case_names : int -> string -> int -> string list
    45   val mk_case_names : int -> string -> int -> string list
    77   val empty_preproc : (Proof.context -> ((string * typ) * mixfix) list -> term list -> 'c) ->
    46   val empty_preproc : (Proof.context -> ((string * typ) * mixfix) list -> term list -> 'c) ->
    78     preproc
    47     preproc
    79   val termination_rule_tac : Proof.context -> int -> tactic
    48   val termination_rule_tac : Proof.context -> int -> tactic
    80   val store_termination_rule : thm -> Context.generic -> Context.generic
    49   val store_termination_rule : thm -> Context.generic -> Context.generic
    81   val get_functions : Proof.context -> (term * info) Item_Net.T
    50   val retrieve_function_data : Proof.context -> term -> (term * info) list
    82   val add_function_data : info -> Context.generic -> Context.generic
    51   val add_function_data : info -> Context.generic -> Context.generic
    83   val termination_prover_tac : bool -> Proof.context -> tactic
    52   val termination_prover_tac : bool -> Proof.context -> tactic
    84   val set_termination_prover : (bool -> Proof.context -> tactic) -> Context.generic ->
    53   val set_termination_prover : (bool -> Proof.context -> tactic) -> Context.generic ->
    85     Context.generic
    54     Context.generic
    86   val get_preproc: Proof.context -> preproc
    55   val get_preproc: Proof.context -> preproc
   105 end
    74 end
   106 
    75 
   107 structure Function_Common : FUNCTION_COMMON =
    76 structure Function_Common : FUNCTION_COMMON =
   108 struct
    77 struct
   109 
    78 
   110 open Function_Data
       
   111 
       
   112 local open Function_Lib in
    79 local open Function_Lib in
       
    80 
       
    81 (* info *)
       
    82 
       
    83 type info =
       
    84  {is_partial : bool,
       
    85   defname : binding,
       
    86     (*contains no logical entities: invariant under morphisms:*)
       
    87   add_simps : (binding -> binding) -> string -> (binding -> binding) ->
       
    88     Token.src list -> thm list -> local_theory -> thm list * local_theory,
       
    89   fnames : binding list,
       
    90   case_names : string list,
       
    91   fs : term list,
       
    92   R : term,
       
    93   dom: term,
       
    94   psimps: thm list,
       
    95   pinducts: thm list,
       
    96   simps : thm list option,
       
    97   inducts : thm list option,
       
    98   termination : thm,
       
    99   totality : thm option,
       
   100   cases : thm list,
       
   101   pelims : thm list list,
       
   102   elims : thm list list option}
       
   103 
       
   104 fun transform_function_data phi ({add_simps, case_names, fnames, fs, R, dom, psimps, pinducts,
       
   105   simps, inducts, termination, totality, defname, is_partial, cases, pelims, elims} : info) =
       
   106     let
       
   107       val term = Morphism.term phi
       
   108       val thm = Morphism.thm phi
       
   109       val fact = Morphism.fact phi
       
   110     in
       
   111       { add_simps = add_simps, case_names = case_names, fnames = fnames,
       
   112         fs = map term fs, R = term R, dom = term dom, psimps = fact psimps,
       
   113         pinducts = fact pinducts, simps = Option.map fact simps,
       
   114         inducts = Option.map fact inducts, termination = thm termination,
       
   115         totality = Option.map thm totality, defname = Morphism.binding phi defname,
       
   116         is_partial = is_partial, cases = fact cases,
       
   117         elims = Option.map (map fact) elims, pelims = map fact pelims }
       
   118     end
   113 
   119 
   114 
   120 
   115 (* profiling *)
   121 (* profiling *)
   116 
   122 
   117 val profile = Unsynchronized.ref false
   123 val profile = Unsynchronized.ref false
   265 fun termination_rule_tac ctxt = resolve_tac ctxt (#1 (Data.get (Context.Proof ctxt)))
   271 fun termination_rule_tac ctxt = resolve_tac ctxt (#1 (Data.get (Context.Proof ctxt)))
   266 
   272 
   267 val store_termination_rule = Data.map o @{apply 4(1)} o cons o Thm.trim_context
   273 val store_termination_rule = Data.map o @{apply 4(1)} o cons o Thm.trim_context
   268 
   274 
   269 val get_functions = #2 o Data.get o Context.Proof
   275 val get_functions = #2 o Data.get o Context.Proof
   270 fun add_function_data (info : info as {fs, termination, ...}) =
   276 
   271   (Data.map o @{apply 4(2)}) (fold (fn f => Item_Net.update (f, info)) fs)
   277 fun retrieve_function_data ctxt t =
   272   #> store_termination_rule termination
   278   Item_Net.retrieve (get_functions ctxt) t
       
   279   |> (map o apsnd)
       
   280       (transform_function_data (Morphism.transfer_morphism (Proof_Context.theory_of ctxt)));
       
   281 
       
   282 val add_function_data =
       
   283   transform_function_data Morphism.trim_context_morphism #>
       
   284   (fn info as {fs, termination, ...} =>
       
   285     (Data.map o @{apply 4(2)}) (fold (fn f => Item_Net.update (f, info)) fs)
       
   286     #> store_termination_rule termination)
   273 
   287 
   274 fun termination_prover_tac quiet ctxt = #3 (Data.get (Context.Proof ctxt)) quiet ctxt
   288 fun termination_prover_tac quiet ctxt = #3 (Data.get (Context.Proof ctxt)) quiet ctxt
   275 val set_termination_prover = Data.map o @{apply 4(3)} o K
   289 val set_termination_prover = Data.map o @{apply 4(3)} o K
   276 
   290 
   277 val get_preproc = #4 o Data.get o Context.Proof
   291 val get_preproc = #4 o Data.get o Context.Proof
   290   cases : thm list,
   304   cases : thm list,
   291   pelims : thm list list,
   305   pelims : thm list list,
   292   termination : thm,
   306   termination : thm,
   293   domintros : thm list option}
   307   domintros : thm list option}
   294 
   308 
   295 fun transform_function_data phi ({add_simps, case_names, fnames, fs, R, dom, psimps, pinducts,
       
   296   simps, inducts, termination, totality, defname, is_partial, cases, pelims, elims} : info) =
       
   297     let
       
   298       val term = Morphism.term phi
       
   299       val thm = Morphism.thm phi
       
   300       val fact = Morphism.fact phi
       
   301     in
       
   302       { add_simps = add_simps, case_names = case_names, fnames = fnames,
       
   303         fs = map term fs, R = term R, dom = term dom, psimps = fact psimps,
       
   304         pinducts = fact pinducts, simps = Option.map fact simps,
       
   305         inducts = Option.map fact inducts, termination = thm termination,
       
   306         totality = Option.map thm totality, defname = Morphism.binding phi defname,
       
   307         is_partial = is_partial, cases = fact cases,
       
   308         elims = Option.map (map fact) elims, pelims = map fact pelims }
       
   309     end
       
   310 
       
   311 fun lift_morphism ctxt f =
   309 fun lift_morphism ctxt f =
   312   let
   310   let
   313     fun term t = Thm.term_of (Drule.cterm_rule f (Thm.cterm_of ctxt t))
   311     fun term t = Thm.term_of (Drule.cterm_rule f (Thm.cterm_of ctxt t))
   314   in
   312   in
   315     Morphism.morphism "lift_morphism"
   313     Morphism.morphism "lift_morphism"
   326 
   324 
   327     fun match (trm, data) =
   325     fun match (trm, data) =
   328       SOME (transform_function_data (inst_morph (Thm.match (Thm.cterm_of ctxt trm, ct))) data)
   326       SOME (transform_function_data (inst_morph (Thm.match (Thm.cterm_of ctxt trm, ct))) data)
   329         handle Pattern.MATCH => NONE
   327         handle Pattern.MATCH => NONE
   330   in
   328   in
   331     get_first match (Item_Net.retrieve (get_functions ctxt) t)
   329     get_first match (retrieve_function_data ctxt t)
   332   end
   330   end
   333 
   331 
   334 fun import_last_function ctxt =
   332 fun import_last_function ctxt =
   335   (case Item_Net.content (get_functions ctxt) of
   333   (case Item_Net.content (get_functions ctxt) of
   336     [] => NONE
   334     [] => NONE