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