src/HOL/Nonstandard_Analysis/transfer_principle.ML
author nipkow
Thu, 17 Jul 2025 21:06:22 +0100
changeset 82885 5d2a599f88af
parent 82643 f1c14af17591
permissions -rw-r--r--
moved lemma
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67635
28f926146986 proper file name;
wenzelm
parents: 64435
diff changeset
     1
(*  Title:      HOL/Nonstandard_Analysis/transfer_principle.ML
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 35625
diff changeset
     2
    Author:     Brian Huffman
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     3
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     4
Transfer principle tactic for nonstandard analysis.
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     5
*)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     6
47328
9f11a3cd84b1 rename ML structure to avoid shadowing earlier name
huffman
parents: 42361
diff changeset
     7
signature TRANSFER_PRINCIPLE =
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     8
sig
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
     9
  val transfer_tac: Proof.context -> thm list -> int -> tactic
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    10
  val add_const: string -> theory -> theory
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    11
end;
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    12
47328
9f11a3cd84b1 rename ML structure to avoid shadowing earlier name
huffman
parents: 42361
diff changeset
    13
structure Transfer_Principle: TRANSFER_PRINCIPLE =
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    14
struct
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    15
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    16
structure Data = Generic_Data
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    17
(
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    18
  type T = {
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    19
    intros: thm list,
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    20
    unfolds: thm list,
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    21
    refolds: thm list,
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    22
    consts: string list
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    23
  };
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    24
  val empty = {intros = [], unfolds = [], refolds = [], consts = []};
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 30528
diff changeset
    25
  fun merge
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    26
    ({intros = intros1, unfolds = unfolds1,
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    27
      refolds = refolds1, consts = consts1},
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    28
     {intros = intros2, unfolds = unfolds2,
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 30528
diff changeset
    29
      refolds = refolds2, consts = consts2}) : T =
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    30
   {intros = Thm.merge_thms (intros1, intros2),
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    31
    unfolds = Thm.merge_thms (unfolds1, unfolds2),
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    32
    refolds = Thm.merge_thms (refolds1, refolds2),
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 30528
diff changeset
    33
    consts = Library.merge (op =) (consts1, consts2)};
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    34
);
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    35
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
    36
fun unstar_typ (Type (\<^type_name>\<open>star\<close>, [t])) = unstar_typ t
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    37
  | unstar_typ (Type (a, Ts)) = Type (a, map unstar_typ Ts)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    38
  | unstar_typ T = T
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    39
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    40
fun unstar_term consts term =
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    41
  let
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    42
    fun unstar (Const(a,T) $ t) = if member (op =) consts a then (unstar t)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    43
          else (Const(a,unstar_typ T) $ unstar t)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    44
      | unstar (f $ t) = unstar f $ unstar t
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    45
      | unstar (Const(a,T)) = Const(a,unstar_typ T)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    46
      | unstar (Abs(a,T,t)) = Abs(a,unstar_typ T,unstar t)
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    47
      | unstar t = t
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    48
  in
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    49
    unstar term
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    50
  end
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    51
64270
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    52
local exception MATCH
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    53
in
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    54
fun transfer_star_tac ctxt =
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    55
  let
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
    56
    fun thm_of (Const (\<^const_name>\<open>Ifun\<close>, _) $ t $ u) = @{thm transfer_Ifun} OF [thm_of t, thm_of u]
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
    57
      | thm_of (Const (\<^const_name>\<open>star_of\<close>, _) $ _) = @{thm star_of_def}
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
    58
      | thm_of (Const (\<^const_name>\<open>star_n\<close>, _) $ _) = @{thm Pure.reflexive}
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    59
      | thm_of _ = raise MATCH;
64270
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    60
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
    61
    fun thm_of_goal (Const (\<^const_name>\<open>Pure.eq\<close>, _) $ t $ (Const (\<^const_name>\<open>star_n\<close>, _) $ _)) =
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    62
          thm_of t
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    63
      | thm_of_goal _ = raise MATCH;
64270
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    64
  in
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    65
    SUBGOAL (fn (t, i) =>
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    66
      resolve_tac ctxt [thm_of_goal (strip_all_body t |> Logic.strip_imp_concl)] i
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    67
      handle MATCH => no_tac)
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    68
  end;
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    69
end;
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    70
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    71
fun transfer_thm_of ctxt ths t =
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    72
  let
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    73
    val {intros,unfolds,refolds,consts} = Data.get (Context.Proof ctxt);
35624
c4e29a0bb8c1 modernized structure Local_Defs;
wenzelm
parents: 33519
diff changeset
    74
    val meta = Local_Defs.meta_rewrite_rule ctxt;
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    75
    val ths' = map meta ths;
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    76
    val unfolds' = map meta unfolds and refolds' = map meta refolds;
82643
f1c14af17591 prefer Simplifier over bootstrap-only Raw_Simplifier
haftmann
parents: 82641
diff changeset
    77
    val (_$_$t') = Thm.concl_of (Simplifier.rewrite_wrt ctxt true unfolds' (Thm.cterm_of ctxt t))
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    78
    val u = unstar_term consts t'
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    79
    val tac =
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 47432
diff changeset
    80
      rewrite_goals_tac ctxt (ths' @ refolds' @ unfolds') THEN
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 47432
diff changeset
    81
      ALLGOALS (Object_Logic.full_atomize_tac ctxt) THEN
58957
c9e744ea8a38 proper context for match_tac etc.;
wenzelm
parents: 58825
diff changeset
    82
      match_tac ctxt [transitive_thm] 1 THEN
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58957
diff changeset
    83
      resolve_tac ctxt [@{thm transfer_start}] 1 THEN
64270
bf474d719011 Modified transfer principle in HOL/NSA to cause less ho-unficiation
Simon Wimmer <wimmers@in.tum.de>
parents: 62479
diff changeset
    84
      REPEAT_ALL_NEW (resolve_tac ctxt intros ORELSE' transfer_star_tac ctxt) 1 THEN
58957
c9e744ea8a38 proper context for match_tac etc.;
wenzelm
parents: 58825
diff changeset
    85
      match_tac ctxt [reflexive_thm] 1
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    86
  in Goal.prove ctxt [] [] (Logic.mk_equals (t,u)) (K tac) end;
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    87
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    88
fun transfer_tac ctxt ths = SUBGOAL (fn (t, _) => (fn th =>
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    89
  let
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    90
    val tr = transfer_thm_of ctxt ths t
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    91
    val (_$ l $ r) = Thm.concl_of tr;
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    92
    val trs = if l aconv r then [] else [tr];
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    93
  in rewrite_goals_tac ctxt trs th end));
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    94
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    95
local
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    96
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
    97
fun map_intros f = Data.map
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    98
  (fn {intros,unfolds,refolds,consts} =>
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
    99
    {intros=f intros, unfolds=unfolds, refolds=refolds, consts=consts})
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   100
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
   101
fun map_unfolds f = Data.map
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   102
  (fn {intros,unfolds,refolds,consts} =>
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   103
    {intros=intros, unfolds=f unfolds, refolds=refolds, consts=consts})
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   104
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
   105
fun map_refolds f = Data.map
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   106
  (fn {intros,unfolds,refolds,consts} =>
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   107
    {intros=intros, unfolds=unfolds, refolds=f refolds, consts=consts})
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   108
in
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
   109
67636
e4eb21f8331c trim context of persistent data;
wenzelm
parents: 67635
diff changeset
   110
val intro_add = Thm.declaration_attribute (map_intros o Thm.add_thm o Thm.trim_context);
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   111
val intro_del = Thm.declaration_attribute (map_intros o Thm.del_thm);
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   112
67636
e4eb21f8331c trim context of persistent data;
wenzelm
parents: 67635
diff changeset
   113
val unfold_add = Thm.declaration_attribute (map_unfolds o Thm.add_thm o Thm.trim_context);
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   114
val unfold_del = Thm.declaration_attribute (map_unfolds o Thm.del_thm);
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   115
67636
e4eb21f8331c trim context of persistent data;
wenzelm
parents: 67635
diff changeset
   116
val refold_add = Thm.declaration_attribute (map_refolds o Thm.add_thm o Thm.trim_context);
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   117
val refold_del = Thm.declaration_attribute (map_refolds o Thm.del_thm);
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
   118
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   119
end
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   120
64435
c93b0e6131c3 misc tuning and modernization;
wenzelm
parents: 64270
diff changeset
   121
fun add_const c = Context.theory_map (Data.map
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   122
  (fn {intros,unfolds,refolds,consts} =>
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   123
    {intros=intros, unfolds=unfolds, refolds=refolds, consts=c::consts}))
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   124
58825
2065f49da190 modernized setup;
wenzelm
parents: 56256
diff changeset
   125
val _ =
2065f49da190 modernized setup;
wenzelm
parents: 56256
diff changeset
   126
  Theory.setup
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
   127
   (Attrib.setup \<^binding>\<open>transfer_intro\<close> (Attrib.add_del intro_add intro_del)
58825
2065f49da190 modernized setup;
wenzelm
parents: 56256
diff changeset
   128
      "declaration of transfer introduction rule" #>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
   129
    Attrib.setup \<^binding>\<open>transfer_unfold\<close> (Attrib.add_del unfold_add unfold_del)
58825
2065f49da190 modernized setup;
wenzelm
parents: 56256
diff changeset
   130
      "declaration of transfer unfolding rule" #>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 67636
diff changeset
   131
    Attrib.setup \<^binding>\<open>transfer_refold\<close> (Attrib.add_del refold_add refold_del)
58825
2065f49da190 modernized setup;
wenzelm
parents: 56256
diff changeset
   132
      "declaration of transfer refolding rule")
27468
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   133
0783dd1dc13d move nonstandard analysis theories to NSA directory
huffman
parents:
diff changeset
   134
end;