src/HOL/Tools/SMT/z3_proof_reconstruction.ML
author wenzelm
Sat Jul 27 16:35:51 2013 +0200 (2013-07-27)
changeset 52732 b4da1f2ec73f
parent 52230 1105b3b5aa77
child 54742 7a86358a3c0b
permissions -rw-r--r--
standardized aliases;
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/z3_proof_reconstruction.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
Proof reconstruction for proofs found by Z3.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature Z3_PROOF_RECONSTRUCTION =
boehmes@36898
     8
sig
boehmes@36899
     9
  val add_z3_rule: thm -> Context.generic -> Context.generic
boehmes@40162
    10
  val reconstruct: Proof.context -> SMT_Translate.recon -> string list ->
boehmes@41127
    11
    int list * thm
boehmes@36898
    12
  val setup: theory -> theory
boehmes@36898
    13
end
boehmes@36898
    14
boehmes@36898
    15
structure Z3_Proof_Reconstruction: Z3_PROOF_RECONSTRUCTION =
boehmes@36898
    16
struct
boehmes@36898
    17
boehmes@36898
    18
boehmes@40424
    19
fun z3_exn msg = raise SMT_Failure.SMT (SMT_Failure.Other_Failure
boehmes@40162
    20
  ("Z3 proof reconstruction: " ^ msg))
boehmes@36898
    21
boehmes@36898
    22
boehmes@36898
    23
boehmes@41130
    24
(* net of schematic rules *)
boehmes@36898
    25
boehmes@36898
    26
val z3_ruleN = "z3_rule"
boehmes@36898
    27
boehmes@36898
    28
local
boehmes@36898
    29
  val description = "declaration of Z3 proof rules"
boehmes@36898
    30
boehmes@36898
    31
  val eq = Thm.eq_thm
boehmes@36898
    32
boehmes@36898
    33
  structure Z3_Rules = Generic_Data
boehmes@36898
    34
  (
boehmes@36898
    35
    type T = thm Net.net
boehmes@36898
    36
    val empty = Net.empty
boehmes@36898
    37
    val extend = I
boehmes@36898
    38
    val merge = Net.merge eq
boehmes@36898
    39
  )
boehmes@36898
    40
wenzelm@52230
    41
  val prep = `Thm.prop_of o rewrite_rule [Z3_Proof_Literals.rewrite_true]
boehmes@36898
    42
boehmes@36898
    43
  fun ins thm net = Net.insert_term eq (prep thm) net handle Net.INSERT => net
boehmes@36898
    44
  fun del thm net = Net.delete_term eq (prep thm) net handle Net.DELETE => net
boehmes@36898
    45
boehmes@36898
    46
  val add = Thm.declaration_attribute (Z3_Rules.map o ins)
boehmes@36898
    47
  val del = Thm.declaration_attribute (Z3_Rules.map o del)
boehmes@36898
    48
in
boehmes@36898
    49
boehmes@36899
    50
val add_z3_rule = Z3_Rules.map o ins
boehmes@36898
    51
boehmes@36898
    52
fun by_schematic_rule ctxt ct =
boehmes@41328
    53
  the (Z3_Proof_Tools.net_instance (Z3_Rules.get (Context.Proof ctxt)) ct)
boehmes@36898
    54
boehmes@36898
    55
val z3_rules_setup =
boehmes@36898
    56
  Attrib.setup (Binding.name z3_ruleN) (Attrib.add_del add del) description #>
wenzelm@39557
    57
  Global_Theory.add_thms_dynamic (Binding.name z3_ruleN, Net.content o Z3_Rules.get)
boehmes@36898
    58
boehmes@36898
    59
end
boehmes@36898
    60
boehmes@36898
    61
boehmes@36898
    62
boehmes@41130
    63
(* proof tools *)
boehmes@36898
    64
boehmes@36898
    65
fun named ctxt name prover ct =
boehmes@40424
    66
  let val _ = SMT_Config.trace_msg ctxt I ("Z3: trying " ^ name ^ " ...")
boehmes@36898
    67
  in prover ct end
boehmes@36898
    68
boehmes@36898
    69
fun NAMED ctxt name tac i st =
boehmes@40424
    70
  let val _ = SMT_Config.trace_msg ctxt I ("Z3: trying " ^ name ^ " ...")
boehmes@36898
    71
  in tac i st end
boehmes@36898
    72
boehmes@36898
    73
fun pretty_goal ctxt thms t =
boehmes@36898
    74
  [Pretty.block [Pretty.str "proposition: ", Syntax.pretty_term ctxt t]]
boehmes@36898
    75
  |> not (null thms) ? cons (Pretty.big_list "assumptions:"
boehmes@36898
    76
       (map (Display.pretty_thm ctxt) thms))
boehmes@36898
    77
boehmes@36898
    78
fun try_apply ctxt thms =
boehmes@36898
    79
  let
boehmes@36898
    80
    fun try_apply_err ct = Pretty.string_of (Pretty.chunks [
boehmes@36898
    81
      Pretty.big_list ("Z3 found a proof," ^
boehmes@36898
    82
        " but proof reconstruction failed at the following subgoal:")
boehmes@36898
    83
        (pretty_goal ctxt thms (Thm.term_of ct)),
boehmes@36898
    84
      Pretty.str ("Adding a rule to the lemma group " ^ quote z3_ruleN ^
boehmes@36898
    85
        " might solve this problem.")])
boehmes@36898
    86
boehmes@36898
    87
    fun apply [] ct = error (try_apply_err ct)
boehmes@36898
    88
      | apply (prover :: provers) ct =
boehmes@36898
    89
          (case try prover ct of
boehmes@40424
    90
            SOME thm => (SMT_Config.trace_msg ctxt I "Z3: succeeded"; thm)
boehmes@36898
    91
          | NONE => apply provers ct)
boehmes@36898
    92
boehmes@43893
    93
    fun schematic_label full = "schematic rules" |> full ? suffix " (full)"
boehmes@43893
    94
    fun schematic ctxt full ct =
boehmes@43893
    95
      ct
boehmes@43893
    96
      |> full ? fold_rev (curry Drule.mk_implies o Thm.cprop_of) thms
boehmes@43893
    97
      |> named ctxt (schematic_label full) (by_schematic_rule ctxt)
boehmes@43893
    98
      |> fold Thm.elim_implies thms
boehmes@43893
    99
boehmes@43893
   100
  in apply o cons (schematic ctxt false) o cons (schematic ctxt true) end
boehmes@36898
   101
boehmes@36899
   102
local
boehmes@36899
   103
  val rewr_if =
boehmes@36899
   104
    @{lemma "(if P then Q1 else Q2) = ((P --> Q1) & (~P --> Q2))" by simp}
boehmes@36899
   105
in
wenzelm@42793
   106
wenzelm@42793
   107
fun HOL_fast_tac ctxt =
wenzelm@42793
   108
  Classical.fast_tac (put_claset HOL_cs ctxt)
wenzelm@42793
   109
wenzelm@42793
   110
fun simp_fast_tac ctxt =
wenzelm@51717
   111
  Simplifier.simp_tac (put_simpset HOL_ss ctxt addsimps [rewr_if])
wenzelm@42793
   112
  THEN_ALL_NEW HOL_fast_tac ctxt
wenzelm@42793
   113
boehmes@36899
   114
end
boehmes@36899
   115
boehmes@36898
   116
boehmes@36898
   117
boehmes@41130
   118
(* theorems and proofs *)
boehmes@36898
   119
boehmes@41130
   120
(** theorem incarnations **)
boehmes@36898
   121
boehmes@36898
   122
datatype theorem =
boehmes@36898
   123
  Thm of thm | (* theorem without special features *)
boehmes@36898
   124
  MetaEq of thm | (* meta equality "t == s" *)
boehmes@41328
   125
  Literals of thm * Z3_Proof_Literals.littab
boehmes@36898
   126
    (* "P1 & ... & Pn" and table of all literals P1, ..., Pn *)
boehmes@36898
   127
boehmes@36898
   128
fun thm_of (Thm thm) = thm
boehmes@36898
   129
  | thm_of (MetaEq thm) = thm COMP @{thm meta_eq_to_obj_eq}
boehmes@36898
   130
  | thm_of (Literals (thm, _)) = thm
boehmes@36898
   131
boehmes@36898
   132
fun meta_eq_of (MetaEq thm) = thm
boehmes@36898
   133
  | meta_eq_of p = mk_meta_eq (thm_of p)
boehmes@36898
   134
boehmes@36898
   135
fun literals_of (Literals (_, lits)) = lits
boehmes@41328
   136
  | literals_of p = Z3_Proof_Literals.make_littab [thm_of p]
boehmes@36898
   137
boehmes@36898
   138
boehmes@36898
   139
boehmes@36898
   140
(** core proof rules **)
boehmes@36898
   141
boehmes@36898
   142
(* assumption *)
boehmes@41131
   143
boehmes@36898
   144
local
boehmes@41131
   145
  val remove_trigger = mk_meta_eq @{thm SMT.trigger_def}
boehmes@41131
   146
  val remove_weight = mk_meta_eq @{thm SMT.weight_def}
boehmes@41131
   147
  val remove_fun_app = mk_meta_eq @{thm SMT.fun_app_def}
boehmes@36898
   148
boehmes@44488
   149
  fun rewrite_conv _ [] = Conv.all_conv
wenzelm@51717
   150
    | rewrite_conv ctxt eqs = Simplifier.full_rewrite (empty_simpset ctxt addsimps eqs)
boehmes@36898
   151
boehmes@41131
   152
  val prep_rules = [@{thm Let_def}, remove_trigger, remove_weight,
boehmes@41328
   153
    remove_fun_app, Z3_Proof_Literals.rewrite_true]
boehmes@41131
   154
boehmes@44488
   155
  fun rewrite _ [] = I
boehmes@44488
   156
    | rewrite ctxt eqs = Conv.fconv_rule (rewrite_conv ctxt eqs)
boehmes@36898
   157
boehmes@41131
   158
  fun lookup_assm assms_net ct =
boehmes@45393
   159
    Z3_Proof_Tools.net_instances assms_net ct
boehmes@45393
   160
    |> map (fn ithm as (_, thm) => (ithm, Thm.cprop_of thm aconvc ct))
boehmes@36898
   161
in
boehmes@41131
   162
boehmes@41131
   163
fun add_asserted outer_ctxt rewrite_rules assms asserted ctxt =
boehmes@36898
   164
  let
boehmes@41328
   165
    val eqs = map (rewrite ctxt [Z3_Proof_Literals.rewrite_true]) rewrite_rules
boehmes@41131
   166
    val eqs' = union Thm.eq_thm eqs prep_rules
boehmes@41131
   167
boehmes@41131
   168
    val assms_net =
boehmes@41127
   169
      assms
boehmes@41131
   170
      |> map (apsnd (rewrite ctxt eqs'))
boehmes@41127
   171
      |> map (apsnd (Conv.fconv_rule Thm.eta_conversion))
boehmes@41328
   172
      |> Z3_Proof_Tools.thm_net_of snd 
boehmes@41131
   173
boehmes@41131
   174
    fun revert_conv ctxt = rewrite_conv ctxt eqs' then_conv Thm.eta_conversion
boehmes@41131
   175
boehmes@41131
   176
    fun assume thm ctxt =
boehmes@41131
   177
      let
boehmes@41131
   178
        val ct = Thm.cprem_of thm 1
boehmes@41131
   179
        val (thm', ctxt') = yield_singleton Assumption.add_assumes ct ctxt
boehmes@41131
   180
      in (Thm.implies_elim thm thm', ctxt') end
boehmes@36898
   181
boehmes@45393
   182
    fun add1 idx thm1 ((i, th), exact) ((is, thms), (ctxt, ptab)) =
boehmes@45393
   183
      let
boehmes@45393
   184
        val (thm, ctxt') =
boehmes@45393
   185
          if exact then (Thm.implies_elim thm1 th, ctxt)
boehmes@45393
   186
          else assume thm1 ctxt
boehmes@45393
   187
        val thms' = if exact then thms else th :: thms
boehmes@45393
   188
      in 
boehmes@45393
   189
        ((insert (op =) i is, thms'),
boehmes@45393
   190
          (ctxt', Inttab.update (idx, Thm thm) ptab))
boehmes@45393
   191
      end
boehmes@45393
   192
boehmes@45393
   193
    fun add (idx, ct) (cx as ((is, thms), (ctxt, ptab))) =
boehmes@41131
   194
      let
boehmes@41131
   195
        val thm1 = 
boehmes@41131
   196
          Thm.trivial ct
boehmes@41131
   197
          |> Conv.fconv_rule (Conv.arg1_conv (revert_conv outer_ctxt))
boehmes@41131
   198
        val thm2 = singleton (Variable.export ctxt outer_ctxt) thm1
boehmes@41131
   199
      in
boehmes@41131
   200
        (case lookup_assm assms_net (Thm.cprem_of thm2 1) of
boehmes@45393
   201
          [] =>
boehmes@41131
   202
            let val (thm, ctxt') = assume thm1 ctxt
boehmes@41131
   203
            in ((is, thms), (ctxt', Inttab.update (idx, Thm thm) ptab)) end
boehmes@45393
   204
        | ithms => fold (add1 idx thm1) ithms cx)
boehmes@41131
   205
      end
boehmes@41131
   206
  in fold add asserted (([], []), (ctxt, Inttab.empty)) end
boehmes@40164
   207
boehmes@36898
   208
end
boehmes@36898
   209
boehmes@36898
   210
boehmes@36898
   211
(* P = Q ==> P ==> Q   or   P --> Q ==> P ==> Q *)
boehmes@36898
   212
local
boehmes@41328
   213
  val precomp = Z3_Proof_Tools.precompose2
boehmes@41328
   214
  val comp = Z3_Proof_Tools.compose
boehmes@36898
   215
boehmes@41328
   216
  val meta_iffD1 = @{lemma "P == Q ==> P ==> (Q::bool)" by simp}
boehmes@41328
   217
  val meta_iffD1_c = precomp Thm.dest_binop meta_iffD1
boehmes@41328
   218
boehmes@41328
   219
  val iffD1_c = precomp (Thm.dest_binop o Thm.dest_arg) @{thm iffD1}
boehmes@41328
   220
  val mp_c = precomp (Thm.dest_binop o Thm.dest_arg) @{thm mp}
boehmes@36898
   221
in
boehmes@41328
   222
fun mp (MetaEq thm) p = Thm (Thm.implies_elim (comp meta_iffD1_c thm) p)
boehmes@36898
   223
  | mp p_q p = 
boehmes@36898
   224
      let
boehmes@36898
   225
        val pq = thm_of p_q
boehmes@41328
   226
        val thm = comp iffD1_c pq handle THM _ => comp mp_c pq
boehmes@36898
   227
      in Thm (Thm.implies_elim thm p) end
boehmes@36898
   228
end
boehmes@36898
   229
boehmes@36898
   230
boehmes@36898
   231
(* and_elim:     P1 & ... & Pn ==> Pi *)
boehmes@36898
   232
(* not_or_elim:  ~(P1 | ... | Pn) ==> ~Pi *)
boehmes@36898
   233
local
boehmes@41328
   234
  fun is_sublit conj t = Z3_Proof_Literals.exists_lit conj (fn u => u aconv t)
boehmes@36898
   235
boehmes@36898
   236
  fun derive conj t lits idx ptab =
boehmes@36898
   237
    let
boehmes@41328
   238
      val lit = the (Z3_Proof_Literals.get_first_lit (is_sublit conj t) lits)
boehmes@41328
   239
      val ls = Z3_Proof_Literals.explode conj false false [t] lit
boehmes@41328
   240
      val lits' = fold Z3_Proof_Literals.insert_lit ls
boehmes@41328
   241
        (Z3_Proof_Literals.delete_lit lit lits)
boehmes@36898
   242
boehmes@41130
   243
      fun upd thm = Literals (thm_of thm, lits')
boehmes@41328
   244
      val ptab' = Inttab.map_entry idx upd ptab
boehmes@41328
   245
    in (the (Z3_Proof_Literals.lookup_lit lits' t), ptab') end
boehmes@36898
   246
boehmes@36898
   247
  fun lit_elim conj (p, idx) ct ptab =
boehmes@36898
   248
    let val lits = literals_of p
boehmes@36898
   249
    in
boehmes@41328
   250
      (case Z3_Proof_Literals.lookup_lit lits (SMT_Utils.term_of ct) of
boehmes@36898
   251
        SOME lit => (Thm lit, ptab)
boehmes@41328
   252
      | NONE => apfst Thm (derive conj (SMT_Utils.term_of ct) lits idx ptab))
boehmes@36898
   253
    end
boehmes@36898
   254
in
boehmes@36898
   255
val and_elim = lit_elim true
boehmes@36898
   256
val not_or_elim = lit_elim false
boehmes@36898
   257
end
boehmes@36898
   258
boehmes@36898
   259
boehmes@36898
   260
(* P1, ..., Pn |- False ==> |- ~P1 | ... | ~Pn *)
boehmes@36898
   261
local
boehmes@36898
   262
  fun step lit thm =
boehmes@36898
   263
    Thm.implies_elim (Thm.implies_intr (Thm.cprop_of lit) thm) lit
boehmes@41328
   264
  val explode_disj = Z3_Proof_Literals.explode false false false
boehmes@36898
   265
  fun intro hyps thm th = fold step (explode_disj hyps th) thm
boehmes@36898
   266
boehmes@36898
   267
  fun dest_ccontr ct = [Thm.dest_arg (Thm.dest_arg (Thm.dest_arg1 ct))]
boehmes@41328
   268
  val ccontr = Z3_Proof_Tools.precompose dest_ccontr @{thm ccontr}
boehmes@36898
   269
in
boehmes@36898
   270
fun lemma thm ct =
boehmes@36898
   271
  let
boehmes@41328
   272
    val cu = Z3_Proof_Literals.negate (Thm.dest_arg ct)
wenzelm@44058
   273
    val hyps = map_filter (try HOLogic.dest_Trueprop) (Thm.hyps_of thm)
boehmes@41328
   274
    val th = Z3_Proof_Tools.under_assumption (intro hyps thm) cu
boehmes@41328
   275
  in Thm (Z3_Proof_Tools.compose ccontr th) end
boehmes@36898
   276
end
boehmes@36898
   277
boehmes@36898
   278
boehmes@36898
   279
(* \/{P1, ..., Pn, Q1, ..., Qn}, ~P1, ..., ~Pn ==> \/{Q1, ..., Qn} *)
boehmes@36898
   280
local
boehmes@41328
   281
  val explode_disj = Z3_Proof_Literals.explode false true false
boehmes@41328
   282
  val join_disj = Z3_Proof_Literals.join false
boehmes@36898
   283
  fun unit thm thms th =
boehmes@41328
   284
    let
boehmes@41328
   285
      val t = @{const Not} $ SMT_Utils.prop_of thm
boehmes@41328
   286
      val ts = map SMT_Utils.prop_of thms
boehmes@41328
   287
    in
boehmes@41328
   288
      join_disj (Z3_Proof_Literals.make_littab (thms @ explode_disj ts th)) t
boehmes@41328
   289
    end
boehmes@36898
   290
boehmes@36898
   291
  fun dest_arg2 ct = Thm.dest_arg (Thm.dest_arg ct)
boehmes@36898
   292
  fun dest ct = pairself dest_arg2 (Thm.dest_binop ct)
boehmes@41328
   293
  val contrapos =
boehmes@41328
   294
    Z3_Proof_Tools.precompose2 dest @{lemma "(~P ==> ~Q) ==> Q ==> P" by fast}
boehmes@36898
   295
in
boehmes@36898
   296
fun unit_resolution thm thms ct =
boehmes@41328
   297
  Z3_Proof_Literals.negate (Thm.dest_arg ct)
boehmes@41328
   298
  |> Z3_Proof_Tools.under_assumption (unit thm thms)
boehmes@41328
   299
  |> Thm o Z3_Proof_Tools.discharge thm o Z3_Proof_Tools.compose contrapos
boehmes@36898
   300
end
boehmes@36898
   301
boehmes@36898
   302
boehmes@36898
   303
(* P ==> P == True   or   P ==> P == False *)
boehmes@36898
   304
local
boehmes@36898
   305
  val iff1 = @{lemma "P ==> P == (~ False)" by simp}
boehmes@36898
   306
  val iff2 = @{lemma "~P ==> P == False" by simp}
boehmes@36898
   307
in
boehmes@36898
   308
fun iff_true thm = MetaEq (thm COMP iff1)
boehmes@36898
   309
fun iff_false thm = MetaEq (thm COMP iff2)
boehmes@36898
   310
end
boehmes@36898
   311
boehmes@36898
   312
boehmes@36898
   313
(* distributivity of | over & *)
boehmes@36898
   314
fun distributivity ctxt = Thm o try_apply ctxt [] [
wenzelm@42793
   315
  named ctxt "fast" (Z3_Proof_Tools.by_tac (HOL_fast_tac ctxt))]
boehmes@36898
   316
    (* FIXME: not very well tested *)
boehmes@36898
   317
boehmes@36898
   318
boehmes@36898
   319
(* Tseitin-like axioms *)
boehmes@36898
   320
local
boehmes@36898
   321
  val disjI1 = @{lemma "(P ==> Q) ==> ~P | Q" by fast}
boehmes@36898
   322
  val disjI2 = @{lemma "(~P ==> Q) ==> P | Q" by fast}
boehmes@36898
   323
  val disjI3 = @{lemma "(~Q ==> P) ==> P | Q" by fast}
boehmes@36898
   324
  val disjI4 = @{lemma "(Q ==> P) ==> P | ~Q" by fast}
boehmes@36898
   325
boehmes@36898
   326
  fun prove' conj1 conj2 ct2 thm =
boehmes@41328
   327
    let
boehmes@41328
   328
      val littab =
boehmes@41328
   329
        Z3_Proof_Literals.explode conj1 true (conj1 <> conj2) [] thm
boehmes@41328
   330
        |> cons Z3_Proof_Literals.true_thm
boehmes@41328
   331
        |> Z3_Proof_Literals.make_littab
boehmes@41328
   332
    in Z3_Proof_Literals.join conj2 littab (Thm.term_of ct2) end
boehmes@36898
   333
boehmes@36898
   334
  fun prove rule (ct1, conj1) (ct2, conj2) =
boehmes@41328
   335
    Z3_Proof_Tools.under_assumption (prove' conj1 conj2 ct2) ct1 COMP rule
boehmes@36898
   336
boehmes@36898
   337
  fun prove_def_axiom ct =
boehmes@36898
   338
    let val (ct1, ct2) = Thm.dest_binop (Thm.dest_arg ct)
boehmes@36898
   339
    in
boehmes@36898
   340
      (case Thm.term_of ct1 of
boehmes@40579
   341
        @{const Not} $ (@{const HOL.conj} $ _ $ _) =>
boehmes@36898
   342
          prove disjI1 (Thm.dest_arg ct1, true) (ct2, true)
boehmes@40579
   343
      | @{const HOL.conj} $ _ $ _ =>
boehmes@41328
   344
          prove disjI3 (Z3_Proof_Literals.negate ct2, false) (ct1, true)
boehmes@40579
   345
      | @{const Not} $ (@{const HOL.disj} $ _ $ _) =>
boehmes@41328
   346
          prove disjI3 (Z3_Proof_Literals.negate ct2, false) (ct1, false)
boehmes@40579
   347
      | @{const HOL.disj} $ _ $ _ =>
boehmes@41328
   348
          prove disjI2 (Z3_Proof_Literals.negate ct1, false) (ct2, true)
boehmes@40681
   349
      | Const (@{const_name distinct}, _) $ _ =>
boehmes@36898
   350
          let
boehmes@36898
   351
            fun dis_conv cv = Conv.arg_conv (Conv.arg1_conv cv)
boehmes@41328
   352
            val unfold_dis_conv = dis_conv Z3_Proof_Tools.unfold_distinct_conv
boehmes@36898
   353
            fun prv cu =
boehmes@36898
   354
              let val (cu1, cu2) = Thm.dest_binop (Thm.dest_arg cu)
boehmes@36898
   355
              in prove disjI4 (Thm.dest_arg cu2, true) (cu1, true) end
boehmes@41328
   356
          in Z3_Proof_Tools.with_conv unfold_dis_conv prv ct end
boehmes@40681
   357
      | @{const Not} $ (Const (@{const_name distinct}, _) $ _) =>
boehmes@36898
   358
          let
boehmes@36898
   359
            fun dis_conv cv = Conv.arg_conv (Conv.arg1_conv (Conv.arg_conv cv))
boehmes@41328
   360
            val unfold_dis_conv = dis_conv Z3_Proof_Tools.unfold_distinct_conv
boehmes@36898
   361
            fun prv cu =
boehmes@36898
   362
              let val (cu1, cu2) = Thm.dest_binop (Thm.dest_arg cu)
boehmes@36898
   363
              in prove disjI1 (Thm.dest_arg cu1, true) (cu2, true) end
boehmes@41328
   364
          in Z3_Proof_Tools.with_conv unfold_dis_conv prv ct end
boehmes@36898
   365
      | _ => raise CTERM ("prove_def_axiom", [ct]))
boehmes@36898
   366
    end
boehmes@36898
   367
in
boehmes@36898
   368
fun def_axiom ctxt = Thm o try_apply ctxt [] [
boehmes@36898
   369
  named ctxt "conj/disj/distinct" prove_def_axiom,
boehmes@42992
   370
  Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
wenzelm@42793
   371
    named ctxt' "simp+fast" (Z3_Proof_Tools.by_tac (simp_fast_tac ctxt')))]
boehmes@36898
   372
end
boehmes@36898
   373
boehmes@36898
   374
boehmes@36898
   375
(* local definitions *)
boehmes@36898
   376
local
boehmes@36898
   377
  val intro_rules = [
boehmes@36898
   378
    @{lemma "n == P ==> (~n | P) & (n | ~P)" by simp},
boehmes@36898
   379
    @{lemma "n == (if P then s else t) ==> (~P | n = s) & (P | n = t)"
boehmes@36898
   380
      by simp},
boehmes@36898
   381
    @{lemma "n == P ==> n = P" by (rule meta_eq_to_obj_eq)} ]
boehmes@36898
   382
boehmes@36898
   383
  val apply_rules = [
boehmes@36898
   384
    @{lemma "(~n | P) & (n | ~P) ==> P == n" by (atomize(full)) fast},
boehmes@36898
   385
    @{lemma "(~P | n = s) & (P | n = t) ==> (if P then s else t) == n"
nipkow@44890
   386
      by (atomize(full)) fastforce} ]
boehmes@36898
   387
boehmes@41328
   388
  val inst_rule = Z3_Proof_Tools.match_instantiate Thm.dest_arg
boehmes@36898
   389
boehmes@36898
   390
  fun apply_rule ct =
boehmes@36898
   391
    (case get_first (try (inst_rule ct)) intro_rules of
boehmes@36898
   392
      SOME thm => thm
boehmes@36898
   393
    | NONE => raise CTERM ("intro_def", [ct]))
boehmes@36898
   394
in
boehmes@41328
   395
fun intro_def ct = Z3_Proof_Tools.make_hyp_def (apply_rule ct) #>> Thm
boehmes@36898
   396
boehmes@36898
   397
fun apply_def thm =
boehmes@36898
   398
  get_first (try (fn rule => MetaEq (thm COMP rule))) apply_rules
boehmes@36898
   399
  |> the_default (Thm thm)
boehmes@36898
   400
end
boehmes@36898
   401
boehmes@36898
   402
boehmes@36898
   403
(* negation normal form *)
boehmes@36898
   404
local
boehmes@36898
   405
  val quant_rules1 = ([
boehmes@36898
   406
    @{lemma "(!!x. P x == Q) ==> ALL x. P x == Q" by simp},
boehmes@36898
   407
    @{lemma "(!!x. P x == Q) ==> EX x. P x == Q" by simp}], [
boehmes@36898
   408
    @{lemma "(!!x. P x == Q x) ==> ALL x. P x == ALL x. Q x" by simp},
boehmes@36898
   409
    @{lemma "(!!x. P x == Q x) ==> EX x. P x == EX x. Q x" by simp}])
boehmes@36898
   410
boehmes@36898
   411
  val quant_rules2 = ([
boehmes@36898
   412
    @{lemma "(!!x. ~P x == Q) ==> ~(ALL x. P x) == Q" by simp},
boehmes@36898
   413
    @{lemma "(!!x. ~P x == Q) ==> ~(EX x. P x) == Q" by simp}], [
boehmes@36898
   414
    @{lemma "(!!x. ~P x == Q x) ==> ~(ALL x. P x) == EX x. Q x" by simp},
boehmes@36898
   415
    @{lemma "(!!x. ~P x == Q x) ==> ~(EX x. P x) == ALL x. Q x" by simp}])
boehmes@36898
   416
boehmes@36898
   417
  fun nnf_quant_tac thm (qs as (qs1, qs2)) i st = (
wenzelm@52732
   418
    rtac thm ORELSE'
wenzelm@52732
   419
    (match_tac qs1 THEN' nnf_quant_tac thm qs) ORELSE'
wenzelm@52732
   420
    (match_tac qs2 THEN' nnf_quant_tac thm qs)) i st
boehmes@36898
   421
boehmes@41328
   422
  fun nnf_quant_tac_varified vars eq =
boehmes@41328
   423
    nnf_quant_tac (Z3_Proof_Tools.varify vars eq)
boehmes@41328
   424
boehmes@36898
   425
  fun nnf_quant vars qs p ct =
boehmes@41328
   426
    Z3_Proof_Tools.as_meta_eq ct
boehmes@41328
   427
    |> Z3_Proof_Tools.by_tac (nnf_quant_tac_varified vars (meta_eq_of p) qs)
boehmes@36898
   428
boehmes@36898
   429
  fun prove_nnf ctxt = try_apply ctxt [] [
boehmes@41328
   430
    named ctxt "conj/disj" Z3_Proof_Literals.prove_conj_disj_eq,
boehmes@42992
   431
    Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
wenzelm@42793
   432
      named ctxt' "simp+fast" (Z3_Proof_Tools.by_tac (simp_fast_tac ctxt')))]
boehmes@36898
   433
in
boehmes@36898
   434
fun nnf ctxt vars ps ct =
boehmes@41328
   435
  (case SMT_Utils.term_of ct of
boehmes@36898
   436
    _ $ (l as Const _ $ Abs _) $ (r as Const _ $ Abs _) =>
boehmes@36898
   437
      if l aconv r
boehmes@36898
   438
      then MetaEq (Thm.reflexive (Thm.dest_arg (Thm.dest_arg ct)))
boehmes@36898
   439
      else MetaEq (nnf_quant vars quant_rules1 (hd ps) ct)
boehmes@40579
   440
  | _ $ (@{const Not} $ (Const _ $ Abs _)) $ (Const _ $ Abs _) =>
boehmes@36898
   441
      MetaEq (nnf_quant vars quant_rules2 (hd ps) ct)
boehmes@36898
   442
  | _ =>
boehmes@36898
   443
      let
boehmes@36898
   444
        val nnf_rewr_conv = Conv.arg_conv (Conv.arg_conv
boehmes@41328
   445
          (Z3_Proof_Tools.unfold_eqs ctxt
boehmes@41328
   446
            (map (Thm.symmetric o meta_eq_of) ps)))
boehmes@41328
   447
      in Thm (Z3_Proof_Tools.with_conv nnf_rewr_conv (prove_nnf ctxt) ct) end)
boehmes@36898
   448
end
boehmes@36898
   449
boehmes@36898
   450
boehmes@36898
   451
boehmes@36898
   452
(** equality proof rules **)
boehmes@36898
   453
boehmes@36898
   454
(* |- t = t *)
boehmes@36898
   455
fun refl ct = MetaEq (Thm.reflexive (Thm.dest_arg (Thm.dest_arg ct)))
boehmes@36898
   456
boehmes@36898
   457
boehmes@36898
   458
(* s = t ==> t = s *)
boehmes@36898
   459
local
boehmes@36898
   460
  val symm_rule = @{lemma "s = t ==> t == s" by simp}
boehmes@36898
   461
in
boehmes@36898
   462
fun symm (MetaEq thm) = MetaEq (Thm.symmetric thm)
boehmes@36898
   463
  | symm p = MetaEq (thm_of p COMP symm_rule)
boehmes@36898
   464
end
boehmes@36898
   465
boehmes@36898
   466
boehmes@36898
   467
(* s = t ==> t = u ==> s = u *)
boehmes@36898
   468
local
boehmes@36898
   469
  val trans1 = @{lemma "s == t ==> t =  u ==> s == u" by simp}
boehmes@36898
   470
  val trans2 = @{lemma "s =  t ==> t == u ==> s == u" by simp}
boehmes@36898
   471
  val trans3 = @{lemma "s =  t ==> t =  u ==> s == u" by simp}
boehmes@36898
   472
in
boehmes@36898
   473
fun trans (MetaEq thm1) (MetaEq thm2) = MetaEq (Thm.transitive thm1 thm2)
boehmes@36898
   474
  | trans (MetaEq thm) q = MetaEq (thm_of q COMP (thm COMP trans1))
boehmes@36898
   475
  | trans p (MetaEq thm) = MetaEq (thm COMP (thm_of p COMP trans2))
boehmes@36898
   476
  | trans p q = MetaEq (thm_of q COMP (thm_of p COMP trans3))
boehmes@36898
   477
end
boehmes@36898
   478
boehmes@36898
   479
boehmes@36898
   480
(* t1 = s1 ==> ... ==> tn = sn ==> f t1 ... tn = f s1 .. sn
boehmes@36898
   481
   (reflexive antecendents are droppped) *)
boehmes@36898
   482
local
boehmes@36898
   483
  exception MONO
boehmes@36898
   484
boehmes@36898
   485
  fun prove_refl (ct, _) = Thm.reflexive ct
boehmes@36898
   486
  fun prove_comb f g cp =
boehmes@36898
   487
    let val ((ct1, ct2), (cu1, cu2)) = pairself Thm.dest_comb cp
boehmes@36898
   488
    in Thm.combination (f (ct1, cu1)) (g (ct2, cu2)) end
boehmes@36898
   489
  fun prove_arg f = prove_comb prove_refl f
boehmes@36898
   490
boehmes@36898
   491
  fun prove f cp = prove_comb (prove f) f cp handle CTERM _ => prove_refl cp
boehmes@36898
   492
boehmes@36898
   493
  fun prove_nary is_comb f =
boehmes@36898
   494
    let
boehmes@36898
   495
      fun prove (cp as (ct, _)) = f cp handle MONO =>
boehmes@36898
   496
        if is_comb (Thm.term_of ct)
boehmes@36898
   497
        then prove_comb (prove_arg prove) prove cp
boehmes@36898
   498
        else prove_refl cp
boehmes@36898
   499
    in prove end
boehmes@36898
   500
boehmes@36898
   501
  fun prove_list f n cp =
boehmes@36898
   502
    if n = 0 then prove_refl cp
boehmes@36898
   503
    else prove_comb (prove_arg f) (prove_list f (n-1)) cp
boehmes@36898
   504
boehmes@36898
   505
  fun with_length f (cp as (cl, _)) =
boehmes@36898
   506
    f (length (HOLogic.dest_list (Thm.term_of cl))) cp
boehmes@36898
   507
boehmes@36898
   508
  fun prove_distinct f = prove_arg (with_length (prove_list f))
boehmes@36898
   509
boehmes@36898
   510
  fun prove_eq exn lookup cp =
boehmes@36898
   511
    (case lookup (Logic.mk_equals (pairself Thm.term_of cp)) of
boehmes@36898
   512
      SOME eq => eq
boehmes@36898
   513
    | NONE => if exn then raise MONO else prove_refl cp)
boehmes@36898
   514
  
boehmes@41328
   515
  val prove_exn = prove_eq true
boehmes@41328
   516
  and prove_safe = prove_eq false
boehmes@36898
   517
boehmes@36898
   518
  fun mono f (cp as (cl, _)) =
boehmes@36898
   519
    (case Term.head_of (Thm.term_of cl) of
boehmes@41328
   520
      @{const HOL.conj} => prove_nary Z3_Proof_Literals.is_conj (prove_exn f)
boehmes@41328
   521
    | @{const HOL.disj} => prove_nary Z3_Proof_Literals.is_disj (prove_exn f)
boehmes@41328
   522
    | Const (@{const_name distinct}, _) => prove_distinct (prove_safe f)
boehmes@41328
   523
    | _ => prove (prove_safe f)) cp
boehmes@36898
   524
in
boehmes@36898
   525
fun monotonicity eqs ct =
boehmes@36898
   526
  let
boehmes@40680
   527
    fun and_symmetric (t, thm) = [(t, thm), (t, Thm.symmetric thm)]
boehmes@40680
   528
    val teqs = maps (and_symmetric o `Thm.prop_of o meta_eq_of) eqs
boehmes@40680
   529
    val lookup = AList.lookup (op aconv) teqs
boehmes@36898
   530
    val cp = Thm.dest_binop (Thm.dest_arg ct)
boehmes@41328
   531
  in MetaEq (prove_exn lookup cp handle MONO => mono lookup cp) end
boehmes@36898
   532
end
boehmes@36898
   533
boehmes@36898
   534
boehmes@36898
   535
(* |- f a b = f b a (where f is equality) *)
boehmes@36898
   536
local
boehmes@36898
   537
  val rule = @{lemma "a = b == b = a" by (atomize(full)) (rule eq_commute)}
boehmes@36898
   538
in
boehmes@41328
   539
fun commutativity ct =
boehmes@41328
   540
  MetaEq (Z3_Proof_Tools.match_instantiate I
boehmes@41328
   541
    (Z3_Proof_Tools.as_meta_eq ct) rule)
boehmes@36898
   542
end
boehmes@36898
   543
boehmes@36898
   544
boehmes@36898
   545
boehmes@36898
   546
(** quantifier proof rules **)
boehmes@36898
   547
boehmes@36898
   548
(* P ?x = Q ?x ==> (ALL x. P x) = (ALL x. Q x)
boehmes@36898
   549
   P ?x = Q ?x ==> (EX x. P x) = (EX x. Q x)    *)
boehmes@36898
   550
local
boehmes@36898
   551
  val rules = [
boehmes@36898
   552
    @{lemma "(!!x. P x == Q x) ==> (ALL x. P x) == (ALL x. Q x)" by simp},
boehmes@36898
   553
    @{lemma "(!!x. P x == Q x) ==> (EX x. P x) == (EX x. Q x)" by simp}]
boehmes@36898
   554
in
boehmes@36898
   555
fun quant_intro vars p ct =
boehmes@36898
   556
  let
boehmes@36898
   557
    val thm = meta_eq_of p
boehmes@41328
   558
    val rules' = Z3_Proof_Tools.varify vars thm :: rules
boehmes@41328
   559
    val cu = Z3_Proof_Tools.as_meta_eq ct
wenzelm@52732
   560
    val tac = REPEAT_ALL_NEW (match_tac rules')
boehmes@41328
   561
  in MetaEq (Z3_Proof_Tools.by_tac tac cu) end
boehmes@36898
   562
end
boehmes@36898
   563
boehmes@36898
   564
boehmes@36898
   565
(* |- ((ALL x. P x) | Q) = (ALL x. P x | Q) *)
boehmes@36898
   566
fun pull_quant ctxt = Thm o try_apply ctxt [] [
wenzelm@42793
   567
  named ctxt "fast" (Z3_Proof_Tools.by_tac (HOL_fast_tac ctxt))]
boehmes@36898
   568
    (* FIXME: not very well tested *)
boehmes@36898
   569
boehmes@36898
   570
boehmes@36898
   571
(* |- (ALL x. P x & Q x) = ((ALL x. P x) & (ALL x. Q x)) *)
boehmes@36898
   572
fun push_quant ctxt = Thm o try_apply ctxt [] [
wenzelm@42793
   573
  named ctxt "fast" (Z3_Proof_Tools.by_tac (HOL_fast_tac ctxt))]
boehmes@36898
   574
    (* FIXME: not very well tested *)
boehmes@36898
   575
boehmes@36898
   576
boehmes@36898
   577
(* |- (ALL x1 ... xn y1 ... yn. P x1 ... xn) = (ALL x1 ... xn. P x1 ... xn) *)
boehmes@36898
   578
local
boehmes@42318
   579
  val elim_all = @{lemma "P = Q ==> (ALL x. P) = Q" by fast}
boehmes@42318
   580
  val elim_ex = @{lemma "P = Q ==> (EX x. P) = Q" by fast}
boehmes@36898
   581
boehmes@42318
   582
  fun elim_unused_tac i st = (
wenzelm@52732
   583
    match_tac [@{thm refl}]
wenzelm@52732
   584
    ORELSE' (match_tac [elim_all, elim_ex] THEN' elim_unused_tac)
boehmes@42318
   585
    ORELSE' (
wenzelm@52732
   586
      match_tac [@{thm iff_allI}, @{thm iff_exI}]
boehmes@42318
   587
      THEN' elim_unused_tac)) i st
boehmes@36898
   588
in
boehmes@42318
   589
boehmes@42318
   590
val elim_unused_vars = Thm o Z3_Proof_Tools.by_tac elim_unused_tac
boehmes@42318
   591
boehmes@36898
   592
end
boehmes@36898
   593
boehmes@36898
   594
boehmes@36898
   595
(* |- (ALL x1 ... xn. ~(x1 = t1 & ... xn = tn) | P x1 ... xn) = P t1 ... tn *)
boehmes@36898
   596
fun dest_eq_res ctxt = Thm o try_apply ctxt [] [
wenzelm@42793
   597
  named ctxt "fast" (Z3_Proof_Tools.by_tac (HOL_fast_tac ctxt))]
boehmes@36898
   598
    (* FIXME: not very well tested *)
boehmes@36898
   599
boehmes@36898
   600
boehmes@36898
   601
(* |- ~(ALL x1...xn. P x1...xn) | P a1...an *)
boehmes@36898
   602
local
boehmes@36898
   603
  val rule = @{lemma "~ P x | Q ==> ~(ALL x. P x) | Q" by fast}
boehmes@36898
   604
in
boehmes@41328
   605
val quant_inst = Thm o Z3_Proof_Tools.by_tac (
wenzelm@52732
   606
  REPEAT_ALL_NEW (match_tac [rule])
wenzelm@52732
   607
  THEN' rtac @{thm excluded_middle})
boehmes@36898
   608
end
boehmes@36898
   609
boehmes@36898
   610
boehmes@42196
   611
(* |- (EX x. P x) = P c     |- ~(ALL x. P x) = ~ P c *)
boehmes@36898
   612
local
boehmes@42191
   613
  val forall =
boehmes@42191
   614
    SMT_Utils.mk_const_pat @{theory} @{const_name all}
boehmes@42191
   615
      (SMT_Utils.destT1 o SMT_Utils.destT1)
boehmes@42191
   616
  fun mk_forall cv ct =
wenzelm@46497
   617
    Thm.apply (SMT_Utils.instT' cv forall) (Thm.lambda cv ct)
boehmes@36898
   618
boehmes@42191
   619
  fun get_vars f mk pred ctxt t =
boehmes@42191
   620
    Term.fold_aterms f t []
boehmes@42191
   621
    |> map_filter (fn v =>
boehmes@42191
   622
         if pred v then SOME (SMT_Utils.certify ctxt (mk v)) else NONE)
boehmes@36898
   623
boehmes@42191
   624
  fun close vars f ct ctxt =
boehmes@42191
   625
    let
boehmes@42191
   626
      val frees_of = get_vars Term.add_frees Free (member (op =) vars o fst)
boehmes@42191
   627
      val vs = frees_of ctxt (Thm.term_of ct)
boehmes@42191
   628
      val (thm, ctxt') = f (fold_rev mk_forall vs ct) ctxt
boehmes@42191
   629
      val vars_of = get_vars Term.add_vars Var (K true) ctxt'
boehmes@42191
   630
    in (Thm.instantiate ([], vars_of (Thm.prop_of thm) ~~ vs) thm, ctxt') end
boehmes@36898
   631
boehmes@42191
   632
  val sk_rules = @{lemma
boehmes@44488
   633
    "c = (SOME x. P x) ==> (EX x. P x) = P c"
boehmes@44488
   634
    "c = (SOME x. ~P x) ==> (~(ALL x. P x)) = (~P c)"
boehmes@42191
   635
    by (metis someI_ex)+}
boehmes@36898
   636
in
boehmes@42191
   637
boehmes@42191
   638
fun skolemize vars =
boehmes@42191
   639
  apfst Thm oo close vars (yield_singleton Assumption.add_assumes)
boehmes@42191
   640
boehmes@42191
   641
fun discharge_sk_tac i st = (
wenzelm@52732
   642
  rtac @{thm trans} i
wenzelm@52732
   643
  THEN resolve_tac sk_rules i
wenzelm@52732
   644
  THEN (rtac @{thm refl} ORELSE' discharge_sk_tac) (i+1)
wenzelm@52732
   645
  THEN rtac @{thm refl} i) st
boehmes@42191
   646
boehmes@36898
   647
end
boehmes@36898
   648
boehmes@36898
   649
boehmes@42191
   650
boehmes@36898
   651
(** theory proof rules **)
boehmes@36898
   652
boehmes@36898
   653
(* theory lemmas: linear arithmetic, arrays *)
boehmes@36898
   654
fun th_lemma ctxt simpset thms = Thm o try_apply ctxt thms [
boehmes@42992
   655
  Z3_Proof_Tools.by_abstraction 0 (false, true) ctxt thms (fn ctxt' =>
boehmes@41328
   656
    Z3_Proof_Tools.by_tac (
boehmes@41328
   657
      NAMED ctxt' "arith" (Arith_Data.arith_tac ctxt')
boehmes@41328
   658
      ORELSE' NAMED ctxt' "simp+arith" (
wenzelm@51717
   659
        Simplifier.asm_full_simp_tac (put_simpset simpset ctxt')
boehmes@41328
   660
        THEN_ALL_NEW Arith_Data.arith_tac ctxt')))]
boehmes@36898
   661
boehmes@36898
   662
boehmes@36898
   663
(* rewriting: prove equalities:
boehmes@36898
   664
     * ACI of conjunction/disjunction
boehmes@36898
   665
     * contradiction, excluded middle
boehmes@36898
   666
     * logical rewriting rules (for negation, implication, equivalence,
boehmes@36898
   667
         distinct)
boehmes@36898
   668
     * normal forms for polynoms (integer/real arithmetic)
boehmes@36898
   669
     * quantifier elimination over linear arithmetic
boehmes@36898
   670
     * ... ? **)
boehmes@36898
   671
structure Z3_Simps = Named_Thms
boehmes@36898
   672
(
wenzelm@45294
   673
  val name = @{binding z3_simp}
boehmes@36898
   674
  val description = "simplification rules for Z3 proof reconstruction"
boehmes@36898
   675
)
boehmes@36898
   676
boehmes@36898
   677
local
boehmes@36898
   678
  fun spec_meta_eq_of thm =
boehmes@36898
   679
    (case try (fn th => th RS @{thm spec}) thm of
boehmes@36898
   680
      SOME thm' => spec_meta_eq_of thm'
boehmes@36898
   681
    | NONE => mk_meta_eq thm)
boehmes@36898
   682
boehmes@36898
   683
  fun prep (Thm thm) = spec_meta_eq_of thm
boehmes@36898
   684
    | prep (MetaEq thm) = thm
boehmes@36898
   685
    | prep (Literals (thm, _)) = spec_meta_eq_of thm
boehmes@36898
   686
boehmes@36898
   687
  fun unfold_conv ctxt ths =
boehmes@41328
   688
    Conv.arg_conv (Conv.binop_conv (Z3_Proof_Tools.unfold_eqs ctxt
boehmes@41328
   689
      (map prep ths)))
boehmes@36898
   690
boehmes@36898
   691
  fun with_conv _ [] prv = prv
boehmes@41328
   692
    | with_conv ctxt ths prv =
boehmes@41328
   693
        Z3_Proof_Tools.with_conv (unfold_conv ctxt ths) prv
boehmes@36898
   694
boehmes@36898
   695
  val unfold_conv =
boehmes@41328
   696
    Conv.arg_conv (Conv.binop_conv
boehmes@41328
   697
      (Conv.try_conv Z3_Proof_Tools.unfold_distinct_conv))
boehmes@41328
   698
  val prove_conj_disj_eq =
boehmes@41328
   699
    Z3_Proof_Tools.with_conv unfold_conv Z3_Proof_Literals.prove_conj_disj_eq
boehmes@40663
   700
boehmes@41899
   701
  fun declare_hyps ctxt thm =
boehmes@41899
   702
    (thm, snd (Assumption.add_assumes (#hyps (Thm.crep_thm thm)) ctxt))
boehmes@36898
   703
in
boehmes@36898
   704
boehmes@42992
   705
val abstraction_depth = 3
boehmes@42992
   706
  (*
boehmes@42992
   707
    This value was chosen large enough to potentially catch exceptions,
boehmes@42992
   708
    yet small enough to not cause too much harm.  The value might be
boehmes@42992
   709
    increased in the future, if reconstructing 'rewrite' fails on problems
boehmes@42992
   710
    that get too much abstracted to be reconstructable.
boehmes@42992
   711
  *)
boehmes@42992
   712
boehmes@40663
   713
fun rewrite simpset ths ct ctxt =
boehmes@41899
   714
  apfst Thm (declare_hyps ctxt (with_conv ctxt ths (try_apply ctxt [] [
boehmes@40663
   715
    named ctxt "conj/disj/distinct" prove_conj_disj_eq,
boehmes@45392
   716
    named ctxt "pull-ite" Z3_Proof_Methods.prove_ite,
boehmes@42992
   717
    Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
boehmes@41328
   718
      Z3_Proof_Tools.by_tac (
wenzelm@51717
   719
        NAMED ctxt' "simp (logic)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
wenzelm@42793
   720
        THEN_ALL_NEW NAMED ctxt' "fast (logic)" (fast_tac ctxt'))),
boehmes@42992
   721
    Z3_Proof_Tools.by_abstraction 0 (false, true) ctxt [] (fn ctxt' =>
boehmes@41328
   722
      Z3_Proof_Tools.by_tac (
wenzelm@52732
   723
        (rtac @{thm iff_allI} ORELSE' K all_tac)
wenzelm@51717
   724
        THEN' NAMED ctxt' "simp (theory)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
boehmes@41328
   725
        THEN_ALL_NEW (
wenzelm@42793
   726
          NAMED ctxt' "fast (theory)" (HOL_fast_tac ctxt')
boehmes@41328
   727
          ORELSE' NAMED ctxt' "arith (theory)" (Arith_Data.arith_tac ctxt')))),
boehmes@42992
   728
    Z3_Proof_Tools.by_abstraction 0 (true, true) ctxt [] (fn ctxt' =>
boehmes@41328
   729
      Z3_Proof_Tools.by_tac (
wenzelm@52732
   730
        (rtac @{thm iff_allI} ORELSE' K all_tac)
wenzelm@51717
   731
        THEN' NAMED ctxt' "simp (full)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
boehmes@41328
   732
        THEN_ALL_NEW (
wenzelm@42793
   733
          NAMED ctxt' "fast (full)" (HOL_fast_tac ctxt')
boehmes@41328
   734
          ORELSE' NAMED ctxt' "arith (full)" (Arith_Data.arith_tac ctxt')))),
boehmes@42992
   735
    named ctxt "injectivity" (Z3_Proof_Methods.prove_injectivity ctxt),
boehmes@42992
   736
    Z3_Proof_Tools.by_abstraction abstraction_depth (true, true) ctxt []
boehmes@42992
   737
      (fn ctxt' =>
boehmes@42992
   738
        Z3_Proof_Tools.by_tac (
wenzelm@52732
   739
          (rtac @{thm iff_allI} ORELSE' K all_tac)
wenzelm@51717
   740
          THEN' NAMED ctxt' "simp (deepen)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
boehmes@42992
   741
          THEN_ALL_NEW (
boehmes@42992
   742
            NAMED ctxt' "fast (deepen)" (HOL_fast_tac ctxt')
boehmes@42992
   743
            ORELSE' NAMED ctxt' "arith (deepen)" (Arith_Data.arith_tac
boehmes@42992
   744
              ctxt'))))]) ct))
boehmes@36898
   745
boehmes@36898
   746
end
boehmes@36898
   747
boehmes@36898
   748
boehmes@36898
   749
boehmes@41130
   750
(* proof reconstruction *)
boehmes@36898
   751
boehmes@41130
   752
(** tracing and checking **)
boehmes@36898
   753
boehmes@41130
   754
fun trace_before ctxt idx = SMT_Config.trace_msg ctxt (fn r =>
boehmes@41328
   755
  "Z3: #" ^ string_of_int idx ^ ": " ^ Z3_Proof_Parser.string_of_rule r)
boehmes@36898
   756
boehmes@41130
   757
fun check_after idx r ps ct (p, (ctxt, _)) =
boehmes@41130
   758
  if not (Config.get ctxt SMT_Config.trace) then ()
boehmes@41130
   759
  else
boehmes@36898
   760
    let val thm = thm_of p |> tap (Thm.join_proofs o single)
boehmes@36898
   761
    in
boehmes@36898
   762
      if (Thm.cprop_of thm) aconvc ct then ()
boehmes@41328
   763
      else
boehmes@41328
   764
        z3_exn (Pretty.string_of (Pretty.big_list
boehmes@41328
   765
          ("proof step failed: " ^ quote (Z3_Proof_Parser.string_of_rule r) ^
boehmes@41328
   766
            " (#" ^ string_of_int idx ^ ")")
boehmes@36898
   767
          (pretty_goal ctxt (map (thm_of o fst) ps) (Thm.prop_of thm) @
boehmes@41328
   768
            [Pretty.block [Pretty.str "expected: ",
boehmes@41328
   769
              Syntax.pretty_term ctxt (Thm.term_of ct)]])))
boehmes@36898
   770
    end
boehmes@36898
   771
boehmes@36898
   772
boehmes@41130
   773
(** overall reconstruction procedure **)
boehmes@36898
   774
boehmes@40164
   775
local
boehmes@40164
   776
  fun not_supported r = raise Fail ("Z3: proof rule not implemented: " ^
boehmes@41328
   777
    quote (Z3_Proof_Parser.string_of_rule r))
boehmes@36898
   778
boehmes@41131
   779
  fun prove_step simpset vars r ps ct (cxp as (cx, ptab)) =
boehmes@40164
   780
    (case (r, ps) of
boehmes@40164
   781
      (* core rules *)
boehmes@41328
   782
      (Z3_Proof_Parser.True_Axiom, _) => (Thm Z3_Proof_Literals.true_thm, cxp)
boehmes@41328
   783
    | (Z3_Proof_Parser.Asserted, _) => raise Fail "bad assertion"
boehmes@41328
   784
    | (Z3_Proof_Parser.Goal, _) => raise Fail "bad assertion"
boehmes@41328
   785
    | (Z3_Proof_Parser.Modus_Ponens, [(p, _), (q, _)]) =>
boehmes@41328
   786
        (mp q (thm_of p), cxp)
boehmes@41328
   787
    | (Z3_Proof_Parser.Modus_Ponens_Oeq, [(p, _), (q, _)]) =>
boehmes@41328
   788
        (mp q (thm_of p), cxp)
boehmes@41328
   789
    | (Z3_Proof_Parser.And_Elim, [(p, i)]) =>
boehmes@41328
   790
        and_elim (p, i) ct ptab ||> pair cx
boehmes@41328
   791
    | (Z3_Proof_Parser.Not_Or_Elim, [(p, i)]) =>
boehmes@41328
   792
        not_or_elim (p, i) ct ptab ||> pair cx
boehmes@41328
   793
    | (Z3_Proof_Parser.Hypothesis, _) => (Thm (Thm.assume ct), cxp)
boehmes@41328
   794
    | (Z3_Proof_Parser.Lemma, [(p, _)]) => (lemma (thm_of p) ct, cxp)
boehmes@41328
   795
    | (Z3_Proof_Parser.Unit_Resolution, (p, _) :: ps) =>
boehmes@40164
   796
        (unit_resolution (thm_of p) (map (thm_of o fst) ps) ct, cxp)
boehmes@41328
   797
    | (Z3_Proof_Parser.Iff_True, [(p, _)]) => (iff_true (thm_of p), cxp)
boehmes@41328
   798
    | (Z3_Proof_Parser.Iff_False, [(p, _)]) => (iff_false (thm_of p), cxp)
boehmes@41328
   799
    | (Z3_Proof_Parser.Distributivity, _) => (distributivity cx ct, cxp)
boehmes@41328
   800
    | (Z3_Proof_Parser.Def_Axiom, _) => (def_axiom cx ct, cxp)
boehmes@41328
   801
    | (Z3_Proof_Parser.Intro_Def, _) => intro_def ct cx ||> rpair ptab
boehmes@41328
   802
    | (Z3_Proof_Parser.Apply_Def, [(p, _)]) => (apply_def (thm_of p), cxp)
boehmes@41328
   803
    | (Z3_Proof_Parser.Iff_Oeq, [(p, _)]) => (p, cxp)
boehmes@41328
   804
    | (Z3_Proof_Parser.Nnf_Pos, _) => (nnf cx vars (map fst ps) ct, cxp)
boehmes@41328
   805
    | (Z3_Proof_Parser.Nnf_Neg, _) => (nnf cx vars (map fst ps) ct, cxp)
boehmes@36898
   806
boehmes@40164
   807
      (* equality rules *)
boehmes@41328
   808
    | (Z3_Proof_Parser.Reflexivity, _) => (refl ct, cxp)
boehmes@41328
   809
    | (Z3_Proof_Parser.Symmetry, [(p, _)]) => (symm p, cxp)
boehmes@41328
   810
    | (Z3_Proof_Parser.Transitivity, [(p, _), (q, _)]) => (trans p q, cxp)
boehmes@41328
   811
    | (Z3_Proof_Parser.Monotonicity, _) => (monotonicity (map fst ps) ct, cxp)
boehmes@41328
   812
    | (Z3_Proof_Parser.Commutativity, _) => (commutativity ct, cxp)
boehmes@40164
   813
boehmes@40164
   814
      (* quantifier rules *)
boehmes@41328
   815
    | (Z3_Proof_Parser.Quant_Intro, [(p, _)]) => (quant_intro vars p ct, cxp)
boehmes@41328
   816
    | (Z3_Proof_Parser.Pull_Quant, _) => (pull_quant cx ct, cxp)
boehmes@41328
   817
    | (Z3_Proof_Parser.Push_Quant, _) => (push_quant cx ct, cxp)
boehmes@42318
   818
    | (Z3_Proof_Parser.Elim_Unused_Vars, _) => (elim_unused_vars ct, cxp)
boehmes@41328
   819
    | (Z3_Proof_Parser.Dest_Eq_Res, _) => (dest_eq_res cx ct, cxp)
boehmes@41328
   820
    | (Z3_Proof_Parser.Quant_Inst, _) => (quant_inst ct, cxp)
boehmes@42191
   821
    | (Z3_Proof_Parser.Skolemize, _) => skolemize vars ct cx ||> rpair ptab
boehmes@40164
   822
boehmes@40164
   823
      (* theory rules *)
boehmes@41328
   824
    | (Z3_Proof_Parser.Th_Lemma _, _) =>  (* FIXME: use arguments *)
boehmes@40164
   825
        (th_lemma cx simpset (map (thm_of o fst) ps) ct, cxp)
boehmes@41328
   826
    | (Z3_Proof_Parser.Rewrite, _) => rewrite simpset [] ct cx ||> rpair ptab
boehmes@41328
   827
    | (Z3_Proof_Parser.Rewrite_Star, ps) =>
boehmes@41328
   828
        rewrite simpset (map fst ps) ct cx ||> rpair ptab
boehmes@36898
   829
boehmes@41328
   830
    | (Z3_Proof_Parser.Nnf_Star, _) => not_supported r
boehmes@41328
   831
    | (Z3_Proof_Parser.Cnf_Star, _) => not_supported r
boehmes@41328
   832
    | (Z3_Proof_Parser.Transitivity_Star, _) => not_supported r
boehmes@41328
   833
    | (Z3_Proof_Parser.Pull_Quant_Star, _) => not_supported r
boehmes@36898
   834
boehmes@41328
   835
    | _ => raise Fail ("Z3: proof rule " ^
boehmes@41328
   836
        quote (Z3_Proof_Parser.string_of_rule r) ^
boehmes@41328
   837
        " has an unexpected number of arguments."))
boehmes@36898
   838
boehmes@41130
   839
  fun lookup_proof ptab idx =
boehmes@41130
   840
    (case Inttab.lookup ptab idx of
boehmes@41130
   841
      SOME p => (p, idx)
boehmes@41130
   842
    | NONE => z3_exn ("unknown proof id: " ^ quote (string_of_int idx)))
boehmes@41130
   843
boehmes@41131
   844
  fun prove simpset vars (idx, step) (_, cxp as (ctxt, ptab)) =
boehmes@40164
   845
    let
boehmes@41328
   846
      val Z3_Proof_Parser.Proof_Step {rule=r, prems, prop, ...} = step
boehmes@41130
   847
      val ps = map (lookup_proof ptab) prems
boehmes@41130
   848
      val _ = trace_before ctxt idx r
boehmes@41130
   849
      val (thm, (ctxt', ptab')) =
boehmes@41130
   850
        cxp
boehmes@41131
   851
        |> prove_step simpset vars r ps prop
boehmes@41130
   852
        |> tap (check_after idx r ps prop)
boehmes@41130
   853
    in (thm, (ctxt', Inttab.update (idx, thm) ptab')) end
boehmes@36898
   854
boehmes@42191
   855
  fun make_discharge_rules rules = rules @ [@{thm allI}, @{thm refl},
boehmes@42191
   856
    @{thm reflexive}, Z3_Proof_Literals.true_thm]
boehmes@42191
   857
boehmes@45393
   858
  fun discharge_assms_tac rules =
wenzelm@52732
   859
    REPEAT (HEADGOAL (resolve_tac rules ORELSE' SOLVED' discharge_sk_tac))
boehmes@45393
   860
    
boehmes@42191
   861
  fun discharge_assms rules thm =
boehmes@42191
   862
    if Thm.nprems_of thm = 0 then Goal.norm_result thm
boehmes@41127
   863
    else
boehmes@45393
   864
      (case Seq.pull (discharge_assms_tac rules thm) of
boehmes@45393
   865
        SOME (thm', _) => Goal.norm_result thm'
boehmes@41127
   866
      | NONE => raise THM ("failed to discharge premise", 1, [thm]))
boehmes@41127
   867
boehmes@41131
   868
  fun discharge rules outer_ctxt (p, (inner_ctxt, _)) =
boehmes@41130
   869
    thm_of p
wenzelm@42361
   870
    |> singleton (Proof_Context.export inner_ctxt outer_ctxt)
boehmes@42191
   871
    |> discharge_assms (make_discharge_rules rules)
boehmes@40164
   872
in
boehmes@40164
   873
boehmes@41127
   874
fun reconstruct outer_ctxt recon output =
boehmes@40164
   875
  let
boehmes@41127
   876
    val {context=ctxt, typs, terms, rewrite_rules, assms} = recon
boehmes@41328
   877
    val (asserted, steps, vars, ctxt1) =
boehmes@41328
   878
      Z3_Proof_Parser.parse ctxt typs terms output
boehmes@41131
   879
boehmes@41328
   880
    val simpset = Z3_Proof_Tools.make_simpset ctxt1 (Z3_Simps.get ctxt1)
boehmes@41131
   881
boehmes@41131
   882
    val ((is, rules), cxp as (ctxt2, _)) =
boehmes@41131
   883
      add_asserted outer_ctxt rewrite_rules assms asserted ctxt1
boehmes@36898
   884
  in
boehmes@41131
   885
    if Config.get ctxt2 SMT_Config.filter_only_facts then (is, @{thm TrueI})
boehmes@41127
   886
    else
boehmes@41131
   887
      (Thm @{thm TrueI}, cxp)
boehmes@41131
   888
      |> fold (prove simpset vars) steps 
boehmes@42191
   889
      |> discharge rules outer_ctxt
boehmes@41127
   890
      |> pair []
boehmes@36898
   891
  end
boehmes@36898
   892
boehmes@40164
   893
end
boehmes@36898
   894
boehmes@40164
   895
val setup = z3_rules_setup #> Z3_Simps.setup
boehmes@36898
   896
boehmes@36898
   897
end