src/HOL/Nonstandard_Analysis/transfer.ML
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64435 c93b0e6131c3
permissions -rw-r--r--
executable domain membership checks
wenzelm@62479
     1
(*  Title:      HOL/Nonstandard_Analysis/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@64435
    16
structure Data = 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
wimmers@64270
    53
local exception MATCH
wimmers@64270
    54
in
wimmers@64270
    55
fun transfer_star_tac ctxt =
wimmers@64270
    56
  let
wimmers@64270
    57
    fun thm_of (Const (@{const_name Ifun}, _) $ t $ u) = @{thm transfer_Ifun} OF [thm_of t, thm_of u]
wenzelm@64435
    58
      | thm_of (Const (@{const_name star_of}, _) $ _) = @{thm star_of_def}
wenzelm@64435
    59
      | thm_of (Const (@{const_name star_n}, _) $ _) = @{thm Pure.reflexive}
wenzelm@64435
    60
      | thm_of _ = raise MATCH;
wimmers@64270
    61
wimmers@64270
    62
    fun thm_of_goal (Const (@{const_name Pure.eq}, _) $ t $ (Const (@{const_name star_n}, _) $ _)) =
wenzelm@64435
    63
          thm_of t
wenzelm@64435
    64
      | thm_of_goal _ = raise MATCH;
wimmers@64270
    65
  in
wimmers@64270
    66
    SUBGOAL (fn (t, i) =>
wimmers@64270
    67
      resolve_tac ctxt [thm_of_goal (strip_all_body t |> Logic.strip_imp_concl)] i
wimmers@64270
    68
      handle MATCH => no_tac)
wimmers@64270
    69
  end;
wimmers@64270
    70
end;
wimmers@64270
    71
huffman@27468
    72
fun transfer_thm_of ctxt ths t =
huffman@27468
    73
  let
wenzelm@64435
    74
    val {intros,unfolds,refolds,consts} = Data.get (Context.Proof ctxt);
wenzelm@35624
    75
    val meta = Local_Defs.meta_rewrite_rule ctxt;
huffman@27468
    76
    val ths' = map meta ths;
huffman@27468
    77
    val unfolds' = map meta unfolds and refolds' = map meta refolds;
wenzelm@59643
    78
    val (_$_$t') = Thm.concl_of (Raw_Simplifier.rewrite ctxt true unfolds' (Thm.cterm_of ctxt t))
huffman@27468
    79
    val u = unstar_term consts t'
wenzelm@64435
    80
    val tac =
wenzelm@54742
    81
      rewrite_goals_tac ctxt (ths' @ refolds' @ unfolds') THEN
wenzelm@54742
    82
      ALLGOALS (Object_Logic.full_atomize_tac ctxt) THEN
wenzelm@58957
    83
      match_tac ctxt [transitive_thm] 1 THEN
wenzelm@59498
    84
      resolve_tac ctxt [@{thm transfer_start}] 1 THEN
wimmers@64270
    85
      REPEAT_ALL_NEW (resolve_tac ctxt intros ORELSE' transfer_star_tac ctxt) 1 THEN
wenzelm@58957
    86
      match_tac ctxt [reflexive_thm] 1
wenzelm@64435
    87
  in Goal.prove ctxt [] [] (Logic.mk_equals (t,u)) (K tac) end;
huffman@27468
    88
wenzelm@64435
    89
fun transfer_tac ctxt ths = SUBGOAL (fn (t, _) => (fn th =>
wenzelm@64435
    90
  let
wenzelm@64435
    91
    val tr = transfer_thm_of ctxt ths t
wenzelm@64435
    92
    val (_$ l $ r) = Thm.concl_of tr;
wenzelm@64435
    93
    val trs = if l aconv r then [] else [tr];
wenzelm@64435
    94
  in rewrite_goals_tac ctxt trs th end));
huffman@27468
    95
huffman@27468
    96
local
huffman@27468
    97
wenzelm@64435
    98
fun map_intros f = Data.map
huffman@27468
    99
  (fn {intros,unfolds,refolds,consts} =>
huffman@27468
   100
    {intros=f intros, unfolds=unfolds, refolds=refolds, consts=consts})
huffman@27468
   101
wenzelm@64435
   102
fun map_unfolds f = Data.map
huffman@27468
   103
  (fn {intros,unfolds,refolds,consts} =>
huffman@27468
   104
    {intros=intros, unfolds=f unfolds, refolds=refolds, consts=consts})
huffman@27468
   105
wenzelm@64435
   106
fun map_refolds f = Data.map
huffman@27468
   107
  (fn {intros,unfolds,refolds,consts} =>
huffman@27468
   108
    {intros=intros, unfolds=unfolds, refolds=f refolds, consts=consts})
huffman@27468
   109
in
wenzelm@64435
   110
huffman@27468
   111
val intro_add = Thm.declaration_attribute (map_intros o Thm.add_thm);
huffman@27468
   112
val intro_del = Thm.declaration_attribute (map_intros o Thm.del_thm);
huffman@27468
   113
huffman@27468
   114
val unfold_add = Thm.declaration_attribute (map_unfolds o Thm.add_thm);
huffman@27468
   115
val unfold_del = Thm.declaration_attribute (map_unfolds o Thm.del_thm);
huffman@27468
   116
huffman@27468
   117
val refold_add = Thm.declaration_attribute (map_refolds o Thm.add_thm);
huffman@27468
   118
val refold_del = Thm.declaration_attribute (map_refolds o Thm.del_thm);
wenzelm@64435
   119
huffman@27468
   120
end
huffman@27468
   121
wenzelm@64435
   122
fun add_const c = Context.theory_map (Data.map
huffman@27468
   123
  (fn {intros,unfolds,refolds,consts} =>
huffman@27468
   124
    {intros=intros, unfolds=unfolds, refolds=refolds, consts=c::consts}))
huffman@27468
   125
wenzelm@58825
   126
val _ =
wenzelm@58825
   127
  Theory.setup
wenzelm@58825
   128
   (Attrib.setup @{binding transfer_intro} (Attrib.add_del intro_add intro_del)
wenzelm@58825
   129
      "declaration of transfer introduction rule" #>
wenzelm@58825
   130
    Attrib.setup @{binding transfer_unfold} (Attrib.add_del unfold_add unfold_del)
wenzelm@58825
   131
      "declaration of transfer unfolding rule" #>
wenzelm@58825
   132
    Attrib.setup @{binding transfer_refold} (Attrib.add_del refold_add refold_del)
wenzelm@58825
   133
      "declaration of transfer refolding rule")
huffman@27468
   134
huffman@27468
   135
end;