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