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