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