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