forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Towers_of_Hanoi_spec.rs
40 lines (35 loc) · 972 Bytes
/
Towers_of_Hanoi_spec.rs
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
//! An adaptation of the example from
//! http://rosettacode.org/wiki/Towers_of_Hanoi#Rust
//!
//! Changes:
//!
//! + Replaced ``println!`` with calling trusted functions.
//!
//! Verified properties:
//!
//! + Absence of panics.
//! + Absence of overflows.
//! + The `print_move` function is called with valid, different, poles
extern crate prusti_contracts;
#[pure]
fn valid_pole(x: i32) -> bool {
x == 1 || x == 2 || x == 3
}
#[trusted]
#[requires="valid_pole(from) && valid_pole(to) && from != to"]
fn print_move(from: i32, to: i32) {
println!("Move disk from pole {} to pole {}", from, to);
}
#[requires="n >= 0"]
#[requires="valid_pole(from) && valid_pole(to) && valid_pole(via)"]
#[requires="from != to && to != via && via != from"]
fn move_(n: i32, from: i32, to: i32, via: i32) {
if n > 0 {
move_(n - 1, from, via, to);
print_move(from, to);
move_(n - 1, via, to, from);
}
}
fn main() {
move_(4, 1,2,3);
}