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