QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Size: px
Start display at page:

Download "QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell"

Transcription

1 QuickCheck, SmallCheck & Reach: Automated Testing in Haskell By Tom Shackell

2 A Brief Introduction to Haskell Haskell is a purely functional language. Based on the idea of evaluation of mathematical functions rather than on manipulating state. Heavy emphasis on 'functions' and data-types. Higher-order functions. Passing functions to other functions. Very powerful static type system including typeinference.

3 Some Simple Examples... A program for calculating factorials: fac :: Int -> Int fac 0 = 1 fac n = n * fac (n - 1) And for calculating the length of a list: length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs

4 Data Types A data type for colours data Colour = Red Green Blue For binary trees data Tree a = Empty Node a (Tree a) (Tree a)

5 Higher Order Functions A function to apply some function to every element of a list: map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs for example map (*2) [3,5,9] = [6,10,18]

6 Class Polymorphism A function to tested whether a list contains a particular element: elem :: Eq a => a -> [a] -> Bool elem x [] = False elem x (y:ys) = x == y elem x ys

7 Classes The previous example makes use of the Eq class. class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x /= y = not (x == y) We can define instances of this class to describe how to compare any data type for equality. instance Eq Colour where Red == Red = True Green == Green = True Blue == Blue = True _ == _ = False

8 QuickCheck

9 QuickCheck Developed by Koen Claessen and John Hughes. Based on the idea of specifying properties that the programmer expects to be true. These properties are then tested with a large number of random inputs. Properties are expressed in Haskell using combinators defined in the QuickCheck library.

10 Simple QuickCheck Properties We can express a property such as: prop_revrev :: [Int] -> Bool prop_revrev xs = reverse (reverse xs) == xs We can then ask QuickCheck to verify this property. Main> quickcheck prop_revrev OK: Passed 100 tests Or perhaps it fails. Falsifiable, after 12 tests: [3,5,1]

11 Conditionals We can specify conditional properties. prop_maxle :: Int -> Int -> Property prop_maxle x y = x <= y ==> max x y == y prop_ordinsert :: Int -> [Int] -> Property prop_ordinsert x xs = ordered xs ==> ordered (insert x xs) This second property has a problem...

12 Trivial Cases The condition in prop_ordinsert is quite restrictive. Most lists generated randomly are not ordered. Of those that are, shorter lists are a lot more likely that long ones. Our test function will likely only test very small cases.

13 Generators In fact it's better to define this property using a 'generator'. prop_ordinsert :: Int -> Property prop_ordinsert x = forall orderedlist $ \ xs -> ordered (insert x xs) The generator orderedlist generates a random ordered list of numbers.

14 Generator Instances We can tell QuickCheck how to generate random data of any type by providing an instance of the arbitrary class. class Arbitrary a where arbitrary :: Gen a for example instance Arbitrary Colour where arbitrary = elements [ Red, Green, Blue ]

15 Generator Functions We can also define generator functions such as orderedlist orderedlist :: (Arbitrary a, Ord a) => Gen [a] orderedlist = do xs <- arbitrary return (sort xs)

16 How QuickCheck is implemented QuickCheck is implemented as a Haskell library imported into the program rather than an external tool. quickcheck is a Haskell function that takes a function of any number of arguments. It generates random values for the arguments, passes them to the function and observes the result. Internally the implementation relies on some quite clever use of Haskell's class system.

17 QuickCheck Used for random testing. Easy to use as it's a standard Haskell library. Generally works best for simple data structures. Works best for properties with simple preconditions. Writing generators can be time consuming and tedious.

18 SmallCheck

19 SmallCheck Developed by Colin Runciman at York. Design is similar to QuickCheck. However the focus is on finding small counter examples through exhaustive search of the space. Uses a depth-bound on the size of data generated to control search space explosion.

20 Usage SmallCheck is used in a very similar way to QuickCheck prop_revrev :: [Bool] -> Bool prop_revrev xs = reverse (reverse xs) == xs Main> smallcheck 5 prop_revrev Completed 63 tests without failure Here we've checked all lists of Bools with length less than or equal to 5.

21 Existentials SmallCheck introduces the idea of existential quantification. prop_isprefix :: [Bool] -> [Bool] -> Property prop_isprefix xs ys = isprefix xs ys ==> exists $ \ zs -> ys == xs ++ zs SmallCheck will search exhaustively for a zs that matches the criteria specified.

22 SmallCheck Implemented in much the same way as QuickCheck but with exhaustive searching. Has issues with large search spaces. Still requires user to write generators.

23 Reach

24 Reach Developed by Matt Naylor and Colin Runciman Is based on the idea of trying to find an input that causes a target expression to be evaluated. Very much based around the idea of avoiding having to write 'generators'. Is implemented using constraint solving and Functional Logic Programming constructs. Like SmallCheck, makes use of a depth-bound.

25 Target Expressions In Reach the user specifies a target expression that they would like to be executed. This is done using the target function. f xs ys = if ordered xs && ordered ys then target (xs ++ ys) else... Reach will then search for an input to the function f that causes target to be evaluated.

26 Property Testing This can be used to test properties just like in QuickCheck or SmallCheck. prop_revrev :: [Int] -> Bool prop_revrev xs = reverse (reverse xs) == xs main xs = refute (prop_revrev xs) refute True = True refute False = target False Reach will search for an input that makes the property false.

27 Constraint Solving Reach does not use exhaustive search as in SmallCheck. Instead it uses constraint solving. At the top level the main function is evaluated with unbound logical variables as the arguments. Evaluation proceeds as normal Haskell but instead of inspecting data structures computation introduces constraints. Evaluation finishes when the target is reached or the depth-bound exceeded.

28 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) main a {a=?} Stack

29 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte a (S Z) {a=?} Stack

30 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte Z (S Z) lte a (S Z) {a=?} Stack

31 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte Z (S Z) {a=z} lte a (S Z) {a=?} Stack

32 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) Target not reached! True {a=z} lte a (S Z) {a=?} Stack

33 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) (S Z) doesn't match Z lte a (S Z) {a=?} Stack

34 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte (S b) (S Z) {a=s b, b =?} lte a (S Z) {a=?} Stack

35 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte (S lte x) b Z (S{a=S Z) {x=s b, b y, = y?} =?} lte a (S Z) {a=?} Stack

36 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte Z Z {a=s b, b = Z} lte (S lte x) b Z (S{a=S Z) {x=s b, b y, = y?} =?} lte a (S Z) {a=?} Stack

37 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) Target not reached! True {a = S b, b = Z} lte (S lte x) b Z (S{a=S Z) {x=s b, b y, = y?} =?} lte a (S Z) {a=?} Stack

38 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) lte (S c) Z {a=s b, b = S c, c =?} lte (S lte x) b Z (S{a=S Z) {x=s b, b y, = y?} =?} lte a (S Z) {a=?} Stack

39 data Nat = Z S Nat lte Z _ = True lte (S _) Z = target False lte (S x) (S y) = lte x y main a = lte a (S Z) Target reached! Answer found: a = S b, b = S c, c =? a = S (S?) target False {a=s b, b = S c, c =?} lte (S lte x) b Z (S{a=S Z) {x=s b, b y, = y?} =?} lte a (S Z) {a=?} Stack

40 Reach Can be used for many of the same problems as SmallCheck. Is often much more efficient than SmallCheck, especially in the presence of complex antecedents. No need to write 'generators' which can be a big win in certain applications. Implemented as an external tool rather than a Haskell library.

41 Conclusion Variety of tools for automated testing in Haskell. The ideas from QuickCheck have been used in several other languages (Erlang, Scheme, Lisp, Python, Ruby, SML). Matt Naylor working on SparseCheck a version of Reach used as a Haskell library.

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Testing. 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 information

Informatics 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 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 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

Shell CSCE 314 TAMU. Functions continued

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

Data Structures. Datatype. Data structure. Today: Two examples. A model of something that we want to represent in our program

Data Structures. Datatype. Data structure. Today: Two examples. A model of something that we want to represent in our program Datastructures Data Structures Datatype A model of something that we want to represent in our program Data structure A particular way of storing data How? Depending on what we want to do with the data

More information

Software System Design and Implementation

Software System Design and Implementation Software System Design and Implementation Property-based Testing Gabriele Keller The University of New South Wales School of Computer Science and Engineering Sydney, Australia COMP3141 17s1 Testing in

More information

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

Introduction to Programming: Lecture 6

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

How does ML deal with +?

How does ML deal with +? How does ML deal with +? Moscow ML version 2.00 (June 2000) - op +; > val it = fn : int * int -> int - 1 + 1; > val it = 2 : int - 1.0 + 1.0; > val it = 2.0 : real - false + false;! Overloaded + cannot

More information

Advanced features of Functional Programming (Haskell)

Advanced features of Functional Programming (Haskell) Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of

More information

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB Monads and all that I. Monads John Hughes Chalmers University/Quviq AB Binary Trees in Haskell data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq,Show) Cf Coq: Inductive tree (A:Set) : Set := leaf

More information

Source-Based Trace Exploration Work in Progress

Source-Based Trace Exploration Work in Progress Source-Based Trace Exploration Work in Progress Olaf Chitil University of Kent, UK Abstract. Hat is a programmer s tool for generating a trace of a computation of a Haskell 98 program and viewing such

More information

INTRODUCTION TO HASKELL

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

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

SmallCheck and Lazy SmallCheck

SmallCheck and Lazy SmallCheck SmallCheck and Lazy SmallCheck automatic exhaustive testing for small values Colin Runciman Matthew Naylor University of York, UK {colin,mfn}@cs.york.ac.uk Fredrik Lindblad Chalmers University of Technology

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

Haskell Overview II (2A) Young Won Lim 8/9/16

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

Logic - CM0845 Introduction to Haskell

Logic - CM0845 Introduction to Haskell Logic - CM0845 Introduction to Haskell Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester

More information

QuickCheck: Beyond the Basics

QuickCheck: Beyond the Basics QuickCheck: Beyond the Basics Dave Laing May 13, 2014 Basic QuickCheck Some properties reverse_involutive :: [Int] -> Bool reverse_involutive xs = xs == (reverse. reverse) xs sort_idempotent :: [Int] ->

More information

Random Testing in PVS

Random Testing in PVS Random Testing in PVS Sam Owre SRI International, Computer Science Laboratory 333 Ravenswood Avenue, Menlo Park, CA 94025, USA owre@csl.sri.com Abstract. Formulas are difficult to formulate and to prove,

More information

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Haskell Types, Classes, and Functions, Currying, and Polymorphism 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the

More information

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm. Quick announcement Midterm date is Wednesday Oct 24, 11-12pm. The lambda calculus = ID (λ ID. ) ( ) The lambda calculus (Racket) = ID (lambda (ID) ) ( )

More information

1 The smallest free number

1 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 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

Programming in Haskell Aug-Nov 2015

Programming in Haskell Aug-Nov 2015 Programming in Haskell Aug-Nov 2015 LECTURE 14 OCTOBER 1, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Enumerated data types The data keyword is used to define new types data Bool = False True data Day

More information

Papers we love - Utrecht

Papers we love - Utrecht Papers we love - Utrecht QuickCheck Wouter Swierstra 1 Requirements for a topic A paper that I love 2 Requirements for a topic A paper that I love A paper that is interesting to academics and developers

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

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

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers: Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers: -- Full credit is given for fully correct answers. -- Partial credit

More information

Static Contract Checking for Haskell

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Haskell Overview III (3A) Young Won Lim 10/4/16

Haskell Overview III (3A) Young Won Lim 10/4/16 (3A) 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 information

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that

More information

Some Advanced ML Features

Some Advanced ML Features Some Advanced ML Features Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming University of Washington: Dan Grossman ML is small Small number of powerful constructs

More information

Automatic Testing of Operation Invariance

Automatic Testing of Operation Invariance Automatic Testing of Operation Invariance Tobias Gödderz and Janis Voigtländer University of Bonn, Germany, {goedderz,jv}@cs.uni-bonn.de Abstract. We present an approach to automatically generating operation

More information

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Haskell: 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 information

Property-Based Testing for Coq. Cătălin Hrițcu

Property-Based Testing for Coq. Cătălin Hrițcu Property-Based Testing for Coq Cătălin Hrițcu Prosecco Reading Group - Friday, November 29, 2013 The own itch I m trying to scratch hard to devise correct safety and security enforcement mechanisms (static

More information

Programming in Haskell Aug-Nov 2015

Programming in Haskell Aug-Nov 2015 Programming in Haskell Aug-Nov 2015 LECTURE 11 SEPTEMBER 10, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Measuring efficiency Measuring efficiency Computation is reduction Application of definitions

More information

From Types to Contracts

From Types to Contracts From Types to Contracts head [] = BAD head (x:xs) = x head :: [a] -> a (head 1) Bug! Type BAD means should not happen: crash null :: [a] - > Bool null [] = True null (x:xs) = False head {xs not (null xs)}

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language

More information

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses CS 6A Delayed Expressions Fall 07 Discussion 9: November 8, 07 Iterables and Iterators An iterable is any container that can be processed sequentially. Examples include lists, tuples, strings, and dictionaries.

More information

CPM: A Declarative Package Manager with Semantic Versioning

CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus (CAU Kiel) CPM: A Declarative Package Manager with Semantic Versioning CICLOPS 2017 1 CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus University of Kiel Programming

More information

Stacks and queues (chapters 6.6, 15.1, 15.5)

Stacks and queues (chapters 6.6, 15.1, 15.5) Stacks and queues (chapters 6.6, 15.1, 15.5) So far... Complexity analysis For recursive and iterative programs Sorting algorithms Insertion, selection, quick, merge, (intro, dual-pivot quick, natural

More information

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group Advanced Type System Features Tom Schrijvers Leuven Haskell User Group Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists and Effect Free Other Handlers

More information

Haskell Overview II (2A) Young Won Lim 8/23/16

Haskell Overview II (2A) Young Won Lim 8/23/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 information

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 5 Function properties Don Sannella University of Edinburgh Part I Booleans and characters Boolean operators not :: Bool -> Bool (&&), ( ) :: Bool -> Bool ->

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

Extended Static Checking for Haskell (ESC/Haskell)

Extended Static Checking for Haskell (ESC/Haskell) Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge Program Errors Give Headache! Module UserPgm where f ::

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

Signature Inference for Functional Property Discovery

Signature Inference for Functional Property Discovery Signature Inference for Functional Property Discovery or: How never to come up with tests manually anymore(*) Tom Sydney Kerckhove FP Complete https://cs-syd.eu/ https://github.com/norfairking https://fpcomplete.com

More information

Background Type Classes (1B) Young Won Lim 6/14/18

Background Type Classes (1B) Young Won Lim 6/14/18 Background Type Classes (1B) Copyright (c) 2016-2017 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

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language

More information

Programming Languages Fall 2013

Programming Languages Fall 2013 Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy

More information

Monad Background (3A) Young Won Lim 11/18/17

Monad Background (3A) Young Won Lim 11/18/17 Copyright (c) 2016-2017 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 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

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

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

An introduction to functional programming. July 23, 2010

An introduction to functional programming. July 23, 2010 An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief

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

Combining Static and Dynamic Contract Checking for Curry

Combining Static and Dynamic Contract Checking for Curry Michael Hanus (CAU Kiel) Combining Static and Dynamic Contract Checking for Curry LOPSTR 2017 1 Combining Static and Dynamic Contract Checking for Curry Michael Hanus University of Kiel Programming Languages

More information

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example CMSC 330: Organization of Programming Languages OCaml 2 Higher Order Functions Tuples Constructed using (e1,..., en) Deconstructed using pattern matching Patterns involve parens and commas, e.g., (p1,p2,

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

DerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern

DerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern DerivingVia or, How to Turn Hand-Written Instances into an Anti-Pattern Baldur Blöndal Ryan Scott 1 Andres Löh 2 1 Indiana University 2 Well-Typed LLP rgscott@indiana.edu github.com/ryanglscott A brief

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

Functional Logic Programming Language Curry

Functional Logic Programming Language Curry Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 Outline Functional Logic Programming Language 1 Functional Logic Programming Language

More information

INFOB3TC Solutions for the Exam

INFOB3TC Solutions for the Exam Department of Information and Computing Sciences Utrecht University INFOB3TC Solutions for the Exam Johan Jeuring Monday, 13 December 2010, 10:30 13:00 lease keep in mind that often, there are many possible

More information

The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3

The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3 The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3 Heijltjes, Wadler Due: The tutorial of week 5 (23/24 Oct.) Reading assignment: Chapters 8 and 9 (pp. 135-166) Please attempt the entire

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

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML CMSC 330: Organization of Programming Languages Functional Programming with OCaml 1 Background ML (Meta Language) Univ. of Edinburgh, 1973 Part of a theorem proving system LCF The Logic of Computable Functions

More information

TESTING WEB SERVICES WITH WEBDRIVER AND QUICKCHECK

TESTING WEB SERVICES WITH WEBDRIVER AND QUICKCHECK TESTING WEB SERVICES WITH WEBDRIVER AND QUICKCHECK Alex Gerdes (joint work with Thomas Arts, John Hughes, Hans Svensson and Ulf Norell) QuviQ AB ABOUT ME From the Netherlands, now living in Sweden Worked

More information

Type Processing by Constraint Reasoning

Type Processing by Constraint Reasoning , Martin Sulzmann, Jeremy Wazny 8th November 2006 Chameleon Chameleon is Haskell-style language treats type problems using constraints gives expressive error messages has a programmable type system Developers:

More information

Quiz Stuff. Use a full sheet of 8½x11" paper. (Half sheet? Half credit!) ...

Quiz Stuff. Use a full sheet of 8½x11 paper. (Half sheet? Half credit!) ... QUIZ! Quiz Stuff Use a full sheet of 8½x11" paper. (Half sheet? Half credit!) Put your last name and first initial in the far upper left hand corner of the paper, where a staple would hit it. (It helps

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

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

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1 Type Checking vs

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

More information

Programming with Universes, Generically

Programming with Universes, Generically Programming with Universes, Generically Andres Löh Well-Typed LLP 24 January 2012 An introduction to Agda Agda Functional programming language Static types Dependent types Pure (explicit effects) Total

More information

Programming Language Concepts, CS2104 Lecture 7

Programming Language Concepts, CS2104 Lecture 7 Reminder of Last Lecture Programming Language Concepts, CS2104 Lecture 7 Tupled Recursion Exceptions Types, ADT, Haskell, Components 5th Oct 2007 CS2104, Lecture 7 1 Overview 5th Oct 2007 CS2104, Lecture

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

Programming with Math and Logic

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

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

Lecture 8: Summary of Haskell course + Type Level Programming

Lecture 8: Summary of Haskell course + Type Level Programming Lecture 8: Summary of Haskell course + Type Level Programming Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 31, 2017 Principles from Haskell

More information

CSci 450: Org. of Programming Languages Overloading and Type Classes

CSci 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 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

GADTs. GADTs in Haskell

GADTs. GADTs in Haskell GADTs GADTs in Haskell ADT vs GADT Algebraic Datatype Data List a = Nil Cons a (List a) Data Tree a b = Tip a Node (Tree a b) b Fork (Tree a b) (Tree a b) Note that types than can be expressed as an ADT

More information

A general introduction to Functional Programming using Haskell

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

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs) Programming Paradigms Written Exam (6 CPs) 22.06.2017 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution

More information

Instances for Free* Neil Mitchell (* Postage and packaging charges may apply)

Instances for Free* Neil Mitchell  (* Postage and packaging charges may apply) Instances for Free* Neil Mitchell www.cs.york.ac.uk/~ndm (* Postage and packaging charges may apply) Haskell has type classes f :: Eq a => a -> a -> Bool f x y = x == y Polymorphic (for all type a) Provided

More information

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

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

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010 Optimising Functional Programming Languages Max Bolingbroke, Cambridge University CPRG Lectures 2010 Objectives Explore optimisation of functional programming languages using the framework of equational

More information

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

Programming Language Concepts: Lecture 14

Programming Language Concepts: Lecture 14 Programming Language Concepts: Lecture 14 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 14, 11 March 2009 Function programming

More information

The Haskell HOP: Higher-order Programming

The Haskell HOP: Higher-order Programming The Haskell HOP: Higher-order Programming COS 441 Slides 6 Slide content credits: Ranjit Jhala, UCSD Agenda Haskell so far: First-order functions This time: Higher-order functions: Functions as data, arguments

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

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source

More information

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 4 Lists and Recursion Don Sannella University of Edinburgh Part I Lists and Recursion Cons and append Cons takes an element and a list. Append takes two lists.

More information

Type families and data kinds

Type families and data kinds Type families and data kinds AFP Summer School Wouter Swierstra 1 Today How do GADTs work? Kinds beyond * Programming with types 2 Calling functions on vectors Given two vectors xs : Vec a n and ys : Vec

More information

Speculate: Discovering Conditional Equations and Inequalities about Black-Box Functions by Reasoning from Test Results

Speculate: Discovering Conditional Equations and Inequalities about Black-Box Functions by Reasoning from Test Results Speculate: Discovering Conditional Equations and Inequalities about Black-Box Functions by Reasoning from Test Results Abstract Rudy Braquehais University of York, UK rmb532@york.ac.uk This paper presents

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,

More information