-
Notifications
You must be signed in to change notification settings - Fork 11
/
Properties.bosatsu
77 lines (61 loc) · 1.94 KB
/
Properties.bosatsu
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Bosatsu/Properties
from Bosatsu/Rand import (Rand, run_Rand, prod_Rand, map_Rand,
int_range, sequence_Rand)
export (Prop, forall_Prop, suite_Prop, run_Prop)
struct Prop(fn: Int -> Rand[Test])
def operator -(a, b): a.sub(b)
def operator &(a, b): a.and_Int(b)
def operator <<(a, b): a.shift_left_Int(b)
def operator >>(a, b): a.shift_right_Int(b)
def forall_Prop[a](rand: Rand[a], name: String, fn: a -> Test) -> Prop:
Prop(cnt -> (
rands = replicate_List(rand, cnt)
seq = sequence_Rand(rands)
seq.map_Rand(as -> TestSuite(name, as.map_List(fn)))
))
def suite_Prop(name: String, props: List[Prop]) -> Prop:
Prop(s ->
s = sequence_Rand(props.map_List(Prop(fn) -> fn(s)))
s.map_Rand(as -> TestSuite(name, as))
)
def run_Prop(prop: Prop, trials: Int, seed: Int) -> Test:
Prop(fn) = prop
rand = fn(trials)
run_Rand(rand, seed)
signed64 = int_range(1 << 64).map_Rand(i -> i - (1 << 63))
def operator ==(a, b): cmp_Int(a, b) matches EQ
not_law = forall_Prop(
signed64,
"not_law",
i -> (
istr = int_to_String(i)
Assertion(i.not_Int() == (-1 - i), "~${istr} == (-1 - ${istr})")
))
shift_unshift_law = forall_Prop(
signed64.prod_Rand(int_range(32)),
"shift_unshift_law",
((i, k)) -> (
istr = int_to_String(i)
kstr = int_to_String(k)
Assertion((i << k) >> k == i, "(${istr} << ${kstr}) >> ${kstr} == ${istr}")
))
def gtez(x): x.cmp_Int(0) matches (GT | EQ)
def operator ||(a, b): True if a else b
def implies(a, b): b if a else True
positive_and_law = forall_Prop(
signed64.prod_Rand(signed64),
"x & y is >= 0 implies x >= 0 or y >= 0",
((x, y)) -> (
xs = int_to_String(x)
ys = int_to_String(y)
msg = "${xs} & ${ys} is >= 0 implies ${xs} >= 0 or ${ys} >= 0"
res = gtez(x & y).implies(gtez(x) || gtez(y))
Assertion(res, msg)
))
all_props = suite_Prop(
"integer props", [
not_law,
shift_unshift_law,
positive_and_law,
])
test = run_Prop(all_props, 100, 42)