-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wrote some programs #9
Changes from 1 commit
8d40628
226e36e
ad1a362
b0a0ce6
ef6c6d6
103a490
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,53 @@ use vstd::prelude::*; | |
|
||
verus! { | ||
|
||
// TODO: Put your solution (the specification, implementation, and proof) to the task here | ||
spec fn spec_bracketing_helper(brackets: Seq<char>) -> (int, bool) { | ||
edwin1729 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
brackets.fold_left((0, true), |p: (int, bool), c| { | ||
let (x, b) = p; | ||
match (c) { | ||
'<' => (x + 1, b), | ||
'>' => (x - 1 , b && x == 0), | ||
_ => (x, b), | ||
} | ||
}) | ||
} | ||
|
||
spec fn spec_bracketing(brackets: Seq<char>) -> bool { | ||
let p = spec_bracketing_helper(brackets); | ||
p.1 && p.0 == 0 | ||
} | ||
|
||
fn correct_bracketing(brackets: &str) -> (ret: bool) | ||
requires | ||
[email protected]() <= i32::MAX | ||
[email protected]() >= i32::MIN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this condition required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its to prevent overflow of the stack size local variable. |
||
ensures | ||
ret <==> spec_bracketing(brackets@) | ||
{ | ||
let mut i = 0; | ||
let mut b = true; | ||
let mut stack_size: i32 = 0; | ||
|
||
while i < brackets.unicode_len() | ||
invariant | ||
(stack_size as int, b) == spec_bracketing_helper([email protected](0, i as int)), | ||
stack_size <= i <= [email protected]() <= i32::MAX, | ||
stack_size >= -i >= [email protected]() >= i32::MIN, | ||
{ | ||
let c = brackets.get_char(i); | ||
let ghost prev = spec_bracketing_helper([email protected](0, i as int)); | ||
if (c == '<') { | ||
stack_size += 1; | ||
} else if (c == '>') { | ||
b = b && stack_size == 0; | ||
stack_size -= 1; | ||
} | ||
assert([email protected](0, i+1 as int).drop_last() =~= [email protected](0, i as int)); | ||
i += 1; | ||
} | ||
assert(brackets@ =~= [email protected](0, i as int)); | ||
b && stack_size == 0 | ||
} | ||
|
||
} // verus! | ||
fn main() {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,35 @@ use vstd::prelude::*; | |
|
||
verus! { | ||
|
||
// TODO: Put your solution (the specification, implementation, and proof) to the task here | ||
|
||
fn monotonic(l: Vec<i32>) -> (ret: bool) | ||
ensures | ||
ret <==> forall |i: int, j: int| 0 <= i < j < [email protected]() ==> [email protected](i) <= [email protected](j) || | ||
edwin1729 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
forall |i: int, j: int| 0 <= i < j < [email protected]() ==> [email protected](i) >= [email protected](j) | ||
{ | ||
if l.len() == 0 || l.len() == 1 { | ||
return true; | ||
} | ||
|
||
let mut increasing = true; | ||
let mut decreasing = true; | ||
|
||
let mut n = 0; | ||
while n < l.len() - 1 | ||
invariant | ||
l.len() > 1, | ||
n <= l.len() - 1, | ||
increasing <==> forall |i: int, j: int| 0 <= i < j < n+1 ==> [email protected](i) <= [email protected](j), | ||
decreasing <==> forall |i: int, j: int| 0 <= i < j < n+1 ==> [email protected](i) >= [email protected](j), | ||
{ | ||
if l[n] < l[n + 1] { | ||
decreasing = false; | ||
} else if l[n] > l[n + 1] { | ||
increasing = false; | ||
} | ||
n += 1; | ||
} | ||
increasing || decreasing | ||
} | ||
} // verus! | ||
fn main() {} | ||
|
||
|
edwin1729 marked this conversation as resolved.
Show resolved
Hide resolved
edwin1729 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,53 @@ use vstd::prelude::*; | |
|
||
verus! { | ||
|
||
// TODO: Put your solution (the specification, implementation, and proof) to the task here | ||
spec fn spec_bracketing_helper(brackets: Seq<char>) -> (int, bool) { | ||
brackets.fold_left((0, true), |p: (int, bool), c| { | ||
let (x, b) = p; | ||
match (c) { | ||
'(' => (x + 1, b), | ||
')' => (x - 1 , b && x == 0), | ||
_ => (x, b), | ||
} | ||
}) | ||
} | ||
|
||
spec fn spec_bracketing(brackets: Seq<char>) -> bool { | ||
let p = spec_bracketing_helper(brackets); | ||
p.1 && p.0 == 0 | ||
} | ||
|
||
fn correct_bracketing(brackets: &str) -> (ret: bool) | ||
requires | ||
[email protected]() <= i32::MAX | ||
[email protected]() >= i32::MIN | ||
ensures | ||
ret <==> spec_bracketing(brackets@) | ||
{ | ||
let mut i = 0; | ||
let mut b = true; | ||
let mut stack_size: i32 = 0; | ||
|
||
while i < brackets.unicode_len() | ||
invariant | ||
(stack_size as int, b) == spec_bracketing_helper([email protected](0, i as int)), | ||
stack_size <= i <= [email protected]() <= i32::MAX, | ||
stack_size >= -i >= [email protected]() >= i32::MIN, | ||
{ | ||
let c = brackets.get_char(i); | ||
let ghost prev = spec_bracketing_helper([email protected](0, i as int)); | ||
if (c == '(') { | ||
stack_size += 1; | ||
} else if (c == ')') { | ||
b = b && stack_size == 0; | ||
stack_size -= 1; | ||
} | ||
assert([email protected](0, i+1 as int).drop_last() =~= [email protected](0, i as int)); | ||
i += 1; | ||
} | ||
assert(brackets@ =~= [email protected](0, i as int)); | ||
b && stack_size == 0 | ||
} | ||
|
||
} // verus! | ||
fn main() {} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a reasonable solution in that it follows the Python version. It might be nice to include a variant of this file (as we've done for a few other challenges) that demonstrates a non-recursive solution as well.