src/Pure/conjunction.ML
author wenzelm
Thu Apr 13 12:00:53 2006 +0200 (2006-04-13)
changeset 19416 4198e7698f6a
child 20238 7e17d70a9303
permissions -rw-r--r--
Meta-level conjunction.
wenzelm@19416
     1
(*  Title:      Pure/conjunction.ML
wenzelm@19416
     2
    ID:         $Id$
wenzelm@19416
     3
    Author:     Makarius
wenzelm@19416
     4
wenzelm@19416
     5
Meta-level conjunction.
wenzelm@19416
     6
*)
wenzelm@19416
     7
wenzelm@19416
     8
signature CONJUNCTION =
wenzelm@19416
     9
sig
wenzelm@19416
    10
  val conjunction: cterm
wenzelm@19416
    11
  val mk_conjunction: cterm * cterm -> cterm
wenzelm@19416
    12
  val dest_conjunction: cterm -> cterm * cterm
wenzelm@19416
    13
  val cong: thm -> thm -> thm
wenzelm@19416
    14
  val conv: int -> (int -> 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@19416
    19
  val intr_list: thm list -> thm
wenzelm@19416
    20
  val elim: thm -> thm * thm
wenzelm@19416
    21
  val elim_list: thm -> thm list
wenzelm@19416
    22
  val elim_precise: int list -> thm -> thm list list
wenzelm@19416
    23
  val curry: int -> thm -> thm
wenzelm@19416
    24
  val uncurry: int -> thm -> thm
wenzelm@19416
    25
  val split_defined: int -> thm -> thm * thm list
wenzelm@19416
    26
end;
wenzelm@19416
    27
wenzelm@19416
    28
structure Conjunction: CONJUNCTION =
wenzelm@19416
    29
struct
wenzelm@19416
    30
wenzelm@19416
    31
wenzelm@19416
    32
(** abstract syntax **)
wenzelm@19416
    33
wenzelm@19416
    34
fun read s = Thm.read_cterm ProtoPure.thy (s, propT);
wenzelm@19416
    35
val cert = Thm.cterm_of ProtoPure.thy;
wenzelm@19416
    36
wenzelm@19416
    37
val conjunction = cert Logic.conjunction;
wenzelm@19416
    38
fun mk_conjunction (A, B) = Thm.capply (Thm.capply conjunction A) B;
wenzelm@19416
    39
wenzelm@19416
    40
fun dest_conjunction ct =
wenzelm@19416
    41
  (case Thm.term_of ct of
wenzelm@19416
    42
    (Const ("ProtoPure.conjunction", _) $ _ $ _) => Drule.dest_binop ct
wenzelm@19416
    43
  | _ => raise TERM ("dest_conjunction", [term_of ct]));
wenzelm@19416
    44
wenzelm@19416
    45
wenzelm@19416
    46
wenzelm@19416
    47
(** derived rules **)
wenzelm@19416
    48
wenzelm@19416
    49
(* conversion *)
wenzelm@19416
    50
wenzelm@19416
    51
(*rewrite the A's in A1 && ... && An*)
wenzelm@19416
    52
wenzelm@19416
    53
val cong = Thm.combination o Thm.combination (Thm.reflexive conjunction);
wenzelm@19416
    54
wenzelm@19416
    55
fun conv 0 _ = reflexive
wenzelm@19416
    56
  | conv n cv =
wenzelm@19416
    57
      let
wenzelm@19416
    58
        fun cnv i ct =
wenzelm@19416
    59
          if i = n then cv i ct
wenzelm@19416
    60
          else
wenzelm@19416
    61
            (case try dest_conjunction ct of
wenzelm@19416
    62
              NONE => cv i ct
wenzelm@19416
    63
            | SOME (A, B) => cong (cv i A) (cnv (i + 1) B));
wenzelm@19416
    64
      in cnv 1 end;
wenzelm@19416
    65
wenzelm@19416
    66
wenzelm@19416
    67
(* intro/elim *)
wenzelm@19416
    68
wenzelm@19416
    69
local
wenzelm@19416
    70
wenzelm@19416
    71
val A = read "PROP A";
wenzelm@19416
    72
val B = read "PROP B";
wenzelm@19416
    73
val C = read "PROP C";
wenzelm@19416
    74
val ABC = read "PROP A ==> PROP B ==> PROP C";
wenzelm@19416
    75
val A_B = read "PROP ProtoPure.conjunction(A, B)"
wenzelm@19416
    76
wenzelm@19416
    77
val conjunction_def = #1 (freeze_thaw ProtoPure.conjunction_def);
wenzelm@19416
    78
wenzelm@19416
    79
fun conjunctionD which =
wenzelm@19416
    80
  Drule.implies_intr_list [A, B] (Thm.assume (which (A, B))) COMP
wenzelm@19416
    81
  Drule.forall_elim_vars 0 (Thm.equal_elim conjunction_def (Thm.assume A_B));
wenzelm@19416
    82
wenzelm@19416
    83
in
wenzelm@19416
    84
wenzelm@19416
    85
val conjunctionD1 = Drule.store_standard_thm "conjunctionD1" (conjunctionD #1);
wenzelm@19416
    86
val conjunctionD2 = Drule.store_standard_thm "conjunctionD2" (conjunctionD #2);
wenzelm@19416
    87
wenzelm@19416
    88
val conjunctionI = Drule.store_standard_thm "conjunctionI"
wenzelm@19416
    89
  (Drule.implies_intr_list [A, B]
wenzelm@19416
    90
    (Thm.equal_elim
wenzelm@19416
    91
      (Thm.symmetric conjunction_def)
wenzelm@19416
    92
      (Thm.forall_intr C (Thm.implies_intr ABC
wenzelm@19416
    93
        (Drule.implies_elim_list (Thm.assume ABC) [Thm.assume A, Thm.assume B])))));
wenzelm@19416
    94
wenzelm@19416
    95
fun intr tha thb = thb COMP (tha COMP Drule.incr_indexes2 tha thb conjunctionI);
wenzelm@19416
    96
wenzelm@19416
    97
fun intr_list [] = asm_rl
wenzelm@19416
    98
  | intr_list ths = foldr1 (uncurry intr) ths;
wenzelm@19416
    99
wenzelm@19416
   100
fun elim th =
wenzelm@19416
   101
 (th COMP Drule.incr_indexes th conjunctionD1,
wenzelm@19416
   102
  th COMP Drule.incr_indexes th conjunctionD2);
wenzelm@19416
   103
wenzelm@19416
   104
(*((A && B) && C) && D && E -- flat*)
wenzelm@19416
   105
fun elim_list th =
wenzelm@19416
   106
  let val (th1, th2) = elim th
wenzelm@19416
   107
  in elim_list th1 @ elim_list th2 end handle THM _ => [th];
wenzelm@19416
   108
wenzelm@19416
   109
(*(A1 && B1 && C1) && (A2 && B2 && C2 && D2) && A3 && B3 -- improper*)
wenzelm@19416
   110
fun elim_precise spans =
wenzelm@19416
   111
  let
wenzelm@19416
   112
    fun elm 0 _ = []
wenzelm@19416
   113
      | elm 1 th = [th]
wenzelm@19416
   114
      | elm n th =
wenzelm@19416
   115
          let val (th1, th2) = elim th
wenzelm@19416
   116
          in th1 :: elm (n - 1) th2 end;
wenzelm@19416
   117
    fun elms (0 :: ns) ths = [] :: elms ns ths
wenzelm@19416
   118
      | elms (n :: ns) (th :: ths) = elm n th :: elms ns ths
wenzelm@19416
   119
      | elms _ _ = [];
wenzelm@19416
   120
  in elms spans o elm (length (filter_out (equal 0) spans)) end;
wenzelm@19416
   121
wenzelm@19416
   122
end;
wenzelm@19416
   123
wenzelm@19416
   124
wenzelm@19416
   125
(* currying *)
wenzelm@19416
   126
wenzelm@19416
   127
local
wenzelm@19416
   128
wenzelm@19416
   129
fun conjs m =
wenzelm@19416
   130
  let val As = map (fn i => Free ("A" ^ string_of_int i, propT)) (1 upto m)
wenzelm@19416
   131
  in (As, Logic.mk_conjunction_list As) end;
wenzelm@19416
   132
wenzelm@19416
   133
val B = Free ("B", propT);
wenzelm@19416
   134
wenzelm@19416
   135
fun comp_rule th rule =
wenzelm@19416
   136
  Thm.adjust_maxidx_thm (th COMP
wenzelm@19416
   137
    (rule |> Drule.forall_intr_frees |> Drule.forall_elim_vars (Thm.maxidx_of th + 1)));
wenzelm@19416
   138
wenzelm@19416
   139
in
wenzelm@19416
   140
wenzelm@19416
   141
(*
wenzelm@19416
   142
   A1 && ... && An ==> B
wenzelm@19416
   143
  -----------------------
wenzelm@19416
   144
  A1 ==> ... ==> An ==> B
wenzelm@19416
   145
*)
wenzelm@19416
   146
fun curry n th =
wenzelm@19416
   147
  let
wenzelm@19416
   148
    val k =
wenzelm@19416
   149
      (case try Logic.dest_implies (Thm.prop_of th) of
wenzelm@19416
   150
        NONE => 0
wenzelm@19416
   151
      | SOME (prem, _) => length (Logic.dest_conjunction_list prem));
wenzelm@19416
   152
    val m = if n = ~1 then k else Int.min (n, k);
wenzelm@19416
   153
  in
wenzelm@19416
   154
    if m < 2 then th
wenzelm@19416
   155
    else
wenzelm@19416
   156
      let
wenzelm@19416
   157
        val (As, C) = conjs m;
wenzelm@19416
   158
        val cAs = map cert As;
wenzelm@19416
   159
        val D = Logic.mk_implies (Logic.mk_conjunction_list As, B) |> cert;
wenzelm@19416
   160
      in
wenzelm@19416
   161
        comp_rule th
wenzelm@19416
   162
          (Thm.implies_elim (Thm.assume D) (intr_list (map Thm.assume cAs))
wenzelm@19416
   163
            |> Drule.implies_intr_list (D :: cAs))
wenzelm@19416
   164
      end
wenzelm@19416
   165
  end;
wenzelm@19416
   166
wenzelm@19416
   167
(*
wenzelm@19416
   168
  A1 ==> ... ==> An ==> B
wenzelm@19416
   169
  -----------------------
wenzelm@19416
   170
   A1 && ... && An ==> B
wenzelm@19416
   171
*)
wenzelm@19416
   172
fun uncurry n th =
wenzelm@19416
   173
  let
wenzelm@19416
   174
    val k = Thm.nprems_of th;
wenzelm@19416
   175
    val m = if n = ~1 then k else Int.min (n, k);
wenzelm@19416
   176
  in
wenzelm@19416
   177
    if m < 2 then th
wenzelm@19416
   178
    else
wenzelm@19416
   179
      let
wenzelm@19416
   180
        val (As, C) = conjs m ||> cert;
wenzelm@19416
   181
        val D = Logic.list_implies (As, B) |> cert;
wenzelm@19416
   182
      in
wenzelm@19416
   183
        comp_rule th
wenzelm@19416
   184
          (Drule.implies_elim_list (Thm.assume D) (elim_list (Thm.assume C))
wenzelm@19416
   185
            |> Drule.implies_intr_list [D, C])
wenzelm@19416
   186
      end
wenzelm@19416
   187
  end;
wenzelm@19416
   188
wenzelm@19416
   189
end;
wenzelm@19416
   190
wenzelm@19416
   191
wenzelm@19416
   192
(* defined conjunctions *)
wenzelm@19416
   193
wenzelm@19416
   194
fun project th 1 = (th RS conjunctionD1 handle THM _ => th)
wenzelm@19416
   195
  | project th k = project (th RS conjunctionD2) (k - 1);
wenzelm@19416
   196
wenzelm@19416
   197
fun split_defined n eq =
wenzelm@19416
   198
  let
wenzelm@19416
   199
    val intro =
wenzelm@19416
   200
      (eq RS Drule.equal_elim_rule2)
wenzelm@19416
   201
      |> curry n
wenzelm@19416
   202
      |> K (n = 0) ? Thm.eq_assumption 1;
wenzelm@19416
   203
    val dests = map (project (eq RS Drule.equal_elim_rule1)) (1 upto n);
wenzelm@19416
   204
  in (intro, dests) end;
wenzelm@19416
   205
wenzelm@19416
   206
end;