src/HOL/SMT_Examples/SMT_Examples.thy
author haftmann
Sat Jul 05 11:01:53 2014 +0200 (2014-07-05)
changeset 57514 bdc2c6b40bf2
parent 57232 8cecd655eef4
child 57696 fb71c6f100f8
permissions -rw-r--r--
prefer ac_simps collections over separate name bindings for add and mult
boehmes@36899
     1
(*  Title:      HOL/SMT_Examples/SMT_Examples.thy
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
*)
boehmes@36898
     4
boehmes@36899
     5
header {* Examples for the SMT binding *}
boehmes@36898
     6
boehmes@36898
     7
theory SMT_Examples
boehmes@36899
     8
imports Complex_Main
boehmes@36898
     9
begin
boehmes@36898
    10
blanchet@47152
    11
declare [[smt_certificates = "SMT_Examples.certs"]]
blanchet@47152
    12
declare [[smt_read_only_certificates = true]]
boehmes@36898
    13
blanchet@56079
    14
declare [[smt2_certificates = "SMT_Examples.certs2"]]
blanchet@56079
    15
declare [[smt2_read_only_certificates = true]]
boehmes@36898
    16
boehmes@36898
    17
boehmes@36898
    18
section {* Propositional and first-order logic *}
boehmes@36898
    19
blanchet@56079
    20
lemma "True" by smt2
blanchet@56079
    21
lemma "p \<or> \<not>p" by smt2
blanchet@56079
    22
lemma "(p \<and> True) = p" by smt2
blanchet@56079
    23
lemma "(p \<or> q) \<and> \<not>p \<Longrightarrow> q" by smt2
blanchet@56109
    24
lemma "(a \<and> b) \<or> (c \<and> d) \<Longrightarrow> (a \<and> b) \<or> (c \<and> d)" by smt2
blanchet@56079
    25
lemma "(p1 \<and> p2) \<or> p3 \<longrightarrow> (p1 \<longrightarrow> (p3 \<and> p2) \<or> (p1 \<and> p3)) \<or> p1" by smt2
blanchet@56079
    26
lemma "P = P = P = P = P = P = P = P = P = P" by smt2
boehmes@36898
    27
blanchet@46084
    28
lemma
blanchet@56079
    29
  assumes "a \<or> b \<or> c \<or> d"
blanchet@56079
    30
      and "e \<or> f \<or> (a \<and> d)"
blanchet@56079
    31
      and "\<not> (a \<or> (c \<and> ~c)) \<or> b"
blanchet@56079
    32
      and "\<not> (b \<and> (x \<or> \<not> x)) \<or> c"
blanchet@56079
    33
      and "\<not> (d \<or> False) \<or> c"
blanchet@56079
    34
      and "\<not> (c \<or> (\<not> p \<and> (p \<or> (q \<and> \<not> q))))"
boehmes@36898
    35
  shows False
blanchet@56079
    36
  using assms by smt2
boehmes@36898
    37
boehmes@36898
    38
axiomatization symm_f :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
boehmes@36898
    39
  symm_f: "symm_f x y = symm_f y x"
blanchet@56109
    40
blanchet@56079
    41
lemma "a = a \<and> symm_f a b = symm_f b a" by (smt2 symm_f)
boehmes@36898
    42
blanchet@46084
    43
(*
boehmes@36898
    44
Taken from ~~/src/HOL/ex/SAT_Examples.thy.
boehmes@36898
    45
Translated from TPTP problem library: PUZ015-2.006.dimacs
boehmes@36898
    46
*)
blanchet@46084
    47
lemma
boehmes@36898
    48
  assumes "~x0"
boehmes@36898
    49
  and "~x30"
boehmes@36898
    50
  and "~x29"
boehmes@36898
    51
  and "~x59"
blanchet@56079
    52
  and "x1 \<or> x31 \<or> x0"
blanchet@56079
    53
  and "x2 \<or> x32 \<or> x1"
blanchet@56079
    54
  and "x3 \<or> x33 \<or> x2"
blanchet@56079
    55
  and "x4 \<or> x34 \<or> x3"
blanchet@56079
    56
  and "x35 \<or> x4"
blanchet@56079
    57
  and "x5 \<or> x36 \<or> x30"
blanchet@56079
    58
  and "x6 \<or> x37 \<or> x5 \<or> x31"
blanchet@56079
    59
  and "x7 \<or> x38 \<or> x6 \<or> x32"
blanchet@56079
    60
  and "x8 \<or> x39 \<or> x7 \<or> x33"
blanchet@56079
    61
  and "x9 \<or> x40 \<or> x8 \<or> x34"
blanchet@56079
    62
  and "x41 \<or> x9 \<or> x35"
blanchet@56079
    63
  and "x10 \<or> x42 \<or> x36"
blanchet@56079
    64
  and "x11 \<or> x43 \<or> x10 \<or> x37"
blanchet@56079
    65
  and "x12 \<or> x44 \<or> x11 \<or> x38"
blanchet@56079
    66
  and "x13 \<or> x45 \<or> x12 \<or> x39"
blanchet@56079
    67
  and "x14 \<or> x46 \<or> x13 \<or> x40"
blanchet@56079
    68
  and "x47 \<or> x14 \<or> x41"
blanchet@56079
    69
  and "x15 \<or> x48 \<or> x42"
blanchet@56079
    70
  and "x16 \<or> x49 \<or> x15 \<or> x43"
blanchet@56079
    71
  and "x17 \<or> x50 \<or> x16 \<or> x44"
blanchet@56079
    72
  and "x18 \<or> x51 \<or> x17 \<or> x45"
blanchet@56079
    73
  and "x19 \<or> x52 \<or> x18 \<or> x46"
blanchet@56079
    74
  and "x53 \<or> x19 \<or> x47"
blanchet@56079
    75
  and "x20 \<or> x54 \<or> x48"
blanchet@56079
    76
  and "x21 \<or> x55 \<or> x20 \<or> x49"
blanchet@56079
    77
  and "x22 \<or> x56 \<or> x21 \<or> x50"
blanchet@56079
    78
  and "x23 \<or> x57 \<or> x22 \<or> x51"
blanchet@56079
    79
  and "x24 \<or> x58 \<or> x23 \<or> x52"
blanchet@56079
    80
  and "x59 \<or> x24 \<or> x53"
blanchet@56079
    81
  and "x25 \<or> x54"
blanchet@56079
    82
  and "x26 \<or> x25 \<or> x55"
blanchet@56079
    83
  and "x27 \<or> x26 \<or> x56"
blanchet@56079
    84
  and "x28 \<or> x27 \<or> x57"
blanchet@56079
    85
  and "x29 \<or> x28 \<or> x58"
blanchet@56079
    86
  and "~x1 \<or> ~x31"
blanchet@56079
    87
  and "~x1 \<or> ~x0"
blanchet@56079
    88
  and "~x31 \<or> ~x0"
blanchet@56079
    89
  and "~x2 \<or> ~x32"
blanchet@56079
    90
  and "~x2 \<or> ~x1"
blanchet@56079
    91
  and "~x32 \<or> ~x1"
blanchet@56079
    92
  and "~x3 \<or> ~x33"
blanchet@56079
    93
  and "~x3 \<or> ~x2"
blanchet@56079
    94
  and "~x33 \<or> ~x2"
blanchet@56079
    95
  and "~x4 \<or> ~x34"
blanchet@56079
    96
  and "~x4 \<or> ~x3"
blanchet@56079
    97
  and "~x34 \<or> ~x3"
blanchet@56079
    98
  and "~x35 \<or> ~x4"
blanchet@56079
    99
  and "~x5 \<or> ~x36"
blanchet@56079
   100
  and "~x5 \<or> ~x30"
blanchet@56079
   101
  and "~x36 \<or> ~x30"
blanchet@56079
   102
  and "~x6 \<or> ~x37"
blanchet@56079
   103
  and "~x6 \<or> ~x5"
blanchet@56079
   104
  and "~x6 \<or> ~x31"
blanchet@56079
   105
  and "~x37 \<or> ~x5"
blanchet@56079
   106
  and "~x37 \<or> ~x31"
blanchet@56079
   107
  and "~x5 \<or> ~x31"
blanchet@56079
   108
  and "~x7 \<or> ~x38"
blanchet@56079
   109
  and "~x7 \<or> ~x6"
blanchet@56079
   110
  and "~x7 \<or> ~x32"
blanchet@56079
   111
  and "~x38 \<or> ~x6"
blanchet@56079
   112
  and "~x38 \<or> ~x32"
blanchet@56079
   113
  and "~x6 \<or> ~x32"
blanchet@56079
   114
  and "~x8 \<or> ~x39"
blanchet@56079
   115
  and "~x8 \<or> ~x7"
blanchet@56079
   116
  and "~x8 \<or> ~x33"
blanchet@56079
   117
  and "~x39 \<or> ~x7"
blanchet@56079
   118
  and "~x39 \<or> ~x33"
blanchet@56079
   119
  and "~x7 \<or> ~x33"
blanchet@56079
   120
  and "~x9 \<or> ~x40"
blanchet@56079
   121
  and "~x9 \<or> ~x8"
blanchet@56079
   122
  and "~x9 \<or> ~x34"
blanchet@56079
   123
  and "~x40 \<or> ~x8"
blanchet@56079
   124
  and "~x40 \<or> ~x34"
blanchet@56079
   125
  and "~x8 \<or> ~x34"
blanchet@56079
   126
  and "~x41 \<or> ~x9"
blanchet@56079
   127
  and "~x41 \<or> ~x35"
blanchet@56079
   128
  and "~x9 \<or> ~x35"
blanchet@56079
   129
  and "~x10 \<or> ~x42"
blanchet@56079
   130
  and "~x10 \<or> ~x36"
blanchet@56079
   131
  and "~x42 \<or> ~x36"
blanchet@56079
   132
  and "~x11 \<or> ~x43"
blanchet@56079
   133
  and "~x11 \<or> ~x10"
blanchet@56079
   134
  and "~x11 \<or> ~x37"
blanchet@56079
   135
  and "~x43 \<or> ~x10"
blanchet@56079
   136
  and "~x43 \<or> ~x37"
blanchet@56079
   137
  and "~x10 \<or> ~x37"
blanchet@56079
   138
  and "~x12 \<or> ~x44"
blanchet@56079
   139
  and "~x12 \<or> ~x11"
blanchet@56079
   140
  and "~x12 \<or> ~x38"
blanchet@56079
   141
  and "~x44 \<or> ~x11"
blanchet@56079
   142
  and "~x44 \<or> ~x38"
blanchet@56079
   143
  and "~x11 \<or> ~x38"
blanchet@56079
   144
  and "~x13 \<or> ~x45"
blanchet@56079
   145
  and "~x13 \<or> ~x12"
blanchet@56079
   146
  and "~x13 \<or> ~x39"
blanchet@56079
   147
  and "~x45 \<or> ~x12"
blanchet@56079
   148
  and "~x45 \<or> ~x39"
blanchet@56079
   149
  and "~x12 \<or> ~x39"
blanchet@56079
   150
  and "~x14 \<or> ~x46"
blanchet@56079
   151
  and "~x14 \<or> ~x13"
blanchet@56079
   152
  and "~x14 \<or> ~x40"
blanchet@56079
   153
  and "~x46 \<or> ~x13"
blanchet@56079
   154
  and "~x46 \<or> ~x40"
blanchet@56079
   155
  and "~x13 \<or> ~x40"
blanchet@56079
   156
  and "~x47 \<or> ~x14"
blanchet@56079
   157
  and "~x47 \<or> ~x41"
blanchet@56079
   158
  and "~x14 \<or> ~x41"
blanchet@56079
   159
  and "~x15 \<or> ~x48"
blanchet@56079
   160
  and "~x15 \<or> ~x42"
blanchet@56079
   161
  and "~x48 \<or> ~x42"
blanchet@56079
   162
  and "~x16 \<or> ~x49"
blanchet@56079
   163
  and "~x16 \<or> ~x15"
blanchet@56079
   164
  and "~x16 \<or> ~x43"
blanchet@56079
   165
  and "~x49 \<or> ~x15"
blanchet@56079
   166
  and "~x49 \<or> ~x43"
blanchet@56079
   167
  and "~x15 \<or> ~x43"
blanchet@56079
   168
  and "~x17 \<or> ~x50"
blanchet@56079
   169
  and "~x17 \<or> ~x16"
blanchet@56079
   170
  and "~x17 \<or> ~x44"
blanchet@56079
   171
  and "~x50 \<or> ~x16"
blanchet@56079
   172
  and "~x50 \<or> ~x44"
blanchet@56079
   173
  and "~x16 \<or> ~x44"
blanchet@56079
   174
  and "~x18 \<or> ~x51"
blanchet@56079
   175
  and "~x18 \<or> ~x17"
blanchet@56079
   176
  and "~x18 \<or> ~x45"
blanchet@56079
   177
  and "~x51 \<or> ~x17"
blanchet@56079
   178
  and "~x51 \<or> ~x45"
blanchet@56079
   179
  and "~x17 \<or> ~x45"
blanchet@56079
   180
  and "~x19 \<or> ~x52"
blanchet@56079
   181
  and "~x19 \<or> ~x18"
blanchet@56079
   182
  and "~x19 \<or> ~x46"
blanchet@56079
   183
  and "~x52 \<or> ~x18"
blanchet@56079
   184
  and "~x52 \<or> ~x46"
blanchet@56079
   185
  and "~x18 \<or> ~x46"
blanchet@56079
   186
  and "~x53 \<or> ~x19"
blanchet@56079
   187
  and "~x53 \<or> ~x47"
blanchet@56079
   188
  and "~x19 \<or> ~x47"
blanchet@56079
   189
  and "~x20 \<or> ~x54"
blanchet@56079
   190
  and "~x20 \<or> ~x48"
blanchet@56079
   191
  and "~x54 \<or> ~x48"
blanchet@56079
   192
  and "~x21 \<or> ~x55"
blanchet@56079
   193
  and "~x21 \<or> ~x20"
blanchet@56079
   194
  and "~x21 \<or> ~x49"
blanchet@56079
   195
  and "~x55 \<or> ~x20"
blanchet@56079
   196
  and "~x55 \<or> ~x49"
blanchet@56079
   197
  and "~x20 \<or> ~x49"
blanchet@56079
   198
  and "~x22 \<or> ~x56"
blanchet@56079
   199
  and "~x22 \<or> ~x21"
blanchet@56079
   200
  and "~x22 \<or> ~x50"
blanchet@56079
   201
  and "~x56 \<or> ~x21"
blanchet@56079
   202
  and "~x56 \<or> ~x50"
blanchet@56079
   203
  and "~x21 \<or> ~x50"
blanchet@56079
   204
  and "~x23 \<or> ~x57"
blanchet@56079
   205
  and "~x23 \<or> ~x22"
blanchet@56079
   206
  and "~x23 \<or> ~x51"
blanchet@56079
   207
  and "~x57 \<or> ~x22"
blanchet@56079
   208
  and "~x57 \<or> ~x51"
blanchet@56079
   209
  and "~x22 \<or> ~x51"
blanchet@56079
   210
  and "~x24 \<or> ~x58"
blanchet@56079
   211
  and "~x24 \<or> ~x23"
blanchet@56079
   212
  and "~x24 \<or> ~x52"
blanchet@56079
   213
  and "~x58 \<or> ~x23"
blanchet@56079
   214
  and "~x58 \<or> ~x52"
blanchet@56079
   215
  and "~x23 \<or> ~x52"
blanchet@56079
   216
  and "~x59 \<or> ~x24"
blanchet@56079
   217
  and "~x59 \<or> ~x53"
blanchet@56079
   218
  and "~x24 \<or> ~x53"
blanchet@56079
   219
  and "~x25 \<or> ~x54"
blanchet@56079
   220
  and "~x26 \<or> ~x25"
blanchet@56079
   221
  and "~x26 \<or> ~x55"
blanchet@56079
   222
  and "~x25 \<or> ~x55"
blanchet@56079
   223
  and "~x27 \<or> ~x26"
blanchet@56079
   224
  and "~x27 \<or> ~x56"
blanchet@56079
   225
  and "~x26 \<or> ~x56"
blanchet@56079
   226
  and "~x28 \<or> ~x27"
blanchet@56079
   227
  and "~x28 \<or> ~x57"
blanchet@56079
   228
  and "~x27 \<or> ~x57"
blanchet@56079
   229
  and "~x29 \<or> ~x28"
blanchet@56079
   230
  and "~x29 \<or> ~x58"
blanchet@56079
   231
  and "~x28 \<or> ~x58"
boehmes@36898
   232
  shows False
blanchet@56727
   233
  using assms by smt2
boehmes@36898
   234
boehmes@36898
   235
lemma "\<forall>x::int. P x \<longrightarrow> (\<forall>y::int. P x \<or> P y)"
blanchet@56079
   236
  by smt2
boehmes@36898
   237
blanchet@46084
   238
lemma
boehmes@36898
   239
  assumes "(\<forall>x y. P x y = x)"
boehmes@36898
   240
  shows "(\<exists>y. P x y) = P x c"
blanchet@56727
   241
  using assms by smt2
boehmes@36898
   242
blanchet@46084
   243
lemma
boehmes@36898
   244
  assumes "(\<forall>x y. P x y = x)"
boehmes@36898
   245
  and "(\<forall>x. \<exists>y. P x y) = (\<forall>x. P x c)"
boehmes@36898
   246
  shows "(EX y. P x y) = P x c"
blanchet@56727
   247
  using assms by smt2
boehmes@36898
   248
boehmes@36898
   249
lemma
boehmes@36898
   250
  assumes "if P x then \<not>(\<exists>y. P y) else (\<forall>y. \<not>P y)"
boehmes@36898
   251
  shows "P x \<longrightarrow> P y"
blanchet@56079
   252
  using assms by smt2
boehmes@36898
   253
boehmes@36898
   254
boehmes@36898
   255
section {* Arithmetic *}
boehmes@36898
   256
boehmes@36898
   257
subsection {* Linear arithmetic over integers and reals *}
boehmes@36898
   258
blanchet@56079
   259
lemma "(3::int) = 3" by smt2
blanchet@56079
   260
lemma "(3::real) = 3" by smt2
blanchet@56079
   261
lemma "(3 :: int) + 1 = 4" by smt2
blanchet@56079
   262
lemma "x + (y + z) = y + (z + (x::int))" by smt2
blanchet@56079
   263
lemma "max (3::int) 8 > 5" by smt2
blanchet@56079
   264
lemma "abs (x :: real) + abs y \<ge> abs (x + y)" by smt2
blanchet@56079
   265
lemma "P ((2::int) < 3) = P True" by smt2
blanchet@56079
   266
lemma "x + 3 \<ge> 4 \<or> x < (1::int)" by smt2
boehmes@36898
   267
boehmes@36898
   268
lemma
boehmes@36898
   269
  assumes "x \<ge> (3::int)" and "y = x + 4"
blanchet@46084
   270
  shows "y - x > 0"
blanchet@56079
   271
  using assms by smt2
boehmes@36898
   272
blanchet@56079
   273
lemma "let x = (2 :: int) in x + x \<noteq> 5" by smt2
boehmes@36898
   274
boehmes@36898
   275
lemma
boehmes@36898
   276
  fixes x :: real
boehmes@36898
   277
  assumes "3 * x + 7 * a < 4" and "3 < 2 * x"
boehmes@36898
   278
  shows "a < 0"
blanchet@56079
   279
  using assms by smt2
boehmes@36898
   280
blanchet@56079
   281
lemma "(0 \<le> y + -1 * x \<or> \<not> 0 \<le> x \<or> 0 \<le> (x::int)) = (\<not> False)" by smt2
boehmes@36898
   282
boehmes@36898
   283
lemma "
blanchet@56079
   284
  (n < m \<and> m < n') \<or> (n < m \<and> m = n') \<or> (n < n' \<and> n' < m) \<or>
blanchet@56079
   285
  (n = n' \<and> n' < m) \<or> (n = m \<and> m < n') \<or>
blanchet@56079
   286
  (n' < m \<and> m < n) \<or> (n' < m \<and> m = n) \<or>
blanchet@56079
   287
  (n' < n \<and> n < m) \<or> (n' = n \<and> n < m) \<or> (n' = m \<and> m < n) \<or>
blanchet@56079
   288
  (m < n \<and> n < n') \<or> (m < n \<and> n' = n) \<or> (m < n' \<and> n' < n) \<or>
blanchet@56079
   289
  (m = n \<and> n < n') \<or> (m = n' \<and> n' < n) \<or>
blanchet@56079
   290
  (n' = m \<and> m = (n::int))"
blanchet@56079
   291
  by smt2
boehmes@36898
   292
blanchet@46084
   293
text{*
boehmes@36898
   294
The following example was taken from HOL/ex/PresburgerEx.thy, where it says:
boehmes@36898
   295
boehmes@36898
   296
  This following theorem proves that all solutions to the
boehmes@36898
   297
  recurrence relation $x_{i+2} = |x_{i+1}| - x_i$ are periodic with
boehmes@36898
   298
  period 9.  The example was brought to our attention by John
boehmes@36898
   299
  Harrison. It does does not require Presburger arithmetic but merely
boehmes@36898
   300
  quantifier-free linear arithmetic and holds for the rationals as well.
boehmes@36898
   301
blanchet@46084
   302
  Warning: it takes (in 2006) over 4.2 minutes!
boehmes@36898
   303
boehmes@36898
   304
There, it is proved by "arith". SMT is able to prove this within a fraction
boehmes@36898
   305
of one second. With proof reconstruction, it takes about 13 seconds on a Core2
boehmes@36898
   306
processor.
boehmes@36898
   307
*}
boehmes@36898
   308
boehmes@36898
   309
lemma "\<lbrakk> x3 = abs x2 - x1; x4 = abs x3 - x2; x5 = abs x4 - x3;
boehmes@36898
   310
         x6 = abs x5 - x4; x7 = abs x6 - x5; x8 = abs x7 - x6;
boehmes@36898
   311
         x9 = abs x8 - x7; x10 = abs x9 - x8; x11 = abs x10 - x9 \<rbrakk>
blanchet@56079
   312
 \<Longrightarrow> x1 = x10 \<and> x2 = (x11::int)"
blanchet@56079
   313
  by smt2
boehmes@36898
   314
boehmes@36898
   315
blanchet@56079
   316
lemma "let P = 2 * x + 1 > x + (x::real) in P \<or> False \<or> P" by smt2
boehmes@36898
   317
boehmes@48069
   318
lemma "x + (let y = x mod 2 in 2 * y + 1) \<ge> x + (1::int)"
blanchet@56109
   319
  using [[z3_new_extensions]] by smt2
boehmes@36898
   320
boehmes@48069
   321
lemma "x + (let y = x mod 2 in y + y) < x + (3::int)"
blanchet@56109
   322
  using [[z3_new_extensions]] by smt2
boehmes@36898
   323
boehmes@36898
   324
lemma
boehmes@36898
   325
  assumes "x \<noteq> (0::real)"
blanchet@56079
   326
  shows "x + x \<noteq> (let P = (abs x > 1) in if P \<or> \<not> P then 4 else 2) * x"
blanchet@56079
   327
  using assms [[z3_new_extensions]] by smt2
boehmes@36898
   328
blanchet@46084
   329
lemma
blanchet@46084
   330
  assumes "(n + m) mod 2 = 0" and "n mod 4 = 3"
blanchet@56079
   331
  shows "n mod 2 = 1 \<and> m mod 2 = (1::int)"
blanchet@56079
   332
  using assms [[z3_new_extensions]] by smt2
boehmes@36898
   333
boehmes@36898
   334
boehmes@36898
   335
subsection {* Linear arithmetic with quantifiers *}
boehmes@36898
   336
blanchet@56079
   337
lemma "~ (\<exists>x::int. False)" by smt2
blanchet@56079
   338
lemma "~ (\<exists>x::real. False)" by smt2
boehmes@36898
   339
blanchet@56727
   340
lemma "\<exists>x::int. 0 < x" by smt2
boehmes@36898
   341
boehmes@36898
   342
lemma "\<exists>x::real. 0 < x"
blanchet@56727
   343
  using [[smt2_oracle=true]] (* no Z3 proof *)
blanchet@56727
   344
  by smt2
boehmes@36898
   345
blanchet@56727
   346
lemma "\<forall>x::int. \<exists>y. y > x" by smt2
boehmes@36898
   347
blanchet@56079
   348
lemma "\<forall>x y::int. (x = 0 \<and> y = 1) \<longrightarrow> x \<noteq> y" by smt2
blanchet@56079
   349
lemma "\<exists>x::int. \<forall>y. x < y \<longrightarrow> y < 0 \<or> y >= 0" by smt2
blanchet@56079
   350
lemma "\<forall>x y::int. x < y \<longrightarrow> (2 * x + 1) < (2 * y)" by smt2
blanchet@56079
   351
lemma "\<forall>x y::int. (2 * x + 1) \<noteq> (2 * y)" by smt2
blanchet@56079
   352
lemma "\<forall>x y::int. x + y > 2 \<or> x + y = 2 \<or> x + y < 2" by smt2
blanchet@56079
   353
lemma "\<forall>x::int. if x > 0 then x + 1 > 0 else 1 > x" by smt2
blanchet@56727
   354
lemma "if (ALL x::int. x < 0 \<or> x > 0) then False else True" by smt2
blanchet@56727
   355
lemma "(if (ALL x::int. x < 0 \<or> x > 0) then -1 else 3) > (0::int)" by smt2
blanchet@56079
   356
lemma "~ (\<exists>x y z::int. 4 * x + -6 * y = (1::int))" by smt2
blanchet@56079
   357
lemma "\<exists>x::int. \<forall>x y. 0 < x \<and> 0 < y \<longrightarrow> (0::int) < x + y" by smt2
blanchet@56079
   358
lemma "\<exists>u::int. \<forall>(x::int) y::real. 0 < x \<and> 0 < y \<longrightarrow> -1 < x" by smt2
blanchet@56727
   359
lemma "\<exists>x::int. (\<forall>y. y \<ge> x \<longrightarrow> y > 0) \<longrightarrow> x > 0" by smt2
blanchet@57232
   360
lemma "\<forall>x::int.
blanchet@57232
   361
  SMT2.trigger (SMT2.Symb_Cons (SMT2.Symb_Cons (SMT2.pat x) SMT2.Symb_Nil) SMT2.Symb_Nil)
blanchet@57232
   362
    (x < a \<longrightarrow> 2 * x < 2 * a)" by smt2
blanchet@56079
   363
lemma "\<forall>(a::int) b::int. 0 < b \<or> b < 1" by smt2
boehmes@42318
   364
boehmes@36898
   365
boehmes@36898
   366
subsection {* Non-linear arithmetic over integers and reals *}
boehmes@36898
   367
boehmes@36898
   368
lemma "a > (0::int) \<Longrightarrow> a*b > 0 \<Longrightarrow> b > 0"
blanchet@56079
   369
  using [[smt2_oracle, z3_new_extensions]]
blanchet@56079
   370
  by smt2
boehmes@36899
   371
boehmes@41282
   372
lemma  "(a::int) * (x + 1 + y) = a * x + a * (y + 1)"
blanchet@56079
   373
  using [[z3_new_extensions]]
blanchet@56079
   374
  by smt2
boehmes@36898
   375
boehmes@41282
   376
lemma "((x::real) * (1 + y) - x * (1 - y)) = (2 * x * y)"
blanchet@56079
   377
  using [[z3_new_extensions]]
blanchet@56079
   378
  by smt2
boehmes@36898
   379
boehmes@36898
   380
lemma
boehmes@36898
   381
  "(U::int) + (1 + p) * (b + e) + p * d =
boehmes@36898
   382
   U + (2 * (1 + p) * (b + e) + (1 + p) * d + d * p) - (1 + p) * (b + d + e)"
blanchet@56109
   383
  using [[z3_new_extensions]] by smt2
boehmes@36898
   384
blanchet@56079
   385
lemma [z3_rule, z3_new_rule]:
boehmes@43893
   386
  fixes x :: "int"
boehmes@43893
   387
  assumes "x * y \<le> 0" and "\<not> y \<le> 0" and "\<not> x \<le> 0"
boehmes@43893
   388
  shows False
boehmes@43893
   389
  using assms by (metis mult_le_0_iff)
boehmes@43893
   390
boehmes@48069
   391
lemma "x * y \<le> (0 :: int) \<Longrightarrow> x \<le> 0 \<or> y \<le> 0"
blanchet@56079
   392
  using [[z3_with_extensions]] [[z3_new_extensions]]
blanchet@56079
   393
  by smt (* smt2 FIXME: "th-lemma" tactic fails *)
boehmes@43893
   394
boehmes@36898
   395
boehmes@36898
   396
subsection {* Linear arithmetic for natural numbers *}
boehmes@36898
   397
blanchet@56079
   398
lemma "2 * (x::nat) ~= 1" by smt2
boehmes@36898
   399
blanchet@56079
   400
lemma "a < 3 \<Longrightarrow> (7::nat) > 2 * a" by smt2
boehmes@36898
   401
blanchet@56079
   402
lemma "let x = (1::nat) + y in x - y > 0 * x" by smt2
boehmes@36898
   403
boehmes@36898
   404
lemma
boehmes@36898
   405
  "let x = (1::nat) + y in
boehmes@36898
   406
   let P = (if x > 0 then True else False) in
boehmes@36898
   407
   False \<or> P = (x - 1 = y) \<or> (\<not>P \<longrightarrow> False)"
blanchet@56079
   408
  by smt2
boehmes@36898
   409
blanchet@56079
   410
lemma "int (nat \<bar>x::int\<bar>) = \<bar>x\<bar>" by smt2
boehmes@36898
   411
boehmes@36898
   412
definition prime_nat :: "nat \<Rightarrow> bool" where
boehmes@36898
   413
  "prime_nat p = (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
blanchet@56079
   414
lemma "prime_nat (4*m + 1) \<Longrightarrow> m \<ge> (1::nat)" by (smt2 prime_nat_def)
boehmes@36898
   415
boehmes@36898
   416
boehmes@36898
   417
section {* Pairs *}
boehmes@36898
   418
boehmes@41132
   419
lemma "fst (x, y) = a \<Longrightarrow> x = a"
blanchet@56109
   420
  using fst_conv by smt2
boehmes@36898
   421
boehmes@41132
   422
lemma "p1 = (x, y) \<and> p2 = (y, x) \<Longrightarrow> fst p1 = snd p2"
blanchet@56109
   423
  using fst_conv snd_conv by smt2
boehmes@36898
   424
boehmes@36898
   425
boehmes@36898
   426
section {* Higher-order problems and recursion *}
boehmes@36898
   427
boehmes@41132
   428
lemma "i \<noteq> i1 \<and> i \<noteq> i2 \<Longrightarrow> (f (i1 := v1, i2 := v2)) i = f i"
blanchet@56109
   429
  using fun_upd_same fun_upd_apply by smt2
boehmes@36898
   430
boehmes@36898
   431
lemma "(f g (x::'a::type) = (g x \<and> True)) \<or> (f g x = True) \<or> (g x = True)"
blanchet@56079
   432
  by smt2
boehmes@36898
   433
blanchet@56109
   434
lemma "id x = x \<and> id True = True"
blanchet@56727
   435
  by (smt2 id_def)
boehmes@36898
   436
boehmes@41132
   437
lemma "i \<noteq> i1 \<and> i \<noteq> i2 \<Longrightarrow> ((f (i1 := v1)) (i2 := v2)) i = f i"
blanchet@56109
   438
  using fun_upd_same fun_upd_apply by smt2
boehmes@36898
   439
boehmes@41786
   440
lemma
boehmes@41786
   441
  "f (\<exists>x. g x) \<Longrightarrow> True"
boehmes@41786
   442
  "f (\<forall>x. g x) \<Longrightarrow> True"
blanchet@56079
   443
  by smt2+
boehmes@36899
   444
blanchet@56079
   445
lemma True using let_rsp by smt2
blanchet@56079
   446
lemma "le = op \<le> \<Longrightarrow> le (3::int) 42" by smt2
blanchet@56079
   447
lemma "map (\<lambda>i::nat. i + 1) [0, 1] = [1, 2]" by (smt2 list.map)
blanchet@56079
   448
lemma "(ALL x. P x) \<or> ~ All P" by smt2
boehmes@36898
   449
boehmes@36898
   450
fun dec_10 :: "nat \<Rightarrow> nat" where
boehmes@36898
   451
  "dec_10 n = (if n < 10 then n else dec_10 (n - 10))"
blanchet@56109
   452
blanchet@56079
   453
lemma "dec_10 (4 * dec_10 4) = 6" by (smt2 dec_10.simps)
boehmes@36898
   454
boehmes@36898
   455
axiomatization
boehmes@36898
   456
  eval_dioph :: "int list \<Rightarrow> nat list \<Rightarrow> int"
blanchet@56109
   457
where
boehmes@36898
   458
  eval_dioph_mod:
boehmes@36898
   459
  "eval_dioph ks xs mod int n = eval_dioph ks (map (\<lambda>x. x mod n) xs) mod int n"
blanchet@56109
   460
and
boehmes@36898
   461
  eval_dioph_div_mult:
boehmes@36898
   462
  "eval_dioph ks (map (\<lambda>x. x div n) xs) * int n +
boehmes@36898
   463
   eval_dioph ks (map (\<lambda>x. x mod n) xs) = eval_dioph ks xs"
blanchet@56109
   464
boehmes@36898
   465
lemma
boehmes@36898
   466
  "(eval_dioph ks xs = l) =
boehmes@36898
   467
   (eval_dioph ks (map (\<lambda>x. x mod 2) xs) mod 2 = l mod 2 \<and>
boehmes@36898
   468
    eval_dioph ks (map (\<lambda>x. x div 2) xs) =
boehmes@36898
   469
      (l - eval_dioph ks (map (\<lambda>x. x mod 2) xs)) div 2)"
blanchet@56079
   470
  using [[smt2_oracle=true]] (*FIXME*)
blanchet@56079
   471
  using [[z3_new_extensions]]
blanchet@56079
   472
  by (smt2 eval_dioph_mod[where n=2] eval_dioph_div_mult[where n=2])
boehmes@36898
   473
boehmes@36898
   474
boehmes@45393
   475
context complete_lattice
boehmes@45393
   476
begin
boehmes@45393
   477
blanchet@46084
   478
lemma
blanchet@56079
   479
  assumes "Sup {a | i::bool. True} \<le> Sup {b | i::bool. True}"
blanchet@56079
   480
  and "Sup {b | i::bool. True} \<le> Sup {a | i::bool. True}"
blanchet@56079
   481
  shows "Sup {a | i::bool. True} \<le> Sup {a | i::bool. True}"
blanchet@56079
   482
  using assms by (smt2 order_trans)
boehmes@45393
   483
boehmes@45393
   484
end
boehmes@45393
   485
boehmes@45393
   486
boehmes@36898
   487
section {* Monomorphization examples *}
boehmes@36898
   488
boehmes@36899
   489
definition Pred :: "'a \<Rightarrow> bool" where "Pred x = True"
blanchet@56079
   490
blanchet@56079
   491
lemma poly_Pred: "Pred x \<and> (Pred [x] \<or> \<not> Pred [x])" by (simp add: Pred_def)
blanchet@56109
   492
blanchet@56079
   493
lemma "Pred (1::int)" by (smt2 poly_Pred)
boehmes@36898
   494
boehmes@36899
   495
axiomatization g :: "'a \<Rightarrow> nat"
boehmes@36899
   496
axiomatization where
boehmes@36899
   497
  g1: "g (Some x) = g [x]" and
boehmes@36899
   498
  g2: "g None = g []" and
boehmes@36898
   499
  g3: "g xs = length xs"
blanchet@56079
   500
blanchet@56727
   501
lemma "g (Some (3::int)) = g (Some True)" by (smt2 g1 g2 g3 list.size)
boehmes@36898
   502
boehmes@36898
   503
end