src/HOL/SMT/SMT_Base.thy
author boehmes
Wed, 12 May 2010 23:53:48 +0200
changeset 36884 88cf4896b980
parent 36084 3176ec2244ad
child 36891 e0d295cb8bfd
permissions -rw-r--r--
moved the addition of DLO tactic into the Z3 theory (DLO is required only for Z3 proof reconstruction)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/SMT/SMT_Base.thy
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     3
*)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     4
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     5
header {* SMT-specific definitions and basic tools *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     6
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     7
theory SMT_Base
35105
1822c658a5e4 use full paths when importing theories
boehmes
parents: 34960
diff changeset
     8
imports Real "~~/src/HOL/Word/Word"
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
     9
uses
35943
51b9155467cc cache_io is now just a single ML file instead of a component
boehmes
parents: 35151
diff changeset
    10
  "~~/src/Tools/cache_io.ML"
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    11
  ("Tools/smt_normalize.ML")
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    12
  ("Tools/smt_monomorph.ML")
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    13
  ("Tools/smt_translate.ML")
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    14
  ("Tools/smt_solver.ML")
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    15
  ("Tools/smtlib_interface.ML")
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    16
begin
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    17
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    18
section {* Triggers for quantifier instantiation *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    19
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    20
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    21
Some SMT solvers support triggers for quantifier instantiation. Each trigger
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    22
consists of one ore more patterns. A pattern may either be a list of positive
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    23
subterms (the first being tagged by "pat" and the consecutive subterms tagged
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    24
by "andpat"), or a list of negative subterms (the first being tagged by "nopat"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    25
and the consecutive subterms tagged by "andpat").
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    26
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    27
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    28
datatype pattern = Pattern
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    29
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    30
definition pat :: "'a \<Rightarrow> pattern"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    31
where "pat _ = Pattern"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    32
33249
2b65e9ed2e6e removed unused file smt_builtin.ML,
boehmes
parents: 33010
diff changeset
    33
definition nopat :: "'a \<Rightarrow> pattern"
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    34
where "nopat _ = Pattern"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    35
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    36
definition andpat :: "pattern \<Rightarrow> 'a \<Rightarrow> pattern" (infixl "andpat" 60)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    37
where "_ andpat _ = Pattern"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    38
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    39
definition trigger :: "pattern list \<Rightarrow> bool \<Rightarrow> bool"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    40
where "trigger _ P = P"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    41
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    42
36884
88cf4896b980 moved the addition of DLO tactic into the Z3 theory (DLO is required only for Z3 proof reconstruction)
boehmes
parents: 36084
diff changeset
    43
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    44
section {* Arithmetic *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    45
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    46
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    47
The sign of @{term "op mod :: int \<Rightarrow> int \<Rightarrow> int"} follows the sign of the
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    48
divisor. In contrast to that, the sign of the following operation is that of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    49
the dividend.
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    50
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    51
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    52
definition rem :: "int \<Rightarrow> int \<Rightarrow> int" (infixl "rem" 70)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    53
where "a rem b = 
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    54
  (if (a \<ge> 0 \<and> b < 0) \<or> (a < 0 \<and> b \<ge> 0) then - (a mod b) else a mod b)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    55
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    56
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    57
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    58
section {* Bitvectors *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    59
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    60
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    61
The following definitions provide additional functions not found in HOL-Word.
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    62
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    63
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    64
definition sdiv :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word" (infix "sdiv" 70)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    65
where "w1 sdiv w2 = word_of_int (sint w1 div sint w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    66
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    67
definition smod :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word" (infix "smod" 70)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    68
  (* sign follows divisor *)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    69
where "w1 smod w2 = word_of_int (sint w1 mod sint w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    70
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    71
definition srem :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word" (infix "srem" 70)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    72
  (* sign follows dividend *)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    73
where "w1 srem w2 = word_of_int (sint w1 rem sint w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    74
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    75
definition bv_shl :: "'a::len0 word \<Rightarrow> 'a word \<Rightarrow> 'a word"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    76
where "bv_shl w1 w2 = (w1 << unat w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    77
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    78
definition bv_lshr :: "'a::len0 word \<Rightarrow> 'a word \<Rightarrow> 'a word"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    79
where "bv_lshr w1 w2 = (w1 >> unat w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    80
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    81
definition bv_ashr :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> 'a word"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    82
where "bv_ashr w1 w2 = (w1 >>> unat w2)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    83
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    84
36884
88cf4896b980 moved the addition of DLO tactic into the Z3 theory (DLO is required only for Z3 proof reconstruction)
boehmes
parents: 36084
diff changeset
    85
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    86
section {* Higher-Order Encoding *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    87
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    88
definition "apply" where "apply f x = f x"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
    89
34960
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    90
definition array_ext where "array_ext a b = (SOME x. a = b \<or> a x \<noteq> b x)"
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    91
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    92
lemma fun_upd_eq: "(f = f (x := y)) = (f x = y)"
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    93
proof
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    94
  assume "f = f(x:=y)"
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    95
  hence "f x = (f(x:=y)) x" by simp
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    96
  thus "f x = y" by simp
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    97
qed (auto simp add: ext)
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    98
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
    99
lemmas array_rules =
1d5ee19ef940 support skolem constant for extensional arrays in Z3 proofs
boehmes
parents: 33249
diff changeset
   100
  ext fun_upd_apply fun_upd_same fun_upd_other fun_upd_upd fun_upd_eq apply_def
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   101
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   102
36884
88cf4896b980 moved the addition of DLO tactic into the Z3 theory (DLO is required only for Z3 proof reconstruction)
boehmes
parents: 36084
diff changeset
   103
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   104
section {* First-order logic *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   105
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   106
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   107
Some SMT solver formats require a strict separation between formulas and terms.
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   108
The following marker symbols are used internally to separate those categories:
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   109
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   110
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   111
definition formula :: "bool \<Rightarrow> bool" where "formula x = x"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   112
definition "term" where "term x = x"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   113
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   114
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   115
Predicate symbols also occurring as function symbols are turned into function
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   116
symbols by translating atomic formulas into terms:
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   117
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   118
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   119
abbreviation holds :: "bool \<Rightarrow> bool" where "holds \<equiv> (\<lambda>P. term P = term True)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   120
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   121
text {*
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   122
The following constant represents equivalence, to be treated differently than
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   123
the (polymorphic) equality predicate:
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   124
*}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   125
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   126
definition iff :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infix "iff" 50) where
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   127
  "(x iff y) = (x = y)"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   128
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   129
36884
88cf4896b980 moved the addition of DLO tactic into the Z3 theory (DLO is required only for Z3 proof reconstruction)
boehmes
parents: 36084
diff changeset
   130
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   131
section {* Setup *}
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   132
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   133
use "Tools/smt_normalize.ML"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   134
use "Tools/smt_monomorph.ML"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   135
use "Tools/smt_translate.ML"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   136
use "Tools/smt_solver.ML"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   137
use "Tools/smtlib_interface.ML"
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   138
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 35943
diff changeset
   139
setup {* SMT_Solver.setup *}
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   140
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents:
diff changeset
   141
end