TP-modelisation/TP6/division.mlw
2023-06-10 20:56:24 +02:00

24 lines
499 B
Plaintext
Executable file

(* Division entière par l'algorithme d'Euclide *)
module Division
use int.Int
use ref.Refint
let division (a b: int) (* renvoie une paire d'entiers (quotient,reste) *)
requires { 0 <= a && 0 < b }
ensures { let (q,r) = result in q * b + r = a && 0 <= r < b }
=
let q = ref 0 in
let r = ref a in
while !r >= b do
invariant { a = !q * b + !r && 0 <= !q && 0 <= !r }
variant { !r - b }
q := (!q) + 1;
r := (!r) - b
done;
(!q,!r)
end