mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-12 21:08:45 +00:00
assignment 1~5: fixes
- assignment05/pascal.mlw: lowered the difficulty (one more invariant given) - assignment02, 03: minor fixes & divide into sub-problems
This commit is contained in:
22
assets/why3/assignment05/binary_search.mlw
Normal file
22
assets/why3/assignment05/binary_search.mlw
Normal file
@@ -0,0 +1,22 @@
|
||||
(* Binary search
|
||||
|
||||
A classical example. Searches a sorted array for a given value v.
|
||||
Consult <https://gitlab.inria.fr/why3/why3/-/blob/master/examples/binary_search.mlw>.
|
||||
*)
|
||||
|
||||
module BinarySearch
|
||||
|
||||
use int.Int
|
||||
use int.ComputerDivision
|
||||
use ref.Ref
|
||||
use array.Array
|
||||
|
||||
let binary_search (a : array int) (v : int) : int
|
||||
requires { forall i1 i2 : int. 0 <= i1 < i2 < length a -> a[i1] <= a[i2] }
|
||||
ensures { 0 <= result <= length a }
|
||||
ensures { forall i: int. 0 <= i < result -> a[i] < v }
|
||||
ensures { forall i: int. result <= i < length a -> v <= a[i] }
|
||||
=
|
||||
(* IMPORTANT: DON'T MODIFY THE ABOVE LINES *)
|
||||
0 (* TODO *)
|
||||
end
|
||||
29
assets/why3/assignment05/max.mlw
Normal file
29
assets/why3/assignment05/max.mlw
Normal file
@@ -0,0 +1,29 @@
|
||||
(* Max
|
||||
|
||||
Given an array `a` of natural numbers with length `n`,
|
||||
return the maximum element of the array.
|
||||
|
||||
You should stengthen the loop invariant.
|
||||
*)
|
||||
|
||||
module Max
|
||||
|
||||
use int.Int
|
||||
use ref.Ref
|
||||
use array.Array
|
||||
|
||||
let max (a: array int) (n: int) : (max: int)
|
||||
requires { n = length a }
|
||||
requires { forall i. 0 <= i < n -> a[i] >= 0 }
|
||||
ensures { forall i. 0 <= i < n -> a[i] <= max }
|
||||
ensures { exists i. 0 <= i < n -> a[i] = max }
|
||||
= let ref max = 0 in
|
||||
for i = 0 to n - 1 do
|
||||
(* IMPORTANT: DON'T MODIFY THE ABOVE LINES *)
|
||||
invariant { true (* TODO: Replace `true` with your solution *) }
|
||||
(* IMPORTANT: DON'T MODIFY THE BELOW LINES *)
|
||||
if max < a[i] then max <- a[i];
|
||||
done;
|
||||
max
|
||||
|
||||
end
|
||||
39
assets/why3/assignment05/pascal.mlw
Normal file
39
assets/why3/assignment05/pascal.mlw
Normal file
@@ -0,0 +1,39 @@
|
||||
(* Pascal
|
||||
|
||||
Prove that the Pascal's triangle indeed computes combinations.
|
||||
HINT: https://en.wikipedia.org/wiki/Pascal%27s_triangle
|
||||
*)
|
||||
|
||||
module Pascal
|
||||
use int.Int
|
||||
use ref.Ref
|
||||
use array.Array
|
||||
|
||||
let rec function comb (n k: int) : int
|
||||
requires { 0 <= k <= n }
|
||||
variant { n }
|
||||
ensures { result >= 1 }
|
||||
= if k = 0 || k = n then 1 else comb (n-1) k + comb (n-1) (k-1)
|
||||
|
||||
(* Insert appropriate invariants so that Why3 can verify this function. *)
|
||||
(* You SHOULD understand the Pascal's triangle first to find good invariants. *)
|
||||
let chooses (n : int) : array int
|
||||
requires { n > 0 }
|
||||
ensures { forall i: int.
|
||||
0 <= i < length result -> result[i] = comb n i }
|
||||
=
|
||||
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 }
|
||||
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 *) }
|
||||
(* 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