src/HOL/NSA/transfer.ML
author wenzelm
Sun Nov 09 17:04:14 2014 +0100 (2014-11-09)
changeset 58957 c9e744ea8a38
parent 58825 2065f49da190
child 59498 50b60f501b05
permissions -rw-r--r--
proper context for match_tac etc.;
     1 (*  Title:      HOL/NSA/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 thy = Proof_Context.theory_of ctxt;
    56     val {intros,unfolds,refolds,consts} = TransferData.get (Context.Proof ctxt);
    57     val meta = Local_Defs.meta_rewrite_rule ctxt;
    58     val ths' = map meta ths;
    59     val unfolds' = map meta unfolds and refolds' = map meta refolds;
    60     val (_$_$t') = concl_of (Raw_Simplifier.rewrite ctxt true unfolds' (cterm_of thy t))
    61     val u = unstar_term consts t'
    62     val tac =
    63       rewrite_goals_tac ctxt (ths' @ refolds' @ unfolds') THEN
    64       ALLGOALS (Object_Logic.full_atomize_tac ctxt) THEN
    65       match_tac ctxt [transitive_thm] 1 THEN
    66       resolve_tac [@{thm transfer_start}] 1 THEN
    67       REPEAT_ALL_NEW (resolve_tac intros) 1 THEN
    68       match_tac ctxt [reflexive_thm] 1
    69   in Goal.prove ctxt [] [] (Logic.mk_equals (t,u)) (K tac) end
    70 
    71 fun transfer_tac ctxt ths =
    72     SUBGOAL (fn (t,i) =>
    73         (fn th =>
    74             let
    75               val tr = transfer_thm_of ctxt ths t
    76               val (_$l$r) = concl_of tr;
    77               val trs = if l aconv r then [] else [tr];
    78             in rewrite_goals_tac ctxt trs th end))
    79 
    80 local
    81 
    82 fun map_intros f = TransferData.map
    83   (fn {intros,unfolds,refolds,consts} =>
    84     {intros=f intros, unfolds=unfolds, refolds=refolds, consts=consts})
    85 
    86 fun map_unfolds f = TransferData.map
    87   (fn {intros,unfolds,refolds,consts} =>
    88     {intros=intros, unfolds=f unfolds, refolds=refolds, consts=consts})
    89 
    90 fun map_refolds f = TransferData.map
    91   (fn {intros,unfolds,refolds,consts} =>
    92     {intros=intros, unfolds=unfolds, refolds=f refolds, consts=consts})
    93 in
    94 val intro_add = Thm.declaration_attribute (map_intros o Thm.add_thm);
    95 val intro_del = Thm.declaration_attribute (map_intros o Thm.del_thm);
    96 
    97 val unfold_add = Thm.declaration_attribute (map_unfolds o Thm.add_thm);
    98 val unfold_del = Thm.declaration_attribute (map_unfolds o Thm.del_thm);
    99 
   100 val refold_add = Thm.declaration_attribute (map_refolds o Thm.add_thm);
   101 val refold_del = Thm.declaration_attribute (map_refolds o Thm.del_thm);
   102 end
   103 
   104 fun add_const c = Context.theory_map (TransferData.map
   105   (fn {intros,unfolds,refolds,consts} =>
   106     {intros=intros, unfolds=unfolds, refolds=refolds, consts=c::consts}))
   107 
   108 val _ =
   109   Theory.setup
   110    (Attrib.setup @{binding transfer_intro} (Attrib.add_del intro_add intro_del)
   111       "declaration of transfer introduction rule" #>
   112     Attrib.setup @{binding transfer_unfold} (Attrib.add_del unfold_add unfold_del)
   113       "declaration of transfer unfolding rule" #>
   114     Attrib.setup @{binding transfer_refold} (Attrib.add_del refold_add refold_del)
   115       "declaration of transfer refolding rule")
   116 
   117 end;