Rosette extends Racket with a primitive bitvector datatype whose values are fixed-size words—or, machine integers. Mainstream programming languages, such as C or Java, support bitvector types with a few fixed sizes (e.g., 8 bits, 16 bits, and 32 bits). Rosette supports bitvectors of arbitrary size, as well as both signed and unsigned versions of various bitvector operations (such as comparisons, division, remainder, etc.). Technically, Rosette’s bitvector datatype embeds the theory of bitvectors into a programming language.
(bv #b0000100 7)
(bv #b0000100 7)
#f
#t
(bvadd x y)
(vc #t b)
> (bv6?1)#f
#t
#f
b
procedure
( bitvector? v)→boolean?
v:any/c
#t
#f
#f
#f
#f
#t
(bvsle v u)
(vc #t b)
(bv #x0 4)
(bv #xf 4)
(bv #xf 4)
(vc #t (! b))
(bv #x2 4)
(bv #b001 3)
(bv #b11110 5)
(bv #x2 4)
(vc #t (! b))
(bv #x1 4)
(bv #x0 4)
(bvneg z)
(bv #x1 4)
(bv #b111 3)
(bv #b11111 5)
(bv #x2 4)
(vc #t b)
(bv #xf7 8)
(union [b (bv #xf7 8)] [(! b) (bv #b111100111 9)])
(bv #b11 2)
(bv #b0 1)
(union
[(&& (= 0 j) (= 1 i)) (bv #b01 2)]
[(|| (&& (= 0 i) (= 0 j)) (&& (= 1 i) (= 1 j)))
(ite* (⊢ (&& (= 0 i) (= 0 j)) (bv #b1 1)) (⊢ (&& (= 1 i) (= 1 j)) ...))])
(vc #t (&& (&& (<= j i) (<= 0 j)) (< i 2)))
procedure
( sign-extend xt)→bv?
x:bv?x:bv?
(bv #b111101 6)
(bv #b001101 6)
(union [b (bv #b01101 5)] [(! b) (bv #b001101 6)])
(bv #b01101 5)
(vc #t b)
(bv #b01101 5)
(vc #t (&& b c))
-1
15
(ite b -1 -3)
-1
(vc #t b)
procedure
( integer->bitvector it)→bv?
i:integer?
(bv #b00 2)
(bv #xf 4)
(union [c (bv #b00011 5)] [(! c) (bv #b000011 6)])
(vc #t (! b))
(bv #b1 1)
(bv #b0 1)
(ite*
(⊢ (= 0 i) (extract 0 0 x))
(⊢ (= 1 i) (extract 1 1 x))
(⊢ (= 2 i) (extract 2 2 x))
(⊢ (= 3 i) (extract 3 3 x)))
(vc #t (&& (<= 0 i) (< i 4)))
(bv #b1 1)
(bv #b0 1)
(ite b (extract 0 0 x) (extract 0 0 y))
(ite b (extract 3 3 x) (extract 7 7 y))
(bveq (bv #x0 4) x)
(bveq (bv #x00 8) y)
(|| (&& (bveq (bv #x00 8) y) (! b)) (&& (bveq (bv #x0 4) x) b))
(bvadd (bv #x1 4) x)
(bvadd (bv #xff 8) y)
(union [b (bvadd (bv #x1 4) x)] [(! b) (bvadd (bv #x01 8) y)])
(union [b (bvadd (bv #xf 4) x)] [(! b) (bvadd (bv #xff 8) y)])
(bv #xf 4)
(bv #x2 4)
(bv #x2 4)
(bv #xf 4)
(bv #xf 4)
(vc #t b)
(bv #xc 4)
(bv #xc 4)
(bv #xc 4)
(vc #t (! b))
procedure
( rotate-left ix)→(bitvector n)
i:integer?i:integer?
(bv #x9 4)
(bv #x9 4)
(union
[b
(ite* (⊢ (= 0 i) (bv #x3 4)) (⊢ (= 1 i) (bv #x6 4)) (⊢ (= 2 i) ...) ...)]
[(! b)
(ite* (⊢ (= 0 i) (bv #x07 8)) (⊢ (= 1 i) (bv #x0e 8)) (⊢ (= 2 ...) ...) ...)])
(vc
#t
(&&
(<= 0 i)
(|| (! b) (&& (<= 0 i) (|| (< i 4) (! (|| b (! (<= 0 i)))))))
(|| b (&& (<= 0 i) (|| (< i 8) (! (|| (! b) (! (<= 0 i)))))))))
procedure
( bitvector->bits x)→(listof (bitvector 1))
(list (bv #b1 1) (bv #b1 1) (bv #b0 1) (bv #b0 1))
(list (extract 0 0 y) (extract 1 1 y))
(union
[b ((bv #b1 1) (bv #b1 1) (bv #b0 1) (bv #b0 1))]
[(! b) ((extract 0 0 y) (extract 1 1 y))])
procedure
( bitvector->bool x)→boolean?
procedure
( bool->bitvector b[t])→bv?
b:any/c
(bv #b000 3)
(bv #b1 1)
(ite b (bv #b1 1) (bv #b0 1))