forked from epfl-lara/stainless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooleanOps.scala
50 lines (39 loc) · 1022 Bytes
/
BooleanOps.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Copyright 2009-2021 EPFL, Lausanne */
import stainless.lang._
/**
* Check that &, | and ^ works on Boolean like it does on Int, with no shortcutting
*/
object BooleanOps {
def foo1(b: Boolean, d: Boolean): Boolean = {
var x = 0
val r = { x += 1; b } & { x *= 2; d }
assert(x == 2) // always
r
}.ensuring { res =>
(res == (b && d)) &&
(res == toBool(toInt(b) & toInt(d)))
}
def foo2(b: Boolean, d: Boolean): Boolean = {
var x = 0
val r = { x += 1; b } | { x *= 2; d }
assert(x == 2) // always
r
}.ensuring { res =>
(res == (b || d)) &&
(res == toBool(toInt(b) | toInt(d)))
}
def foo3(b: Boolean, d: Boolean): Boolean = {
var x = 0
val r = { x += 1; b } ^ { x *= 2; d }
assert(x == 2) // always
r
}.ensuring { res =>
(res == (b != d)) &&
(res == toBool(toInt(b) ^ toInt(d)))
}
private def toInt(b: Boolean): Int = if (b) 1 else 0
private def toBool(x: Int): Boolean = {
require(x == 0 || x == 1)
x == 1
}
}