src/HOL/Nonstandard_Analysis/transfer.ML
author wenzelm
Mon Feb 29 22:34:36 2016 +0100 (2016-02-29)
changeset 62479 716336f19aa9
parent 59816 src/HOL/NSA/transfer.ML@034b13f4efae
child 64270 bf474d719011
permissions -rw-r--r--
clarified session;
tuned headers;
     1 (*  Title:      HOL/Nonstandard_Analysis/transfer.ML
     2     Author:     Brian Huffman
     3 
     4 Transfer principle tactic for nonstandard analysis.
     5 *)
     6 
     7 signature TRANSFER_PRINCIPLE =
     8 sig
     9   val transfer_tac: Proof.context -> thm list -> int -> tactic
    10   val add_const: string -> theory -> theory
    11 end;
    12 
    13 structure Transfer_Principle: TRANSFER_PRINCIPLE =
    14 struct
    15 
    16 structure TransferData = Generic_Data
    17 (
    18   type T = {
    19     intros: thm list,
    20     unfolds: thm list,
    21     refolds: thm list,
    22     consts: string list
    23   };
    24   val empty = {intros = [], unfolds = [], refolds = [], consts = []};
    25   val extend = I;
    26   fun merge
    27     ({intros = intros1, unfolds = unfolds1,
    28       refolds = refolds1, consts = consts1},
    29      {intros = intros2, unfolds = unfolds2,
    30       refolds = refolds2, consts = consts2}) : T =
    31    {intros = Thm.merge_thms (intros1, intros2),
    32     unfolds = Thm.merge_thms (unfolds1, unfolds2),
    33     refolds = Thm.merge_thms (refolds1, refolds2),
    34     consts = Library.merge (op =) (consts1, consts2)};
    35 );
    36 
    37 fun unstar_typ (Type (@{type_name star}, [t])) = unstar_typ t
    38   | unstar_typ (Type (a, Ts)) = Type (a, map unstar_typ Ts)
    39   | unstar_typ T = T
    40 
    41 fun unstar_term consts term =
    42   let
    43     fun unstar (Const(a,T) $ t) = if member (op =) consts a then (unstar t)
    44           else (Const(a,unstar_typ T) $ unstar t)
    45       | unstar (f $ t) = unstar f $ unstar t
    46       | unstar (Const(a,T)) = Const(a,unstar_typ T)
    47       | unstar (Abs(a,T,t)) = Abs(a,unstar_typ T,unstar t)
    48       | unstar t = t
    49   in
    50     unstar term
    51   end
    52 
    53 fun transfer_thm_of ctxt ths t =
    54   let
    55     val {intros,unfolds,refolds,consts} = TransferData.get (Context.Proof ctxt);
    56     val meta = Local_Defs.meta_rewrite_rule ctxt;
    57     val ths' = map meta ths;
    58     val unfolds' = map meta unfolds and refolds' = map meta refolds;
    59     val (_$_$t') = Thm.concl_of (Raw_Simplifier.rewrite ctxt true unfolds' (Thm.cterm_of ctxt t))
    60     val u = unstar_term consts t'
    61     val tac =
    62       rewrite_goals_tac ctxt (ths' @ refolds' @ unfolds') THEN
    63       ALLGOALS (Object_Logic.full_atomize_tac ctxt) THEN
    64       match_tac ctxt [transitive_thm] 1 THEN
    65       resolve_tac ctxt [@{thm transfer_start}] 1 THEN
    66       REPEAT_ALL_NEW (resolve_tac ctxt intros) 1 THEN
    67       match_tac ctxt [reflexive_thm] 1
    68   in Goal.prove ctxt [] [] (Logic.mk_equals (t,u)) (K tac) end
    69 
    70 fun transfer_tac ctxt ths =
    71     SUBGOAL (fn (t, _) =>
    72         (fn th =>
    73             let
    74               val tr = transfer_thm_of ctxt ths t
    75               val (_$l$r) = Thm.concl_of tr;
    76               val trs = if l aconv r then [] else [tr];
    77             in rewrite_goals_tac ctxt trs th end))
    78 
    79 local
    80 
    81 fun map_intros f = TransferData.map
    82   (fn {intros,unfolds,refolds,consts} =>
    83     {intros=f intros, unfolds=unfolds, refolds=refolds, consts=consts})
    84 
    85 fun map_unfolds f = TransferData.map
    86   (fn {intros,unfolds,refolds,consts} =>
    87     {intros=intros, unfolds=f unfolds, refolds=refolds, consts=consts})
    88 
    89 fun map_refolds f = TransferData.map
    90   (fn {intros,unfolds,refolds,consts} =>
    91     {intros=intros, unfolds=unfolds, refolds=f refolds, consts=consts})
    92 in
    93 val intro_add = Thm.declaration_attribute (map_intros o Thm.add_thm);
    94 val intro_del = Thm.declaration_attribute (map_intros o Thm.del_thm);
    95 
    96 val unfold_add = Thm.declaration_attribute (map_unfolds o Thm.add_thm);
    97 val unfold_del = Thm.declaration_attribute (map_unfolds o Thm.del_thm);
    98 
    99 val refold_add = Thm.declaration_attribute (map_refolds o Thm.add_thm);
   100 val refold_del = Thm.declaration_attribute (map_refolds o Thm.del_thm);
   101 end
   102 
   103 fun add_const c = Context.theory_map (TransferData.map
   104   (fn {intros,unfolds,refolds,consts} =>
   105     {intros=intros, unfolds=unfolds, refolds=refolds, consts=c::consts}))
   106 
   107 val _ =
   108   Theory.setup
   109    (Attrib.setup @{binding transfer_intro} (Attrib.add_del intro_add intro_del)
   110       "declaration of transfer introduction rule" #>
   111     Attrib.setup @{binding transfer_unfold} (Attrib.add_del unfold_add unfold_del)
   112       "declaration of transfer unfolding rule" #>
   113     Attrib.setup @{binding transfer_refold} (Attrib.add_del refold_add refold_del)
   114       "declaration of transfer refolding rule")
   115 
   116 end;