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