diff --git a/dafny/2022/dafny_exercises_2022.pdf b/dafny/2022/dafny_exercises_2022.pdf new file mode 100644 index 0000000..4cc080e Binary files /dev/null and b/dafny/2022/dafny_exercises_2022.pdf differ diff --git a/dafny/2022/tasks_2022.dfy b/dafny/2022/tasks_2022.dfy new file mode 100644 index 0000000..cf8d68c --- /dev/null +++ b/dafny/2022/tasks_2022.dfy @@ -0,0 +1,120 @@ +// Dafny coursework tasks +// Autumn term, 2022 +// +// Authors: John Wickerson + +method countsquares(n:nat) returns (result:nat) + //ensures result == ... +{ + var i := 0; + result := 0; + while i < n { + i := i + 1; + result := result + i * i; + } +} + +method countsquares2(n:nat) returns (result:nat) + // ensures result == ... +{ + var i := n; + result := 0; + while i > 0 { + result := result + i * i; + i := i - 1; + } +} + +predicate sorted(A:array) + reads A +{ + forall m,n :: 0 <= m < n < A.Length ==> A[m] <= A[n] +} + +method binarysearch_between(A:array, v:int, lo:int, hi:int) returns (result:bool) + requires false // Please remove this line. +{ + if lo == hi { + return false; + } + var mid:int := (lo + hi) / 2; + if v == A[mid] { + return true; + } + if v < A[mid] { + result := binarysearch_between(A, v, lo, mid); + } + if v > A[mid] { + result := binarysearch_between(A, v, mid+1, hi); + } +} + +method binarysearch(A:array, v:int) returns (result:bool) + requires false // Please remove this line. +{ + result := binarysearch_between(A, v, 0, A.Length); +} + +method binarysearch_iter(A:array, v:int) returns (result:bool) +{ + // Please provide an implementation of this method. +} + +method partition(A:array, lo:int, hi:int) returns (pivot:int) + requires 0 <= lo < hi <= A.Length + ensures 0 <= lo <= pivot < hi + ensures forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k] + modifies A +{ + pivot := lo; + var i := lo+1; + while i < hi + invariant 0 <= lo <= pivot < i <= hi + invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k] + decreases hi - i + { + if A[i] < A[pivot] { + var j := i-1; + var tmp := A[i]; + A[i] := A[j]; + while pivot < j + invariant forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k] + decreases j + { + A[j+1] := A[j]; + j := j-1; + } + A[pivot+1] := A[pivot]; + A[pivot] := tmp; + pivot := pivot+1; + } + i := i+1; + } +} + +method quicksort_between(A:array, lo:int, hi:int) + requires 0 <= lo <= hi <= A.Length + ensures forall k :: (0 <= k < lo || hi <= k < A.Length) ==> old(A[k]) == A[k] + modifies A + decreases hi - lo +{ + if lo+1 >= hi { return; } + var pivot := partition(A, lo, hi); + quicksort_between(A, lo, pivot); + quicksort_between(A, pivot+1, hi); +} + +method quicksort(A:array) + modifies A +{ + quicksort_between(A, 0, A.Length); +} + +method Main() { + var A:array := new int[7] [4,0,1,9,7,1,2]; + print "Before: ", A[0], A[1], A[2], A[3], + A[4], A[5], A[6], "\n"; + quicksort(A); + print "After: ", A[0], A[1], A[2], A[3], + A[4], A[5], A[6], "\n"; +} diff --git a/isabelle/2022/HSV_tasks_2022.thy b/isabelle/2022/HSV_tasks_2022.thy new file mode 100644 index 0000000..e139bbb --- /dev/null +++ b/isabelle/2022/HSV_tasks_2022.thy @@ -0,0 +1,97 @@ +theory HSV_tasks_2022 imports Complex_Main begin + +section {* Task 1: Full adders *} + +fun fulladder :: "bool * bool * bool \ bool * bool" +where + "fulladder (a,b,cin) = ( + let (tmp1, tmp2) = halfadder(a,b) in + let (tmp3, s) = halfadder(cin,tmp2) in + let cout = tmp1 | tmp3 in + (cout, s))" + + +section {* Task 2: Fifth powers *} + +theorem "(n::nat) ^ 5 mod 10 = n mod 10" + oops + +section {* Task 3: Logic optimisation *} + +(* Datatype for representing simple circuits. *) +datatype "circuit" = + NOT "circuit" +| AND "circuit" "circuit" +| OR "circuit" "circuit" +| TRUE +| FALSE +| INPUT "int" + +(* Simulates a circuit given a valuation for each input wire. *) +fun simulate where + "simulate (AND c1 c2) \ = ((simulate c1 \) \ (simulate c2 \))" +| "simulate (OR c1 c2) \ = ((simulate c1 \) \ (simulate c2 \))" +| "simulate (NOT c) \ = (\ (simulate c \))" +| "simulate TRUE \ = True" +| "simulate FALSE \ = False" +| "simulate (INPUT i) \ = \ i" + +(* Equivalence between circuits. *) +fun circuits_equiv (infix "\" 50) (* the "50" indicates the operator precedence *) where + "c1 \ c2 = (\\. simulate c1 \ = simulate c2 \)" + +(* An optimisation that exploits the following Boolean identities: + `a | a = a` + `a & a = a` + *) +fun opt_ident where + "opt_ident (NOT c) = NOT (opt_ident c)" +| "opt_ident (AND c1 c2) = ( + let c1' = opt_ident c1 in + let c2' = opt_ident c2 in + if c1' = c2' then c1' else AND c1' c2')" +| "opt_ident (OR c1 c2) = ( + let c1' = opt_ident c1 in + let c2' = opt_ident c2 in + if c1' = c2' then c1' else OR c1' c2')" +| "opt_ident TRUE = TRUE" +| "opt_ident FALSE = FALSE" +| "opt_ident (INPUT i) = INPUT i" + +lemma (* test case *) + "opt_ident (AND (INPUT 1) (OR (INPUT 1) (INPUT 1))) = INPUT 1" +by eval + +theorem opt_ident_is_sound: "opt_ident c \ c" + oops + +fun area :: "circuit \ nat" where + "area (NOT c) = 1 + area c" +| "area (AND c1 c2) = 1 + area c1 + area c2" +| "area (OR c1 c2) = 1 + area c1 + area c2" +| "area _ = 0" + +section {* Task 4: More logic optimisation *} + +lemma (* test case *) + "opt_redundancy (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))) + = INPUT 1" + (* by eval *) oops +lemma (* test case *) + "opt_redundancy (AND (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))) + (OR (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))) (INPUT 2))) + = INPUT 1" + (* by eval *) oops +lemma (* test case *) + "opt_redundancy (AND (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))) + (OR (INPUT 2) (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))))) + = INPUT 1" + (* by eval *) oops +lemma (* test case *) + "opt_redundancy (AND (AND (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))) + (OR (INPUT 2) (AND (INPUT 1) (OR (INPUT 1) (INPUT 2))))) + (OR (INPUT 1) (INPUT 2))) + = INPUT 1" + (* by eval *) oops + +end \ No newline at end of file diff --git a/isabelle/2022/isabelle_exercises_2022.pdf b/isabelle/2022/isabelle_exercises_2022.pdf new file mode 100644 index 0000000..38217d5 Binary files /dev/null and b/isabelle/2022/isabelle_exercises_2022.pdf differ