src/HOL/SMT/Examples/SMT_Examples.thy
author boehmes
Fri, 18 Sep 2009 18:13:19 +0200
changeset 32618 42865636d006
child 32619 02f45a09a9f2
permissions -rw-r--r--
added new method "smt": an oracle-based connection to external SMT solvers
boehmes@32618
     1
(*  Title:       SMT_Examples.thy
boehmes@32618
     2
    Author:      Sascha Böhme, TU Muenchen
boehmes@32618
     3
*)
boehmes@32618
     4
boehmes@32618
     5
header {* Examples for the 'smt' tactic. *}
boehmes@32618
     6
boehmes@32618
     7
theory SMT_Examples
boehmes@32618
     8
imports "../SMT"
boehmes@32618
     9
begin
boehmes@32618
    10
boehmes@32618
    11
declare [[smt_solver=z3, z3_proofs=false]]
boehmes@32618
    12
declare [[smt_trace=true]] (*FIXME*)
boehmes@32618
    13
boehmes@32618
    14
boehmes@32618
    15
section {* Propositional and first-order logic *}
boehmes@32618
    16
boehmes@32618
    17
lemma "True" by smt
boehmes@32618
    18
lemma "p \<or> \<not>p" by smt
boehmes@32618
    19
lemma "(p \<and> True) = p" by smt
boehmes@32618
    20
lemma "(p \<or> q) \<and> \<not>p \<Longrightarrow> q" by smt
boehmes@32618
    21
lemma "(a \<and> b) \<or> (c \<and> d) \<Longrightarrow> (a \<and> b) \<or> (c \<and> d)" by smt
boehmes@32618
    22
lemma "P=P=P=P=P=P=P=P=P=P" by smt
boehmes@32618
    23
boehmes@32618
    24
axiomatization symm_f :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
boehmes@32618
    25
  symm_f: "symm_f x y = symm_f y x"
boehmes@32618
    26
lemma "a = a \<and> symm_f a b = symm_f b a" by (smt add: symm_f)
boehmes@32618
    27
boehmes@32618
    28
boehmes@32618
    29
section {* Linear arithmetic *}
boehmes@32618
    30
boehmes@32618
    31
lemma "(3::int) = 3" by smt
boehmes@32618
    32
lemma "(3::real) = 3" by smt
boehmes@32618
    33
lemma "(3 :: int) + 1 = 4" by smt
boehmes@32618
    34
lemma "max (3::int) 8 > 5" by smt
boehmes@32618
    35
lemma "abs (x :: real) + abs y \<ge> abs (x + y)" by smt
boehmes@32618
    36
lemma "let x = (2 :: int) in x + x \<noteq> 5" by smt
boehmes@32618
    37
boehmes@32618
    38
text{* 
boehmes@32618
    39
The following example was taken from HOL/ex/PresburgerEx.thy, where it says:
boehmes@32618
    40
boehmes@32618
    41
  This following theorem proves that all solutions to the
boehmes@32618
    42
  recurrence relation $x_{i+2} = |x_{i+1}| - x_i$ are periodic with
boehmes@32618
    43
  period 9.  The example was brought to our attention by John
boehmes@32618
    44
  Harrison. It does does not require Presburger arithmetic but merely
boehmes@32618
    45
  quantifier-free linear arithmetic and holds for the rationals as well.
boehmes@32618
    46
boehmes@32618
    47
  Warning: it takes (in 2006) over 4.2 minutes! 
boehmes@32618
    48
boehmes@32618
    49
There, it is proved by "arith". SMT is able to prove this within a fraction
boehmes@32618
    50
of one second.
boehmes@32618
    51
*}
boehmes@32618
    52
boehmes@32618
    53
lemma "\<lbrakk> x3 = abs x2 - x1; x4 = abs x3 - x2; x5 = abs x4 - x3;
boehmes@32618
    54
         x6 = abs x5 - x4; x7 = abs x6 - x5; x8 = abs x7 - x6;
boehmes@32618
    55
         x9 = abs x8 - x7; x10 = abs x9 - x8; x11 = abs x10 - x9 \<rbrakk>
boehmes@32618
    56
 \<Longrightarrow> x1 = x10 & x2 = (x11::int)"
boehmes@32618
    57
  by smt
boehmes@32618
    58
boehmes@32618
    59
lemma "\<exists>x::int. 0 < x" by smt
boehmes@32618
    60
lemma "\<exists>x::real. 0 < x" by smt
boehmes@32618
    61
lemma "\<forall>x y::int. x < y \<longrightarrow> (2 * x + 1) < (2 * y)" by smt
boehmes@32618
    62
lemma "\<forall>x y::int. (2 * x + 1) \<noteq> (2 * y)" by smt
boehmes@32618
    63
lemma "~ (\<exists>x y z::int. 4 * x + -6 * y = (1::int))" by smt
boehmes@32618
    64
lemma "~ (\<exists>x::int. False)" by smt
boehmes@32618
    65
boehmes@32618
    66
boehmes@32618
    67
section {* Non-linear arithmetic *}
boehmes@32618
    68
boehmes@32618
    69
lemma "((x::int) * (1 + y) - x * (1 - y)) = (2 * x * y)" by smt
boehmes@32618
    70
lemma
boehmes@32618
    71
  "(U::int) + (1 + p) * (b + e) + p * d =
boehmes@32618
    72
   U + (2 * (1 + p) * (b + e) + (1 + p) * d + d * p) - (1 + p) * (b + d + e)"
boehmes@32618
    73
  by smt
boehmes@32618
    74
boehmes@32618
    75
boehmes@32618
    76
section {* Linear arithmetic for natural numbers *}
boehmes@32618
    77
boehmes@32618
    78
lemma "a < 3 \<Longrightarrow> (7::nat) > 2 * a" by smt
boehmes@32618
    79
lemma "let x = (1::nat) + y in x - y > 0 * x" by smt
boehmes@32618
    80
lemma
boehmes@32618
    81
  "let x = (1::nat) + y in
boehmes@32618
    82
   let P = (if x > 0 then True else False) in
boehmes@32618
    83
   False \<or> P = (x - 1 = y) \<or> (\<not>P \<longrightarrow> False)"
boehmes@32618
    84
  by smt
boehmes@32618
    85
boehmes@32618
    86
boehmes@32618
    87
section {* Bitvectors *}
boehmes@32618
    88
boehmes@32618
    89
locale bv
boehmes@32618
    90
begin
boehmes@32618
    91
boehmes@32618
    92
declare [[smt_solver=z3]]
boehmes@32618
    93
boehmes@32618
    94
lemma "(27 :: 4 word) = -5" by smt
boehmes@32618
    95
lemma "(27 :: 4 word) = 11" by smt
boehmes@32618
    96
lemma "23 < (27::8 word)" by smt
boehmes@32618
    97
lemma "27 + 11 = (6::5 word)" by smt
boehmes@32618
    98
lemma "7 * 3 = (21::8 word)" by smt
boehmes@32618
    99
lemma "11 - 27 = (-16::8 word)" by smt
boehmes@32618
   100
lemma "- -11 = (11::5 word)" by smt
boehmes@32618
   101
lemma "-40 + 1 = (-39::7 word)" by smt
boehmes@32618
   102
lemma "a + 2 * b + c - b = (b + c) + (a :: 32 word)" by smt
boehmes@32618
   103
boehmes@32618
   104
lemma "0b110 AND 0b101 = (0b100 :: 32 word)" by smt
boehmes@32618
   105
lemma "0b110 OR 0b011 = (0b111 :: 8 word)" by smt
boehmes@32618
   106
lemma "0xF0 XOR 0xFF = (0x0F :: 8 word)" by smt
boehmes@32618
   107
lemma "NOT (0xF0 :: 16 word) = 0xFF0F" by smt
boehmes@32618
   108
boehmes@32618
   109
lemma "word_cat (27::4 word) (27::8 word) = (2843::12 word)" by smt
boehmes@32618
   110
lemma "word_cat (0b0011::4 word) (0b1111::6word) = (0b0011001111 :: 10 word)" 
boehmes@32618
   111
  by smt
boehmes@32618
   112
boehmes@32618
   113
lemma "slice 1 (0b10110 :: 4 word) = (0b11 :: 2 word)" by smt
boehmes@32618
   114
boehmes@32618
   115
lemma "ucast (0b1010 :: 4 word) = (0b1010 :: 10 word)" by smt
boehmes@32618
   116
lemma "scast (0b1010 :: 4 word) = (0b111010 :: 6 word)" by smt
boehmes@32618
   117
boehmes@32618
   118
lemma "bv_lshr 0b10011 2 = (0b100::8 word)" by smt
boehmes@32618
   119
lemma "bv_ashr 0b10011 2 = (0b100::8 word)" by smt
boehmes@32618
   120
boehmes@32618
   121
lemma "word_rotr 2 0b0110 = (0b1001::4 word)" by smt
boehmes@32618
   122
lemma "word_rotl 1 0b1110 = (0b1101::4 word)" by smt
boehmes@32618
   123
boehmes@32618
   124
lemma "(x AND 0xff00) OR (x AND 0x00ff) = (x::16 word)" by smt
boehmes@32618
   125
boehmes@32618
   126
lemma "w < 256 \<Longrightarrow> (w :: 16 word) AND 0x00FF = w" by smt
boehmes@32618
   127
boehmes@32618
   128
end
boehmes@32618
   129
boehmes@32618
   130
boehmes@32618
   131
section {* Pairs *}
boehmes@32618
   132
boehmes@32618
   133
lemma "fst (x, y) = a \<Longrightarrow> x = a" by smt
boehmes@32618
   134
lemma "p1 = (x, y) \<and> p2 = (y, x) \<Longrightarrow> fst p1 = snd p2" by smt
boehmes@32618
   135
boehmes@32618
   136
boehmes@32618
   137
section {* Higher-order problems and recursion *}
boehmes@32618
   138
boehmes@32618
   139
lemma "(f g x = (g x \<and> True)) \<or> (f g x = True) \<or> (g x = True)" by smt
boehmes@32618
   140
lemma "P ((2::int) < 3) = P True" by smt
boehmes@32618
   141
lemma "P ((2::int) < 3) = (P True :: bool)" by smt
boehmes@32618
   142
lemma "P (0 \<le> (a :: 4 word)) = P True" using [[smt_solver=z3]] by smt
boehmes@32618
   143
lemma "id 3 = 3 \<and> id True = True" by (smt add: id_def)
boehmes@32618
   144
lemma "i \<noteq> i1 \<and> i \<noteq> i2 \<Longrightarrow> ((f (i1 := v1)) (i2 := v2)) i = f i" by smt
boehmes@32618
   145
lemma "map (\<lambda>i::nat. i + 1) [0, 1] = [1, 2]" by (smt add: map.simps)
boehmes@32618
   146
lemma "(ALL x. P x) | ~ All P" by smt
boehmes@32618
   147
boehmes@32618
   148
fun dec_10 :: "nat \<Rightarrow> nat" where
boehmes@32618
   149
  "dec_10 n = (if n < 10 then n else dec_10 (n - 10))"
boehmes@32618
   150
lemma "dec_10 (4 * dec_10 4) = 6" by (smt add: dec_10.simps)
boehmes@32618
   151
boehmes@32618
   152
axiomatization
boehmes@32618
   153
  eval_dioph :: "int list \<Rightarrow> nat list \<Rightarrow> int"
boehmes@32618
   154
  where
boehmes@32618
   155
  eval_dioph_mod:
boehmes@32618
   156
  "eval_dioph ks xs mod int n = eval_dioph ks (map (\<lambda>x. x mod n) xs) mod int n"
boehmes@32618
   157
  and
boehmes@32618
   158
  eval_dioph_div_mult:
boehmes@32618
   159
  "eval_dioph ks (map (\<lambda>x. x div n) xs) * int n +
boehmes@32618
   160
   eval_dioph ks (map (\<lambda>x. x mod n) xs) = eval_dioph ks xs"
boehmes@32618
   161
lemma
boehmes@32618
   162
  "(eval_dioph ks xs = l) =
boehmes@32618
   163
   (eval_dioph ks (map (\<lambda>x. x mod 2) xs) mod 2 = l mod 2 \<and>
boehmes@32618
   164
    eval_dioph ks (map (\<lambda>x. x div 2) xs) =
boehmes@32618
   165
      (l - eval_dioph ks (map (\<lambda>x. x mod 2) xs)) div 2)"
boehmes@32618
   166
  by (smt add: eval_dioph_mod[where n=2] eval_dioph_div_mult[where n=2])
boehmes@32618
   167
boehmes@32618
   168
boehmes@32618
   169
section {* Monomorphization examples *}
boehmes@32618
   170
boehmes@32618
   171
definition P :: "'a \<Rightarrow> bool" where "P x = True"
boehmes@32618
   172
lemma poly_P: "P x \<and> (P [x] \<or> \<not>P[x])" by (simp add: P_def)
boehmes@32618
   173
lemma "P (1::int)" by (smt add: poly_P)
boehmes@32618
   174
boehmes@32618
   175
consts g :: "'a \<Rightarrow> nat"
boehmes@32618
   176
axioms
boehmes@32618
   177
  g1: "g (Some x) = g [x]"
boehmes@32618
   178
  g2: "g None = g []"
boehmes@32618
   179
  g3: "g xs = length xs"
boehmes@32618
   180
lemma "g (Some (3::int)) = g (Some True)" by (smt add: g1 g2 g3 list.size)
boehmes@32618
   181
boehmes@32618
   182
end