Assertions, Assertive Coding + Application to Sudoku Puzzles
|
|
- Myron Bruce
- 5 years ago
- Views:
Transcription
1 Assertions, Assertive Coding + Application to Sudoku Puzzles Jan van Eijck CWI & ILLC, Amsterdam Specification and Testing, Week 4, 2012
2 Abstract We show how to specify preconditions, postconditions, assertions and invariants, and how to wrap these around functional code or functional imperative code. We illustrate the use of this for writing programs for automated testing of code that is wrapped in appropriate assertions. We call this assertive coding. An assertive version of a function f is a function f that behaves exactly like f as long as f complies with its specification, and aborts with error otherwise. This is a much stronger sense of self-testing than what is called self-testing code (code with builtin tests) in test driven development. The lecture gives examples of how to use (inefficient) specification code to test (efficient) implementation code, and how to turn assertive code into production code by replacing the self-testing versions of the assertion wrappers by self-documenting versions that skip the assertion tests. We end with a demonstration of the use of formal methods in developing a sudoku solver.
3 Module Declaration module Week5 where import Data.List import Week4 hiding (g)
4 Algorithm Design and Specification: Some excellent books
5 And some more:
6 Functional Imperative Algorithm Specification This course will teach you a purely functional way to look at algorithms as they are designed, presented and analyzed in these books. This complements the approach of [5] and [2], which propose to give functional solutions for classical algorithmic problems. Instead, this course will show that classical algorithmic problems plus their classical solutions can be presented in a purely functional way.
7 Preconditions, Postconditions, Assertions and Invariants A (Hoare) assertion about an imperative program [3] has the form {Pre} Program {Post} where Pre and Post are conditions on states. This Hoare statement is true in state s if truth of Pre in s guarantees truth of Post in any state s that is a result state of performing Program in state s. One way to write assertions for functional code is as wrappers around functions. This results in a much stronger sense of self-testing than what is called self-testing code (code with built-in tests) in test driven development [1].
8 Precondition Wrappers The precondition of a function is a condition on its input parameter(s), the postcondition is a condition on its value. Here is a precondion wrapper for functions with one argument. The wrapper takes a precondition property and a function and produces a new function that behaves as the old one, provided the precondition is satisfied, pre1 :: (a -> Bool) -> (a -> b) -> a -> b pre1 p f x = if p x then f x else error "pre1"
9 Postcondition Wrappers A postcondition wrapper for functions with one argument. post1 :: (b -> Bool) -> (a -> b) -> a -> b post1 p f x = if p (f x) then f x else error "post1"
10 Example This can be used to specify the expected behaviour of a function. Consider the function g: g = while1 even ( div 2) The g function should always output an odd integer: godd = post1 odd g Note that godd has the same type as g, and that the two functions compute the same value whenever godd is defined (i.e., whenever the output value of g satisfies the postcondition).
11 Assertions More generally, an assertion is a condition that may relate input parameters to the computed value. Here is an assertion wrapper for functions with one argument. The wrapper wraps a binary relation expressing a condition on input and output around a function and produces a new function that behaves as the old one, provided that the relation holds. assert1 :: (a -> b -> Bool) -> (a -> b) -> a -> b assert1 p f x = if p x (f x) then f x else error "assert1" Example use: ga = assert1 (\ i o -> signum i == signum o) g Note that ga has the same type as g. Indeed, as long as the assertion holds, ga and g compute the same value.
12 Invariants An invariant of a program P in a state s is a condition C with the property that if C holds in s then C will also hold in any state that results from execution of P in s. Thus, invariants are Hoare assertions of the form: {C} Program {C} If you wrap an invariant around a step function in a loop, the invariant documents the expected behaviour of the loop.
13 Invariant Wrappers First an invariant wrapper for the case of a function with a single parameter: a function f :: a -> a fails an invariant p :: a -> Bool if the input of the function satisfies p but the output does not: invar1 :: (a -> Bool) -> (a -> a) -> a -> a invar1 p f x = let x = f x in if p x && not (p x ) then error "invar1" else x
14 Two Examples Example of an invariant wrap around g: gsign = invar1 (>0) g Another example: gsign = invar1 (<0) g
15 Use of Invariant Inside While Loop We can also use the invariant inside a while loop: g3 = while1 even (invar1 (>0) ( div 2))
16 An Assertive List Merge Algorithm Consider the problem of merging two sorted lists into a result list that is also sorted, and that contains the two original lists as sublists.
17 Implication Operator For writing specifications an operator for Boolean implication is good to have. infix 1 ==> (==>) :: Bool -> Bool -> Bool p ==> q = (not p) q
18 Sorted Property The specification for merge uses the following property: sortedprop :: Ord a => [a] -> [a] -> [a] -> Bool sortedprop xs ys zs = (sorted xs && sorted ys) ==> sorted zs sorted :: Ord a => [a] -> Bool sorted [] = True sorted [_] = True sorted (x:y:zs) = x <= y && sorted (y:zs)
19 Sublist Property Each list should occur as a sublist in the merge: sublistprop :: Eq a => [a] -> [a] -> [a] -> Bool sublistprop xs ys zs = sublist xs zs && sublist ys zs sublist :: Eq a => [a] -> [a] -> Bool sublist [] _ = True sublist (x:xs) ys = elem x ys && sublist xs (ys \\ [x])
20 Assertion wrapper for functions with two parameters assert2 :: (a -> b -> c -> Bool) -> (a -> b -> c) -> a -> b -> c assert2 p f x y = if p x y (f x y) then f x y else error "assert2"
21 A merge function merge :: Ord a => [a] -> [a] -> [a] merge xs [] = xs merge [] ys = ys merge (x:xs) (y:ys) = if x <= y then x : merge xs (y:ys) else y : merge (x:xs) ys And an assertive version of the merge function: mergea :: Ord a => [a] -> [a] -> [a] mergea = assert2 sortedprop $ assert2 sublistprop merge
22 Wrap Arond Wrap We have wrapped an assertion around a wrap of an assertion around a function. This cause no problems, for the wrap of an assertion around a function has the same type as the original function. Note that sortedprop is an implication. If we apply test-merge to a list that is not sorted, the property still holds: *AssertiveCoding> mergea [2,1] [3..10] [2,1,3,4,5,6,7,8,9,10]
23 Assertive Versions of the GCD Algorithm A precondition of the GCD algorithm is that its arguments are positive integers. This can be expressed as follows: euclid = assert2 (\ m n _ -> m > 0 && n > 0) euclidgcd This function has the same type as euclidgcd. Both euclid and euclidgcd are partial functions; in fact they are the same partial function. The difference is that euclid aborts where euclidgcd diverges.
24 Testing with Assertions An example assertion for Euclid s algorithm is that the result value is the greatest common divisor of the two inputs. To express that, we implement a bit of logic.
25 Universal Quantification The Haskell function all has type (a -> Bool) -> [a] -> Bool. Sometimes it is more convenient to have a universal quantifier of type [a] -> (a -> Bool) -> Bool. Here it is: forall = flip all
26 Divides The definition of GCD is given in terms of the divides relation. An integer n divides another integer m if there is an integer k with nk = m, in other words, if the process of dividing m by n leaves a remainder 0. divides :: Integer -> Integer -> Bool divides n m = rem m n == 0
27 GCD Definition An integer n is the GCD of k and m if n divides both k and m, and every divisor of k and m also divides n. isgcd :: Integer -> Integer -> Integer -> Bool isgcd k m n = divides n k && divides n m && forall [1..min k m] (\ x -> (divides x k && divides x m) ==> divides x n)
28 Assertive Version of Euclid s GCD Function euclid :: Integer -> Integer -> Integer euclid = assert2 isgcd euclid In case you don t see what s happening: the new function euclid computes the same value as euclid, but the assertion guarantees that if it would ever compute the wrong value, an exception will be raised.
29 A Test testeuclid :: Integer -> Bool testeuclid k = and [ (assert2 isgcd euclid) n m > 0 n <- [1..k], m <- [1..k] ] The subtlety here is that the assertion allows us to define what is right and what is wrong. Imperative programs are dumb. Functional programs are dumb, too. But assertions allow us to relate what a program does to what the program is supposed to do. The following test succeeds in under 25 seconds on my 3 Ghz dualcore machine: *AssertiveCoding> testeuclid 300 True
30 Finding An Invariant Another thing we can do is find a suitable invariant. An invariant for Euclid s step function is that the set of common divisors does not change. The following function gives the common divisors of two integers: divisors :: Integer -> Integer -> [Integer] divisors m n = let k = min m n in [ d d <- [2..k], divides d m, divides d n ]
31 Assertion for the Step Function We can use this in an assertion about the step function, as follows: samedivisors x y (x,y ) = divisors x y == divisors x y
32 Wrap the assertion around the step function euclidgcd :: Integer -> Integer -> Integer euclidgcd = while2 (\ x y -> x /= y) (assert2 samedivisors (\ x y -> if x > y then (x-y,y) else (x,y-x))) Note that the assertion samedivisors is in fact an invariant, stated as a relation between input and output of the step function.
33 Invariant Wrapper for Step Functions with Two Arguments invar2 :: (a -> b -> Bool) -> (a -> b -> (a,b)) -> a -> b -> (a,b) invar2 p f x y = let (x,y ) = f x y in if p x y && not (p x y ) then error "invar2" else (x,y )
34 Example As an example of how this is used, consider the following invariant. If d divides both x and y before the step, then d should divide x and y after the step. Let s add a parameter for such a divisor d, and state the invariant: euclidgcd :: Integer -> Integer -> Integer -> Integer euclidgcd = \ d -> while2 (\ x y -> x /= y) (invar2 (\ x y -> divides d x && divides d y) (\ x y -> if x > y then (x-y,y) else (x,y-x)))
35 Use of Example in Test testeuclid2 :: Integer -> Bool testeuclid2 k = and [ euclidgcd d n m >= 0 n <- [1..k], m <- [1..k], d <- [2..min n m] ]
36 The Extended GCD Algorithm The extended GCD algoritm extends the Euclidean algorithm, as follows. Instead of finding the GCD of two (positive) integers M and N it finds two integers x and y satisfying the so-called Bézout identity (or: Bézout equality): xm + yn = gcd(m, N). For example, for arguments M = 12 and N = 26, the extended GCD algorithm gives the pair x = 2 and y = 1. And indeed, = 2, which is the GCD of 12 and 26. Here is an imperative (iterative) version of the algorithm:
37 Extended GCD algorithm 1. Let positive integers a and b be given. 2. x := 0; 3. lastx := 1; 4. y := 1; 5. lasty := 0; 6. while b 0 do (a) (q, r) := quotrem(a, b); (b) (a, b) := (b, r); (c) (x, lastx) := (lastx q x, x); (d) (y, lasty) := (lasty q y, y). 7. Return (lastx, lasty).
38 Functional imperative version, in Haskell: ext_gcd :: Integer -> Integer -> (Integer,Integer) ext_gcd a b = let x = 0 y = 1 lastx = 1 lasty = 0 in ext_gcd a b x y (lastx,lasty) ext_gcd = while5 (\ _ b _ -> b /= 0) (\ a b x y (lastx,lasty) -> let (q,r) = quotrem a b (x,lastx ) = (lastx-q*x,x) (y,lasty ) = (lasty-q*y,y) in (b,r,x,y,(lastx,lasty )))
39 While5 This uses a while5 loop: while5 :: (a -> b -> c -> d -> e -> Bool) -> (a -> b -> c -> d -> e -> (a,b,c,d,e)) -> a -> b -> c -> d -> e -> e while5 p f x y z v w p x y z v w = let (x,y,z,v,w ) = f x y z v w in while5 p f x y z v w otherwise = w
40 Bézout s identity Bézout s identity is turned into an assertion, as follows: bezout :: Integer -> Integer -> (Integer,Integer) -> Bool bezout m n (x,y) = x*m + y*n == euclid m n Use of this to produce assertive code for the extended algorithm: ext_gcda = assert2 bezout ext_gcd
41 Extended Euclidean Algorithm, Functional Version A functional (recursive) version of the extended Euclidean algorithm: fct_gcd :: Integer -> Integer -> (Integer,Integer) fct_gcd a b = if b == 0 then (1,0) else let (q,r) = quotrem a b (s,t) = fct_gcd b r in (t, s - q*t) Again, an assertive version of this: fct_gcda = assert2 bezout fct_gcd
42 Assertion wrapper for functions with three arguments assert3 :: (a -> b -> c -> d -> Bool) -> (a -> b -> c -> d) -> a -> b -> c -> d assert3 p f x y z = if p x y z (f x y z) then f x y z else error "assert3"
43 Invariant wrapper for step functions with three arguments invar3 :: (a -> b -> c -> Bool) -> (a -> b -> c -> (a,b,c)) -> a -> b -> c -> (a,b,c) invar3 p f x y z = let (x,y,z ) = f x y z in if p x y z && not (p x y z ) then error "invar3" else (x,y,z )
44 Assertion wrapper for functions with four arguments assert4 :: (a -> b -> c -> d -> e -> Bool) -> (a -> b -> c -> d -> e) -> a -> b -> c -> d -> e assert4 p f x y z u = if p x y z u (f x y z u) then f x y z u else error "assert4"
45 Invariant wrapper for step functions with four arguments invar4 :: (a -> b -> c -> d -> Bool) -> (a -> b -> c -> d -> (a,b,c,d)) -> a -> b -> c -> d -> (a,b,c,d) invar4 p f x y z u = let (x,y,z,u ) = f x y z u in if p x y z u && not (p x y z u ) then error "invar4" else (x,y,z,u )
46 Asertion wrapper for functions with five arguments assert5 :: (a -> b -> c -> d -> e -> f -> Bool) -> (a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> f assert5 p f x y z u v = if p x y z u v (f x y z u v) then f x y z u v else error "assert5"
47 Invariant wrapper for step functions with five arguments invar5 :: (a -> b -> c -> d -> e -> Bool) -> (a -> b -> c -> d -> e -> (a,b,c,d,e)) -> a -> b -> c -> d -> e -> (a,b,c,d,e) invar5 p f x y z u v = let (x,y,z,u,v ) = f x y z u v in if p x y z u v && not (p x y z u v ) then error "invar5" else (x,y,z,u,v )
48 Assertive Code is Efficient Self-Documenting Code More often than not, an assertive version of a function is much less efficient than the regular version: the assertions are inefficient specification algorithms to test the behaviour of efficient functions. But this does not matter. To turn assertive code into self-documenting production code, all you have to do is load a module with alternative definitions of the assertion and invariant wrappers. Take the definition of assert1. This is replaced by: assert1 :: (a -> b -> Bool) -> (a -> b) -> a -> b assert1 _ = id And so on for the other wrappers. See module AssertDoc on the Course Website. The assertions are still in the code, but instead of being executed they now serve as documentation. The assertive version of a function executes exactly as the version without the assertion. Assertive code comes with absolutely no efficiency penalty.
49 What Are We Testing? Suppose a program (implemented function) fails its implemented assertion. What should we conclude? This is a pertinent question, for the assertion itself is a piece of code too, in the same programming language as the function that we want to test. So what are we testing: the correctness of the code? the correctness of the implememented specification for the code?
50 We Are Testing Both In fact, we are testing both at the same time. Therefore, the failure of a test can mean either of two things, and we should be careful to find out wat our situation is: 1. There is something wrong with the program. 2. There is something wrong with the specification of the assertion for the program. It is up to us to find out which case we are in. In both cases it is important to find out where the problem resides. In the first case, we have to fix a code defect, and we are in a good position to do so because we have the specification as a yardstick. In the second case, we are not ready to fix code defects. First and foremost, we have to fix a defect in our understanding of what our program is supposed to do. Without that growth in understanding, it will be very hard indeed to detect and fix possible defects in the code itself.
51 Usefulness of Formal Methods
52 Usefulness of Formal Methods Why are formal methods useful for software engineering?
53 Usefulness of Formal Methods Why are formal methods useful for software engineering? Why are formal methods useful for software testing?
54 Usefulness of Formal Methods Why are formal methods useful for software engineering? Why are formal methods useful for software testing? Isn t software testing supposed to be practical?
55 A Hero of Software Testing
56 About Tony Hoare
57 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort
58 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort Inventor of Hoare Logic logic
59 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort Inventor of Hoare Logic logic Inventor of CSP (specification language for concurrent processes) and moving force behind Occam.
60 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort Inventor of Hoare Logic logic Inventor of CSP (specification language for concurrent processes) and moving force behind Occam. Winner of the 1980 ACM Turing Award
61 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort Inventor of Hoare Logic logic Inventor of CSP (specification language for concurrent processes) and moving force behind Occam. Winner of the 1980 ACM Turing Award Main Focus of Research: make software more reliable, explain how software gets more reliable in practice.
62 About Tony Hoare Inventor of the QuickSort Algorithm Quicksort Inventor of Hoare Logic logic Inventor of CSP (specification language for concurrent processes) and moving force behind Occam. Winner of the 1980 ACM Turing Award Main Focus of Research: make software more reliable, explain how software gets more reliable in practice. See
63 Tony Hoare on the Purpose of Testing Philosophers of science have pointed out that no series of experiments, however long and however favourable can ever prove a theory correct; but even only a single contrary experiment will certainly falsify it. And it is a basic slogan of quality assurance that you cannot test quality into a product. How then can testing contribute to reliability of programs, theories and products? Is the confidence it gives illusory? [4]
64 The Purpose of Testing is to Test a Method The resolution of the paradox is well known in the theory of quality control. It is to ensure that a test made on a product is not a test of the product itself, but rather of the methods that have been used to produce it the processes, the production lines, the machine tools, their parameter settings and operating disciplines. If a test fails, it is not enough to mend the faulty product. It is not enough just to throw it away, or even to reject the whole batch of products in which a defective one is found. The first principle is that the whole production line must be re-examined, inspected, adjusted or even closed until the root cause of the defect has been found and eliminated. [4]
65 Hoare on the Value of Testing The real value of tests is not that they detect bugs in the code, but that they detect inadequacy in the methods, concentration and skills of those who design and produce the code. Programmers who consistently fail to meet their testing schedules are quickly isolated, and assigned to less intellectually demanding tasks. The most reliable code is produced by teams of programmers who have survived the rigours of testing and delivery to deadline over a period of ten years or more. By experience, intuition, and a sense of personal responsibility they are well qualified to continue to meet the highest standards of quality and reliability. But don t stop the tests: they are still essential to counteract the distracting effects and the perpetual pressure of close deadlines, even on the most meticulous programmers. [4]
66 Informal Specification Versus Formal Specification
67 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings
68 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings
69 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings Examples of formal languages:
70 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings Examples of formal languages: Logical languages: First order logic, Higher order logic,...
71 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings Examples of formal languages: Logical languages: First order logic, Higher order logic,... Picture languages: UML,...
72 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings Examples of formal languages: Logical languages: First order logic, Higher order logic,... Picture languages: UML,... What makes a formal language formal? Precise definition of what each language construct means.
73 Informal Specification Versus Formal Specification Informal specification: natural language, or drawings Formal specification: formal language, or language of drawings Examples of formal languages: Logical languages: First order logic, Higher order logic,... Picture languages: UML,... What makes a formal language formal? Precise definition of what each language construct means. Expressive power versus tool support: more expressive = more difficult to check or reason with.
74 Importance of Declarative Thinking (1) declarative thinking thinking in terms of states of affairs. Key question: what is the case? operational thinking thinking in terms of actions, changes in computer memory, etc. Key question: what happens?
75 Importance of Declarative Thinking (2)
76 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions.
77 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions. Learning (predicate) logic is one way of learning how to think declaratively. Learning functional programming is another. Learning Alloy is yet another.
78 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions. Learning (predicate) logic is one way of learning how to think declaratively. Learning functional programming is another. Learning Alloy is yet another. How does declarative thinking help to become better at testing?
79 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions. Learning (predicate) logic is one way of learning how to think declaratively. Learning functional programming is another. Learning Alloy is yet another. How does declarative thinking help to become better at testing? Java programmers are often very poor at declarative thinking. Haskell programmers are often very good at it. Why?
80 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions. Learning (predicate) logic is one way of learning how to think declaratively. Learning functional programming is another. Learning Alloy is yet another. How does declarative thinking help to become better at testing? Java programmers are often very poor at declarative thinking. Haskell programmers are often very good at it. Why? The main purpose of this course is to teach you how to think declaratively.
81 Importance of Declarative Thinking (2) States of affairs are conceptually simpler than actions. Learning (predicate) logic is one way of learning how to think declaratively. Learning functional programming is another. Learning Alloy is yet another. How does declarative thinking help to become better at testing? Java programmers are often very poor at declarative thinking. Haskell programmers are often very good at it. Why? The main purpose of this course is to teach you how to think declaratively. Declarative thinkers are the best testers.
82 Declarative Specifications: Assertions
83 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming).
84 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming).
85 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming). How do we express assertions about programs written in programming language X?
86 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming). How do we express assertions about programs written in programming language X? In a formal language: propositional logic, predicate logic, Boolean expressions of programming language X,...
87 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming). How do we express assertions about programs written in programming language X? In a formal language: propositional logic, predicate logic, Boolean expressions of programming language X,... Assertions are statements that are true or false. Each instantiation of the variables in an assertion gives us a test.
88 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming). How do we express assertions about programs written in programming language X? In a formal language: propositional logic, predicate logic, Boolean expressions of programming language X,... Assertions are statements that are true or false. Each instantiation of the variables in an assertion gives us a test. Take the statement x < f(x), where x ranges over integers, and f is some procedure you want to test.
89 Declarative Specifications: Assertions Assertions are statements about what has to be true at some state of a program (imperative programming). Assertions are statements about what has to be true about the inputs or outputs of functions (functional programming). How do we express assertions about programs written in programming language X? In a formal language: propositional logic, predicate logic, Boolean expressions of programming language X,... Assertions are statements that are true or false. Each instantiation of the variables in an assertion gives us a test. Take the statement x < f(x), where x ranges over integers, and f is some procedure you want to test. If you take a value for x, this gives a test.
90 Example: What is a Sudoku Problem?
91 Example: What is a Sudoku Problem?
92 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is.
93 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints:
94 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints: Every row should contain each number in {1,..., 9}
95 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints: Every row should contain each number in {1,..., 9} Every column should contain each number in {1,..., 9}
96 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints: Every row should contain each number in {1,..., 9} Every column should contain each number in {1,..., 9} Every subgrid [i, j] with i, j ranging over 1..3, 4..6 and 7..9 should contain each number in {1,..., 9}.
97 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints: Every row should contain each number in {1,..., 9} Every column should contain each number in {1,..., 9} Every subgrid [i, j] with i, j ranging over 1..3, 4..6 and 7..9 should contain each number in {1,..., 9}. A sudoku problem is a partial sudoku matrix (a list of values in the matrix).
98 If declarative specification is to be taken seriously, all there is to solving sudokus is specifying what a sudoku problem is. A sudoku is a 9 9 matrix of numbers in {1,..., 9} satisfying a number of constraints: Every row should contain each number in {1,..., 9} Every column should contain each number in {1,..., 9} Every subgrid [i, j] with i, j ranging over 1..3, 4..6 and 7..9 should contain each number in {1,..., 9}. A sudoku problem is a partial sudoku matrix (a list of values in the matrix). A solution to a sudoku problem is a complete extension of the problem, satisfying the sudoku constraints.
99 Example Problem, With Solution
100 Sudoku Constraints: Injectivity
101 Sudoku Constraints: Injectivity To express the sudoku constraints, we have to be able to express the property that a function is injective (or: one-to-one, or: an injection).
102 Sudoku Constraints: Injectivity To express the sudoku constraints, we have to be able to express the property that a function is injective (or: one-to-one, or: an injection). A function f : X Y is an injection if it preserves distinctions: if x 1 x 2 then f(x 1 ) f(x 2 ).
103 Sudoku Constraints: Injectivity To express the sudoku constraints, we have to be able to express the property that a function is injective (or: one-to-one, or: an injection). A function f : X Y is an injection if it preserves distinctions: if x 1 x 2 then f(x 1 ) f(x 2 ). Equivalently: a function f : X Y is injective if f(x 1 ) = f(x 2 ) implies that x 1 = x 2.
104 Sudoku Constraints as Injectivity Requirements
105 Sudoku Constraints as Injectivity Requirements Represent a sudoku as a function f[i, j].
106 Sudoku Constraints as Injectivity Requirements Represent a sudoku as a function f[i, j]. Requirements:
107 Sudoku Constraints as Injectivity Requirements Represent a sudoku as a function f[i, j]. Requirements: The members of each row should be all different. I.e., for every i, the function j f[i, j] should be injective (one to one). I.e., the list of values [f [i,j] j <- [1..9] ] should not have duplicates.
108 Sudoku Constraints as Injectivity Requirements Represent a sudoku as a function f[i, j]. Requirements: The members of each row should be all different. I.e., for every i, the function j f[i, j] should be injective (one to one). I.e., the list of values [f [i,j] j <- [1..9] ] should not have duplicates. The members of every column should be all different. I.e., for every j, the function i f[i, j] should be injective (one to one). I.e., the list of values [f [i,j] i <- [1..9] ] should not have duplicates.
109 Sudoku Constraints as Injectivity Requirements Represent a sudoku as a function f[i, j]. Requirements: The members of each row should be all different. I.e., for every i, the function j f[i, j] should be injective (one to one). I.e., the list of values [f [i,j] j <- [1..9] ] should not have duplicates. The members of every column should be all different. I.e., for every j, the function i f[i, j] should be injective (one to one). I.e., the list of values [f [i,j] i <- [1..9] ] should not have duplicates.
110 The members of every subgrid [i, j] with i, j ranging over 1..3, 4..6 and 7..9 should be all different. I.e., the list of values [f [i,j] i <- [1..3], j <- [1..3] ] should not have duplicates, and similarly for the other subgrids.
111 In Haskell... type Row = Int type Column = Int type Value = Int type Grid = [[Value]] positions, values :: [Int] positions = [1..9] values = [1..9] blocks :: [[Int]] blocks = [[1..3],[4..6],[7..9]]
112 Showing Sudoku Stuff Use 0 for a blank slot, so show 0 as a blank. showdgt :: Value -> String showdgt 0 = " " showdgt d = show d
113 showrow :: [Value] -> IO() showrow [a1,a2,a3,a4,a5,a6,a7,a8,a9] = do putchar ; putchar putstr (showdgt a1) ; putchar putstr (showdgt a2) ; putchar putstr (showdgt a3) ; putchar putchar ; putchar putstr (showdgt a4) ; putchar putstr (showdgt a5) ; putchar putstr (showdgt a6) ; putchar putchar ; putchar putstr (showdgt a7) ; putchar putstr (showdgt a8) ; putchar putstr (showdgt a9) ; putchar putchar ; putchar \n
114 showgrid :: Grid -> IO() showgrid [as,bs,cs,ds,es,fs,gs,hs,is] = do putstrln (" ") showrow as; showrow bs; showrow cs putstrln (" ") showrow ds; showrow es; showrow fs putstrln (" ") showrow gs; showrow hs; showrow is putstrln (" ")
115 Sudoku Type Define a sudoku as a function from positions to values type Sudoku = (Row,Column) -> Value Useful conversions: sud2grid :: Sudoku -> Grid sud2grid s = [ [ s (r,c) c <- [1..9] ] r <- [1..9] ] grid2sud :: Grid -> Sudoku grid2sud gr = \ (r,c) -> pos gr (r,c) where pos :: [[a]] -> (Row,Column) -> a pos gr (r,c) = (gr!! (r-1))!! (c-1)
116 Showing a sudoku Show a sudoku by displaying its grid: showsudoku :: Sudoku -> IO() showsudoku = showgrid. sud2grid
117 Picking the block of a position bl :: Int -> [Int] bl x = concat $ filter (elem x) blocks Picking the subgrid of a position in a sudoku. subgrid :: Sudoku -> (Row,Column) -> [Value] subgrid s (r,c) = [ s (r,c ) r <- bl r, c <- bl c ]
118 Free Values Free values are available values at open slot positions. freeinseq :: [Value] -> [Value] freeinseq seq = values \\ seq freeinrow :: Sudoku -> Row -> [Value] freeinrow s r = freeinseq [ s (r,i) i <- positions ] freeincolumn :: Sudoku -> Column -> [Value] freeincolumn s c = freeinseq [ s (i,c) i <- positions ] freeinsubgrid :: Sudoku -> (Row,Column) -> [Value] freeinsubgrid s (r,c) = freeinseq (subgrid s (r,c))
119 The key notion The available values at a position. freeatpos :: Sudoku -> (Row,Column) -> [Value] freeatpos s (r,c) = (freeinrow s r) intersect (freeincolumn s c) intersect (freeinsubgrid s (r,c))
120 Injectivity A list of values is injective if each value occurs only once in the list: injective :: Eq a => [a] -> Bool injective xs = nub xs == xs
121 Injectivity Check for Rows, Columns, Blocks Check (the non-zero values on) the rows, colums and subgrids for injectivity. rowinjective :: Sudoku -> Row -> Bool rowinjective s r = injective vs where vs = filter (/= 0) [ s (r,i) i <- positions ] colinjective :: Sudoku -> Column -> Bool colinjective s c = injective vs where vs = filter (/= 0) [ s (i,c) i <- positions ] subgridinjective :: Sudoku -> (Row,Column) -> Bool subgridinjective s (r,c) = injective vs where vs = filter (/= 0) (subgrid s (r,c))
122 Consistency Check consistent :: Sudoku -> Bool consistent s = and $ [ rowinjective s r r <- positions ] ++ [ colinjective s c c <- positions ] ++ [ subgridinjective s (r,c) r <- [1,4,7], c <- [1,4,7]]
123 Sudoku Extension Extend a sudoku by filling in a value in a new position extend :: Sudoku -> (Row,Column,Value) -> Sudoku extend s (r,c,v) (i,j) (i,j) == (r,c) = v otherwise = s (i,j)
124 The Solution Search Tree A sudoku constraint is a list of possible values for a particular position. type Constraint = (Row,Column,[Value]) Nodes in the search tree are pairs consisting of a sudoku and the list of all empty positions in it, together with possible values for those positions, according to the constraints imposed by the sudoku. type Node = (Sudoku,[Constraint]) shownode :: Node -> IO() shownode = showsudoku. fst
125 Solution A sudoku is solved if there are no more empty slots. solved :: Node -> Bool solved = null. snd
126 Successors in the Search Tree The successors of a node are the nodes where the sudoku gets extended at the next empty slot position on the list, using the values listed in the constraint for that position. extendnode :: Node -> Constraint -> [Node] extendnode (s,constraints) (r,c,vs) = [(extend s (r,c,v), sortby length3rd $ prune (r,c,v) constraints) v <- vs ] prune removes the new value v from the relevant constraints, given that v now occupies position (r, c). The definition of prune is given below.
127 Put constraints that are easiest to solve first length3rd :: (a,b,[c]) -> (a,b,[c]) -> Ordering length3rd (_,_,zs) (_,_,zs ) = compare (length zs) (length zs )
128 Pruning Prune values that are no longer possible from constraint list, given a new guess (r, c, v) for the value of (r, c). prune :: (Row,Column,Value) -> [Constraint] -> [Constraint] prune _ [] = [] prune (r,c,v) ((x,y,zs):rest) r == x = (x,y,zs\\[v]) : prune (r,c,v) rest c == y = (x,y,zs\\[v]) : prune (r,c,v) rest sameblock (r,c) (x,y) = (x,y,zs\\[v]) : prune (r,c,v) rest otherwise = (x,y,zs) : prune (r,c,v) rest sameblock :: (Row,Column) -> (Row,Column) -> Bool sameblock (r,c) (x,y) = bl r == bl x && bl c == bl y
129 Initialisation Success is indicated by return of a unit node [n]. initnode :: Grid -> [Node] initnode gr = let s = grid2sud gr in if (not. consistent) s then [] else [(s, constraints s)] The open positions of a sudoku are the positions with value 0. openpositions :: Sudoku -> [(Row,Column)] openpositions s = [ (r,c) r <- positions, c <- positions, s (r,c) == 0 ]
130 Sudoku constraints, in a useful order Put the constraints with the shortest lists of possible values first. constraints :: Sudoku -> [Constraint] constraints s = sortby length3rd [(r,c, freeatpos s (r,c)) (r,c) <- openpositions s ]
131 Depth First Search The depth first search algorithm is completely standard. The goal property is used to end the search. search :: (node -> [node]) -> (node -> Bool) -> [node] -> [node] search succ goal [] = [] search succ goal (x:xs) goal x = x : search succ goal xs otherwise = search succ goal ((succ x) ++ xs)
132 Pursuing the Search solvens :: [Node] -> [Node] solvens = search succnode solved succnode :: Node -> [Node] succnode (s,[]) = [] succnode (s,p:ps) = extendnode (s,ps) p
133 Solving and showing the results This uses some monad operators: fmap and sequence. solveandshow :: Grid -> IO[()] solveandshow gr = solveshowns (initnode gr) solveshowns :: [Node] -> IO[()] solveshowns ns = sequence $ fmap shownode (solvens ns)
134 Examples example1 :: Grid example1 = [[5,3,0,0,7,0,0,0,0], [6,0,0,1,9,5,0,0,0], [0,9,8,0,0,0,0,6,0], [8,0,0,0,6,0,0,0,3], [4,0,0,8,0,3,0,0,1], [7,0,0,0,2,0,0,0,6], [0,6,0,0,0,0,2,8,0], [0,0,0,4,1,9,0,0,5], [0,0,0,0,8,0,0,7,9]]
135 example2 :: Grid example2 = [[0,3,0,0,7,0,0,0,0], [6,0,0,1,9,5,0,0,0], [0,9,8,0,0,0,0,6,0], [8,0,0,0,6,0,0,0,3], [4,0,0,8,0,3,0,0,1], [7,0,0,0,2,0,0,0,6], [0,6,0,0,0,0,2,8,0], [0,0,0,4,1,9,0,0,5], [0,0,0,0,8,0,0,7,9]]
136 example3 :: Grid example3 = [[1,0,0,0,3,0,5,0,4], [0,0,0,0,0,0,0,0,3], [0,0,2,0,0,5,0,9,8], [0,0,9,0,0,0,0,3,0], [2,0,0,0,0,0,0,0,7], [8,0,3,0,9,1,0,6,0], [0,5,1,4,7,0,0,0,0], [0,0,0,3,0,0,0,0,0], [0,4,0,0,0,9,7,0,0]]
137 example4 :: Grid example4 = [[1,2,3,4,5,6,7,8,9], [2,0,0,0,0,0,0,0,0], [3,0,0,0,0,0,0,0,0], [4,0,0,0,0,0,0,0,0], [5,0,0,0,0,0,0,0,0], [6,0,0,0,0,0,0,0,0], [7,0,0,0,0,0,0,0,0], [8,0,0,0,0,0,0,0,0], [9,0,0,0,0,0,0,0,0]]
138 example5 :: Grid example5 = [[1,0,0,0,0,0,0,0,0], [0,2,0,0,0,0,0,0,0], [0,0,3,0,0,0,0,0,0], [0,0,0,4,0,0,0,0,0], [0,0,0,0,5,0,0,0,0], [0,0,0,0,0,6,0,0,0], [0,0,0,0,0,0,7,0,0], [0,0,0,0,0,0,0,8,0], [0,0,0,0,0,0,0,0,9]]
139 Next Week More about algorithm specification, and writing assertive (self-testing) versions of algorithms.
140 References [1] Kent Beck. Test Driven Development By Example. Addison-Wesley Longman, Boston, MA, [2] Richard Bird. Pearls of Functional Algorithm Design. Cambridge University Press, [3] C.A.R. Hoare. An axiomatic basis for computer programming. Communications of the ACM, 12(10): , 583, [4] C.A.R. Hoare. How did software get so reliable without proof? In FME 96: Proceedings of the Third International Symposium of Formal Methods Europe on Industrial Benefit and Advances in Formal Methods, pages 1 17, London, UK, Springer-Verlag. Keynote Address. [5] F. Rabhi and G. Lapalme. Algorithms: a Functional Programming Approach. Addison-Wesley, 1999.
Let s Talk About Logic
Let s Talk About Logic Jan van Eijck CWI & ILLC, Amsterdam Masterclass Logica, 2 Maart 2017 Abstract This lecture shows how to talk about logic in computer science. To keep things simple, we will focus
More informationPROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions
PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product
More informationSpecifications and Assertions
Specifications and Assertions Jan van Eijck Master SE, 29 September 2010 Abstract As a start, we give further examples of Alloy specifications. Next we turn to specification of imperative programs. Assertions
More informationShell CSCE 314 TAMU. Functions continued
1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in
More informationLogical Methods in... using Haskell Getting Started
Logical Methods in... using Haskell Getting Started Jan van Eijck May 4, 2005 Abstract The purpose of this course is to teach a bit of functional programming and logic, and to connect logical reasoning
More informationFormal Methods. CITS5501 Software Testing and Quality Assurance
Formal Methods CITS5501 Software Testing and Quality Assurance Pressman, R. Software Engineering: A Practitioner s Approach. Chapter 28. McGraw-Hill, 2005 The Science of Programming, David Gries, 1981
More informationProgramming with Math and Logic
.. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing
More informationLecture Notes on Contracts
Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,
More informationA Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16
A Sudoku Solver (1A) Richard Bird Implementation Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License,
More informationSOFTWARE ENGINEERING DESIGN I
2 SOFTWARE ENGINEERING DESIGN I 3. Schemas and Theories The aim of this course is to learn how to write formal specifications of computer systems, using classical logic. The key descriptional technique
More informationStandard prelude. Appendix A. A.1 Classes
Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from
More information1 The smallest free number
1 The smallest free number Introduction Consider the problem of computing the smallest natural number not in a given finite set X of natural numbers. The problem is a simplification of a common programming
More informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationTesting. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]
Testing Advanced functional programming - Lecture 2 Wouter Swierstra and Alejandro Serrano 1 Program Correctness 2 Testing and correctness When is a program correct? 3 Testing and correctness When is a
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationHoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré
Hoare Logic COMP2600 Formal Methods for Software Engineering Rajeev Goré Australian National University Semester 2, 2016 (Slides courtesy of Ranald Clouston) COMP 2600 Hoare Logic 1 Australian Capital
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
More informationPurely Functional Algorithm Specification. Version April 26, 2012
Purely Functional Algorithm Specification ESSLLI 2012 Lecture Notes Version April 26, 2012 Jan van Eijck 0-2 Preface This course develops a perspective on algorithm specification in terms of purely functional
More informationIntroduction to Programming: Lecture 6
Introduction to Programming: Lecture 6 K Narayan Kumar Chennai Mathematical Institute http://www.cmi.ac.in/~kumar 28 August 2012 Example: initial segments Write a Haskell function initsegs which returns
More informationA Sudoku Solver Pruning (3A)
A Sudoku Solver Pruning (3A) Richard Bird Implementation Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation
More informationLecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationn Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition
Programming as a contract Assertions, pre/postconditions and invariants Assertions: Section 4.2 in Savitch (p. 239) Loop invariants: Section 4.5 in Rosen Specifying what each method does q Specify it in
More informationLecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationIn Our Last Exciting Episode
In Our Last Exciting Episode #1 Lessons From Model Checking To find bugs, we need specifications What are some good specifications? To convert a program into a model, we need predicates/invariants and
More informationMSO Lecture Design by Contract"
1 MSO Lecture Design by Contract" Wouter Swierstra (adapted by HP, AL) October 8, 2018 2 MSO SO FAR Recap Abstract Classes UP & Requirements Analysis & UML OO & GRASP principles Design Patterns (Facade,
More informationInformatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lecture 11 Data Representation Don Sannella University of Edinburgh Part I Complexity t = n vs t = n 2 10.4 9.6 8.8 8 7.2 6.4 5.6 4.8 4 3.2 2.4 1.6 0.8 0 0.8 1.6 2.4
More informationHow invariants help writing loops Author: Sander Kooijmans Document version: 1.0
How invariants help writing loops Author: Sander Kooijmans Document version: 1.0 Why this document? Did you ever feel frustrated because of a nasty bug in your code? Did you spend hours looking at the
More informationStatic Contract Checking for Haskell
Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Simon Peyton Jones Microsoft Research Cambridge Joint work with Koen Claessen Chalmers University of Technology
More informationAdvances in Programming Languages
T O Y H Advances in Programming Languages APL8: Multiparameter Type Classes, Constructor Classes Ian Stark School of Informatics The University of Edinburgh Thursday 4 February Semester 2 Week 4 E H U
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,
More informationLecture 5 - Axiomatic semantics
Program Verification March 2014 Lecture 5 - Axiomatic semantics Lecturer: Noam Rinetzky Scribes by: Nir Hemed 1.1 Axiomatic semantics The development of the theory is contributed to Robert Floyd, C.A.R
More informationAn Annotated Language
Hoare Logic An Annotated Language State and Semantics Expressions are interpreted as functions from states to the corresponding domain of interpretation Operators have the obvious interpretation Free of
More informationLECTURE 16. Functional Programming
LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative
More informationIt is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
More informationLecture Notes on Linear Search
Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 28, 2014 1 Introduction One of the fundamental and recurring problems in computer science is
More informationInformatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lecture 12 Data Abstraction Don Sannella University of Edinburgh Part I Sets as lists without abstraction We will look again at our four ways of implementing sets.
More informationTypes and Static Type Checking (Introducing Micro-Haskell)
Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 13 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 16 October, 2012 1 / 21 1 Types 2 3 4 2 / 21 Thus far
More informationApplicative, traversable, foldable
Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over
More informationReasoning About Imperative Programs. COS 441 Slides 10
Reasoning About Imperative Programs COS 441 Slides 10 The last few weeks Agenda reasoning about functional programming It s very simple and very uniform: substitution of equal expressions for equal expressions
More informationTesting, Debugging, and Verification
Testing, Debugging, and Verification Formal Specification, Part II Srinivas Pinisetty 23 November 2017 Introduction Today: Introduction to Dafny: An imperative language with integrated support for formal
More informationCS 11 Haskell track: lecture 5. This week: State monads
CS 11 Haskell track: lecture 5 This week: State monads Reference "Monads for the Working Haskell Programmer" http://www.engr.mun.ca/~theo/misc/ haskell_and_monads.htm Good explanation of state monads Today's
More informationFunctional Programming TDA 452, DIT 142
Chalmers Göteborgs Universitet 2016-04-07 Examiner: David Sands dave@chalmers.se. Answering questions on the day of the exam (at approx 15.00): Gregoire Detrez (tel: 073 55 69 550) and at other times by
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More informationCSCE 314 Programming Languages Functors, Applicatives, and Monads
CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::
More informationCITS5501 Software Testing and Quality Assurance Formal methods
CITS5501 Software Testing and Quality Assurance Formal methods Unit coordinator: Arran Stewart May 1, 2018 1 / 49 Sources Pressman, R., Software Engineering: A Practitioner s Approach, McGraw-Hill, 2005
More information(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.
EXAM FUNCTIONAL PROGRAMMING Tuesday the 1st of October 2016, 08.30 h. - 10.30 h. Name: Student number: Before you begin: Do not forget to write down your name and student number above. If necessary, explain
More informationCS 209 Functional Programming
CS 209 Functional Programming Lecture 03 - Intro to Monads Dr. Greg Lavender Department of Computer Science Stanford University "The most important thing in a programming language is the name. A language
More informationLargest Online Community of VU Students
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions
More informationApplicative, traversable, foldable
Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.
More informationGenerative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31
Generative and accumulative recursion Readings: Sections 25, 26, 27, 30, 31 Some subsections not explicitly covered in lecture Section 27.2 technique applied to strings CS 135 Fall 2017 11: Generative
More informationInformatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lectures 15 and 16 IO and Monads Don Sannella University of Edinburgh Part I The Mind-Body Problem The Mind-Body Problem Part II Commands Print a character putchar
More informationMathematical Induction
Mathematical Induction Victor Adamchik Fall of 2005 Lecture 3 (out of three) Plan 1. Recursive Definitions 2. Recursively Defined Sets 3. Program Correctness Recursive Definitions Sometimes it is easier
More informationDiscrete Mathematics Lecture 4. Harper Langston New York University
Discrete Mathematics Lecture 4 Harper Langston New York University Sequences Sequence is a set of (usually infinite number of) ordered elements: a 1, a 2,, a n, Each individual element a k is called a
More informationIntroduction to Haskell
Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming
More informationEDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.
EDAF40 2nd June 2017 14:00-19:00 WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read. DO NOT WRITE WITH OTHER COLOUR THAN BLACK - coloured text
More informationCSci 450: Org. of Programming Languages Overloading and Type Classes
CSci 450: Org. of Programming Languages Overloading and Type Classes H. Conrad Cunningham 27 October 2017 (after class) Contents 9 Overloading and Type Classes 1 9.1 Chapter Introduction.........................
More informationFUNCTIONAL PEARLS The countdown problem
To appear in the Journal of Functional Programming 1 FUNCTIONAL PEARLS The countdown problem GRAHAM HUTTON School of Computer Science and IT University of Nottingham, Nottingham, UK www.cs.nott.ac.uk/
More informationPROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions
PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}
More informationf() ! " Run a higher-priority task? ! " Yield the processor to another task because our ! " Use the processor for some other activity while
CS 410/510: Advanced Programming Continuing a Computation: Continuations Mark P Jones Portland State University 1 Standard nested function call pattern 2 Continuing a Computation: What might we want to
More informationCS 410/510: Advanced Programming
CS 410/510: Advanced Programming Continuations Mark P Jones Portland State University 1 Continuing a Computation: f() g() h() Standard nested function call pattern 2 Continuing a Computation: f() g() h()
More informationRepetition Through Recursion
Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something
More informationOverloading, Type Classes, and Algebraic Datatypes
Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827
More information12/30/2013 S. NALINI,AP/CSE
12/30/2013 S. NALINI,AP/CSE 1 UNIT I ITERATIVE AND RECURSIVE ALGORITHMS Iterative Algorithms: Measures of Progress and Loop Invariants-Paradigm Shift: Sequence of Actions versus Sequence of Assertions-
More informationInformatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh
Informatics 1 Functional Programming 19 Tuesday 23 November 2010 IO and Monads Philip Wadler University of Edinburgh The 2010 Informatics 1 Competition Sponsored by Galois (galois.com) List everyone who
More informationRecursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions
Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin
More informationCOMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th
COMS 1003 Fall 2005 Introduction to Computer Programming in C Bits, Boolean Logic & Discrete Math September 13 th Hello World! Logistics See the website: http://www.cs.columbia.edu/~locasto/ Course Web
More informationA Partial Correctness Proof for Programs with Decided Specifications
Applied Mathematics & Information Sciences 1(2)(2007), 195-202 An International Journal c 2007 Dixie W Publishing Corporation, U. S. A. A Partial Correctness Proof for Programs with Decided Specifications
More informationINTRODUCTION TO HASKELL
INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be
More informationLimitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and
Computer Language Theory Chapter 4: Decidability 1 Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and
More informationINTRODUCTION TO FUNCTIONAL PROGRAMMING
INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham adapted by Gordon Uszkay 1 What is Functional Programming? Opinions differ, and it is difficult to give a precise definition,
More informationLogic, Languages and Programming
Logic, Languages and Programming Jan van Eijck CWI & ILLC, Amsterdam Guest Lecture Minor Programming January 20, 2014 Abstract This lecture will combine the topics of the title in various ways. First I
More informationPROGRAMMING IN HASKELL. Chapter 2 - First Steps
PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited
More informationLecture 4 Searching Arrays
Lecture 4 Searching Arrays 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning One of the fundamental and recurring problems in computer science is to find elements in collections,
More informationInduction and Semantics in Dafny
15-414 Lecture 11 1 Instructor: Matt Fredrikson Induction and Semantics in Dafny TA: Ryan Wagner Encoding the syntax of Imp Recall the abstract syntax of Imp: a AExp ::= n Z x Var a 1 + a 2 b BExp ::=
More informationPROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming
PROGRAMMING IN HASKELL Chapter 10 - Interactive Programming 0 Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start and give all their
More informationIntroduction to Homotopy Type Theory
Introduction to Homotopy Type Theory Lecture notes for a course at EWSCS 2017 Thorsten Altenkirch March 5, 2017 1 What is this course about? To explain what Homotopy Type Theory is, I will first talk about
More informationTentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya
Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya 04-05-2001, 09.00-12.00, Educatorium Gamma This test consists of two parts. For the first part, which is
More informationHaskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules
Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]
More informationCS Lecture 19: Loop invariants
CS 1110 Lecture 19: Loop invariants Announcements Prelim 2 conflicts Today (April 2) is two weeks before the prelim, and the deadline for submitting prelim conflicts. Instructor travel This week and the
More informationHaske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell
Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program
More informationHaskell Overview II (2A) Young Won Lim 8/9/16
(2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationForward Assignment; Strongest Postconditions
3/1 new version Forward Assignment; Strongest Postconditions CS 536: Science of Programming, Spring 2018 A. Why? At times, a forward version of the assignment rule is more appropriate than the backward
More informationIntroduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD
Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD In this session, we will write another algorithm to solve a mathematical problem. If you
More information6. Hoare Logic and Weakest Preconditions
6. Hoare Logic and Weakest Preconditions Program Verification ETH Zurich, Spring Semester 07 Alexander J. Summers 30 Program Correctness There are many notions of correctness properties for a given program
More informationTypes of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion
Types of recursion Readings: none. In this module: learn to use accumulative recursion learn to recognize generative recursion CS 135 Fall 2018 07: Types of recursion 1 Structural vs. general recursion
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And
More informationaxiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.
CS 6110 S18 Lecture 18 Denotational Semantics 1 What is Denotational Semantics? So far we have looked at operational semantics involving rules for state transitions, definitional semantics involving translations
More informationTypes and Static Type Checking (Introducing Micro-Haskell)
Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 14 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 17 October 2017 1 / 21 1 Types 2 3 4 2 / 21 So far in
More informationPractical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.
Practical Practical An introduction to functional programming July 21, 2011 Contents Practical Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning
More informationThe Substitution Model
The Substitution Model Prof. Clarkson Fall 2017 Today s music: Substitute by The Who Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationIntro to Haskell Notes: Part 5
Intro to Haskell Notes: Part 5 Adrian Brasoveanu October 5, 2013 Contents 1 Curried functions and related issues 1 1.1 Curried functions......................................... 1 1.2 Partially applied
More informationSet Haskell Exercises
Set Haskell Exercises Young W. Lim 2018-11-20 Tue Young W. Lim Set Haskell Exercises 2018-11-20 Tue 1 / 71 Outline 1 Based on 2 Pardoxes and Haskell type system Using STAL.hs Paradox Types and Type Classes
More informationCS103 Spring 2018 Mathematical Vocabulary
CS103 Spring 2018 Mathematical Vocabulary You keep using that word. I do not think it means what you think it means. - Inigo Montoya, from The Princess Bride Consider the humble while loop in most programming
More informationSemantics of programming languages
Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming
More informationCOMPUTER SCIENCE TRIPOS
CST.2000.1.1 COMPUTER SCIENCE TRIPOS Part IA Monday 5 June 2000 1.30 to 4.30 Paper 1 Answer two questions from Section A, and one question from each of Sections B, C, D and E. Submit the answers in six
More informationChapter 3 (part 3) Describing Syntax and Semantics
Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings
More informationSolutions to the Second Midterm Exam
CS/Math 240: Intro to Discrete Math 3/27/2011 Instructor: Dieter van Melkebeek Solutions to the Second Midterm Exam Problem 1 This question deals with the following implementation of binary search. Function
More informationHaskell Refresher Informatics 2D
Haskell Purely functional! : Everything is a function Haskell Refresher Informatics 2D Kobby. K.A. Nuamah 30 January 2015 Main topics: Recursion Currying Higher-order functions List processing functions
More information