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:
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
|
||||
Reference in New Issue
Block a user