mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-14 22:18:46 +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:
30
assets/why3/exercises/solutions/ex1_eucl_div_sol.mlw
Normal file
30
assets/why3/exercises/solutions/ex1_eucl_div_sol.mlw
Normal file
@@ -0,0 +1,30 @@
|
||||
(* Euclidean division
|
||||
|
||||
1. Prove correctness of euclideian divison:
|
||||
`division a b` returns an integer `q` such that
|
||||
`a = bq+r` and `0 <= r < b` for some `r`.
|
||||
|
||||
- You have to strengthen the precondition.
|
||||
- You have to strengthen the loop invariant.
|
||||
*)
|
||||
|
||||
module Division
|
||||
|
||||
use int.Int
|
||||
|
||||
let division (a b: int) : int
|
||||
requires { a >= 0 }
|
||||
requires { b > 0 }
|
||||
ensures { exists r: int. a = b * result + r /\ 0 <= r < b }
|
||||
=
|
||||
let ref q = 0 in
|
||||
let ref r = a in
|
||||
while r >= b do
|
||||
invariant { a = b * q + r /\ 0 <= r }
|
||||
variant { r }
|
||||
q <- q + 1;
|
||||
r <- r - b
|
||||
done;
|
||||
q
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user