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:
AnHaechan
2023-08-21 07:13:27 +00:00
parent 24dc47a7cf
commit d28bca2b18
27 changed files with 863 additions and 938 deletions

View 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