src/HOL/Word/Tools/smt_word.ML
author boehmes
Wed Dec 15 10:12:44 2010 +0100 (2010-12-15)
changeset 41127 2ea84c8535c6
parent 41072 9f9bc1bdacef
child 41281 679118e35378
permissions -rw-r--r--
re-implemented eta-expansion, lambda-lifting, and explicit application on terms (exploiting the control over the term structure);
abolished SMT interface concept in favor of solver classes (now also the translation configuration is stored in the context);
proof reconstruction is now expected to return a theorem stating False (and hence needs to discharge all hypothetical definitions);
built-in functions carry additionally their arity and their most general type;
slightly generalized the definition of fun_app
boehmes@41060
     1
(*  Title:      HOL/Tools/SMT/smt_word.ML
boehmes@41060
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@41060
     3
boehmes@41060
     4
SMT setup for words.
boehmes@41060
     5
*)
boehmes@41060
     6
boehmes@41060
     7
signature SMT_WORD =
boehmes@41060
     8
sig
boehmes@41060
     9
  val setup: theory -> theory
boehmes@41060
    10
end
boehmes@41060
    11
boehmes@41060
    12
structure SMT_Word: SMT_WORD =
boehmes@41060
    13
struct
boehmes@41060
    14
boehmes@41061
    15
structure B = SMT_Builtin
boehmes@41061
    16
boehmes@41060
    17
boehmes@41060
    18
(* utilities *)
boehmes@41060
    19
boehmes@41060
    20
fun dest_binT T =
boehmes@41060
    21
  (case T of
boehmes@41060
    22
    Type (@{type_name "Numeral_Type.num0"}, _) => 0
boehmes@41060
    23
  | Type (@{type_name "Numeral_Type.num1"}, _) => 1
boehmes@41060
    24
  | Type (@{type_name "Numeral_Type.bit0"}, [T]) => 2 * dest_binT T
boehmes@41060
    25
  | Type (@{type_name "Numeral_Type.bit1"}, [T]) => 1 + 2 * dest_binT T
boehmes@41060
    26
  | _ => raise TYPE ("dest_binT", [T], []))
boehmes@41060
    27
boehmes@41060
    28
fun is_wordT (Type (@{type_name word}, _)) = true
boehmes@41060
    29
  | is_wordT _ = false
boehmes@41060
    30
boehmes@41060
    31
fun dest_wordT (Type (@{type_name word}, [T])) = dest_binT T
boehmes@41060
    32
  | dest_wordT T = raise TYPE ("dest_wordT", [T], [])
boehmes@41060
    33
boehmes@41060
    34
boehmes@41060
    35
(* SMT-LIB logic *)
boehmes@41060
    36
boehmes@41060
    37
fun smtlib_logic ts =
boehmes@41060
    38
  if exists (Term.exists_type (Term.exists_subtype is_wordT)) ts
boehmes@41060
    39
  then SOME "QF_AUFBV"
boehmes@41060
    40
  else NONE
boehmes@41060
    41
boehmes@41060
    42
boehmes@41060
    43
(* SMT-LIB builtins *)
boehmes@41060
    44
boehmes@41060
    45
local
boehmes@41061
    46
  val smtlibC = SMTLIB_Interface.smtlibC
boehmes@41061
    47
boehmes@41061
    48
  val wordT = @{typ "'a::len word"}
boehmes@41061
    49
boehmes@41060
    50
  fun index1 n i = n ^ "[" ^ string_of_int i ^ "]"
boehmes@41060
    51
  fun index2 n i j = n ^ "[" ^ string_of_int i ^ ":" ^ string_of_int j ^ "]"
boehmes@41060
    52
boehmes@41061
    53
  fun word_typ (Type (@{type_name word}, [T])) =
boehmes@41060
    54
        Option.map (index1 "BitVec") (try dest_binT T)
boehmes@41061
    55
    | word_typ _ = NONE
boehmes@41060
    56
boehmes@41061
    57
  fun word_num (Type (@{type_name word}, [T])) i =
boehmes@41060
    58
        Option.map (index1 ("bv" ^ string_of_int i)) (try dest_binT T)
boehmes@41061
    59
    | word_num _ _ = NONE
boehmes@41060
    60
boehmes@41060
    61
  fun if_fixed n T ts =
boehmes@41127
    62
    let val (Us, U) = Term.strip_type T
boehmes@41127
    63
    in
boehmes@41127
    64
      if forall (can dest_wordT) (U :: Us) then
boehmes@41127
    65
        SOME (((n, length Us), T), ts, T)
boehmes@41127
    66
      else NONE
boehmes@41127
    67
    end
boehmes@41060
    68
boehmes@41061
    69
  fun if_fixed' n T ts =
boehmes@41127
    70
    let val Ts = Term.binder_types T
boehmes@41127
    71
    in
boehmes@41127
    72
      if forall (can dest_wordT) Ts then SOME (((n, length Ts), T), ts, T)
boehmes@41127
    73
      else NONE
boehmes@41127
    74
    end
boehmes@41061
    75
boehmes@41061
    76
  fun add_word_fun f (t, n) =
boehmes@41061
    77
    B.add_builtin_fun smtlibC (Term.dest_Const t, K (f n))
boehmes@41061
    78
boehmes@41061
    79
  fun add_word_fun' f (t, n) = add_word_fun f (t, n)
boehmes@41061
    80
boehmes@41060
    81
  fun dest_word_funT (Type ("fun", [T, U])) = (dest_wordT T, dest_wordT U)
boehmes@41060
    82
    | dest_word_funT T = raise TYPE ("dest_word_funT", [T], [])
boehmes@41060
    83
  fun dest_nat (@{const nat} $ n :: ts) = (snd (HOLogic.dest_number n), ts)
boehmes@41060
    84
    | dest_nat ts = raise TERM ("dest_nat", ts)
boehmes@41060
    85
  fun dest_nat_word_funT (T, ts) =
boehmes@41060
    86
    (dest_word_funT (Term.range_type T), dest_nat ts)
boehmes@41060
    87
boehmes@41060
    88
  fun shift n T ts =
boehmes@41127
    89
    let
boehmes@41127
    90
      val U = Term.domain_type T
boehmes@41127
    91
      val T' = [U, U] ---> U
boehmes@41060
    92
    in
boehmes@41127
    93
      (case (can dest_wordT T', ts) of
boehmes@41060
    94
        (true, [t, u]) =>
boehmes@41060
    95
          (case try HOLogic.dest_number u of
boehmes@41127
    96
            SOME (_, i) => SOME (((n, 2), T'), [t, HOLogic.mk_number T' i], T')
boehmes@41060
    97
          | NONE => NONE)  (* FIXME: also support non-numerical shifts *)
boehmes@41060
    98
      | _ => NONE)
boehmes@41060
    99
    end
boehmes@41060
   100
boehmes@41061
   101
  fun extract n T ts =
boehmes@41061
   102
    try dest_nat_word_funT (T, ts)
boehmes@41127
   103
    |> Option.map (fn ((_, i), (lb, ts')) =>
boehmes@41127
   104
         let val T' = Term.range_type T
boehmes@41127
   105
         in (((index2 n (i + lb - 1) lb, 1), T'), ts', T') end)
boehmes@41061
   106
boehmes@41060
   107
  fun extend n T ts =
boehmes@41060
   108
    (case try dest_word_funT T of
boehmes@41127
   109
      SOME (i, j) =>
boehmes@41127
   110
        if j-i >= 0 then SOME (((index1 n (j-i), 1), T), ts, T)
boehmes@41127
   111
        else NONE
boehmes@41060
   112
    | _ => NONE)
boehmes@41060
   113
boehmes@41060
   114
  fun rotate n T ts =
boehmes@41127
   115
    let val T' = Term.range_type T
boehmes@41127
   116
    in
boehmes@41127
   117
      try dest_nat ts
boehmes@41127
   118
      |> Option.map (fn (i, ts') => (((index1 n i, 1), T'), ts', T'))
boehmes@41127
   119
    end
boehmes@41060
   120
in
boehmes@41060
   121
boehmes@41061
   122
val setup_builtins =
boehmes@41061
   123
  B.add_builtin_typ smtlibC (wordT, word_typ, word_num) #>
boehmes@41061
   124
  fold (add_word_fun' if_fixed) [
boehmes@41061
   125
    (@{term "uminus :: 'a::len word => _"}, "bvneg"),
boehmes@41061
   126
    (@{term "plus :: 'a::len word => _"}, "bvadd"),
boehmes@41061
   127
    (@{term "minus :: 'a::len word => _"}, "bvsub"),
boehmes@41061
   128
    (@{term "times :: 'a::len word => _"}, "bvmul"),
boehmes@41061
   129
    (@{term "bitNOT :: 'a::len word => _"}, "bvnot"),
boehmes@41061
   130
    (@{term "bitAND :: 'a::len word => _"}, "bvand"),
boehmes@41061
   131
    (@{term "bitOR :: 'a::len word => _"}, "bvor"),
boehmes@41061
   132
    (@{term "bitXOR :: 'a::len word => _"}, "bvxor"),
boehmes@41061
   133
    (@{term "word_cat :: 'a::len word => _"}, "concat") ] #>
boehmes@41061
   134
  fold (add_word_fun shift) [
boehmes@41061
   135
    (@{term "shiftl :: 'a::len word => _ "}, "bvshl"),
boehmes@41061
   136
    (@{term "shiftr :: 'a::len word => _"}, "bvlshr"),
boehmes@41061
   137
    (@{term "sshiftr :: 'a::len word => _"}, "bvashr") ] #>
boehmes@41061
   138
  add_word_fun extract
boehmes@41061
   139
    (@{term "slice :: _ => 'a::len word => _"}, "extract") #>
boehmes@41061
   140
  fold (add_word_fun extend) [
boehmes@41061
   141
    (@{term "ucast :: 'a::len word => _"}, "zero_extend"),
boehmes@41061
   142
    (@{term "scast :: 'a::len word => _"}, "sign_extend") ] #>
boehmes@41061
   143
  fold (add_word_fun rotate) [
boehmes@41061
   144
    (@{term word_rotl}, "rotate_left"),
boehmes@41061
   145
    (@{term word_rotr}, "rotate_right") ] #>
boehmes@41061
   146
  fold (add_word_fun' if_fixed') [
boehmes@41061
   147
    (@{term "less :: 'a::len word => _"}, "bvult"),
boehmes@41061
   148
    (@{term "less_eq :: 'a::len word => _"}, "bvule"),
boehmes@41061
   149
    (@{term word_sless}, "bvslt"),
boehmes@41061
   150
    (@{term word_sle}, "bvsle") ]
boehmes@41060
   151
boehmes@41060
   152
end
boehmes@41060
   153
boehmes@41060
   154
boehmes@41060
   155
(* setup *)
boehmes@41060
   156
boehmes@41060
   157
val setup = 
boehmes@41072
   158
  Context.theory_map (
boehmes@41072
   159
    SMTLIB_Interface.add_logic (20, smtlib_logic) #>
boehmes@41072
   160
    setup_builtins)
boehmes@41060
   161
boehmes@41060
   162
end