mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-12 21:08:45 +00:00
Cleanups
This commit is contained in:
@@ -19,5 +19,5 @@ module BinarySearch
|
||||
=
|
||||
(* IMPORTANT: DON'T MODIFY THE ABOVE LINES *)
|
||||
0 (* TODO *)
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
Given an array `a` of integers with length `n` greater than `0`,
|
||||
return `max_idx`, the index of the maximum element of that array.
|
||||
|
||||
|
||||
E.g. `max_idx [5, 12, 34, 10] 4` will return `2`
|
||||
E.g. `max_idx [4, 3, 2] 3` will return `0`
|
||||
E.g. `max_idx [1, 2, 3, 4] 4` will return `3`
|
||||
|
||||
|
||||
Prove the below program indeed follows the given specification,
|
||||
by giving an appropriate invariant.
|
||||
*)
|
||||
@@ -22,12 +22,13 @@ module Max
|
||||
requires { n = length a }
|
||||
ensures { 0 <= max_idx <= n-1 }
|
||||
ensures { forall i. 0 <= i <= n-1 -> a[i] <= a[max_idx] }
|
||||
=
|
||||
=
|
||||
let ref max_idx = 0 in
|
||||
for i = 0 to n-1 do
|
||||
invariant { 0 <= max_idx <= n-1 }
|
||||
(* IMPORTANT: DON'T MODIFY THE ABOVE LINES *)
|
||||
invariant { true (* TODO: Replace `true` with your solution. Your solution MUST be a single line, at line number 30. DON'T add another line of codes. *) }
|
||||
(* TODO: Replace `true` with your solution. Your solution MUST be a single line, at line 31. DON'T add more code above or below. *)
|
||||
invariant { true }
|
||||
(* IMPORTANT: DON'T MODIFY THE BELOW LINES *)
|
||||
if a[max_idx] < a[i] then max_idx <- i;
|
||||
done;
|
||||
|
||||
@@ -17,9 +17,9 @@ module Pascal
|
||||
ensures { result >= 1 }
|
||||
= if k = 0 || k = n then 1 else comb (n-1) k + comb (n-1) (k-1)
|
||||
|
||||
(* Computes the Pascal's triangle and returns the `n`th row of it. *)
|
||||
(* Computes Pascal's triangle and returns the `n`th row of it. *)
|
||||
(* Insert an appropriate invariant so that Why3 can verify this function. *)
|
||||
(* You SHOULD understand the Pascal's triangle first to find good invariants. *)
|
||||
(* You should understand Pascal's triangle first to find good invariants. *)
|
||||
let chooses (n : int) : array int
|
||||
requires { n > 0 }
|
||||
ensures { forall i: int.
|
||||
@@ -28,16 +28,17 @@ module Pascal
|
||||
let ref row = Array.make 1 1 in
|
||||
for r = 1 to n do
|
||||
invariant { length row = r }
|
||||
invariant { forall c: int. 0 <= c < r -> row[c] = comb (r-1) c }
|
||||
invariant { forall c: int. 0 <= c < r -> row[c] = comb (r-1) c }
|
||||
let new_row = Array.make (r+1) 1 in
|
||||
for c = 1 to r-1 do
|
||||
(* IMPORTANT: DON'T MODIFY THE ABOVE LINES *)
|
||||
invariant { true (* TODO: Replace `true` with your solution. Your solution MUST be a single line, at line number 35. DON'T add another lines. *) }
|
||||
(* TODO: Replace `true` with your solution. Your solution MUST be a single line, at line 36. DON'T add more code above or below. *)
|
||||
invariant { true }
|
||||
(* IMPORTANT: DON'T MODIFY THE BELOW LINES *)
|
||||
new_row[c] <- row[c-1] + row[c]
|
||||
done;
|
||||
row <- new_row
|
||||
done;
|
||||
row
|
||||
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user