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