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:
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
|
||||
37
assets/why3/exercises/solutions/ex2_fact_sol.mlw
Normal file
37
assets/why3/exercises/solutions/ex2_fact_sol.mlw
Normal file
@@ -0,0 +1,37 @@
|
||||
(* Two programs to compute the factorial
|
||||
|
||||
*)
|
||||
|
||||
module FactRecursive
|
||||
|
||||
use int.Int
|
||||
use int.Fact
|
||||
|
||||
let rec fact_rec (n: int) : int
|
||||
requires { n >= 0 }
|
||||
ensures { result = fact n }
|
||||
variant { n }
|
||||
=
|
||||
if n = 0 then 1 else n * fact_rec (n - 1)
|
||||
|
||||
end
|
||||
|
||||
module FactLoop
|
||||
|
||||
use int.Int
|
||||
use int.Fact
|
||||
|
||||
let fact_loop (n: int) : int
|
||||
requires { 0 < n }
|
||||
ensures { result = fact n }
|
||||
= let ref m = 0 in
|
||||
let ref r = 1 in
|
||||
while m < n do
|
||||
invariant { 0 <= m <= n /\ r = fact m }
|
||||
variant { n - m }
|
||||
m <- m + 1;
|
||||
r <- r * m
|
||||
done;
|
||||
r
|
||||
|
||||
end
|
||||
49
assets/why3/exercises/solutions/ex3_two_way_sol.mlw
Normal file
49
assets/why3/exercises/solutions/ex3_two_way_sol.mlw
Normal file
@@ -0,0 +1,49 @@
|
||||
(* 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 invariant 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 -> a[i1] = False }
|
||||
invariant { forall i2: int. j < i2 < length a -> a[i2] = True }
|
||||
invariant { permut_all (old a) a }
|
||||
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