src/HOL/Tools/SMT/z3_proof_tools.ML
author haftmann
Thu, 26 Aug 2010 20:51:17 +0200
changeset 39019 e46e7a9cb622
parent 38963 6513ea67d95d
child 39028 848be46708dc
permissions -rw-r--r--
formerly unnamed infix impliciation now named HOL.implies
boehmes@36890
     1
(*  Title:      HOL/Tools/SMT/z3_proof_tools.ML
boehmes@36890
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36890
     3
boehmes@36890
     4
Helper functions required for Z3 proof reconstruction.
boehmes@36890
     5
*)
boehmes@36890
     6
boehmes@36890
     7
signature Z3_PROOF_TOOLS =
boehmes@36890
     8
sig
boehmes@36890
     9
  (* accessing and modifying terms *)
boehmes@36890
    10
  val term_of: cterm -> term
boehmes@36890
    11
  val prop_of: thm -> term
boehmes@36890
    12
  val mk_prop: cterm -> cterm
boehmes@36890
    13
  val as_meta_eq: cterm -> cterm
boehmes@36890
    14
boehmes@36890
    15
  (* theorem nets *)
boehmes@36890
    16
  val thm_net_of: thm list -> thm Net.net
boehmes@36890
    17
  val net_instance: thm Net.net -> cterm -> thm option
boehmes@36890
    18
boehmes@36890
    19
  (* proof combinators *)
boehmes@36890
    20
  val under_assumption: (thm -> thm) -> cterm -> thm
boehmes@36890
    21
  val with_conv: conv -> (cterm -> thm) -> cterm -> thm
boehmes@36890
    22
  val discharge: thm -> thm -> thm
boehmes@36890
    23
  val varify: string list -> thm -> thm
boehmes@36890
    24
  val unfold_eqs: Proof.context -> thm list -> conv
boehmes@36890
    25
  val match_instantiate: (cterm -> cterm) -> cterm -> thm -> thm
boehmes@36890
    26
  val by_tac: (int -> tactic) -> cterm -> thm
boehmes@36890
    27
  val make_hyp_def: thm -> Proof.context -> thm * Proof.context
boehmes@36891
    28
  val by_abstraction: bool * bool -> Proof.context -> thm list ->
boehmes@36891
    29
    (Proof.context -> cterm -> thm) -> cterm -> thm
boehmes@36890
    30
boehmes@36890
    31
  (* a faster COMP *)
boehmes@36890
    32
  type compose_data
boehmes@36890
    33
  val precompose: (cterm -> cterm list) -> thm -> compose_data
boehmes@36890
    34
  val precompose2: (cterm -> cterm * cterm) -> thm -> compose_data
boehmes@36890
    35
  val compose: compose_data -> thm -> thm
boehmes@36890
    36
boehmes@36890
    37
  (* unfolding of 'distinct' *)
boehmes@36890
    38
  val unfold_distinct_conv: conv
boehmes@36890
    39
boehmes@36890
    40
  (* simpset *)
boehmes@36891
    41
  val add_simproc: Simplifier.simproc -> Context.generic -> Context.generic
boehmes@36890
    42
  val make_simpset: Proof.context -> thm list -> simpset
boehmes@36890
    43
end
boehmes@36890
    44
boehmes@36890
    45
structure Z3_Proof_Tools: Z3_PROOF_TOOLS =
boehmes@36890
    46
struct
boehmes@36890
    47
boehmes@36891
    48
structure I = Z3_Interface
boehmes@36891
    49
boehmes@36890
    50
boehmes@36890
    51
boehmes@36890
    52
(* accessing terms *)
boehmes@36890
    53
boehmes@36890
    54
val dest_prop = (fn @{term Trueprop} $ t => t | t => t)
boehmes@36890
    55
boehmes@36890
    56
fun term_of ct = dest_prop (Thm.term_of ct)
boehmes@36890
    57
fun prop_of thm = dest_prop (Thm.prop_of thm)
boehmes@36890
    58
boehmes@36890
    59
val mk_prop = Thm.capply @{cterm Trueprop}
boehmes@36890
    60
boehmes@36891
    61
val eq = I.mk_inst_pair I.destT1 @{cpat "op =="}
boehmes@36891
    62
fun mk_meta_eq_cterm ct cu = Thm.mk_binop (I.instT' ct eq) ct cu
boehmes@36890
    63
boehmes@36890
    64
fun as_meta_eq ct = uncurry mk_meta_eq_cterm (Thm.dest_binop (Thm.dest_arg ct))
boehmes@36890
    65
boehmes@36890
    66
boehmes@36890
    67
boehmes@36890
    68
(* theorem nets *)
boehmes@36890
    69
boehmes@36890
    70
fun thm_net_of thms =
boehmes@36890
    71
  let fun insert thm = Net.insert_term (K false) (Thm.prop_of thm, thm)
boehmes@36890
    72
  in fold insert thms Net.empty end
boehmes@36890
    73
boehmes@36890
    74
fun maybe_instantiate ct thm =
boehmes@36890
    75
  try Thm.first_order_match (Thm.cprop_of thm, ct)
boehmes@36890
    76
  |> Option.map (fn inst => Thm.instantiate inst thm)
boehmes@36890
    77
boehmes@36890
    78
fun first_of thms ct = get_first (maybe_instantiate ct) thms
boehmes@36890
    79
fun net_instance net ct = first_of (Net.match_term net (Thm.term_of ct)) ct
boehmes@36890
    80
boehmes@36890
    81
boehmes@36890
    82
boehmes@36890
    83
(* proof combinators *)
boehmes@36890
    84
boehmes@36890
    85
fun under_assumption f ct =
boehmes@36890
    86
  let val ct' = mk_prop ct
boehmes@36890
    87
  in Thm.implies_intr ct' (f (Thm.assume ct')) end
boehmes@36890
    88
boehmes@36890
    89
fun with_conv conv prove ct =
boehmes@36890
    90
  let val eq = Thm.symmetric (conv ct)
boehmes@36890
    91
  in Thm.equal_elim eq (prove (Thm.lhs_of eq)) end
boehmes@36890
    92
boehmes@36890
    93
fun discharge p pq = Thm.implies_elim pq p
boehmes@36890
    94
boehmes@36890
    95
fun varify vars = Drule.generalize ([], vars)
boehmes@36890
    96
boehmes@36890
    97
fun unfold_eqs _ [] = Conv.all_conv
boehmes@36890
    98
  | unfold_eqs ctxt eqs =
wenzelm@36938
    99
      Conv.top_sweep_conv (K (Conv.rewrs_conv eqs)) ctxt
boehmes@36890
   100
boehmes@36890
   101
fun match_instantiate f ct thm =
boehmes@36890
   102
  Thm.instantiate (Thm.match (f (Thm.cprop_of thm), ct)) thm
boehmes@36890
   103
boehmes@36890
   104
fun by_tac tac ct = Goal.norm_result (Goal.prove_internal [] ct (K (tac 1)))
boehmes@36890
   105
boehmes@36890
   106
(* |- c x == t x ==> P (c x)  ~~>  c == t |- P (c x) *) 
boehmes@36890
   107
fun make_hyp_def thm ctxt =
boehmes@36890
   108
  let
boehmes@36890
   109
    val (lhs, rhs) = Thm.dest_binop (Thm.cprem_of thm 1)
boehmes@36890
   110
    val (cf, cvs) = Drule.strip_comb lhs
boehmes@36890
   111
    val eq = mk_meta_eq_cterm cf (fold_rev Thm.cabs cvs rhs)
boehmes@36890
   112
    fun apply cv th =
boehmes@36890
   113
      Thm.combination th (Thm.reflexive cv)
boehmes@36890
   114
      |> Conv.fconv_rule (Conv.arg_conv (Thm.beta_conversion false))
boehmes@36890
   115
  in
boehmes@36890
   116
    yield_singleton Assumption.add_assumes eq ctxt
boehmes@36890
   117
    |>> Thm.implies_elim thm o fold apply cvs
boehmes@36890
   118
  end
boehmes@36890
   119
boehmes@36890
   120
boehmes@36890
   121
boehmes@36890
   122
(* abstraction *)
boehmes@36890
   123
boehmes@36890
   124
local
boehmes@36890
   125
boehmes@36890
   126
fun typ_of ct = #T (Thm.rep_cterm ct)
boehmes@36890
   127
fun certify ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
boehmes@36890
   128
boehmes@36890
   129
fun abs_context ctxt = (ctxt, Termtab.empty, 1, false)
boehmes@36890
   130
boehmes@36890
   131
fun context_of (ctxt, _, _, _) = ctxt
boehmes@36890
   132
boehmes@36891
   133
fun replace (_, (cv, ct)) = Thm.forall_elim ct o Thm.forall_intr cv
boehmes@36890
   134
boehmes@36890
   135
fun abs_instantiate (_, tab, _, beta_norm) =
boehmes@36891
   136
  fold replace (Termtab.dest tab) #>
boehmes@36890
   137
  beta_norm ? Conv.fconv_rule (Thm.beta_conversion true)
boehmes@36890
   138
boehmes@36891
   139
fun lambda_abstract cvs t =
boehmes@36890
   140
  let
boehmes@36891
   141
    val frees = map Free (Term.add_frees t [])
boehmes@36891
   142
    val cvs' = filter (fn cv => member (op aconv) frees (Thm.term_of cv)) cvs
boehmes@36891
   143
    val vs = map (Term.dest_Free o Thm.term_of) cvs'
boehmes@36891
   144
  in (Term.list_abs_free (vs, t), cvs') end
boehmes@36890
   145
boehmes@36890
   146
fun fresh_abstraction cvs ct (cx as (ctxt, tab, idx, beta_norm)) =
boehmes@36891
   147
  let val (t, cvs') = lambda_abstract cvs (Thm.term_of ct)
boehmes@36890
   148
  in
boehmes@36890
   149
    (case Termtab.lookup tab t of
boehmes@36891
   150
      SOME (cv, _) => (Drule.list_comb (cv, cvs'), cx)
boehmes@36890
   151
    | NONE =>
boehmes@36890
   152
        let
boehmes@36890
   153
          val (n, ctxt') = yield_singleton Variable.variant_fixes "x" ctxt
boehmes@36891
   154
          val cv = certify ctxt' (Free (n, map typ_of cvs' ---> typ_of ct))
boehmes@36891
   155
          val cu = Drule.list_comb (cv, cvs')
boehmes@36890
   156
          val e = (t, (cv, fold_rev Thm.cabs cvs' ct))
boehmes@36890
   157
          val beta_norm' = beta_norm orelse not (null cvs')
boehmes@36891
   158
        in (cu, (ctxt', Termtab.update e tab, idx + 1, beta_norm')) end)
boehmes@36890
   159
  end
boehmes@36890
   160
boehmes@36890
   161
fun abs_comb f g cvs ct =
boehmes@36890
   162
  let val (cf, cu) = Thm.dest_comb ct
boehmes@36890
   163
  in f cvs cf ##>> g cvs cu #>> uncurry Thm.capply end
boehmes@36890
   164
boehmes@36891
   165
fun abs_arg f = abs_comb (K pair) f
boehmes@36891
   166
boehmes@36891
   167
fun abs_args f cvs ct =
boehmes@36891
   168
  (case Thm.term_of ct of
boehmes@36891
   169
    _ $ _ => abs_comb (abs_args f) f cvs ct
boehmes@36891
   170
  | _ => pair ct)
boehmes@36891
   171
boehmes@36890
   172
fun abs_list f g cvs ct =
boehmes@36890
   173
  (case Thm.term_of ct of
boehmes@36890
   174
    Const (@{const_name Nil}, _) => pair ct
boehmes@36890
   175
  | Const (@{const_name Cons}, _) $ _ $ _ =>
boehmes@36890
   176
      abs_comb (abs_arg f) (abs_list f g) cvs ct
boehmes@36890
   177
  | _ => g cvs ct)
boehmes@36890
   178
boehmes@36890
   179
fun abs_abs f cvs ct =
boehmes@36890
   180
  let val (cv, cu) = Thm.dest_abs NONE ct
boehmes@36890
   181
  in f (cv :: cvs) cu #>> Thm.cabs cv end
boehmes@36890
   182
boehmes@36890
   183
val is_atomic = (fn _ $ _ => false | Abs _ => false | _ => true)
boehmes@36890
   184
boehmes@36890
   185
fun abstract (ext_logic, with_theories) =
boehmes@36890
   186
  let
boehmes@36890
   187
    fun abstr1 cvs ct = abs_arg abstr cvs ct
boehmes@36890
   188
    and abstr2 cvs ct = abs_comb abstr1 abstr cvs ct
boehmes@36890
   189
    and abstr3 cvs ct = abs_comb abstr2 abstr cvs ct
boehmes@36890
   190
    and abstr_abs cvs ct = abs_arg (abs_abs abstr) cvs ct
boehmes@36890
   191
boehmes@36890
   192
    and abstr cvs ct =
boehmes@36890
   193
      (case Thm.term_of ct of
boehmes@36890
   194
        @{term Trueprop} $ _ => abstr1 cvs ct
boehmes@36890
   195
      | @{term "op ==>"} $ _ $ _ => abstr2 cvs ct
boehmes@36890
   196
      | @{term True} => pair ct
boehmes@36890
   197
      | @{term False} => pair ct
boehmes@36890
   198
      | @{term Not} $ _ => abstr1 cvs ct
boehmes@36890
   199
      | @{term "op &"} $ _ $ _ => abstr2 cvs ct
boehmes@36890
   200
      | @{term "op |"} $ _ $ _ => abstr2 cvs ct
haftmann@39019
   201
      | @{term HOL.implies} $ _ $ _ => abstr2 cvs ct
boehmes@36890
   202
      | Const (@{const_name "op ="}, _) $ _ $ _ => abstr2 cvs ct
boehmes@36890
   203
      | Const (@{const_name distinct}, _) $ _ =>
boehmes@36890
   204
          if ext_logic then abs_arg (abs_list abstr fresh_abstraction) cvs ct
boehmes@36890
   205
          else fresh_abstraction cvs ct
boehmes@36890
   206
      | Const (@{const_name If}, _) $ _ $ _ $ _ =>
boehmes@36890
   207
          if ext_logic then abstr3 cvs ct else fresh_abstraction cvs ct
boehmes@36890
   208
      | Const (@{const_name All}, _) $ _ =>
boehmes@36890
   209
          if ext_logic then abstr_abs cvs ct else fresh_abstraction cvs ct
boehmes@36890
   210
      | Const (@{const_name Ex}, _) $ _ =>
boehmes@36890
   211
          if ext_logic then abstr_abs cvs ct else fresh_abstraction cvs ct
boehmes@36891
   212
      | t => (fn cx =>
boehmes@36891
   213
          if is_atomic t orelse can HOLogic.dest_number t then (ct, cx)
boehmes@36891
   214
          else if with_theories andalso
boehmes@36891
   215
            I.is_builtin_theory_term (context_of cx) t
boehmes@36891
   216
          then abs_args abstr cvs ct cx
boehmes@36891
   217
          else fresh_abstraction cvs ct cx))
boehmes@36890
   218
  in abstr [] end
boehmes@36890
   219
boehmes@36890
   220
fun with_prems thms f ct =
boehmes@36890
   221
  fold_rev (Thm.mk_binop @{cterm "op ==>"} o Thm.cprop_of) thms ct
boehmes@36890
   222
  |> f
boehmes@36890
   223
  |> fold (fn prem => fn th => Thm.implies_elim th prem) thms
boehmes@36890
   224
boehmes@36890
   225
in
boehmes@36890
   226
boehmes@36891
   227
fun by_abstraction mode ctxt thms prove = with_prems thms (fn ct =>
boehmes@36891
   228
  let val (cu, cx) = abstract mode ct (abs_context ctxt)
boehmes@36890
   229
  in abs_instantiate cx (prove (context_of cx) cu) end)
boehmes@36890
   230
boehmes@36890
   231
end
boehmes@36890
   232
boehmes@36890
   233
boehmes@36890
   234
boehmes@36890
   235
(* a faster COMP *)
boehmes@36890
   236
boehmes@36890
   237
type compose_data = cterm list * (cterm -> cterm list) * thm
boehmes@36890
   238
boehmes@36890
   239
fun list2 (x, y) = [x, y]
boehmes@36890
   240
boehmes@36890
   241
fun precompose f rule = (f (Thm.cprem_of rule 1), f, rule)
boehmes@36890
   242
fun precompose2 f rule = precompose (list2 o f) rule
boehmes@36890
   243
boehmes@36890
   244
fun compose (cvs, f, rule) thm =
boehmes@36890
   245
  discharge thm (Thm.instantiate ([], cvs ~~ f (Thm.cprop_of thm)) rule)
boehmes@36890
   246
boehmes@36890
   247
boehmes@36890
   248
boehmes@36890
   249
(* unfolding of 'distinct' *)
boehmes@36890
   250
boehmes@36890
   251
local
boehmes@36890
   252
  val set1 = @{lemma "x ~: set [] == ~False" by simp}
boehmes@36890
   253
  val set2 = @{lemma "x ~: set [x] == False" by simp}
boehmes@36890
   254
  val set3 = @{lemma "x ~: set [y] == x ~= y" by simp}
boehmes@36890
   255
  val set4 = @{lemma "x ~: set (x # ys) == False" by simp}
boehmes@36890
   256
  val set5 = @{lemma "x ~: set (y # ys) == x ~= y & x ~: set ys" by simp}
boehmes@36890
   257
boehmes@36890
   258
  fun set_conv ct =
wenzelm@36938
   259
    (Conv.rewrs_conv [set1, set2, set3, set4] else_conv
boehmes@36890
   260
    (Conv.rewr_conv set5 then_conv Conv.arg_conv set_conv)) ct
boehmes@36890
   261
boehmes@36890
   262
  val dist1 = @{lemma "distinct [] == ~False" by simp}
boehmes@36890
   263
  val dist2 = @{lemma "distinct [x] == ~False" by simp}
boehmes@36890
   264
  val dist3 = @{lemma "distinct (x # xs) == x ~: set xs & distinct xs"
boehmes@36890
   265
    by simp}
boehmes@36890
   266
boehmes@36890
   267
  fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
boehmes@36890
   268
in
boehmes@36890
   269
fun unfold_distinct_conv ct =
wenzelm@36938
   270
  (Conv.rewrs_conv [dist1, dist2] else_conv
boehmes@36890
   271
  (Conv.rewr_conv dist3 then_conv binop_conv set_conv unfold_distinct_conv)) ct
boehmes@36890
   272
end
boehmes@36890
   273
boehmes@36890
   274
boehmes@36890
   275
boehmes@36890
   276
(* simpset *)
boehmes@36890
   277
boehmes@36890
   278
local
boehmes@36890
   279
  val antisym_le1 = mk_meta_eq @{thm order_class.antisym_conv}
boehmes@36890
   280
  val antisym_le2 = mk_meta_eq @{thm linorder_class.antisym_conv2}
boehmes@36890
   281
  val antisym_less1 = mk_meta_eq @{thm linorder_class.antisym_conv1}
boehmes@36890
   282
  val antisym_less2 = mk_meta_eq @{thm linorder_class.antisym_conv3}
boehmes@36890
   283
boehmes@36890
   284
  fun eq_prop t thm = HOLogic.mk_Trueprop t aconv Thm.prop_of thm
boehmes@36890
   285
  fun dest_binop ((c as Const _) $ t $ u) = (c, t, u)
boehmes@36890
   286
    | dest_binop t = raise TERM ("dest_binop", [t])
boehmes@36890
   287
boehmes@36890
   288
  fun prove_antisym_le ss t =
boehmes@36890
   289
    let
boehmes@36890
   290
      val (le, r, s) = dest_binop t
boehmes@36890
   291
      val less = Const (@{const_name less}, Term.fastype_of le)
boehmes@36890
   292
      val prems = Simplifier.prems_of_ss ss
boehmes@36890
   293
    in
boehmes@36890
   294
      (case find_first (eq_prop (le $ s $ r)) prems of
boehmes@36890
   295
        NONE =>
boehmes@36890
   296
          find_first (eq_prop (HOLogic.mk_not (less $ r $ s))) prems
boehmes@36890
   297
          |> Option.map (fn thm => thm RS antisym_less1)
boehmes@36890
   298
      | SOME thm => SOME (thm RS antisym_le1))
boehmes@36890
   299
    end
boehmes@36890
   300
    handle THM _ => NONE
boehmes@36890
   301
boehmes@36890
   302
  fun prove_antisym_less ss t =
boehmes@36890
   303
    let
boehmes@36890
   304
      val (less, r, s) = dest_binop (HOLogic.dest_not t)
boehmes@36890
   305
      val le = Const (@{const_name less_eq}, Term.fastype_of less)
boehmes@36890
   306
      val prems = prems_of_ss ss
boehmes@36890
   307
    in
boehmes@36890
   308
      (case find_first (eq_prop (le $ r $ s)) prems of
boehmes@36890
   309
        NONE =>
boehmes@36890
   310
          find_first (eq_prop (HOLogic.mk_not (less $ s $ r))) prems
boehmes@36890
   311
          |> Option.map (fn thm => thm RS antisym_less2)
boehmes@36890
   312
      | SOME thm => SOME (thm RS antisym_le2))
boehmes@36890
   313
  end
boehmes@36890
   314
  handle THM _ => NONE
boehmes@36891
   315
boehmes@36891
   316
  val basic_simpset = HOL_ss addsimps @{thms field_simps}
boehmes@36891
   317
    addsimps [@{thm times_divide_eq_right}, @{thm times_divide_eq_left}]
boehmes@36891
   318
    addsimps @{thms arith_special} addsimps @{thms less_bin_simps}
boehmes@36891
   319
    addsimps @{thms le_bin_simps} addsimps @{thms eq_bin_simps}
boehmes@36891
   320
    addsimps @{thms add_bin_simps} addsimps @{thms succ_bin_simps}
boehmes@36891
   321
    addsimps @{thms minus_bin_simps} addsimps @{thms pred_bin_simps}
boehmes@36891
   322
    addsimps @{thms mult_bin_simps} addsimps @{thms iszero_simps}
boehmes@36891
   323
    addsimps @{thms array_rules}
boehmes@37146
   324
    addsimps @{thms z3div_def} addsimps @{thms z3mod_def}
boehmes@36891
   325
    addsimprocs [
wenzelm@38963
   326
      Simplifier.simproc_global @{theory} "fast_int_arith" [
boehmes@36891
   327
        "(m::int) < n", "(m::int) <= n", "(m::int) = n"] (K Lin_Arith.simproc),
wenzelm@38963
   328
      Simplifier.simproc_global @{theory} "antisym_le" ["(x::'a::order) <= y"]
boehmes@36891
   329
        (K prove_antisym_le),
wenzelm@38963
   330
      Simplifier.simproc_global @{theory} "antisym_less" ["~ (x::'a::linorder) < y"]
boehmes@36891
   331
        (K prove_antisym_less)]
boehmes@36891
   332
boehmes@36891
   333
  structure Simpset = Generic_Data
boehmes@36891
   334
  (
boehmes@36891
   335
    type T = simpset
boehmes@36891
   336
    val empty = basic_simpset
boehmes@36891
   337
    val extend = I
boehmes@36891
   338
    val merge = Simplifier.merge_ss
boehmes@36891
   339
  )
boehmes@36890
   340
in
boehmes@36890
   341
boehmes@36891
   342
fun add_simproc simproc = Simpset.map (fn ss => ss addsimprocs [simproc])
boehmes@36891
   343
boehmes@36891
   344
fun make_simpset ctxt rules =
boehmes@36891
   345
  Simplifier.context ctxt (Simpset.get (Context.Proof ctxt)) addsimps rules
boehmes@36890
   346
boehmes@36890
   347
end
boehmes@36890
   348
boehmes@36890
   349
end