removed Old_SMT legacy module
authorblanchet
Thu Apr 20 16:21:28 2017 +0200 (2017-04-20)
changeset 65515f595b7532dc9
parent 65514 d10f0bbc7ea1
child 65516 03efd17e083b
removed Old_SMT legacy module
NEWS
src/HOL/Library/Old_SMT.thy
src/HOL/Library/Old_SMT/old_smt_builtin.ML
src/HOL/Library/Old_SMT/old_smt_config.ML
src/HOL/Library/Old_SMT/old_smt_datatypes.ML
src/HOL/Library/Old_SMT/old_smt_failure.ML
src/HOL/Library/Old_SMT/old_smt_normalize.ML
src/HOL/Library/Old_SMT/old_smt_real.ML
src/HOL/Library/Old_SMT/old_smt_setup_solvers.ML
src/HOL/Library/Old_SMT/old_smt_solver.ML
src/HOL/Library/Old_SMT/old_smt_translate.ML
src/HOL/Library/Old_SMT/old_smt_utils.ML
src/HOL/Library/Old_SMT/old_smt_word.ML
src/HOL/Library/Old_SMT/old_smtlib_interface.ML
src/HOL/Library/Old_SMT/old_z3_interface.ML
src/HOL/Library/Old_SMT/old_z3_model.ML
src/HOL/Library/Old_SMT/old_z3_proof_literals.ML
src/HOL/Library/Old_SMT/old_z3_proof_methods.ML
src/HOL/Library/Old_SMT/old_z3_proof_parser.ML
src/HOL/Library/Old_SMT/old_z3_proof_reconstruction.ML
src/HOL/Library/Old_SMT/old_z3_proof_tools.ML
src/HOL/ROOT
     1.1 --- a/NEWS	Thu Apr 20 10:45:52 2017 +0200
     1.2 +++ b/NEWS	Thu Apr 20 16:21:28 2017 +0200
     1.3 @@ -154,6 +154,8 @@
     1.4  * Session HOL-Algebra extended by additional lattice theory: the
     1.5  Knaster-Tarski fixed point theorem and Galois Connections.
     1.6  
     1.7 +* SMT module: The legacy module 'src/HOL/Library/Old_SMT.thy' has been removed.
     1.8 +
     1.9  * Session HOL-Analysis: more material involving arcs, paths, covering
    1.10  spaces, innessential maps, retracts. Major results include the Jordan
    1.11  Curve Theorem and the Great Picard Theorem.
     2.1 --- a/src/HOL/Library/Old_SMT.thy	Thu Apr 20 10:45:52 2017 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,431 +0,0 @@
     2.4 -(*  Title:      HOL/Library/Old_SMT.thy
     2.5 -    Author:     Sascha Boehme, TU Muenchen
     2.6 -*)
     2.7 -
     2.8 -section \<open>Old Version of Bindings to Satisfiability Modulo Theories (SMT) solvers\<close>
     2.9 -
    2.10 -theory Old_SMT
    2.11 -imports "../Real" "../Word/Word"
    2.12 -keywords "old_smt_status" :: diag
    2.13 -begin
    2.14 -
    2.15 -ML_file "Old_SMT/old_smt_utils.ML"
    2.16 -ML_file "Old_SMT/old_smt_failure.ML"
    2.17 -ML_file "Old_SMT/old_smt_config.ML"
    2.18 -
    2.19 -
    2.20 -subsection \<open>Triggers for quantifier instantiation\<close>
    2.21 -
    2.22 -text \<open>
    2.23 -Some SMT solvers support patterns as a quantifier instantiation
    2.24 -heuristics.  Patterns may either be positive terms (tagged by "pat")
    2.25 -triggering quantifier instantiations -- when the solver finds a
    2.26 -term matching a positive pattern, it instantiates the corresponding
    2.27 -quantifier accordingly -- or negative terms (tagged by "nopat")
    2.28 -inhibiting quantifier instantiations.  A list of patterns
    2.29 -of the same kind is called a multipattern, and all patterns in a
    2.30 -multipattern are considered conjunctively for quantifier instantiation.
    2.31 -A list of multipatterns is called a trigger, and their multipatterns
    2.32 -act disjunctively during quantifier instantiation.  Each multipattern
    2.33 -should mention at least all quantified variables of the preceding
    2.34 -quantifier block.
    2.35 -\<close>
    2.36 -
    2.37 -typedecl pattern
    2.38 -
    2.39 -consts
    2.40 -  pat :: "'a \<Rightarrow> pattern"
    2.41 -  nopat :: "'a \<Rightarrow> pattern"
    2.42 -
    2.43 -definition trigger :: "pattern list list \<Rightarrow> bool \<Rightarrow> bool" where "trigger _ P = P"
    2.44 -
    2.45 -
    2.46 -subsection \<open>Quantifier weights\<close>
    2.47 -
    2.48 -text \<open>
    2.49 -Weight annotations to quantifiers influence the priority of quantifier
    2.50 -instantiations.  They should be handled with care for solvers, which support
    2.51 -them, because incorrect choices of weights might render a problem unsolvable.
    2.52 -\<close>
    2.53 -
    2.54 -definition weight :: "int \<Rightarrow> bool \<Rightarrow> bool" where "weight _ P = P"
    2.55 -
    2.56 -text \<open>
    2.57 -Weights must be non-negative.  The value \<open>0\<close> is equivalent to providing
    2.58 -no weight at all.
    2.59 -
    2.60 -Weights should only be used at quantifiers and only inside triggers (if the
    2.61 -quantifier has triggers).  Valid usages of weights are as follows:
    2.62 -
    2.63 -\begin{itemize}
    2.64 -\item
    2.65 -@{term "\<forall>x. trigger [[pat (P x)]] (weight 2 (P x))"}
    2.66 -\item
    2.67 -@{term "\<forall>x. weight 3 (P x)"}
    2.68 -\end{itemize}
    2.69 -\<close>
    2.70 -
    2.71 -
    2.72 -subsection \<open>Higher-order encoding\<close>
    2.73 -
    2.74 -text \<open>
    2.75 -Application is made explicit for constants occurring with varying
    2.76 -numbers of arguments.  This is achieved by the introduction of the
    2.77 -following constant.
    2.78 -\<close>
    2.79 -
    2.80 -definition fun_app where "fun_app f = f"
    2.81 -
    2.82 -text \<open>
    2.83 -Some solvers support a theory of arrays which can be used to encode
    2.84 -higher-order functions.  The following set of lemmas specifies the
    2.85 -properties of such (extensional) arrays.
    2.86 -\<close>
    2.87 -
    2.88 -lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other
    2.89 -  fun_upd_upd fun_app_def
    2.90 -
    2.91 -
    2.92 -subsection \<open>First-order logic\<close>
    2.93 -
    2.94 -text \<open>
    2.95 -Some SMT solvers only accept problems in first-order logic, i.e.,
    2.96 -where formulas and terms are syntactically separated. When
    2.97 -translating higher-order into first-order problems, all
    2.98 -uninterpreted constants (those not built-in in the target solver)
    2.99 -are treated as function symbols in the first-order sense.  Their
   2.100 -occurrences as head symbols in atoms (i.e., as predicate symbols) are
   2.101 -turned into terms by logically equating such atoms with @{term True}.
   2.102 -For technical reasons, @{term True} and @{term False} occurring inside
   2.103 -terms are replaced by the following constants.
   2.104 -\<close>
   2.105 -
   2.106 -definition term_true where "term_true = True"
   2.107 -definition term_false where "term_false = False"
   2.108 -
   2.109 -
   2.110 -subsection \<open>Integer division and modulo for Z3\<close>
   2.111 -
   2.112 -definition z3div :: "int \<Rightarrow> int \<Rightarrow> int" where
   2.113 -  "z3div k l = (if 0 \<le> l then k div l else -(k div (-l)))"
   2.114 -
   2.115 -definition z3mod :: "int \<Rightarrow> int \<Rightarrow> int" where
   2.116 -  "z3mod k l = (if 0 \<le> l then k mod l else k mod (-l))"
   2.117 -
   2.118 -
   2.119 -subsection \<open>Setup\<close>
   2.120 -
   2.121 -ML_file "Old_SMT/old_smt_builtin.ML"
   2.122 -ML_file "Old_SMT/old_smt_datatypes.ML"
   2.123 -ML_file "Old_SMT/old_smt_normalize.ML"
   2.124 -ML_file "Old_SMT/old_smt_translate.ML"
   2.125 -ML_file "Old_SMT/old_smt_solver.ML"
   2.126 -ML_file "Old_SMT/old_smtlib_interface.ML"
   2.127 -ML_file "Old_SMT/old_z3_interface.ML"
   2.128 -ML_file "Old_SMT/old_z3_proof_parser.ML"
   2.129 -ML_file "Old_SMT/old_z3_proof_tools.ML"
   2.130 -ML_file "Old_SMT/old_z3_proof_literals.ML"
   2.131 -ML_file "Old_SMT/old_z3_proof_methods.ML"
   2.132 -named_theorems old_z3_simp "simplification rules for Z3 proof reconstruction"
   2.133 -ML_file "Old_SMT/old_z3_proof_reconstruction.ML"
   2.134 -ML_file "Old_SMT/old_z3_model.ML"
   2.135 -ML_file "Old_SMT/old_smt_setup_solvers.ML"
   2.136 -
   2.137 -setup \<open>
   2.138 -  Old_SMT_Config.setup #>
   2.139 -  Old_SMT_Normalize.setup #>
   2.140 -  Old_SMTLIB_Interface.setup #>
   2.141 -  Old_Z3_Interface.setup #>
   2.142 -  Old_SMT_Setup_Solvers.setup
   2.143 -\<close>
   2.144 -
   2.145 -method_setup old_smt = \<open>
   2.146 -  Scan.optional Attrib.thms [] >>
   2.147 -    (fn thms => fn ctxt =>
   2.148 -      (legacy_feature "Proof method \"old_smt\" will be discontinued soon -- use \"smt\" instead";
   2.149 -       METHOD (fn facts => HEADGOAL (Old_SMT_Solver.smt_tac ctxt (thms @ facts)))))
   2.150 -\<close> "apply an SMT solver to the current goal"
   2.151 -
   2.152 -
   2.153 -subsection \<open>Configuration\<close>
   2.154 -
   2.155 -text \<open>
   2.156 -The current configuration can be printed by the command
   2.157 -\<open>old_smt_status\<close>, which shows the values of most options.
   2.158 -\<close>
   2.159 -
   2.160 -
   2.161 -
   2.162 -subsection \<open>General configuration options\<close>
   2.163 -
   2.164 -text \<open>
   2.165 -The option \<open>old_smt_solver\<close> can be used to change the target SMT
   2.166 -solver.  The possible values can be obtained from the \<open>old_smt_status\<close>
   2.167 -command.
   2.168 -
   2.169 -Due to licensing restrictions, Yices and Z3 are not installed/enabled
   2.170 -by default.  Z3 is free for non-commercial applications and can be enabled
   2.171 -by setting the \<open>OLD_Z3_NON_COMMERCIAL\<close> environment variable to
   2.172 -\<open>yes\<close>.
   2.173 -\<close>
   2.174 -
   2.175 -declare [[ old_smt_solver = z3 ]]
   2.176 -
   2.177 -text \<open>
   2.178 -Since SMT solvers are potentially non-terminating, there is a timeout
   2.179 -(given in seconds) to restrict their runtime.  A value greater than
   2.180 -120 (seconds) is in most cases not advisable.
   2.181 -\<close>
   2.182 -
   2.183 -declare [[ old_smt_timeout = 20 ]]
   2.184 -
   2.185 -text \<open>
   2.186 -SMT solvers apply randomized heuristics.  In case a problem is not
   2.187 -solvable by an SMT solver, changing the following option might help.
   2.188 -\<close>
   2.189 -
   2.190 -declare [[ old_smt_random_seed = 1 ]]
   2.191 -
   2.192 -text \<open>
   2.193 -In general, the binding to SMT solvers runs as an oracle, i.e, the SMT
   2.194 -solvers are fully trusted without additional checks.  The following
   2.195 -option can cause the SMT solver to run in proof-producing mode, giving
   2.196 -a checkable certificate.  This is currently only implemented for Z3.
   2.197 -\<close>
   2.198 -
   2.199 -declare [[ old_smt_oracle = false ]]
   2.200 -
   2.201 -text \<open>
   2.202 -Each SMT solver provides several commandline options to tweak its
   2.203 -behaviour.  They can be passed to the solver by setting the following
   2.204 -options.
   2.205 -\<close>
   2.206 -
   2.207 -declare [[ old_cvc3_options = "" ]]
   2.208 -declare [[ old_yices_options = "" ]]
   2.209 -declare [[ old_z3_options = "" ]]
   2.210 -
   2.211 -text \<open>
   2.212 -Enable the following option to use built-in support for datatypes and
   2.213 -records.  Currently, this is only implemented for Z3 running in oracle
   2.214 -mode.
   2.215 -\<close>
   2.216 -
   2.217 -declare [[ old_smt_datatypes = false ]]
   2.218 -
   2.219 -text \<open>
   2.220 -The SMT method provides an inference mechanism to detect simple triggers
   2.221 -in quantified formulas, which might increase the number of problems
   2.222 -solvable by SMT solvers (note: triggers guide quantifier instantiations
   2.223 -in the SMT solver).  To turn it on, set the following option.
   2.224 -\<close>
   2.225 -
   2.226 -declare [[ old_smt_infer_triggers = false ]]
   2.227 -
   2.228 -text \<open>
   2.229 -The SMT method monomorphizes the given facts, that is, it tries to
   2.230 -instantiate all schematic type variables with fixed types occurring
   2.231 -in the problem.  This is a (possibly nonterminating) fixed-point
   2.232 -construction whose cycles are limited by the following option.
   2.233 -\<close>
   2.234 -
   2.235 -declare [[ monomorph_max_rounds = 5 ]]
   2.236 -
   2.237 -text \<open>
   2.238 -In addition, the number of generated monomorphic instances is limited
   2.239 -by the following option.
   2.240 -\<close>
   2.241 -
   2.242 -declare [[ monomorph_max_new_instances = 500 ]]
   2.243 -
   2.244 -
   2.245 -
   2.246 -subsection \<open>Certificates\<close>
   2.247 -
   2.248 -text \<open>
   2.249 -By setting the option \<open>old_smt_certificates\<close> to the name of a file,
   2.250 -all following applications of an SMT solver a cached in that file.
   2.251 -Any further application of the same SMT solver (using the very same
   2.252 -configuration) re-uses the cached certificate instead of invoking the
   2.253 -solver.  An empty string disables caching certificates.
   2.254 -
   2.255 -The filename should be given as an explicit path.  It is good
   2.256 -practice to use the name of the current theory (with ending
   2.257 -\<open>.certs\<close> instead of \<open>.thy\<close>) as the certificates file.
   2.258 -Certificate files should be used at most once in a certain theory context,
   2.259 -to avoid race conditions with other concurrent accesses.
   2.260 -\<close>
   2.261 -
   2.262 -declare [[ old_smt_certificates = "" ]]
   2.263 -
   2.264 -text \<open>
   2.265 -The option \<open>old_smt_read_only_certificates\<close> controls whether only
   2.266 -stored certificates are should be used or invocation of an SMT solver
   2.267 -is allowed.  When set to \<open>true\<close>, no SMT solver will ever be
   2.268 -invoked and only the existing certificates found in the configured
   2.269 -cache are used;  when set to \<open>false\<close> and there is no cached
   2.270 -certificate for some proposition, then the configured SMT solver is
   2.271 -invoked.
   2.272 -\<close>
   2.273 -
   2.274 -declare [[ old_smt_read_only_certificates = false ]]
   2.275 -
   2.276 -
   2.277 -
   2.278 -subsection \<open>Tracing\<close>
   2.279 -
   2.280 -text \<open>
   2.281 -The SMT method, when applied, traces important information.  To
   2.282 -make it entirely silent, set the following option to \<open>false\<close>.
   2.283 -\<close>
   2.284 -
   2.285 -declare [[ old_smt_verbose = true ]]
   2.286 -
   2.287 -text \<open>
   2.288 -For tracing the generated problem file given to the SMT solver as
   2.289 -well as the returned result of the solver, the option
   2.290 -\<open>old_smt_trace\<close> should be set to \<open>true\<close>.
   2.291 -\<close>
   2.292 -
   2.293 -declare [[ old_smt_trace = false ]]
   2.294 -
   2.295 -text \<open>
   2.296 -From the set of assumptions given to the SMT solver, those assumptions
   2.297 -used in the proof are traced when the following option is set to
   2.298 -@{term true}.  This only works for Z3 when it runs in non-oracle mode
   2.299 -(see options \<open>old_smt_solver\<close> and \<open>old_smt_oracle\<close> above).
   2.300 -\<close>
   2.301 -
   2.302 -declare [[ old_smt_trace_used_facts = false ]]
   2.303 -
   2.304 -
   2.305 -
   2.306 -subsection \<open>Schematic rules for Z3 proof reconstruction\<close>
   2.307 -
   2.308 -text \<open>
   2.309 -Several prof rules of Z3 are not very well documented.  There are two
   2.310 -lemma groups which can turn failing Z3 proof reconstruction attempts
   2.311 -into succeeding ones: the facts in \<open>z3_rule\<close> are tried prior to
   2.312 -any implemented reconstruction procedure for all uncertain Z3 proof
   2.313 -rules;  the facts in \<open>z3_simp\<close> are only fed to invocations of
   2.314 -the simplifier when reconstructing theory-specific proof steps.
   2.315 -\<close>
   2.316 -
   2.317 -lemmas [old_z3_rule] =
   2.318 -  refl eq_commute conj_commute disj_commute simp_thms nnf_simps
   2.319 -  ring_distribs field_simps times_divide_eq_right times_divide_eq_left
   2.320 -  if_True if_False not_not
   2.321 -
   2.322 -lemma [old_z3_rule]:
   2.323 -  "(P \<and> Q) = (\<not>(\<not>P \<or> \<not>Q))"
   2.324 -  "(P \<and> Q) = (\<not>(\<not>Q \<or> \<not>P))"
   2.325 -  "(\<not>P \<and> Q) = (\<not>(P \<or> \<not>Q))"
   2.326 -  "(\<not>P \<and> Q) = (\<not>(\<not>Q \<or> P))"
   2.327 -  "(P \<and> \<not>Q) = (\<not>(\<not>P \<or> Q))"
   2.328 -  "(P \<and> \<not>Q) = (\<not>(Q \<or> \<not>P))"
   2.329 -  "(\<not>P \<and> \<not>Q) = (\<not>(P \<or> Q))"
   2.330 -  "(\<not>P \<and> \<not>Q) = (\<not>(Q \<or> P))"
   2.331 -  by auto
   2.332 -
   2.333 -lemma [old_z3_rule]:
   2.334 -  "(P \<longrightarrow> Q) = (Q \<or> \<not>P)"
   2.335 -  "(\<not>P \<longrightarrow> Q) = (P \<or> Q)"
   2.336 -  "(\<not>P \<longrightarrow> Q) = (Q \<or> P)"
   2.337 -  "(True \<longrightarrow> P) = P"
   2.338 -  "(P \<longrightarrow> True) = True"
   2.339 -  "(False \<longrightarrow> P) = True"
   2.340 -  "(P \<longrightarrow> P) = True"
   2.341 -  by auto
   2.342 -
   2.343 -lemma [old_z3_rule]:
   2.344 -  "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not>P)))"
   2.345 -  by auto
   2.346 -
   2.347 -lemma [old_z3_rule]:
   2.348 -  "(\<not>True) = False"
   2.349 -  "(\<not>False) = True"
   2.350 -  "(x = x) = True"
   2.351 -  "(P = True) = P"
   2.352 -  "(True = P) = P"
   2.353 -  "(P = False) = (\<not>P)"
   2.354 -  "(False = P) = (\<not>P)"
   2.355 -  "((\<not>P) = P) = False"
   2.356 -  "(P = (\<not>P)) = False"
   2.357 -  "((\<not>P) = (\<not>Q)) = (P = Q)"
   2.358 -  "\<not>(P = (\<not>Q)) = (P = Q)"
   2.359 -  "\<not>((\<not>P) = Q) = (P = Q)"
   2.360 -  "(P \<noteq> Q) = (Q = (\<not>P))"
   2.361 -  "(P = Q) = ((\<not>P \<or> Q) \<and> (P \<or> \<not>Q))"
   2.362 -  "(P \<noteq> Q) = ((\<not>P \<or> \<not>Q) \<and> (P \<or> Q))"
   2.363 -  by auto
   2.364 -
   2.365 -lemma [old_z3_rule]:
   2.366 -  "(if P then P else \<not>P) = True"
   2.367 -  "(if \<not>P then \<not>P else P) = True"
   2.368 -  "(if P then True else False) = P"
   2.369 -  "(if P then False else True) = (\<not>P)"
   2.370 -  "(if P then Q else True) = ((\<not>P) \<or> Q)"
   2.371 -  "(if P then Q else True) = (Q \<or> (\<not>P))"
   2.372 -  "(if P then Q else \<not>Q) = (P = Q)"
   2.373 -  "(if P then Q else \<not>Q) = (Q = P)"
   2.374 -  "(if P then \<not>Q else Q) = (P = (\<not>Q))"
   2.375 -  "(if P then \<not>Q else Q) = ((\<not>Q) = P)"
   2.376 -  "(if \<not>P then x else y) = (if P then y else x)"
   2.377 -  "(if P then (if Q then x else y) else x) = (if P \<and> (\<not>Q) then y else x)"
   2.378 -  "(if P then (if Q then x else y) else x) = (if (\<not>Q) \<and> P then y else x)"
   2.379 -  "(if P then (if Q then x else y) else y) = (if P \<and> Q then x else y)"
   2.380 -  "(if P then (if Q then x else y) else y) = (if Q \<and> P then x else y)"
   2.381 -  "(if P then x else if P then y else z) = (if P then x else z)"
   2.382 -  "(if P then x else if Q then x else y) = (if P \<or> Q then x else y)"
   2.383 -  "(if P then x else if Q then x else y) = (if Q \<or> P then x else y)"
   2.384 -  "(if P then x = y else x = z) = (x = (if P then y else z))"
   2.385 -  "(if P then x = y else y = z) = (y = (if P then x else z))"
   2.386 -  "(if P then x = y else z = y) = (y = (if P then x else z))"
   2.387 -  by auto
   2.388 -
   2.389 -lemma [old_z3_rule]:
   2.390 -  "0 + (x::int) = x"
   2.391 -  "x + 0 = x"
   2.392 -  "x + x = 2 * x"
   2.393 -  "0 * x = 0"
   2.394 -  "1 * x = x"
   2.395 -  "x + y = y + x"
   2.396 -  by auto
   2.397 -
   2.398 -lemma [old_z3_rule]:  (* for def-axiom *)
   2.399 -  "P = Q \<or> P \<or> Q"
   2.400 -  "P = Q \<or> \<not>P \<or> \<not>Q"
   2.401 -  "(\<not>P) = Q \<or> \<not>P \<or> Q"
   2.402 -  "(\<not>P) = Q \<or> P \<or> \<not>Q"
   2.403 -  "P = (\<not>Q) \<or> \<not>P \<or> Q"
   2.404 -  "P = (\<not>Q) \<or> P \<or> \<not>Q"
   2.405 -  "P \<noteq> Q \<or> P \<or> \<not>Q"
   2.406 -  "P \<noteq> Q \<or> \<not>P \<or> Q"
   2.407 -  "P \<noteq> (\<not>Q) \<or> P \<or> Q"
   2.408 -  "(\<not>P) \<noteq> Q \<or> P \<or> Q"
   2.409 -  "P \<or> Q \<or> P \<noteq> (\<not>Q)"
   2.410 -  "P \<or> Q \<or> (\<not>P) \<noteq> Q"
   2.411 -  "P \<or> \<not>Q \<or> P \<noteq> Q"
   2.412 -  "\<not>P \<or> Q \<or> P \<noteq> Q"
   2.413 -  "P \<or> y = (if P then x else y)"
   2.414 -  "P \<or> (if P then x else y) = y"
   2.415 -  "\<not>P \<or> x = (if P then x else y)"
   2.416 -  "\<not>P \<or>  (if P then x else y) = x"
   2.417 -  "P \<or> R \<or> \<not>(if P then Q else R)"
   2.418 -  "\<not>P \<or> Q \<or> \<not>(if P then Q else R)"
   2.419 -  "\<not>(if P then Q else R) \<or> \<not>P \<or> Q"
   2.420 -  "\<not>(if P then Q else R) \<or> P \<or> R"
   2.421 -  "(if P then Q else R) \<or> \<not>P \<or> \<not>Q"
   2.422 -  "(if P then Q else R) \<or> P \<or> \<not>R"
   2.423 -  "(if P then \<not>Q else R) \<or> \<not>P \<or> Q"
   2.424 -  "(if P then Q else \<not>R) \<or> P \<or> R"
   2.425 -  by auto
   2.426 -
   2.427 -ML_file "Old_SMT/old_smt_real.ML"
   2.428 -ML_file "Old_SMT/old_smt_word.ML"
   2.429 -
   2.430 -hide_type (open) pattern
   2.431 -hide_const fun_app term_true term_false z3div z3mod
   2.432 -hide_const (open) trigger pat nopat weight
   2.433 -
   2.434 -end
     3.1 --- a/src/HOL/Library/Old_SMT/old_smt_builtin.ML	Thu Apr 20 10:45:52 2017 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,231 +0,0 @@
     3.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_builtin.ML
     3.5 -    Author:     Sascha Boehme, TU Muenchen
     3.6 -
     3.7 -Tables of types and terms directly supported by SMT solvers.
     3.8 -*)
     3.9 -
    3.10 -signature OLD_SMT_BUILTIN =
    3.11 -sig
    3.12 -  (*for experiments*)
    3.13 -  val filter_builtins: (typ -> bool) -> Proof.context -> Proof.context
    3.14 -
    3.15 -  (*built-in types*)
    3.16 -  val add_builtin_typ: Old_SMT_Utils.class ->
    3.17 -    typ * (typ -> string option) * (typ -> int -> string option) ->
    3.18 -    Context.generic -> Context.generic
    3.19 -  val add_builtin_typ_ext: typ * (typ -> bool) -> Context.generic ->
    3.20 -    Context.generic
    3.21 -  val dest_builtin_typ: Proof.context -> typ -> string option
    3.22 -  val is_builtin_typ_ext: Proof.context -> typ -> bool
    3.23 -
    3.24 -  (*built-in numbers*)
    3.25 -  val dest_builtin_num: Proof.context -> term -> (string * typ) option
    3.26 -  val is_builtin_num: Proof.context -> term -> bool
    3.27 -  val is_builtin_num_ext: Proof.context -> term -> bool
    3.28 -
    3.29 -  (*built-in functions*)
    3.30 -  type 'a bfun = Proof.context -> typ -> term list -> 'a
    3.31 -  type bfunr = string * int * term list * (term list -> term)
    3.32 -  val add_builtin_fun: Old_SMT_Utils.class ->
    3.33 -    (string * typ) * bfunr option bfun -> Context.generic -> Context.generic
    3.34 -  val add_builtin_fun': Old_SMT_Utils.class -> term * string -> Context.generic ->
    3.35 -    Context.generic
    3.36 -  val add_builtin_fun_ext: (string * typ) * term list bfun ->
    3.37 -    Context.generic -> Context.generic
    3.38 -  val add_builtin_fun_ext': string * typ -> Context.generic -> Context.generic
    3.39 -  val add_builtin_fun_ext'': string -> Context.generic -> Context.generic
    3.40 -  val dest_builtin_fun: Proof.context -> string * typ -> term list ->
    3.41 -    bfunr option
    3.42 -  val dest_builtin_eq: Proof.context -> term -> term -> bfunr option
    3.43 -  val dest_builtin_pred: Proof.context -> string * typ -> term list ->
    3.44 -    bfunr option
    3.45 -  val dest_builtin_conn: Proof.context -> string * typ -> term list ->
    3.46 -    bfunr option
    3.47 -  val dest_builtin: Proof.context -> string * typ -> term list -> bfunr option
    3.48 -  val dest_builtin_ext: Proof.context -> string * typ -> term list ->
    3.49 -    term list option
    3.50 -  val is_builtin_fun: Proof.context -> string * typ -> term list -> bool
    3.51 -  val is_builtin_fun_ext: Proof.context -> string * typ -> term list -> bool
    3.52 -end
    3.53 -
    3.54 -structure Old_SMT_Builtin: OLD_SMT_BUILTIN =
    3.55 -struct
    3.56 -
    3.57 -
    3.58 -(* built-in tables *)
    3.59 -
    3.60 -datatype ('a, 'b) kind = Ext of 'a | Int of 'b
    3.61 -
    3.62 -type ('a, 'b) ttab = ((typ * ('a, 'b) kind) Ord_List.T) Old_SMT_Utils.dict 
    3.63 -
    3.64 -fun typ_ord ((T, _), (U, _)) =
    3.65 -  let
    3.66 -    fun tord (TVar _, Type _) = GREATER
    3.67 -      | tord (Type _, TVar _) = LESS
    3.68 -      | tord (Type (n, Ts), Type (m, Us)) =
    3.69 -          if n = m then list_ord tord (Ts, Us)
    3.70 -          else Term_Ord.typ_ord (T, U)
    3.71 -      | tord TU = Term_Ord.typ_ord TU
    3.72 -  in tord (T, U) end
    3.73 -
    3.74 -fun insert_ttab cs T f =
    3.75 -  Old_SMT_Utils.dict_map_default (cs, [])
    3.76 -    (Ord_List.insert typ_ord (perhaps (try Logic.varifyT_global) T, f))
    3.77 -
    3.78 -fun merge_ttab ttabp =
    3.79 -  Old_SMT_Utils.dict_merge (Ord_List.merge typ_ord) ttabp
    3.80 -
    3.81 -fun lookup_ttab ctxt ttab T =
    3.82 -  let fun match (U, _) = Sign.typ_instance (Proof_Context.theory_of ctxt) (T, U)
    3.83 -  in
    3.84 -    get_first (find_first match)
    3.85 -      (Old_SMT_Utils.dict_lookup ttab (Old_SMT_Config.solver_class_of ctxt))
    3.86 -  end
    3.87 -
    3.88 -type ('a, 'b) btab = ('a, 'b) ttab Symtab.table
    3.89 -
    3.90 -fun insert_btab cs n T f =
    3.91 -  Symtab.map_default (n, []) (insert_ttab cs T f)
    3.92 -
    3.93 -fun merge_btab btabp = Symtab.join (K merge_ttab) btabp
    3.94 -
    3.95 -fun lookup_btab ctxt btab (n, T) =
    3.96 -  (case Symtab.lookup btab n of
    3.97 -    NONE => NONE
    3.98 -  | SOME ttab => lookup_ttab ctxt ttab T)
    3.99 -
   3.100 -type 'a bfun = Proof.context -> typ -> term list -> 'a
   3.101 -
   3.102 -type bfunr = string * int * term list * (term list -> term)
   3.103 -
   3.104 -structure Builtins = Generic_Data
   3.105 -(
   3.106 -  type T =
   3.107 -    (typ -> bool, (typ -> string option) * (typ -> int -> string option)) ttab *
   3.108 -    (term list bfun, bfunr option bfun) btab
   3.109 -  val empty = ([], Symtab.empty)
   3.110 -  val extend = I
   3.111 -  fun merge ((t1, b1), (t2, b2)) = (merge_ttab (t1, t2), merge_btab (b1, b2))
   3.112 -)
   3.113 -
   3.114 -fun filter_ttab keep_T = map (apsnd (filter (keep_T o fst)))
   3.115 -
   3.116 -fun filter_builtins keep_T =
   3.117 -  Context.proof_map (Builtins.map (fn (ttab, btab) =>
   3.118 -    (filter_ttab keep_T ttab, Symtab.map (K (filter_ttab keep_T)) btab)))
   3.119 -
   3.120 -
   3.121 -(* built-in types *)
   3.122 -
   3.123 -fun add_builtin_typ cs (T, f, g) =
   3.124 -  Builtins.map (apfst (insert_ttab cs T (Int (f, g))))
   3.125 -
   3.126 -fun add_builtin_typ_ext (T, f) =
   3.127 -  Builtins.map (apfst (insert_ttab Old_SMT_Utils.basicC T (Ext f)))
   3.128 -
   3.129 -fun lookup_builtin_typ ctxt =
   3.130 -  lookup_ttab ctxt (fst (Builtins.get (Context.Proof ctxt)))
   3.131 -
   3.132 -fun dest_builtin_typ ctxt T =
   3.133 -  (case lookup_builtin_typ ctxt T of
   3.134 -    SOME (_, Int (f, _)) => f T
   3.135 -  | _ => NONE) 
   3.136 -
   3.137 -fun is_builtin_typ_ext ctxt T =
   3.138 -  (case lookup_builtin_typ ctxt T of
   3.139 -    SOME (_, Int (f, _)) => is_some (f T)
   3.140 -  | SOME (_, Ext f) => f T
   3.141 -  | NONE => false)
   3.142 -
   3.143 -
   3.144 -(* built-in numbers *)
   3.145 -
   3.146 -fun dest_builtin_num ctxt t =
   3.147 -  (case try HOLogic.dest_number t of
   3.148 -    NONE => NONE
   3.149 -  | SOME (T, i) =>
   3.150 -      if i < 0 then NONE else
   3.151 -        (case lookup_builtin_typ ctxt T of
   3.152 -          SOME (_, Int (_, g)) => g T i |> Option.map (rpair T)
   3.153 -        | _ => NONE))
   3.154 -
   3.155 -val is_builtin_num = is_some oo dest_builtin_num
   3.156 -
   3.157 -fun is_builtin_num_ext ctxt t =
   3.158 -  (case try HOLogic.dest_number t of
   3.159 -    NONE => false
   3.160 -  | SOME (T, _) => is_builtin_typ_ext ctxt T)
   3.161 -
   3.162 -
   3.163 -(* built-in functions *)
   3.164 -
   3.165 -fun add_builtin_fun cs ((n, T), f) =
   3.166 -  Builtins.map (apsnd (insert_btab cs n T (Int f)))
   3.167 -
   3.168 -fun add_builtin_fun' cs (t, n) =
   3.169 -  let
   3.170 -    val c as (m, T) = Term.dest_Const t
   3.171 -    fun app U ts = Term.list_comb (Const (m, U), ts)
   3.172 -    fun bfun _ U ts = SOME (n, length (Term.binder_types T), ts, app U)
   3.173 -  in add_builtin_fun cs (c, bfun) end
   3.174 -
   3.175 -fun add_builtin_fun_ext ((n, T), f) =
   3.176 -  Builtins.map (apsnd (insert_btab Old_SMT_Utils.basicC n T (Ext f)))
   3.177 -
   3.178 -fun add_builtin_fun_ext' c = add_builtin_fun_ext (c, fn _ => fn _ => I)
   3.179 -
   3.180 -fun add_builtin_fun_ext'' n context =
   3.181 -  let val thy = Context.theory_of context
   3.182 -  in add_builtin_fun_ext' (n, Sign.the_const_type thy n) context end
   3.183 -
   3.184 -fun lookup_builtin_fun ctxt =
   3.185 -  lookup_btab ctxt (snd (Builtins.get (Context.Proof ctxt)))
   3.186 -
   3.187 -fun dest_builtin_fun ctxt (c as (_, T)) ts =
   3.188 -  (case lookup_builtin_fun ctxt c of
   3.189 -    SOME (_, Int f) => f ctxt T ts
   3.190 -  | _ => NONE)
   3.191 -
   3.192 -fun dest_builtin_eq ctxt t u =
   3.193 -  let
   3.194 -    val aT = TFree (Name.aT, @{sort type})
   3.195 -    val c = (@{const_name HOL.eq}, aT --> aT --> @{typ bool})
   3.196 -    fun mk ts = Term.list_comb (HOLogic.eq_const (Term.fastype_of (hd ts)), ts)
   3.197 -  in
   3.198 -    dest_builtin_fun ctxt c []
   3.199 -    |> Option.map (fn (n, i, _, _) => (n, i, [t, u], mk))
   3.200 -  end
   3.201 -
   3.202 -fun special_builtin_fun pred ctxt (c as (_, T)) ts =
   3.203 -  if pred (Term.body_type T, Term.binder_types T) then
   3.204 -    dest_builtin_fun ctxt c ts
   3.205 -  else NONE
   3.206 -
   3.207 -fun dest_builtin_pred ctxt = special_builtin_fun (equal @{typ bool} o fst) ctxt
   3.208 -
   3.209 -fun dest_builtin_conn ctxt =
   3.210 -  special_builtin_fun (forall (equal @{typ bool}) o (op ::)) ctxt
   3.211 -
   3.212 -fun dest_builtin ctxt c ts =
   3.213 -  let val t = Term.list_comb (Const c, ts)
   3.214 -  in
   3.215 -    (case dest_builtin_num ctxt t of
   3.216 -      SOME (n, _) => SOME (n, 0, [], K t)
   3.217 -    | NONE => dest_builtin_fun ctxt c ts)
   3.218 -  end
   3.219 -
   3.220 -fun dest_builtin_fun_ext ctxt (c as (_, T)) ts =    
   3.221 -  (case lookup_builtin_fun ctxt c of
   3.222 -    SOME (_, Int f) => f ctxt T ts |> Option.map (fn (_, _, us, _) => us)
   3.223 -  | SOME (_, Ext f) => SOME (f ctxt T ts)
   3.224 -  | NONE => NONE)
   3.225 -
   3.226 -fun dest_builtin_ext ctxt c ts =
   3.227 -  if is_builtin_num_ext ctxt (Term.list_comb (Const c, ts)) then SOME []
   3.228 -  else dest_builtin_fun_ext ctxt c ts
   3.229 -
   3.230 -fun is_builtin_fun ctxt c ts = is_some (dest_builtin_fun ctxt c ts)
   3.231 -
   3.232 -fun is_builtin_fun_ext ctxt c ts = is_some (dest_builtin_fun_ext ctxt c ts)
   3.233 -
   3.234 -end
     4.1 --- a/src/HOL/Library/Old_SMT/old_smt_config.ML	Thu Apr 20 10:45:52 2017 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,254 +0,0 @@
     4.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_config.ML
     4.5 -    Author:     Sascha Boehme, TU Muenchen
     4.6 -
     4.7 -Configuration options and diagnostic tools for SMT.
     4.8 -*)
     4.9 -
    4.10 -signature OLD_SMT_CONFIG =
    4.11 -sig
    4.12 -  (*solver*)
    4.13 -  type solver_info = {
    4.14 -    name: string,
    4.15 -    class: Proof.context -> Old_SMT_Utils.class,
    4.16 -    avail: unit -> bool,
    4.17 -    options: Proof.context -> string list }
    4.18 -  val add_solver: solver_info -> Context.generic -> Context.generic
    4.19 -  val set_solver_options: string * string -> Context.generic -> Context.generic
    4.20 -  val is_available: Proof.context -> string -> bool
    4.21 -  val available_solvers_of: Proof.context -> string list
    4.22 -  val select_solver: string -> Context.generic -> Context.generic
    4.23 -  val solver_of: Proof.context -> string
    4.24 -  val solver_class_of: Proof.context -> Old_SMT_Utils.class
    4.25 -  val solver_options_of: Proof.context -> string list
    4.26 -
    4.27 -  (*options*)
    4.28 -  val oracle: bool Config.T
    4.29 -  val datatypes: bool Config.T
    4.30 -  val timeout: real Config.T
    4.31 -  val random_seed: int Config.T
    4.32 -  val read_only_certificates: bool Config.T
    4.33 -  val verbose: bool Config.T
    4.34 -  val trace: bool Config.T
    4.35 -  val trace_used_facts: bool Config.T
    4.36 -  val monomorph_limit: int Config.T
    4.37 -  val monomorph_instances: int Config.T
    4.38 -  val infer_triggers: bool Config.T
    4.39 -  val filter_only_facts: bool Config.T
    4.40 -  val debug_files: string Config.T
    4.41 -
    4.42 -  (*tools*)
    4.43 -  val with_timeout: Proof.context -> ('a -> 'b) -> 'a -> 'b
    4.44 -
    4.45 -  (*diagnostics*)
    4.46 -  val trace_msg: Proof.context -> ('a -> string) -> 'a -> unit
    4.47 -  val verbose_msg: Proof.context -> ('a -> string) -> 'a -> unit
    4.48 -
    4.49 -  (*certificates*)
    4.50 -  val select_certificates: string -> Context.generic -> Context.generic
    4.51 -  val certificates_of: Proof.context -> Cache_IO.cache option
    4.52 -
    4.53 -  (*setup*)
    4.54 -  val setup: theory -> theory
    4.55 -  val print_setup: Proof.context -> unit
    4.56 -end
    4.57 -
    4.58 -structure Old_SMT_Config: OLD_SMT_CONFIG =
    4.59 -struct
    4.60 -
    4.61 -(* solver *)
    4.62 -
    4.63 -type solver_info = {
    4.64 -  name: string,
    4.65 -  class: Proof.context -> Old_SMT_Utils.class,
    4.66 -  avail: unit -> bool,
    4.67 -  options: Proof.context -> string list }
    4.68 -
    4.69 -(* FIXME just one data slot (record) per program unit *)
    4.70 -structure Solvers = Generic_Data
    4.71 -(
    4.72 -  type T = (solver_info * string list) Symtab.table * string option
    4.73 -  val empty = (Symtab.empty, NONE)
    4.74 -  val extend = I
    4.75 -  fun merge ((ss1, s1), (ss2, s2)) =
    4.76 -    (Symtab.merge (K true) (ss1, ss2), merge_options (s1, s2))
    4.77 -)
    4.78 -
    4.79 -fun set_solver_options (name, options) =
    4.80 -  let val opts = String.tokens (Symbol.is_ascii_blank o str) options
    4.81 -  in Solvers.map (apfst (Symtab.map_entry name (apsnd (K opts)))) end
    4.82 -
    4.83 -fun add_solver (info as {name, ...} : solver_info) context =
    4.84 -  if Symtab.defined (fst (Solvers.get context)) name then
    4.85 -    error ("Solver already registered: " ^ quote name)
    4.86 -  else
    4.87 -    context
    4.88 -    |> Solvers.map (apfst (Symtab.update (name, (info, []))))
    4.89 -    |> Context.map_theory (Attrib.setup (Binding.name ("old_" ^ name ^ "_options"))
    4.90 -        (Scan.lift (@{keyword "="} |-- Args.name) >>
    4.91 -          (Thm.declaration_attribute o K o set_solver_options o pair name))
    4.92 -        ("additional command line options for SMT solver " ^ quote name))
    4.93 -
    4.94 -fun all_solvers_of ctxt = Symtab.keys (fst (Solvers.get (Context.Proof ctxt)))
    4.95 -
    4.96 -fun solver_name_of ctxt = snd (Solvers.get (Context.Proof ctxt))
    4.97 -
    4.98 -fun is_available ctxt name =
    4.99 -  (case Symtab.lookup (fst (Solvers.get (Context.Proof ctxt))) name of
   4.100 -    SOME ({avail, ...}, _) => avail ()
   4.101 -  | NONE => false)
   4.102 -
   4.103 -fun available_solvers_of ctxt =
   4.104 -  filter (is_available ctxt) (all_solvers_of ctxt)
   4.105 -
   4.106 -fun warn_solver (Context.Proof ctxt) name =
   4.107 -      if Context_Position.is_visible ctxt then
   4.108 -        warning ("The SMT solver " ^ quote name ^ " is not installed.")
   4.109 -      else ()
   4.110 -  | warn_solver _ _ = ();
   4.111 -
   4.112 -fun select_solver name context =
   4.113 -  let
   4.114 -    val ctxt = Context.proof_of context
   4.115 -    val upd = Solvers.map (apsnd (K (SOME name)))
   4.116 -  in
   4.117 -    if not (member (op =) (all_solvers_of ctxt) name) then
   4.118 -      error ("Trying to select unknown solver: " ^ quote name)
   4.119 -    else if not (is_available ctxt name) then
   4.120 -      (warn_solver context name; upd context)
   4.121 -    else upd context
   4.122 -  end
   4.123 -
   4.124 -fun no_solver_err () = error "No SMT solver selected"
   4.125 -
   4.126 -fun solver_of ctxt =
   4.127 -  (case solver_name_of ctxt of
   4.128 -    SOME name => name
   4.129 -  | NONE => no_solver_err ())
   4.130 -
   4.131 -fun solver_info_of default select ctxt =
   4.132 -  (case Solvers.get (Context.Proof ctxt) of
   4.133 -    (solvers, SOME name) => select (Symtab.lookup solvers name)
   4.134 -  | (_, NONE) => default ())
   4.135 -
   4.136 -fun solver_class_of ctxt =
   4.137 -  let fun class_of ({class, ...}: solver_info, _) = class ctxt
   4.138 -  in solver_info_of no_solver_err (class_of o the) ctxt end
   4.139 -
   4.140 -fun solver_options_of ctxt =
   4.141 -  let
   4.142 -    fun all_options NONE = []
   4.143 -      | all_options (SOME ({options, ...} : solver_info, opts)) =
   4.144 -          opts @ options ctxt
   4.145 -  in solver_info_of (K []) all_options ctxt end
   4.146 -
   4.147 -val setup_solver =
   4.148 -  Attrib.setup @{binding old_smt_solver}
   4.149 -    (Scan.lift (@{keyword "="} |-- Args.name) >>
   4.150 -      (Thm.declaration_attribute o K o select_solver))
   4.151 -    "SMT solver configuration"
   4.152 -
   4.153 -
   4.154 -(* options *)
   4.155 -
   4.156 -val oracle = Attrib.setup_config_bool @{binding old_smt_oracle} (K true)
   4.157 -val datatypes = Attrib.setup_config_bool @{binding old_smt_datatypes} (K false)
   4.158 -val timeout = Attrib.setup_config_real @{binding old_smt_timeout} (K 30.0)
   4.159 -val random_seed = Attrib.setup_config_int @{binding old_smt_random_seed} (K 1)
   4.160 -val read_only_certificates = Attrib.setup_config_bool @{binding old_smt_read_only_certificates} (K false)
   4.161 -val verbose = Attrib.setup_config_bool @{binding old_smt_verbose} (K true)
   4.162 -val trace = Attrib.setup_config_bool @{binding old_smt_trace} (K false)
   4.163 -val trace_used_facts = Attrib.setup_config_bool @{binding old_smt_trace_used_facts} (K false)
   4.164 -val monomorph_limit = Attrib.setup_config_int @{binding old_smt_monomorph_limit} (K 10)
   4.165 -val monomorph_instances = Attrib.setup_config_int @{binding old_smt_monomorph_instances} (K 500)
   4.166 -val infer_triggers = Attrib.setup_config_bool @{binding old_smt_infer_triggers} (K false)
   4.167 -val filter_only_facts = Attrib.setup_config_bool @{binding old_smt_filter_only_facts} (K false)
   4.168 -val debug_files = Attrib.setup_config_string @{binding old_smt_debug_files} (K "")
   4.169 -
   4.170 -
   4.171 -(* diagnostics *)
   4.172 -
   4.173 -fun cond_trace flag f x = if flag then tracing ("SMT: " ^ f x) else ()
   4.174 -
   4.175 -fun verbose_msg ctxt = cond_trace (Config.get ctxt verbose)
   4.176 -
   4.177 -fun trace_msg ctxt = cond_trace (Config.get ctxt trace)
   4.178 -
   4.179 -
   4.180 -(* tools *)
   4.181 -
   4.182 -fun with_timeout ctxt f x =
   4.183 -  Timeout.apply (seconds (Config.get ctxt timeout)) f x
   4.184 -    handle Timeout.TIMEOUT _ => raise Old_SMT_Failure.SMT Old_SMT_Failure.Time_Out
   4.185 -
   4.186 -
   4.187 -(* certificates *)
   4.188 -
   4.189 -(* FIXME just one data slot (record) per program unit *)
   4.190 -structure Certificates = Generic_Data
   4.191 -(
   4.192 -  type T = Cache_IO.cache option
   4.193 -  val empty = NONE
   4.194 -  val extend = I
   4.195 -  fun merge (s, _) = s  (* FIXME merge options!? *)
   4.196 -)
   4.197 -
   4.198 -val get_certificates_path =
   4.199 -  Option.map (Cache_IO.cache_path_of) o Certificates.get o Context.Proof
   4.200 -
   4.201 -fun select_certificates name context = context |> Certificates.put (
   4.202 -  if name = "" then NONE
   4.203 -  else
   4.204 -    Path.explode name
   4.205 -    |> Path.append (Resources.master_directory (Context.theory_of context))
   4.206 -    |> SOME o Cache_IO.unsynchronized_init)
   4.207 -
   4.208 -val certificates_of = Certificates.get o Context.Proof
   4.209 -
   4.210 -val setup_certificates =
   4.211 -  Attrib.setup @{binding old_smt_certificates}
   4.212 -    (Scan.lift (@{keyword "="} |-- Args.name) >>
   4.213 -      (Thm.declaration_attribute o K o select_certificates))
   4.214 -    "SMT certificates configuration"
   4.215 -
   4.216 -
   4.217 -(* setup *)
   4.218 -
   4.219 -val setup =
   4.220 -  setup_solver #>
   4.221 -  setup_certificates
   4.222 -
   4.223 -fun print_setup ctxt =
   4.224 -  let
   4.225 -    fun string_of_bool b = if b then "true" else "false"
   4.226 -
   4.227 -    val names = available_solvers_of ctxt
   4.228 -    val ns = if null names then ["(none)"] else sort_strings names
   4.229 -    val n = the_default "(none)" (solver_name_of ctxt)
   4.230 -    val opts = solver_options_of ctxt
   4.231 -    
   4.232 -    val t = string_of_real (Config.get ctxt timeout)
   4.233 -
   4.234 -    val certs_filename =
   4.235 -      (case get_certificates_path ctxt of
   4.236 -        SOME path => Path.print path
   4.237 -      | NONE => "(disabled)")
   4.238 -  in
   4.239 -    Pretty.writeln (Pretty.big_list "SMT setup:" [
   4.240 -      Pretty.str ("Current SMT solver: " ^ n),
   4.241 -      Pretty.str ("Current SMT solver options: " ^ space_implode " " opts),
   4.242 -      Pretty.str_list "Available SMT solvers: "  "" ns,
   4.243 -      Pretty.str ("Current timeout: " ^ t ^ " seconds"),
   4.244 -      Pretty.str ("With proofs: " ^
   4.245 -        string_of_bool (not (Config.get ctxt oracle))),
   4.246 -      Pretty.str ("Certificates cache: " ^ certs_filename),
   4.247 -      Pretty.str ("Fixed certificates: " ^
   4.248 -        string_of_bool (Config.get ctxt read_only_certificates))])
   4.249 -  end
   4.250 -
   4.251 -val _ =
   4.252 -  Outer_Syntax.command @{command_keyword old_smt_status}
   4.253 -    "show the available SMT solvers, the currently selected SMT solver, \
   4.254 -    \and the values of SMT configuration options"
   4.255 -    (Scan.succeed (Toplevel.keep (print_setup o Toplevel.context_of)))
   4.256 -
   4.257 -end
     5.1 --- a/src/HOL/Library/Old_SMT/old_smt_datatypes.ML	Thu Apr 20 10:45:52 2017 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,94 +0,0 @@
     5.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_datatypes.ML
     5.5 -    Author:     Sascha Boehme, TU Muenchen
     5.6 -
     5.7 -Collector functions for common type declarations and their representation
     5.8 -as algebraic datatypes.
     5.9 -*)
    5.10 -
    5.11 -signature OLD_SMT_DATATYPES =
    5.12 -sig
    5.13 -  val add_decls: typ ->
    5.14 -    (typ * (term * term list) list) list list * Proof.context ->
    5.15 -    (typ * (term * term list) list) list list * Proof.context
    5.16 -end
    5.17 -
    5.18 -structure Old_SMT_Datatypes: OLD_SMT_DATATYPES =
    5.19 -struct
    5.20 -
    5.21 -fun mk_selectors T Ts =
    5.22 -  Variable.variant_fixes (replicate (length Ts) "select")
    5.23 -  #>> map2 (fn U => fn n => Free (n, T --> U)) Ts
    5.24 -
    5.25 -
    5.26 -(* free constructor type declarations *)
    5.27 -
    5.28 -fun get_ctr_sugar_decl ({ctrs, ...} : Ctr_Sugar.ctr_sugar) T Ts ctxt =
    5.29 -  let
    5.30 -    fun mk_constr ctr0 =
    5.31 -      let val ctr = Ctr_Sugar.mk_ctr Ts ctr0 in
    5.32 -        mk_selectors T (binder_types (fastype_of ctr)) #>> pair ctr
    5.33 -      end
    5.34 -  in
    5.35 -    fold_map mk_constr ctrs ctxt
    5.36 -    |>> (pair T #> single)
    5.37 -  end
    5.38 -
    5.39 -
    5.40 -(* typedef declarations *)
    5.41 -
    5.42 -fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
    5.43 -    : Typedef.info) T Ts =
    5.44 -  if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
    5.45 -    let
    5.46 -      val env = snd (Term.dest_Type abs_type) ~~ Ts
    5.47 -      val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
    5.48 -
    5.49 -      val constr = Const (Abs_name, instT (rep_type --> abs_type))
    5.50 -      val select = Const (Rep_name, instT (abs_type --> rep_type))
    5.51 -    in [(T, [(constr, [select])])] end
    5.52 -  else
    5.53 -    []
    5.54 -
    5.55 -
    5.56 -(* collection of declarations *)
    5.57 -
    5.58 -fun declared declss T = exists (exists (equal T o fst)) declss
    5.59 -fun declared' dss T = exists (exists (equal T o fst) o snd) dss
    5.60 -
    5.61 -fun get_decls T n Ts ctxt =
    5.62 -  (case Ctr_Sugar.ctr_sugar_of ctxt n of
    5.63 -    SOME ctr_sugar => get_ctr_sugar_decl ctr_sugar T Ts ctxt
    5.64 -  | NONE =>
    5.65 -      (case Typedef.get_info ctxt n of
    5.66 -        [] => ([], ctxt)
    5.67 -      | info :: _ => (get_typedef_decl info T Ts, ctxt)))
    5.68 -
    5.69 -fun add_decls T (declss, ctxt) =
    5.70 -  let
    5.71 -    fun depends Ts ds = exists (member (op =) (map fst ds)) Ts
    5.72 -
    5.73 -    fun add (TFree _) = I
    5.74 -      | add (TVar _) = I
    5.75 -      | add (T as Type (@{type_name fun}, _)) =
    5.76 -          fold add (Term.body_type T :: Term.binder_types T)
    5.77 -      | add @{typ bool} = I
    5.78 -      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
    5.79 -          if declared declss T orelse declared' dss T then (dss, ctxt1)
    5.80 -          else if Old_SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
    5.81 -          else
    5.82 -            (case get_decls T n Ts ctxt1 of
    5.83 -              ([], _) => (dss, ctxt1)
    5.84 -            | (ds, ctxt2) =>
    5.85 -                let
    5.86 -                  val constrTs =
    5.87 -                    maps (map (snd o Term.dest_Const o fst) o snd) ds
    5.88 -                  val Us = fold (union (op =) o Term.binder_types) constrTs []
    5.89 -
    5.90 -                  fun ins [] = [(Us, ds)]
    5.91 -                    | ins ((Uds as (Us', _)) :: Udss) =
    5.92 -                        if depends Us' ds then (Us, ds) :: Uds :: Udss
    5.93 -                        else Uds :: ins Udss
    5.94 -            in fold add Us (ins dss, ctxt2) end))
    5.95 -  in add T ([], ctxt) |>> append declss o map snd end
    5.96 -
    5.97 -end
     6.1 --- a/src/HOL/Library/Old_SMT/old_smt_failure.ML	Thu Apr 20 10:45:52 2017 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,61 +0,0 @@
     6.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_failure.ML
     6.5 -    Author:     Sascha Boehme, TU Muenchen
     6.6 -
     6.7 -Failures and exception of SMT.
     6.8 -*)
     6.9 -
    6.10 -signature OLD_SMT_FAILURE =
    6.11 -sig
    6.12 -  type counterexample = {
    6.13 -    is_real_cex: bool,
    6.14 -    free_constraints: term list,
    6.15 -    const_defs: term list}
    6.16 -  datatype failure =
    6.17 -    Counterexample of counterexample |
    6.18 -    Time_Out |
    6.19 -    Out_Of_Memory |
    6.20 -    Abnormal_Termination of int |
    6.21 -    Other_Failure of string
    6.22 -  val pretty_counterexample: Proof.context -> counterexample -> Pretty.T
    6.23 -  val string_of_failure: Proof.context -> failure -> string
    6.24 -  exception SMT of failure
    6.25 -end
    6.26 -
    6.27 -structure Old_SMT_Failure: OLD_SMT_FAILURE =
    6.28 -struct
    6.29 -
    6.30 -type counterexample = {
    6.31 -  is_real_cex: bool,
    6.32 -  free_constraints: term list,
    6.33 -  const_defs: term list}
    6.34 -
    6.35 -datatype failure =
    6.36 -  Counterexample of counterexample |
    6.37 -  Time_Out |
    6.38 -  Out_Of_Memory |
    6.39 -  Abnormal_Termination of int |
    6.40 -  Other_Failure of string
    6.41 -
    6.42 -fun pretty_counterexample ctxt {is_real_cex, free_constraints, const_defs} =
    6.43 -  let
    6.44 -    val msg =
    6.45 -      if is_real_cex then "Counterexample found (possibly spurious)"
    6.46 -      else "Potential counterexample found"
    6.47 -  in
    6.48 -    if null free_constraints andalso null const_defs then Pretty.str msg
    6.49 -    else
    6.50 -      Pretty.big_list (msg ^ ":")
    6.51 -        (map (Syntax.pretty_term ctxt) (free_constraints @ const_defs))
    6.52 -  end
    6.53 -
    6.54 -fun string_of_failure ctxt (Counterexample cex) =
    6.55 -      Pretty.string_of (pretty_counterexample ctxt cex)
    6.56 -  | string_of_failure _ Time_Out = "Timed out"
    6.57 -  | string_of_failure _ Out_Of_Memory = "Ran out of memory"
    6.58 -  | string_of_failure _ (Abnormal_Termination err) =
    6.59 -      "Solver terminated abnormally with error code " ^ string_of_int err
    6.60 -  | string_of_failure _ (Other_Failure msg) = msg
    6.61 -
    6.62 -exception SMT of failure
    6.63 -
    6.64 -end
     7.1 --- a/src/HOL/Library/Old_SMT/old_smt_normalize.ML	Thu Apr 20 10:45:52 2017 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,652 +0,0 @@
     7.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_normalize.ML
     7.5 -    Author:     Sascha Boehme, TU Muenchen
     7.6 -
     7.7 -Normalization steps on theorems required by SMT solvers.
     7.8 -*)
     7.9 -
    7.10 -signature OLD_SMT_NORMALIZE =
    7.11 -sig
    7.12 -  val drop_fact_warning: Proof.context -> thm -> unit
    7.13 -  val atomize_conv: Proof.context -> conv
    7.14 -  type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
    7.15 -  val add_extra_norm: Old_SMT_Utils.class * extra_norm -> Context.generic ->
    7.16 -    Context.generic
    7.17 -  val normalize: (int * (int option * thm)) list -> Proof.context ->
    7.18 -    (int * thm) list * Proof.context
    7.19 -  val setup: theory -> theory
    7.20 -end
    7.21 -
    7.22 -structure Old_SMT_Normalize: OLD_SMT_NORMALIZE =
    7.23 -struct
    7.24 -
    7.25 -fun drop_fact_warning ctxt =
    7.26 -  Old_SMT_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
    7.27 -    Thm.string_of_thm ctxt)
    7.28 -
    7.29 -
    7.30 -(* general theorem normalizations *)
    7.31 -
    7.32 -(** instantiate elimination rules **)
    7.33 - 
    7.34 -local
    7.35 -  val (cpfalse, cfalse) =
    7.36 -    `Old_SMT_Utils.mk_cprop (Thm.cterm_of @{context} @{const False})
    7.37 -
    7.38 -  fun inst f ct thm =
    7.39 -    let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
    7.40 -    in Thm.instantiate ([], [(dest_Var (Thm.term_of cv), ct)]) thm end
    7.41 -in
    7.42 -
    7.43 -fun instantiate_elim thm =
    7.44 -  (case Thm.concl_of thm of
    7.45 -    @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
    7.46 -  | Var _ => inst I cpfalse thm
    7.47 -  | _ => thm)
    7.48 -
    7.49 -end
    7.50 -
    7.51 -
    7.52 -(** normalize definitions **)
    7.53 -
    7.54 -fun norm_def thm =
    7.55 -  (case Thm.prop_of thm of
    7.56 -    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
    7.57 -      norm_def (thm RS @{thm fun_cong})
    7.58 -  | Const (@{const_name Pure.eq}, _) $ _ $ Abs _ =>
    7.59 -      norm_def (thm RS @{thm meta_eq_to_obj_eq})
    7.60 -  | _ => thm)
    7.61 -
    7.62 -
    7.63 -(** atomization **)
    7.64 -
    7.65 -fun atomize_conv ctxt ct =
    7.66 -  (case Thm.term_of ct of
    7.67 -    @{const Pure.imp} $ _ $ _ =>
    7.68 -      Conv.binop_conv (atomize_conv ctxt) then_conv
    7.69 -      Conv.rewr_conv @{thm atomize_imp}
    7.70 -  | Const (@{const_name Pure.eq}, _) $ _ $ _ =>
    7.71 -      Conv.binop_conv (atomize_conv ctxt) then_conv
    7.72 -      Conv.rewr_conv @{thm atomize_eq}
    7.73 -  | Const (@{const_name Pure.all}, _) $ Abs _ =>
    7.74 -      Conv.binder_conv (atomize_conv o snd) ctxt then_conv
    7.75 -      Conv.rewr_conv @{thm atomize_all}
    7.76 -  | _ => Conv.all_conv) ct
    7.77 -
    7.78 -val setup_atomize =
    7.79 -  fold Old_SMT_Builtin.add_builtin_fun_ext'' [@{const_name Pure.imp},
    7.80 -    @{const_name Pure.eq}, @{const_name Pure.all}, @{const_name Trueprop}]
    7.81 -
    7.82 -
    7.83 -(** unfold special quantifiers **)
    7.84 -
    7.85 -local
    7.86 -  val ex1_def = mk_meta_eq @{lemma
    7.87 -    "Ex1 = (%P. EX x. P x & (ALL y. P y --> y = x))"
    7.88 -    by (rule ext) (simp only: Ex1_def)}
    7.89 -
    7.90 -  val ball_def = mk_meta_eq @{lemma "Ball = (%A P. ALL x. x : A --> P x)"
    7.91 -    by (rule ext)+ (rule Ball_def)}
    7.92 -
    7.93 -  val bex_def = mk_meta_eq @{lemma "Bex = (%A P. EX x. x : A & P x)"
    7.94 -    by (rule ext)+ (rule Bex_def)}
    7.95 -
    7.96 -  val special_quants = [(@{const_name Ex1}, ex1_def),
    7.97 -    (@{const_name Ball}, ball_def), (@{const_name Bex}, bex_def)]
    7.98 -  
    7.99 -  fun special_quant (Const (n, _)) = AList.lookup (op =) special_quants n
   7.100 -    | special_quant _ = NONE
   7.101 -
   7.102 -  fun special_quant_conv _ ct =
   7.103 -    (case special_quant (Thm.term_of ct) of
   7.104 -      SOME thm => Conv.rewr_conv thm
   7.105 -    | NONE => Conv.all_conv) ct
   7.106 -in
   7.107 -
   7.108 -fun unfold_special_quants_conv ctxt =
   7.109 -  Old_SMT_Utils.if_exists_conv (is_some o special_quant)
   7.110 -    (Conv.top_conv special_quant_conv ctxt)
   7.111 -
   7.112 -val setup_unfolded_quants =
   7.113 -  fold (Old_SMT_Builtin.add_builtin_fun_ext'' o fst) special_quants
   7.114 -
   7.115 -end
   7.116 -
   7.117 -
   7.118 -(** trigger inference **)
   7.119 -
   7.120 -local
   7.121 -  (*** check trigger syntax ***)
   7.122 -
   7.123 -  fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
   7.124 -    | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
   7.125 -    | dest_trigger _ = NONE
   7.126 -
   7.127 -  fun eq_list [] = false
   7.128 -    | eq_list (b :: bs) = forall (equal b) bs
   7.129 -
   7.130 -  fun proper_trigger t =
   7.131 -    t
   7.132 -    |> these o try HOLogic.dest_list
   7.133 -    |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
   7.134 -    |> (fn [] => false | bss => forall eq_list bss)
   7.135 -
   7.136 -  fun proper_quant inside f t =
   7.137 -    (case t of
   7.138 -      Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
   7.139 -    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
   7.140 -    | @{const trigger} $ p $ u =>
   7.141 -        (if inside then f p else false) andalso proper_quant false f u
   7.142 -    | Abs (_, _, u) => proper_quant false f u
   7.143 -    | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
   7.144 -    | _ => true)
   7.145 -
   7.146 -  fun check_trigger_error ctxt t =
   7.147 -    error ("SMT triggers must only occur under quantifier and multipatterns " ^
   7.148 -      "must have the same kind: " ^ Syntax.string_of_term ctxt t)
   7.149 -
   7.150 -  fun check_trigger_conv ctxt ct =
   7.151 -    if proper_quant false proper_trigger (Old_SMT_Utils.term_of ct) then
   7.152 -      Conv.all_conv ct
   7.153 -    else check_trigger_error ctxt (Thm.term_of ct)
   7.154 -
   7.155 -
   7.156 -  (*** infer simple triggers ***)
   7.157 -
   7.158 -  fun dest_cond_eq ct =
   7.159 -    (case Thm.term_of ct of
   7.160 -      Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
   7.161 -    | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
   7.162 -    | _ => raise CTERM ("no equation", [ct]))
   7.163 -
   7.164 -  fun get_constrs thy (Type (n, _)) = these (Old_Datatype_Data.get_constrs thy n)
   7.165 -    | get_constrs _ _ = []
   7.166 -
   7.167 -  fun is_constr thy (n, T) =
   7.168 -    let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
   7.169 -    in can (the o find_first match o get_constrs thy o Term.body_type) T end
   7.170 -
   7.171 -  fun is_constr_pat thy t =
   7.172 -    (case Term.strip_comb t of
   7.173 -      (Free _, []) => true
   7.174 -    | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
   7.175 -    | _ => false)
   7.176 -
   7.177 -  fun is_simp_lhs ctxt t =
   7.178 -    (case Term.strip_comb t of
   7.179 -      (Const c, ts as _ :: _) =>
   7.180 -        not (Old_SMT_Builtin.is_builtin_fun_ext ctxt c ts) andalso
   7.181 -        forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
   7.182 -    | _ => false)
   7.183 -
   7.184 -  fun has_all_vars vs t =
   7.185 -    subset (op aconv) (vs, map Free (Term.add_frees t []))
   7.186 -
   7.187 -  fun minimal_pats vs ct =
   7.188 -    if has_all_vars vs (Thm.term_of ct) then
   7.189 -      (case Thm.term_of ct of
   7.190 -        _ $ _ =>
   7.191 -          (case apply2 (minimal_pats vs) (Thm.dest_comb ct) of
   7.192 -            ([], []) => [[ct]]
   7.193 -          | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
   7.194 -      | _ => [])
   7.195 -    else []
   7.196 -
   7.197 -  fun proper_mpat _ _ _ [] = false
   7.198 -    | proper_mpat thy gen u cts =
   7.199 -        let
   7.200 -          val tps = (op ~~) (`gen (map Thm.term_of cts))
   7.201 -          fun some_match u = tps |> exists (fn (t', t) =>
   7.202 -            Pattern.matches thy (t', u) andalso not (t aconv u))
   7.203 -        in not (Term.exists_subterm some_match u) end
   7.204 -
   7.205 -  val pat =
   7.206 -    Old_SMT_Utils.mk_const_pat @{theory} @{const_name pat} Old_SMT_Utils.destT1
   7.207 -  fun mk_pat ct = Thm.apply (Old_SMT_Utils.instT' ct pat) ct
   7.208 -
   7.209 -  fun mk_clist T = apply2 (Thm.cterm_of @{context}) (HOLogic.cons_const T, HOLogic.nil_const T)
   7.210 -  fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
   7.211 -  val mk_pat_list = mk_list (mk_clist @{typ pattern})
   7.212 -  val mk_mpat_list = mk_list (mk_clist @{typ "pattern list"})  
   7.213 -  fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
   7.214 -
   7.215 -  val trigger_eq =
   7.216 -    mk_meta_eq @{lemma "p = trigger t p" by (simp add: trigger_def)}
   7.217 -
   7.218 -  fun insert_trigger_conv [] ct = Conv.all_conv ct
   7.219 -    | insert_trigger_conv ctss ct =
   7.220 -        let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
   7.221 -        in Thm.instantiate ([], map (apfst (dest_Var o Thm.term_of)) [cp, (ctr, mk_trigger ctss)]) trigger_eq end
   7.222 -
   7.223 -  fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
   7.224 -    let
   7.225 -      val (lhs, rhs) = dest_cond_eq ct
   7.226 -
   7.227 -      val vs = map Thm.term_of cvs
   7.228 -      val thy = Proof_Context.theory_of ctxt
   7.229 -
   7.230 -      fun get_mpats ct =
   7.231 -        if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
   7.232 -        else []
   7.233 -      val gen = Variable.export_terms ctxt outer_ctxt
   7.234 -      val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
   7.235 -
   7.236 -    in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
   7.237 -
   7.238 -  fun has_trigger (@{const trigger} $ _ $ _) = true
   7.239 -    | has_trigger _ = false
   7.240 -
   7.241 -  fun try_trigger_conv cv ct =
   7.242 -    if Old_SMT_Utils.under_quant has_trigger (Old_SMT_Utils.term_of ct) then
   7.243 -      Conv.all_conv ct
   7.244 -    else Conv.try_conv cv ct
   7.245 -
   7.246 -  fun infer_trigger_conv ctxt =
   7.247 -    if Config.get ctxt Old_SMT_Config.infer_triggers then
   7.248 -      try_trigger_conv
   7.249 -        (Old_SMT_Utils.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
   7.250 -    else Conv.all_conv
   7.251 -in
   7.252 -
   7.253 -fun trigger_conv ctxt =
   7.254 -  Old_SMT_Utils.prop_conv
   7.255 -    (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
   7.256 -
   7.257 -val setup_trigger =
   7.258 -  fold Old_SMT_Builtin.add_builtin_fun_ext''
   7.259 -    [@{const_name pat}, @{const_name nopat}, @{const_name trigger}]
   7.260 -
   7.261 -end
   7.262 -
   7.263 -
   7.264 -(** adding quantifier weights **)
   7.265 -
   7.266 -local
   7.267 -  (*** check weight syntax ***)
   7.268 -
   7.269 -  val has_no_weight =
   7.270 -    not o Term.exists_subterm (fn @{const weight} => true | _ => false)
   7.271 -
   7.272 -  fun is_weight (@{const weight} $ w $ t) =
   7.273 -        (case try HOLogic.dest_number w of
   7.274 -          SOME (_, i) => i >= 0 andalso has_no_weight t
   7.275 -        | _ => false)
   7.276 -    | is_weight t = has_no_weight t
   7.277 -
   7.278 -  fun proper_trigger (@{const trigger} $ _ $ t) = is_weight t
   7.279 -    | proper_trigger t = is_weight t 
   7.280 -
   7.281 -  fun check_weight_error ctxt t =
   7.282 -    error ("SMT weight must be a non-negative number and must only occur " ^
   7.283 -      "under the top-most quantifier and an optional trigger: " ^
   7.284 -      Syntax.string_of_term ctxt t)
   7.285 -
   7.286 -  fun check_weight_conv ctxt ct =
   7.287 -    if Old_SMT_Utils.under_quant proper_trigger (Old_SMT_Utils.term_of ct) then
   7.288 -      Conv.all_conv ct
   7.289 -    else check_weight_error ctxt (Thm.term_of ct)
   7.290 -
   7.291 -
   7.292 -  (*** insertion of weights ***)
   7.293 -
   7.294 -  fun under_trigger_conv cv ct =
   7.295 -    (case Thm.term_of ct of
   7.296 -      @{const trigger} $ _ $ _ => Conv.arg_conv cv
   7.297 -    | _ => cv) ct
   7.298 -
   7.299 -  val weight_eq =
   7.300 -    mk_meta_eq @{lemma "p = weight i p" by (simp add: weight_def)}
   7.301 -  fun mk_weight_eq w =
   7.302 -    let val cv = Thm.dest_arg1 (Thm.rhs_of weight_eq)
   7.303 -    in
   7.304 -      Thm.instantiate ([], [(dest_Var (Thm.term_of cv), Numeral.mk_cnumber @{ctyp int} w)])
   7.305 -        weight_eq
   7.306 -    end
   7.307 -
   7.308 -  fun add_weight_conv NONE _ = Conv.all_conv
   7.309 -    | add_weight_conv (SOME weight) ctxt =
   7.310 -        let val cv = Conv.rewr_conv (mk_weight_eq weight)
   7.311 -        in Old_SMT_Utils.under_quant_conv (K (under_trigger_conv cv)) ctxt end
   7.312 -in
   7.313 -
   7.314 -fun weight_conv weight ctxt = 
   7.315 -  Old_SMT_Utils.prop_conv
   7.316 -    (check_weight_conv ctxt then_conv add_weight_conv weight ctxt)
   7.317 -
   7.318 -val setup_weight = Old_SMT_Builtin.add_builtin_fun_ext'' @{const_name weight}
   7.319 -
   7.320 -end
   7.321 -
   7.322 -
   7.323 -(** combined general normalizations **)
   7.324 -
   7.325 -fun gen_normalize1_conv ctxt weight =
   7.326 -  atomize_conv ctxt then_conv
   7.327 -  unfold_special_quants_conv ctxt then_conv
   7.328 -  Thm.beta_conversion true then_conv
   7.329 -  trigger_conv ctxt then_conv
   7.330 -  weight_conv weight ctxt
   7.331 -
   7.332 -fun gen_normalize1 ctxt weight thm =
   7.333 -  thm
   7.334 -  |> instantiate_elim
   7.335 -  |> norm_def
   7.336 -  |> Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion)
   7.337 -  |> Drule.forall_intr_vars
   7.338 -  |> Conv.fconv_rule (gen_normalize1_conv ctxt weight)
   7.339 -
   7.340 -fun gen_norm1_safe ctxt (i, (weight, thm)) =
   7.341 -  (case try (gen_normalize1 ctxt weight) thm of
   7.342 -    SOME thm' => SOME (i, thm')
   7.343 -  | NONE => (drop_fact_warning ctxt thm; NONE))
   7.344 -
   7.345 -fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
   7.346 -
   7.347 -
   7.348 -
   7.349 -(* unfolding of definitions and theory-specific rewritings *)
   7.350 -
   7.351 -fun expand_head_conv cv ct =
   7.352 -  (case Thm.term_of ct of
   7.353 -    _ $ _ =>
   7.354 -      Conv.fun_conv (expand_head_conv cv) then_conv
   7.355 -      Conv.try_conv (Thm.beta_conversion false)
   7.356 -  | _ => cv) ct
   7.357 -
   7.358 -
   7.359 -(** rewrite bool case expressions as if expressions **)
   7.360 -
   7.361 -local
   7.362 -  fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
   7.363 -    | is_case_bool _ = false
   7.364 -
   7.365 -  val thm = mk_meta_eq @{lemma
   7.366 -    "case_bool = (%x y P. if P then x else y)" by (rule ext)+ simp}
   7.367 -
   7.368 -  fun unfold_conv _ =
   7.369 -    Old_SMT_Utils.if_true_conv (is_case_bool o Term.head_of)
   7.370 -      (expand_head_conv (Conv.rewr_conv thm))
   7.371 -in
   7.372 -
   7.373 -fun rewrite_case_bool_conv ctxt =
   7.374 -  Old_SMT_Utils.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
   7.375 -
   7.376 -val setup_case_bool =
   7.377 -  Old_SMT_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
   7.378 -
   7.379 -end
   7.380 -
   7.381 -
   7.382 -(** unfold abs, min and max **)
   7.383 -
   7.384 -local
   7.385 -  val abs_def = mk_meta_eq @{lemma
   7.386 -    "abs = (%a::'a::abs_if. if a < 0 then - a else a)"
   7.387 -    by (rule ext) (rule abs_if)}
   7.388 -
   7.389 -  val min_def = mk_meta_eq @{lemma "min = (%a b. if a <= b then a else b)"
   7.390 -    by (rule ext)+ (rule min_def)}
   7.391 -
   7.392 -  val max_def = mk_meta_eq  @{lemma "max = (%a b. if a <= b then b else a)"
   7.393 -    by (rule ext)+ (rule max_def)}
   7.394 -
   7.395 -  val defs = [(@{const_name min}, min_def), (@{const_name max}, max_def),
   7.396 -    (@{const_name abs}, abs_def)]
   7.397 -
   7.398 -  fun is_builtinT ctxt T =
   7.399 -    Old_SMT_Builtin.is_builtin_typ_ext ctxt (Term.domain_type T)
   7.400 -
   7.401 -  fun abs_min_max ctxt (Const (n, T)) =
   7.402 -        (case AList.lookup (op =) defs n of
   7.403 -          NONE => NONE
   7.404 -        | SOME thm => if is_builtinT ctxt T then SOME thm else NONE)
   7.405 -    | abs_min_max _ _ = NONE
   7.406 -
   7.407 -  fun unfold_amm_conv ctxt ct =
   7.408 -    (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   7.409 -      SOME thm => expand_head_conv (Conv.rewr_conv thm)
   7.410 -    | NONE => Conv.all_conv) ct
   7.411 -in
   7.412 -
   7.413 -fun unfold_abs_min_max_conv ctxt =
   7.414 -  Old_SMT_Utils.if_exists_conv (is_some o abs_min_max ctxt)
   7.415 -    (Conv.top_conv unfold_amm_conv ctxt)
   7.416 -  
   7.417 -val setup_abs_min_max = fold (Old_SMT_Builtin.add_builtin_fun_ext'' o fst) defs
   7.418 -
   7.419 -end
   7.420 -
   7.421 -
   7.422 -(** embedding of standard natural number operations into integer operations **)
   7.423 -
   7.424 -local
   7.425 -  val nat_embedding = @{lemma
   7.426 -    "ALL n. nat (int n) = n"
   7.427 -    "ALL i. i >= 0 --> int (nat i) = i"
   7.428 -    "ALL i. i < 0 --> int (nat i) = 0"
   7.429 -    by simp_all}
   7.430 -
   7.431 -  val simple_nat_ops = [
   7.432 -    @{const less (nat)}, @{const less_eq (nat)},
   7.433 -    @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   7.434 -
   7.435 -  val mult_nat_ops =
   7.436 -    [@{const times (nat)}, @{const divide (nat)}, @{const modulo (nat)}]
   7.437 -
   7.438 -  val nat_ops = simple_nat_ops @ mult_nat_ops
   7.439 -
   7.440 -  val nat_consts = nat_ops @ [@{const numeral (nat)},
   7.441 -    @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   7.442 -
   7.443 -  val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   7.444 -
   7.445 -  val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   7.446 -
   7.447 -  val is_nat_const = member (op aconv) nat_consts
   7.448 -
   7.449 -  fun is_nat_const' @{const of_nat (int)} = true
   7.450 -    | is_nat_const' t = is_nat_const t
   7.451 -
   7.452 -  val expands = map mk_meta_eq @{lemma
   7.453 -    "0 = nat 0"
   7.454 -    "1 = nat 1"
   7.455 -    "(numeral :: num => nat) = (%i. nat (numeral i))"
   7.456 -    "op < = (%a b. int a < int b)"
   7.457 -    "op <= = (%a b. int a <= int b)"
   7.458 -    "Suc = (%a. nat (int a + 1))"
   7.459 -    "op + = (%a b. nat (int a + int b))"
   7.460 -    "op - = (%a b. nat (int a - int b))"
   7.461 -    "op * = (%a b. nat (int a * int b))"
   7.462 -    "op div = (%a b. nat (int a div int b))"
   7.463 -    "op mod = (%a b. nat (int a mod int b))"
   7.464 -    by (fastforce simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib)+}
   7.465 -
   7.466 -  val ints = map mk_meta_eq @{lemma
   7.467 -    "int 0 = 0"
   7.468 -    "int 1 = 1"
   7.469 -    "int (Suc n) = int n + 1"
   7.470 -    "int (n + m) = int n + int m"
   7.471 -    "int (n - m) = int (nat (int n - int m))"
   7.472 -    "int (n * m) = int n * int m"
   7.473 -    "int (n div m) = int n div int m"
   7.474 -    "int (n mod m) = int n mod int m"
   7.475 -    by (auto simp add: of_nat_mult zdiv_int zmod_int)}
   7.476 -
   7.477 -  val int_if = mk_meta_eq @{lemma
   7.478 -    "int (if P then n else m) = (if P then int n else int m)"
   7.479 -    by simp}
   7.480 -
   7.481 -  fun mk_number_eq ctxt i lhs =
   7.482 -    let
   7.483 -      val eq = Old_SMT_Utils.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   7.484 -      val tac =
   7.485 -        Simplifier.simp_tac (put_simpset HOL_ss ctxt addsimps [@{thm of_nat_numeral}]) 1
   7.486 -    in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
   7.487 -
   7.488 -  fun ite_conv cv1 cv2 =
   7.489 -    Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   7.490 -
   7.491 -  fun int_conv ctxt ct =
   7.492 -    (case Thm.term_of ct of
   7.493 -      @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   7.494 -        Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   7.495 -    | @{const of_nat (int)} $ _ =>
   7.496 -        (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   7.497 -        (Conv.rewr_conv int_if then_conv
   7.498 -          ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   7.499 -        Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   7.500 -    | _ => Conv.no_conv) ct
   7.501 -
   7.502 -  and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   7.503 -
   7.504 -  and expand_conv ctxt =
   7.505 -    Old_SMT_Utils.if_conv (is_nat_const o Term.head_of)
   7.506 -      (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt)
   7.507 -      (int_conv ctxt)
   7.508 -
   7.509 -  and nat_conv ctxt = Old_SMT_Utils.if_exists_conv is_nat_const'
   7.510 -    (Conv.top_sweep_conv expand_conv ctxt)
   7.511 -
   7.512 -  val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   7.513 -in
   7.514 -
   7.515 -val nat_as_int_conv = nat_conv
   7.516 -
   7.517 -fun add_nat_embedding thms =
   7.518 -  if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding)
   7.519 -  else (thms, [])
   7.520 -
   7.521 -val setup_nat_as_int =
   7.522 -  Old_SMT_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   7.523 -  fold (Old_SMT_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   7.524 -
   7.525 -end
   7.526 -
   7.527 -
   7.528 -(** normalize numerals **)
   7.529 -
   7.530 -local
   7.531 -  (*
   7.532 -    rewrite Numeral1 into 1
   7.533 -    rewrite - 0 into 0
   7.534 -  *)
   7.535 -
   7.536 -  fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
   7.537 -        true
   7.538 -    | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
   7.539 -        true
   7.540 -    | is_irregular_number _ =
   7.541 -        false;
   7.542 -
   7.543 -  fun is_strange_number ctxt t = is_irregular_number t andalso Old_SMT_Builtin.is_builtin_num ctxt t;
   7.544 -
   7.545 -  val proper_num_ss =
   7.546 -    simpset_of (put_simpset HOL_ss @{context}
   7.547 -      addsimps @{thms Num.numeral_One minus_zero})
   7.548 -
   7.549 -  fun norm_num_conv ctxt =
   7.550 -    Old_SMT_Utils.if_conv (is_strange_number ctxt)
   7.551 -      (Simplifier.rewrite (put_simpset proper_num_ss ctxt)) Conv.no_conv
   7.552 -in
   7.553 -
   7.554 -fun normalize_numerals_conv ctxt =
   7.555 -  Old_SMT_Utils.if_exists_conv (is_strange_number ctxt)
   7.556 -    (Conv.top_sweep_conv norm_num_conv ctxt)
   7.557 -
   7.558 -end
   7.559 -
   7.560 -
   7.561 -(** combined unfoldings and rewritings **)
   7.562 -
   7.563 -fun unfold_conv ctxt =
   7.564 -  rewrite_case_bool_conv ctxt then_conv
   7.565 -  unfold_abs_min_max_conv ctxt then_conv
   7.566 -  nat_as_int_conv ctxt then_conv
   7.567 -  Thm.beta_conversion true
   7.568 -
   7.569 -fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   7.570 -
   7.571 -fun burrow_ids f ithms =
   7.572 -  let
   7.573 -    val (is, thms) = split_list ithms
   7.574 -    val (thms', extra_thms) = f thms
   7.575 -  in (is ~~ thms') @ map (pair ~1) extra_thms end
   7.576 -
   7.577 -fun unfold2 ctxt ithms =
   7.578 -  ithms
   7.579 -  |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   7.580 -  |> burrow_ids add_nat_embedding
   7.581 -
   7.582 -
   7.583 -
   7.584 -(* overall normalization *)
   7.585 -
   7.586 -type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   7.587 -
   7.588 -structure Extra_Norms = Generic_Data
   7.589 -(
   7.590 -  type T = extra_norm Old_SMT_Utils.dict
   7.591 -  val empty = []
   7.592 -  val extend = I
   7.593 -  fun merge data = Old_SMT_Utils.dict_merge fst data
   7.594 -)
   7.595 -
   7.596 -fun add_extra_norm (cs, norm) =
   7.597 -  Extra_Norms.map (Old_SMT_Utils.dict_update (cs, norm))
   7.598 -
   7.599 -fun apply_extra_norms ctxt ithms =
   7.600 -  let
   7.601 -    val cs = Old_SMT_Config.solver_class_of ctxt
   7.602 -    val es = Old_SMT_Utils.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   7.603 -  in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   7.604 -
   7.605 -local
   7.606 -  val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   7.607 -    @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   7.608 -
   7.609 -  val schematic_consts_of =
   7.610 -    let
   7.611 -      fun collect (@{const trigger} $ p $ t) =
   7.612 -            collect_trigger p #> collect t
   7.613 -        | collect (t $ u) = collect t #> collect u
   7.614 -        | collect (Abs (_, _, t)) = collect t
   7.615 -        | collect (t as Const (n, _)) = 
   7.616 -            if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   7.617 -        | collect _ = I
   7.618 -      and collect_trigger t =
   7.619 -        let val dest = these o try HOLogic.dest_list 
   7.620 -        in fold (fold collect_pat o dest) (dest t) end
   7.621 -      and collect_pat (Const (@{const_name pat}, _) $ t) = collect t
   7.622 -        | collect_pat (Const (@{const_name nopat}, _) $ t) = collect t
   7.623 -        | collect_pat _ = I
   7.624 -    in (fn t => collect t Symtab.empty) end
   7.625 -in
   7.626 -
   7.627 -fun monomorph ctxt xthms =
   7.628 -  let val (xs, thms) = split_list xthms
   7.629 -  in
   7.630 -    map (pair 1) thms
   7.631 -    |> Monomorph.monomorph schematic_consts_of ctxt
   7.632 -    |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   7.633 -  end
   7.634 -
   7.635 -end
   7.636 -
   7.637 -fun normalize iwthms ctxt =
   7.638 -  iwthms
   7.639 -  |> gen_normalize ctxt
   7.640 -  |> unfold1 ctxt
   7.641 -  |> monomorph ctxt
   7.642 -  |> unfold2 ctxt
   7.643 -  |> apply_extra_norms ctxt
   7.644 -  |> rpair ctxt
   7.645 -
   7.646 -val setup = Context.theory_map (
   7.647 -  setup_atomize #>
   7.648 -  setup_unfolded_quants #>
   7.649 -  setup_trigger #>
   7.650 -  setup_weight #>
   7.651 -  setup_case_bool #>
   7.652 -  setup_abs_min_max #>
   7.653 -  setup_nat_as_int)
   7.654 -
   7.655 -end
     8.1 --- a/src/HOL/Library/Old_SMT/old_smt_real.ML	Thu Apr 20 10:45:52 2017 +0200
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,134 +0,0 @@
     8.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_real.ML
     8.5 -    Author:     Sascha Boehme, TU Muenchen
     8.6 -
     8.7 -SMT setup for reals.
     8.8 -*)
     8.9 -
    8.10 -structure Old_SMT_Real: sig end =
    8.11 -struct
    8.12 -
    8.13 -
    8.14 -(* SMT-LIB logic *)
    8.15 -
    8.16 -fun smtlib_logic ts =
    8.17 -  if exists (Term.exists_type (Term.exists_subtype (equal @{typ real}))) ts
    8.18 -  then SOME "AUFLIRA"
    8.19 -  else NONE
    8.20 -
    8.21 -
    8.22 -(* SMT-LIB and Z3 built-ins *)
    8.23 -
    8.24 -local
    8.25 -  fun real_num _ i = SOME (string_of_int i ^ ".0")
    8.26 -
    8.27 -  fun is_linear [t] = Old_SMT_Utils.is_number t
    8.28 -    | is_linear [t, u] = Old_SMT_Utils.is_number t orelse Old_SMT_Utils.is_number u
    8.29 -    | is_linear _ = false
    8.30 -
    8.31 -  fun mk_times ts = Term.list_comb (@{const times (real)}, ts)
    8.32 -
    8.33 -  fun times _ _ ts = if is_linear ts then SOME ("*", 2, ts, mk_times) else NONE
    8.34 -in
    8.35 -
    8.36 -val setup_builtins =
    8.37 -  Old_SMT_Builtin.add_builtin_typ Old_SMTLIB_Interface.smtlibC
    8.38 -    (@{typ real}, K (SOME "Real"), real_num) #>
    8.39 -  fold (Old_SMT_Builtin.add_builtin_fun' Old_SMTLIB_Interface.smtlibC) [
    8.40 -    (@{const less (real)}, "<"),
    8.41 -    (@{const less_eq (real)}, "<="),
    8.42 -    (@{const uminus (real)}, "~"),
    8.43 -    (@{const plus (real)}, "+"),
    8.44 -    (@{const minus (real)}, "-") ] #>
    8.45 -  Old_SMT_Builtin.add_builtin_fun Old_SMTLIB_Interface.smtlibC
    8.46 -    (Term.dest_Const @{const times (real)}, times) #>
    8.47 -  Old_SMT_Builtin.add_builtin_fun' Old_Z3_Interface.smtlib_z3C
    8.48 -    (@{const times (real)}, "*") #>
    8.49 -  Old_SMT_Builtin.add_builtin_fun' Old_Z3_Interface.smtlib_z3C
    8.50 -    (@{const divide (real)}, "/")
    8.51 -
    8.52 -end
    8.53 -
    8.54 -
    8.55 -(* Z3 constructors *)
    8.56 -
    8.57 -local
    8.58 -  fun z3_mk_builtin_typ (Old_Z3_Interface.Sym ("Real", _)) = SOME @{typ real}
    8.59 -    | z3_mk_builtin_typ (Old_Z3_Interface.Sym ("real", _)) = SOME @{typ real}
    8.60 -        (*FIXME: delete*)
    8.61 -    | z3_mk_builtin_typ _ = NONE
    8.62 -
    8.63 -  fun z3_mk_builtin_num _ i T =
    8.64 -    if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
    8.65 -    else NONE
    8.66 -
    8.67 -  fun mk_nary _ cu [] = cu
    8.68 -    | mk_nary ct _ cts = uncurry (fold_rev (Thm.mk_binop ct)) (split_last cts)
    8.69 -
    8.70 -  val mk_uminus = Thm.apply (Thm.cterm_of @{context} @{const uminus (real)})
    8.71 -  val add = Thm.cterm_of @{context} @{const plus (real)}
    8.72 -  val real0 = Numeral.mk_cnumber @{ctyp real} 0
    8.73 -  val mk_sub = Thm.mk_binop (Thm.cterm_of @{context} @{const minus (real)})
    8.74 -  val mk_mul = Thm.mk_binop (Thm.cterm_of @{context} @{const times (real)})
    8.75 -  val mk_div = Thm.mk_binop (Thm.cterm_of @{context} @{const divide (real)})
    8.76 -  val mk_lt = Thm.mk_binop (Thm.cterm_of @{context} @{const less (real)})
    8.77 -  val mk_le = Thm.mk_binop (Thm.cterm_of @{context} @{const less_eq (real)})
    8.78 -
    8.79 -  fun z3_mk_builtin_fun (Old_Z3_Interface.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
    8.80 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("+", _)) cts =
    8.81 -        SOME (mk_nary add real0 cts)
    8.82 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("-", _)) [ct, cu] =
    8.83 -        SOME (mk_sub ct cu)
    8.84 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("*", _)) [ct, cu] =
    8.85 -        SOME (mk_mul ct cu)
    8.86 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("/", _)) [ct, cu] =
    8.87 -        SOME (mk_div ct cu)
    8.88 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("<", _)) [ct, cu] =
    8.89 -        SOME (mk_lt ct cu)
    8.90 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("<=", _)) [ct, cu] =
    8.91 -        SOME (mk_le ct cu)
    8.92 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym (">", _)) [ct, cu] =
    8.93 -        SOME (mk_lt cu ct)
    8.94 -    | z3_mk_builtin_fun (Old_Z3_Interface.Sym (">=", _)) [ct, cu] =
    8.95 -        SOME (mk_le cu ct)
    8.96 -    | z3_mk_builtin_fun _ _ = NONE
    8.97 -in
    8.98 -
    8.99 -val z3_mk_builtins = {
   8.100 -  mk_builtin_typ = z3_mk_builtin_typ,
   8.101 -  mk_builtin_num = z3_mk_builtin_num,
   8.102 -  mk_builtin_fun = (fn _ => fn sym => fn cts =>
   8.103 -    (case try (Thm.typ_of_cterm o hd) cts of
   8.104 -      SOME @{typ real} => z3_mk_builtin_fun sym cts
   8.105 -    | _ => NONE)) }
   8.106 -
   8.107 -end
   8.108 -
   8.109 -
   8.110 -(* Z3 proof reconstruction *)
   8.111 -
   8.112 -val real_rules = @{lemma
   8.113 -  "0 + (x::real) = x"
   8.114 -  "x + 0 = x"
   8.115 -  "0 * x = 0"
   8.116 -  "1 * x = x"
   8.117 -  "x + y = y + x"
   8.118 -  by auto}
   8.119 -
   8.120 -val real_linarith_proc =
   8.121 -  Simplifier.make_simproc @{context} "fast_real_arith"
   8.122 -   {lhss = [@{term "(m::real) < n"}, @{term "(m::real) \<le> n"}, @{term "(m::real) = n"}],
   8.123 -    proc = K Lin_Arith.simproc}
   8.124 -
   8.125 -
   8.126 -(* setup *)
   8.127 -
   8.128 -val _ =
   8.129 -  Theory.setup
   8.130 -   (Context.theory_map (
   8.131 -      Old_SMTLIB_Interface.add_logic (10, smtlib_logic) #>
   8.132 -      setup_builtins #>
   8.133 -      Old_Z3_Interface.add_mk_builtins z3_mk_builtins #>
   8.134 -      fold Old_Z3_Proof_Reconstruction.add_z3_rule real_rules #>
   8.135 -      Old_Z3_Proof_Tools.add_simproc real_linarith_proc))
   8.136 -
   8.137 -end
     9.1 --- a/src/HOL/Library/Old_SMT/old_smt_setup_solvers.ML	Thu Apr 20 10:45:52 2017 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,189 +0,0 @@
     9.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_setup_solvers.ML
     9.5 -    Author:     Sascha Boehme, TU Muenchen
     9.6 -
     9.7 -Setup SMT solvers.
     9.8 -*)
     9.9 -
    9.10 -signature OLD_SMT_SETUP_SOLVERS =
    9.11 -sig
    9.12 -  datatype z3_non_commercial =
    9.13 -    Z3_Non_Commercial_Unknown |
    9.14 -    Z3_Non_Commercial_Accepted |
    9.15 -    Z3_Non_Commercial_Declined
    9.16 -  val z3_non_commercial: unit -> z3_non_commercial
    9.17 -  val z3_with_extensions: bool Config.T
    9.18 -  val setup: theory -> theory
    9.19 -end
    9.20 -
    9.21 -structure Old_SMT_Setup_Solvers: OLD_SMT_SETUP_SOLVERS =
    9.22 -struct
    9.23 -
    9.24 -(* helper functions *)
    9.25 -
    9.26 -fun make_avail name () = getenv ("OLD_" ^ name ^ "_SOLVER") <> ""
    9.27 -fun make_command name () = [getenv ("OLD_" ^ name ^ "_SOLVER")]
    9.28 -
    9.29 -fun outcome_of unsat sat unknown solver_name line =
    9.30 -  if String.isPrefix unsat line then Old_SMT_Solver.Unsat
    9.31 -  else if String.isPrefix sat line then Old_SMT_Solver.Sat
    9.32 -  else if String.isPrefix unknown line then Old_SMT_Solver.Unknown
    9.33 -  else raise Old_SMT_Failure.SMT (Old_SMT_Failure.Other_Failure ("Solver " ^
    9.34 -    quote solver_name ^ " failed. Enable SMT tracing by setting the " ^
    9.35 -    "configuration option " ^ quote (Config.name_of Old_SMT_Config.trace) ^ " and " ^
    9.36 -    "see the trace for details."))
    9.37 -
    9.38 -fun on_first_line test_outcome solver_name lines =
    9.39 -  let
    9.40 -    val empty_line = (fn "" => true | _ => false)
    9.41 -    val split_first = (fn [] => ("", []) | l :: ls => (l, ls))
    9.42 -    val (l, ls) = split_first (snd (take_prefix empty_line lines))
    9.43 -  in (test_outcome solver_name l, ls) end
    9.44 -
    9.45 -
    9.46 -(* CVC3 *)
    9.47 -
    9.48 -local
    9.49 -  fun cvc3_options ctxt = [
    9.50 -    "-seed", string_of_int (Config.get ctxt Old_SMT_Config.random_seed),
    9.51 -    "-lang", "smtlib", "-output-lang", "presentation",
    9.52 -    "-timeout", string_of_int (Real.ceil (Config.get ctxt Old_SMT_Config.timeout))]
    9.53 -in
    9.54 -
    9.55 -val cvc3: Old_SMT_Solver.solver_config = {
    9.56 -  name = "cvc3",
    9.57 -  class = K Old_SMTLIB_Interface.smtlibC,
    9.58 -  avail = make_avail "CVC3",
    9.59 -  command = make_command "CVC3",
    9.60 -  options = cvc3_options,
    9.61 -  default_max_relevant = 400 (* FUDGE *),
    9.62 -  supports_filter = false,
    9.63 -  outcome =
    9.64 -    on_first_line (outcome_of "Unsatisfiable." "Satisfiable." "Unknown."),
    9.65 -  cex_parser = NONE,
    9.66 -  reconstruct = NONE }
    9.67 -
    9.68 -end
    9.69 -
    9.70 -
    9.71 -(* Yices *)
    9.72 -
    9.73 -val yices: Old_SMT_Solver.solver_config = {
    9.74 -  name = "yices",
    9.75 -  class = K Old_SMTLIB_Interface.smtlibC,
    9.76 -  avail = make_avail "YICES",
    9.77 -  command = make_command "YICES",
    9.78 -  options = (fn ctxt => [
    9.79 -    "--rand-seed=" ^ string_of_int (Config.get ctxt Old_SMT_Config.random_seed),
    9.80 -    "--timeout=" ^
    9.81 -      string_of_int (Real.ceil (Config.get ctxt Old_SMT_Config.timeout)),
    9.82 -    "--smtlib"]),
    9.83 -  default_max_relevant = 350 (* FUDGE *),
    9.84 -  supports_filter = false,
    9.85 -  outcome = on_first_line (outcome_of "unsat" "sat" "unknown"),
    9.86 -  cex_parser = NONE,
    9.87 -  reconstruct = NONE }
    9.88 -
    9.89 -
    9.90 -(* Z3 *)
    9.91 -
    9.92 -datatype z3_non_commercial =
    9.93 -  Z3_Non_Commercial_Unknown |
    9.94 -  Z3_Non_Commercial_Accepted |
    9.95 -  Z3_Non_Commercial_Declined
    9.96 -
    9.97 -
    9.98 -local
    9.99 -  val accepted = member (op =) ["yes", "Yes", "YES"]
   9.100 -  val declined = member (op =) ["no", "No", "NO"]
   9.101 -in
   9.102 -
   9.103 -fun z3_non_commercial () =
   9.104 -  let
   9.105 -    val flag2 = getenv "OLD_Z3_NON_COMMERCIAL"
   9.106 -  in
   9.107 -    if accepted flag2 then Z3_Non_Commercial_Accepted
   9.108 -    else if declined flag2 then Z3_Non_Commercial_Declined
   9.109 -    else Z3_Non_Commercial_Unknown
   9.110 -  end
   9.111 -
   9.112 -fun if_z3_non_commercial f =
   9.113 -  (case z3_non_commercial () of
   9.114 -    Z3_Non_Commercial_Accepted => f ()
   9.115 -  | Z3_Non_Commercial_Declined =>
   9.116 -      error (Pretty.string_of (Pretty.para
   9.117 -        "The SMT solver Z3 may only be used for non-commercial applications."))
   9.118 -  | Z3_Non_Commercial_Unknown =>
   9.119 -      error
   9.120 -        (Pretty.string_of
   9.121 -          (Pretty.para
   9.122 -            ("The SMT solver Z3 is not activated. To activate it, set the Isabelle \
   9.123 -             \system option \"z3_non_commercial\" to \"yes\" (e.g. via \
   9.124 -             \the Isabelle/jEdit menu Plugin Options / Isabelle / General).")) ^
   9.125 -        "\n\nSee also " ^ Url.print (Url.explode "http://z3.codeplex.com/license")))
   9.126 -
   9.127 -end
   9.128 -
   9.129 -
   9.130 -val z3_with_extensions =
   9.131 -  Attrib.setup_config_bool @{binding old_z3_with_extensions} (K false)
   9.132 -
   9.133 -local
   9.134 -  fun z3_make_command name () = if_z3_non_commercial (make_command name)
   9.135 -
   9.136 -  fun z3_options ctxt =
   9.137 -    ["-rs:" ^ string_of_int (Config.get ctxt Old_SMT_Config.random_seed),
   9.138 -      "MODEL=true",
   9.139 -      "SOFT_TIMEOUT=" ^
   9.140 -        string_of_int (Real.ceil (1000.0 * Config.get ctxt Old_SMT_Config.timeout)),
   9.141 -      "-smt"]
   9.142 -    |> not (Config.get ctxt Old_SMT_Config.oracle) ?
   9.143 -         append ["DISPLAY_PROOF=true", "PROOF_MODE=2"]
   9.144 -
   9.145 -  fun z3_on_first_or_last_line solver_name lines =
   9.146 -    let
   9.147 -      fun junk l =
   9.148 -        if String.isPrefix "WARNING: Out of allocated virtual memory" l
   9.149 -        then raise Old_SMT_Failure.SMT Old_SMT_Failure.Out_Of_Memory
   9.150 -        else
   9.151 -          String.isPrefix "WARNING" l orelse
   9.152 -          String.isPrefix "ERROR" l orelse
   9.153 -          forall Symbol.is_ascii_blank (Symbol.explode l)
   9.154 -      val lines = filter_out junk lines
   9.155 -      fun outcome split =
   9.156 -        the_default ("", []) (try split lines)
   9.157 -        |>> outcome_of "unsat" "sat" "unknown" solver_name
   9.158 -    in
   9.159 -      (* Starting with version 4.0, Z3 puts the outcome on the first line of the
   9.160 -         output rather than on the last line. *)
   9.161 -      outcome (fn lines => (hd lines, tl lines))
   9.162 -      handle Old_SMT_Failure.SMT _ => outcome (swap o split_last)
   9.163 -    end
   9.164 -
   9.165 -  fun select_class ctxt =
   9.166 -    if Config.get ctxt z3_with_extensions then Old_Z3_Interface.smtlib_z3C
   9.167 -    else Old_SMTLIB_Interface.smtlibC
   9.168 -in
   9.169 -
   9.170 -val z3: Old_SMT_Solver.solver_config = {
   9.171 -  name = "z3",
   9.172 -  class = select_class,
   9.173 -  avail = make_avail "Z3",
   9.174 -  command = z3_make_command "Z3",
   9.175 -  options = z3_options,
   9.176 -  default_max_relevant = 350 (* FUDGE *),
   9.177 -  supports_filter = true,
   9.178 -  outcome = z3_on_first_or_last_line,
   9.179 -  cex_parser = SOME Old_Z3_Model.parse_counterex,
   9.180 -  reconstruct = SOME Old_Z3_Proof_Reconstruction.reconstruct }
   9.181 -
   9.182 -end
   9.183 -
   9.184 -
   9.185 -(* overall setup *)
   9.186 -
   9.187 -val setup =
   9.188 -  Old_SMT_Solver.add_solver cvc3 #>
   9.189 -  Old_SMT_Solver.add_solver yices #>
   9.190 -  Old_SMT_Solver.add_solver z3
   9.191 -
   9.192 -end
    10.1 --- a/src/HOL/Library/Old_SMT/old_smt_solver.ML	Thu Apr 20 10:45:52 2017 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,374 +0,0 @@
    10.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_solver.ML
    10.5 -    Author:     Sascha Boehme, TU Muenchen
    10.6 -
    10.7 -SMT solvers registry and SMT tactic.
    10.8 -*)
    10.9 -
   10.10 -signature OLD_SMT_SOLVER =
   10.11 -sig
   10.12 -  (*configuration*)
   10.13 -  datatype outcome = Unsat | Sat | Unknown
   10.14 -  type solver_config = {
   10.15 -    name: string,
   10.16 -    class: Proof.context -> Old_SMT_Utils.class,
   10.17 -    avail: unit -> bool,
   10.18 -    command: unit -> string list,
   10.19 -    options: Proof.context -> string list,
   10.20 -    default_max_relevant: int,
   10.21 -    supports_filter: bool,
   10.22 -    outcome: string -> string list -> outcome * string list,
   10.23 -    cex_parser: (Proof.context -> Old_SMT_Translate.recon -> string list ->
   10.24 -      term list * term list) option,
   10.25 -    reconstruct: (Proof.context -> Old_SMT_Translate.recon -> string list ->
   10.26 -      int list * thm) option }
   10.27 -
   10.28 -  (*registry*)
   10.29 -  val add_solver: solver_config -> theory -> theory
   10.30 -  val solver_name_of: Proof.context -> string
   10.31 -  val available_solvers_of: Proof.context -> string list
   10.32 -  val apply_solver: Proof.context -> (int * (int option * thm)) list ->
   10.33 -    int list * thm
   10.34 -  val default_max_relevant: Proof.context -> string -> int
   10.35 -
   10.36 -  (*filter*)
   10.37 -  type 'a smt_filter_data =
   10.38 -    ('a * thm) list * ((int * thm) list * Proof.context)
   10.39 -  val smt_filter_preprocess: Proof.context -> thm list -> thm ->
   10.40 -    ('a * (int option * thm)) list -> int -> 'a smt_filter_data
   10.41 -  val smt_filter_apply: Time.time -> 'a smt_filter_data ->
   10.42 -    {outcome: Old_SMT_Failure.failure option, used_facts: ('a * thm) list}
   10.43 -
   10.44 -  (*tactic*)
   10.45 -  val smt_tac: Proof.context -> thm list -> int -> tactic
   10.46 -  val smt_tac': Proof.context -> thm list -> int -> tactic
   10.47 -end
   10.48 -
   10.49 -structure Old_SMT_Solver: OLD_SMT_SOLVER =
   10.50 -struct
   10.51 -
   10.52 -
   10.53 -(* interface to external solvers *)
   10.54 -
   10.55 -local
   10.56 -
   10.57 -fun make_cmd command options problem_path proof_path =
   10.58 -  space_implode " "
   10.59 -    ("(exec 2>&1;" :: map Bash.string (command () @ options) @
   10.60 -      [File.bash_path problem_path, ")", ">", File.bash_path proof_path])
   10.61 -
   10.62 -fun trace_and ctxt msg f x =
   10.63 -  let val _ = Old_SMT_Config.trace_msg ctxt (fn () => msg) ()
   10.64 -  in f x end
   10.65 -
   10.66 -fun run ctxt name mk_cmd input =
   10.67 -  (case Old_SMT_Config.certificates_of ctxt of
   10.68 -    NONE =>
   10.69 -      if not (Old_SMT_Config.is_available ctxt name) then
   10.70 -        error ("The SMT solver " ^ quote name ^ " is not installed.")
   10.71 -      else if Config.get ctxt Old_SMT_Config.debug_files = "" then
   10.72 -        trace_and ctxt ("Invoking SMT solver " ^ quote name ^ " ...")
   10.73 -          (Cache_IO.run mk_cmd) input
   10.74 -      else
   10.75 -        let
   10.76 -          val base_path = Path.explode (Config.get ctxt Old_SMT_Config.debug_files)
   10.77 -          val in_path = Path.ext "smt_in" base_path
   10.78 -          val out_path = Path.ext "smt_out" base_path
   10.79 -        in Cache_IO.raw_run mk_cmd input in_path out_path end
   10.80 -  | SOME certs =>
   10.81 -      (case Cache_IO.lookup certs input of
   10.82 -        (NONE, key) =>
   10.83 -          if Config.get ctxt Old_SMT_Config.read_only_certificates then
   10.84 -            error ("Bad certificate cache: missing certificate")
   10.85 -          else
   10.86 -            Cache_IO.run_and_cache certs key mk_cmd input
   10.87 -      | (SOME output, _) =>
   10.88 -          trace_and ctxt ("Using cached certificate from " ^
   10.89 -            Path.print (Cache_IO.cache_path_of certs) ^ " ...")
   10.90 -            I output))
   10.91 -
   10.92 -fun run_solver ctxt name mk_cmd input =
   10.93 -  let
   10.94 -    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag
   10.95 -      (map Pretty.str ls))
   10.96 -
   10.97 -    val _ = Old_SMT_Config.trace_msg ctxt (pretty "Problem:" o split_lines) input
   10.98 -
   10.99 -    val {redirected_output=res, output=err, return_code} =
  10.100 -      Old_SMT_Config.with_timeout ctxt (run ctxt name mk_cmd) input
  10.101 -    val _ = Old_SMT_Config.trace_msg ctxt (pretty "Solver:") err
  10.102 -
  10.103 -    val ls = fst (take_suffix (equal "") res)
  10.104 -    val _ = Old_SMT_Config.trace_msg ctxt (pretty "Result:") ls
  10.105 -
  10.106 -    val _ = return_code <> 0 andalso
  10.107 -      raise Old_SMT_Failure.SMT (Old_SMT_Failure.Abnormal_Termination return_code)
  10.108 -  in ls end
  10.109 -
  10.110 -fun trace_assms ctxt =
  10.111 -  Old_SMT_Config.trace_msg ctxt (Pretty.string_of o
  10.112 -    Pretty.big_list "Assertions:" o map (Thm.pretty_thm ctxt o snd))
  10.113 -
  10.114 -fun trace_recon_data ({context=ctxt, typs, terms, ...} : Old_SMT_Translate.recon) =
  10.115 -  let
  10.116 -    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
  10.117 -    fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
  10.118 -    fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
  10.119 -  in
  10.120 -    Old_SMT_Config.trace_msg ctxt (fn () =>
  10.121 -      Pretty.string_of (Pretty.big_list "Names:" [
  10.122 -        Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)),
  10.123 -        Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) ()
  10.124 -  end
  10.125 -
  10.126 -in
  10.127 -
  10.128 -fun invoke name command ithms ctxt =
  10.129 -  let
  10.130 -    val options = Old_SMT_Config.solver_options_of ctxt
  10.131 -    val comments = ("solver: " ^ name) ::
  10.132 -      ("timeout: " ^ string_of_real (Config.get ctxt Old_SMT_Config.timeout)) ::
  10.133 -      ("random seed: " ^
  10.134 -        string_of_int (Config.get ctxt Old_SMT_Config.random_seed)) ::
  10.135 -      "arguments:" :: options
  10.136 -
  10.137 -    val (str, recon as {context=ctxt', ...}) =
  10.138 -      ithms
  10.139 -      |> tap (trace_assms ctxt)
  10.140 -      |> Old_SMT_Translate.translate ctxt comments
  10.141 -      ||> tap trace_recon_data
  10.142 -  in (run_solver ctxt' name (make_cmd command options) str, recon) end
  10.143 -
  10.144 -end
  10.145 -
  10.146 -
  10.147 -(* configuration *)
  10.148 -
  10.149 -datatype outcome = Unsat | Sat | Unknown
  10.150 -
  10.151 -type solver_config = {
  10.152 -  name: string,
  10.153 -  class: Proof.context -> Old_SMT_Utils.class,
  10.154 -  avail: unit -> bool,
  10.155 -  command: unit -> string list,
  10.156 -  options: Proof.context -> string list,
  10.157 -  default_max_relevant: int,
  10.158 -  supports_filter: bool,
  10.159 -  outcome: string -> string list -> outcome * string list,
  10.160 -  cex_parser: (Proof.context -> Old_SMT_Translate.recon -> string list ->
  10.161 -    term list * term list) option,
  10.162 -  reconstruct: (Proof.context -> Old_SMT_Translate.recon -> string list ->
  10.163 -    int list * thm) option }
  10.164 -
  10.165 -
  10.166 -(* registry *)
  10.167 -
  10.168 -type solver_info = {
  10.169 -  command: unit -> string list,
  10.170 -  default_max_relevant: int,
  10.171 -  supports_filter: bool,
  10.172 -  reconstruct: Proof.context -> string list * Old_SMT_Translate.recon ->
  10.173 -    int list * thm }
  10.174 -
  10.175 -structure Solvers = Generic_Data
  10.176 -(
  10.177 -  type T = solver_info Symtab.table
  10.178 -  val empty = Symtab.empty
  10.179 -  val extend = I
  10.180 -  fun merge data = Symtab.merge (K true) data
  10.181 -)
  10.182 -
  10.183 -local
  10.184 -  fun finish outcome cex_parser reconstruct ocl outer_ctxt
  10.185 -      (output, (recon as {context=ctxt, ...} : Old_SMT_Translate.recon)) =
  10.186 -    (case outcome output of
  10.187 -      (Unsat, ls) =>
  10.188 -        if not (Config.get ctxt Old_SMT_Config.oracle) andalso is_some reconstruct
  10.189 -        then the reconstruct outer_ctxt recon ls
  10.190 -        else ([], ocl ())
  10.191 -    | (result, ls) =>
  10.192 -        let
  10.193 -          val (ts, us) =
  10.194 -            (case cex_parser of SOME f => f ctxt recon ls | _ => ([], []))
  10.195 -         in
  10.196 -          raise Old_SMT_Failure.SMT (Old_SMT_Failure.Counterexample {
  10.197 -            is_real_cex = (result = Sat),
  10.198 -            free_constraints = ts,
  10.199 -            const_defs = us})
  10.200 -        end)
  10.201 -
  10.202 -  val cfalse = Thm.cterm_of @{context} (@{const Trueprop} $ @{const False})
  10.203 -in
  10.204 -
  10.205 -fun add_solver cfg =
  10.206 -  let
  10.207 -    val {name, class, avail, command, options, default_max_relevant,
  10.208 -      supports_filter, outcome, cex_parser, reconstruct} = cfg
  10.209 -
  10.210 -    fun core_oracle () = cfalse
  10.211 -
  10.212 -    fun solver ocl = {
  10.213 -      command = command,
  10.214 -      default_max_relevant = default_max_relevant,
  10.215 -      supports_filter = supports_filter,
  10.216 -      reconstruct = finish (outcome name) cex_parser reconstruct ocl }
  10.217 -
  10.218 -    val info = {name=name, class=class, avail=avail, options=options}
  10.219 -  in
  10.220 -    Thm.add_oracle (Binding.name name, core_oracle) #-> (fn (_, ocl) =>
  10.221 -    Context.theory_map (Solvers.map (Symtab.update_new (name, solver ocl)))) #>
  10.222 -    Context.theory_map (Old_SMT_Config.add_solver info)
  10.223 -  end
  10.224 -
  10.225 -end
  10.226 -
  10.227 -fun get_info ctxt name =
  10.228 -  the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
  10.229 -
  10.230 -val solver_name_of = Old_SMT_Config.solver_of
  10.231 -
  10.232 -val available_solvers_of = Old_SMT_Config.available_solvers_of
  10.233 -
  10.234 -fun name_and_info_of ctxt =
  10.235 -  let val name = solver_name_of ctxt
  10.236 -  in (name, get_info ctxt name) end
  10.237 -
  10.238 -fun gen_preprocess ctxt iwthms = Old_SMT_Normalize.normalize iwthms ctxt
  10.239 -
  10.240 -fun gen_apply (ithms, ctxt) =
  10.241 -  let val (name, {command, reconstruct, ...}) = name_and_info_of ctxt
  10.242 -  in
  10.243 -    (ithms, ctxt)
  10.244 -    |-> invoke name command
  10.245 -    |> reconstruct ctxt
  10.246 -    |>> distinct (op =)
  10.247 -  end
  10.248 -
  10.249 -fun apply_solver ctxt = gen_apply o gen_preprocess ctxt
  10.250 -
  10.251 -val default_max_relevant = #default_max_relevant oo get_info
  10.252 -
  10.253 -val supports_filter = #supports_filter o snd o name_and_info_of 
  10.254 -
  10.255 -
  10.256 -(* check well-sortedness *)
  10.257 -
  10.258 -val has_topsort = Term.exists_type (Term.exists_subtype (fn
  10.259 -    TFree (_, []) => true
  10.260 -  | TVar (_, []) => true
  10.261 -  | _ => false))
  10.262 -
  10.263 -(* without this test, we would run into problems when atomizing the rules: *)
  10.264 -fun check_topsort ctxt thm =
  10.265 -  if has_topsort (Thm.prop_of thm) then
  10.266 -    (Old_SMT_Normalize.drop_fact_warning ctxt thm; TrueI)
  10.267 -  else
  10.268 -    thm
  10.269 -
  10.270 -fun check_topsorts ctxt iwthms = map (apsnd (apsnd (check_topsort ctxt))) iwthms
  10.271 -
  10.272 -
  10.273 -(* filter *)
  10.274 -
  10.275 -val cnot = Thm.cterm_of @{context} @{const Not}
  10.276 -
  10.277 -fun mk_result outcome xrules = { outcome = outcome, used_facts = xrules }
  10.278 -
  10.279 -type 'a smt_filter_data = ('a * thm) list * ((int * thm) list * Proof.context)
  10.280 -
  10.281 -fun smt_filter_preprocess ctxt facts goal xwthms i =
  10.282 -  let
  10.283 -    val ctxt =
  10.284 -      ctxt
  10.285 -      |> Config.put Old_SMT_Config.oracle false
  10.286 -      |> Config.put Old_SMT_Config.filter_only_facts true
  10.287 -
  10.288 -    val ({context=ctxt', prems, concl, ...}, _) = Subgoal.focus ctxt i NONE goal
  10.289 -    fun negate ct = Thm.dest_comb ct ||> Thm.apply cnot |-> Thm.apply
  10.290 -    val cprop =
  10.291 -      (case try negate (Thm.rhs_of (Old_SMT_Normalize.atomize_conv ctxt' concl)) of
  10.292 -        SOME ct => ct
  10.293 -      | NONE => raise Old_SMT_Failure.SMT (Old_SMT_Failure.Other_Failure (
  10.294 -          "goal is not a HOL term")))
  10.295 -  in
  10.296 -    map snd xwthms
  10.297 -    |> map_index I
  10.298 -    |> append (map (pair ~1 o pair NONE) (Thm.assume cprop :: prems @ facts))
  10.299 -    |> check_topsorts ctxt'
  10.300 -    |> gen_preprocess ctxt'
  10.301 -    |> pair (map (apsnd snd) xwthms)
  10.302 -  end
  10.303 -
  10.304 -fun smt_filter_apply time_limit (xthms, (ithms, ctxt)) =
  10.305 -  let
  10.306 -    val ctxt' =
  10.307 -      ctxt
  10.308 -      |> Config.put Old_SMT_Config.timeout (Time.toReal time_limit)
  10.309 -
  10.310 -    fun filter_thms false = K xthms
  10.311 -      | filter_thms true = map_filter (try (nth xthms)) o fst
  10.312 -  in
  10.313 -    (ithms, ctxt')
  10.314 -    |> gen_apply
  10.315 -    |> filter_thms (supports_filter ctxt')
  10.316 -    |> mk_result NONE
  10.317 -  end
  10.318 -  handle Old_SMT_Failure.SMT fail => mk_result (SOME fail) []
  10.319 -
  10.320 -
  10.321 -(* SMT tactic *)
  10.322 -
  10.323 -local
  10.324 -  fun trace_assumptions ctxt iwthms idxs =
  10.325 -    let
  10.326 -      val wthms =
  10.327 -        idxs
  10.328 -        |> filter (fn i => i >= 0)
  10.329 -        |> map_filter (AList.lookup (op =) iwthms)
  10.330 -    in
  10.331 -      if Config.get ctxt Old_SMT_Config.trace_used_facts andalso length wthms > 0
  10.332 -      then
  10.333 -        tracing (Pretty.string_of (Pretty.big_list "SMT used facts:"
  10.334 -          (map (Thm.pretty_thm ctxt o snd) wthms)))
  10.335 -      else ()
  10.336 -    end
  10.337 -
  10.338 -  fun solve ctxt iwthms =
  10.339 -    iwthms
  10.340 -    |> check_topsorts ctxt
  10.341 -    |> apply_solver ctxt
  10.342 -    |>> trace_assumptions ctxt iwthms
  10.343 -    |> snd
  10.344 -
  10.345 -  fun str_of ctxt fail =
  10.346 -    Old_SMT_Failure.string_of_failure ctxt fail
  10.347 -    |> prefix ("Solver " ^ Old_SMT_Config.solver_of ctxt ^ ": ")
  10.348 -
  10.349 -  fun safe_solve ctxt iwthms = SOME (solve ctxt iwthms)
  10.350 -    handle
  10.351 -      Old_SMT_Failure.SMT (fail as Old_SMT_Failure.Counterexample _) =>
  10.352 -        (Old_SMT_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
  10.353 -    | Old_SMT_Failure.SMT (fail as Old_SMT_Failure.Time_Out) =>
  10.354 -        error ("SMT: Solver " ^ quote (Old_SMT_Config.solver_of ctxt) ^ ": " ^
  10.355 -          Old_SMT_Failure.string_of_failure ctxt fail ^ " (setting the " ^
  10.356 -          "configuration option " ^ quote (Config.name_of Old_SMT_Config.timeout) ^ " might help)")
  10.357 -    | Old_SMT_Failure.SMT fail => error (str_of ctxt fail)
  10.358 -
  10.359 -  fun tag_rules thms = map_index (apsnd (pair NONE)) thms
  10.360 -  fun tag_prems thms = map (pair ~1 o pair NONE) thms
  10.361 -
  10.362 -  fun resolve ctxt (SOME thm) = resolve_tac ctxt [thm] 1
  10.363 -    | resolve _ NONE = no_tac
  10.364 -
  10.365 -  fun tac prove ctxt rules =
  10.366 -    CONVERSION (Old_SMT_Normalize.atomize_conv ctxt)
  10.367 -    THEN' resolve_tac ctxt @{thms ccontr}
  10.368 -    THEN' SUBPROOF (fn {context = ctxt', prems, ...} =>
  10.369 -      resolve ctxt' (prove ctxt' (tag_rules rules @ tag_prems prems))) ctxt
  10.370 -in
  10.371 -
  10.372 -val smt_tac = tac safe_solve
  10.373 -val smt_tac' = tac (SOME oo solve)
  10.374 -
  10.375 -end
  10.376 -
  10.377 -end
    11.1 --- a/src/HOL/Library/Old_SMT/old_smt_translate.ML	Thu Apr 20 10:45:52 2017 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,589 +0,0 @@
    11.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_translate.ML
    11.5 -    Author:     Sascha Boehme, TU Muenchen
    11.6 -
    11.7 -Translate theorems into an SMT intermediate format and serialize them.
    11.8 -*)
    11.9 -
   11.10 -signature OLD_SMT_TRANSLATE =
   11.11 -sig
   11.12 -  (*intermediate term structure*)
   11.13 -  datatype squant = SForall | SExists
   11.14 -  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
   11.15 -  datatype sterm =
   11.16 -    SVar of int |
   11.17 -    SApp of string * sterm list |
   11.18 -    SLet of string * sterm * sterm |
   11.19 -    SQua of squant * string list * sterm spattern list * int option * sterm
   11.20 -
   11.21 -  (*translation configuration*)
   11.22 -  type prefixes = {sort_prefix: string, func_prefix: string}
   11.23 -  type sign = {
   11.24 -    header: string list,
   11.25 -    sorts: string list,
   11.26 -    dtyps: (string * (string * (string * string) list) list) list list,
   11.27 -    funcs: (string * (string list * string)) list }
   11.28 -  type config = {
   11.29 -    prefixes: prefixes,
   11.30 -    header: term list -> string list,
   11.31 -    is_fol: bool,
   11.32 -    has_datatypes: bool,
   11.33 -    serialize: string list -> sign -> sterm list -> string }
   11.34 -  type recon = {
   11.35 -    context: Proof.context,
   11.36 -    typs: typ Symtab.table,
   11.37 -    terms: term Symtab.table,
   11.38 -    rewrite_rules: thm list,
   11.39 -    assms: (int * thm) list }
   11.40 -
   11.41 -  (*translation*)
   11.42 -  val add_config: Old_SMT_Utils.class * (Proof.context -> config) ->
   11.43 -    Context.generic -> Context.generic 
   11.44 -  val translate: Proof.context -> string list -> (int * thm) list ->
   11.45 -    string * recon
   11.46 -end
   11.47 -
   11.48 -structure Old_SMT_Translate: OLD_SMT_TRANSLATE =
   11.49 -struct
   11.50 -
   11.51 -
   11.52 -(* intermediate term structure *)
   11.53 -
   11.54 -datatype squant = SForall | SExists
   11.55 -
   11.56 -datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
   11.57 -
   11.58 -datatype sterm =
   11.59 -  SVar of int |
   11.60 -  SApp of string * sterm list |
   11.61 -  SLet of string * sterm * sterm |
   11.62 -  SQua of squant * string list * sterm spattern list * int option * sterm
   11.63 -
   11.64 -
   11.65 -
   11.66 -(* translation configuration *)
   11.67 -
   11.68 -type prefixes = {sort_prefix: string, func_prefix: string}
   11.69 -
   11.70 -type sign = {
   11.71 -  header: string list,
   11.72 -  sorts: string list,
   11.73 -  dtyps: (string * (string * (string * string) list) list) list list,
   11.74 -  funcs: (string * (string list * string)) list }
   11.75 -
   11.76 -type config = {
   11.77 -  prefixes: prefixes,
   11.78 -  header: term list -> string list,
   11.79 -  is_fol: bool,
   11.80 -  has_datatypes: bool,
   11.81 -  serialize: string list -> sign -> sterm list -> string }
   11.82 -
   11.83 -type recon = {
   11.84 -  context: Proof.context,
   11.85 -  typs: typ Symtab.table,
   11.86 -  terms: term Symtab.table,
   11.87 -  rewrite_rules: thm list,
   11.88 -  assms: (int * thm) list }
   11.89 -
   11.90 -
   11.91 -
   11.92 -(* translation context *)
   11.93 -
   11.94 -fun make_tr_context {sort_prefix, func_prefix} =
   11.95 -  (sort_prefix, 1, Typtab.empty, func_prefix, 1, Termtab.empty)
   11.96 -
   11.97 -fun string_of_index pre i = pre ^ string_of_int i
   11.98 -
   11.99 -fun add_typ T proper (cx as (sp, Tidx, typs, fp, idx, terms)) =
  11.100 -  (case Typtab.lookup typs T of
  11.101 -    SOME (n, _) => (n, cx)
  11.102 -  | NONE =>
  11.103 -      let
  11.104 -        val n = string_of_index sp Tidx
  11.105 -        val typs' = Typtab.update (T, (n, proper)) typs
  11.106 -      in (n, (sp, Tidx+1, typs', fp, idx, terms)) end)
  11.107 -
  11.108 -fun add_fun t sort (cx as (sp, Tidx, typs, fp, idx, terms)) =
  11.109 -  (case Termtab.lookup terms t of
  11.110 -    SOME (n, _) => (n, cx)
  11.111 -  | NONE => 
  11.112 -      let
  11.113 -        val n = string_of_index fp idx
  11.114 -        val terms' = Termtab.update (t, (n, sort)) terms
  11.115 -      in (n, (sp, Tidx, typs, fp, idx+1, terms')) end)
  11.116 -
  11.117 -fun sign_of header dtyps (_, _, typs, _, _, terms) = {
  11.118 -  header = header,
  11.119 -  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
  11.120 -  dtyps = dtyps,
  11.121 -  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
  11.122 -
  11.123 -fun recon_of ctxt rules thms ithms (_, _, typs, _, _, terms) =
  11.124 -  let
  11.125 -    fun add_typ (T, (n, _)) = Symtab.update (n, T)
  11.126 -    val typs' = Typtab.fold add_typ typs Symtab.empty
  11.127 -
  11.128 -    fun add_fun (t, (n, _)) = Symtab.update (n, t)
  11.129 -    val terms' = Termtab.fold add_fun terms Symtab.empty
  11.130 -
  11.131 -    val assms = map (pair ~1) thms @ ithms
  11.132 -  in
  11.133 -    {context=ctxt, typs=typs', terms=terms', rewrite_rules=rules, assms=assms}
  11.134 -  end
  11.135 -
  11.136 -
  11.137 -
  11.138 -(* preprocessing *)
  11.139 -
  11.140 -(** datatype declarations **)
  11.141 -
  11.142 -fun collect_datatypes_and_records (tr_context, ctxt) ts =
  11.143 -  let
  11.144 -    val (declss, ctxt') =
  11.145 -      fold (Term.fold_types Old_SMT_Datatypes.add_decls) ts ([], ctxt)
  11.146 -
  11.147 -    fun is_decl_typ T = exists (exists (equal T o fst)) declss
  11.148 -
  11.149 -    fun add_typ' T proper =
  11.150 -      (case Old_SMT_Builtin.dest_builtin_typ ctxt' T of
  11.151 -        SOME n => pair n
  11.152 -      | NONE => add_typ T proper)
  11.153 -
  11.154 -    fun tr_select sel =
  11.155 -      let val T = Term.range_type (Term.fastype_of sel)
  11.156 -      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
  11.157 -    fun tr_constr (constr, selects) =
  11.158 -      add_fun constr NONE ##>> fold_map tr_select selects
  11.159 -    fun tr_typ (T, cases) = add_typ' T false ##>> fold_map tr_constr cases
  11.160 -    val (declss', tr_context') = fold_map (fold_map tr_typ) declss tr_context
  11.161 -
  11.162 -    fun add (constr, selects) =
  11.163 -      Termtab.update (constr, length selects) #>
  11.164 -      fold (Termtab.update o rpair 1) selects
  11.165 -    val funcs = fold (fold (fold add o snd)) declss Termtab.empty
  11.166 -  in ((funcs, declss', tr_context', ctxt'), ts) end
  11.167 -    (* FIXME: also return necessary datatype and record theorems *)
  11.168 -
  11.169 -
  11.170 -(** eta-expand quantifiers, let expressions and built-ins *)
  11.171 -
  11.172 -local
  11.173 -  fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
  11.174 -
  11.175 -  fun exp f T = eta f (Term.domain_type (Term.domain_type T))
  11.176 -
  11.177 -  fun exp2 T q =
  11.178 -    let val U = Term.domain_type T
  11.179 -    in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
  11.180 -
  11.181 -  fun exp2' T l =
  11.182 -    let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
  11.183 -    in Abs (Name.uu, U1, eta I U2 (l $ Bound 0)) end
  11.184 -
  11.185 -  fun expf k i T t =
  11.186 -    let val Ts = drop i (fst (Old_SMT_Utils.dest_funT k T))
  11.187 -    in
  11.188 -      Term.incr_boundvars (length Ts) t
  11.189 -      |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
  11.190 -      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
  11.191 -    end
  11.192 -in
  11.193 -
  11.194 -fun eta_expand ctxt is_fol funcs =
  11.195 -  let
  11.196 -    fun exp_func t T ts =
  11.197 -      (case Termtab.lookup funcs t of
  11.198 -        SOME k =>
  11.199 -          Term.list_comb (t, ts)
  11.200 -          |> k <> length ts ? expf k (length ts) T
  11.201 -      | NONE => Term.list_comb (t, ts))
  11.202 -
  11.203 -    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
  11.204 -      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
  11.205 -      | expand (q as Const (@{const_name All}, T)) = exp2 T q
  11.206 -      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
  11.207 -      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
  11.208 -      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
  11.209 -      | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
  11.210 -          if is_fol then expand (Term.betapply (Abs a, t))
  11.211 -          else l $ expand t $ abs_expand a
  11.212 -      | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
  11.213 -          if is_fol then expand (u $ t)
  11.214 -          else l $ expand t $ exp expand (Term.range_type T) u
  11.215 -      | expand ((l as Const (@{const_name Let}, T)) $ t) =
  11.216 -          if is_fol then
  11.217 -            let val U = Term.domain_type (Term.range_type T)
  11.218 -            in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
  11.219 -          else exp2 T (l $ expand t)
  11.220 -      | expand (l as Const (@{const_name Let}, T)) =
  11.221 -          if is_fol then 
  11.222 -            let val U = Term.domain_type (Term.range_type T)
  11.223 -            in
  11.224 -              Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U,
  11.225 -                Bound 0 $ Bound 1))
  11.226 -            end
  11.227 -          else exp2' T l
  11.228 -      | expand t =
  11.229 -          (case Term.strip_comb t of
  11.230 -            (u as Const (c as (_, T)), ts) =>
  11.231 -              (case Old_SMT_Builtin.dest_builtin ctxt c ts of
  11.232 -                SOME (_, k, us, mk) =>
  11.233 -                  if k = length us then mk (map expand us)
  11.234 -                  else if k < length us then
  11.235 -                    chop k (map expand us) |>> mk |> Term.list_comb
  11.236 -                  else expf k (length ts) T (mk (map expand us))
  11.237 -              | NONE => exp_func u T (map expand ts))
  11.238 -          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
  11.239 -          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
  11.240 -          | (u, ts) => Term.list_comb (u, map expand ts))
  11.241 -
  11.242 -    and abs_expand (n, T, t) = Abs (n, T, expand t)
  11.243 -  
  11.244 -  in map expand end
  11.245 -
  11.246 -end
  11.247 -
  11.248 -
  11.249 -(** introduce explicit applications **)
  11.250 -
  11.251 -local
  11.252 -  (*
  11.253 -    Make application explicit for functions with varying number of arguments.
  11.254 -  *)
  11.255 -
  11.256 -  fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
  11.257 -  fun add_type T = apsnd (Typtab.update (T, ()))
  11.258 -
  11.259 -  fun min_arities t =
  11.260 -    (case Term.strip_comb t of
  11.261 -      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
  11.262 -    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
  11.263 -    | (Abs (_, T, u), ts) => add_type T #> min_arities u #> fold min_arities ts
  11.264 -    | (_, ts) => fold min_arities ts)
  11.265 -
  11.266 -  fun minimize types t i =
  11.267 -    let
  11.268 -      fun find_min j [] _ = j
  11.269 -        | find_min j (U :: Us) T =
  11.270 -            if Typtab.defined types T then j
  11.271 -            else find_min (j + 1) Us (U --> T)
  11.272 -
  11.273 -      val (Ts, T) = Term.strip_type (Term.type_of t)
  11.274 -    in find_min 0 (take i (rev Ts)) T end
  11.275 -
  11.276 -  fun app u (t, T) =
  11.277 -    (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
  11.278 -
  11.279 -  fun apply i t T ts =
  11.280 -    let
  11.281 -      val (ts1, ts2) = chop i ts
  11.282 -      val (_, U) = Old_SMT_Utils.dest_funT i T
  11.283 -    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
  11.284 -in
  11.285 -
  11.286 -fun intro_explicit_application ctxt funcs ts =
  11.287 -  let
  11.288 -    val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
  11.289 -    val arities' = Termtab.map (minimize types) arities
  11.290 -
  11.291 -    fun app_func t T ts =
  11.292 -      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
  11.293 -      else apply (the (Termtab.lookup arities' t)) t T ts
  11.294 -
  11.295 -    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
  11.296 -
  11.297 -    fun traverse Ts t =
  11.298 -      (case Term.strip_comb t of
  11.299 -        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
  11.300 -          q $ Abs (x, T, in_trigger (T :: Ts) u)
  11.301 -      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
  11.302 -          q $ Abs (x, T, in_trigger (T :: Ts) u)
  11.303 -      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
  11.304 -          q $ traverse Ts u1 $ traverse Ts u2
  11.305 -      | (u as Const (c as (_, T)), ts) =>
  11.306 -          (case Old_SMT_Builtin.dest_builtin ctxt c ts of
  11.307 -            SOME (_, k, us, mk) =>
  11.308 -              let
  11.309 -                val (ts1, ts2) = chop k (map (traverse Ts) us)
  11.310 -                val U = Term.strip_type T |>> snd o chop k |> (op --->)
  11.311 -              in apply 0 (mk ts1) U ts2 end
  11.312 -          | NONE => app_func u T (map (traverse Ts) ts))
  11.313 -      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
  11.314 -      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
  11.315 -      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
  11.316 -      | (u, ts) => traverses Ts u ts)
  11.317 -    and in_trigger Ts ((c as @{const trigger}) $ p $ t) =
  11.318 -          c $ in_pats Ts p $ in_weight Ts t
  11.319 -      | in_trigger Ts t = in_weight Ts t
  11.320 -    and in_pats Ts ps =
  11.321 -      in_list @{typ "pattern list"}
  11.322 -        (in_list @{typ pattern} (in_pat Ts)) ps
  11.323 -    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) =
  11.324 -          p $ traverse Ts t
  11.325 -      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) =
  11.326 -          p $ traverse Ts t
  11.327 -      | in_pat _ t = raise TERM ("bad pattern", [t])
  11.328 -    and in_weight Ts ((c as @{const weight}) $ w $ t) =
  11.329 -          c $ w $ traverse Ts t
  11.330 -      | in_weight Ts t = traverse Ts t 
  11.331 -    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
  11.332 -  in map (traverse []) ts end
  11.333 -
  11.334 -val fun_app_eq = mk_meta_eq @{thm fun_app_def}
  11.335 -
  11.336 -end
  11.337 -
  11.338 -
  11.339 -(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
  11.340 -
  11.341 -local
  11.342 -  val term_bool = @{lemma "term_true ~= term_false"
  11.343 -    by (simp add: term_true_def term_false_def)}
  11.344 -
  11.345 -  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
  11.346 -
  11.347 -  val fol_rules = [
  11.348 -    Let_def,
  11.349 -    mk_meta_eq @{thm term_true_def},
  11.350 -    mk_meta_eq @{thm term_false_def},
  11.351 -    @{lemma "P = True == P" by (rule eq_reflection) simp},
  11.352 -    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
  11.353 -
  11.354 -  fun as_term t = @{const HOL.eq (bool)} $ t $ @{const term_true}
  11.355 -
  11.356 -  exception BAD_PATTERN of unit
  11.357 -
  11.358 -  fun wrap_in_if pat t =
  11.359 -    if pat then
  11.360 -      raise BAD_PATTERN ()
  11.361 -    else
  11.362 -      @{const If (bool)} $ t $ @{const term_true} $ @{const term_false}
  11.363 -
  11.364 -  fun is_builtin_conn_or_pred ctxt c ts =
  11.365 -    is_some (Old_SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
  11.366 -    is_some (Old_SMT_Builtin.dest_builtin_pred ctxt c ts)
  11.367 -
  11.368 -  fun builtin b ctxt c ts =
  11.369 -    (case (Const c, ts) of
  11.370 -      (@{const HOL.eq (bool)}, [t, u]) =>
  11.371 -        if t = @{const term_true} orelse u = @{const term_true} then
  11.372 -          Old_SMT_Builtin.dest_builtin_eq ctxt t u
  11.373 -        else b ctxt c ts
  11.374 -    | _ => b ctxt c ts)
  11.375 -in
  11.376 -
  11.377 -fun folify ctxt =
  11.378 -  let
  11.379 -    fun in_list T f t = HOLogic.mk_list T (map_filter f (HOLogic.dest_list t))
  11.380 -
  11.381 -    fun in_term pat t =
  11.382 -      (case Term.strip_comb t of
  11.383 -        (@{const True}, []) => @{const term_true}
  11.384 -      | (@{const False}, []) => @{const term_false}
  11.385 -      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
  11.386 -          if pat then raise BAD_PATTERN ()
  11.387 -          else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
  11.388 -      | (Const (c as (n, _)), ts) =>
  11.389 -          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if pat (in_form t)
  11.390 -          else if is_quant n then wrap_in_if pat (in_form t)
  11.391 -          else Term.list_comb (Const c, map (in_term pat) ts)
  11.392 -      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
  11.393 -      | _ => t)
  11.394 -
  11.395 -    and in_weight ((c as @{const weight}) $ w $ t) = c $ w $ in_form t
  11.396 -      | in_weight t = in_form t 
  11.397 -
  11.398 -    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
  11.399 -          p $ in_term true t
  11.400 -      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
  11.401 -          p $ in_term true t
  11.402 -      | in_pat t = raise TERM ("bad pattern", [t])
  11.403 -
  11.404 -    and in_pats ps =
  11.405 -      in_list @{typ "pattern list"}
  11.406 -        (SOME o in_list @{typ pattern} (try in_pat)) ps
  11.407 -
  11.408 -    and in_trigger ((c as @{const trigger}) $ p $ t) =
  11.409 -          c $ in_pats p $ in_weight t
  11.410 -      | in_trigger t = in_weight t
  11.411 -
  11.412 -    and in_form t =
  11.413 -      (case Term.strip_comb t of
  11.414 -        (q as Const (qn, _), [Abs (n, T, u)]) =>
  11.415 -          if is_quant qn then q $ Abs (n, T, in_trigger u)
  11.416 -          else as_term (in_term false t)
  11.417 -      | (Const c, ts) =>
  11.418 -          (case Old_SMT_Builtin.dest_builtin_conn ctxt c ts of
  11.419 -            SOME (_, _, us, mk) => mk (map in_form us)
  11.420 -          | NONE =>
  11.421 -              (case Old_SMT_Builtin.dest_builtin_pred ctxt c ts of
  11.422 -                SOME (_, _, us, mk) => mk (map (in_term false) us)
  11.423 -              | NONE => as_term (in_term false t)))
  11.424 -      | _ => as_term (in_term false t))
  11.425 -  in
  11.426 -    map in_form #>
  11.427 -    cons (Old_SMT_Utils.prop_of term_bool) #>
  11.428 -    pair (fol_rules, [term_bool], builtin)
  11.429 -  end
  11.430 -
  11.431 -end
  11.432 -
  11.433 -
  11.434 -(* translation into intermediate format *)
  11.435 -
  11.436 -(** utility functions **)
  11.437 -
  11.438 -val quantifier = (fn
  11.439 -    @{const_name All} => SOME SForall
  11.440 -  | @{const_name Ex} => SOME SExists
  11.441 -  | _ => NONE)
  11.442 -
  11.443 -fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
  11.444 -      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
  11.445 -  | group_quant _ Ts t = (Ts, t)
  11.446 -
  11.447 -fun dest_weight (@{const weight} $ w $ t) =
  11.448 -      (SOME (snd (HOLogic.dest_number w)), t)
  11.449 -  | dest_weight t = (NONE, t)
  11.450 -
  11.451 -fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
  11.452 -  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
  11.453 -  | dest_pat t = raise TERM ("bad pattern", [t])
  11.454 -
  11.455 -fun dest_pats [] = I
  11.456 -  | dest_pats ts =
  11.457 -      (case map dest_pat ts |> split_list ||> distinct (op =) of
  11.458 -        (ps, [true]) => cons (SPat ps)
  11.459 -      | (ps, [false]) => cons (SNoPat ps)
  11.460 -      | _ => raise TERM ("bad multi-pattern", ts))
  11.461 -
  11.462 -fun dest_trigger (@{const trigger} $ tl $ t) =
  11.463 -      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
  11.464 -  | dest_trigger t = ([], t)
  11.465 -
  11.466 -fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
  11.467 -  let
  11.468 -    val (Ts, u) = group_quant qn [T] t
  11.469 -    val (ps, p) = dest_trigger u
  11.470 -    val (w, b) = dest_weight p
  11.471 -  in (q, rev Ts, ps, w, b) end)
  11.472 -
  11.473 -fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
  11.474 -  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
  11.475 -
  11.476 -
  11.477 -(** translation from Isabelle terms into SMT intermediate terms **)
  11.478 -
  11.479 -fun intermediate header dtyps builtin ctxt ts trx =
  11.480 -  let
  11.481 -    fun transT (T as TFree _) = add_typ T true
  11.482 -      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
  11.483 -      | transT (T as Type _) =
  11.484 -          (case Old_SMT_Builtin.dest_builtin_typ ctxt T of
  11.485 -            SOME n => pair n
  11.486 -          | NONE => add_typ T true)
  11.487 -
  11.488 -    fun app n ts = SApp (n, ts)
  11.489 -
  11.490 -    fun trans t =
  11.491 -      (case Term.strip_comb t of
  11.492 -        (Const (qn, _), [Abs (_, T, t1)]) =>
  11.493 -          (case dest_quant qn T t1 of
  11.494 -            SOME (q, Ts, ps, w, b) =>
  11.495 -              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
  11.496 -              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
  11.497 -          | NONE => raise TERM ("unsupported quantifier", [t]))
  11.498 -      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
  11.499 -          transT T ##>> trans t1 ##>> trans t2 #>>
  11.500 -          (fn ((U, u1), u2) => SLet (U, u1, u2))
  11.501 -      | (u as Const (c as (_, T)), ts) =>
  11.502 -          (case builtin ctxt c ts of
  11.503 -            SOME (n, _, us, _) => fold_map trans us #>> app n
  11.504 -          | NONE => transs u T ts)
  11.505 -      | (u as Free (_, T), ts) => transs u T ts
  11.506 -      | (Bound i, []) => pair (SVar i)
  11.507 -      | _ => raise TERM ("bad SMT term", [t]))
  11.508 - 
  11.509 -    and transs t T ts =
  11.510 -      let val (Us, U) = Old_SMT_Utils.dest_funT (length ts) T
  11.511 -      in
  11.512 -        fold_map transT Us ##>> transT U #-> (fn Up =>
  11.513 -        add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
  11.514 -      end
  11.515 -
  11.516 -    val (us, trx') = fold_map trans ts trx
  11.517 -  in ((sign_of (header ts) dtyps trx', us), trx') end
  11.518 -
  11.519 -
  11.520 -
  11.521 -(* translation *)
  11.522 -
  11.523 -structure Configs = Generic_Data
  11.524 -(
  11.525 -  type T = (Proof.context -> config) Old_SMT_Utils.dict
  11.526 -  val empty = []
  11.527 -  val extend = I
  11.528 -  fun merge data = Old_SMT_Utils.dict_merge fst data
  11.529 -)
  11.530 -
  11.531 -fun add_config (cs, cfg) = Configs.map (Old_SMT_Utils.dict_update (cs, cfg))
  11.532 -
  11.533 -fun get_config ctxt = 
  11.534 -  let val cs = Old_SMT_Config.solver_class_of ctxt
  11.535 -  in
  11.536 -    (case Old_SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
  11.537 -      SOME cfg => cfg ctxt
  11.538 -    | NONE => error ("SMT: no translation configuration found " ^
  11.539 -        "for solver class " ^ quote (Old_SMT_Utils.string_of_class cs)))
  11.540 -  end
  11.541 -
  11.542 -fun translate ctxt comments ithms =
  11.543 -  let
  11.544 -    val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
  11.545 -
  11.546 -    val with_datatypes =
  11.547 -      has_datatypes andalso Config.get ctxt Old_SMT_Config.datatypes
  11.548 -
  11.549 -    fun no_dtyps (tr_context, ctxt) ts =
  11.550 -      ((Termtab.empty, [], tr_context, ctxt), ts)
  11.551 -
  11.552 -    val ts1 = map (Envir.beta_eta_contract o Old_SMT_Utils.prop_of o snd) ithms
  11.553 -
  11.554 -    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
  11.555 -      ((make_tr_context prefixes, ctxt), ts1)
  11.556 -      |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
  11.557 -
  11.558 -    fun is_binder (Const (@{const_name Let}, _) $ _) = true
  11.559 -      | is_binder t = Lambda_Lifting.is_quantifier t
  11.560 -
  11.561 -    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
  11.562 -          q $ Abs (n, T, mk_trigger t)
  11.563 -      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
  11.564 -          Term.domain_type T --> @{typ pattern}
  11.565 -          |> (fn T => Const (@{const_name pat}, T) $ lhs)
  11.566 -          |> HOLogic.mk_list @{typ pattern} o single
  11.567 -          |> HOLogic.mk_list @{typ "pattern list"} o single
  11.568 -          |> (fn t => @{const trigger} $ t $ eq)
  11.569 -      | mk_trigger t = t
  11.570 -
  11.571 -    val (ctxt2, ts3) =
  11.572 -      ts2
  11.573 -      |> eta_expand ctxt1 is_fol funcs
  11.574 -      |> rpair ctxt1
  11.575 -      |-> Lambda_Lifting.lift_lambdas NONE is_binder
  11.576 -      |-> (fn (ts', defs) => fn ctxt' =>
  11.577 -          map mk_trigger defs @ ts'
  11.578 -          |> intro_explicit_application ctxt' funcs 
  11.579 -          |> pair ctxt')
  11.580 -
  11.581 -    val ((rewrite_rules, extra_thms, builtin), ts4) =
  11.582 -      (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
  11.583 -
  11.584 -    val rewrite_rules' = fun_app_eq :: rewrite_rules
  11.585 -  in
  11.586 -    (ts4, tr_context)
  11.587 -    |-> intermediate header dtyps (builtin Old_SMT_Builtin.dest_builtin) ctxt2
  11.588 -    |>> uncurry (serialize comments)
  11.589 -    ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
  11.590 -  end
  11.591 -
  11.592 -end
    12.1 --- a/src/HOL/Library/Old_SMT/old_smt_utils.ML	Thu Apr 20 10:45:52 2017 +0200
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,221 +0,0 @@
    12.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_utils.ML
    12.5 -    Author:     Sascha Boehme, TU Muenchen
    12.6 -
    12.7 -General utility functions.
    12.8 -*)
    12.9 -
   12.10 -signature OLD_SMT_UTILS =
   12.11 -sig
   12.12 -  (*basic combinators*)
   12.13 -  val repeat: ('a -> 'a option) -> 'a -> 'a
   12.14 -  val repeat_yield: ('a -> 'b -> ('a * 'b) option) -> 'a -> 'b -> 'a * 'b
   12.15 -
   12.16 -  (*class dictionaries*)
   12.17 -  type class = string list
   12.18 -  val basicC: class
   12.19 -  val string_of_class: class -> string
   12.20 -  type 'a dict = (class * 'a) Ord_List.T
   12.21 -  val dict_map_default: class * 'a -> ('a -> 'a) -> 'a dict -> 'a dict
   12.22 -  val dict_update: class * 'a -> 'a dict -> 'a dict
   12.23 -  val dict_merge: ('a * 'a -> 'a) -> 'a dict * 'a dict -> 'a dict
   12.24 -  val dict_lookup: 'a dict -> class -> 'a list
   12.25 -  val dict_get: 'a dict -> class -> 'a option
   12.26 -
   12.27 -  (*types*)
   12.28 -  val dest_funT: int -> typ -> typ list * typ
   12.29 -
   12.30 -  (*terms*)
   12.31 -  val dest_conj: term -> term * term
   12.32 -  val dest_disj: term -> term * term
   12.33 -  val under_quant: (term -> 'a) -> term -> 'a
   12.34 -  val is_number: term -> bool
   12.35 -
   12.36 -  (*patterns and instantiations*)
   12.37 -  val mk_const_pat: theory -> string -> (ctyp -> 'a) -> 'a * cterm
   12.38 -  val destT1: ctyp -> ctyp
   12.39 -  val destT2: ctyp -> ctyp
   12.40 -  val instTs: ctyp list -> ctyp list * cterm -> cterm
   12.41 -  val instT: ctyp -> ctyp * cterm -> cterm
   12.42 -  val instT': cterm -> ctyp * cterm -> cterm
   12.43 -
   12.44 -  (*certified terms*)
   12.45 -  val dest_cabs: cterm -> Proof.context -> cterm * Proof.context
   12.46 -  val dest_all_cabs: cterm -> Proof.context -> cterm * Proof.context
   12.47 -  val dest_cbinder: cterm -> Proof.context -> cterm * Proof.context
   12.48 -  val dest_all_cbinders: cterm -> Proof.context -> cterm * Proof.context
   12.49 -  val mk_cprop: cterm -> cterm
   12.50 -  val dest_cprop: cterm -> cterm
   12.51 -  val mk_cequals: cterm -> cterm -> cterm
   12.52 -  val term_of: cterm -> term
   12.53 -  val prop_of: thm -> term
   12.54 -
   12.55 -  (*conversions*)
   12.56 -  val if_conv: (term -> bool) -> conv -> conv -> conv
   12.57 -  val if_true_conv: (term -> bool) -> conv -> conv
   12.58 -  val if_exists_conv: (term -> bool) -> conv -> conv
   12.59 -  val binders_conv: (Proof.context -> conv) -> Proof.context -> conv
   12.60 -  val under_quant_conv: (Proof.context * cterm list -> conv) ->
   12.61 -    Proof.context -> conv
   12.62 -  val prop_conv: conv -> conv
   12.63 -end
   12.64 -
   12.65 -structure Old_SMT_Utils: OLD_SMT_UTILS =
   12.66 -struct
   12.67 -
   12.68 -(* basic combinators *)
   12.69 -
   12.70 -fun repeat f =
   12.71 -  let fun rep x = (case f x of SOME y => rep y | NONE => x)
   12.72 -  in rep end
   12.73 -
   12.74 -fun repeat_yield f =
   12.75 -  let fun rep x y = (case f x y of SOME (x', y') => rep x' y' | NONE => (x, y))
   12.76 -  in rep end
   12.77 -
   12.78 -
   12.79 -(* class dictionaries *)
   12.80 -
   12.81 -type class = string list
   12.82 -
   12.83 -val basicC = []
   12.84 -
   12.85 -fun string_of_class [] = "basic"
   12.86 -  | string_of_class cs = "basic." ^ space_implode "." cs
   12.87 -
   12.88 -type 'a dict = (class * 'a) Ord_List.T
   12.89 -
   12.90 -fun class_ord ((cs1, _), (cs2, _)) =
   12.91 -  rev_order (list_ord fast_string_ord (cs1, cs2))
   12.92 -
   12.93 -fun dict_insert (cs, x) d =
   12.94 -  if AList.defined (op =) d cs then d
   12.95 -  else Ord_List.insert class_ord (cs, x) d
   12.96 -
   12.97 -fun dict_map_default (cs, x) f =
   12.98 -  dict_insert (cs, x) #> AList.map_entry (op =) cs f
   12.99 -
  12.100 -fun dict_update (e as (_, x)) = dict_map_default e (K x)
  12.101 -
  12.102 -fun dict_merge val_merge = sort class_ord o AList.join (op =) (K val_merge)
  12.103 -
  12.104 -fun dict_lookup d cs =
  12.105 -  let fun match (cs', x) = if is_prefix (op =) cs' cs then SOME x else NONE
  12.106 -  in map_filter match d end
  12.107 -
  12.108 -fun dict_get d cs =
  12.109 -  (case AList.lookup (op =) d cs of
  12.110 -    NONE => (case cs of [] => NONE | _ => dict_get d (take (length cs - 1) cs))
  12.111 -  | SOME x => SOME x)
  12.112 -
  12.113 -
  12.114 -(* types *)
  12.115 -
  12.116 -val dest_funT =
  12.117 -  let
  12.118 -    fun dest Ts 0 T = (rev Ts, T)
  12.119 -      | dest Ts i (Type ("fun", [T, U])) = dest (T::Ts) (i-1) U
  12.120 -      | dest _ _ T = raise TYPE ("not a function type", [T], [])
  12.121 -  in dest [] end
  12.122 -
  12.123 -
  12.124 -(* terms *)
  12.125 -
  12.126 -fun dest_conj (@{const HOL.conj} $ t $ u) = (t, u)
  12.127 -  | dest_conj t = raise TERM ("not a conjunction", [t])
  12.128 -
  12.129 -fun dest_disj (@{const HOL.disj} $ t $ u) = (t, u)
  12.130 -  | dest_disj t = raise TERM ("not a disjunction", [t])
  12.131 -
  12.132 -fun under_quant f t =
  12.133 -  (case t of
  12.134 -    Const (@{const_name All}, _) $ Abs (_, _, u) => under_quant f u
  12.135 -  | Const (@{const_name Ex}, _) $ Abs (_, _, u) => under_quant f u
  12.136 -  | _ => f t)
  12.137 -
  12.138 -val is_number =
  12.139 -  let
  12.140 -    fun is_num env (Const (@{const_name If}, _) $ _ $ t $ u) =
  12.141 -          is_num env t andalso is_num env u
  12.142 -      | is_num env (Const (@{const_name Let}, _) $ t $ Abs (_, _, u)) =
  12.143 -          is_num (t :: env) u
  12.144 -      | is_num env (Bound i) = i < length env andalso is_num env (nth env i)
  12.145 -      | is_num _ t = can HOLogic.dest_number t
  12.146 -  in is_num [] end
  12.147 -
  12.148 -
  12.149 -(* patterns and instantiations *)
  12.150 -
  12.151 -fun mk_const_pat thy name destT =
  12.152 -  let val cpat = Thm.global_cterm_of thy (Const (name, Sign.the_const_type thy name))
  12.153 -  in (destT (Thm.ctyp_of_cterm cpat), cpat) end
  12.154 -
  12.155 -val destT1 = hd o Thm.dest_ctyp
  12.156 -val destT2 = hd o tl o Thm.dest_ctyp
  12.157 -
  12.158 -fun instTs cUs (cTs, ct) = Thm.instantiate_cterm (map (dest_TVar o Thm.typ_of) cTs ~~ cUs, []) ct
  12.159 -fun instT cU (cT, ct) = instTs [cU] ([cT], ct)
  12.160 -fun instT' ct = instT (Thm.ctyp_of_cterm ct)
  12.161 -
  12.162 -
  12.163 -(* certified terms *)
  12.164 -
  12.165 -fun dest_cabs ct ctxt =
  12.166 -  (case Thm.term_of ct of
  12.167 -    Abs _ =>
  12.168 -      let val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
  12.169 -      in (snd (Thm.dest_abs (SOME n) ct), ctxt') end
  12.170 -  | _ => raise CTERM ("no abstraction", [ct]))
  12.171 -
  12.172 -val dest_all_cabs = repeat_yield (try o dest_cabs) 
  12.173 -
  12.174 -fun dest_cbinder ct ctxt =
  12.175 -  (case Thm.term_of ct of
  12.176 -    Const _ $ Abs _ => dest_cabs (Thm.dest_arg ct) ctxt
  12.177 -  | _ => raise CTERM ("not a binder", [ct]))
  12.178 -
  12.179 -val dest_all_cbinders = repeat_yield (try o dest_cbinder)
  12.180 -
  12.181 -val mk_cprop = Thm.apply (Thm.cterm_of @{context} @{const Trueprop})
  12.182 -
  12.183 -fun dest_cprop ct =
  12.184 -  (case Thm.term_of ct of
  12.185 -    @{const Trueprop} $ _ => Thm.dest_arg ct
  12.186 -  | _ => raise CTERM ("not a property", [ct]))
  12.187 -
  12.188 -val equals = mk_const_pat @{theory} @{const_name Pure.eq} destT1
  12.189 -fun mk_cequals ct cu = Thm.mk_binop (instT' ct equals) ct cu
  12.190 -
  12.191 -val dest_prop = (fn @{const Trueprop} $ t => t | t => t)
  12.192 -fun term_of ct = dest_prop (Thm.term_of ct)
  12.193 -fun prop_of thm = dest_prop (Thm.prop_of thm)
  12.194 -
  12.195 -
  12.196 -(* conversions *)
  12.197 -
  12.198 -fun if_conv pred cv1 cv2 ct = if pred (Thm.term_of ct) then cv1 ct else cv2 ct
  12.199 -
  12.200 -fun if_true_conv pred cv = if_conv pred cv Conv.all_conv
  12.201 -
  12.202 -fun if_exists_conv pred = if_true_conv (Term.exists_subterm pred)
  12.203 -
  12.204 -fun binders_conv cv ctxt =
  12.205 -  Conv.binder_conv (binders_conv cv o snd) ctxt else_conv cv ctxt
  12.206 -
  12.207 -fun under_quant_conv cv ctxt =
  12.208 -  let
  12.209 -    fun quant_conv inside ctxt cvs ct =
  12.210 -      (case Thm.term_of ct of
  12.211 -        Const (@{const_name All}, _) $ Abs _ =>
  12.212 -          Conv.binder_conv (under_conv cvs) ctxt
  12.213 -      | Const (@{const_name Ex}, _) $ Abs _ =>
  12.214 -          Conv.binder_conv (under_conv cvs) ctxt
  12.215 -      | _ => if inside then cv (ctxt, cvs) else Conv.all_conv) ct
  12.216 -    and under_conv cvs (cv, ctxt) = quant_conv true ctxt (cv :: cvs)
  12.217 -  in quant_conv false ctxt [] end
  12.218 -
  12.219 -fun prop_conv cv ct =
  12.220 -  (case Thm.term_of ct of
  12.221 -    @{const Trueprop} $ _ => Conv.arg_conv cv ct
  12.222 -  | _ => raise CTERM ("not a property", [ct]))
  12.223 -
  12.224 -end
    13.1 --- a/src/HOL/Library/Old_SMT/old_smt_word.ML	Thu Apr 20 10:45:52 2017 +0200
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,146 +0,0 @@
    13.4 -(*  Title:      HOL/Library/Old_SMT/old_smt_word.ML
    13.5 -    Author:     Sascha Boehme, TU Muenchen
    13.6 -
    13.7 -SMT setup for words.
    13.8 -*)
    13.9 -
   13.10 -structure Old_SMT_Word: sig end =
   13.11 -struct
   13.12 -
   13.13 -open Word_Lib
   13.14 -
   13.15 -(* SMT-LIB logic *)
   13.16 -
   13.17 -fun smtlib_logic ts =
   13.18 -  if exists (Term.exists_type (Term.exists_subtype is_wordT)) ts
   13.19 -  then SOME "QF_AUFBV"
   13.20 -  else NONE
   13.21 -
   13.22 -
   13.23 -(* SMT-LIB builtins *)
   13.24 -
   13.25 -local
   13.26 -  val smtlibC = Old_SMTLIB_Interface.smtlibC
   13.27 -
   13.28 -  val wordT = @{typ "'a::len word"}
   13.29 -
   13.30 -  fun index1 n i = n ^ "[" ^ string_of_int i ^ "]"
   13.31 -  fun index2 n i j = n ^ "[" ^ string_of_int i ^ ":" ^ string_of_int j ^ "]"
   13.32 -
   13.33 -  fun word_typ (Type (@{type_name word}, [T])) =
   13.34 -        Option.map (index1 "BitVec") (try dest_binT T)
   13.35 -    | word_typ _ = NONE
   13.36 -
   13.37 -  fun word_num (Type (@{type_name word}, [T])) i =
   13.38 -        Option.map (index1 ("bv" ^ string_of_int i)) (try dest_binT T)
   13.39 -    | word_num _ _ = NONE
   13.40 -
   13.41 -  fun if_fixed pred m n T ts =
   13.42 -    let val (Us, U) = Term.strip_type T
   13.43 -    in
   13.44 -      if pred (U, Us) then
   13.45 -        SOME (n, length Us, ts, Term.list_comb o pair (Const (m, T)))
   13.46 -      else NONE
   13.47 -    end
   13.48 -
   13.49 -  fun if_fixed_all m = if_fixed (forall (can dest_wordT) o (op ::)) m
   13.50 -  fun if_fixed_args m = if_fixed (forall (can dest_wordT) o snd) m
   13.51 -
   13.52 -  fun add_word_fun f (t, n) =
   13.53 -    let val (m, _) = Term.dest_Const t
   13.54 -    in Old_SMT_Builtin.add_builtin_fun smtlibC (Term.dest_Const t, K (f m n)) end
   13.55 -
   13.56 -  fun hd2 xs = hd (tl xs)
   13.57 -
   13.58 -  fun mk_nat i = @{const nat} $ HOLogic.mk_number @{typ nat} i
   13.59 -
   13.60 -  fun dest_nat (@{const nat} $ n) = snd (HOLogic.dest_number n)
   13.61 -    | dest_nat t = raise TERM ("not a natural number", [t])
   13.62 -
   13.63 -  fun mk_shift c [t, u] = Const c $ t $ mk_nat (snd (HOLogic.dest_number u))
   13.64 -    | mk_shift c ts = raise TERM ("bad arguments", Const c :: ts)
   13.65 -
   13.66 -  fun shift m n T ts =
   13.67 -    let val U = Term.domain_type T
   13.68 -    in
   13.69 -      (case (can dest_wordT U, try (dest_nat o hd2) ts) of
   13.70 -        (true, SOME i) =>
   13.71 -          SOME (n, 2, [hd ts, HOLogic.mk_number U i], mk_shift (m, T))
   13.72 -      | _ => NONE)   (* FIXME: also support non-numerical shifts *)
   13.73 -    end
   13.74 -
   13.75 -  fun mk_extract c i ts = Term.list_comb (Const c, mk_nat i :: ts)
   13.76 -
   13.77 -  fun extract m n T ts =
   13.78 -    let val U = Term.range_type (Term.range_type T)
   13.79 -    in
   13.80 -      (case (try (dest_nat o hd) ts, try dest_wordT U) of
   13.81 -        (SOME lb, SOME i) =>
   13.82 -          SOME (index2 n (i + lb - 1) lb, 1, tl ts, mk_extract (m, T) lb)
   13.83 -      | _ => NONE)
   13.84 -    end
   13.85 -
   13.86 -  fun mk_extend c ts = Term.list_comb (Const c, ts)
   13.87 -
   13.88 -  fun extend m n T ts =
   13.89 -    let val (U1, U2) = Term.dest_funT T
   13.90 -    in
   13.91 -      (case (try dest_wordT U1, try dest_wordT U2) of
   13.92 -        (SOME i, SOME j) =>
   13.93 -          if j-i >= 0 then SOME (index1 n (j-i), 1, ts, mk_extend (m, T))
   13.94 -          else NONE
   13.95 -      | _ => NONE)
   13.96 -    end
   13.97 -
   13.98 -  fun mk_rotate c i ts = Term.list_comb (Const c, mk_nat i :: ts)
   13.99 -
  13.100 -  fun rotate m n T ts =
  13.101 -    let val U = Term.domain_type (Term.range_type T)
  13.102 -    in
  13.103 -      (case (can dest_wordT U, try (dest_nat o hd) ts) of
  13.104 -        (true, SOME i) => SOME (index1 n i, 1, tl ts, mk_rotate (m, T) i)
  13.105 -      | _ => NONE)
  13.106 -    end
  13.107 -in
  13.108 -
  13.109 -val setup_builtins =
  13.110 -  Old_SMT_Builtin.add_builtin_typ smtlibC (wordT, word_typ, word_num) #>
  13.111 -  fold (add_word_fun if_fixed_all) [
  13.112 -    (@{term "uminus :: 'a::len word => _"}, "bvneg"),
  13.113 -    (@{term "plus :: 'a::len word => _"}, "bvadd"),
  13.114 -    (@{term "minus :: 'a::len word => _"}, "bvsub"),
  13.115 -    (@{term "times :: 'a::len word => _"}, "bvmul"),
  13.116 -    (@{term "bitNOT :: 'a::len word => _"}, "bvnot"),
  13.117 -    (@{term "bitAND :: 'a::len word => _"}, "bvand"),
  13.118 -    (@{term "bitOR :: 'a::len word => _"}, "bvor"),
  13.119 -    (@{term "bitXOR :: 'a::len word => _"}, "bvxor"),
  13.120 -    (@{term "word_cat :: 'a::len word => _"}, "concat") ] #>
  13.121 -  fold (add_word_fun shift) [
  13.122 -    (@{term "shiftl :: 'a::len word => _ "}, "bvshl"),
  13.123 -    (@{term "shiftr :: 'a::len word => _"}, "bvlshr"),
  13.124 -    (@{term "sshiftr :: 'a::len word => _"}, "bvashr") ] #>
  13.125 -  add_word_fun extract
  13.126 -    (@{term "slice :: _ => 'a::len word => _"}, "extract") #>
  13.127 -  fold (add_word_fun extend) [
  13.128 -    (@{term "ucast :: 'a::len word => _"}, "zero_extend"),
  13.129 -    (@{term "scast :: 'a::len word => _"}, "sign_extend") ] #>
  13.130 -  fold (add_word_fun rotate) [
  13.131 -    (@{term word_rotl}, "rotate_left"),
  13.132 -    (@{term word_rotr}, "rotate_right") ] #>
  13.133 -  fold (add_word_fun if_fixed_args) [
  13.134 -    (@{term "less :: 'a::len word => _"}, "bvult"),
  13.135 -    (@{term "less_eq :: 'a::len word => _"}, "bvule"),
  13.136 -    (@{term word_sless}, "bvslt"),
  13.137 -    (@{term word_sle}, "bvsle") ]
  13.138 -
  13.139 -end
  13.140 -
  13.141 -
  13.142 -(* setup *)
  13.143 -
  13.144 -val _ = 
  13.145 -  Theory.setup
  13.146 -    (Context.theory_map
  13.147 -      (Old_SMTLIB_Interface.add_logic (20, smtlib_logic) #> setup_builtins))
  13.148 -
  13.149 -end
    14.1 --- a/src/HOL/Library/Old_SMT/old_smtlib_interface.ML	Thu Apr 20 10:45:52 2017 +0200
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,161 +0,0 @@
    14.4 -(*  Title:      HOL/Library/Old_SMT/old_smtlib_interface.ML
    14.5 -    Author:     Sascha Boehme, TU Muenchen
    14.6 -
    14.7 -Interface to SMT solvers based on the SMT-LIB format.
    14.8 -*)
    14.9 -
   14.10 -signature OLD_SMTLIB_INTERFACE =
   14.11 -sig
   14.12 -  val smtlibC: Old_SMT_Utils.class
   14.13 -  val add_logic: int * (term list -> string option) -> Context.generic ->
   14.14 -    Context.generic
   14.15 -  val translate_config: Proof.context -> Old_SMT_Translate.config
   14.16 -  val setup: theory -> theory
   14.17 -end
   14.18 -
   14.19 -structure Old_SMTLIB_Interface: OLD_SMTLIB_INTERFACE =
   14.20 -struct
   14.21 -
   14.22 -
   14.23 -val smtlibC = ["smtlib"]
   14.24 -
   14.25 -
   14.26 -(* builtins *)
   14.27 -
   14.28 -local
   14.29 -  fun int_num _ i = SOME (string_of_int i)
   14.30 -
   14.31 -  fun is_linear [t] = Old_SMT_Utils.is_number t
   14.32 -    | is_linear [t, u] = Old_SMT_Utils.is_number t orelse Old_SMT_Utils.is_number u
   14.33 -    | is_linear _ = false
   14.34 -
   14.35 -  fun times _ _ ts =
   14.36 -    let val mk = Term.list_comb o pair @{const times (int)}
   14.37 -    in if is_linear ts then SOME ("*", 2, ts, mk) else NONE end
   14.38 -in
   14.39 -
   14.40 -val setup_builtins =
   14.41 -  Old_SMT_Builtin.add_builtin_typ smtlibC (@{typ int}, K (SOME "Int"), int_num) #>
   14.42 -  fold (Old_SMT_Builtin.add_builtin_fun' smtlibC) [
   14.43 -    (@{const True}, "true"),
   14.44 -    (@{const False}, "false"),
   14.45 -    (@{const Not}, "not"),
   14.46 -    (@{const HOL.conj}, "and"),
   14.47 -    (@{const HOL.disj}, "or"),
   14.48 -    (@{const HOL.implies}, "implies"),
   14.49 -    (@{const HOL.eq (bool)}, "iff"),
   14.50 -    (@{const HOL.eq ('a)}, "="),
   14.51 -    (@{const If (bool)}, "if_then_else"),
   14.52 -    (@{const If ('a)}, "ite"),
   14.53 -    (@{const less (int)}, "<"),
   14.54 -    (@{const less_eq (int)}, "<="),
   14.55 -    (@{const uminus (int)}, "~"),
   14.56 -    (@{const plus (int)}, "+"),
   14.57 -    (@{const minus (int)}, "-") ] #>
   14.58 -  Old_SMT_Builtin.add_builtin_fun smtlibC
   14.59 -    (Term.dest_Const @{const times (int)}, times)
   14.60 -
   14.61 -end
   14.62 -
   14.63 -
   14.64 -(* serialization *)
   14.65 -
   14.66 -(** header **)
   14.67 -
   14.68 -fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
   14.69 -
   14.70 -structure Logics = Generic_Data
   14.71 -(
   14.72 -  type T = (int * (term list -> string option)) list
   14.73 -  val empty = []
   14.74 -  val extend = I
   14.75 -  fun merge data = Ord_List.merge fst_int_ord data
   14.76 -)
   14.77 -
   14.78 -fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf)
   14.79 -
   14.80 -fun choose_logic ctxt ts =
   14.81 -  let
   14.82 -    fun choose [] = "AUFLIA"
   14.83 -      | choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs)
   14.84 -  in [":logic " ^ choose (Logics.get (Context.Proof ctxt))] end
   14.85 -
   14.86 -
   14.87 -(** serialization **)
   14.88 -
   14.89 -val add = Buffer.add
   14.90 -fun sep f = add " " #> f
   14.91 -fun enclose l r f = sep (add l #> f #> add r)
   14.92 -val par = enclose "(" ")"
   14.93 -fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs))
   14.94 -fun line f = f #> add "\n"
   14.95 -
   14.96 -fun var i = add "?v" #> add (string_of_int i)
   14.97 -
   14.98 -fun sterm l (Old_SMT_Translate.SVar i) = sep (var (l - i - 1))
   14.99 -  | sterm l (Old_SMT_Translate.SApp (n, ts)) = app n (sterm l) ts
  14.100 -  | sterm _ (Old_SMT_Translate.SLet _) =
  14.101 -      raise Fail "SMT-LIB: unsupported let expression"
  14.102 -  | sterm l (Old_SMT_Translate.SQua (q, ss, ps, w, t)) =
  14.103 -      let
  14.104 -        fun quant Old_SMT_Translate.SForall = add "forall"
  14.105 -          | quant Old_SMT_Translate.SExists = add "exists"
  14.106 -        val vs = map_index (apfst (Integer.add l)) ss
  14.107 -        fun var_decl (i, s) = par (var i #> sep (add s))
  14.108 -        val sub = sterm (l + length ss)
  14.109 -        fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts))
  14.110 -        fun pats (Old_SMT_Translate.SPat ts) = pat ":pat" ts
  14.111 -          | pats (Old_SMT_Translate.SNoPat ts) = pat ":nopat" ts
  14.112 -        fun weight NONE = I
  14.113 -          | weight (SOME i) =
  14.114 -              sep (add ":weight { " #> add (string_of_int i) #> add " }")
  14.115 -      in
  14.116 -        par (quant q #> fold var_decl vs #> sub t #> fold pats ps #> weight w)
  14.117 -      end
  14.118 -
  14.119 -fun ssort sorts = sort fast_string_ord sorts
  14.120 -fun fsort funcs = sort (prod_ord fast_string_ord (K EQUAL)) funcs
  14.121 -
  14.122 -fun sdatatypes decls =
  14.123 -  let
  14.124 -    fun con (n, []) = sep (add n)
  14.125 -      | con (n, sels) = par (add n #>
  14.126 -          fold (fn (n, s) => par (add n #> sep (add s))) sels)
  14.127 -    fun dtyp (n, decl) = add n #> fold con decl
  14.128 -  in line (add ":datatypes " #> par (fold (par o dtyp) decls)) end
  14.129 -
  14.130 -fun serialize comments {header, sorts, dtyps, funcs} ts =
  14.131 -  Buffer.empty
  14.132 -  |> line (add "(benchmark Isabelle")
  14.133 -  |> line (add ":status unknown")
  14.134 -  |> fold (line o add) header
  14.135 -  |> length sorts > 0 ?
  14.136 -       line (add ":extrasorts" #> par (fold (sep o add) (ssort sorts)))
  14.137 -  |> fold sdatatypes dtyps
  14.138 -  |> length funcs > 0 ? (
  14.139 -       line (add ":extrafuns" #> add " (") #>
  14.140 -       fold (fn (f, (ss, s)) =>
  14.141 -         line (sep (app f (sep o add) (ss @ [s])))) (fsort funcs) #>
  14.142 -       line (add ")"))
  14.143 -  |> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts
  14.144 -  |> line (add ":formula true)")
  14.145 -  |> fold (fn str => line (add "; " #> add str)) comments
  14.146 -  |> Buffer.content
  14.147 -
  14.148 -
  14.149 -(* interface *)
  14.150 -
  14.151 -fun translate_config ctxt = {
  14.152 -  prefixes = {
  14.153 -    sort_prefix = "S",
  14.154 -    func_prefix = "f"},
  14.155 -  header = choose_logic ctxt,
  14.156 -  is_fol = true,
  14.157 -  has_datatypes = false,
  14.158 -  serialize = serialize}
  14.159 -
  14.160 -val setup = Context.theory_map (
  14.161 -  setup_builtins #>
  14.162 -  Old_SMT_Translate.add_config (smtlibC, translate_config))
  14.163 -
  14.164 -end
    15.1 --- a/src/HOL/Library/Old_SMT/old_z3_interface.ML	Thu Apr 20 10:45:52 2017 +0200
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,239 +0,0 @@
    15.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_interface.ML
    15.5 -    Author:     Sascha Boehme, TU Muenchen
    15.6 -
    15.7 -Interface to Z3 based on a relaxed version of SMT-LIB.
    15.8 -*)
    15.9 -
   15.10 -signature OLD_Z3_INTERFACE =
   15.11 -sig
   15.12 -  val smtlib_z3C: Old_SMT_Utils.class
   15.13 -  val setup: theory -> theory
   15.14 -
   15.15 -  datatype sym = Sym of string * sym list
   15.16 -  type mk_builtins = {
   15.17 -    mk_builtin_typ: sym -> typ option,
   15.18 -    mk_builtin_num: theory -> int -> typ -> cterm option,
   15.19 -    mk_builtin_fun: theory -> sym -> cterm list -> cterm option }
   15.20 -  val add_mk_builtins: mk_builtins -> Context.generic -> Context.generic
   15.21 -  val mk_builtin_typ: Proof.context -> sym -> typ option
   15.22 -  val mk_builtin_num: Proof.context -> int -> typ -> cterm option
   15.23 -  val mk_builtin_fun: Proof.context -> sym -> cterm list -> cterm option
   15.24 -
   15.25 -  val is_builtin_theory_term: Proof.context -> term -> bool
   15.26 -end
   15.27 -
   15.28 -structure Old_Z3_Interface: OLD_Z3_INTERFACE =
   15.29 -struct
   15.30 -
   15.31 -val smtlib_z3C = Old_SMTLIB_Interface.smtlibC @ ["z3"]
   15.32 -
   15.33 -
   15.34 -
   15.35 -(* interface *)
   15.36 -
   15.37 -local
   15.38 -  fun translate_config ctxt =
   15.39 -    let
   15.40 -      val {prefixes, header, is_fol, serialize, ...} =
   15.41 -        Old_SMTLIB_Interface.translate_config ctxt
   15.42 -    in
   15.43 -      {prefixes=prefixes, header=header, is_fol=is_fol, serialize=serialize,
   15.44 -        has_datatypes=true}
   15.45 -    end
   15.46 -
   15.47 -  fun is_div_mod @{const divide (int)} = true
   15.48 -    | is_div_mod @{const modulo (int)} = true
   15.49 -    | is_div_mod _ = false
   15.50 -
   15.51 -  val div_by_z3div = @{lemma
   15.52 -    "ALL k l. k div l = (
   15.53 -      if k = 0 | l = 0 then 0
   15.54 -      else if (0 < k & 0 < l) | (k < 0 & 0 < l) then z3div k l
   15.55 -      else z3div (-k) (-l))"
   15.56 -    by (simp add: z3div_def)}
   15.57 -
   15.58 -  val mod_by_z3mod = @{lemma
   15.59 -    "ALL k l. k mod l = (
   15.60 -      if l = 0 then k
   15.61 -      else if k = 0 then 0
   15.62 -      else if (0 < k & 0 < l) | (k < 0 & 0 < l) then z3mod k l
   15.63 -      else - z3mod (-k) (-l))"
   15.64 -    by (simp add: z3mod_def)}
   15.65 -
   15.66 -  val have_int_div_mod =
   15.67 -    exists (Term.exists_subterm is_div_mod o Thm.prop_of)
   15.68 -
   15.69 -  fun add_div_mod _ (thms, extra_thms) =
   15.70 -    if have_int_div_mod thms orelse have_int_div_mod extra_thms then
   15.71 -      (thms, div_by_z3div :: mod_by_z3mod :: extra_thms)
   15.72 -    else (thms, extra_thms)
   15.73 -
   15.74 -  val setup_builtins =
   15.75 -    Old_SMT_Builtin.add_builtin_fun' smtlib_z3C (@{const times (int)}, "*") #>
   15.76 -    Old_SMT_Builtin.add_builtin_fun' smtlib_z3C (@{const z3div}, "div") #>
   15.77 -    Old_SMT_Builtin.add_builtin_fun' smtlib_z3C (@{const z3mod}, "mod")
   15.78 -in
   15.79 -
   15.80 -val setup = Context.theory_map (
   15.81 -  setup_builtins #>
   15.82 -  Old_SMT_Normalize.add_extra_norm (smtlib_z3C, add_div_mod) #>
   15.83 -  Old_SMT_Translate.add_config (smtlib_z3C, translate_config))
   15.84 -
   15.85 -end
   15.86 -
   15.87 -
   15.88 -
   15.89 -(* constructors *)
   15.90 -
   15.91 -datatype sym = Sym of string * sym list
   15.92 -
   15.93 -
   15.94 -(** additional constructors **)
   15.95 -
   15.96 -type mk_builtins = {
   15.97 -  mk_builtin_typ: sym -> typ option,
   15.98 -  mk_builtin_num: theory -> int -> typ -> cterm option,
   15.99 -  mk_builtin_fun: theory -> sym -> cterm list -> cterm option }
  15.100 -
  15.101 -fun chained _ [] = NONE
  15.102 -  | chained f (b :: bs) = (case f b of SOME y => SOME y | NONE => chained f bs)
  15.103 -
  15.104 -fun chained_mk_builtin_typ bs sym =
  15.105 -  chained (fn {mk_builtin_typ=mk, ...} : mk_builtins => mk sym) bs
  15.106 -
  15.107 -fun chained_mk_builtin_num ctxt bs i T =
  15.108 -  let val thy = Proof_Context.theory_of ctxt
  15.109 -  in chained (fn {mk_builtin_num=mk, ...} : mk_builtins => mk thy i T) bs end
  15.110 -
  15.111 -fun chained_mk_builtin_fun ctxt bs s cts =
  15.112 -  let val thy = Proof_Context.theory_of ctxt
  15.113 -  in chained (fn {mk_builtin_fun=mk, ...} : mk_builtins => mk thy s cts) bs end
  15.114 -
  15.115 -fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
  15.116 -
  15.117 -structure Mk_Builtins = Generic_Data
  15.118 -(
  15.119 -  type T = (int * mk_builtins) list
  15.120 -  val empty = []
  15.121 -  val extend = I
  15.122 -  fun merge data = Ord_List.merge fst_int_ord data
  15.123 -)
  15.124 -
  15.125 -fun add_mk_builtins mk =
  15.126 -  Mk_Builtins.map (Ord_List.insert fst_int_ord (serial (), mk))
  15.127 -
  15.128 -fun get_mk_builtins ctxt = map snd (Mk_Builtins.get (Context.Proof ctxt))
  15.129 -
  15.130 -
  15.131 -(** basic and additional constructors **)
  15.132 -
  15.133 -fun mk_builtin_typ _ (Sym ("Bool", _)) = SOME @{typ bool}
  15.134 -  | mk_builtin_typ _ (Sym ("Int", _)) = SOME @{typ int}
  15.135 -  | mk_builtin_typ _ (Sym ("bool", _)) = SOME @{typ bool}  (*FIXME: legacy*)
  15.136 -  | mk_builtin_typ _ (Sym ("int", _)) = SOME @{typ int}  (*FIXME: legacy*)
  15.137 -  | mk_builtin_typ ctxt sym = chained_mk_builtin_typ (get_mk_builtins ctxt) sym
  15.138 -
  15.139 -fun mk_builtin_num _ i @{typ int} = SOME (Numeral.mk_cnumber @{ctyp int} i)
  15.140 -  | mk_builtin_num ctxt i T =
  15.141 -      chained_mk_builtin_num ctxt (get_mk_builtins ctxt) i T
  15.142 -
  15.143 -val mk_true = Thm.cterm_of @{context} (@{const Not} $ @{const False})
  15.144 -val mk_false = Thm.cterm_of @{context} @{const False}
  15.145 -val mk_not = Thm.apply (Thm.cterm_of @{context} @{const Not})
  15.146 -val mk_implies = Thm.mk_binop (Thm.cterm_of @{context} @{const HOL.implies})
  15.147 -val mk_iff = Thm.mk_binop (Thm.cterm_of @{context} @{const HOL.eq (bool)})
  15.148 -val conj = Thm.cterm_of @{context} @{const HOL.conj}
  15.149 -val disj = Thm.cterm_of @{context} @{const HOL.disj}
  15.150 -
  15.151 -fun mk_nary _ cu [] = cu
  15.152 -  | mk_nary ct _ cts = uncurry (fold_rev (Thm.mk_binop ct)) (split_last cts)
  15.153 -
  15.154 -val eq = Old_SMT_Utils.mk_const_pat @{theory} @{const_name HOL.eq} Old_SMT_Utils.destT1
  15.155 -fun mk_eq ct cu = Thm.mk_binop (Old_SMT_Utils.instT' ct eq) ct cu
  15.156 -
  15.157 -val if_term =
  15.158 -  Old_SMT_Utils.mk_const_pat @{theory} @{const_name If}
  15.159 -    (Old_SMT_Utils.destT1 o Old_SMT_Utils.destT2)
  15.160 -fun mk_if cc ct cu =
  15.161 -  Thm.mk_binop (Thm.apply (Old_SMT_Utils.instT' ct if_term) cc) ct cu
  15.162 -
  15.163 -val nil_term =
  15.164 -  Old_SMT_Utils.mk_const_pat @{theory} @{const_name Nil} Old_SMT_Utils.destT1
  15.165 -val cons_term =
  15.166 -  Old_SMT_Utils.mk_const_pat @{theory} @{const_name Cons} Old_SMT_Utils.destT1
  15.167 -fun mk_list cT cts =
  15.168 -  fold_rev (Thm.mk_binop (Old_SMT_Utils.instT cT cons_term)) cts
  15.169 -    (Old_SMT_Utils.instT cT nil_term)
  15.170 -
  15.171 -val distinct = Old_SMT_Utils.mk_const_pat @{theory} @{const_name distinct}
  15.172 -  (Old_SMT_Utils.destT1 o Old_SMT_Utils.destT1)
  15.173 -fun mk_distinct [] = mk_true
  15.174 -  | mk_distinct (cts as (ct :: _)) =
  15.175 -      Thm.apply (Old_SMT_Utils.instT' ct distinct)
  15.176 -        (mk_list (Thm.ctyp_of_cterm ct) cts)
  15.177 -
  15.178 -val access =
  15.179 -  Old_SMT_Utils.mk_const_pat @{theory} @{const_name fun_app} Old_SMT_Utils.destT1
  15.180 -fun mk_access array = Thm.apply (Old_SMT_Utils.instT' array access) array
  15.181 -
  15.182 -val update = Old_SMT_Utils.mk_const_pat @{theory} @{const_name fun_upd}
  15.183 -  (Thm.dest_ctyp o Old_SMT_Utils.destT1)
  15.184 -fun mk_update array index value =
  15.185 -  let val cTs = Thm.dest_ctyp (Thm.ctyp_of_cterm array)
  15.186 -  in
  15.187 -    Thm.apply (Thm.mk_binop (Old_SMT_Utils.instTs cTs update) array index) value
  15.188 -  end
  15.189 -
  15.190 -val mk_uminus = Thm.apply (Thm.cterm_of @{context} @{const uminus (int)})
  15.191 -val add = Thm.cterm_of @{context} @{const plus (int)}
  15.192 -val int0 = Numeral.mk_cnumber @{ctyp int} 0
  15.193 -val mk_sub = Thm.mk_binop (Thm.cterm_of @{context} @{const minus (int)})
  15.194 -val mk_mul = Thm.mk_binop (Thm.cterm_of @{context} @{const times (int)})
  15.195 -val mk_div = Thm.mk_binop (Thm.cterm_of @{context} @{const z3div})
  15.196 -val mk_mod = Thm.mk_binop (Thm.cterm_of @{context} @{const z3mod})
  15.197 -val mk_lt = Thm.mk_binop (Thm.cterm_of @{context} @{const less (int)})
  15.198 -val mk_le = Thm.mk_binop (Thm.cterm_of @{context} @{const less_eq (int)})
  15.199 -
  15.200 -fun mk_builtin_fun ctxt sym cts =
  15.201 -  (case (sym, cts) of
  15.202 -    (Sym ("true", _), []) => SOME mk_true
  15.203 -  | (Sym ("false", _), []) => SOME mk_false
  15.204 -  | (Sym ("not", _), [ct]) => SOME (mk_not ct)
  15.205 -  | (Sym ("and", _), _) => SOME (mk_nary conj mk_true cts)
  15.206 -  | (Sym ("or", _), _) => SOME (mk_nary disj mk_false cts)
  15.207 -  | (Sym ("implies", _), [ct, cu]) => SOME (mk_implies ct cu)
  15.208 -  | (Sym ("iff", _), [ct, cu]) => SOME (mk_iff ct cu)
  15.209 -  | (Sym ("~", _), [ct, cu]) => SOME (mk_iff ct cu)
  15.210 -  | (Sym ("xor", _), [ct, cu]) => SOME (mk_not (mk_iff ct cu))
  15.211 -  | (Sym ("if", _), [ct1, ct2, ct3]) => SOME (mk_if ct1 ct2 ct3)
  15.212 -  | (Sym ("ite", _), [ct1, ct2, ct3]) => SOME (mk_if ct1 ct2 ct3) (* FIXME: remove *)
  15.213 -  | (Sym ("=", _), [ct, cu]) => SOME (mk_eq ct cu)
  15.214 -  | (Sym ("distinct", _), _) => SOME (mk_distinct cts)
  15.215 -  | (Sym ("select", _), [ca, ck]) => SOME (Thm.apply (mk_access ca) ck)
  15.216 -  | (Sym ("store", _), [ca, ck, cv]) => SOME (mk_update ca ck cv)
  15.217 -  | _ =>
  15.218 -    (case (sym, try (Thm.typ_of_cterm o hd) cts, cts) of
  15.219 -      (Sym ("+", _), SOME @{typ int}, _) => SOME (mk_nary add int0 cts)
  15.220 -    | (Sym ("-", _), SOME @{typ int}, [ct]) => SOME (mk_uminus ct)
  15.221 -    | (Sym ("-", _), SOME @{typ int}, [ct, cu]) => SOME (mk_sub ct cu)
  15.222 -    | (Sym ("*", _), SOME @{typ int}, [ct, cu]) => SOME (mk_mul ct cu)
  15.223 -    | (Sym ("div", _), SOME @{typ int}, [ct, cu]) => SOME (mk_div ct cu)
  15.224 -    | (Sym ("mod", _), SOME @{typ int}, [ct, cu]) => SOME (mk_mod ct cu)
  15.225 -    | (Sym ("<", _), SOME @{typ int}, [ct, cu]) => SOME (mk_lt ct cu)
  15.226 -    | (Sym ("<=", _), SOME @{typ int}, [ct, cu]) => SOME (mk_le ct cu)
  15.227 -    | (Sym (">", _), SOME @{typ int}, [ct, cu]) => SOME (mk_lt cu ct)
  15.228 -    | (Sym (">=", _), SOME @{typ int}, [ct, cu]) => SOME (mk_le cu ct)
  15.229 -    | _ => chained_mk_builtin_fun ctxt (get_mk_builtins ctxt) sym cts))
  15.230 -
  15.231 -
  15.232 -
  15.233 -(* abstraction *)
  15.234 -
  15.235 -fun is_builtin_theory_term ctxt t =
  15.236 -  if Old_SMT_Builtin.is_builtin_num ctxt t then true
  15.237 -  else
  15.238 -    (case Term.strip_comb t of
  15.239 -      (Const c, ts) => Old_SMT_Builtin.is_builtin_fun ctxt c ts
  15.240 -    | _ => false)
  15.241 -
  15.242 -end
    16.1 --- a/src/HOL/Library/Old_SMT/old_z3_model.ML	Thu Apr 20 10:45:52 2017 +0200
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,337 +0,0 @@
    16.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_model.ML
    16.5 -    Author:     Sascha Boehme and Philipp Meyer, TU Muenchen
    16.6 -
    16.7 -Parser for counterexamples generated by Z3.
    16.8 -*)
    16.9 -
   16.10 -signature OLD_Z3_MODEL =
   16.11 -sig
   16.12 -  val parse_counterex: Proof.context -> Old_SMT_Translate.recon -> string list ->
   16.13 -    term list * term list
   16.14 -end
   16.15 -
   16.16 -structure Old_Z3_Model: OLD_Z3_MODEL =
   16.17 -struct
   16.18 -
   16.19 -
   16.20 -(* counterexample expressions *)
   16.21 -
   16.22 -datatype expr = True | False | Number of int * int option | Value of int |
   16.23 -  Array of array | App of string * expr list
   16.24 -and array = Fresh of expr | Store of (array * expr) * expr
   16.25 -
   16.26 -
   16.27 -(* parsing *)
   16.28 -
   16.29 -val space = Scan.many Symbol.is_ascii_blank
   16.30 -fun spaced p = p --| space
   16.31 -fun in_parens p = spaced (Scan.$$ "(") |-- p --| spaced (Scan.$$ ")")
   16.32 -fun in_braces p = spaced (Scan.$$ "{") |-- p --| spaced (Scan.$$ "}")
   16.33 -
   16.34 -val digit = (fn
   16.35 -  "0" => SOME 0 | "1" => SOME 1 | "2" => SOME 2 | "3" => SOME 3 |
   16.36 -  "4" => SOME 4 | "5" => SOME 5 | "6" => SOME 6 | "7" => SOME 7 |
   16.37 -  "8" => SOME 8 | "9" => SOME 9 | _ => NONE)
   16.38 -
   16.39 -val nat_num = spaced (Scan.repeat1 (Scan.some digit) >>
   16.40 -  (fn ds => fold (fn d => fn i => i * 10 + d) ds 0))
   16.41 -val int_num = spaced (Scan.optional ($$ "-" >> K (fn i => ~i)) I :|--
   16.42 -  (fn sign => nat_num >> sign))
   16.43 -
   16.44 -val is_char = Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf
   16.45 -  member (op =) (raw_explode "_+*-/%~=<>$&|?!.@^#")
   16.46 -val name = spaced (Scan.many1 is_char >> implode)
   16.47 -
   16.48 -fun $$$ s = spaced (Scan.this_string s)
   16.49 -
   16.50 -fun array_expr st = st |> in_parens (
   16.51 -  $$$ "const" |-- expr >> Fresh ||
   16.52 -  $$$ "store" |-- array_expr -- expr -- expr >> Store)
   16.53 -
   16.54 -and expr st = st |> (
   16.55 -  $$$ "true" >> K True ||
   16.56 -  $$$ "false" >> K False ||
   16.57 -  int_num -- Scan.option ($$$ "/" |-- int_num) >> Number ||
   16.58 -  $$$ "val!" |-- nat_num >> Value ||
   16.59 -  name >> (App o rpair []) ||
   16.60 -  array_expr >> Array ||
   16.61 -  in_parens (name -- Scan.repeat1 expr) >> App)
   16.62 -
   16.63 -fun args st = ($$$ "->" >> K [] || expr ::: args) st
   16.64 -val args_case = args -- expr
   16.65 -val else_case = $$$ "else" -- $$$ "->" |-- expr >> pair ([] : expr list)
   16.66 -
   16.67 -val func =
   16.68 -  let fun cases st = (else_case >> single || args_case ::: cases) st
   16.69 -  in in_braces cases end
   16.70 -
   16.71 -val cex = space |--
   16.72 -  Scan.repeat (name --| $$$ "->" -- (func || expr >> (single o pair [])))
   16.73 -
   16.74 -fun resolve terms ((n, k), cases) =
   16.75 -  (case Symtab.lookup terms n of
   16.76 -    NONE => NONE
   16.77 -  | SOME t => SOME ((t, k), cases))
   16.78 -
   16.79 -fun annotate _ (_, []) = NONE
   16.80 -  | annotate terms (n, [([], c)]) = resolve terms ((n, 0), (c, []))
   16.81 -  | annotate _ (_, [_]) = NONE
   16.82 -  | annotate terms (n, cases as (args, _) :: _) =
   16.83 -      let val (cases', (_, else_case)) = split_last cases
   16.84 -      in resolve terms ((n, length args), (else_case, cases')) end
   16.85 -
   16.86 -fun read_cex terms ls =
   16.87 -  maps (cons "\n" o raw_explode) ls
   16.88 -  |> try (fst o Scan.finite Symbol.stopper cex)
   16.89 -  |> the_default []
   16.90 -  |> map_filter (annotate terms)
   16.91 -
   16.92 -
   16.93 -(* translation into terms *)
   16.94 -
   16.95 -fun max_value vs =
   16.96 -  let
   16.97 -    fun max_val_expr (Value i) = Integer.max i
   16.98 -      | max_val_expr (App (_, es)) = fold max_val_expr es
   16.99 -      | max_val_expr (Array a) = max_val_array a
  16.100 -      | max_val_expr _ = I
  16.101 -
  16.102 -    and max_val_array (Fresh e) = max_val_expr e
  16.103 -      | max_val_array (Store ((a, e1), e2)) =
  16.104 -          max_val_array a #> max_val_expr e1 #> max_val_expr e2
  16.105 -
  16.106 -    fun max_val (_, (ec, cs)) =
  16.107 -      max_val_expr ec #> fold (fn (es, e) => fold max_val_expr (e :: es)) cs
  16.108 -
  16.109 -  in fold max_val vs ~1 end
  16.110 -
  16.111 -fun with_context terms f vs = fst (fold_map f vs (terms, max_value vs + 1))
  16.112 -
  16.113 -fun get_term n T es (cx as (terms, next_val)) =
  16.114 -  (case Symtab.lookup terms n of
  16.115 -    SOME t => ((t, es), cx)
  16.116 -  | NONE =>
  16.117 -      let val t = Var (("skolem", next_val), T)
  16.118 -      in ((t, []), (Symtab.update (n, t) terms, next_val + 1)) end)
  16.119 -
  16.120 -fun trans_expr _ True = pair @{const True}
  16.121 -  | trans_expr _ False = pair @{const False}
  16.122 -  | trans_expr T (Number (i, NONE)) = pair (HOLogic.mk_number T i)
  16.123 -  | trans_expr T (Number (i, SOME j)) =
  16.124 -      pair (Const (@{const_name divide}, [T, T] ---> T) $
  16.125 -        HOLogic.mk_number T i $ HOLogic.mk_number T j)
  16.126 -  | trans_expr T (Value i) = pair (Var (("value", i), T))
  16.127 -  | trans_expr T (Array a) = trans_array T a
  16.128 -  | trans_expr T (App (n, es)) = get_term n T es #-> (fn (t, es') =>
  16.129 -      let val Ts = fst (Old_SMT_Utils.dest_funT (length es') (Term.fastype_of t))
  16.130 -      in
  16.131 -        fold_map (uncurry trans_expr) (Ts ~~ es') #>> Term.list_comb o pair t
  16.132 -      end)
  16.133 -
  16.134 -and trans_array T a =
  16.135 -  let val (dT, rT) = Term.dest_funT T
  16.136 -  in
  16.137 -    (case a of
  16.138 -      Fresh e => trans_expr rT e #>> (fn t => Abs ("x", dT, t))
  16.139 -    | Store ((a', e1), e2) =>
  16.140 -        trans_array T a' ##>> trans_expr dT e1 ##>> trans_expr rT e2 #>>
  16.141 -        (fn ((m, k), v) =>
  16.142 -          Const (@{const_name fun_upd}, [T, dT, rT] ---> T) $ m $ k $ v))
  16.143 -  end
  16.144 -
  16.145 -fun trans_pattern T ([], e) = trans_expr T e #>> pair []
  16.146 -  | trans_pattern T (arg :: args, e) =
  16.147 -      trans_expr (Term.domain_type T) arg ##>>
  16.148 -      trans_pattern (Term.range_type T) (args, e) #>>
  16.149 -      (fn (arg', (args', e')) => (arg' :: args', e'))
  16.150 -
  16.151 -fun mk_fun_upd T U = Const (@{const_name fun_upd}, [T --> U, T, U, T] ---> U)
  16.152 -
  16.153 -fun mk_update ([], u) _ = u
  16.154 -  | mk_update ([t], u) f =
  16.155 -      uncurry mk_fun_upd (Term.dest_funT (Term.fastype_of f)) $ f $ t $ u
  16.156 -  | mk_update (t :: ts, u) f =
  16.157 -      let
  16.158 -        val (dT, rT) = Term.dest_funT (Term.fastype_of f)
  16.159 -        val (dT', rT') = Term.dest_funT rT
  16.160 -      in
  16.161 -        mk_fun_upd dT rT $ f $ t $
  16.162 -          mk_update (ts, u) (absdummy dT' (Const ("_", rT')))
  16.163 -      end
  16.164 -
  16.165 -fun mk_lambda Ts (t, pats) =
  16.166 -  fold_rev absdummy Ts t |> fold mk_update pats
  16.167 -
  16.168 -fun translate ((t, k), (e, cs)) =
  16.169 -  let
  16.170 -    val T = Term.fastype_of t
  16.171 -    val (Us, U) = Old_SMT_Utils.dest_funT k (Term.fastype_of t)
  16.172 -
  16.173 -    fun mk_full_def u' pats =
  16.174 -      pats
  16.175 -      |> filter_out (fn (_, u) => u aconv u')
  16.176 -      |> HOLogic.mk_eq o pair t o mk_lambda Us o pair u'
  16.177 -
  16.178 -    fun mk_eq (us, u) = HOLogic.mk_eq (Term.list_comb (t, us), u)
  16.179 -    fun mk_eqs u' [] = [HOLogic.mk_eq (t, u')]
  16.180 -      | mk_eqs _ pats = map mk_eq pats
  16.181 -  in
  16.182 -    trans_expr U e ##>>
  16.183 -    (if k = 0 then pair [] else fold_map (trans_pattern T) cs) #>>
  16.184 -    (fn (u', pats) => (mk_eqs u' pats, mk_full_def u' pats))
  16.185 -  end
  16.186 -
  16.187 -
  16.188 -(* normalization *)
  16.189 -
  16.190 -fun partition_eqs f =
  16.191 -  let
  16.192 -    fun part t (xs, ts) =
  16.193 -      (case try HOLogic.dest_eq t of
  16.194 -        SOME (l, r) => (case f l r of SOME x => (x::xs, ts) | _ => (xs, t::ts))
  16.195 -      | NONE => (xs, t :: ts))
  16.196 -  in (fn ts => fold part ts ([], [])) end
  16.197 -
  16.198 -fun first_eq pred =
  16.199 -  let
  16.200 -    fun part _ [] = NONE
  16.201 -      | part us (t :: ts) =
  16.202 -          (case try (pred o HOLogic.dest_eq) t of
  16.203 -            SOME (SOME lr) => SOME (lr, fold cons us ts)
  16.204 -          | _ => part (t :: us) ts)
  16.205 -  in (fn ts => part [] ts) end
  16.206 -
  16.207 -fun replace_vars tab =
  16.208 -  let
  16.209 -    fun repl v = the_default v (AList.lookup (op aconv) tab v)
  16.210 -    fun replace (v as Var _) = repl v
  16.211 -      | replace (v as Free _) = repl v
  16.212 -      | replace t = t
  16.213 -  in map (Term.map_aterms replace) end
  16.214 -
  16.215 -fun remove_int_nat_coercions (eqs, defs) =
  16.216 -  let
  16.217 -    fun mk_nat_num t i =
  16.218 -      (case try HOLogic.dest_number i of
  16.219 -        SOME (_, n) => SOME (t, HOLogic.mk_number @{typ nat} n)
  16.220 -      | NONE => NONE)
  16.221 -    fun nat_of (@{const of_nat (int)} $ (t as Var _)) i = mk_nat_num t i
  16.222 -      | nat_of (@{const nat} $ i) (t as Var _) = mk_nat_num t i
  16.223 -      | nat_of _ _ = NONE
  16.224 -    val (nats, eqs') = partition_eqs nat_of eqs
  16.225 -
  16.226 -    fun is_coercion t =
  16.227 -      (case try HOLogic.dest_eq t of
  16.228 -        SOME (@{const of_nat (int)}, _) => true
  16.229 -      | SOME (@{const nat}, _) => true
  16.230 -      | _ => false)
  16.231 -  in apply2 (replace_vars nats) (eqs', filter_out is_coercion defs) end
  16.232 -
  16.233 -fun unfold_funapp (eqs, defs) =
  16.234 -  let
  16.235 -    fun unfold_app (Const (@{const_name fun_app}, _) $ f $ t) = f $ t
  16.236 -      | unfold_app t = t
  16.237 -    fun unfold_eq ((eq as Const (@{const_name HOL.eq}, _)) $ t $ u) =
  16.238 -          eq $ unfold_app t $ u
  16.239 -      | unfold_eq t = t
  16.240 -
  16.241 -    fun is_fun_app t =
  16.242 -      (case try HOLogic.dest_eq t of
  16.243 -        SOME (Const (@{const_name fun_app}, _), _) => true
  16.244 -      | _ => false)
  16.245 -
  16.246 -  in (map unfold_eq eqs, filter_out is_fun_app defs) end
  16.247 -
  16.248 -val unfold_eqs =
  16.249 -  let
  16.250 -    val is_ground = not o Term.exists_subterm Term.is_Var
  16.251 -    fun is_non_rec (v, t) = not (Term.exists_subterm (equal v) t)
  16.252 -
  16.253 -    fun rewr_var (l as Var _, r) = if is_ground r then SOME (l, r) else NONE
  16.254 -      | rewr_var (r, l as Var _) = if is_ground r then SOME (l, r) else NONE
  16.255 -      | rewr_var _ = NONE
  16.256 -
  16.257 -    fun rewr_free' e = if is_non_rec e then SOME e else NONE
  16.258 -    fun rewr_free (e as (Free _, _)) = rewr_free' e
  16.259 -      | rewr_free (e as (_, Free _)) = rewr_free' (swap e)
  16.260 -      | rewr_free _ = NONE
  16.261 -
  16.262 -    fun is_trivial (Const (@{const_name HOL.eq}, _) $ t $ u) = t aconv u
  16.263 -      | is_trivial _ = false
  16.264 -
  16.265 -    fun replace r = replace_vars [r] #> filter_out is_trivial
  16.266 -
  16.267 -    fun unfold_vars (es, ds) =
  16.268 -      (case first_eq rewr_var es of
  16.269 -        SOME (lr, es') => unfold_vars (apply2 (replace lr) (es', ds))
  16.270 -      | NONE => (es, ds))
  16.271 -
  16.272 -    fun unfold_frees ues (es, ds) =
  16.273 -      (case first_eq rewr_free es of
  16.274 -        SOME (lr, es') =>
  16.275 -          apply2 (replace lr) (es', ds)
  16.276 -          |> unfold_frees (HOLogic.mk_eq lr :: replace lr ues)
  16.277 -      | NONE => (ues @ es, ds))
  16.278 -
  16.279 -  in unfold_vars #> unfold_frees [] end
  16.280 -
  16.281 -fun swap_free ((eq as Const (@{const_name HOL.eq}, _)) $ t $ (u as Free _)) =
  16.282 -      eq $ u $ t
  16.283 -  | swap_free t = t
  16.284 -
  16.285 -fun frees_for_vars ctxt (eqs, defs) =
  16.286 -  let
  16.287 -    fun fresh_free i T (cx as (frees, ctxt)) =
  16.288 -      (case Inttab.lookup frees i of
  16.289 -        SOME t => (t, cx)
  16.290 -      | NONE =>
  16.291 -          let
  16.292 -            val (n, ctxt') = yield_singleton Variable.variant_fixes "" ctxt
  16.293 -            val t = Free (n, T)
  16.294 -          in (t, (Inttab.update (i, t) frees, ctxt')) end)
  16.295 -
  16.296 -    fun repl_var (Var ((_, i), T)) = fresh_free i T
  16.297 -      | repl_var (t $ u) = repl_var t ##>> repl_var u #>> op $
  16.298 -      | repl_var (Abs (n, T, t)) = repl_var t #>> (fn t' => Abs (n, T, t'))
  16.299 -      | repl_var t = pair t
  16.300 -  in
  16.301 -    (Inttab.empty, ctxt)
  16.302 -    |> fold_map repl_var eqs
  16.303 -    ||>> fold_map repl_var defs
  16.304 -    |> fst
  16.305 -  end
  16.306 -
  16.307 -
  16.308 -(* overall procedure *)
  16.309 -
  16.310 -val is_free_constraint = Term.exists_subterm (fn Free _ => true | _ => false)
  16.311 -
  16.312 -fun is_free_def (Const (@{const_name HOL.eq}, _) $ Free _ $ _) = true
  16.313 -  | is_free_def _ = false
  16.314 -
  16.315 -fun defined tp =
  16.316 -  try (apply2 (fst o HOLogic.dest_eq)) tp
  16.317 -  |> the_default false o Option.map (op aconv)
  16.318 -
  16.319 -fun add_free_defs free_cs defs =
  16.320 -  let val (free_defs, defs') = List.partition is_free_def defs
  16.321 -  in (free_cs @ filter_out (member defined free_cs) free_defs, defs') end
  16.322 -
  16.323 -fun is_const_def (Const (@{const_name HOL.eq}, _) $ Const _ $ _) = true
  16.324 -  | is_const_def _ = false
  16.325 -
  16.326 -fun parse_counterex ctxt ({terms, ...} : Old_SMT_Translate.recon) ls =
  16.327 -  read_cex terms ls
  16.328 -  |> with_context terms translate
  16.329 -  |> apfst flat o split_list
  16.330 -  |> remove_int_nat_coercions
  16.331 -  |> unfold_funapp
  16.332 -  |> unfold_eqs
  16.333 -  |>> map swap_free
  16.334 -  |>> filter is_free_constraint
  16.335 -  |-> add_free_defs
  16.336 -  |> frees_for_vars ctxt
  16.337 -  ||> filter is_const_def
  16.338 -
  16.339 -end
  16.340 -
    17.1 --- a/src/HOL/Library/Old_SMT/old_z3_proof_literals.ML	Thu Apr 20 10:45:52 2017 +0200
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,363 +0,0 @@
    17.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_proof_literals.ML
    17.5 -    Author:     Sascha Boehme, TU Muenchen
    17.6 -
    17.7 -Proof tools related to conjunctions and disjunctions.
    17.8 -*)
    17.9 -
   17.10 -signature OLD_Z3_PROOF_LITERALS =
   17.11 -sig
   17.12 -  (*literal table*)
   17.13 -  type littab = thm Termtab.table
   17.14 -  val make_littab: thm list -> littab
   17.15 -  val insert_lit: thm -> littab -> littab
   17.16 -  val delete_lit: thm -> littab -> littab
   17.17 -  val lookup_lit: littab -> term -> thm option
   17.18 -  val get_first_lit: (term -> bool) -> littab -> thm option
   17.19 -
   17.20 -  (*rules*)
   17.21 -  val true_thm: thm
   17.22 -  val rewrite_true: thm
   17.23 -
   17.24 -  (*properties*)
   17.25 -  val is_conj: term -> bool
   17.26 -  val is_disj: term -> bool
   17.27 -  val exists_lit: bool -> (term -> bool) -> term -> bool
   17.28 -  val negate: cterm -> cterm
   17.29 -
   17.30 -  (*proof tools*)
   17.31 -  val explode: bool -> bool -> bool -> term list -> thm -> thm list
   17.32 -  val join: bool -> littab -> term -> thm
   17.33 -  val prove_conj_disj_eq: cterm -> thm
   17.34 -end
   17.35 -
   17.36 -structure Old_Z3_Proof_Literals: OLD_Z3_PROOF_LITERALS =
   17.37 -struct
   17.38 -
   17.39 -
   17.40 -
   17.41 -(* literal table *)
   17.42 -
   17.43 -type littab = thm Termtab.table
   17.44 -
   17.45 -fun make_littab thms =
   17.46 -  fold (Termtab.update o `Old_SMT_Utils.prop_of) thms Termtab.empty
   17.47 -
   17.48 -fun insert_lit thm = Termtab.update (`Old_SMT_Utils.prop_of thm)
   17.49 -fun delete_lit thm = Termtab.delete (Old_SMT_Utils.prop_of thm)
   17.50 -fun lookup_lit lits = Termtab.lookup lits
   17.51 -fun get_first_lit f =
   17.52 -  Termtab.get_first (fn (t, thm) => if f t then SOME thm else NONE)
   17.53 -
   17.54 -
   17.55 -
   17.56 -(* rules *)
   17.57 -
   17.58 -val true_thm = @{lemma "~False" by simp}
   17.59 -val rewrite_true = @{lemma "True == ~ False" by simp}
   17.60 -
   17.61 -
   17.62 -
   17.63 -(* properties and term operations *)
   17.64 -
   17.65 -val is_neg = (fn @{const Not} $ _ => true | _ => false)
   17.66 -fun is_neg' f = (fn @{const Not} $ t => f t | _ => false)
   17.67 -val is_dneg = is_neg' is_neg
   17.68 -val is_conj = (fn @{const HOL.conj} $ _ $ _ => true | _ => false)
   17.69 -val is_disj = (fn @{const HOL.disj} $ _ $ _ => true | _ => false)
   17.70 -
   17.71 -fun dest_disj_term' f = (fn
   17.72 -    @{const Not} $ (@{const HOL.disj} $ t $ u) => SOME (f t, f u)
   17.73 -  | _ => NONE)
   17.74 -
   17.75 -val dest_conj_term = (fn @{const HOL.conj} $ t $ u => SOME (t, u) | _ => NONE)
   17.76 -val dest_disj_term =
   17.77 -  dest_disj_term' (fn @{const Not} $ t => t | t => @{const Not} $ t)
   17.78 -
   17.79 -fun exists_lit is_conj P =
   17.80 -  let
   17.81 -    val dest = if is_conj then dest_conj_term else dest_disj_term
   17.82 -    fun exists t = P t orelse
   17.83 -      (case dest t of
   17.84 -        SOME (t1, t2) => exists t1 orelse exists t2
   17.85 -      | NONE => false)
   17.86 -  in exists end
   17.87 -
   17.88 -val negate = Thm.apply (Thm.cterm_of @{context} @{const Not})
   17.89 -
   17.90 -
   17.91 -
   17.92 -(* proof tools *)
   17.93 -
   17.94 -(** explosion of conjunctions and disjunctions **)
   17.95 -
   17.96 -local
   17.97 -  val precomp = Old_Z3_Proof_Tools.precompose2
   17.98 -
   17.99 -  fun destc ct = Thm.dest_binop (Thm.dest_arg ct)
  17.100 -  val dest_conj1 = precomp destc @{thm conjunct1}
  17.101 -  val dest_conj2 = precomp destc @{thm conjunct2}
  17.102 -  fun dest_conj_rules t =
  17.103 -    dest_conj_term t |> Option.map (K (dest_conj1, dest_conj2))
  17.104 -    
  17.105 -  fun destd f ct = f (Thm.dest_binop (Thm.dest_arg (Thm.dest_arg ct)))
  17.106 -  val dn1 = apfst Thm.dest_arg and dn2 = apsnd Thm.dest_arg
  17.107 -  val dest_disj1 = precomp (destd I) @{lemma "~(P | Q) ==> ~P" by fast}
  17.108 -  val dest_disj2 = precomp (destd dn1) @{lemma "~(~P | Q) ==> P" by fast}
  17.109 -  val dest_disj3 = precomp (destd I) @{lemma "~(P | Q) ==> ~Q" by fast}
  17.110 -  val dest_disj4 = precomp (destd dn2) @{lemma "~(P | ~Q) ==> Q" by fast}
  17.111 -
  17.112 -  fun dest_disj_rules t =
  17.113 -    (case dest_disj_term' is_neg t of
  17.114 -      SOME (true, true) => SOME (dest_disj2, dest_disj4)
  17.115 -    | SOME (true, false) => SOME (dest_disj2, dest_disj3)
  17.116 -    | SOME (false, true) => SOME (dest_disj1, dest_disj4)
  17.117 -    | SOME (false, false) => SOME (dest_disj1, dest_disj3)
  17.118 -    | NONE => NONE)
  17.119 -
  17.120 -  fun destn ct = [Thm.dest_arg (Thm.dest_arg (Thm.dest_arg ct))]
  17.121 -  val dneg_rule = Old_Z3_Proof_Tools.precompose destn @{thm notnotD}
  17.122 -in
  17.123 -
  17.124 -(*
  17.125 -  explode a term into literals and collect all rules to be able to deduce
  17.126 -  particular literals afterwards
  17.127 -*)
  17.128 -fun explode_term is_conj =
  17.129 -  let
  17.130 -    val dest = if is_conj then dest_conj_term else dest_disj_term
  17.131 -    val dest_rules = if is_conj then dest_conj_rules else dest_disj_rules
  17.132 -
  17.133 -    fun add (t, rs) = Termtab.map_default (t, rs)
  17.134 -      (fn rs' => if length rs' < length rs then rs' else rs)
  17.135 -
  17.136 -    fun explode1 rules t =
  17.137 -      (case dest t of
  17.138 -        SOME (t1, t2) =>
  17.139 -          let val (rule1, rule2) = the (dest_rules t)
  17.140 -          in
  17.141 -            explode1 (rule1 :: rules) t1 #>
  17.142 -            explode1 (rule2 :: rules) t2 #>
  17.143 -            add (t, rev rules)
  17.144 -          end
  17.145 -      | NONE => add (t, rev rules))
  17.146 -
  17.147 -    fun explode0 (@{const Not} $ (@{const Not} $ t)) =
  17.148 -          Termtab.make [(t, [dneg_rule])]
  17.149 -      | explode0 t = explode1 [] t Termtab.empty
  17.150 -
  17.151 -  in explode0 end
  17.152 -
  17.153 -(*
  17.154 -  extract a literal by applying previously collected rules
  17.155 -*)
  17.156 -fun extract_lit thm rules = fold Old_Z3_Proof_Tools.compose rules thm
  17.157 -
  17.158 -
  17.159 -(*
  17.160 -  explode a theorem into its literals
  17.161 -*)
  17.162 -fun explode is_conj full keep_intermediate stop_lits =
  17.163 -  let
  17.164 -    val dest_rules = if is_conj then dest_conj_rules else dest_disj_rules
  17.165 -    val tab = fold (Termtab.update o rpair ()) stop_lits Termtab.empty
  17.166 -
  17.167 -    fun explode1 thm =
  17.168 -      if Termtab.defined tab (Old_SMT_Utils.prop_of thm) then cons thm
  17.169 -      else
  17.170 -        (case dest_rules (Old_SMT_Utils.prop_of thm) of
  17.171 -          SOME (rule1, rule2) =>
  17.172 -            explode2 rule1 thm #>
  17.173 -            explode2 rule2 thm #>
  17.174 -            keep_intermediate ? cons thm
  17.175 -        | NONE => cons thm)
  17.176 -
  17.177 -    and explode2 dest_rule thm =
  17.178 -      if full orelse
  17.179 -        exists_lit is_conj (Termtab.defined tab) (Old_SMT_Utils.prop_of thm)
  17.180 -      then explode1 (Old_Z3_Proof_Tools.compose dest_rule thm)
  17.181 -      else cons (Old_Z3_Proof_Tools.compose dest_rule thm)
  17.182 -
  17.183 -    fun explode0 thm =
  17.184 -      if not is_conj andalso is_dneg (Old_SMT_Utils.prop_of thm)
  17.185 -      then [Old_Z3_Proof_Tools.compose dneg_rule thm]
  17.186 -      else explode1 thm []
  17.187 -
  17.188 -  in explode0 end
  17.189 -
  17.190 -end
  17.191 -
  17.192 -
  17.193 -
  17.194 -(** joining of literals to conjunctions or disjunctions **)
  17.195 -
  17.196 -local
  17.197 -  fun on_cprem i f thm = f (Thm.cprem_of thm i)
  17.198 -  fun on_cprop f thm = f (Thm.cprop_of thm)
  17.199 -  fun precomp2 f g thm = (on_cprem 1 f thm, on_cprem 2 g thm, f, g, thm)
  17.200 -  fun comp2 (cv1, cv2, f, g, rule) thm1 thm2 =
  17.201 -    Thm.instantiate ([],
  17.202 -      [(dest_Var (Thm.term_of cv1), on_cprop f thm1),
  17.203 -       (dest_Var (Thm.term_of cv2), on_cprop g thm2)]) rule
  17.204 -    |> Old_Z3_Proof_Tools.discharge thm1 |> Old_Z3_Proof_Tools.discharge thm2
  17.205 -
  17.206 -  fun d1 ct = Thm.dest_arg ct and d2 ct = Thm.dest_arg (Thm.dest_arg ct)
  17.207 -
  17.208 -  val conj_rule = precomp2 d1 d1 @{thm conjI}
  17.209 -  fun comp_conj ((_, thm1), (_, thm2)) = comp2 conj_rule thm1 thm2
  17.210 -
  17.211 -  val disj1 = precomp2 d2 d2 @{lemma "~P ==> ~Q ==> ~(P | Q)" by fast}
  17.212 -  val disj2 = precomp2 d2 d1 @{lemma "~P ==> Q ==> ~(P | ~Q)" by fast}
  17.213 -  val disj3 = precomp2 d1 d2 @{lemma "P ==> ~Q ==> ~(~P | Q)" by fast}
  17.214 -  val disj4 = precomp2 d1 d1 @{lemma "P ==> Q ==> ~(~P | ~Q)" by fast}
  17.215 -
  17.216 -  fun comp_disj ((false, thm1), (false, thm2)) = comp2 disj1 thm1 thm2
  17.217 -    | comp_disj ((false, thm1), (true, thm2)) = comp2 disj2 thm1 thm2
  17.218 -    | comp_disj ((true, thm1), (false, thm2)) = comp2 disj3 thm1 thm2
  17.219 -    | comp_disj ((true, thm1), (true, thm2)) = comp2 disj4 thm1 thm2
  17.220 -
  17.221 -  fun dest_conj (@{const HOL.conj} $ t $ u) = ((false, t), (false, u))
  17.222 -    | dest_conj t = raise TERM ("dest_conj", [t])
  17.223 -
  17.224 -  val neg = (fn @{const Not} $ t => (true, t) | t => (false, @{const Not} $ t))
  17.225 -  fun dest_disj (@{const Not} $ (@{const HOL.disj} $ t $ u)) = (neg t, neg u)
  17.226 -    | dest_disj t = raise TERM ("dest_disj", [t])
  17.227 -
  17.228 -  val precomp = Old_Z3_Proof_Tools.precompose
  17.229 -  val dnegE = precomp (single o d2 o d1) @{thm notnotD}
  17.230 -  val dnegI = precomp (single o d1) @{lemma "P ==> ~~P" by fast}
  17.231 -  fun as_dneg f t = f (@{const Not} $ (@{const Not} $ t))
  17.232 -
  17.233 -  val precomp2 = Old_Z3_Proof_Tools.precompose2
  17.234 -  fun dni f = apsnd f o Thm.dest_binop o f o d1
  17.235 -  val negIffE = precomp2 (dni d1) @{lemma "~(P = (~Q)) ==> Q = P" by fast}
  17.236 -  val negIffI = precomp2 (dni I) @{lemma "P = Q ==> ~(Q = (~P))" by fast}
  17.237 -  val iff_const = @{const HOL.eq (bool)}
  17.238 -  fun as_negIff f (@{const HOL.eq (bool)} $ t $ u) =
  17.239 -        f (@{const Not} $ (iff_const $ u $ (@{const Not} $ t)))
  17.240 -    | as_negIff _ _ = NONE
  17.241 -in
  17.242 -
  17.243 -fun join is_conj littab t =
  17.244 -  let
  17.245 -    val comp = if is_conj then comp_conj else comp_disj
  17.246 -    val dest = if is_conj then dest_conj else dest_disj
  17.247 -
  17.248 -    val lookup = lookup_lit littab
  17.249 -
  17.250 -    fun lookup_rule t =
  17.251 -      (case t of
  17.252 -        @{const Not} $ (@{const Not} $ t) =>
  17.253 -          (Old_Z3_Proof_Tools.compose dnegI, lookup t)
  17.254 -      | @{const Not} $ (@{const HOL.eq (bool)} $ t $ (@{const Not} $ u)) =>
  17.255 -          (Old_Z3_Proof_Tools.compose negIffI, lookup (iff_const $ u $ t))
  17.256 -      | @{const Not} $ ((eq as Const (@{const_name HOL.eq}, _)) $ t $ u) =>
  17.257 -          let fun rewr lit = lit COMP @{thm not_sym}
  17.258 -          in (rewr, lookup (@{const Not} $ (eq $ u $ t))) end
  17.259 -      | _ =>
  17.260 -          (case as_dneg lookup t of
  17.261 -            NONE => (Old_Z3_Proof_Tools.compose negIffE, as_negIff lookup t)
  17.262 -          | x => (Old_Z3_Proof_Tools.compose dnegE, x)))
  17.263 -
  17.264 -    fun join1 (s, t) =
  17.265 -      (case lookup t of
  17.266 -        SOME lit => (s, lit)
  17.267 -      | NONE => 
  17.268 -          (case lookup_rule t of
  17.269 -            (rewrite, SOME lit) => (s, rewrite lit)
  17.270 -          | (_, NONE) => (s, comp (apply2 join1 (dest t)))))
  17.271 -
  17.272 -  in snd (join1 (if is_conj then (false, t) else (true, t))) end
  17.273 -
  17.274 -end
  17.275 -
  17.276 -
  17.277 -
  17.278 -(** proving equality of conjunctions or disjunctions **)
  17.279 -
  17.280 -fun iff_intro thm1 thm2 = thm2 COMP (thm1 COMP @{thm iffI})
  17.281 -
  17.282 -local
  17.283 -  val cp1 = @{lemma "(~P) = (~Q) ==> P = Q" by simp}
  17.284 -  val cp2 = @{lemma "(~P) = Q ==> P = (~Q)" by fastforce}
  17.285 -  val cp3 = @{lemma "P = (~Q) ==> (~P) = Q" by simp}
  17.286 -in
  17.287 -fun contrapos1 prove (ct, cu) = prove (negate ct, negate cu) COMP cp1
  17.288 -fun contrapos2 prove (ct, cu) = prove (negate ct, Thm.dest_arg cu) COMP cp2
  17.289 -fun contrapos3 prove (ct, cu) = prove (Thm.dest_arg ct, negate cu) COMP cp3
  17.290 -end
  17.291 -
  17.292 -
  17.293 -local
  17.294 -  val contra_rule = @{lemma "P ==> ~P ==> False" by (rule notE)}
  17.295 -  fun contra_left conj thm =
  17.296 -    let
  17.297 -      val rules = explode_term conj (Old_SMT_Utils.prop_of thm)
  17.298 -      fun contra_lits (t, rs) =
  17.299 -        (case t of
  17.300 -          @{const Not} $ u => Termtab.lookup rules u |> Option.map (pair rs)
  17.301 -        | _ => NONE)
  17.302 -    in
  17.303 -      (case Termtab.lookup rules @{const False} of
  17.304 -        SOME rs => extract_lit thm rs
  17.305 -      | NONE =>
  17.306 -          the (Termtab.get_first contra_lits rules)
  17.307 -          |> apply2 (extract_lit thm)
  17.308 -          |> (fn (nlit, plit) => nlit COMP (plit COMP contra_rule)))
  17.309 -    end
  17.310 -
  17.311 -  val falseE_v = dest_Var (Thm.term_of (Thm.dest_arg (Thm.dest_arg (Thm.cprop_of @{thm FalseE}))))
  17.312 -  fun contra_right ct = Thm.instantiate ([], [(falseE_v, ct)]) @{thm FalseE}
  17.313 -in
  17.314 -fun contradict conj ct =
  17.315 -  iff_intro (Old_Z3_Proof_Tools.under_assumption (contra_left conj) ct)
  17.316 -    (contra_right ct)
  17.317 -end
  17.318 -
  17.319 -
  17.320 -local
  17.321 -  fun prove_eq l r (cl, cr) =
  17.322 -    let
  17.323 -      fun explode' is_conj = explode is_conj true (l <> r) []
  17.324 -      fun make_tab is_conj thm = make_littab (true_thm :: explode' is_conj thm)
  17.325 -      fun prove is_conj ct tab = join is_conj tab (Thm.term_of ct)
  17.326 -
  17.327 -      val thm1 = Old_Z3_Proof_Tools.under_assumption (prove r cr o make_tab l) cl
  17.328 -      val thm2 = Old_Z3_Proof_Tools.under_assumption (prove l cl o make_tab r) cr
  17.329 -    in iff_intro thm1 thm2 end
  17.330 -
  17.331 -  datatype conj_disj = CONJ | DISJ | NCON | NDIS
  17.332 -  fun kind_of t =
  17.333 -    if is_conj t then SOME CONJ
  17.334 -    else if is_disj t then SOME DISJ
  17.335 -    else if is_neg' is_conj t then SOME NCON
  17.336 -    else if is_neg' is_disj t then SOME NDIS
  17.337 -    else NONE
  17.338 -in
  17.339 -
  17.340 -fun prove_conj_disj_eq ct =
  17.341 -  let val cp as (cl, cr) = Thm.dest_binop (Thm.dest_arg ct)
  17.342 -  in
  17.343 -    (case (kind_of (Thm.term_of cl), Thm.term_of cr) of
  17.344 -      (SOME CONJ, @{const False}) => contradict true cl
  17.345 -    | (SOME DISJ, @{const Not} $ @{const False}) =>
  17.346 -        contrapos2 (contradict false o fst) cp
  17.347 -    | (kl, _) =>
  17.348 -        (case (kl, kind_of (Thm.term_of cr)) of
  17.349 -          (SOME CONJ, SOME CONJ) => prove_eq true true cp
  17.350 -        | (SOME CONJ, SOME NDIS) => prove_eq true false cp
  17.351 -        | (SOME CONJ, _) => prove_eq true true cp
  17.352 -        | (SOME DISJ, SOME DISJ) => contrapos1 (prove_eq false false) cp
  17.353 -        | (SOME DISJ, SOME NCON) => contrapos2 (prove_eq false true) cp
  17.354 -        | (SOME DISJ, _) => contrapos1 (prove_eq false false) cp
  17.355 -        | (SOME NCON, SOME NCON) => contrapos1 (prove_eq true true) cp
  17.356 -        | (SOME NCON, SOME DISJ) => contrapos3 (prove_eq true false) cp
  17.357 -        | (SOME NCON, NONE) => contrapos3 (prove_eq true false) cp
  17.358 -        | (SOME NDIS, SOME NDIS) => prove_eq false false cp
  17.359 -        | (SOME NDIS, SOME CONJ) => prove_eq false true cp
  17.360 -        | (SOME NDIS, NONE) => prove_eq false true cp
  17.361 -        | _ => raise CTERM ("prove_conj_disj_eq", [ct])))
  17.362 -  end
  17.363 -
  17.364 -end
  17.365 -
  17.366 -end
    18.1 --- a/src/HOL/Library/Old_SMT/old_z3_proof_methods.ML	Thu Apr 20 10:45:52 2017 +0200
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,149 +0,0 @@
    18.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_proof_methods.ML
    18.5 -    Author:     Sascha Boehme, TU Muenchen
    18.6 -
    18.7 -Proof methods for Z3 proof reconstruction.
    18.8 -*)
    18.9 -
   18.10 -signature OLD_Z3_PROOF_METHODS =
   18.11 -sig
   18.12 -  val prove_injectivity: Proof.context -> cterm -> thm
   18.13 -  val prove_ite: Proof.context -> cterm -> thm
   18.14 -end
   18.15 -
   18.16 -structure Old_Z3_Proof_Methods: OLD_Z3_PROOF_METHODS =
   18.17 -struct
   18.18 -
   18.19 -
   18.20 -fun apply tac st =
   18.21 -  (case Seq.pull (tac 1 st) of
   18.22 -    NONE => raise THM ("tactic failed", 1, [st])
   18.23 -  | SOME (st', _) => st')
   18.24 -
   18.25 -
   18.26 -
   18.27 -(* if-then-else *)
   18.28 -
   18.29 -val pull_ite = mk_meta_eq
   18.30 -  @{lemma "f (if P then x else y) = (if P then f x else f y)" by simp}
   18.31 -
   18.32 -fun pull_ites_conv ct =
   18.33 -  (Conv.rewr_conv pull_ite then_conv
   18.34 -   Conv.binop_conv (Conv.try_conv pull_ites_conv)) ct
   18.35 -
   18.36 -fun prove_ite ctxt =
   18.37 -  Old_Z3_Proof_Tools.by_tac ctxt (
   18.38 -    CONVERSION (Conv.arg_conv (Conv.arg1_conv pull_ites_conv))
   18.39 -    THEN' resolve_tac ctxt @{thms refl})
   18.40 -
   18.41 -
   18.42 -
   18.43 -(* injectivity *)
   18.44 -
   18.45 -local
   18.46 -
   18.47 -val B = @{typ bool}
   18.48 -fun mk_univ T = Const (@{const_name top}, HOLogic.mk_setT T)
   18.49 -fun mk_inj_on T U =
   18.50 -  Const (@{const_name inj_on}, (T --> U) --> HOLogic.mk_setT T --> B)
   18.51 -fun mk_inv_into T U =
   18.52 -  Const (@{const_name inv_into}, [HOLogic.mk_setT T, T --> U, U] ---> T)
   18.53 -
   18.54 -fun mk_inv_of ctxt ct =
   18.55 -  let
   18.56 -    val (dT, rT) = Term.dest_funT (Thm.typ_of_cterm ct)
   18.57 -    val inv = Thm.cterm_of ctxt (mk_inv_into dT rT)
   18.58 -    val univ = Thm.cterm_of ctxt (mk_univ dT)
   18.59 -  in Thm.mk_binop inv univ ct end
   18.60 -
   18.61 -fun mk_inj_prop ctxt ct =
   18.62 -  let
   18.63 -    val (dT, rT) = Term.dest_funT (Thm.typ_of_cterm ct)
   18.64 -    val inj = Thm.cterm_of ctxt (mk_inj_on dT rT)
   18.65 -    val univ = Thm.cterm_of ctxt (mk_univ dT)
   18.66 -  in Old_SMT_Utils.mk_cprop (Thm.mk_binop inj ct univ) end
   18.67 -
   18.68 -
   18.69 -val disjE = @{lemma "~P | Q ==> P ==> Q" by fast}
   18.70 -
   18.71 -fun prove_inj_prop ctxt def lhs =
   18.72 -  let
   18.73 -    val (ct, ctxt') = Old_SMT_Utils.dest_all_cabs (Thm.rhs_of def) ctxt
   18.74 -    val rule = disjE OF [Object_Logic.rulify ctxt' (Thm.assume lhs)]
   18.75 -  in
   18.76 -    Goal.init (mk_inj_prop ctxt' (Thm.dest_arg ct))
   18.77 -    |> apply (resolve_tac ctxt' @{thms injI})
   18.78 -    |> apply (Tactic.solve_tac ctxt' [rule, rule RS @{thm sym}])
   18.79 -    |> Goal.norm_result ctxt' o Goal.finish ctxt'
   18.80 -    |> singleton (Variable.export ctxt' ctxt)
   18.81 -  end
   18.82 -
   18.83 -fun prove_rhs ctxt def lhs =
   18.84 -  Old_Z3_Proof_Tools.by_tac ctxt (
   18.85 -    CONVERSION (Conv.top_sweep_conv (K (Conv.rewr_conv def)) ctxt)
   18.86 -    THEN' REPEAT_ALL_NEW (match_tac ctxt @{thms allI})
   18.87 -    THEN' resolve_tac ctxt [@{thm inv_f_f} OF [prove_inj_prop ctxt def lhs]])
   18.88 -
   18.89 -
   18.90 -fun expand thm ct =
   18.91 -  let
   18.92 -    val cpat = Thm.dest_arg (Thm.rhs_of thm)
   18.93 -    val (cl, cr) = Thm.dest_binop (Thm.dest_arg (Thm.dest_arg1 ct))
   18.94 -    val thm1 = Thm.instantiate (Thm.match (cpat, cl)) thm
   18.95 -    val thm2 = Thm.instantiate (Thm.match (cpat, cr)) thm
   18.96 -  in Conv.arg_conv (Conv.binop_conv (Conv.rewrs_conv [thm1, thm2])) ct end
   18.97 -
   18.98 -fun prove_lhs ctxt rhs =
   18.99 -  let
  18.100 -    val eq = Thm.symmetric (mk_meta_eq (Object_Logic.rulify ctxt (Thm.assume rhs)))
  18.101 -    val conv = Old_SMT_Utils.binders_conv (K (expand eq)) ctxt
  18.102 -  in
  18.103 -    Old_Z3_Proof_Tools.by_tac ctxt (
  18.104 -      CONVERSION (Old_SMT_Utils.prop_conv conv)
  18.105 -      THEN' Simplifier.simp_tac (put_simpset HOL_ss ctxt))
  18.106 -  end
  18.107 -
  18.108 -
  18.109 -fun mk_inv_def ctxt rhs =
  18.110 -  let
  18.111 -    val (ct, ctxt') =
  18.112 -      Old_SMT_Utils.dest_all_cbinders (Old_SMT_Utils.dest_cprop rhs) ctxt
  18.113 -    val (cl, cv) = Thm.dest_binop ct
  18.114 -    val (cg, (cargs, cf)) = Drule.strip_comb cl ||> split_last
  18.115 -    val cu = fold_rev Thm.lambda cargs (mk_inv_of ctxt' (Thm.lambda cv cf))
  18.116 -  in Thm.assume (Old_SMT_Utils.mk_cequals cg cu) end
  18.117 -
  18.118 -fun prove_inj_eq ctxt ct =
  18.119 -  let
  18.120 -    val (lhs, rhs) =
  18.121 -      apply2 Old_SMT_Utils.mk_cprop (Thm.dest_binop (Old_SMT_Utils.dest_cprop ct))
  18.122 -    val lhs_thm = Thm.implies_intr rhs (prove_lhs ctxt rhs lhs)
  18.123 -    val rhs_thm =
  18.124 -      Thm.implies_intr lhs (prove_rhs ctxt (mk_inv_def ctxt rhs) lhs rhs)
  18.125 -  in lhs_thm COMP (rhs_thm COMP @{thm iffI}) end
  18.126 -
  18.127 -
  18.128 -val swap_eq_thm = mk_meta_eq @{thm eq_commute}
  18.129 -val swap_disj_thm = mk_meta_eq @{thm disj_commute}
  18.130 -
  18.131 -fun swap_conv dest eq =
  18.132 -  Old_SMT_Utils.if_true_conv ((op <) o apply2 Term.size_of_term o dest)
  18.133 -    (Conv.rewr_conv eq)
  18.134 -
  18.135 -val swap_eq_conv = swap_conv HOLogic.dest_eq swap_eq_thm
  18.136 -val swap_disj_conv = swap_conv Old_SMT_Utils.dest_disj swap_disj_thm
  18.137 -
  18.138 -fun norm_conv ctxt =
  18.139 -  swap_eq_conv then_conv
  18.140 -  Conv.arg1_conv (Old_SMT_Utils.binders_conv (K swap_disj_conv) ctxt) then_conv
  18.141 -  Conv.arg_conv (Old_SMT_Utils.binders_conv (K swap_eq_conv) ctxt)
  18.142 -
  18.143 -in
  18.144 -
  18.145 -fun prove_injectivity ctxt =
  18.146 -  Old_Z3_Proof_Tools.by_tac ctxt (
  18.147 -    CONVERSION (Old_SMT_Utils.prop_conv (norm_conv ctxt))
  18.148 -    THEN' CSUBGOAL (uncurry (resolve_tac ctxt o single o prove_inj_eq ctxt)))
  18.149 -
  18.150 -end
  18.151 -
  18.152 -end
    19.1 --- a/src/HOL/Library/Old_SMT/old_z3_proof_parser.ML	Thu Apr 20 10:45:52 2017 +0200
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,446 +0,0 @@
    19.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_proof_parser.ML
    19.5 -    Author:     Sascha Boehme, TU Muenchen
    19.6 -
    19.7 -Parser for Z3 proofs.
    19.8 -*)
    19.9 -
   19.10 -signature OLD_Z3_PROOF_PARSER =
   19.11 -sig
   19.12 -  (*proof rules*)
   19.13 -  datatype rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
   19.14 -    Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
   19.15 -    Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
   19.16 -    Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars |
   19.17 -    Dest_Eq_Res | Quant_Inst | Hypothesis | Lemma | Unit_Resolution |
   19.18 -    Iff_True | Iff_False | Commutativity | Def_Axiom | Intro_Def | Apply_Def |
   19.19 -    Iff_Oeq | Nnf_Pos | Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize |
   19.20 -    Modus_Ponens_Oeq | Th_Lemma of string list
   19.21 -  val string_of_rule: rule -> string
   19.22 -
   19.23 -  (*proof parser*)
   19.24 -  datatype proof_step = Proof_Step of {
   19.25 -    rule: rule,
   19.26 -    args: cterm list,
   19.27 -    prems: int list,
   19.28 -    prop: cterm }
   19.29 -  val parse: Proof.context -> typ Symtab.table -> term Symtab.table ->
   19.30 -    string list ->
   19.31 -    (int * cterm) list * (int * proof_step) list * string list * Proof.context
   19.32 -end
   19.33 -
   19.34 -structure Old_Z3_Proof_Parser: OLD_Z3_PROOF_PARSER =
   19.35 -struct
   19.36 -
   19.37 -
   19.38 -(* proof rules *)
   19.39 -
   19.40 -datatype rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
   19.41 -  Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
   19.42 -  Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
   19.43 -  Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars | Dest_Eq_Res |
   19.44 -  Quant_Inst | Hypothesis | Lemma | Unit_Resolution | Iff_True | Iff_False |
   19.45 -  Commutativity | Def_Axiom | Intro_Def | Apply_Def | Iff_Oeq | Nnf_Pos |
   19.46 -  Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize | Modus_Ponens_Oeq |
   19.47 -  Th_Lemma of string list
   19.48 -
   19.49 -val rule_names = Symtab.make [
   19.50 -  ("true-axiom", True_Axiom),
   19.51 -  ("asserted", Asserted),
   19.52 -  ("goal", Goal),
   19.53 -  ("mp", Modus_Ponens),
   19.54 -  ("refl", Reflexivity),
   19.55 -  ("symm", Symmetry),
   19.56 -  ("trans", Transitivity),
   19.57 -  ("trans*", Transitivity_Star),
   19.58 -  ("monotonicity", Monotonicity),
   19.59 -  ("quant-intro", Quant_Intro),
   19.60 -  ("distributivity", Distributivity),
   19.61 -  ("and-elim", And_Elim),
   19.62 -  ("not-or-elim", Not_Or_Elim),
   19.63 -  ("rewrite", Rewrite),
   19.64 -  ("rewrite*", Rewrite_Star),
   19.65 -  ("pull-quant", Pull_Quant),
   19.66 -  ("pull-quant*", Pull_Quant_Star),
   19.67 -  ("push-quant", Push_Quant),
   19.68 -  ("elim-unused", Elim_Unused_Vars),
   19.69 -  ("der", Dest_Eq_Res),
   19.70 -  ("quant-inst", Quant_Inst),
   19.71 -  ("hypothesis", Hypothesis),
   19.72 -  ("lemma", Lemma),
   19.73 -  ("unit-resolution", Unit_Resolution),
   19.74 -  ("iff-true", Iff_True),
   19.75 -  ("iff-false", Iff_False),
   19.76 -  ("commutativity", Commutativity),
   19.77 -  ("def-axiom", Def_Axiom),
   19.78 -  ("intro-def", Intro_Def),
   19.79 -  ("apply-def", Apply_Def),
   19.80 -  ("iff~", Iff_Oeq),
   19.81 -  ("nnf-pos", Nnf_Pos),
   19.82 -  ("nnf-neg", Nnf_Neg),
   19.83 -  ("nnf*", Nnf_Star),
   19.84 -  ("cnf*", Cnf_Star),
   19.85 -  ("sk", Skolemize),
   19.86 -  ("mp~", Modus_Ponens_Oeq),
   19.87 -  ("th-lemma", Th_Lemma [])]
   19.88 -
   19.89 -fun string_of_rule (Th_Lemma args) = space_implode " " ("th-lemma" :: args)
   19.90 -  | string_of_rule r =
   19.91 -      let fun eq_rule (s, r') = if r = r' then SOME s else NONE 
   19.92 -      in the (Symtab.get_first eq_rule rule_names) end
   19.93 -
   19.94 -
   19.95 -
   19.96 -(* certified terms and variables *)
   19.97 -
   19.98 -val (var_prefix, decl_prefix) = ("v", "sk")
   19.99 -(*
  19.100 -  "decl_prefix" is for skolem constants (represented by free variables),
  19.101 -  "var_prefix" is for pseudo-schematic variables (schematic with respect
  19.102 -  to the Z3 proof, but represented by free variables).
  19.103 -
  19.104 -  Both prefixes must be distinct to avoid name interferences.
  19.105 -  More precisely, the naming of pseudo-schematic variables must be
  19.106 -  context-independent modulo the current proof context to be able to
  19.107 -  use fast inference kernel rules during proof reconstruction.
  19.108 -*)
  19.109 -
  19.110 -fun mk_inst ctxt vars =
  19.111 -  let
  19.112 -    val max = fold (Integer.max o fst) vars 0
  19.113 -    val ns = fst (Variable.variant_fixes (replicate (max + 1) var_prefix) ctxt)
  19.114 -    fun mk (i, v) =
  19.115 -      (dest_Var (Thm.term_of v), Thm.cterm_of ctxt (Free (nth ns i, Thm.typ_of_cterm v)))
  19.116 -  in map mk vars end
  19.117 -
  19.118 -fun close ctxt (ct, vars) =
  19.119 -  let
  19.120 -    val inst = mk_inst ctxt vars
  19.121 -    val names = fold (Term.add_free_names o Thm.term_of o snd) inst []
  19.122 -  in (Thm.instantiate_cterm ([], inst) ct, names) end
  19.123 -
  19.124 -
  19.125 -fun mk_bound ctxt (i, T) =
  19.126 -  let val ct = Thm.cterm_of ctxt (Var ((Name.uu, 0), T))
  19.127 -  in (ct, [(i, ct)]) end
  19.128 -
  19.129 -local
  19.130 -  fun mk_quant1 ctxt q T (ct, vars) =
  19.131 -    let
  19.132 -      val cv =
  19.133 -        (case AList.lookup (op =) vars 0 of
  19.134 -          SOME cv => cv
  19.135 -        | _ => Thm.cterm_of ctxt (Var ((Name.uu, Thm.maxidx_of_cterm ct + 1), T)))
  19.136 -      fun dec (i, v) = if i = 0 then NONE else SOME (i-1, v)
  19.137 -      val vars' = map_filter dec vars
  19.138 -    in (Thm.apply (Old_SMT_Utils.instT' cv q) (Thm.lambda cv ct), vars') end
  19.139 -
  19.140 -  fun quant name =
  19.141 -    Old_SMT_Utils.mk_const_pat @{theory} name (Old_SMT_Utils.destT1 o Old_SMT_Utils.destT1)
  19.142 -  val forall = quant @{const_name All}
  19.143 -  val exists = quant @{const_name Ex}
  19.144 -in
  19.145 -
  19.146 -fun mk_quant is_forall ctxt =
  19.147 -  fold_rev (mk_quant1 ctxt (if is_forall then forall else exists))
  19.148 -
  19.149 -end
  19.150 -
  19.151 -local
  19.152 -  fun prep (ct, vars) (maxidx, all_vars) =
  19.153 -    let
  19.154 -      val maxidx' = maxidx + Thm.maxidx_of_cterm ct + 1
  19.155 -
  19.156 -      fun part (i, cv) =
  19.157 -        (case AList.lookup (op =) all_vars i of
  19.158 -          SOME cu => apfst (if cu aconvc cv then I else cons (cv, cu))
  19.159 -        | NONE =>
  19.160 -            let val cv' = Thm.incr_indexes_cterm maxidx cv
  19.161 -            in apfst (cons (cv, cv')) #> apsnd (cons (i, cv')) end)
  19.162 -
  19.163 -      val (inst, vars') =
  19.164 -        if null vars then ([], vars)
  19.165 -        else fold part vars ([], [])
  19.166 -
  19.167 -    in
  19.168 -      (Thm.instantiate_cterm ([], map (apfst (dest_Var o Thm.term_of)) inst) ct,
  19.169 -        (maxidx', vars' @ all_vars))
  19.170 -    end
  19.171 -in
  19.172 -fun mk_fun f ts =
  19.173 -  let val (cts, (_, vars)) = fold_map prep ts (0, [])
  19.174 -  in f cts |> Option.map (rpair vars) end
  19.175 -end
  19.176 -
  19.177 -
  19.178 -
  19.179 -(* proof parser *)
  19.180 -
  19.181 -datatype proof_step = Proof_Step of {
  19.182 -  rule: rule,
  19.183 -  args: cterm list,
  19.184 -  prems: int list,
  19.185 -  prop: cterm }
  19.186 -
  19.187 -
  19.188 -(** parser context **)
  19.189 -
  19.190 -val not_false = Thm.cterm_of @{context} (@{const Not} $ @{const False})
  19.191 -
  19.192 -fun make_context ctxt typs terms =
  19.193 -  let
  19.194 -    val ctxt' = 
  19.195 -      ctxt
  19.196 -      |> Symtab.fold (Variable.declare_typ o snd) typs
  19.197 -      |> Symtab.fold (Variable.declare_term o snd) terms
  19.198 -
  19.199 -    fun cert @{const True} = not_false
  19.200 -      | cert t = Thm.cterm_of ctxt' t
  19.201 -
  19.202 -  in (typs, Symtab.map (K cert) terms, Inttab.empty, [], [], ctxt') end
  19.203 -
  19.204 -fun fresh_name n (typs, terms, exprs, steps, vars, ctxt) =
  19.205 -  let val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
  19.206 -  in (n', (typs, terms, exprs, steps, vars, ctxt')) end
  19.207 -
  19.208 -fun context_of (_, _, _, _, _, ctxt) = ctxt
  19.209 -
  19.210 -fun add_decl (n, T) (cx as (_, terms, _, _, _, _)) =
  19.211 -  (case Symtab.lookup terms n of
  19.212 -    SOME _ => cx
  19.213 -  | NONE => cx |> fresh_name (decl_prefix ^ n)
  19.214 -      |> (fn (m, (typs, terms, exprs, steps, vars, ctxt)) =>
  19.215 -           let
  19.216 -             val upd = Symtab.update (n, Thm.cterm_of ctxt (Free (m, T)))
  19.217 -           in (typs, upd terms, exprs, steps, vars, ctxt) end))
  19.218 -
  19.219 -fun mk_typ (typs, _, _, _, _, ctxt) (s as Old_Z3_Interface.Sym (n, _)) = 
  19.220 -  (case Old_Z3_Interface.mk_builtin_typ ctxt s of
  19.221 -    SOME T => SOME T
  19.222 -  | NONE => Symtab.lookup typs n)
  19.223 -
  19.224 -fun mk_num (_, _, _, _, _, ctxt) (i, T) =
  19.225 -  mk_fun (K (Old_Z3_Interface.mk_builtin_num ctxt i T)) []
  19.226 -
  19.227 -fun mk_app (_, terms, _, _, _, ctxt) (s as Old_Z3_Interface.Sym (n, _), es) =
  19.228 -  mk_fun (fn cts =>
  19.229 -    (case Old_Z3_Interface.mk_builtin_fun ctxt s cts of
  19.230 -      SOME ct => SOME ct
  19.231 -    | NONE =>
  19.232 -        Symtab.lookup terms n |> Option.map (Drule.list_comb o rpair cts))) es
  19.233 -
  19.234 -fun add_expr k t (typs, terms, exprs, steps, vars, ctxt) =
  19.235 -  (typs, terms, Inttab.update (k, t) exprs, steps, vars, ctxt)
  19.236 -
  19.237 -fun lookup_expr (_, _, exprs, _, _, _) = Inttab.lookup exprs
  19.238 -
  19.239 -fun add_proof_step k ((r, args), prop) cx =
  19.240 -  let
  19.241 -    val (typs, terms, exprs, steps, vars, ctxt) = cx
  19.242 -    val (ct, vs) = close ctxt prop
  19.243 -    fun part (SOME e, _) (cts, ps) = (close ctxt e :: cts, ps)
  19.244 -      | part (NONE, i) (cts, ps) = (cts, i :: ps)
  19.245 -    val (args', prems) = fold (part o `(lookup_expr cx)) args ([], [])
  19.246 -    val (cts, vss) = split_list args'
  19.247 -    val step = Proof_Step {rule=r, args=rev cts, prems=rev prems,
  19.248 -      prop = Old_SMT_Utils.mk_cprop ct}
  19.249 -    val vars' = fold (union (op =)) (vs :: vss) vars
  19.250 -  in (typs, terms, exprs, (k, step) :: steps, vars', ctxt) end
  19.251 -
  19.252 -fun finish (_, _, _, steps, vars, ctxt) =
  19.253 -  let
  19.254 -    fun coll (p as (k, Proof_Step {prems, rule, prop, ...})) (ars, ps, ids) =
  19.255 -      (case rule of
  19.256 -        Asserted => ((k, prop) :: ars, ps, ids)
  19.257 -      | Goal => ((k, prop) :: ars, ps, ids)
  19.258 -      | _ =>
  19.259 -          if Inttab.defined ids k then
  19.260 -            (ars, p :: ps, fold (Inttab.update o rpair ()) prems ids)
  19.261 -          else (ars, ps, ids))
  19.262 -
  19.263 -    val (ars, steps', _) = fold coll steps ([], [], Inttab.make [(~1, ())])
  19.264 -  in (ars, steps', vars, ctxt) end
  19.265 -
  19.266 -
  19.267 -(** core parser **)
  19.268 -
  19.269 -fun parse_exn line_no msg = raise Old_SMT_Failure.SMT (Old_SMT_Failure.Other_Failure
  19.270 -  ("Z3 proof parser (line " ^ string_of_int line_no ^ "): " ^ msg))
  19.271 -
  19.272 -fun scan_exn msg ((line_no, _), _) = parse_exn line_no msg
  19.273 -
  19.274 -fun with_info f cx =
  19.275 -  (case f ((NONE, 1), cx) of
  19.276 -    ((SOME _, _), cx') => cx'
  19.277 -  | ((_, line_no), _) => parse_exn line_no "bad proof")
  19.278 -
  19.279 -fun parse_line _ _ (st as ((SOME _, _), _)) = st
  19.280 -  | parse_line scan line ((_, line_no), cx) =
  19.281 -      let val st = ((line_no, cx), raw_explode line)
  19.282 -      in
  19.283 -        (case Scan.catch (Scan.finite' Symbol.stopper (Scan.option scan)) st of
  19.284 -          (SOME r, ((_, cx'), _)) => ((r, line_no+1), cx')
  19.285 -        | (NONE, _) => parse_exn line_no ("bad proof line: " ^ quote line))
  19.286 -      end
  19.287 -
  19.288 -fun with_context f x ((line_no, cx), st) =
  19.289 -  let val (y, cx') = f x cx
  19.290 -  in (y, ((line_no, cx'), st)) end
  19.291 -  
  19.292 -
  19.293 -fun lookup_context f x (st as ((_, cx), _)) = (f cx x, st)
  19.294 -
  19.295 -
  19.296 -(** parser combinators and parsers for basic entities **)
  19.297 -
  19.298 -fun $$ s = Scan.lift (Scan.$$ s)
  19.299 -fun this s = Scan.lift (Scan.this_string s)
  19.300 -val is_blank = Symbol.is_ascii_blank
  19.301 -fun blank st = Scan.lift (Scan.many1 is_blank) st
  19.302 -fun sep scan = blank |-- scan
  19.303 -fun seps scan = Scan.repeat (sep scan)
  19.304 -fun seps1 scan = Scan.repeat1 (sep scan)
  19.305 -fun seps_by scan_sep scan = scan ::: Scan.repeat (scan_sep |-- scan)
  19.306 -
  19.307 -val lpar = "(" and rpar = ")"
  19.308 -val lbra = "[" and rbra = "]"
  19.309 -fun par scan = $$ lpar |-- scan --| $$ rpar
  19.310 -fun bra scan = $$ lbra |-- scan --| $$ rbra
  19.311 -
  19.312 -val digit = (fn
  19.313 -  "0" => SOME 0 | "1" => SOME 1 | "2" => SOME 2 | "3" => SOME 3 |
  19.314 -  "4" => SOME 4 | "5" => SOME 5 | "6" => SOME 6 | "7" => SOME 7 |
  19.315 -  "8" => SOME 8 | "9" => SOME 9 | _ => NONE)
  19.316 -
  19.317 -fun digits st = (Scan.lift (Scan.many1 Symbol.is_ascii_digit) >> implode) st
  19.318 -
  19.319 -fun nat_num st = (Scan.lift (Scan.repeat1 (Scan.some digit)) >> (fn ds =>
  19.320 -  fold (fn d => fn i => i * 10 + d) ds 0)) st
  19.321 -
  19.322 -fun int_num st = (Scan.optional ($$ "-" >> K (fn i => ~i)) I :|--
  19.323 -  (fn sign => nat_num >> sign)) st
  19.324 -
  19.325 -val is_char = Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf
  19.326 -  member (op =) (raw_explode "_+*-/%~=<>$&|?!.@^#")
  19.327 -
  19.328 -fun name st = (Scan.lift (Scan.many1 is_char) >> implode) st
  19.329 -
  19.330 -fun sym st = (name --
  19.331 -  Scan.optional (bra (seps_by ($$ ":") sym)) [] >> Old_Z3_Interface.Sym) st
  19.332 -
  19.333 -fun id st = ($$ "#" |-- nat_num) st
  19.334 -
  19.335 -
  19.336 -(** parsers for various parts of Z3 proofs **)
  19.337 -
  19.338 -fun sort st = Scan.first [
  19.339 -  this "array" |-- bra (sort --| $$ ":" -- sort) >> (op -->),
  19.340 -  par (this "->" |-- seps1 sort) >> ((op --->) o split_last),
  19.341 -  sym :|-- (fn s as Old_Z3_Interface.Sym (n, _) => lookup_context mk_typ s :|-- (fn
  19.342 -    SOME T => Scan.succeed T
  19.343 -  | NONE => scan_exn ("unknown sort: " ^ quote n)))] st
  19.344 -
  19.345 -fun bound st = (par (this ":var" |-- sep nat_num -- sep sort) :|--
  19.346 -  lookup_context (mk_bound o context_of)) st
  19.347 -
  19.348 -fun numb (n as (i, _)) = lookup_context mk_num n :|-- (fn
  19.349 -    SOME n' => Scan.succeed n'
  19.350 -  | NONE => scan_exn ("unknown number: " ^ quote (string_of_int i)))
  19.351 -
  19.352 -fun appl (app as (Old_Z3_Interface.Sym (n, _), _)) =
  19.353 -  lookup_context mk_app app :|-- (fn 
  19.354 -      SOME app' => Scan.succeed app'
  19.355 -    | NONE => scan_exn ("unknown function symbol: " ^ quote n))
  19.356 -
  19.357 -fun bv_size st = (digits >> (fn sz =>
  19.358 -  Old_Z3_Interface.Sym ("bv", [Old_Z3_Interface.Sym (sz, [])]))) st
  19.359 -
  19.360 -fun bv_number_sort st = (bv_size :|-- lookup_context mk_typ :|-- (fn
  19.361 -    SOME cT => Scan.succeed cT
  19.362 -  | NONE => scan_exn ("unknown sort: " ^ quote "bv"))) st
  19.363 -
  19.364 -fun bv_number st =
  19.365 -  (this "bv" |-- bra (nat_num --| $$ ":" -- bv_number_sort) :|-- numb) st
  19.366 -
  19.367 -fun frac_number st = (
  19.368 -  int_num --| $$ "/" -- int_num --| this "::" -- sort :|-- (fn ((i, j), T) =>
  19.369 -    numb (i, T) -- numb (j, T) :|-- (fn (n, m) =>
  19.370 -      appl (Old_Z3_Interface.Sym ("/", []), [n, m])))) st
  19.371 -
  19.372 -fun plain_number st = (int_num --| this "::" -- sort :|-- numb) st
  19.373 -
  19.374 -fun number st = Scan.first [bv_number, frac_number, plain_number] st
  19.375 -
  19.376 -fun constant st = ((sym >> rpair []) :|-- appl) st
  19.377 -
  19.378 -fun expr_id st = (id :|-- (fn i => lookup_context lookup_expr i :|-- (fn
  19.379 -    SOME e => Scan.succeed e
  19.380 -  | NONE => scan_exn ("unknown term id: " ^ quote (string_of_int i))))) st
  19.381 -
  19.382 -fun arg st = Scan.first [expr_id, number, constant] st
  19.383 -
  19.384 -fun application st = par ((sym -- Scan.repeat1 (sep arg)) :|-- appl) st
  19.385 -
  19.386 -fun variables st = par (this "vars" |-- seps1 (par (name |-- sep sort))) st
  19.387 -
  19.388 -fun pats st = seps (par ((this ":pat" || this ":nopat") |-- seps1 id)) st
  19.389 -
  19.390 -val ctrue = Thm.cterm_of @{context} @{const True}
  19.391 -
  19.392 -fun pattern st = par (this "pattern" |-- Scan.repeat1 (sep arg) >>
  19.393 -  (the o mk_fun (K (SOME ctrue)))) st
  19.394 -
  19.395 -fun quant_kind st = st |> (
  19.396 -  this "forall" >> K (mk_quant true o context_of) ||
  19.397 -  this "exists" >> K (mk_quant false o context_of))
  19.398 -
  19.399 -fun quantifier st =
  19.400 -  (par (quant_kind -- sep variables --| pats -- sep arg) :|--
  19.401 -     lookup_context (fn cx => fn ((mk_q, Ts), body) => mk_q cx Ts body)) st
  19.402 -
  19.403 -fun expr k =
  19.404 -  Scan.first [bound, quantifier, pattern, application, number, constant] :|--
  19.405 -  with_context (pair NONE oo add_expr k)
  19.406 -
  19.407 -val rule_arg = id
  19.408 -  (* if this is modified, then 'th_lemma_arg' needs reviewing *)
  19.409 -
  19.410 -fun th_lemma_arg st = Scan.unless (sep rule_arg >> K "" || $$ rbra) (sep name) st
  19.411 -
  19.412 -fun rule_name st = ((name >> `(Symtab.lookup rule_names)) :|-- (fn 
  19.413 -    (SOME (Th_Lemma _), _) => Scan.repeat th_lemma_arg >> Th_Lemma
  19.414 -  | (SOME r, _) => Scan.succeed r
  19.415 -  | (NONE, n) => scan_exn ("unknown proof rule: " ^ quote n))) st
  19.416 -
  19.417 -fun rule f k =
  19.418 -  bra (rule_name -- seps id) --| $$ ":" -- sep arg #->
  19.419 -  with_context (pair (f k) oo add_proof_step k)
  19.420 -
  19.421 -fun decl st = (this "decl" |-- sep name --| sep (this "::") -- sep sort :|--
  19.422 -  with_context (pair NONE oo add_decl)) st
  19.423 -
  19.424 -fun def st = (id --| sep (this ":=")) st
  19.425 -
  19.426 -fun node st = st |> (
  19.427 -  decl ||
  19.428 -  def :|-- (fn k => sep (expr k) || sep (rule (K NONE) k)) ||
  19.429 -  rule SOME ~1)
  19.430 -
  19.431 -
  19.432 -(** overall parser **)
  19.433 -
  19.434 -(*
  19.435 -  Currently, terms are parsed bottom-up (i.e., along with parsing the proof
  19.436 -  text line by line), but proofs are reconstructed top-down (i.e. by an
  19.437 -  in-order top-down traversal of the proof tree/graph).  The latter approach
  19.438 -  was taken because some proof texts comprise irrelevant proof steps which
  19.439 -  will thus not be reconstructed.  This approach might also be beneficial
  19.440 -  for constructing terms, but it would also increase the complexity of the
  19.441 -  (otherwise rather modular) code.
  19.442 -*)
  19.443 -
  19.444 -fun parse ctxt typs terms proof_text =
  19.445 -  make_context ctxt typs terms
  19.446 -  |> with_info (fold (parse_line node) proof_text)
  19.447 -  |> finish
  19.448 -
  19.449 -end
    20.1 --- a/src/HOL/Library/Old_SMT/old_z3_proof_reconstruction.ML	Thu Apr 20 10:45:52 2017 +0200
    20.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.3 @@ -1,893 +0,0 @@
    20.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_proof_reconstruction.ML
    20.5 -    Author:     Sascha Boehme, TU Muenchen
    20.6 -
    20.7 -Proof reconstruction for proofs found by Z3.
    20.8 -*)
    20.9 -
   20.10 -signature OLD_Z3_PROOF_RECONSTRUCTION =
   20.11 -sig
   20.12 -  val add_z3_rule: thm -> Context.generic -> Context.generic
   20.13 -  val reconstruct: Proof.context -> Old_SMT_Translate.recon -> string list -> int list * thm
   20.14 -end
   20.15 -
   20.16 -structure Old_Z3_Proof_Reconstruction: OLD_Z3_PROOF_RECONSTRUCTION =
   20.17 -struct
   20.18 -
   20.19 -
   20.20 -fun z3_exn msg = raise Old_SMT_Failure.SMT (Old_SMT_Failure.Other_Failure
   20.21 -  ("Z3 proof reconstruction: " ^ msg))
   20.22 -
   20.23 -
   20.24 -
   20.25 -(* net of schematic rules *)
   20.26 -
   20.27 -local
   20.28 -  val description = "declaration of Z3 proof rules"
   20.29 -
   20.30 -  val eq = Thm.eq_thm
   20.31 -
   20.32 -  structure Old_Z3_Rules = Generic_Data
   20.33 -  (
   20.34 -    type T = thm Net.net
   20.35 -    val empty = Net.empty
   20.36 -    val extend = I
   20.37 -    val merge = Net.merge eq
   20.38 -  )
   20.39 -
   20.40 -  fun prep context =
   20.41 -    `Thm.prop_of o rewrite_rule (Context.proof_of context) [Old_Z3_Proof_Literals.rewrite_true]
   20.42 -
   20.43 -  fun ins thm context =
   20.44 -    context |> Old_Z3_Rules.map (fn net => Net.insert_term eq (prep context thm) net handle Net.INSERT => net)
   20.45 -  fun rem thm context =
   20.46 -    context |> Old_Z3_Rules.map (fn net => Net.delete_term eq (prep context thm) net handle Net.DELETE => net)
   20.47 -
   20.48 -  val add = Thm.declaration_attribute ins
   20.49 -  val del = Thm.declaration_attribute rem
   20.50 -in
   20.51 -
   20.52 -val add_z3_rule = ins
   20.53 -
   20.54 -fun by_schematic_rule ctxt ct =
   20.55 -  the (Old_Z3_Proof_Tools.net_instance (Old_Z3_Rules.get (Context.Proof ctxt)) ct)
   20.56 -
   20.57 -val _ = Theory.setup
   20.58 - (Attrib.setup @{binding old_z3_rule} (Attrib.add_del add del) description #>
   20.59 -  Global_Theory.add_thms_dynamic (@{binding old_z3_rule}, Net.content o Old_Z3_Rules.get))
   20.60 -
   20.61 -end
   20.62 -
   20.63 -
   20.64 -
   20.65 -(* proof tools *)
   20.66 -
   20.67 -fun named ctxt name prover ct =
   20.68 -  let val _ = Old_SMT_Config.trace_msg ctxt I ("Z3: trying " ^ name ^ " ...")
   20.69 -  in prover ct end
   20.70 -
   20.71 -fun NAMED ctxt name tac i st =
   20.72 -  let val _ = Old_SMT_Config.trace_msg ctxt I ("Z3: trying " ^ name ^ " ...")
   20.73 -  in tac i st end
   20.74 -
   20.75 -fun pretty_goal ctxt thms t =
   20.76 -  [Pretty.block [Pretty.str "proposition: ", Syntax.pretty_term ctxt t]]
   20.77 -  |> not (null thms) ? cons (Pretty.big_list "assumptions:"
   20.78 -       (map (Thm.pretty_thm ctxt) thms))
   20.79 -
   20.80 -fun try_apply ctxt thms =
   20.81 -  let
   20.82 -    fun try_apply_err ct = Pretty.string_of (Pretty.chunks [
   20.83 -      Pretty.big_list ("Z3 found a proof," ^
   20.84 -        " but proof reconstruction failed at the following subgoal:")
   20.85 -        (pretty_goal ctxt thms (Thm.term_of ct)),
   20.86 -      Pretty.str ("Declaring a rule as [old_z3_rule] might solve this problem.")])
   20.87 -
   20.88 -    fun apply [] ct = error (try_apply_err ct)
   20.89 -      | apply (prover :: provers) ct =
   20.90 -          (case try prover ct of
   20.91 -            SOME thm => (Old_SMT_Config.trace_msg ctxt I "Z3: succeeded"; thm)
   20.92 -          | NONE => apply provers ct)
   20.93 -
   20.94 -    fun schematic_label full = "schematic rules" |> full ? suffix " (full)"
   20.95 -    fun schematic ctxt full ct =
   20.96 -      ct
   20.97 -      |> full ? fold_rev (curry Drule.mk_implies o Thm.cprop_of) thms
   20.98 -      |> named ctxt (schematic_label full) (by_schematic_rule ctxt)
   20.99 -      |> fold Thm.elim_implies thms
  20.100 -
  20.101 -  in apply o cons (schematic ctxt false) o cons (schematic ctxt true) end
  20.102 -
  20.103 -local
  20.104 -  val rewr_if =
  20.105 -    @{lemma "(if P then Q1 else Q2) = ((P --> Q1) & (~P --> Q2))" by simp}
  20.106 -in
  20.107 -
  20.108 -fun HOL_fast_tac ctxt =
  20.109 -  Classical.fast_tac (put_claset HOL_cs ctxt)
  20.110 -
  20.111 -fun simp_fast_tac ctxt =
  20.112 -  Simplifier.simp_tac (put_simpset HOL_ss ctxt addsimps [rewr_if])
  20.113 -  THEN_ALL_NEW HOL_fast_tac ctxt
  20.114 -
  20.115 -end
  20.116 -
  20.117 -
  20.118 -
  20.119 -(* theorems and proofs *)
  20.120 -
  20.121 -(** theorem incarnations **)
  20.122 -
  20.123 -datatype theorem =
  20.124 -  Thm of thm | (* theorem without special features *)
  20.125 -  MetaEq of thm | (* meta equality "t == s" *)
  20.126 -  Literals of thm * Old_Z3_Proof_Literals.littab
  20.127 -    (* "P1 & ... & Pn" and table of all literals P1, ..., Pn *)
  20.128 -
  20.129 -fun thm_of (Thm thm) = thm
  20.130 -  | thm_of (MetaEq thm) = thm COMP @{thm meta_eq_to_obj_eq}
  20.131 -  | thm_of (Literals (thm, _)) = thm
  20.132 -
  20.133 -fun meta_eq_of (MetaEq thm) = thm
  20.134 -  | meta_eq_of p = mk_meta_eq (thm_of p)
  20.135 -
  20.136 -fun literals_of (Literals (_, lits)) = lits
  20.137 -  | literals_of p = Old_Z3_Proof_Literals.make_littab [thm_of p]
  20.138 -
  20.139 -
  20.140 -
  20.141 -(** core proof rules **)
  20.142 -
  20.143 -(* assumption *)
  20.144 -
  20.145 -local
  20.146 -  val remove_trigger = mk_meta_eq @{thm trigger_def}
  20.147 -  val remove_weight = mk_meta_eq @{thm weight_def}
  20.148 -  val remove_fun_app = mk_meta_eq @{thm fun_app_def}
  20.149 -
  20.150 -  fun rewrite_conv _ [] = Conv.all_conv
  20.151 -    | rewrite_conv ctxt eqs = Simplifier.full_rewrite (empty_simpset ctxt addsimps eqs)
  20.152 -
  20.153 -  val prep_rules = [@{thm Let_def}, remove_trigger, remove_weight,
  20.154 -    remove_fun_app, Old_Z3_Proof_Literals.rewrite_true]
  20.155 -
  20.156 -  fun rewrite _ [] = I
  20.157 -    | rewrite ctxt eqs = Conv.fconv_rule (rewrite_conv ctxt eqs)
  20.158 -
  20.159 -  fun lookup_assm assms_net ct =
  20.160 -    Old_Z3_Proof_Tools.net_instances assms_net ct
  20.161 -    |> map (fn ithm as (_, thm) => (ithm, Thm.cprop_of thm aconvc ct))
  20.162 -in
  20.163 -
  20.164 -fun add_asserted outer_ctxt rewrite_rules assms asserted ctxt =
  20.165 -  let
  20.166 -    val eqs = map (rewrite ctxt [Old_Z3_Proof_Literals.rewrite_true]) rewrite_rules
  20.167 -    val eqs' = union Thm.eq_thm eqs prep_rules
  20.168 -
  20.169 -    val assms_net =
  20.170 -      assms
  20.171 -      |> map (apsnd (rewrite ctxt eqs'))
  20.172 -      |> map (apsnd (Conv.fconv_rule Thm.eta_conversion))
  20.173 -      |> Old_Z3_Proof_Tools.thm_net_of snd
  20.174 -
  20.175 -    fun revert_conv ctxt = rewrite_conv ctxt eqs' then_conv Thm.eta_conversion
  20.176 -
  20.177 -    fun assume thm ctxt =
  20.178 -      let
  20.179 -        val ct = Thm.cprem_of thm 1
  20.180 -        val (thm', ctxt') = yield_singleton Assumption.add_assumes ct ctxt
  20.181 -      in (Thm.implies_elim thm thm', ctxt') end
  20.182 -
  20.183 -    fun add1 idx thm1 ((i, th), exact) ((is, thms), (ctxt, ptab)) =
  20.184 -      let
  20.185 -        val (thm, ctxt') =
  20.186 -          if exact then (Thm.implies_elim thm1 th, ctxt)
  20.187 -          else assume thm1 ctxt
  20.188 -        val thms' = if exact then thms else th :: thms
  20.189 -      in
  20.190 -        ((insert (op =) i is, thms'),
  20.191 -          (ctxt', Inttab.update (idx, Thm thm) ptab))
  20.192 -      end
  20.193 -
  20.194 -    fun add (idx, ct) (cx as ((is, thms), (ctxt, ptab))) =
  20.195 -      let
  20.196 -        val thm1 =
  20.197 -          Thm.trivial ct
  20.198 -          |> Conv.fconv_rule (Conv.arg1_conv (revert_conv outer_ctxt))
  20.199 -        val thm2 = singleton (Variable.export ctxt outer_ctxt) thm1
  20.200 -      in
  20.201 -        (case lookup_assm assms_net (Thm.cprem_of thm2 1) of
  20.202 -          [] =>
  20.203 -            let val (thm, ctxt') = assume thm1 ctxt
  20.204 -            in ((is, thms), (ctxt', Inttab.update (idx, Thm thm) ptab)) end
  20.205 -        | ithms => fold (add1 idx thm1) ithms cx)
  20.206 -      end
  20.207 -  in fold add asserted (([], []), (ctxt, Inttab.empty)) end
  20.208 -
  20.209 -end
  20.210 -
  20.211 -
  20.212 -(* P = Q ==> P ==> Q   or   P --> Q ==> P ==> Q *)
  20.213 -local
  20.214 -  val precomp = Old_Z3_Proof_Tools.precompose2
  20.215 -  val comp = Old_Z3_Proof_Tools.compose
  20.216 -
  20.217 -  val meta_iffD1 = @{lemma "P == Q ==> P ==> (Q::bool)" by simp}
  20.218 -  val meta_iffD1_c = precomp Thm.dest_binop meta_iffD1
  20.219 -
  20.220 -  val iffD1_c = precomp (Thm.dest_binop o Thm.dest_arg) @{thm iffD1}
  20.221 -  val mp_c = precomp (Thm.dest_binop o Thm.dest_arg) @{thm mp}
  20.222 -in
  20.223 -fun mp (MetaEq thm) p = Thm (Thm.implies_elim (comp meta_iffD1_c thm) p)
  20.224 -  | mp p_q p =
  20.225 -      let
  20.226 -        val pq = thm_of p_q
  20.227 -        val thm = comp iffD1_c pq handle THM _ => comp mp_c pq
  20.228 -      in Thm (Thm.implies_elim thm p) end
  20.229 -end
  20.230 -
  20.231 -
  20.232 -(* and_elim:     P1 & ... & Pn ==> Pi *)
  20.233 -(* not_or_elim:  ~(P1 | ... | Pn) ==> ~Pi *)
  20.234 -local
  20.235 -  fun is_sublit conj t = Old_Z3_Proof_Literals.exists_lit conj (fn u => u aconv t)
  20.236 -
  20.237 -  fun derive conj t lits idx ptab =
  20.238 -    let
  20.239 -      val lit = the (Old_Z3_Proof_Literals.get_first_lit (is_sublit conj t) lits)
  20.240 -      val ls = Old_Z3_Proof_Literals.explode conj false false [t] lit
  20.241 -      val lits' = fold Old_Z3_Proof_Literals.insert_lit ls
  20.242 -        (Old_Z3_Proof_Literals.delete_lit lit lits)
  20.243 -
  20.244 -      fun upd thm = Literals (thm_of thm, lits')
  20.245 -      val ptab' = Inttab.map_entry idx upd ptab
  20.246 -    in (the (Old_Z3_Proof_Literals.lookup_lit lits' t), ptab') end
  20.247 -
  20.248 -  fun lit_elim conj (p, idx) ct ptab =
  20.249 -    let val lits = literals_of p
  20.250 -    in
  20.251 -      (case Old_Z3_Proof_Literals.lookup_lit lits (Old_SMT_Utils.term_of ct) of
  20.252 -        SOME lit => (Thm lit, ptab)
  20.253 -      | NONE => apfst Thm (derive conj (Old_SMT_Utils.term_of ct) lits idx ptab))
  20.254 -    end
  20.255 -in
  20.256 -val and_elim = lit_elim true
  20.257 -val not_or_elim = lit_elim false
  20.258 -end
  20.259 -
  20.260 -
  20.261 -(* P1, ..., Pn |- False ==> |- ~P1 | ... | ~Pn *)
  20.262 -local
  20.263 -  fun step lit thm =
  20.264 -    Thm.implies_elim (Thm.implies_intr (Thm.cprop_of lit) thm) lit
  20.265 -  val explode_disj = Old_Z3_Proof_Literals.explode false false false
  20.266 -  fun intro hyps thm th = fold step (explode_disj hyps th) thm
  20.267 -
  20.268 -  fun dest_ccontr ct = [Thm.dest_arg (Thm.dest_arg (Thm.dest_arg1 ct))]
  20.269 -  val ccontr = Old_Z3_Proof_Tools.precompose dest_ccontr @{thm ccontr}
  20.270 -in
  20.271 -fun lemma thm ct =
  20.272 -  let
  20.273 -    val cu = Old_Z3_Proof_Literals.negate (Thm.dest_arg ct)
  20.274 -    val hyps = map_filter (try HOLogic.dest_Trueprop) (Thm.hyps_of thm)
  20.275 -    val th = Old_Z3_Proof_Tools.under_assumption (intro hyps thm) cu
  20.276 -  in Thm (Old_Z3_Proof_Tools.compose ccontr th) end
  20.277 -end
  20.278 -
  20.279 -
  20.280 -(* \/{P1, ..., Pn, Q1, ..., Qn}, ~P1, ..., ~Pn ==> \/{Q1, ..., Qn} *)
  20.281 -local
  20.282 -  val explode_disj = Old_Z3_Proof_Literals.explode false true false
  20.283 -  val join_disj = Old_Z3_Proof_Literals.join false
  20.284 -  fun unit thm thms th =
  20.285 -    let
  20.286 -      val t = @{const Not} $ Old_SMT_Utils.prop_of thm
  20.287 -      val ts = map Old_SMT_Utils.prop_of thms
  20.288 -    in
  20.289 -      join_disj (Old_Z3_Proof_Literals.make_littab (thms @ explode_disj ts th)) t
  20.290 -    end
  20.291 -
  20.292 -  fun dest_arg2 ct = Thm.dest_arg (Thm.dest_arg ct)
  20.293 -  fun dest ct = apply2 dest_arg2 (Thm.dest_binop ct)
  20.294 -  val contrapos =
  20.295 -    Old_Z3_Proof_Tools.precompose2 dest @{lemma "(~P ==> ~Q) ==> Q ==> P" by fast}
  20.296 -in
  20.297 -fun unit_resolution thm thms ct =
  20.298 -  Old_Z3_Proof_Literals.negate (Thm.dest_arg ct)
  20.299 -  |> Old_Z3_Proof_Tools.under_assumption (unit thm thms)
  20.300 -  |> Thm o Old_Z3_Proof_Tools.discharge thm o Old_Z3_Proof_Tools.compose contrapos
  20.301 -end
  20.302 -
  20.303 -
  20.304 -(* P ==> P == True   or   P ==> P == False *)
  20.305 -local
  20.306 -  val iff1 = @{lemma "P ==> P == (~ False)" by simp}
  20.307 -  val iff2 = @{lemma "~P ==> P == False" by simp}
  20.308 -in
  20.309 -fun iff_true thm = MetaEq (thm COMP iff1)
  20.310 -fun iff_false thm = MetaEq (thm COMP iff2)
  20.311 -end
  20.312 -
  20.313 -
  20.314 -(* distributivity of | over & *)
  20.315 -fun distributivity ctxt = Thm o try_apply ctxt [] [
  20.316 -  named ctxt "fast" (Old_Z3_Proof_Tools.by_tac ctxt (HOL_fast_tac ctxt))]
  20.317 -    (* FIXME: not very well tested *)
  20.318 -
  20.319 -
  20.320 -(* Tseitin-like axioms *)
  20.321 -local
  20.322 -  val disjI1 = @{lemma "(P ==> Q) ==> ~P | Q" by fast}
  20.323 -  val disjI2 = @{lemma "(~P ==> Q) ==> P | Q" by fast}
  20.324 -  val disjI3 = @{lemma "(~Q ==> P) ==> P | Q" by fast}
  20.325 -  val disjI4 = @{lemma "(Q ==> P) ==> P | ~Q" by fast}
  20.326 -
  20.327 -  fun prove' conj1 conj2 ct2 thm =
  20.328 -    let
  20.329 -      val littab =
  20.330 -        Old_Z3_Proof_Literals.explode conj1 true (conj1 <> conj2) [] thm
  20.331 -        |> cons Old_Z3_Proof_Literals.true_thm
  20.332 -        |> Old_Z3_Proof_Literals.make_littab
  20.333 -    in Old_Z3_Proof_Literals.join conj2 littab (Thm.term_of ct2) end
  20.334 -
  20.335 -  fun prove rule (ct1, conj1) (ct2, conj2) =
  20.336 -    Old_Z3_Proof_Tools.under_assumption (prove' conj1 conj2 ct2) ct1 COMP rule
  20.337 -
  20.338 -  fun prove_def_axiom ct =
  20.339 -    let val (ct1, ct2) = Thm.dest_binop (Thm.dest_arg ct)
  20.340 -    in
  20.341 -      (case Thm.term_of ct1 of
  20.342 -        @{const Not} $ (@{const HOL.conj} $ _ $ _) =>
  20.343 -          prove disjI1 (Thm.dest_arg ct1, true) (ct2, true)
  20.344 -      | @{const HOL.conj} $ _ $ _ =>
  20.345 -          prove disjI3 (Old_Z3_Proof_Literals.negate ct2, false) (ct1, true)
  20.346 -      | @{const Not} $ (@{const HOL.disj} $ _ $ _) =>
  20.347 -          prove disjI3 (Old_Z3_Proof_Literals.negate ct2, false) (ct1, false)
  20.348 -      | @{const HOL.disj} $ _ $ _ =>
  20.349 -          prove disjI2 (Old_Z3_Proof_Literals.negate ct1, false) (ct2, true)
  20.350 -      | Const (@{const_name distinct}, _) $ _ =>
  20.351 -          let
  20.352 -            fun dis_conv cv = Conv.arg_conv (Conv.arg1_conv cv)
  20.353 -            val unfold_dis_conv = dis_conv Old_Z3_Proof_Tools.unfold_distinct_conv
  20.354 -            fun prv cu =
  20.355 -              let val (cu1, cu2) = Thm.dest_binop (Thm.dest_arg cu)
  20.356 -              in prove disjI4 (Thm.dest_arg cu2, true) (cu1, true) end
  20.357 -          in Old_Z3_Proof_Tools.with_conv unfold_dis_conv prv ct end
  20.358 -      | @{const Not} $ (Const (@{const_name distinct}, _) $ _) =>
  20.359 -          let
  20.360 -            fun dis_conv cv = Conv.arg_conv (Conv.arg1_conv (Conv.arg_conv cv))
  20.361 -            val unfold_dis_conv = dis_conv Old_Z3_Proof_Tools.unfold_distinct_conv
  20.362 -            fun prv cu =
  20.363 -              let val (cu1, cu2) = Thm.dest_binop (Thm.dest_arg cu)
  20.364 -              in prove disjI1 (Thm.dest_arg cu1, true) (cu2, true) end
  20.365 -          in Old_Z3_Proof_Tools.with_conv unfold_dis_conv prv ct end
  20.366 -      | _ => raise CTERM ("prove_def_axiom", [ct]))
  20.367 -    end
  20.368 -in
  20.369 -fun def_axiom ctxt = Thm o try_apply ctxt [] [
  20.370 -  named ctxt "conj/disj/distinct" prove_def_axiom,
  20.371 -  Old_Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
  20.372 -    named ctxt' "simp+fast" (Old_Z3_Proof_Tools.by_tac ctxt (simp_fast_tac ctxt')))]
  20.373 -end
  20.374 -
  20.375 -
  20.376 -(* local definitions *)
  20.377 -local
  20.378 -  val intro_rules = [
  20.379 -    @{lemma "n == P ==> (~n | P) & (n | ~P)" by simp},
  20.380 -    @{lemma "n == (if P then s else t) ==> (~P | n = s) & (P | n = t)"
  20.381 -      by simp},
  20.382 -    @{lemma "n == P ==> n = P" by (rule meta_eq_to_obj_eq)} ]
  20.383 -
  20.384 -  val apply_rules = [
  20.385 -    @{lemma "(~n | P) & (n | ~P) ==> P == n" by (atomize(full)) fast},
  20.386 -    @{lemma "(~P | n = s) & (P | n = t) ==> (if P then s else t) == n"
  20.387 -      by (atomize(full)) fastforce} ]
  20.388 -
  20.389 -  val inst_rule = Old_Z3_Proof_Tools.match_instantiate Thm.dest_arg
  20.390 -
  20.391 -  fun apply_rule ct =
  20.392 -    (case get_first (try (inst_rule ct)) intro_rules of
  20.393 -      SOME thm => thm
  20.394 -    | NONE => raise CTERM ("intro_def", [ct]))
  20.395 -in
  20.396 -fun intro_def ct = Old_Z3_Proof_Tools.make_hyp_def (apply_rule ct) #>> Thm
  20.397 -
  20.398 -fun apply_def thm =
  20.399 -  get_first (try (fn rule => MetaEq (thm COMP rule))) apply_rules
  20.400 -  |> the_default (Thm thm)
  20.401 -end
  20.402 -
  20.403 -
  20.404 -(* negation normal form *)
  20.405 -local
  20.406 -  val quant_rules1 = ([
  20.407 -    @{lemma "(!!x. P x == Q) ==> ALL x. P x == Q" by simp},
  20.408 -    @{lemma "(!!x. P x == Q) ==> EX x. P x == Q" by simp}], [
  20.409 -    @{lemma "(!!x. P x == Q x) ==> ALL x. P x == ALL x. Q x" by simp},
  20.410 -    @{lemma "(!!x. P x == Q x) ==> EX x. P x == EX x. Q x" by simp}])
  20.411 -
  20.412 -  val quant_rules2 = ([
  20.413 -    @{lemma "(!!x. ~P x == Q) ==> ~(ALL x. P x) == Q" by simp},
  20.414 -    @{lemma "(!!x. ~P x == Q) ==> ~(EX x. P x) == Q" by simp}], [
  20.415 -    @{lemma "(!!x. ~P x == Q x) ==> ~(ALL x. P x) == EX x. Q x" by simp},
  20.416 -    @{lemma "(!!x. ~P x == Q x) ==> ~(EX x. P x) == ALL x. Q x" by simp}])
  20.417 -
  20.418 -  fun nnf_quant_tac ctxt thm (qs as (qs1, qs2)) i st = (
  20.419 -    resolve_tac ctxt [thm] ORELSE'
  20.420 -    (match_tac ctxt qs1 THEN' nnf_quant_tac ctxt thm qs) ORELSE'
  20.421 -    (match_tac ctxt qs2 THEN' nnf_quant_tac ctxt thm qs)) i st
  20.422 -
  20.423 -  fun nnf_quant_tac_varified ctxt vars eq =
  20.424 -    nnf_quant_tac ctxt (Old_Z3_Proof_Tools.varify vars eq)
  20.425 -
  20.426 -  fun nnf_quant ctxt vars qs p ct =
  20.427 -    Old_Z3_Proof_Tools.as_meta_eq ct
  20.428 -    |> Old_Z3_Proof_Tools.by_tac ctxt (nnf_quant_tac_varified ctxt vars (meta_eq_of p) qs)
  20.429 -
  20.430 -  fun prove_nnf ctxt = try_apply ctxt [] [
  20.431 -    named ctxt "conj/disj" Old_Z3_Proof_Literals.prove_conj_disj_eq,
  20.432 -    Old_Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
  20.433 -      named ctxt' "simp+fast" (Old_Z3_Proof_Tools.by_tac ctxt' (simp_fast_tac ctxt')))]
  20.434 -in
  20.435 -fun nnf ctxt vars ps ct =
  20.436 -  (case Old_SMT_Utils.term_of ct of
  20.437 -    _ $ (l as Const _ $ Abs _) $ (r as Const _ $ Abs _) =>
  20.438 -      if l aconv r
  20.439 -      then MetaEq (Thm.reflexive (Thm.dest_arg (Thm.dest_arg ct)))
  20.440 -      else MetaEq (nnf_quant ctxt vars quant_rules1 (hd ps) ct)
  20.441 -  | _ $ (@{const Not} $ (Const _ $ Abs _)) $ (Const _ $ Abs _) =>
  20.442 -      MetaEq (nnf_quant ctxt vars quant_rules2 (hd ps) ct)
  20.443 -  | _ =>
  20.444 -      let
  20.445 -        val nnf_rewr_conv = Conv.arg_conv (Conv.arg_conv
  20.446 -          (Old_Z3_Proof_Tools.unfold_eqs ctxt
  20.447 -            (map (Thm.symmetric o meta_eq_of) ps)))
  20.448 -      in Thm (Old_Z3_Proof_Tools.with_conv nnf_rewr_conv (prove_nnf ctxt) ct) end)
  20.449 -end
  20.450 -
  20.451 -
  20.452 -
  20.453 -(** equality proof rules **)
  20.454 -
  20.455 -(* |- t = t *)
  20.456 -fun refl ct = MetaEq (Thm.reflexive (Thm.dest_arg (Thm.dest_arg ct)))
  20.457 -
  20.458 -
  20.459 -(* s = t ==> t = s *)
  20.460 -local
  20.461 -  val symm_rule = @{lemma "s = t ==> t == s" by simp}
  20.462 -in
  20.463 -fun symm (MetaEq thm) = MetaEq (Thm.symmetric thm)
  20.464 -  | symm p = MetaEq (thm_of p COMP symm_rule)
  20.465 -end
  20.466 -
  20.467 -
  20.468 -(* s = t ==> t = u ==> s = u *)
  20.469 -local
  20.470 -  val trans1 = @{lemma "s == t ==> t =  u ==> s == u" by simp}
  20.471 -  val trans2 = @{lemma "s =  t ==> t == u ==> s == u" by simp}
  20.472 -  val trans3 = @{lemma "s =  t ==> t =  u ==> s == u" by simp}
  20.473 -in
  20.474 -fun trans (MetaEq thm1) (MetaEq thm2) = MetaEq (Thm.transitive thm1 thm2)
  20.475 -  | trans (MetaEq thm) q = MetaEq (thm_of q COMP (thm COMP trans1))
  20.476 -  | trans p (MetaEq thm) = MetaEq (thm COMP (thm_of p COMP trans2))
  20.477 -  | trans p q = MetaEq (thm_of q COMP (thm_of p COMP trans3))
  20.478 -end
  20.479 -
  20.480 -
  20.481 -(* t1 = s1 ==> ... ==> tn = sn ==> f t1 ... tn = f s1 .. sn
  20.482 -   (reflexive antecendents are droppped) *)
  20.483 -local
  20.484 -  exception MONO
  20.485 -
  20.486 -  fun prove_refl (ct, _) = Thm.reflexive ct
  20.487 -  fun prove_comb f g cp =
  20.488 -    let val ((ct1, ct2), (cu1, cu2)) = apply2 Thm.dest_comb cp
  20.489 -    in Thm.combination (f (ct1, cu1)) (g (ct2, cu2)) end
  20.490 -  fun prove_arg f = prove_comb prove_refl f
  20.491 -
  20.492 -  fun prove f cp = prove_comb (prove f) f cp handle CTERM _ => prove_refl cp
  20.493 -
  20.494 -  fun prove_nary is_comb f =
  20.495 -    let
  20.496 -      fun prove (cp as (ct, _)) = f cp handle MONO =>
  20.497 -        if is_comb (Thm.term_of ct)
  20.498 -        then prove_comb (prove_arg prove) prove cp
  20.499 -        else prove_refl cp
  20.500 -    in prove end
  20.501 -
  20.502 -  fun prove_list f n cp =
  20.503 -    if n = 0 then prove_refl cp
  20.504 -    else prove_comb (prove_arg f) (prove_list f (n-1)) cp
  20.505 -
  20.506 -  fun with_length f (cp as (cl, _)) =
  20.507 -    f (length (HOLogic.dest_list (Thm.term_of cl))) cp
  20.508 -
  20.509 -  fun prove_distinct f = prove_arg (with_length (prove_list f))
  20.510 -
  20.511 -  fun prove_eq exn lookup cp =
  20.512 -    (case lookup (Logic.mk_equals (apply2 Thm.term_of cp)) of
  20.513 -      SOME eq => eq
  20.514 -    | NONE => if exn then raise MONO else prove_refl cp)
  20.515 -
  20.516 -  val prove_exn = prove_eq true
  20.517 -  and prove_safe = prove_eq false
  20.518 -
  20.519 -  fun mono f (cp as (cl, _)) =
  20.520 -    (case Term.head_of (Thm.term_of cl) of
  20.521 -      @{const HOL.conj} => prove_nary Old_Z3_Proof_Literals.is_conj (prove_exn f)
  20.522 -    | @{const HOL.disj} => prove_nary Old_Z3_Proof_Literals.is_disj (prove_exn f)
  20.523 -    | Const (@{const_name distinct}, _) => prove_distinct (prove_safe f)
  20.524 -    | _ => prove (prove_safe f)) cp
  20.525 -in
  20.526 -fun monotonicity eqs ct =
  20.527 -  let
  20.528 -    fun and_symmetric (t, thm) = [(t, thm), (t, Thm.symmetric thm)]
  20.529 -    val teqs = maps (and_symmetric o `Thm.prop_of o meta_eq_of) eqs
  20.530 -    val lookup = AList.lookup (op aconv) teqs
  20.531 -    val cp = Thm.dest_binop (Thm.dest_arg ct)
  20.532 -  in MetaEq (prove_exn lookup cp handle MONO => mono lookup cp) end
  20.533 -end
  20.534 -
  20.535 -
  20.536 -(* |- f a b = f b a (where f is equality) *)
  20.537 -local
  20.538 -  val rule = @{lemma "a = b == b = a" by (atomize(full)) (rule eq_commute)}
  20.539 -in
  20.540 -fun commutativity ct =
  20.541 -  MetaEq (Old_Z3_Proof_Tools.match_instantiate I
  20.542 -    (Old_Z3_Proof_Tools.as_meta_eq ct) rule)
  20.543 -end
  20.544 -
  20.545 -
  20.546 -
  20.547 -(** quantifier proof rules **)
  20.548 -
  20.549 -(* P ?x = Q ?x ==> (ALL x. P x) = (ALL x. Q x)
  20.550 -   P ?x = Q ?x ==> (EX x. P x) = (EX x. Q x)    *)
  20.551 -local
  20.552 -  val rules = [
  20.553 -    @{lemma "(!!x. P x == Q x) ==> (ALL x. P x) == (ALL x. Q x)" by simp},
  20.554 -    @{lemma "(!!x. P x == Q x) ==> (EX x. P x) == (EX x. Q x)" by simp}]
  20.555 -in
  20.556 -fun quant_intro ctxt vars p ct =
  20.557 -  let
  20.558 -    val thm = meta_eq_of p
  20.559 -    val rules' = Old_Z3_Proof_Tools.varify vars thm :: rules
  20.560 -    val cu = Old_Z3_Proof_Tools.as_meta_eq ct
  20.561 -    val tac = REPEAT_ALL_NEW (match_tac ctxt rules')
  20.562 -  in MetaEq (Old_Z3_Proof_Tools.by_tac ctxt tac cu) end
  20.563 -end
  20.564 -
  20.565 -
  20.566 -(* |- ((ALL x. P x) | Q) = (ALL x. P x | Q) *)
  20.567 -fun pull_quant ctxt = Thm o try_apply ctxt [] [
  20.568 -  named ctxt "fast" (Old_Z3_Proof_Tools.by_tac ctxt (HOL_fast_tac ctxt))]
  20.569 -    (* FIXME: not very well tested *)
  20.570 -
  20.571 -
  20.572 -(* |- (ALL x. P x & Q x) = ((ALL x. P x) & (ALL x. Q x)) *)
  20.573 -fun push_quant ctxt = Thm o try_apply ctxt [] [
  20.574 -  named ctxt "fast" (Old_Z3_Proof_Tools.by_tac ctxt (HOL_fast_tac ctxt))]
  20.575 -    (* FIXME: not very well tested *)
  20.576 -
  20.577 -
  20.578 -(* |- (ALL x1 ... xn y1 ... yn. P x1 ... xn) = (ALL x1 ... xn. P x1 ... xn) *)
  20.579 -local
  20.580 -  val elim_all = @{lemma "P = Q ==> (ALL x. P) = Q" by fast}
  20.581 -  val elim_ex = @{lemma "P = Q ==> (EX x. P) = Q" by fast}
  20.582 -
  20.583 -  fun elim_unused_tac ctxt i st = (
  20.584 -    match_tac ctxt [@{thm refl}]
  20.585 -    ORELSE' (match_tac ctxt [elim_all, elim_ex] THEN' elim_unused_tac ctxt)
  20.586 -    ORELSE' (
  20.587 -      match_tac ctxt [@{thm iff_allI}, @{thm iff_exI}]
  20.588 -      THEN' elim_unused_tac ctxt)) i st
  20.589 -in
  20.590 -
  20.591 -fun elim_unused_vars ctxt = Thm o Old_Z3_Proof_Tools.by_tac ctxt (elim_unused_tac ctxt)
  20.592 -
  20.593 -end
  20.594 -
  20.595 -
  20.596 -(* |- (ALL x1 ... xn. ~(x1 = t1 & ... xn = tn) | P x1 ... xn) = P t1 ... tn *)
  20.597 -fun dest_eq_res ctxt = Thm o try_apply ctxt [] [
  20.598 -  named ctxt "fast" (Old_Z3_Proof_Tools.by_tac ctxt (HOL_fast_tac ctxt))]
  20.599 -    (* FIXME: not very well tested *)
  20.600 -
  20.601 -
  20.602 -(* |- ~(ALL x1...xn. P x1...xn) | P a1...an *)
  20.603 -local
  20.604 -  val rule = @{lemma "~ P x | Q ==> ~(ALL x. P x) | Q" by fast}
  20.605 -in
  20.606 -fun quant_inst ctxt = Thm o Old_Z3_Proof_Tools.by_tac ctxt (
  20.607 -  REPEAT_ALL_NEW (match_tac ctxt [rule])
  20.608 -  THEN' resolve_tac ctxt @{thms excluded_middle})
  20.609 -end
  20.610 -
  20.611 -
  20.612 -(* |- (EX x. P x) = P c     |- ~(ALL x. P x) = ~ P c *)
  20.613 -local
  20.614 -  val forall =
  20.615 -    Old_SMT_Utils.mk_const_pat @{theory} @{const_name Pure.all}
  20.616 -      (Old_SMT_Utils.destT1 o Old_SMT_Utils.destT1)
  20.617 -  fun mk_forall cv ct =
  20.618 -    Thm.apply (Old_SMT_Utils.instT' cv forall) (Thm.lambda cv ct)
  20.619 -
  20.620 -  fun get_vars f mk pred ctxt t =
  20.621 -    Term.fold_aterms f t []
  20.622 -    |> map_filter (fn v =>
  20.623 -         if pred v then SOME (Thm.cterm_of ctxt (mk v)) else NONE)
  20.624 -
  20.625 -  fun close vars f ct ctxt =
  20.626 -    let
  20.627 -      val frees_of = get_vars Term.add_frees Free (member (op =) vars o fst)
  20.628 -      val vs = frees_of ctxt (Thm.term_of ct)
  20.629 -      val (thm, ctxt') = f (fold_rev mk_forall vs ct) ctxt
  20.630 -      val vars_of = get_vars Term.add_vars Var (K true) ctxt'
  20.631 -    in
  20.632 -      (Thm.instantiate ([], map (dest_Var o Thm.term_of) (vars_of (Thm.prop_of thm)) ~~ vs) thm,
  20.633 -        ctxt')
  20.634 -    end
  20.635 -
  20.636 -  val sk_rules = @{lemma
  20.637 -    "c = (SOME x. P x) ==> (EX x. P x) = P c"
  20.638 -    "c = (SOME x. ~P x) ==> (~(ALL x. P x)) = (~P c)"
  20.639 -    by (metis someI_ex)+}
  20.640 -in
  20.641 -
  20.642 -fun skolemize vars =
  20.643 -  apfst Thm oo close vars (yield_singleton Assumption.add_assumes)
  20.644 -
  20.645 -fun discharge_sk_tac ctxt i st = (
  20.646 -  resolve_tac ctxt @{thms trans} i
  20.647 -  THEN resolve_tac ctxt sk_rules i
  20.648 -  THEN (resolve_tac ctxt @{thms refl} ORELSE' discharge_sk_tac ctxt) (i+1)
  20.649 -  THEN resolve_tac ctxt @{thms refl} i) st
  20.650 -
  20.651 -end
  20.652 -
  20.653 -
  20.654 -
  20.655 -(** theory proof rules **)
  20.656 -
  20.657 -(* theory lemmas: linear arithmetic, arrays *)
  20.658 -fun th_lemma ctxt simpset thms = Thm o try_apply ctxt thms [
  20.659 -  Old_Z3_Proof_Tools.by_abstraction 0 (false, true) ctxt thms (fn ctxt' =>
  20.660 -    Old_Z3_Proof_Tools.by_tac ctxt' (
  20.661 -      NAMED ctxt' "arith" (Arith_Data.arith_tac ctxt')
  20.662 -      ORELSE' NAMED ctxt' "simp+arith" (
  20.663 -        Simplifier.asm_full_simp_tac (put_simpset simpset ctxt')
  20.664 -        THEN_ALL_NEW Arith_Data.arith_tac ctxt')))]
  20.665 -
  20.666 -
  20.667 -(* rewriting: prove equalities:
  20.668 -     * ACI of conjunction/disjunction
  20.669 -     * contradiction, excluded middle
  20.670 -     * logical rewriting rules (for negation, implication, equivalence,
  20.671 -         distinct)
  20.672 -     * normal forms for polynoms (integer/real arithmetic)
  20.673 -     * quantifier elimination over linear arithmetic
  20.674 -     * ... ? **)
  20.675 -local
  20.676 -  fun spec_meta_eq_of thm =
  20.677 -    (case try (fn th => th RS @{thm spec}) thm of
  20.678 -      SOME thm' => spec_meta_eq_of thm'
  20.679 -    | NONE => mk_meta_eq thm)
  20.680 -
  20.681 -  fun prep (Thm thm) = spec_meta_eq_of thm
  20.682 -    | prep (MetaEq thm) = thm
  20.683 -    | prep (Literals (thm, _)) = spec_meta_eq_of thm
  20.684 -
  20.685 -  fun unfold_conv ctxt ths =
  20.686 -    Conv.arg_conv (Conv.binop_conv (Old_Z3_Proof_Tools.unfold_eqs ctxt
  20.687 -      (map prep ths)))
  20.688 -
  20.689 -  fun with_conv _ [] prv = prv
  20.690 -    | with_conv ctxt ths prv =
  20.691 -        Old_Z3_Proof_Tools.with_conv (unfold_conv ctxt ths) prv
  20.692 -
  20.693 -  val unfold_conv =
  20.694 -    Conv.arg_conv (Conv.binop_conv
  20.695 -      (Conv.try_conv Old_Z3_Proof_Tools.unfold_distinct_conv))
  20.696 -  val prove_conj_disj_eq =
  20.697 -    Old_Z3_Proof_Tools.with_conv unfold_conv Old_Z3_Proof_Literals.prove_conj_disj_eq
  20.698 -
  20.699 -  fun declare_hyps ctxt thm =
  20.700 -    (thm, snd (Assumption.add_assumes (Thm.chyps_of thm) ctxt))
  20.701 -in
  20.702 -
  20.703 -val abstraction_depth = 3
  20.704 -  (*
  20.705 -    This value was chosen large enough to potentially catch exceptions,
  20.706 -    yet small enough to not cause too much harm.  The value might be
  20.707 -    increased in the future, if reconstructing 'rewrite' fails on problems
  20.708 -    that get too much abstracted to be reconstructable.
  20.709 -  *)
  20.710 -
  20.711 -fun rewrite simpset ths ct ctxt =
  20.712 -  apfst Thm (declare_hyps ctxt (with_conv ctxt ths (try_apply ctxt [] [
  20.713 -    named ctxt "conj/disj/distinct" prove_conj_disj_eq,
  20.714 -    named ctxt "pull-ite" Old_Z3_Proof_Methods.prove_ite ctxt,
  20.715 -    Old_Z3_Proof_Tools.by_abstraction 0 (true, false) ctxt [] (fn ctxt' =>
  20.716 -      Old_Z3_Proof_Tools.by_tac ctxt' (
  20.717 -        NAMED ctxt' "simp (logic)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
  20.718 -        THEN_ALL_NEW NAMED ctxt' "fast (logic)" (fast_tac ctxt'))),
  20.719 -    Old_Z3_Proof_Tools.by_abstraction 0 (false, true) ctxt [] (fn ctxt' =>
  20.720 -      Old_Z3_Proof_Tools.by_tac ctxt' (
  20.721 -        (resolve_tac ctxt' @{thms iff_allI} ORELSE' K all_tac)
  20.722 -        THEN' NAMED ctxt' "simp (theory)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
  20.723 -        THEN_ALL_NEW (
  20.724 -          NAMED ctxt' "fast (theory)" (HOL_fast_tac ctxt')
  20.725 -          ORELSE' NAMED ctxt' "arith (theory)" (Arith_Data.arith_tac ctxt')))),
  20.726 -    Old_Z3_Proof_Tools.by_abstraction 0 (true, true) ctxt [] (fn ctxt' =>
  20.727 -      Old_Z3_Proof_Tools.by_tac ctxt' (
  20.728 -        (resolve_tac ctxt' @{thms iff_allI} ORELSE' K all_tac)
  20.729 -        THEN' NAMED ctxt' "simp (full)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
  20.730 -        THEN_ALL_NEW (
  20.731 -          NAMED ctxt' "fast (full)" (HOL_fast_tac ctxt')
  20.732 -          ORELSE' NAMED ctxt' "arith (full)" (Arith_Data.arith_tac ctxt')))),
  20.733 -    named ctxt "injectivity" (Old_Z3_Proof_Methods.prove_injectivity ctxt),
  20.734 -    Old_Z3_Proof_Tools.by_abstraction abstraction_depth (true, true) ctxt []
  20.735 -      (fn ctxt' =>
  20.736 -        Old_Z3_Proof_Tools.by_tac ctxt' (
  20.737 -          (resolve_tac ctxt' @{thms iff_allI} ORELSE' K all_tac)
  20.738 -          THEN' NAMED ctxt' "simp (deepen)" (Simplifier.simp_tac (put_simpset simpset ctxt'))
  20.739 -          THEN_ALL_NEW (
  20.740 -            NAMED ctxt' "fast (deepen)" (HOL_fast_tac ctxt')
  20.741 -            ORELSE' NAMED ctxt' "arith (deepen)" (Arith_Data.arith_tac
  20.742 -              ctxt'))))]) ct))
  20.743 -
  20.744 -end
  20.745 -
  20.746 -
  20.747 -
  20.748 -(* proof reconstruction *)
  20.749 -
  20.750 -(** tracing and checking **)
  20.751 -
  20.752 -fun trace_before ctxt idx = Old_SMT_Config.trace_msg ctxt (fn r =>
  20.753 -  "Z3: #" ^ string_of_int idx ^ ": " ^ Old_Z3_Proof_Parser.string_of_rule r)
  20.754 -
  20.755 -fun check_after idx r ps ct (p, (ctxt, _)) =
  20.756 -  if not (Config.get ctxt Old_SMT_Config.trace) then ()
  20.757 -  else
  20.758 -    let
  20.759 -      val thm = thm_of p
  20.760 -      val _ = Thm.consolidate thm
  20.761 -    in
  20.762 -      if (Thm.cprop_of thm) aconvc ct then ()
  20.763 -      else
  20.764 -        z3_exn (Pretty.string_of (Pretty.big_list
  20.765 -          ("proof step failed: " ^ quote (Old_Z3_Proof_Parser.string_of_rule r) ^
  20.766 -            " (#" ^ string_of_int idx ^ ")")
  20.767 -          (pretty_goal ctxt (map (thm_of o fst) ps) (Thm.prop_of thm) @
  20.768 -            [Pretty.block [Pretty.str "expected: ",
  20.769 -              Syntax.pretty_term ctxt (Thm.term_of ct)]])))
  20.770 -    end
  20.771 -
  20.772 -
  20.773 -(** overall reconstruction procedure **)
  20.774 -
  20.775 -local
  20.776 -  fun not_supported r = raise Fail ("Z3: proof rule not implemented: " ^
  20.777 -    quote (Old_Z3_Proof_Parser.string_of_rule r))
  20.778 -
  20.779 -  fun prove_step simpset vars r ps ct (cxp as (cx, ptab)) =
  20.780 -    (case (r, ps) of
  20.781 -      (* core rules *)
  20.782 -      (Old_Z3_Proof_Parser.True_Axiom, _) => (Thm Old_Z3_Proof_Literals.true_thm, cxp)
  20.783 -    | (Old_Z3_Proof_Parser.Asserted, _) => raise Fail "bad assertion"
  20.784 -    | (Old_Z3_Proof_Parser.Goal, _) => raise Fail "bad assertion"
  20.785 -    | (Old_Z3_Proof_Parser.Modus_Ponens, [(p, _), (q, _)]) =>
  20.786 -        (mp q (thm_of p), cxp)
  20.787 -    | (Old_Z3_Proof_Parser.Modus_Ponens_Oeq, [(p, _), (q, _)]) =>
  20.788 -        (mp q (thm_of p), cxp)
  20.789 -    | (Old_Z3_Proof_Parser.And_Elim, [(p, i)]) =>
  20.790 -        and_elim (p, i) ct ptab ||> pair cx
  20.791 -    | (Old_Z3_Proof_Parser.Not_Or_Elim, [(p, i)]) =>
  20.792 -        not_or_elim (p, i) ct ptab ||> pair cx
  20.793 -    | (Old_Z3_Proof_Parser.Hypothesis, _) => (Thm (Thm.assume ct), cxp)
  20.794 -    | (Old_Z3_Proof_Parser.Lemma, [(p, _)]) => (lemma (thm_of p) ct, cxp)
  20.795 -    | (Old_Z3_Proof_Parser.Unit_Resolution, (p, _) :: ps) =>
  20.796 -        (unit_resolution (thm_of p) (map (thm_of o fst) ps) ct, cxp)
  20.797 -    | (Old_Z3_Proof_Parser.Iff_True, [(p, _)]) => (iff_true (thm_of p), cxp)
  20.798 -    | (Old_Z3_Proof_Parser.Iff_False, [(p, _)]) => (iff_false (thm_of p), cxp)
  20.799 -    | (Old_Z3_Proof_Parser.Distributivity, _) => (distributivity cx ct, cxp)
  20.800 -    | (Old_Z3_Proof_Parser.Def_Axiom, _) => (def_axiom cx ct, cxp)
  20.801 -    | (Old_Z3_Proof_Parser.Intro_Def, _) => intro_def ct cx ||> rpair ptab
  20.802 -    | (Old_Z3_Proof_Parser.Apply_Def, [(p, _)]) => (apply_def (thm_of p), cxp)
  20.803 -    | (Old_Z3_Proof_Parser.Iff_Oeq, [(p, _)]) => (p, cxp)
  20.804 -    | (Old_Z3_Proof_Parser.Nnf_Pos, _) => (nnf cx vars (map fst ps) ct, cxp)
  20.805 -    | (Old_Z3_Proof_Parser.Nnf_Neg, _) => (nnf cx vars (map fst ps) ct, cxp)
  20.806 -
  20.807 -      (* equality rules *)
  20.808 -    | (Old_Z3_Proof_Parser.Reflexivity, _) => (refl ct, cxp)
  20.809 -    | (Old_Z3_Proof_Parser.Symmetry, [(p, _)]) => (symm p, cxp)
  20.810 -    | (Old_Z3_Proof_Parser.Transitivity, [(p, _), (q, _)]) => (trans p q, cxp)
  20.811 -    | (Old_Z3_Proof_Parser.Monotonicity, _) => (monotonicity (map fst ps) ct, cxp)
  20.812 -    | (Old_Z3_Proof_Parser.Commutativity, _) => (commutativity ct, cxp)
  20.813 -
  20.814 -      (* quantifier rules *)
  20.815 -    | (Old_Z3_Proof_Parser.Quant_Intro, [(p, _)]) => (quant_intro cx vars p ct, cxp)
  20.816 -    | (Old_Z3_Proof_Parser.Pull_Quant, _) => (pull_quant cx ct, cxp)
  20.817 -    | (Old_Z3_Proof_Parser.Push_Quant, _) => (push_quant cx ct, cxp)
  20.818 -    | (Old_Z3_Proof_Parser.Elim_Unused_Vars, _) => (elim_unused_vars cx ct, cxp)
  20.819 -    | (Old_Z3_Proof_Parser.Dest_Eq_Res, _) => (dest_eq_res cx ct, cxp)
  20.820 -    | (Old_Z3_Proof_Parser.Quant_Inst, _) => (quant_inst cx ct, cxp)
  20.821 -    | (Old_Z3_Proof_Parser.Skolemize, _) => skolemize vars ct cx ||> rpair ptab
  20.822 -
  20.823 -      (* theory rules *)
  20.824 -    | (Old_Z3_Proof_Parser.Th_Lemma _, _) =>  (* FIXME: use arguments *)
  20.825 -        (th_lemma cx simpset (map (thm_of o fst) ps) ct, cxp)
  20.826 -    | (Old_Z3_Proof_Parser.Rewrite, _) => rewrite simpset [] ct cx ||> rpair ptab
  20.827 -    | (Old_Z3_Proof_Parser.Rewrite_Star, ps) =>
  20.828 -        rewrite simpset (map fst ps) ct cx ||> rpair ptab
  20.829 -
  20.830 -    | (Old_Z3_Proof_Parser.Nnf_Star, _) => not_supported r
  20.831 -    | (Old_Z3_Proof_Parser.Cnf_Star, _) => not_supported r
  20.832 -    | (Old_Z3_Proof_Parser.Transitivity_Star, _) => not_supported r
  20.833 -    | (Old_Z3_Proof_Parser.Pull_Quant_Star, _) => not_supported r
  20.834 -
  20.835 -    | _ => raise Fail ("Z3: proof rule " ^
  20.836 -        quote (Old_Z3_Proof_Parser.string_of_rule r) ^
  20.837 -        " has an unexpected number of arguments."))
  20.838 -
  20.839 -  fun lookup_proof ptab idx =
  20.840 -    (case Inttab.lookup ptab idx of
  20.841 -      SOME p => (p, idx)
  20.842 -    | NONE => z3_exn ("unknown proof id: " ^ quote (string_of_int idx)))
  20.843 -
  20.844 -  fun prove simpset vars (idx, step) (_, cxp as (ctxt, ptab)) =
  20.845 -    let
  20.846 -      val Old_Z3_Proof_Parser.Proof_Step {rule=r, prems, prop, ...} = step
  20.847 -      val ps = map (lookup_proof ptab) prems
  20.848 -      val _ = trace_before ctxt idx r
  20.849 -      val (thm, (ctxt', ptab')) =
  20.850 -        cxp
  20.851 -        |> prove_step simpset vars r ps prop
  20.852 -        |> tap (check_after idx r ps prop)
  20.853 -    in (thm, (ctxt', Inttab.update (idx, thm) ptab')) end
  20.854 -
  20.855 -  fun make_discharge_rules rules = rules @ [@{thm allI}, @{thm refl},
  20.856 -    @{thm reflexive}, Old_Z3_Proof_Literals.true_thm]
  20.857 -
  20.858 -  fun discharge_assms_tac ctxt rules =
  20.859 -    REPEAT (HEADGOAL (resolve_tac ctxt rules ORELSE' SOLVED' (discharge_sk_tac ctxt)))
  20.860 -
  20.861 -  fun discharge_assms ctxt rules thm =
  20.862 -    if Thm.nprems_of thm = 0 then Goal.norm_result ctxt thm
  20.863 -    else
  20.864 -      (case Seq.pull (discharge_assms_tac ctxt rules thm) of
  20.865 -        SOME (thm', _) => Goal.norm_result ctxt thm'
  20.866 -      | NONE => raise THM ("failed to discharge premise", 1, [thm]))
  20.867 -
  20.868 -  fun discharge rules outer_ctxt (p, (inner_ctxt, _)) =
  20.869 -    thm_of p
  20.870 -    |> singleton (Proof_Context.export inner_ctxt outer_ctxt)
  20.871 -    |> discharge_assms outer_ctxt (make_discharge_rules rules)
  20.872 -in
  20.873 -
  20.874 -fun reconstruct outer_ctxt recon output =
  20.875 -  let
  20.876 -    val {context=ctxt, typs, terms, rewrite_rules, assms} = recon
  20.877 -    val (asserted, steps, vars, ctxt1) =
  20.878 -      Old_Z3_Proof_Parser.parse ctxt typs terms output
  20.879 -
  20.880 -    val simpset =
  20.881 -      Old_Z3_Proof_Tools.make_simpset ctxt1 (Named_Theorems.get ctxt1 @{named_theorems old_z3_simp})
  20.882 -
  20.883 -    val ((is, rules), cxp as (ctxt2, _)) =
  20.884 -      add_asserted outer_ctxt rewrite_rules assms asserted ctxt1
  20.885 -  in
  20.886 -    if Config.get ctxt2 Old_SMT_Config.filter_only_facts then (is, @{thm TrueI})
  20.887 -    else
  20.888 -      (Thm @{thm TrueI}, cxp)
  20.889 -      |> fold (prove simpset vars) steps
  20.890 -      |> discharge rules outer_ctxt
  20.891 -      |> pair []
  20.892 -  end
  20.893 -
  20.894 -end
  20.895 -
  20.896 -end
    21.1 --- a/src/HOL/Library/Old_SMT/old_z3_proof_tools.ML	Thu Apr 20 10:45:52 2017 +0200
    21.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.3 @@ -1,374 +0,0 @@
    21.4 -(*  Title:      HOL/Library/Old_SMT/old_z3_proof_tools.ML
    21.5 -    Author:     Sascha Boehme, TU Muenchen
    21.6 -
    21.7 -Helper functions required for Z3 proof reconstruction.
    21.8 -*)
    21.9 -
   21.10 -signature OLD_Z3_PROOF_TOOLS =
   21.11 -sig
   21.12 -  (*modifying terms*)
   21.13 -  val as_meta_eq: cterm -> cterm
   21.14 -
   21.15 -  (*theorem nets*)
   21.16 -  val thm_net_of: ('a -> thm) -> 'a list -> 'a Net.net
   21.17 -  val net_instances: (int * thm) Net.net -> cterm -> (int * thm) list
   21.18 -  val net_instance: thm Net.net -> cterm -> thm option
   21.19 -
   21.20 -  (*proof combinators*)
   21.21 -  val under_assumption: (thm -> thm) -> cterm -> thm
   21.22 -  val with_conv: conv -> (cterm -> thm) -> cterm -> thm
   21.23 -  val discharge: thm -> thm -> thm
   21.24 -  val varify: string list -> thm -> thm
   21.25 -  val unfold_eqs: Proof.context -> thm list -> conv
   21.26 -  val match_instantiate: (cterm -> cterm) -> cterm -> thm -> thm
   21.27 -  val by_tac: Proof.context -> (int -> tactic) -> cterm -> thm
   21.28 -  val make_hyp_def: thm -> Proof.context -> thm * Proof.context
   21.29 -  val by_abstraction: int -> bool * bool -> Proof.context -> thm list ->
   21.30 -    (Proof.context -> cterm -> thm) -> cterm -> thm
   21.31 -
   21.32 -  (*a faster COMP*)
   21.33 -  type compose_data = cterm list * (cterm -> cterm list) * thm
   21.34 -  val precompose: (cterm -> cterm list) -> thm -> compose_data
   21.35 -  val precompose2: (cterm -> cterm * cterm) -> thm -> compose_data
   21.36 -  val compose: compose_data -> thm -> thm
   21.37 -
   21.38 -  (*unfolding of 'distinct'*)
   21.39 -  val unfold_distinct_conv: conv
   21.40 -
   21.41 -  (*simpset*)
   21.42 -  val add_simproc: Simplifier.simproc -> Context.generic -> Context.generic
   21.43 -  val make_simpset: Proof.context -> thm list -> simpset
   21.44 -end
   21.45 -
   21.46 -structure Old_Z3_Proof_Tools: OLD_Z3_PROOF_TOOLS =
   21.47 -struct
   21.48 -
   21.49 -
   21.50 -
   21.51 -(* modifying terms *)
   21.52 -
   21.53 -fun as_meta_eq ct =
   21.54 -  uncurry Old_SMT_Utils.mk_cequals (Thm.dest_binop (Old_SMT_Utils.dest_cprop ct))
   21.55 -
   21.56 -
   21.57 -
   21.58 -(* theorem nets *)
   21.59 -
   21.60 -fun thm_net_of f xthms =
   21.61 -  let fun insert xthm = Net.insert_term (K false) (Thm.prop_of (f xthm), xthm)
   21.62 -  in fold insert xthms Net.empty end
   21.63 -
   21.64 -fun maybe_instantiate ct thm =
   21.65 -  try Thm.first_order_match (Thm.cprop_of thm, ct)
   21.66 -  |> Option.map (fn inst => Thm.instantiate inst thm)
   21.67 -
   21.68 -local
   21.69 -  fun instances_from_net match f net ct =
   21.70 -    let
   21.71 -      val lookup = if match then Net.match_term else Net.unify_term
   21.72 -      val xthms = lookup net (Thm.term_of ct)
   21.73 -      fun select ct = map_filter (f (maybe_instantiate ct)) xthms 
   21.74 -      fun select' ct =
   21.75 -        let val thm = Thm.trivial ct
   21.76 -        in map_filter (f (try (fn rule => rule COMP thm))) xthms end
   21.77 -    in (case select ct of [] => select' ct | xthms' => xthms') end
   21.78 -in
   21.79 -
   21.80 -fun net_instances net =
   21.81 -  instances_from_net false (fn f => fn (i, thm) => Option.map (pair i) (f thm))
   21.82 -    net
   21.83 -
   21.84 -fun net_instance net = try hd o instances_from_net true I net
   21.85 -
   21.86 -end
   21.87 -
   21.88 -
   21.89 -
   21.90 -(* proof combinators *)
   21.91 -
   21.92 -fun under_assumption f ct =
   21.93 -  let val ct' = Old_SMT_Utils.mk_cprop ct
   21.94 -  in Thm.implies_intr ct' (f (Thm.assume ct')) end
   21.95 -
   21.96 -fun with_conv conv prove ct =
   21.97 -  let val eq = Thm.symmetric (conv ct)
   21.98 -  in Thm.equal_elim eq (prove (Thm.lhs_of eq)) end
   21.99 -
  21.100 -fun discharge p pq = Thm.implies_elim pq p
  21.101 -
  21.102 -fun varify vars = Drule.generalize ([], vars)
  21.103 -
  21.104 -fun unfold_eqs _ [] = Conv.all_conv
  21.105 -  | unfold_eqs ctxt eqs =
  21.106 -      Conv.top_sweep_conv (K (Conv.rewrs_conv eqs)) ctxt
  21.107 -
  21.108 -fun match_instantiate f ct thm =
  21.109 -  Thm.instantiate (Thm.match (f (Thm.cprop_of thm), ct)) thm
  21.110 -
  21.111 -fun by_tac ctxt tac ct = Goal.norm_result ctxt (Goal.prove_internal ctxt [] ct (K (tac 1)))
  21.112 -
  21.113 -(*
  21.114 -   |- c x == t x ==> P (c x)
  21.115 -  ---------------------------
  21.116 -      c == t |- P (c x)
  21.117 -*) 
  21.118 -fun make_hyp_def thm ctxt =
  21.119 -  let
  21.120 -    val (lhs, rhs) = Thm.dest_binop (Thm.cprem_of thm 1)
  21.121 -    val (cf, cvs) = Drule.strip_comb lhs
  21.122 -    val eq = Old_SMT_Utils.mk_cequals cf (fold_rev Thm.lambda cvs rhs)
  21.123 -    fun apply cv th =
  21.124 -      Thm.combination th (Thm.reflexive cv)
  21.125 -      |> Conv.fconv_rule (Conv.arg_conv (Thm.beta_conversion false))
  21.126 -  in
  21.127 -    yield_singleton Assumption.add_assumes eq ctxt
  21.128 -    |>> Thm.implies_elim thm o fold apply cvs
  21.129 -  end
  21.130 -
  21.131 -
  21.132 -
  21.133 -(* abstraction *)
  21.134 -
  21.135 -local
  21.136 -
  21.137 -fun abs_context ctxt = (ctxt, Termtab.empty, 1, false)
  21.138 -
  21.139 -fun context_of (ctxt, _, _, _) = ctxt
  21.140 -
  21.141 -fun replace (_, (cv, ct)) = Thm.forall_elim ct o Thm.forall_intr cv
  21.142 -
  21.143 -fun abs_instantiate (_, tab, _, beta_norm) =
  21.144 -  fold replace (Termtab.dest tab) #>
  21.145 -  beta_norm ? Conv.fconv_rule (Thm.beta_conversion true)
  21.146 -
  21.147 -fun lambda_abstract cvs t =
  21.148 -  let
  21.149 -    val frees = map Free (Term.add_frees t [])
  21.150 -    val cvs' = filter (fn cv => member (op aconv) frees (Thm.term_of cv)) cvs
  21.151 -    val vs = map (Term.dest_Free o Thm.term_of) cvs'
  21.152 -  in (fold_rev absfree vs t, cvs') end
  21.153 -
  21.154 -fun fresh_abstraction (_, cvs) ct (cx as (ctxt, tab, idx, beta_norm)) =
  21.155 -  let val (t, cvs') = lambda_abstract cvs (Thm.term_of ct)
  21.156 -  in
  21.157 -    (case Termtab.lookup tab t of
  21.158 -      SOME (cv, _) => (Drule.list_comb (cv, cvs'), cx)
  21.159 -    | NONE =>
  21.160 -        let
  21.161 -          val (n, ctxt') = yield_singleton Variable.variant_fixes "x" ctxt
  21.162 -          val cv = Thm.cterm_of ctxt'
  21.163 -            (Free (n, map Thm.typ_of_cterm cvs' ---> Thm.typ_of_cterm ct))
  21.164 -          val cu = Drule.list_comb (cv, cvs')
  21.165 -          val e = (t, (cv, fold_rev Thm.lambda cvs' ct))
  21.166 -          val beta_norm' = beta_norm orelse not (null cvs')
  21.167 -        in (cu, (ctxt', Termtab.update e tab, idx + 1, beta_norm')) end)
  21.168 -  end
  21.169 -
  21.170 -fun abs_comb f g dcvs ct =
  21.171 -  let val (cf, cu) = Thm.dest_comb ct
  21.172 -  in f dcvs cf ##>> g dcvs cu #>> uncurry Thm.apply end
  21.173 -
  21.174 -fun abs_arg f = abs_comb (K pair) f
  21.175 -
  21.176 -fun abs_args f dcvs ct =
  21.177 -  (case Thm.term_of ct of
  21.178 -    _ $ _ => abs_comb (abs_args f) f dcvs ct
  21.179 -  | _ => pair ct)
  21.180 -
  21.181 -fun abs_list f g dcvs ct =
  21.182 -  (case Thm.term_of ct of
  21.183 -    Const (@{const_name Nil}, _) => pair ct
  21.184 -  | Const (@{const_name Cons}, _) $ _ $ _ =>
  21.185 -      abs_comb (abs_arg f) (abs_list f g) dcvs ct
  21.186 -  | _ => g dcvs ct)
  21.187 -
  21.188 -fun abs_abs f (depth, cvs) ct =
  21.189 -  let val (cv, cu) = Thm.dest_abs NONE ct
  21.190 -  in f (depth, cv :: cvs) cu #>> Thm.lambda cv end
  21.191 -
  21.192 -val is_atomic =
  21.193 -  (fn Free _ => true | Var _ => true | Bound _ => true | _ => false)
  21.194 -
  21.195 -fun abstract depth (ext_logic, with_theories) =
  21.196 -  let
  21.197 -    fun abstr1 cvs ct = abs_arg abstr cvs ct
  21.198 -    and abstr2 cvs ct = abs_comb abstr1 abstr cvs ct
  21.199 -    and abstr3 cvs ct = abs_comb abstr2 abstr cvs ct
  21.200 -    and abstr_abs cvs ct = abs_arg (abs_abs abstr) cvs ct
  21.201 -
  21.202 -    and abstr (dcvs as (d, cvs)) ct =
  21.203 -      (case Thm.term_of ct of
  21.204 -        @{const Trueprop} $ _ => abstr1 dcvs ct
  21.205 -      | @{const Pure.imp} $ _ $ _ => abstr2 dcvs ct
  21.206 -      | @{const True} => pair ct
  21.207 -      | @{const False} => pair ct
  21.208 -      | @{const Not} $ _ => abstr1 dcvs ct
  21.209 -      | @{const HOL.conj} $ _ $ _ => abstr2 dcvs ct
  21.210 -      | @{const HOL.disj} $ _ $ _ => abstr2 dcvs ct
  21.211 -      | @{const HOL.implies} $ _ $ _ => abstr2 dcvs ct
  21.212 -      | Const (@{const_name HOL.eq}, _) $ _ $ _ => abstr2 dcvs ct
  21.213 -      | Const (@{const_name distinct}, _) $ _ =>
  21.214 -          if ext_logic then abs_arg (abs_list abstr fresh_abstraction) dcvs ct
  21.215 -          else fresh_abstraction dcvs ct
  21.216 -      | Const (@{const_name If}, _) $ _ $ _ $ _ =>
  21.217 -          if ext_logic then abstr3 dcvs ct else fresh_abstraction dcvs ct
  21.218 -      | Const (@{const_name All}, _) $ _ =>
  21.219 -          if ext_logic then abstr_abs dcvs ct else fresh_abstraction dcvs ct
  21.220 -      | Const (@{const_name Ex}, _) $ _ =>
  21.221 -          if ext_logic then abstr_abs dcvs ct else fresh_abstraction dcvs ct
  21.222 -      | t => (fn cx =>
  21.223 -          if is_atomic t orelse can HOLogic.dest_number t then (ct, cx)
  21.224 -          else if with_theories andalso
  21.225 -            Old_Z3_Interface.is_builtin_theory_term (context_of cx) t
  21.226 -          then abs_args abstr dcvs ct cx
  21.227 -          else if d = 0 then fresh_abstraction dcvs ct cx
  21.228 -          else
  21.229 -            (case Term.strip_comb t of
  21.230 -              (Const _, _) => abs_args abstr (d-1, cvs) ct cx
  21.231 -            | (Free _, _) => abs_args abstr (d-1, cvs) ct cx
  21.232 -            | _ => fresh_abstraction dcvs ct cx)))
  21.233 -  in abstr (depth, []) end
  21.234 -
  21.235 -val cimp = Thm.cterm_of @{context} @{const Pure.imp}
  21.236 -
  21.237 -fun deepen depth f x =
  21.238 -  if depth = 0 then f depth x
  21.239 -  else (case try (f depth) x of SOME y => y | NONE => deepen (depth - 1) f x)
  21.240 -
  21.241 -fun with_prems depth thms f ct =
  21.242 -  fold_rev (Thm.mk_binop cimp o Thm.cprop_of) thms ct
  21.243 -  |> deepen depth f
  21.244 -  |> fold (fn prem => fn th => Thm.implies_elim th prem) thms
  21.245 -
  21.246 -in
  21.247 -
  21.248 -fun by_abstraction depth mode ctxt thms prove =
  21.249 -  with_prems depth thms (fn d => fn ct =>
  21.250 -    let val (cu, cx) = abstract d mode ct (abs_context ctxt)
  21.251 -    in abs_instantiate cx (prove (context_of cx) cu) end)
  21.252 -
  21.253 -end
  21.254 -
  21.255 -
  21.256 -
  21.257 -(* a faster COMP *)
  21.258 -
  21.259 -type compose_data = cterm list * (cterm -> cterm list) * thm
  21.260 -
  21.261 -fun list2 (x, y) = [x, y]
  21.262 -
  21.263 -fun precompose f rule : compose_data = (f (Thm.cprem_of rule 1), f, rule)
  21.264 -fun precompose2 f rule : compose_data = precompose (list2 o f) rule
  21.265 -
  21.266 -fun compose (cvs, f, rule) thm =
  21.267 -  discharge thm (Thm.instantiate ([], map (dest_Var o Thm.term_of) cvs ~~ f (Thm.cprop_of thm)) rule)
  21.268 -
  21.269 -
  21.270 -
  21.271 -(* unfolding of 'distinct' *)
  21.272 -
  21.273 -local
  21.274 -  val set1 = @{lemma "x ~: set [] == ~False" by simp}
  21.275 -  val set2 = @{lemma "x ~: set [x] == False" by simp}
  21.276 -  val set3 = @{lemma "x ~: set [y] == x ~= y" by simp}
  21.277 -  val set4 = @{lemma "x ~: set (x # ys) == False" by simp}
  21.278 -  val set5 = @{lemma "x ~: set (y # ys) == x ~= y & x ~: set ys" by simp}
  21.279 -
  21.280 -  fun set_conv ct =
  21.281 -    (Conv.rewrs_conv [set1, set2, set3, set4] else_conv
  21.282 -    (Conv.rewr_conv set5 then_conv Conv.arg_conv set_conv)) ct
  21.283 -
  21.284 -  val dist1 = @{lemma "distinct [] == ~False" by (simp add: distinct_def)}
  21.285 -  val dist2 = @{lemma "distinct [x] == ~False" by (simp add: distinct_def)}
  21.286 -  val dist3 = @{lemma "distinct (x # xs) == x ~: set xs & distinct xs"
  21.287 -    by (simp add: distinct_def)}
  21.288 -
  21.289 -  fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
  21.290 -in
  21.291 -fun unfold_distinct_conv ct =
  21.292 -  (Conv.rewrs_conv [dist1, dist2] else_conv
  21.293 -  (Conv.rewr_conv dist3 then_conv binop_conv set_conv unfold_distinct_conv)) ct
  21.294 -end
  21.295 -
  21.296 -
  21.297 -
  21.298 -(* simpset *)
  21.299 -
  21.300 -local
  21.301 -  val antisym_le1 = mk_meta_eq @{thm order_class.antisym_conv}
  21.302 -  val antisym_le2 = mk_meta_eq @{thm linorder_class.antisym_conv2}
  21.303 -  val antisym_less1 = mk_meta_eq @{thm linorder_class.antisym_conv1}
  21.304 -  val antisym_less2 = mk_meta_eq @{thm linorder_class.antisym_conv3}
  21.305 -
  21.306 -  fun eq_prop t thm = HOLogic.mk_Trueprop t aconv Thm.prop_of thm
  21.307 -  fun dest_binop ((c as Const _) $ t $ u) = (c, t, u)
  21.308 -    | dest_binop t = raise TERM ("dest_binop", [t])
  21.309 -
  21.310 -  fun prove_antisym_le ctxt ct =
  21.311 -    let
  21.312 -      val (le, r, s) = dest_binop (Thm.term_of ct)
  21.313 -      val less = Const (@{const_name less}, Term.fastype_of le)
  21.314 -      val prems = Simplifier.prems_of ctxt
  21.315 -    in
  21.316 -      (case find_first (eq_prop (le $ s $ r)) prems of
  21.317 -        NONE =>
  21.318 -          find_first (eq_prop (HOLogic.mk_not (less $ r $ s))) prems
  21.319 -          |> Option.map (fn thm => thm RS antisym_less1)
  21.320 -      | SOME thm => SOME (thm RS antisym_le1))
  21.321 -    end
  21.322 -    handle THM _ => NONE
  21.323 -
  21.324 -  fun prove_antisym_less ctxt ct =
  21.325 -    let
  21.326 -      val (less, r, s) = dest_binop (HOLogic.dest_not (Thm.term_of ct))
  21.327 -      val le = Const (@{const_name less_eq}, Term.fastype_of less)
  21.328 -      val prems = Simplifier.prems_of ctxt
  21.329 -    in
  21.330 -      (case find_first (eq_prop (le $ r $ s)) prems of
  21.331 -        NONE =>
  21.332 -          find_first (eq_prop (HOLogic.mk_not (less $ s $ r))) prems
  21.333 -          |> Option.map (fn thm => thm RS antisym_less2)
  21.334 -      | SOME thm => SOME (thm RS antisym_le2))
  21.335 -  end
  21.336 -  handle THM _ => NONE
  21.337 -
  21.338 -  val basic_simpset =
  21.339 -    simpset_of (put_simpset HOL_ss @{context}
  21.340 -      addsimps @{thms field_simps}
  21.341 -      addsimps [@{thm times_divide_eq_right}, @{thm times_divide_eq_left}]
  21.342 -      addsimps @{thms arith_special} addsimps @{thms arith_simps}
  21.343 -      addsimps @{thms rel_simps}
  21.344 -      addsimps @{thms array_rules}
  21.345 -      addsimps @{thms term_true_def} addsimps @{thms term_false_def}
  21.346 -      addsimps @{thms z3div_def} addsimps @{thms z3mod_def}
  21.347 -      addsimprocs [@{simproc numeral_divmod}]
  21.348 -      addsimprocs [
  21.349 -        Simplifier.make_simproc @{context} "fast_int_arith"
  21.350 -         {lhss = [@{term "(m::int) < n"}, @{term "(m::int) \<le> n"}, @{term "(m::int) = n"}],
  21.351 -          proc = K Lin_Arith.simproc},
  21.352 -        Simplifier.make_simproc @{context} "antisym_le"
  21.353 -         {lhss = [@{term "(x::'a::order) \<le> y"}],
  21.354 -          proc = K prove_antisym_le},
  21.355 -        Simplifier.make_simproc @{context} "antisym_less"
  21.356 -         {lhss = [@{term "\<not> (x::'a::linorder) < y"}],
  21.357 -          proc = K prove_antisym_less}])
  21.358 -
  21.359 -  structure Simpset = Generic_Data
  21.360 -  (
  21.361 -    type T = simpset
  21.362 -    val empty = basic_simpset
  21.363 -    val extend = I
  21.364 -    val merge = Simplifier.merge_ss
  21.365 -  )
  21.366 -in
  21.367 -
  21.368 -fun add_simproc simproc context =
  21.369 -  Simpset.map (simpset_map (Context.proof_of context)
  21.370 -    (fn ctxt => ctxt addsimprocs [simproc])) context
  21.371 -
  21.372 -fun make_simpset ctxt rules =
  21.373 -  simpset_of (put_simpset (Simpset.get (Context.Proof ctxt)) ctxt addsimps rules)
  21.374 -
  21.375 -end
  21.376 -
  21.377 -end
    22.1 --- a/src/HOL/ROOT	Thu Apr 20 10:45:52 2017 +0200
    22.2 +++ b/src/HOL/ROOT	Thu Apr 20 16:21:28 2017 +0200
    22.3 @@ -55,7 +55,6 @@
    22.4      (*legacy tools*)
    22.5      Old_Datatype
    22.6      Old_Recdef
    22.7 -    Old_SMT
    22.8      Refute
    22.9    document_files "root.bib" "root.tex"
   22.10