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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#[crate::check_module(crate)]
#[allow(dead_code)]
mod rvn {
#[define]
#[derive(Clone, Debug, PartialEq, Eq)]
enum Nat {
// The zero variant
Z,
// The successor variant. The sub-value needs to go inside a
// Box (i.e. needs to get heap-allocated) so that Nat has a
// fixed byte-size, which is a requirement for all types in
// Rust.
//
// You unbox a value `n: Box<Nat>` using `*n`.
//
// You re-box a value `n: Nat` using `Box::new(n)`.
S(Box<Nat>)
}
impl Nat {
// This is a convenience function for the Rust tests at the
// bottom of this file. We don't declare it to Ravencheck.
fn from_usize(n: usize) -> Self {
if n == 0 {
Self::Z
} else {
Self::S(Box::new(Self::from_usize(n - 1)))
}
}
}
/// A trivial function that pattern-matches a Nat to the bottom
/// `Z` value, and then returns `Z`.
#[define]
#[recursive]
fn get_zero(a: Nat) -> Nat {
match a {
Nat::Z => Nat::Z,
// Note that `a_minus` must be unboxed (*) before passing
// it to `get_zero`.
Nat::S(a_minus) => get_zero(*a_minus),
}
}
// This is equivalent to the following #[annotate(..)] condition.
#[annotate_multi]
#[for_values(a: Nat)]
#[for_call(get_zero(a) => z)]
fn always_zero1() -> bool {
z == Nat::Z
}
#[annotate(get_zero(a) => z)]
fn always_zero2() -> bool {
z == Nat::Z
}
#[define]
#[recursive]
fn add(a: Nat, b: Nat) -> Nat {
match a {
Nat::Z => b,
Nat::S(a_minus) =>
// We unbox `a_minus` before calling `add`, and then
// re-box the output of `add` so that we can wrap it
// in Nat::S.
Nat::S(Box::new(add(*a_minus,b))),
}
}
// The following commutativity property for 'add' does not
// directly verify, but the same property for 'add_alt' does
// directly verify.
//
// What extra annotation(s) would let us verify 'add' without
// modifying it?
// #[annotate_multi]
// #[for_values(a: Nat, b: Nat)]
// #[for_call(add(a,b) => c)]
// #[for_call(add(b,a) => d)]
// fn add_commutative() -> bool {
// c == d
// }
#[define]
#[recursive]
fn add_alt(a: Nat, b: Nat) -> Nat {
match a.clone() {
Nat::Z => b,
Nat::S(a_minus) => match b {
Nat::Z => a,
Nat::S(b_minus) =>
Nat::S(Box::new(
Nat::S(Box::new(
add_alt(*a_minus, *b_minus)
))
))
}
}
}
// The #[annotate_multi] command allows you to recursively verify
// multiple function calls together, allowing for conditions like
// commutativity.
//
// Unlike #[annotate(..)], you must explicitly forall-quantify the
// inputs to the calls with the #[for_values] line.
#[annotate_multi]
#[for_values(a: Nat, b: Nat)]
#[for_call(add_alt(a,b) => c)]
#[for_call(add_alt(b,a) => d)]
fn add_alt_commutative() -> bool {
c == d
}
#[define]
#[recursive]
fn max(a: Nat, b: Nat) -> Nat {
match a {
Nat::Z => b,
Nat::S(a_minus) => match b {
Nat::Z => Nat::S(a_minus),
Nat::S(b_minus) => {
let c = max(*a_minus, *b_minus);
Nat::S(Box::new(c))
}
}
}
}
#[annotate_multi]
#[for_values(a: Nat, b: Nat)]
#[for_call(max(a,b) => c)]
#[for_call(max(b,a) => d)]
fn max_commutative() -> bool {
c == d
}
// Here are some normal Rust tests that actually run the above
// functions, just to check that we haven't made any simple
// mistakes in their definitions.
#[test]
fn max1() {
assert!(
max(Nat::from_usize(1), Nat::from_usize(0)) == Nat::from_usize(1)
)
}
#[test]
fn max2() {
assert!(
max(Nat::from_usize(0), Nat::from_usize(1)) == Nat::from_usize(1)
)
}
#[test]
fn max3() {
assert!(
max(Nat::from_usize(7), Nat::from_usize(7)) == Nat::from_usize(7)
)
}
#[test]
fn max4() {
assert!(
max(Nat::from_usize(7), Nat::from_usize(20)) == Nat::from_usize(20)
)
}
#[test]
fn max5() {
assert!(
max(Nat::from_usize(7), Nat::from_usize(2)) == Nat::from_usize(7)
)
}
#[test]
fn add1() {
assert!(
add(Nat::from_usize(1), Nat::from_usize(2)) == Nat::from_usize(3)
)
}
#[test]
fn add2() {
assert!(
add(Nat::from_usize(2), Nat::from_usize(1)) == Nat::from_usize(3)
)
}
}