src/HOL/Tools/SMT/z3_proof_literals.ML
author haftmann
Sat Aug 28 16:14:32 2010 +0200 (2010-08-28)
changeset 38864 4abe644fcea5
parent 38795 848be46708dc
child 40579 98ebd2300823
permissions -rw-r--r--
formerly unnamed infix equality now named HOL.eq
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/z3_proof_literals.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
Proof tools related to conjunctions and disjunctions.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature Z3_PROOF_LITERALS =
boehmes@36898
     8
sig
boehmes@36898
     9
  (* literal table *)
boehmes@36898
    10
  type littab = thm Termtab.table
boehmes@36898
    11
  val make_littab: thm list -> littab
boehmes@36898
    12
  val insert_lit: thm -> littab -> littab
boehmes@36898
    13
  val delete_lit: thm -> littab -> littab
boehmes@36898
    14
  val lookup_lit: littab -> term -> thm option
boehmes@36898
    15
  val get_first_lit: (term -> bool) -> littab -> thm option
boehmes@36898
    16
boehmes@36898
    17
  (* rules *)
boehmes@36898
    18
  val true_thm: thm
boehmes@36898
    19
  val rewrite_true: thm
boehmes@36898
    20
boehmes@36898
    21
  (* properties *)
boehmes@36898
    22
  val is_conj: term -> bool
boehmes@36898
    23
  val is_disj: term -> bool
boehmes@36898
    24
  val exists_lit: bool -> (term -> bool) -> term -> bool
boehmes@36898
    25
boehmes@36898
    26
  (* proof tools *)
boehmes@36898
    27
  val explode: bool -> bool -> bool -> term list -> thm -> thm list
boehmes@36898
    28
  val join: bool -> littab -> term -> thm
boehmes@36898
    29
  val prove_conj_disj_eq: cterm -> thm
boehmes@36898
    30
end
boehmes@36898
    31
boehmes@36898
    32
structure Z3_Proof_Literals: Z3_PROOF_LITERALS =
boehmes@36898
    33
struct
boehmes@36898
    34
boehmes@36898
    35
structure T = Z3_Proof_Tools
boehmes@36898
    36
boehmes@36898
    37
boehmes@36898
    38
boehmes@36898
    39
(** literal table **)
boehmes@36898
    40
boehmes@36898
    41
type littab = thm Termtab.table
boehmes@36898
    42
boehmes@36898
    43
fun make_littab thms = fold (Termtab.update o `T.prop_of) thms Termtab.empty
boehmes@36898
    44
boehmes@36898
    45
fun insert_lit thm = Termtab.update (`T.prop_of thm)
boehmes@36898
    46
fun delete_lit thm = Termtab.delete (T.prop_of thm)
boehmes@36898
    47
fun lookup_lit lits = Termtab.lookup lits
boehmes@36898
    48
fun get_first_lit f =
boehmes@36898
    49
  Termtab.get_first (fn (t, thm) => if f t then SOME thm else NONE)
boehmes@36898
    50
boehmes@36898
    51
boehmes@36898
    52
boehmes@36898
    53
(** rules **)
boehmes@36898
    54
boehmes@36898
    55
val true_thm = @{lemma "~False" by simp}
boehmes@36898
    56
val rewrite_true = @{lemma "True == ~ False" by simp}
boehmes@36898
    57
boehmes@36898
    58
boehmes@36898
    59
boehmes@36898
    60
(** properties and term operations **)
boehmes@36898
    61
boehmes@36898
    62
val is_neg = (fn @{term Not} $ _ => true | _ => false)
boehmes@36898
    63
fun is_neg' f = (fn @{term Not} $ t => f t | _ => false)
boehmes@36898
    64
val is_dneg = is_neg' is_neg
haftmann@38795
    65
val is_conj = (fn @{term HOL.conj} $ _ $ _ => true | _ => false)
haftmann@38795
    66
val is_disj = (fn @{term HOL.disj} $ _ $ _ => true | _ => false)
boehmes@36898
    67
boehmes@36898
    68
fun dest_disj_term' f = (fn
haftmann@38795
    69
    @{term Not} $ (@{term HOL.disj} $ t $ u) => SOME (f t, f u)
boehmes@36898
    70
  | _ => NONE)
boehmes@36898
    71
haftmann@38795
    72
val dest_conj_term = (fn @{term HOL.conj} $ t $ u => SOME (t, u) | _ => NONE)
boehmes@36898
    73
val dest_disj_term =
boehmes@36898
    74
  dest_disj_term' (fn @{term Not} $ t => t | t => @{term Not} $ t)
boehmes@36898
    75
boehmes@36898
    76
fun exists_lit is_conj P =
boehmes@36898
    77
  let
boehmes@36898
    78
    val dest = if is_conj then dest_conj_term else dest_disj_term
boehmes@36898
    79
    fun exists t = P t orelse
boehmes@36898
    80
      (case dest t of
boehmes@36898
    81
        SOME (t1, t2) => exists t1 orelse exists t2
boehmes@36898
    82
      | NONE => false)
boehmes@36898
    83
  in exists end
boehmes@36898
    84
boehmes@36898
    85
boehmes@36898
    86
boehmes@36898
    87
(** proof tools **)
boehmes@36898
    88
boehmes@36898
    89
(* explosion of conjunctions and disjunctions *)
boehmes@36898
    90
boehmes@36898
    91
local
boehmes@36898
    92
  fun destc ct = Thm.dest_binop (Thm.dest_arg ct)
boehmes@36898
    93
  val dest_conj1 = T.precompose2 destc @{thm conjunct1}
boehmes@36898
    94
  val dest_conj2 = T.precompose2 destc @{thm conjunct2}
boehmes@36898
    95
  fun dest_conj_rules t =
boehmes@36898
    96
    dest_conj_term t |> Option.map (K (dest_conj1, dest_conj2))
boehmes@36898
    97
    
boehmes@36898
    98
  fun destd f ct = f (Thm.dest_binop (Thm.dest_arg (Thm.dest_arg ct)))
boehmes@36898
    99
  val dn1 = apfst Thm.dest_arg and dn2 = apsnd Thm.dest_arg
boehmes@36898
   100
  val dest_disj1 = T.precompose2 (destd I) @{lemma "~(P | Q) ==> ~P" by fast}
boehmes@36898
   101
  val dest_disj2 = T.precompose2 (destd dn1) @{lemma "~(~P | Q) ==> P" by fast}
boehmes@36898
   102
  val dest_disj3 = T.precompose2 (destd I) @{lemma "~(P | Q) ==> ~Q" by fast}
boehmes@36898
   103
  val dest_disj4 = T.precompose2 (destd dn2) @{lemma "~(P | ~Q) ==> Q" by fast}
boehmes@36898
   104
boehmes@36898
   105
  fun dest_disj_rules t =
boehmes@36898
   106
    (case dest_disj_term' is_neg t of
boehmes@36898
   107
      SOME (true, true) => SOME (dest_disj2, dest_disj4)
boehmes@36898
   108
    | SOME (true, false) => SOME (dest_disj2, dest_disj3)
boehmes@36898
   109
    | SOME (false, true) => SOME (dest_disj1, dest_disj4)
boehmes@36898
   110
    | SOME (false, false) => SOME (dest_disj1, dest_disj3)
boehmes@36898
   111
    | NONE => NONE)
boehmes@36898
   112
boehmes@36898
   113
  fun destn ct = [Thm.dest_arg (Thm.dest_arg (Thm.dest_arg ct))]
boehmes@36898
   114
  val dneg_rule = T.precompose destn @{thm notnotD}
boehmes@36898
   115
in
boehmes@36898
   116
boehmes@36898
   117
(* explode a term into literals and collect all rules to be able to deduce
boehmes@36898
   118
   particular literals afterwards *)
boehmes@36898
   119
fun explode_term is_conj =
boehmes@36898
   120
  let
boehmes@36898
   121
    val dest = if is_conj then dest_conj_term else dest_disj_term
boehmes@36898
   122
    val dest_rules = if is_conj then dest_conj_rules else dest_disj_rules
boehmes@36898
   123
boehmes@36898
   124
    fun add (t, rs) = Termtab.map_default (t, rs)
boehmes@36898
   125
      (fn rs' => if length rs' < length rs then rs' else rs)
boehmes@36898
   126
boehmes@36898
   127
    fun explode1 rules t =
boehmes@36898
   128
      (case dest t of
boehmes@36898
   129
        SOME (t1, t2) =>
boehmes@36898
   130
          let val (rule1, rule2) = the (dest_rules t)
boehmes@36898
   131
          in
boehmes@36898
   132
            explode1 (rule1 :: rules) t1 #>
boehmes@36898
   133
            explode1 (rule2 :: rules) t2 #>
boehmes@36898
   134
            add (t, rev rules)
boehmes@36898
   135
          end
boehmes@36898
   136
      | NONE => add (t, rev rules))
boehmes@36898
   137
boehmes@36898
   138
    fun explode0 (@{term Not} $ (@{term Not} $ t)) =
boehmes@36898
   139
          Termtab.make [(t, [dneg_rule])]
boehmes@36898
   140
      | explode0 t = explode1 [] t Termtab.empty
boehmes@36898
   141
boehmes@36898
   142
  in explode0 end
boehmes@36898
   143
boehmes@36898
   144
(* extract a literal by applying previously collected rules *)
boehmes@36898
   145
fun extract_lit thm rules = fold T.compose rules thm
boehmes@36898
   146
boehmes@36898
   147
boehmes@36898
   148
(* explode a theorem into its literals *)
boehmes@36898
   149
fun explode is_conj full keep_intermediate stop_lits =
boehmes@36898
   150
  let
boehmes@36898
   151
    val dest_rules = if is_conj then dest_conj_rules else dest_disj_rules
boehmes@36898
   152
    val tab = fold (Termtab.update o rpair ()) stop_lits Termtab.empty
boehmes@36898
   153
boehmes@36898
   154
    fun explode1 thm =
boehmes@36898
   155
      if Termtab.defined tab (T.prop_of thm) then cons thm
boehmes@36898
   156
      else
boehmes@36898
   157
        (case dest_rules (T.prop_of thm) of
boehmes@36898
   158
          SOME (rule1, rule2) =>
boehmes@36898
   159
            explode2 rule1 thm #>
boehmes@36898
   160
            explode2 rule2 thm #>
boehmes@36898
   161
            keep_intermediate ? cons thm
boehmes@36898
   162
        | NONE => cons thm)
boehmes@36898
   163
boehmes@36898
   164
    and explode2 dest_rule thm =
boehmes@36898
   165
      if full orelse exists_lit is_conj (Termtab.defined tab) (T.prop_of thm)
boehmes@36898
   166
      then explode1 (T.compose dest_rule thm)
boehmes@36898
   167
      else cons (T.compose dest_rule thm)
boehmes@36898
   168
boehmes@36898
   169
    fun explode0 thm =
boehmes@36898
   170
      if not is_conj andalso is_dneg (T.prop_of thm)
boehmes@36898
   171
      then [T.compose dneg_rule thm]
boehmes@36898
   172
      else explode1 thm []
boehmes@36898
   173
boehmes@36898
   174
  in explode0 end
boehmes@36898
   175
boehmes@36898
   176
end
boehmes@36898
   177
boehmes@36898
   178
boehmes@36898
   179
boehmes@36898
   180
(* joining of literals to conjunctions or disjunctions *)
boehmes@36898
   181
boehmes@36898
   182
local
boehmes@36898
   183
  fun on_cprem i f thm = f (Thm.cprem_of thm i)
boehmes@36898
   184
  fun on_cprop f thm = f (Thm.cprop_of thm)
boehmes@36898
   185
  fun precomp2 f g thm = (on_cprem 1 f thm, on_cprem 2 g thm, f, g, thm)
boehmes@36898
   186
  fun comp2 (cv1, cv2, f, g, rule) thm1 thm2 =
boehmes@36898
   187
    Thm.instantiate ([], [(cv1, on_cprop f thm1), (cv2, on_cprop g thm2)]) rule
boehmes@36898
   188
    |> T.discharge thm1 |> T.discharge thm2
boehmes@36898
   189
boehmes@36898
   190
  fun d1 ct = Thm.dest_arg ct and d2 ct = Thm.dest_arg (Thm.dest_arg ct)
boehmes@36898
   191
boehmes@36898
   192
  val conj_rule = precomp2 d1 d1 @{thm conjI}
boehmes@36898
   193
  fun comp_conj ((_, thm1), (_, thm2)) = comp2 conj_rule thm1 thm2
boehmes@36898
   194
boehmes@36898
   195
  val disj1 = precomp2 d2 d2 @{lemma "~P ==> ~Q ==> ~(P | Q)" by fast}
boehmes@36898
   196
  val disj2 = precomp2 d2 d1 @{lemma "~P ==> Q ==> ~(P | ~Q)" by fast}
boehmes@36898
   197
  val disj3 = precomp2 d1 d2 @{lemma "P ==> ~Q ==> ~(~P | Q)" by fast}
boehmes@36898
   198
  val disj4 = precomp2 d1 d1 @{lemma "P ==> Q ==> ~(~P | ~Q)" by fast}
boehmes@36898
   199
boehmes@36898
   200
  fun comp_disj ((false, thm1), (false, thm2)) = comp2 disj1 thm1 thm2
boehmes@36898
   201
    | comp_disj ((false, thm1), (true, thm2)) = comp2 disj2 thm1 thm2
boehmes@36898
   202
    | comp_disj ((true, thm1), (false, thm2)) = comp2 disj3 thm1 thm2
boehmes@36898
   203
    | comp_disj ((true, thm1), (true, thm2)) = comp2 disj4 thm1 thm2
boehmes@36898
   204
haftmann@38795
   205
  fun dest_conj (@{term HOL.conj} $ t $ u) = ((false, t), (false, u))
boehmes@36898
   206
    | dest_conj t = raise TERM ("dest_conj", [t])
boehmes@36898
   207
boehmes@36898
   208
  val neg = (fn @{term Not} $ t => (true, t) | t => (false, @{term Not} $ t))
haftmann@38795
   209
  fun dest_disj (@{term Not} $ (@{term HOL.disj} $ t $ u)) = (neg t, neg u)
boehmes@36898
   210
    | dest_disj t = raise TERM ("dest_disj", [t])
boehmes@36898
   211
boehmes@36898
   212
  val dnegE = T.precompose (single o d2 o d1) @{thm notnotD}
boehmes@36898
   213
  val dnegI = T.precompose (single o d1) @{lemma "P ==> ~~P" by fast}
boehmes@36898
   214
  fun as_dneg f t = f (@{term Not} $ (@{term Not} $ t))
boehmes@36898
   215
boehmes@36898
   216
  fun dni f = apsnd f o Thm.dest_binop o f o d1
boehmes@36898
   217
  val negIffE = T.precompose2 (dni d1) @{lemma "~(P = (~Q)) ==> Q = P" by fast}
boehmes@36898
   218
  val negIffI = T.precompose2 (dni I) @{lemma "P = Q ==> ~(Q = (~P))" by fast}
boehmes@36898
   219
  val iff_const = @{term "op = :: bool => _"}
boehmes@36898
   220
  fun as_negIff f (@{term "op = :: bool => _"} $ t $ u) =
boehmes@36898
   221
        f (@{term Not} $ (iff_const $ u $ (@{term Not} $ t)))
boehmes@36898
   222
    | as_negIff _ _ = NONE
boehmes@36898
   223
in
boehmes@36898
   224
boehmes@36898
   225
fun join is_conj littab t =
boehmes@36898
   226
  let
boehmes@36898
   227
    val comp = if is_conj then comp_conj else comp_disj
boehmes@36898
   228
    val dest = if is_conj then dest_conj else dest_disj
boehmes@36898
   229
boehmes@36898
   230
    val lookup = lookup_lit littab
boehmes@36898
   231
boehmes@36898
   232
    fun lookup_rule t =
boehmes@36898
   233
      (case t of
boehmes@36898
   234
        @{term Not} $ (@{term Not} $ t) => (T.compose dnegI, lookup t)
boehmes@36898
   235
      | @{term Not} $ (@{term "op = :: bool => _"} $ t $ (@{term Not} $ u)) =>
boehmes@36898
   236
          (T.compose negIffI, lookup (iff_const $ u $ t))
haftmann@38864
   237
      | @{term Not} $ ((eq as Const (@{const_name HOL.eq}, _)) $ t $ u) =>
boehmes@36898
   238
          let fun rewr lit = lit COMP @{thm not_sym}
boehmes@36898
   239
          in (rewr, lookup (@{term Not} $ (eq $ u $ t))) end
boehmes@36898
   240
      | _ =>
boehmes@36898
   241
          (case as_dneg lookup t of
boehmes@36898
   242
            NONE => (T.compose negIffE, as_negIff lookup t)
boehmes@36898
   243
          | x => (T.compose dnegE, x)))
boehmes@36898
   244
boehmes@36898
   245
    fun join1 (s, t) =
boehmes@36898
   246
      (case lookup t of
boehmes@36898
   247
        SOME lit => (s, lit)
boehmes@36898
   248
      | NONE => 
boehmes@36898
   249
          (case lookup_rule t of
boehmes@36898
   250
            (rewrite, SOME lit) => (s, rewrite lit)
boehmes@36898
   251
          | (_, NONE) => (s, comp (pairself join1 (dest t)))))
boehmes@36898
   252
boehmes@36898
   253
  in snd (join1 (if is_conj then (false, t) else (true, t))) end
boehmes@36898
   254
boehmes@36898
   255
end
boehmes@36898
   256
boehmes@36898
   257
boehmes@36898
   258
boehmes@36898
   259
(* proving equality of conjunctions or disjunctions *)
boehmes@36898
   260
boehmes@36898
   261
fun iff_intro thm1 thm2 = thm2 COMP (thm1 COMP @{thm iffI})
boehmes@36898
   262
boehmes@36898
   263
local
boehmes@36898
   264
  val cp1 = @{lemma "(~P) = (~Q) ==> P = Q" by simp}
boehmes@36898
   265
  val cp2 = @{lemma "(~P) = Q ==> P = (~Q)" by fastsimp}
boehmes@36898
   266
  val cp3 = @{lemma "P = (~Q) ==> (~P) = Q" by simp}
boehmes@36898
   267
  val neg = Thm.capply @{cterm Not}
boehmes@36898
   268
in
boehmes@36898
   269
fun contrapos1 prove (ct, cu) = prove (neg ct, neg cu) COMP cp1
boehmes@36898
   270
fun contrapos2 prove (ct, cu) = prove (neg ct, Thm.dest_arg cu) COMP cp2
boehmes@36898
   271
fun contrapos3 prove (ct, cu) = prove (Thm.dest_arg ct, neg cu) COMP cp3
boehmes@36898
   272
end
boehmes@36898
   273
boehmes@36898
   274
boehmes@36898
   275
local
boehmes@36898
   276
  val contra_rule = @{lemma "P ==> ~P ==> False" by (rule notE)}
boehmes@36898
   277
  fun contra_left conj thm =
boehmes@36898
   278
    let
boehmes@36898
   279
      val rules = explode_term conj (T.prop_of thm)
boehmes@36898
   280
      fun contra_lits (t, rs) =
boehmes@36898
   281
        (case t of
boehmes@36898
   282
          @{term Not} $ u => Termtab.lookup rules u |> Option.map (pair rs)
boehmes@36898
   283
        | _ => NONE)
boehmes@36898
   284
    in
boehmes@36898
   285
      (case Termtab.lookup rules @{term False} of
boehmes@36898
   286
        SOME rs => extract_lit thm rs
boehmes@36898
   287
      | NONE =>
boehmes@36898
   288
          the (Termtab.get_first contra_lits rules)
boehmes@36898
   289
          |> pairself (extract_lit thm)
boehmes@36898
   290
          |> (fn (nlit, plit) => nlit COMP (plit COMP contra_rule)))
boehmes@36898
   291
    end
boehmes@36898
   292
boehmes@36898
   293
  val falseE_v = Thm.dest_arg (Thm.dest_arg (Thm.cprop_of @{thm FalseE}))
boehmes@36898
   294
  fun contra_right ct = Thm.instantiate ([], [(falseE_v, ct)]) @{thm FalseE}
boehmes@36898
   295
in
boehmes@36898
   296
fun contradict conj ct =
boehmes@36898
   297
  iff_intro (T.under_assumption (contra_left conj) ct) (contra_right ct)
boehmes@36898
   298
end
boehmes@36898
   299
boehmes@36898
   300
boehmes@36898
   301
local
boehmes@36898
   302
  fun prove_eq l r (cl, cr) =
boehmes@36898
   303
    let
boehmes@36898
   304
      fun explode' is_conj = explode is_conj true (l <> r) []
boehmes@36898
   305
      fun make_tab is_conj thm = make_littab (true_thm :: explode' is_conj thm)
boehmes@36898
   306
      fun prove is_conj ct tab = join is_conj tab (Thm.term_of ct)
boehmes@36898
   307
boehmes@36898
   308
      val thm1 = T.under_assumption (prove r cr o make_tab l) cl
boehmes@36898
   309
      val thm2 = T.under_assumption (prove l cl o make_tab r) cr
boehmes@36898
   310
    in iff_intro thm1 thm2 end
boehmes@36898
   311
boehmes@36898
   312
  datatype conj_disj = CONJ | DISJ | NCON | NDIS
boehmes@36898
   313
  fun kind_of t =
boehmes@36898
   314
    if is_conj t then SOME CONJ
boehmes@36898
   315
    else if is_disj t then SOME DISJ
boehmes@36898
   316
    else if is_neg' is_conj t then SOME NCON
boehmes@36898
   317
    else if is_neg' is_disj t then SOME NDIS
boehmes@36898
   318
    else NONE
boehmes@36898
   319
in
boehmes@36898
   320
boehmes@36898
   321
fun prove_conj_disj_eq ct =
boehmes@36898
   322
  let val cp as (cl, cr) = Thm.dest_binop (Thm.dest_arg ct)
boehmes@36898
   323
  in
boehmes@36898
   324
    (case (kind_of (Thm.term_of cl), Thm.term_of cr) of
boehmes@36898
   325
      (SOME CONJ, @{term False}) => contradict true cl
boehmes@36898
   326
    | (SOME DISJ, @{term "~False"}) => contrapos2 (contradict false o fst) cp
boehmes@36898
   327
    | (kl, _) =>
boehmes@36898
   328
        (case (kl, kind_of (Thm.term_of cr)) of
boehmes@36898
   329
          (SOME CONJ, SOME CONJ) => prove_eq true true cp
boehmes@36898
   330
        | (SOME CONJ, SOME NDIS) => prove_eq true false cp
boehmes@36898
   331
        | (SOME CONJ, _) => prove_eq true true cp
boehmes@36898
   332
        | (SOME DISJ, SOME DISJ) => contrapos1 (prove_eq false false) cp
boehmes@36898
   333
        | (SOME DISJ, SOME NCON) => contrapos2 (prove_eq false true) cp
boehmes@36898
   334
        | (SOME DISJ, _) => contrapos1 (prove_eq false false) cp
boehmes@36898
   335
        | (SOME NCON, SOME NCON) => contrapos1 (prove_eq true true) cp
boehmes@36898
   336
        | (SOME NCON, SOME DISJ) => contrapos3 (prove_eq true false) cp
boehmes@36898
   337
        | (SOME NCON, NONE) => contrapos3 (prove_eq true false) cp
boehmes@36898
   338
        | (SOME NDIS, SOME NDIS) => prove_eq false false cp
boehmes@36898
   339
        | (SOME NDIS, SOME CONJ) => prove_eq false true cp
boehmes@36898
   340
        | (SOME NDIS, NONE) => prove_eq false true cp
boehmes@36898
   341
        | _ => raise CTERM ("prove_conj_disj_eq", [ct])))
boehmes@36898
   342
  end
boehmes@36898
   343
boehmes@36898
   344
end
boehmes@36898
   345
boehmes@36898
   346
end