src/Pure/conjunction.ML
author wenzelm
Sun Apr 06 15:43:45 2014 +0200 (2014-04-06)
changeset 56436 30ccec1e82fb
parent 46497 89ccf66aa73d
child 59621 291934bac95e
permissions -rw-r--r--
more source positions;
wenzelm@19416
     1
(*  Title:      Pure/conjunction.ML
wenzelm@19416
     2
    Author:     Makarius
wenzelm@19416
     3
wenzelm@19416
     4
Meta-level conjunction.
wenzelm@19416
     5
*)
wenzelm@19416
     6
wenzelm@19416
     7
signature CONJUNCTION =
wenzelm@19416
     8
sig
wenzelm@19416
     9
  val conjunction: cterm
wenzelm@19416
    10
  val mk_conjunction: cterm * cterm -> cterm
wenzelm@23422
    11
  val mk_conjunction_balanced: cterm list -> cterm
wenzelm@19416
    12
  val dest_conjunction: cterm -> cterm * cterm
wenzelm@30823
    13
  val dest_conjunctions: cterm -> cterm list
wenzelm@19416
    14
  val cong: thm -> thm -> thm
wenzelm@23422
    15
  val convs: (cterm -> thm) -> cterm -> thm
wenzelm@19416
    16
  val conjunctionD1: thm
wenzelm@19416
    17
  val conjunctionD2: thm
wenzelm@19416
    18
  val conjunctionI: thm
wenzelm@19416
    19
  val intr: thm -> thm -> thm
wenzelm@23422
    20
  val intr_balanced: thm list -> thm
wenzelm@19416
    21
  val elim: thm -> thm * thm
wenzelm@23422
    22
  val elim_balanced: int -> thm -> thm list
wenzelm@23422
    23
  val curry_balanced: int -> thm -> thm
wenzelm@23422
    24
  val uncurry_balanced: int -> thm -> thm
wenzelm@19416
    25
end;
wenzelm@19416
    26
wenzelm@19416
    27
structure Conjunction: CONJUNCTION =
wenzelm@19416
    28
struct
wenzelm@19416
    29
wenzelm@19416
    30
(** abstract syntax **)
wenzelm@19416
    31
wenzelm@26485
    32
fun certify t = Thm.cterm_of (Context.the_theory (Context.the_thread_data ())) t;
wenzelm@33384
    33
val read_prop = certify o Simple_Syntax.read_prop;
wenzelm@19416
    34
wenzelm@26485
    35
val true_prop = certify Logic.true_prop;
wenzelm@26485
    36
val conjunction = certify Logic.conjunction;
wenzelm@23422
    37
wenzelm@46497
    38
fun mk_conjunction (A, B) = Thm.apply (Thm.apply conjunction A) B;
wenzelm@19416
    39
wenzelm@23422
    40
fun mk_conjunction_balanced [] = true_prop
wenzelm@32765
    41
  | mk_conjunction_balanced ts = Balanced_Tree.make mk_conjunction ts;
wenzelm@23422
    42
wenzelm@19416
    43
fun dest_conjunction ct =
wenzelm@19416
    44
  (case Thm.term_of ct of
wenzelm@26424
    45
    (Const ("Pure.conjunction", _) $ _ $ _) => Thm.dest_binop ct
wenzelm@23422
    46
  | _ => raise TERM ("dest_conjunction", [Thm.term_of ct]));
wenzelm@19416
    47
wenzelm@30823
    48
fun dest_conjunctions ct =
wenzelm@30823
    49
  (case try dest_conjunction ct of
wenzelm@30823
    50
    NONE => [ct]
wenzelm@30823
    51
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@30823
    52
wenzelm@19416
    53
wenzelm@19416
    54
wenzelm@19416
    55
(** derived rules **)
wenzelm@19416
    56
wenzelm@19416
    57
(* conversion *)
wenzelm@19416
    58
wenzelm@19416
    59
val cong = Thm.combination o Thm.combination (Thm.reflexive conjunction);
wenzelm@19416
    60
wenzelm@23422
    61
fun convs cv ct =
wenzelm@23422
    62
  (case try dest_conjunction ct of
wenzelm@23422
    63
    NONE => cv ct
wenzelm@23422
    64
  | SOME (A, B) => cong (convs cv A) (convs cv B));
wenzelm@19416
    65
wenzelm@19416
    66
wenzelm@19416
    67
(* intro/elim *)
wenzelm@19416
    68
wenzelm@19416
    69
local
wenzelm@19416
    70
wenzelm@24241
    71
val A = read_prop "A" and vA = read_prop "?A";
wenzelm@24241
    72
val B = read_prop "B" and vB = read_prop "?B";
wenzelm@24241
    73
val C = read_prop "C";
wenzelm@24241
    74
val ABC = read_prop "A ==> B ==> C";
wenzelm@28856
    75
val A_B = read_prop "A &&& B";
wenzelm@19416
    76
wenzelm@26424
    77
val conjunction_def =
wenzelm@35845
    78
  Thm.unvarify_global
wenzelm@35845
    79
    (Thm.axiom (Context.the_theory (Context.the_thread_data ())) "Pure.conjunction_def");
wenzelm@19416
    80
wenzelm@19416
    81
fun conjunctionD which =
wenzelm@19416
    82
  Drule.implies_intr_list [A, B] (Thm.assume (which (A, B))) COMP
wenzelm@26653
    83
  Thm.forall_elim_vars 0 (Thm.equal_elim conjunction_def (Thm.assume A_B));
wenzelm@19416
    84
wenzelm@19416
    85
in
wenzelm@19416
    86
wenzelm@56436
    87
val conjunctionD1 =
wenzelm@56436
    88
  Drule.store_standard_thm (Binding.make ("conjunctionD1", @{here})) (conjunctionD #1);
wenzelm@56436
    89
wenzelm@56436
    90
val conjunctionD2 =
wenzelm@56436
    91
  Drule.store_standard_thm (Binding.make ("conjunctionD2", @{here})) (conjunctionD #2);
wenzelm@19416
    92
wenzelm@33277
    93
val conjunctionI =
wenzelm@56436
    94
  Drule.store_standard_thm (Binding.make ("conjunctionI", @{here}))
wenzelm@33277
    95
    (Drule.implies_intr_list [A, B]
wenzelm@33277
    96
      (Thm.equal_elim
wenzelm@33277
    97
        (Thm.symmetric conjunction_def)
wenzelm@33277
    98
        (Thm.forall_intr C (Thm.implies_intr ABC
wenzelm@33277
    99
          (Drule.implies_elim_list (Thm.assume ABC) [Thm.assume A, Thm.assume B])))));
wenzelm@19416
   100
wenzelm@23422
   101
wenzelm@20508
   102
fun intr tha thb =
wenzelm@20508
   103
  Thm.implies_elim
wenzelm@20508
   104
    (Thm.implies_elim
wenzelm@20508
   105
      (Thm.instantiate ([], [(vA, Thm.cprop_of tha), (vB, Thm.cprop_of thb)]) conjunctionI)
wenzelm@20508
   106
    tha)
wenzelm@20508
   107
  thb;
wenzelm@19416
   108
wenzelm@19416
   109
fun elim th =
wenzelm@20508
   110
  let
wenzelm@20508
   111
    val (A, B) = dest_conjunction (Thm.cprop_of th)
wenzelm@20508
   112
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@20508
   113
    val inst = Thm.instantiate ([], [(vA, A), (vB, B)]);
wenzelm@20508
   114
  in
wenzelm@20508
   115
   (Thm.implies_elim (inst conjunctionD1) th,
wenzelm@20508
   116
    Thm.implies_elim (inst conjunctionD2) th)
wenzelm@20508
   117
  end;
wenzelm@19416
   118
wenzelm@23422
   119
end;
wenzelm@23422
   120
wenzelm@23422
   121
wenzelm@23535
   122
(* balanced conjuncts *)
wenzelm@23422
   123
wenzelm@23422
   124
fun intr_balanced [] = asm_rl
wenzelm@32765
   125
  | intr_balanced ths = Balanced_Tree.make (uncurry intr) ths;
wenzelm@23422
   126
wenzelm@23422
   127
fun elim_balanced 0 _ = []
wenzelm@32765
   128
  | elim_balanced n th = Balanced_Tree.dest elim n th;
wenzelm@19416
   129
wenzelm@19416
   130
wenzelm@19416
   131
(* currying *)
wenzelm@19416
   132
wenzelm@19416
   133
local
wenzelm@19416
   134
wenzelm@26424
   135
fun conjs thy n =
wenzelm@43329
   136
  let val As = map (fn A => Thm.cterm_of thy (Free (A, propT))) (Name.invent Name.context "A" n)
wenzelm@23422
   137
  in (As, mk_conjunction_balanced As) end;
wenzelm@19416
   138
wenzelm@24241
   139
val B = read_prop "B";
wenzelm@19416
   140
wenzelm@19416
   141
fun comp_rule th rule =
wenzelm@20260
   142
  Thm.adjust_maxidx_thm ~1 (th COMP
wenzelm@35985
   143
    (rule |> Thm.forall_intr_frees |> Thm.forall_elim_vars (Thm.maxidx_of th + 1)));
wenzelm@19416
   144
wenzelm@19416
   145
in
wenzelm@19416
   146
wenzelm@19416
   147
(*
wenzelm@28856
   148
  A1 &&& ... &&& An ==> B
wenzelm@19416
   149
  -----------------------
wenzelm@19416
   150
  A1 ==> ... ==> An ==> B
wenzelm@19416
   151
*)
wenzelm@23422
   152
fun curry_balanced n th =
wenzelm@23422
   153
  if n < 2 then th
wenzelm@23422
   154
  else
wenzelm@23422
   155
    let
wenzelm@26424
   156
      val thy = Thm.theory_of_thm th;
wenzelm@26424
   157
      val (As, C) = conjs thy n;
wenzelm@23422
   158
      val D = Drule.mk_implies (C, B);
wenzelm@23422
   159
    in
wenzelm@23422
   160
      comp_rule th
wenzelm@23422
   161
        (Thm.implies_elim (Thm.assume D) (intr_balanced (map Thm.assume As))
wenzelm@23422
   162
          |> Drule.implies_intr_list (D :: As))
wenzelm@23422
   163
    end;
wenzelm@19416
   164
wenzelm@19416
   165
(*
wenzelm@19416
   166
  A1 ==> ... ==> An ==> B
wenzelm@19416
   167
  -----------------------
wenzelm@28856
   168
  A1 &&& ... &&& An ==> B
wenzelm@19416
   169
*)
wenzelm@23422
   170
fun uncurry_balanced n th =
wenzelm@23422
   171
  if n < 2 then th
wenzelm@23422
   172
  else
wenzelm@23422
   173
    let
wenzelm@26424
   174
      val thy = Thm.theory_of_thm th;
wenzelm@26424
   175
      val (As, C) = conjs thy n;
wenzelm@23422
   176
      val D = Drule.list_implies (As, B);
wenzelm@23422
   177
    in
wenzelm@23422
   178
      comp_rule th
wenzelm@23422
   179
        (Drule.implies_elim_list (Thm.assume D) (elim_balanced n (Thm.assume C))
wenzelm@23422
   180
          |> Drule.implies_intr_list [D, C])
wenzelm@23422
   181
    end;
wenzelm@19416
   182
wenzelm@19416
   183
end;
wenzelm@19416
   184
wenzelm@19416
   185
end;