src/Pure/conjunction.ML
author wenzelm
Thu Oct 23 15:28:01 2008 +0200 (2008-10-23)
changeset 28674 08a77c495dc1
parent 26653 60e0cf6bef89
child 28856 5e009a80fe6d
permissions -rw-r--r--
renamed Thm.get_axiom_i to Thm.axiom;
     1 (*  Title:      Pure/conjunction.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Meta-level conjunction.
     6 *)
     7 
     8 signature CONJUNCTION =
     9 sig
    10   val conjunction: cterm
    11   val mk_conjunction: cterm * cterm -> cterm
    12   val mk_conjunction_balanced: cterm list -> cterm
    13   val dest_conjunction: cterm -> cterm * cterm
    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 SimpleSyntax.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.capply (Thm.capply conjunction A) B;
    39 
    40 fun mk_conjunction_balanced [] = true_prop
    41   | mk_conjunction_balanced ts = BalancedTree.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 
    49 
    50 (** derived rules **)
    51 
    52 (* conversion *)
    53 
    54 val cong = Thm.combination o Thm.combination (Thm.reflexive conjunction);
    55 
    56 fun convs cv ct =
    57   (case try dest_conjunction ct of
    58     NONE => cv ct
    59   | SOME (A, B) => cong (convs cv A) (convs cv B));
    60 
    61 
    62 (* intro/elim *)
    63 
    64 local
    65 
    66 val A = read_prop "A" and vA = read_prop "?A";
    67 val B = read_prop "B" and vB = read_prop "?B";
    68 val C = read_prop "C";
    69 val ABC = read_prop "A ==> B ==> C";
    70 val A_B = read_prop "A && B";
    71 
    72 val conjunction_def =
    73   Thm.unvarify (Thm.axiom (Context.the_theory (Context.the_thread_data ())) "Pure.conjunction_def");
    74 
    75 fun conjunctionD which =
    76   Drule.implies_intr_list [A, B] (Thm.assume (which (A, B))) COMP
    77   Thm.forall_elim_vars 0 (Thm.equal_elim conjunction_def (Thm.assume A_B));
    78 
    79 in
    80 
    81 val conjunctionD1 = Drule.store_standard_thm "conjunctionD1" (conjunctionD #1);
    82 val conjunctionD2 = Drule.store_standard_thm "conjunctionD2" (conjunctionD #2);
    83 
    84 val conjunctionI = Drule.store_standard_thm "conjunctionI"
    85   (Drule.implies_intr_list [A, B]
    86     (Thm.equal_elim
    87       (Thm.symmetric conjunction_def)
    88       (Thm.forall_intr C (Thm.implies_intr ABC
    89         (Drule.implies_elim_list (Thm.assume ABC) [Thm.assume A, Thm.assume B])))));
    90 
    91 
    92 fun intr tha thb =
    93   Thm.implies_elim
    94     (Thm.implies_elim
    95       (Thm.instantiate ([], [(vA, Thm.cprop_of tha), (vB, Thm.cprop_of thb)]) conjunctionI)
    96     tha)
    97   thb;
    98 
    99 fun elim th =
   100   let
   101     val (A, B) = dest_conjunction (Thm.cprop_of th)
   102       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   103     val inst = Thm.instantiate ([], [(vA, A), (vB, B)]);
   104   in
   105    (Thm.implies_elim (inst conjunctionD1) th,
   106     Thm.implies_elim (inst conjunctionD2) th)
   107   end;
   108 
   109 end;
   110 
   111 
   112 (* balanced conjuncts *)
   113 
   114 fun intr_balanced [] = asm_rl
   115   | intr_balanced ths = BalancedTree.make (uncurry intr) ths;
   116 
   117 fun elim_balanced 0 _ = []
   118   | elim_balanced n th = BalancedTree.dest elim n th;
   119 
   120 
   121 (* currying *)
   122 
   123 local
   124 
   125 fun conjs thy n =
   126   let val As = map (fn A => Thm.cterm_of thy (Free (A, propT))) (Name.invents Name.context "A" n)
   127   in (As, mk_conjunction_balanced As) end;
   128 
   129 val B = read_prop "B";
   130 
   131 fun comp_rule th rule =
   132   Thm.adjust_maxidx_thm ~1 (th COMP
   133     (rule |> Drule.forall_intr_frees |> Thm.forall_elim_vars (Thm.maxidx_of th + 1)));
   134 
   135 in
   136 
   137 (*
   138    A1 && ... && An ==> B
   139   -----------------------
   140   A1 ==> ... ==> An ==> B
   141 *)
   142 fun curry_balanced n th =
   143   if n < 2 then th
   144   else
   145     let
   146       val thy = Thm.theory_of_thm th;
   147       val (As, C) = conjs thy n;
   148       val D = Drule.mk_implies (C, B);
   149     in
   150       comp_rule th
   151         (Thm.implies_elim (Thm.assume D) (intr_balanced (map Thm.assume As))
   152           |> Drule.implies_intr_list (D :: As))
   153     end;
   154 
   155 (*
   156   A1 ==> ... ==> An ==> B
   157   -----------------------
   158   A1 && ... && An ==> B
   159 *)
   160 fun uncurry_balanced n th =
   161   if n < 2 then th
   162   else
   163     let
   164       val thy = Thm.theory_of_thm th;
   165       val (As, C) = conjs thy n;
   166       val D = Drule.list_implies (As, B);
   167     in
   168       comp_rule th
   169         (Drule.implies_elim_list (Thm.assume D) (elim_balanced n (Thm.assume C))
   170           |> Drule.implies_intr_list [D, C])
   171     end;
   172 
   173 end;
   174 
   175 end;