Program Calculus Calculational Programming

Size: px
Start display at page:

Download "Program Calculus Calculational Programming"

Transcription

1 Program Calculus Calculational Programming National Institute of Informatics June 21 / June 28 / July 5, 2010 Program Calculus Calculational Programming

2 What we will learn? Discussing the mathematical structures in programs, and Program Calculus Calculational Programming

3 What we will learn? Discussing the mathematical structures in programs, and explaining how mathematical reasoning plays an important role in designing efficient and correct algorithms (programs). Program Calculus Calculational Programming

4 What we will learn? Discussing the mathematical structures in programs, and explaining how mathematical reasoning plays an important role in designing efficient and correct algorithms (programs). A new programming style: calculational programming Program Calculus Calculational Programming

5 Calculation is widely used in solving our daily problems, but its importance in programming has not been fully recognized. Program Calculus Calculational Programming

6 Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Program Calculus Calculational Programming

7 Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. The kindergarten approach: plain simple enumeration! Crane 0, Tortoise 5... No. Crane 1, Tortoise 4... No. Crane 2, Tortoise 3... No. Crane 3, Tortoise 2... Yes! Crane 4, Tortoise 1... No. Crane 5, Tortoise 0... No. Program Calculus Calculational Programming

8 Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Elementary school: let s do some reasoning... If all 5 animals were cranes, there ought to be 5 2 = 10 legs. However, there are in fact 14 legs. The extra 4 legs must belong to some tortoises. There must be (14 10)/2 = 2 tortoises. So there must be 5 2 = 3 cranes. It generalises to larger numbers of heads and legs. Program Calculus Calculational Programming

9 Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. Junior high school: algebra (theory of equation)! x + y = 5 2x + 4y = 14 It s a general approach applicable to many other problems and perhaps easier. Program Calculus Calculational Programming

10 Tsuru-Kame-Zan An Overview The Tsuru-Kame Problem Some cranes (tsuru) and tortoises (kame) are mixed in a cage. Known is that there are 5 heads and 14 legs. Find out the numbers of cranes and tortoises. rules and theories are useful for solving problems easily and systematically. Problems are declaratively specified. Implementation is hidden. Program Calculus Calculational Programming

11 Can we have a set of useful rules and theories in programming for developing correct and efficient algorithms (programs)? Program Calculus Calculational Programming

12 A Programming Problem Can you develop a correct linear-time program for solving the following problem? Maximum Segment Sum Problem Given a list of numbers, find the maximum of sums of all consecutive sublists. [ 1, 3, 3, 4, 1, 4, 2, 1] = 7 [ 1, 3, 1, 4, 1, 4, 2, 1] = 6 [ 1, 3, 1, 4, 1, 1, 2, 1] = 4 Program Calculus Calculational Programming

13 A Simple Solution An Overview 1 Enumerating all segments (segs); Program Calculus Calculational Programming

14 A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); Program Calculus Calculational Programming

15 A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Program Calculus Calculational Programming

16 A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Program Calculus Calculational Programming

17 A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Exercise How many segments does a list of length n have? Program Calculus Calculational Programming

18 A Simple Solution An Overview 1 Enumerating all segments (segs); 2 Computing sum for each segment(sums); 3 Calculating the maximum of all the sums (max). Exercise How many segments does a list of length n have? Exercise What is the time complexity of this simple solution? Program Calculus Calculational Programming

19 There indeed exists a clever solution! mss=0; s=0; for(i=0;i<n;i++){ s += x[i]; if(s<0) s=0; if(mss<s) mss= s; } x[i] s mss Program Calculus Calculational Programming

20 There is a big gap between the simple and clever solutions! Program Calculus Calculational Programming

21 There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? Program Calculus Calculational Programming

22 There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? Program Calculus Calculational Programming

23 There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? How to apply the rules and theorems to do so? Program Calculus Calculational Programming

24 There is a big gap between the simple and clever solutions! Can we calculate the clever solution from the simple solution? What rules and theorems are necessary to do so? How to apply the rules and theorems to do so? Program Calculus Calculational Programming

25 Transformational Programming One starts by writing clean and correct programs, and then use program transformation techniques to transform them step-by-step to more efficient equivalents. Specificaiton: Clean and Correct programs Folding/Unfolding Program Transformation Efficient Programs Program Calculus Calculational Programming

26 Program Calculation Program calculation is a kind of program transformation based on Constructive Algorithmics, a framework for developing laws/rules/theories for manipulating programs. Program Calculus Calculational Programming

27 Program Calculation Program calculation is a kind of program transformation based on Constructive Algorithmics, a framework for developing laws/rules/theories for manipulating programs. Specificaiton: Clean and Correct programs Folding-free Program Transformation Efficient Programs Program Calculus Calculational Programming

28 References Richard Bird, Lecture Notes on Constructive Functional Programming, Technical Monograph PRG-69, Oxford University, Anne Kaldewaij, Programming: The Derivation of Algorithms, Prentice Hall, Richard Bird and Oege de Moor, The Algebra of Programming, Prentice-Hall, Roland Backhouse, Program Construction: Calculating Implementation from Specification, Wiley, Program Calculus Calculational Programming

29 National Institute of Informatics June 21 / June 28 / July 5, 2010

30 A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible.

31 A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible. An implementation describes how task is to perform, expresses an algorithm (an execution), should be efficiently done within the time and space available.

32 A specification describes what task an algorithm is to perform, expresses the programmers intent, should be as clear as possible. An implementation describes how task is to perform, expresses an algorithm (an execution), should be efficiently done within the time and space available. The link is that the implementation should be proved to satisfy the specification.

33 How to write a specification? By predicates: describing intended relationship between input and output of an algorithm.

34 How to write a specification? By predicates: describing intended relationship between input and output of an algorithm. By functions: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient.

35 Specifying Algorithms by Predicates (1/3) Specification: describing intended relationship between input and output of an algorithm.

36 Specifying Algorithms by Predicates (1/3) Specification: describing intended relationship between input and output of an algorithm. Example: increase The specification increase :: Int Int increase x > square x says that the result of increase should be strictly greater than the square of its input, where square x = x x.

37 Specifying Algorithms by Predicates (2/3) In this case, an Implementation is first given and then proved to satisfy the specification.

38 Specifying Algorithms by Predicates (2/3) In this case, an Implementation is first given and then proved to satisfy the specification. Example: increase (continue) One implementation is increase x = square x + 1 which can be proved by the following simple calculation. increase x = { definition of increase } square x + 1 > { arithmetic property } square x

39 Specifying Algorithms by Predicates (3/3) Exercise S1 Give another implementation of increase and prove that your implementation meets its specification.

40 Specifying Algorithms by Functions (1/3) Specification: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient.

41 Specifying Algorithms by Functions (1/3) Specification: describing straightforward functional mapping from input to output of an algorithm, which is executable but could be terribly inefficient. Example: quad The specification for computing quadruple of a number can be described straightforwardly by quad x = x x x x which is not efficient in the sense that multiplications are used three times.

42 Specifying Algorithms by Functions (2/3) With functional specification, we do not need to invent the implementation; just to improve specification via calculation.

43 Specifying Algorithms by Functions (2/3) With functional specification, we do not need to invent the implementation; just to improve specification via calculation. Example: quad (continue) We derive (develop) an efficient algorithm with only two multiplications by the following calcualtion. quad x = { specification } x x x x = { since x is associative } (x x) (x x) = { definition of square } square x square x = { definition of square } square (aquare x)

44 Specifying Algorithms by Functions (3/3) Exercise S2 Extend the idea in the derivation of efficient quad to develop an efficient algorithm for computing exp defined by exp(x, n) = x n.

45 Advantages of Functional Specification Functional specification is executable.

46 Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition.

47 Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition. Functional specification is suitable for reasoning, when functions used are well-structured with good algebraic properties.

48 Advantages of Functional Specification Functional specification is executable. Functional specification is powerful to express intended mappings directly by functions or through their composition. Functional specification is suitable for reasoning, when functions used are well-structured with good algebraic properties. In this course, we will consider functional specification.

49 Introduction to Bird Meertens Formalisms National Institute of Informatics June 21 / June 28 / July 5, 2010 Introduction to Bird Meertens Formalisms

50 Bird Meentens Formalisms (BMF) Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition BMF is a calculus of functions for people to derive programs from specifications: a range of concepts and notations for defining functions over lists; a set of algebraic laws for manipulating functions. Introduction to Bird Meertens Formalisms

51 A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a = ((1 a 1 + 1) a 2 + 1) a This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. Introduction to Bird Meertens Formalisms

52 A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a = ((1 a 1 + 1) a 2 + 1) a This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Introduction to Bird Meertens Formalisms

53 A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a = ((1 a 1 + 1) a 2 + 1) a This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Can we generalize to, + to? What are the essential constraints for and? Introduction to Bird Meertens Formalisms

54 A Problem An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a = ((1 a 1 + 1) a 2 + 1) a This equation generalizes in the obvious way to n variables a 1, a 2,..., a n, and we will refer to it as Horner e rule. How many are used in each side? Can we generalize to, + to? What are the essential constraints for and? Do you have suitable notation for expressing the Horner s rule concisely? Introduction to Bird Meertens Formalisms

55 Functions An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function f that has source type α and target type β is denoted by f : α β We shall say that f takes arguments in α and returns results in β. Function application is written without brackets; thus f a means f (a). Function application is more binding than any other operation, so f a b means (f a) b. Functions are curried and applications associates to the left, so f a b means (f a) b (sometimes written as f a b. Introduction to Bird Meertens Formalisms

56 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Function composition is denoted by a centralized dot ( ). We have (f g) x = f (g x) Exercise: Show the following equation state that functional compositon is associative. (f ) (g ) = ((f g) ) Introduction to Bird Meertens Formalisms

57 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Binary operators will be denoted by,,, etc. Binary operators can be sectioned. This means that ( ), (a ) and ( a) all denote functions. The definitions are: ( ) a b = a b (a ) b = a b ( b) a = a b Exercise: If has type : α β γ, then what are the types for ( ), (a ) and ( b) for all a in α and b in β? Introduction to Bird Meertens Formalisms

58 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The identity element of : α α α, if it exists, will be denoted by id. Thus, a id = id a = a Exericise: What is the identity element of functional composition? The constant values function K : α β α is defined by the equation K a b = a Introduction to Bird Meertens Formalisms

59 Lists An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Lists are finite sequence of values of the same type. We use the notation [α] to describe the type of lists whose elements have type α. Examples: [1, 2, 1] : [Int] [[1], [1, 2], [1, 2, 1]] : [[Int]] [ ] : [α] Introduction to Bird Meertens Formalisms

60 List Data Constructors Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition [ ] : [α] constructs an empty list. [.] : α [α] maps elements of α into singleton lists. [.] a = [a] The primitive operator on lists is concatenation, denoted by +. [1] ++ [2] ++ [1] = [1, 2, 1] Concatenation is associative: x ++ (y ++ z) = (x ++ y) ++ z Exercise: What is the identity for concatenation? Introduction to Bird Meertens Formalisms

61 Algebraic View of Lists Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition ([α], +, [ ]) is a monoid. ([α], +, [ ]) is a free monoid generated by α under the assignment [.] : α [α]. ([α] +, + ) is a semigroup. Introduction to Bird Meertens Formalisms

62 List Functions: s Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function h defined in the following form is called homomorphism: h [ ] = id h [a] = f a h (x ++ y) = h x h y It defines a map from the monoid ([α], +, [ ]) to the monoid (β, : β β β, id : β). Introduction to Bird Meertens Formalisms

63 List Functions: s Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A function h defined in the following form is called homomorphism: h [ ] = id h [a] = f a h (x ++ y) = h x h y It defines a map from the monoid ([α], +, [ ]) to the monoid (β, : β β β, id : β). Property: h is uniquely determined by f and. Introduction to Bird Meertens Formalisms

64 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition An Example: the function returning the length of a list. # [ ] = 0 # [a] = 1 # (x ++ y) = # x + # y Note that (Int, +, 0) is a monoid. Introduction to Bird Meertens Formalisms

65 Bags and Sets An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A bag is a list in which the order of the elements is ignored. Bags are constructed by adding the rule that + is commutative (as well as associative): x ++ y = y ++ x A set is a bag in which repetitions of elements are ignored. Sets are constructed by adding the rule that + is idempotent (as well as commutative and associative): x ++ x = x Introduction to Bird Meertens Formalisms

66 Map An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The operator (pronounced map) takes a function on lts left and a list on its right. Informally, we have f [a 1, a 2,..., a n ] = [f a 1, f a 2,..., f a n ] Formally, (f ) (or sometimes simply written as f ) is a homomorphism: f [ ] = [ ] f [a] = [f a] f (x ++ y) = (f x) ++ (f y) Map Distributivity: (f g) = (f ) (g ) Introduction to Bird Meertens Formalisms

67 Reduce An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The operator / (pronounced reduce) takes an associative binary operator on lts left and a list on its right. Informally, we have /[a 1, a 2,..., a n ] = a 1 a 2 a n Formally, / is a homomorphism: /[ ] = id { if id exists } /[a] = a /(x ++ y) = ( /x) ( /y) Introduction to Bird Meertens Formalisms

68 Examples: An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition max : [Int] Int max = / where a b = if a b then b else a head : [α] + α head = / where a b = a last : [α] + α last = / where a b = b Introduction to Bird Meertens Formalisms

69 Promotion An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition f and / can be expressed as identities between functions. Empty Rules One-Point Rules Join Rules f K [ ] = K [ ] / K [ ] = id f [ ] = [ ] f / [ ] = id f ++ / = ++ / (f ) / ++ / = /.( /) Introduction to Bird Meertens Formalisms

70 An Example of Calculation Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / f ++ / g = { map promotion } / ++ / f g = { reduce promotion } / ( /) f g = { map distribution } / ( / f g) Introduction to Bird Meertens Formalisms

71 Directed Reductions Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition We introduce two more computation patterns / (pronounced left-to-right reduce) and / (right-to-left reduce) which are closely related to /. Informally, we have / e [a 1, a 2,..., a n ] = ((e a 1 ) ) a n / e [a 1, a 2,..., a n ] = a 1 (a 2 (a n e)) Formally, we can define / e on lists by two equations. / e [ ] = e / e (x ++ [a]) = ( / e x) a Exercise: Give a formal definition for / e. Introduction to Bird Meertens Formalisms

72 Directed Reductions without Seeds Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / [a 1, a 2,..., a n ] = ((a 1 a 2 ) ) a n / [a 1, a 2,..., a n ] = a 1 (a 2 (a n 1 a n )) Properties: ( / ) ([a] ++ ) = / a ( / ) (++ [a]) = / a Introduction to Bird Meertens Formalisms

73 An Example Use of Left-Reduce Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the right-hand side of Horner s rule: (((1 a 1 + 1) a 2 + 1) + 1) a n + 1 This expression can be written using a left-reduce: / 1 [a 1, a 2,..., a n ] where a b = (a b) + 1 Exercise: Give the definition of such that the following holds. / [a 1, a 2,..., a n ] = (((a 1 a 2 +a 2 ) a 3 +a 3 ) +a n 1 ) a n +a n Introduction to Bird Meertens Formalisms

74 Accumulations An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition With each form of directed reduction over lists there corresponds a form of computation called an accumulation. These forms are expressed with the operators / (pronounced left-accumualte) and / (right-accumulate) and are defined informally by / e [a 1, a 2,..., a n ] = [e, e a 1,..., ((e a 1 ) ) a n ] / e [a 1, a 2,..., a n ] = [a 1 (a 2 (a n e)),..., a n e, e] Introduction to Bird Meertens Formalisms

75 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Formally, we can define / e on lists by two equations by / e [ ] = [e] / e ([a] ++ x) = [e] ++ ( / e a x), or / e [ ] = [e] / e (x ++ [a]) = ( / e x) ++ [b a] where b = last( / e x). Introduction to Bird Meertens Formalisms

76 Efficiency in Accumulate Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition / e [a 1, a 2,..., a n ]: can be evaluated with n 1 calculations of. Exercise: Consider computation of first n + 1 factorial numbers: [0!, 1!,..., n!]. How many calculations of are required for the following two programs? 1 / 1 [1, 2,..., n] 2 fact [0, 1, 2,, n] where fact 0 = 1 and fact k = 1 2 k. Introduction to Bird Meertens Formalisms

77 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Relation between Reduce and Accumulate / e = last / e / e = / [e] where x a = x ++ [last x a] Introduction to Bird Meertens Formalisms

78 Segments An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition A list y is a segment of x if there exists u and v such that x = u ++ y ++ v. If u = [], then y is called an initial segment. If v = [], then y is called an final segment. An Example: segs [1, 2, 3] = [[], [1], [1, 2], [2], [1, 2, 3], [2, 3], [3]] Exercise: How many segments for a list [a 1, a 2,..., a n ]? Introduction to Bird Meertens Formalisms

79 inits An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The function inits returns the list of initial segments of a list, in increasing order of a list. inits [a 1, a 2,..., a n ] = [[ ], [a 1 ], [a 1, a 2 ],..., [a 1, a 2,..., a n ]] inits = ( + / [] ) [ ] Introduction to Bird Meertens Formalisms

80 tails An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The function tails returns the list of final segments of a list, in decreasing order of a list. tails [a 1, a 2,..., a n ] = [[a 1, a 2,..., a n ], [a 2,..., a n ],..., [a n ], [ ]] tails = ( + / [] ) [ ] Introduction to Bird Meertens Formalisms

81 segs An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition segs = ++ / tails inits Exercise: Show the result of segs [1, 2]. Introduction to Bird Meertens Formalisms

82 Accumulation Lemma Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition ( / e ) = ( / e ) inits ( / ) = ( / ) inits + The accumulation lemma is used frequently in the derivation of efficient algorithms for problems about segments. On lists of length n, evaluation of the LHS requires O(n) computations involving, while the RHS requires O(n 2 ) computations. Introduction to Bird Meertens Formalisms

83 The Problem: Revisit Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Consider the following simple identity: (a 1 a 2 a 3 ) + (a 2 a 3 ) + a = ((1 a 1 + 1) a 2 + 1) a This equation generalizes in the obvious way to n variables a 1, a 2,..., a 2, and we will refer to it as Horner e rule. Can we generalize to, + to? What are the essential constraints for and? Do you have suitable notation for expressing the Horner s rule concisely? Introduction to Bird Meertens Formalisms

84 Horner s Rule An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The following equation / / tails = / e where e = id a b = (a b) e holds, provided that distributes (backwards) over : for all a, b, and c. (a b) c = (a c) (b c) Introduction to Bird Meertens Formalisms

85 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Exercise: Prove the correctness of the Horner s rule. Hints: Show that is equivalent to Show that satisfies the equations (a b) c = (a c) (b c) ( c) / = / ( c). f = / / tails f [ ] = e f (x ++ [a]) = f x a Introduction to Bird Meertens Formalisms

86 Generalizations of Horner s Rule Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Generalization 1: / / tails + = / where a b = (a b) b Introduction to Bird Meertens Formalisms

87 Generalizations of Horner s Rule Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Generalization 1: / / tails + = / where a b = (a b) b Generalization 2: / ( / f ) tails = / e where e = id a b = (a f b) e Introduction to Bird Meertens Formalisms

88 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The Maximum Segment Sum (mss) Problem Compute the maximum of the sums of all segments of a given sequence of numbers, positive, negative, or zero. mss [3, 1, 4, 1, 5, 9, 2] = 6 Introduction to Bird Meertens Formalisms

89 A Direct Solution An Overview Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition mss = / +/ segs Introduction to Bird Meertens Formalisms

90 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Calculating a Linear Algorithm using Horner s Rule mss = { definition of mss } / +/ segs = { definition of segs } / +/ ++ / tails inits = { map and reduce promotion } / ( / +/ tails) inits = { Horner s rule with a b = (a + b) 0 } / / 0 inits = { accumulation lemma } / / 0 Introduction to Bird Meertens Formalisms

91 A Program in Haskell Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition Exercise: Code the derived linear algorithm for mss in your favorite programming language. Introduction to Bird Meertens Formalisms

92 Segment Decomposition Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition The sequence of calculation steps given in the derivation of the mss problem arises frequently. The essential idea can be summarized as a general theorem. Theorem (Segment Decomposition) Suppose S and T are defined by S = / f segs T = / f tails If T can be expressed in the form T = h / e, then we have S = / h / e Introduction to Bird Meertens Formalisms

93 Functions Lists Structured Recursive Computation Patterns Segments Horner s Rule Application Segment Decomposition National Institute of Informatics June 21 / June 28 / July 5, 2010

94 Longest Even Segment Problem Given is a sequence x and a predicate p. Required is an efficient algorithm for computing some longest segment of x, all of whose elements satisfy p. lsp even [3, 1, 4, 1, 5, 9, 2, 6, 5] = [2, 6]

95 s An Overview A homomorphism from a monoid (α,, id ) to a monoid (β,, id ) is a function h satisfying the two equations: h id = id h (x y) = h x h y

96 Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h

97 Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h Proof Sketch. : simple. : by induction.

98 Lemma (Promotion) h is a homomorphism if and only if the following holds. h / = / h Proof Sketch. : simple. : by induction. So we have f ++ / = ++ / f / ++ / = / ( /)

99 Characterization of s Lemma h is a homomorphism from the list monoid if and only if there exists f and such that h = / f

100 Proof : h = { definition of id } h id = { identity lemma (can you prove it?) } h ++ / [ ] = { h is a homomorphism } / h [ ] = { map distributivity } / (h [ ]) = { definition of h on singleton } / f

101 Proof (Cont.) An Overview : We reason that h = / f is a homomorphism by calculating h ++ / = { given form for h } / f ++ / = { map and reduce promotion } / ( / f ) = { hypothesis } / h

102 Examples of s #: compute the length of a list. # = +/ K 1

103 Examples of s #: compute the length of a list. # = +/ K 1 reverse: reverses the order of the elements in a list. Here, x y = y x. reverse = + / [ ]

104 sort: reorders the elements of a list into ascending order. sort = / [ ] Here, (pronounced merge) is defined by the equations: x [ ] = x [ ] y = y ([a] ++ x) ([b] ++ y) = [a] ++ (x ([b] ++ y)), if a b = [b] ++ (([a] ++ x) y), otherwise

105 all p: returns True if every element of the input list satisfies the predicate p. all p = / p

106 all p: returns True if every element of the input list satisfies the predicate p. all p = / p some p: returns True if at least one element of the input list satisfies the predicate p. some p = / p

107 split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b)

108 split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b) Note: split is a homomorphism on the semigroup ([α] +, + ).

109 split: splits a non-empty list into its last element and the remainder. split : [α] + ([α], α) split [a] = ([ ], a) split (x ++ y) = split x split y where (x, a) (y, b) = (x ++ [a] ++ y, b) Note: split is a homomorphism on the semigroup ([α] +, + ). Exercise: Let init = π 1 split, where π 1 (a, b) = a. Show that init is not a homomorphism.

110 All applied to An Overview The operator o (pronounced all applied to) takes a sequence of functions and a value and returns the result of applying each function to the value. Formally, we have [f, g,..., h] o a = [f a, g a,..., h a] [ ] o a = [ ] [f ] o a = [f a] (fs ++ gs) o a = (fs o a) ++ (gs o a) so, ( o a) is a homomorphism. Exercise: Show that [ ] = [id] o.

111 Conditional Expressions The conditional notation h x = f x, if p x = g x, otherwise will be written by the McCarthy conditional form: h = (p f, g)

112 Conditional Expressions The conditional notation h x = f x, if p x = g x, otherwise will be written by the McCarthy conditional form: h = (p f, g) Laws on Conditional Forms h (p f, g) = (p h f, h g) (p f, g) h = (p h f h, g h) (p f, f ) = f (Note: all functions are assumed to be total.)

113 Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o )

114 Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o ) Exercise: Prove that the filter satisfies the filter promotion property: (p ) ++ / = ++ / (p )

115 Filter The operator (pronounced filter) takes a predicate p and a list x and returns the sublist of x consisting, in order, of all those elements of x that satisfy p. p = ++ / (p [id] o, [ ] o ) Exercise: Prove that the filter satisfies the filter promotion property: (p ) ++ / = ++ / (p ) Exercise: Prove that the filter satisfies the map-filter swap property: (p ) f = f (p f )

116 Cross-product An Overview X is a binary operator that takes two lists x and y and returns a list of values of the form a b for all a in x and b in y. [a, b]x [c, d, e] = [a c, b c, a d, b d, a e, b e] Formally, we define X by three equations: xx [ ] = [ ] xx [a] = ( a) x xx (y ++ z) = (xx y) ++ (xx z) Thus xx is a homomorphism.

117 Properties [ ] is the zero element of X : We have cross promotion rules: [ ]X x = xx [ ] = [ ] f X + / = X + / f / X + / = X / (X /)

118 Example Uses of Cross-product cp: takes a list of lists and returns a list of lists of elements, one from each component. cp [[a, b], [c], [d, e]] = [[a, c, d], [b, c, d], [a, c, e], [b, c, e]] cp = X + / ([id] o )

119 subs: computes all subsequences of a list. subs [a, b, c] = [[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c]] subs = X + / [[ ] o, [id] o ] o

120 subs: computes all subsequences of a list. subs [a, b, c] = [[], [a], [b], [a, b], [c], [a, c], [b, c], [a, b, c]] subs = X + / [[ ] o, [id] o ] o Note: subs = cp [[] o, [id] o ].

121 (all p) : (all even) [[1, 3], [2, 4], [1, 2, 3]] = [[2, 4]] (all p) = ++ / (X + / (p [[id] o ] o, [ ] o ) )

122 Selection Operators An Overview Suppose f is a numeric valued function. We want to define an operator f such that 1 f is associative, commutative and idempotent; 2 f is selective in that 3 f is maximizing in that x f y = x or x f y = y f (x f y) = f x f y

123 Selection Operators An Overview Suppose f is a numeric valued function. We want to define an operator f such that 1 f is associative, commutative and idempotent; 2 f is selective in that 3 f is maximizing in that x f y = x or x f y = y f (x f y) = f x f y Condition: f should be injective.

124 An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4]

125 An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4] To solve this problem, we may refine f to an injective function f such that f x < f y f x < f y.

126 An Example: # But if f is not injective, then x f y is not specified when x y but f x = f y. [1, 2] # [3, 4] To solve this problem, we may refine f to an injective function f such that f x < f y f x < f y. So we may select the lexicographically least sequence as the value of x # y when #x = #y.

127 In this case, + distributes through # : x ++ (y # z) = (x ++ y) # (x ++ z) (x # y) ++ z = (x ++ z) # (y ++ z) That is, (x ++ ) # / = # / (x ++ ) (++ x) # / = # / (++ x). We assume ω = # /[ ], satisfying #ω =. (ω is the zero element of + )

128 A short calculation An Overview # / (all p) = { definition before } # / ++ / (X + / (p [[id] o ] o, [ ] o ) ) = { reduce promotion } # / ( # / X + / (p [[id] o ] o, [ ] o ) ) = { + distributes over # } # / (++ / ( # /) (p [[id] o ] o, [ ] o ) ) = { many steps... } # / (++ / (p [id] o, K ω ) )

129 Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y.

130 Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y. Proof Sketch. : obvious by assuming h = / f.

131 Existence of Existence Lemma The list function h is a homomorphism iff the implication h v = h x h w = h y h (v ++ w) = h (x ++ y) holds for all lists v, w, x, y. Proof Sketch. : obvious by assuming h = / f. : Define by t u = h (g t ++ g u). for some g such that h = h g h (such a g exisits!). Thus h (x ++ y) = h x h y.

132 Reference Lemma For every computatble total function h with enumerable domain, there is a computatble (but possibly partial) function g such that h g h = h.

133 Reference Lemma For every computatble total function h with enumerable domain, there is a computatble (but possibly partial) function g such that h g h = h. Proof. Here is one suitable definition of g. g t = head [x h x = t] If t is in the range of h then this process terminates.

134 Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs

135 Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs Property: lsp is not a homomorphism.

136 Specification of the Problem Recall the problem of computing the longest segment of a list, all of whose elements satisfied some given property p. lsp = # / (all p) segs Property: lsp is not a homomorphism. This is because: does not imply lsp [2, 1] = lsp [2] = [2] lsp [4] = lsp[4] = [4] lsp ([2, 1] ++ [4]) = lsp ([2] ++ [4]).

137 Calculating a Solution to the Problem # / (all p) segs = { segment decomposition } # / ( # / (all p) tails) inits = { result before } # / ( # / (++ / (p [id] o, K ω ) ) tails) inits = { Horner s rule with x a = (x ++ (p a [a], ω) # [ ] } # / [ ] inits = { accumulation lemma } # / [ ]

138 Calculating a Solution to the Problem # / (all p) segs = { segment decomposition } # / ( # / (all p) tails) inits = { result before } # / ( # / (++ / (p [id] o, K ω ) ) tails) inits = { Horner s rule with x a = (x ++ (p a [a], ω) # [ ] } # / [ ] inits = { accumulation lemma } # / [ ] Exercise: Show the final program is linear in the number of calculation of p.

139 National Institute of Informatics June 21 / June 28 / July 5, 2010

140 The Minimax Problem Given is a list of lists of numbers. Required is an efficient algorithm for computing the minimum of the maximum numbers in each list. More succinctly, we want to compute as efficiently as possible. minimax = / /

141 Three Views of Lists Monoid View: every list is either (i) the empty list; (ii) a singleton list; or (iii) the concatenation of two (non-empty) lists. Snoc View: every list is either (i) the empty list; or (ii) of the form x ++ [a] for some list x and value a. Cons View: every list is either (i) the empty list; or (ii) of the form [a] ++ [x] for some list x and value a.

142 Three General Computation Forms Monoid View: homomorphism Snoc View: left reduction (foldl) / e [ ] = e / e (x ++ [a]) = ( / e x) a Cons View: right reduction (foldr) / e [ ] = e / e ([a] ++ x) = a ( / e x)

143 Loops and Left Reductions A left reduction / e x can be translated into the following program in a conventional imperative language. [ var a; a := e; for b in x do a := a oplus b; return a ]

144 Left Zeros Left reductions require that the argument list be traversed in its entirety. Such a traversal can be cut short if we recognize the possibility that an operator may have left zeros. ω is a left zero of if ω a = ω for all a.

145 Left Zeros Left reductions require that the argument list be traversed in its entirety. Such a traversal can be cut short if we recognize the possibility that an operator may have left zeros. ω is a left zero of if ω a = ω for all a. Exercise: Prove that if ω is a zero left of then / ω x = ω for all x. (by induction on snoc list x.)

146 Specialization Lemma Lemma Specialization Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b

147 Specialization Lemma Lemma Specialization Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b Exercise: Prove the specialization lemma.

148 Specialization Lemma Every homomorphism on lists can be expressed as a left (or also a right) reduction. More precisely, / f = / e where e = id a b = a f b Exercise: Prove the specialization lemma.

149 Minimax Let us return to the problem of computing minimax = / / efficiently. Using the specialization lemma, we can write minimax = / where is the identity element of /, and a x = a ( /x)

150 Since distributes through we have a x = / ((a ) x) Using the specialization lemma a second time, we have a x = a / x where b a c = b (a c)

151 Since distributes through we have a x = / ((a ) x) Using the specialization lemma a second time, we have a x = a / x where b a c = b (a c) Exercise: What are left zeros for a and?

152 An Efficient Implementation of minimax xs [ var a; a := infinity; for x in xs while a <> \infinity do a := a odot x; return a ] where the assignment a := a odot x can be implemented by the loop: [ var b; b := -infinity; for c in x while c <> a do b := b max (a min c); a := b ]

Mathematical Structures in Programming Introduction

Mathematical Structures in Programming Introduction Mathematical Structures in Programming to Calculational Programming SOKENDAI/National Institute of Informatics Email: hu@nii.ac.jp URL: http://research.nii.ac.jp/ hu Sokendai, 2015 Mathematical Structures

More information

FUNCTIONAL PEARLS The countdown problem

FUNCTIONAL 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 information

Lecture 4: Higher Order Functions

Lecture 4: Higher Order Functions Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function

More information

Shell CSCE 314 TAMU. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Programming Languages 3. Definition and Proof by Induction

Programming Languages 3. Definition and Proof by Induction Programming Languages 3. Definition and Proof by Induction Shin-Cheng Mu Oct. 22, 2015 Total Functional Programming The next few lectures concerns inductive definitions and proofs of datatypes and programs.

More information

1 Problem Description

1 Problem Description Tree Isomorphism: An Exercise in Functional Programming Jayadev Misra 9/10/01 1 Problem Description The problem is to decide if two unordered trees are the same. More precisely, define a binary relation

More information

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 440: Programming Languages and Translators, Spring 2019 Mon Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Lecture Notes on Data Representation

Lecture Notes on Data Representation Lecture Notes on Data Representation 15-814: Types and Programming Languages Frank Pfenning Lecture 9 Tuesday, October 2, 2018 1 Introduction In this lecture we ll see our type system in action. In particular

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 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 information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 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 information

LECTURE 16. Functional Programming

LECTURE 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 information

LOGIC AND DISCRETE MATHEMATICS

LOGIC AND DISCRETE MATHEMATICS LOGIC AND DISCRETE MATHEMATICS A Computer Science Perspective WINFRIED KARL GRASSMANN Department of Computer Science University of Saskatchewan JEAN-PAUL TREMBLAY Department of Computer Science University

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37 Tjark Weber Functional Programming 1 Based on notes by Sven-Olof Nyström Tjark Weber (UU) Recursion 1 / 37 Background FP I / Advanced FP FP I / Advanced FP This course (Functional Programming I) (5 hp,

More information

Lecture 6: Sequential Sorting

Lecture 6: Sequential Sorting 15-150 Lecture 6: Sequential Sorting Lecture by Dan Licata February 2, 2012 Today s lecture is about sorting. Along the way, we ll learn about divide and conquer algorithms, the tree method, and complete

More information

Contents. Chapter 1 SPECIFYING SYNTAX 1

Contents. Chapter 1 SPECIFYING SYNTAX 1 Contents Chapter 1 SPECIFYING SYNTAX 1 1.1 GRAMMARS AND BNF 2 Context-Free Grammars 4 Context-Sensitive Grammars 8 Exercises 8 1.2 THE PROGRAMMING LANGUAGE WREN 10 Ambiguity 12 Context Constraints in Wren

More information

Week 5 Tutorial Structural Induction

Week 5 Tutorial Structural Induction Department of Computer Science, Australian National University COMP2600 / COMP6260 Formal Methods in Software Engineering Semester 2, 2016 Week 5 Tutorial Structural Induction You should hand in attempts

More information

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

Continuation Passing Style. Continuation Passing Style

Continuation Passing Style. Continuation Passing Style 161 162 Agenda functional programming recap problem: regular expression matcher continuation passing style (CPS) movie regular expression matcher based on CPS correctness proof, verification change of

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections p.

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections p. CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections 10.1-10.3 p. 1/106 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

Reasoning about programs. Chapter 9 of Thompson

Reasoning about programs. Chapter 9 of Thompson Reasoning about programs Chapter 9 of Thompson Proof versus testing A proof will state some property of a program that holds for all inputs. Testing shows only that a property holds for a particular set

More information

Natural Numbers. We will use natural numbers to illustrate several ideas that will apply to Haskell data types in general.

Natural Numbers. We will use natural numbers to illustrate several ideas that will apply to Haskell data types in general. Natural Numbers We will use natural numbers to illustrate several ideas that will apply to Haskell data types in general. For the moment we will ignore that fact that each type in Haskell includes possible

More information

3.7 Denotational Semantics

3.7 Denotational Semantics 3.7 Denotational Semantics Denotational semantics, also known as fixed-point semantics, associates to each programming language construct a well-defined and rigorously understood mathematical object. These

More information

Exercise 1 ( = 22 points)

Exercise 1 ( = 22 points) 1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b

More information

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

How 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 information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

Discrete mathematics

Discrete mathematics Discrete mathematics Petr Kovář petr.kovar@vsb.cz VŠB Technical University of Ostrava DiM 470-2301/02, Winter term 2018/2019 About this file This file is meant to be a guideline for the lecturer. Many

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee 1 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as

More information

Shell CSCE 314 TAMU. Higher Order Functions

Shell CSCE 314 TAMU. Higher Order Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Higher Order Functions 2 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as a result.

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING 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 information

Accurate Step Counting

Accurate Step Counting Accurate Step Counting Catherine Hope and Graham Hutton University of Nottingham Abstract Starting with an evaluator for a language, an abstract machine for the same language can be mechanically derived

More information

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haske 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 information

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics CONVENTIONAL EXECUTABLE SEMANTICS Grigore Rosu CS522 Programming Language Semantics Conventional Semantic Approaches A language designer should understand the existing design approaches, techniques and

More information

Functional Programming

Functional Programming Functional Programming Overview! Functional vs. imperative programming! Fundamental concepts! Evaluation strategies! Pattern matching! Higher order functions! Lazy lists References! Richard Bird, Introduction

More information

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 7 Map, filter, fold Don Sannella University of Edinburgh Part I Map Squares *Main> squares [1,-2,3] [1,4,9] squares :: [Int] -> [Int] squares xs [ x*x x

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

More information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

An introduction introduction to functional functional programming programming using usin Haskell

An 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 information

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics CONVENTIONAL EXECUTABLE SEMANTICS Grigore Rosu CS422 Programming Language Semantics Conventional Semantic Approaches A language designer should understand the existing design approaches, techniques and

More information

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1 LAMBDA CALCULUS 1. Background λ-calculus is a formal system with a variety of applications in mathematics, logic, and computer science. It examines the concept of functions as processes, rather than the

More information

MPRI course 2-4 Functional programming languages Exercises

MPRI course 2-4 Functional programming languages Exercises MPRI course 2-4 Functional programming languages Exercises Xavier Leroy October 13, 2016 Part I: Interpreters and operational semantics Exercise I.1 (**) Prove theorem 2 (the unique decomposition theorem).

More information

A Small Interpreted Language

A Small Interpreted Language A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

More information

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph. Trees 1 Introduction Trees are very special kind of (undirected) graphs. Formally speaking, a tree is a connected graph that is acyclic. 1 This definition has some drawbacks: given a graph it is not trivial

More information

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor COSC252: Programming Languages: Semantic Specification Jeremy Bolton, PhD Adjunct Professor Outline I. What happens after syntactic analysis (parsing)? II. Attribute Grammars: bridging the gap III. Semantic

More information

Z Notation. June 21, 2018

Z Notation. June 21, 2018 Z Notation June 21, 2018 1 Definitions There are many different ways to introduce an object in a Z specification: declarations, abbreviations, axiomatic definitions, and free types. Keep in mind that the

More information

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström Lars-Henrik Eriksson Functional Programming 1 Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström Tjark Weber (UU) Recursion 1 / 41 Comparison: Imperative/Functional Programming Comparison:

More information

Derivation of a Pattern-Matching Compiler

Derivation of a Pattern-Matching Compiler Derivation of a Pattern-Matching Compiler Geoff Barrett and Philip Wadler Oxford University Computing Laboratory, Programming Research Group 1986 Introduction This paper presents the derivation of an efficient

More information

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona CSc 372 Comparative Programming Languages 11 : Haskell Higher-Order Functions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2010 Christian Collberg Higher-Order Functions

More information

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10 Oct 04, 16 21:55 Page 1/10 * Lecture 02 Infer some type arguments automatically. Set Implicit Arguments. Note that the type constructor for functions (arrow " >") associates to the right: A > B > C = A

More information

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search

More information

CS 671, Automated Reasoning

CS 671, Automated Reasoning CS 671, Automated Reasoning Lesson 20: Type Constructs based on Intersection (II): dependent records, abstract data types, basic algebra April 3, 2001 Last time we discussed record types and their representation

More information

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions Higher-Order Functions CSc 520 Principles of Programming Languages 16: Haskell Higher-Order Functions Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright

More information

Reverse Polish notation in constructing the algorithm for polygon triangulation

Reverse Polish notation in constructing the algorithm for polygon triangulation Reverse Polish notation in constructing the algorithm for polygon triangulation Predrag V. Krtolica, Predrag S. Stanimirović and Rade Stanojević Abstract The reverse Polish notation properties are used

More information

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np Chapter 1: Introduction Introduction Purpose of the Theory of Computation: Develop formal mathematical models of computation that reflect real-world computers. Nowadays, the Theory of Computation can be

More information

Theorem Proving Principles, Techniques, Applications Recursion

Theorem Proving Principles, Techniques, Applications Recursion NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1 CONTENT Intro & motivation, getting started with Isabelle Foundations & Principles Lambda Calculus Higher Order Logic,

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

Introduction to Programming, Aug-Dec 2006

Introduction to Programming, Aug-Dec 2006 Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components

More information

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics 400 lecture note #4 [Ch 6] Set Theory 1. Basic Concepts and Definitions 1) Basics Element: ; A is a set consisting of elements x which is in a/another set S such that P(x) is true. Empty set: notated {

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics. Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms

More information

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator 1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning

More information

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham λ-calculus Lecture 1 Venanzio Capretta MGS 2018 - Nottingham Table of contents 1. History of λ-calculus 2. Definition of λ-calculus 3. Data Structures 1 History of λ-calculus Hilbert s Program David Hilbert

More information

Handout 9: Imperative Programs and State

Handout 9: Imperative Programs and State 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 9: Imperative Programs and State Imperative

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Lecture 1. 1 Notation

Lecture 1. 1 Notation Lecture 1 (The material on mathematical logic is covered in the textbook starting with Chapter 5; however, for the first few lectures, I will be providing some required background topics and will not be

More information

SAT-CNF Is N P-complete

SAT-CNF Is N P-complete SAT-CNF Is N P-complete Rod Howell Kansas State University November 9, 2000 The purpose of this paper is to give a detailed presentation of an N P- completeness proof using the definition of N P given

More information

15 212: Principles of Programming. Some Notes on Continuations

15 212: Principles of Programming. Some Notes on Continuations 15 212: Principles of Programming Some Notes on Continuations Michael Erdmann Spring 2011 These notes provide a brief introduction to continuations as a programming technique. Continuations have a rich

More information

Chapter S:V. V. Formal Properties of A*

Chapter S:V. V. Formal Properties of A* Chapter S:V V. Formal Properties of A* Properties of Search Space Graphs Auxiliary Concepts Roadmap Completeness of A* Admissibility of A* Efficiency of A* Monotone Heuristic Functions S:V-1 Formal Properties

More information

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF Copyright Cengage Learning. All rights reserved. SECTION 4.3 Direct Proof and Counterexample III: Divisibility Copyright Cengage Learning. All rights

More information

To illustrate what is intended the following are three write ups by students. Diagonalization

To illustrate what is intended the following are three write ups by students. Diagonalization General guidelines: You may work with other people, as long as you write up your solution in your own words and understand everything you turn in. Make sure to justify your answers they should be clear

More information

A GRAPH FROM THE VIEWPOINT OF ALGEBRAIC TOPOLOGY

A GRAPH FROM THE VIEWPOINT OF ALGEBRAIC TOPOLOGY A GRAPH FROM THE VIEWPOINT OF ALGEBRAIC TOPOLOGY KARL L. STRATOS Abstract. The conventional method of describing a graph as a pair (V, E), where V and E repectively denote the sets of vertices and edges,

More information

Structural polymorphism in Generic Haskell

Structural polymorphism in Generic Haskell Structural polymorphism in Generic Haskell Andres Löh andres@cs.uu.nl 5 February 2005 Overview About Haskell Genericity and other types of polymorphism Examples of generic functions Generic Haskell Overview

More information

Computation with No Memory, and Rearrangeable Multicast Networks

Computation with No Memory, and Rearrangeable Multicast Networks Discrete Mathematics and Theoretical Computer Science DMTCS vol. 16:1, 2014, 121 142 Computation with No Memory, and Rearrangeable Multicast Networks Serge Burckel 1 Emeric Gioan 2 Emmanuel Thomé 3 1 ERMIT,

More information

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS What is discrete? Sets (Rosen, Chapter 2) TOPICS Discrete math Set Definition Set Operations Tuples Consisting of distinct or unconnected elements, not continuous (calculus) Helps us in Computer Science

More information

Part I Basic Concepts 1

Part I Basic Concepts 1 Introduction xiii Part I Basic Concepts 1 Chapter 1 Integer Arithmetic 3 1.1 Example Program 3 1.2 Computer Program 4 1.3 Documentation 5 1.4 Input 6 1.5 Assignment Statement 7 1.5.1 Basics of assignment

More information

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

n 1 x i = xn 1 x 1. i= (2n 1) = n 2. Questions 1. Use mathematical induction to prove that, for any n 1 n 1 x i = xn 1 x 1. (Recall that I proved it in a different way back in lecture 2.) 2. Use mathematical induction to prove that, for all

More information

STABILITY AND PARADOX IN ALGORITHMIC LOGIC

STABILITY AND PARADOX IN ALGORITHMIC LOGIC STABILITY AND PARADOX IN ALGORITHMIC LOGIC WAYNE AITKEN, JEFFREY A. BARRETT Abstract. Algorithmic logic is the logic of basic statements concerning algorithms and the algorithmic rules of deduction between

More information

Formally-Proven Kosaraju s algorithm

Formally-Proven Kosaraju s algorithm Formally-Proven Kosaraju s algorithm Laurent Théry Laurent.Thery@sophia.inria.fr Abstract This notes explains how the Kosaraju s algorithm that computes the strong-connected components of a directed graph

More information

THREE LECTURES ON BASIC TOPOLOGY. 1. Basic notions.

THREE LECTURES ON BASIC TOPOLOGY. 1. Basic notions. THREE LECTURES ON BASIC TOPOLOGY PHILIP FOTH 1. Basic notions. Let X be a set. To make a topological space out of X, one must specify a collection T of subsets of X, which are said to be open subsets of

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Introduction to the Lambda Calculus

Introduction to the Lambda Calculus Introduction to the Lambda Calculus Overview: What is Computability? Church s Thesis The Lambda Calculus Scope and lexical address The Church-Rosser Property Recursion References: Daniel P. Friedman et

More information

7. The Gauss-Bonnet theorem

7. The Gauss-Bonnet theorem 7. The Gauss-Bonnet theorem 7.1 Hyperbolic polygons In Euclidean geometry, an n-sided polygon is a subset of the Euclidean plane bounded by n straight lines. Thus the edges of a Euclidean polygon are formed

More information

Recursive Data and Recursive Functions

Recursive Data and Recursive Functions Structural Recursion and Induction Of all the material in this course, this lesson is probably the hardest for students to grasp at first, but it s also one of the most fundamental. Once you understand

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

Lecture 6,

Lecture 6, Lecture 6, 4.16.2009 Today: Review: Basic Set Operation: Recall the basic set operator,!. From this operator come other set quantifiers and operations:!,!,!,! \ Set difference (sometimes denoted, a minus

More information

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu, Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

More information

Discrete Mathematics Lecture 4. Harper Langston New York University

Discrete 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 information

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic 3.4 Deduction and Evaluation: Tools 3.4.1 Conditional-Equational Logic The general definition of a formal specification from above was based on the existence of a precisely defined semantics for the syntax

More information

Foundations of Computation

Foundations of Computation The Australian National University Semester 2, 2018 Research School of Computer Science Tutorial 5 Dirk Pattinson Foundations of Computation The tutorial contains a number of exercises designed for the

More information

Functional Programming in Haskell Part I : Basics

Functional Programming in Haskell Part I : Basics Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Recursion June 27, 2017 Tong Wang UMass Boston CS 310 June 27, 2017 1 / 20 Recursion Recursion means defining something, such as a function, in terms of itself

More information

Functional Programming in Haskell for A level teachers

Functional Programming in Haskell for A level teachers Functional Programming in Haskell for A level teachers About this document Functional Programming is now part of the A level curriculum. This document is intended to get those who already have some programming

More information

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2012 Midterm Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score

More information

SOFTWARE ENGINEERING DESIGN I

SOFTWARE 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 information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information