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:
51
assets/why3/exercises/ex3_two_way.mlw
Normal file
51
assets/why3/exercises/ex3_two_way.mlw
Normal file
@@ -0,0 +1,51 @@
|
||||
(* Two Way Sort
|
||||
|
||||
The following program sorts an array of Boolean values, with False<True.
|
||||
|
||||
E.g.
|
||||
two_way_sorted [True; False; False; True; False]
|
||||
= [False; False; False; True; True]
|
||||
|
||||
- Strengthen the invariants to prove correctness.
|
||||
*)
|
||||
|
||||
module TwoWaySort
|
||||
|
||||
use int.Int
|
||||
use bool.Bool
|
||||
use ref.Refint
|
||||
use array.Array
|
||||
use array.ArraySwap
|
||||
use array.ArrayPermut
|
||||
|
||||
predicate (<<) (x y: bool) = x = False \/ y = True
|
||||
|
||||
predicate sorted (a: array bool) =
|
||||
forall i1 i2: int. 0 <= i1 <= i2 < a.length -> a[i1] << a[i2]
|
||||
|
||||
let two_way_sort (a: array bool) : unit
|
||||
ensures { sorted a }
|
||||
ensures { permut_all (old a) a }
|
||||
=
|
||||
let ref i = 0 in
|
||||
let ref j = length a - 1 in
|
||||
while i < j do
|
||||
invariant { 0 <= i /\ j < length a }
|
||||
invariant { forall i1: int. 0 <= i1 < i
|
||||
-> true (* TODO: Replace `true` with your solution *) }
|
||||
invariant { forall i2: int. j < i2 < length a
|
||||
-> true (* TODO: Replace `true` with your solution *) }
|
||||
invariant { true (* TODO: Replace `true` with your solution *) }
|
||||
variant { j - i }
|
||||
if not a[i] then
|
||||
incr i
|
||||
else if a[j] then
|
||||
decr j
|
||||
else begin
|
||||
swap a i j;
|
||||
incr i;
|
||||
decr j
|
||||
end
|
||||
done
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user