Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Size: px
Start display at page:

Download "Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group"

Transcription

1 Advanced Type System Features Tom Schrijvers Leuven Haskell User Group

2 Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists and Effect Free Other Handlers Theorems Monoids

3 Data Recursion Genericity Schemes Expression Problem Monads GADTs GADTS DSLs Type Type Families Classes Lists and Effect Free Other Handlers Theorems Monoids

4 Static Type System

5 Static Type System Lightweight specifications Verified / enforced by the type checker

6 Static Type System

7 Static Type System Rules out many invalid programs

8 Static Type System Rules out many invalid programs Rules out some valid programs

9 Advanced Type Systems accept more valid programs accept fewer invalid programs

10 Advanced Type Systems accept more valid programs Java 5 generics Java 4 accept fewer invalid programs

11 A Problem with Expressions

12 Expressions data Exp = ILit Int Add Exp Exp eval :: Exp -> Int eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2

13 More Expressions data Exp = ILit Int Add Exp Exp BLit Bool

14 Revised Evaluator eval :: Exp -> Int eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

15 Revised Evaluator eval :: Exp -> Int eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

16 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

17 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

18 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = IVal n eval (Add e1 e2) = IVal (eval e1 + eval e2) eval (BLit b) = BVal b

19 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = IVal n eval (Add e1 e2) = IVal (eval e1 + eval e2) eval (BLit b) = BVal b

20 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = IVal n eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> IVal (n1+n2) eval (BLit b) = BVal b

21 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Val eval (ILit n) = IVal n eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> IVal (n1+n2) _ ->??? eval (BLit b) = BVal b

22 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = IVal n eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> IVal (n1+n2) _ -> Nothing eval (BLit b) = BVal b

23 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = IVal n eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> IVal (n1+n2) _ -> Nothing eval (BLit b) = BVal b

24 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = Just (IVal n) eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> Just (IVal (n1+n2)) _ -> Nothing eval (BLit b) = Just (BVal b)

25 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = Just (IVal n) eval (Add e1 e2) = case (eval e1, eval e2) of (IVal n1, IVal n2) -> Just (IVal (n1+n2)) _ -> Nothing eval (BLit b) = Just (BVal b)

26 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = Just (IVal n) eval (Add e1 e2) = case (eval e1, eval e2) of (Just (IVal n1), Just (IVal n2)) -> Just (IVal (n1+n2)) _ -> Nothing eval (BLit b) = Just (BVal b)

27 Revised Evaluator data Val = IVal Int BVal Bool eval :: Exp -> Maybe Val eval (ILit n) = Just (IVal n) eval (Add e1 e2) Overhead due to possibly ill-typed expressions = case (eval e1, eval e2) of (Just (IVal n1), Just (IVal n2)) -> Just (IVal (n1+n2)) _ -> Nothing eval (BLit b) = Just (BVal b)

28 GADTs to the Rescue

29 The Problem admits invalid values Exp

30 The Problem admits invalid values Exp Add (BLit True) (ILit 5)

31 The Problem accept more valid programs admits invalid values Exp accept fewer invalid programs

32 The Problem accept more valid programs admits invalid values Exp GADTs accept fewer invalid programs

33 The Problem accept more valid programs admits invalid values Exp Exp a GADTs accept fewer invalid programs

34 The Problem accept more valid programs admits invalid values Exp admits only valid values Exp a GADTs accept fewer invalid programs

35 Parametrised Expressions Exp a

36 Parametrised Expressions Expressions that yield a value of type a type a Exp a

37 Parametrised Expressions Expressions that yield a value of type a type a Exp a indexed family index

38 Parametrised Expressions Expressions that yield a value of type a type a Exp a indexed family index family members Exp Int Exp Bool

39 Parametrised Expressions Expressions that yield a value of type a type a Exp a indexed family index family members Exp Int Exp Bool ILit 0 Add (ILit 1) (ILit 2)

40 Parametrised Expressions Expressions that yield a value of type a type a Exp a indexed family index family members Exp Int ILit 0 Exp Bool BLit True Add (ILit 1) (ILit 2) BLit False

41 Generalized Algebraic Data Type data Exp where ILit :: Int -> Exp Add :: Exp -> Exp -> Exp BLit :: Bool -> Exp

42 Generalized Algebraic Data Type data Exp a where ILit :: Int -> Exp Int Add :: Exp Int -> Exp Int -> Exp Int BLit :: Bool -> Exp Bool

43 Generalized Algebraic Data Type indexed family data Exp a where ILit :: Int -> Exp Int Add :: Exp Int -> Exp Int -> Exp Int BLit :: Bool -> Exp Bool

44 Generalized Algebraic Data Type indexed family Refined index data Exp a where ILit :: Int -> Exp Int Add :: Exp Int -> Exp Int -> Exp Int BLit :: Bool -> Exp Bool

45 GADT Evaluator eval :: Exp a -> a eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

46 GADT Evaluator eval :: Exp a -> a No noise due to possibly ill-typed expressions! eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

47 GADT Evaluator eval :: Exp a -> a No noise due to possibly ill-typed expressions! eval (ILit n) = n From the pattern match we know that a = Int eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b

48 GADT Evaluator eval :: Exp a -> a No noise due to possibly ill-typed expressions! eval (ILit n) = n From the pattern match we know that a = Int eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b From the pattern match we know that a = Bool

49 GADT Extension data Exp a where ILit :: Int -> Exp Int Add :: Exp Int -> Exp Int -> Exp Int BLit :: Bool -> Exp Bool IfThenElse :: Exp Bool -> Exp a -> Exp a -> Exp a

50 Extended GADT Evaluator eval :: Exp a -> a eval (ILit n) = n eval (Add e1 e2) = eval e1 + eval e2 eval (BLit b) = b eval (IfThenElse i t e) = if eval i then eval t else eval e

51 Alternative Syntax data Exp = ILit Int Add (Exp ) (Exp ) BLit Bool

52 Alternative Syntax data Exp a = a ~ Int => ILit Int a ~ Int => Add (Exp Int) (Exp Int) a ~ Bool => BLit Bool

53 Alternative Syntax data Exp a = a ~ Int => ILit Int a ~ Int => Add (Exp Int) (Exp Int) a ~ Bool => BLit Bool Equality Constraint

54 Alternative Syntax data Exp a = a ~ Int => ILit Int a ~ Int => Add (Exp Int) (Exp Int) a ~ Bool => BLit Bool Equality Constraint a ~ Int a equals Int

55 Ensure & Assume Construct value ILit n :: Exp a Type checker has to assure that a ~ Int

56 Ensure & Assume Construct value ILit n :: Exp a Type checker has to assure that a ~ Int Deconstruct value case e :: Exp a of ILit n -> body Type checker can assume that a ~ Int

57 Length-Indexed Lists with GADTs

58 Regular Lists data List a = Nil Cons a (List a) head :: List a -> a head (Cons x xs) = x

59 Regular Lists data List a = Nil Cons a (List a) head :: List a -> a head (Cons x xs) = x

60 Regular Lists data List a = Nil Cons a (List a) head :: List a -> a head (Cons x xs) = x crashes on empty list!

61 The Problem crashes on empty list head List a

62 The Problem crashes on empty list head List a head Nil

63 The Problem accept more valid programs crashes on empty list head List a accept fewer invalid programs

64 The Problem accept more valid programs crashes on empty list head List a GADTs accept fewer invalid programs

65 The Problem accept more valid programs crashes on empty list head List a head List n a GADTs accept fewer invalid programs

66 The Problem accept more valid programs crashes on empty list head List a does not accept head Nil head List n a GADTs accept fewer invalid programs

67 Length-Indexed Lists List n a

68 Length-Indexed Lists List n a indexed family index

69 Length-Indexed Lists List n a indexed family index family members List Z a List (S Z) a List (S (S Z)) a

70 Length-Indexed Lists List n a indexed family index family members List Z a List (S Z) a List (S (S Z)) a Nil Cons x Nil Cons x (Cons y)ni

71 Length-Index List GADT data List a where Nil :: List a Cons :: a -> List a -> List a

72 Length-Index List GADT data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a

73 Length-Index List GADT indexed family data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a

74 Length-Index List GADT indexed family Refined index data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a

75 Length-Index List GADT indexed family Refined index data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a data Z data S n Empty data types: only used as type indices

76 Safe Head data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a head :: List a -> a head (Cons x xs) = x

77 Safe Head data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a head :: List (S n) a -> a head (Cons x xs) = x

78 Safe Head data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a head :: List (S n) a -> a head (Cons x xs) = x > head Nil Couldn't match type Z with S n0 Expected type: List (S n0) a Actual type: List Z a In the first argument of head, namely Nil In the expression: head Nil

79 Safe Zip data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a zip :: List a -> List b -> List (a,b) zip Nil Nil = Nil zip (Cons x xs) (Cons y ys) = Cons (x,y) (zip xs ys) lists of different length!

80 Safe Zip data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a zip :: List n a -> List n b -> List n (a,b) zip Nil Nil = Nil zip (Cons x xs) (Cons y ys) = Cons (x,y) (zip xs ys)

81 Type Families

82 Append? data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a append :: List n a -> List m a -> List? a append Nil ys = ys append (Cons x xs) ys = Cons x (append xs ys)

83 Append? data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a append :: List n a -> List m a -> List? a append Nil ys n + m = ys append (Cons x xs) ys = Cons x (append xs ys)

84 Append? data List n a where Nil :: List Z a Cons :: a -> List n a -> List (S n) a append :: List n a -> List m a -> List? a append Nil ys n + m = ys append (Cons x xs) ys = Cons x (append xs ys) type-level computation

85 The Problem append

86 The Problem accept more valid programs append accept fewer invalid programs

87 The Problem accept more valid programs Type Families append accept fewer invalid programs

88 The Problem accept more valid programs append Type Families append accept fewer invalid programs

89 Type Family aka Type Function type family Add n m where Add Z m = m Add (S n) m = S (Add n m)

90 Append with Type Family type family Add n m where Add Z m = m Add (S n) m = S (Add n m) append :: List n a -> List m a -> List (Add n m) a append Nil ys = ys append (Cons x xs) ys = Cons x (append xs ys)

91 Open & Associated Type Families

92 Collect Type Class class Collect c where insert :: c ->? -> c

93 Elem Type Family (open) type class declaration class Collect c where insert :: c -> Elem c -> c type family Elem c open type family declaration

94 Instances type class instance instance Collect [e] where insert l c = c : l type instance Elem [e] = e type family instance

95 Open Nature type instance Elem [e] = e instance Collect [e] where insert l c = c : l type instance Elem ByteString = Word8 instance Collect ByteString where insert l c = cons c l

96 Open Nature type instance Elem [e] = e instance Collect [e] where insert l c = c : l type instance Elem ByteString = Word8 instance Collect ByteString where insert l c = cons c l double instances

97 Associated Type Family class Collect c where type Elem c insert :: c -> Elem c -> c instance Collect [e] where type Elem [e] = e insert l c = c : l instance Collect ByteString where type Elem ByteString = Word8 insert l c = cons c l

98 Associated Type Family class Collect c where type Elem c insert :: c -> Elem c -> c instance Collect [e] where type Elem [e] = e insert l c = c : l instance Collect ByteString where type Elem ByteString = Word8 insert l c = cons c l

99 Summary

100 Advanced Type System Features Generalised Algebraic Data Types Type-level Functions aka Type Families

101 Advanced Type System Features Generalised Algebraic Data Types Type-level Functions aka Type Families accept more valid programs accept fewer invalid programs

102 More To Learn Existential Types Rank-n Polymorphism Kinds: Type Promotion, Kind Polymorphism Type Classes: Functional Dependencies, Resolution Extensions Value-Dependent Types (beyond Haskell)

103 Next time: 19/5/2015

104 Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists and Effect Free other Handlers Theorems Monoids

105 Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists Quattro Stagioni Effect Free Handlers Theorems other of Haskell Monoids and

106 Join the Google Group Leuven Haskell User Group

Bringing Functions into the Fold Tom Schrijvers. Leuven Haskell User Group

Bringing Functions into the Fold Tom Schrijvers. Leuven Haskell User Group Bringing Functions into the Fold Tom Schrijvers Leuven Haskell User Group Data Recursion Genericity Schemes Expression Problem Rank-N Polymorphism Monads GADTs DSLs Type Type Families Classes Effect Free

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

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

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences] GADTs Advanced functional programming - Lecture 7 Wouter Swierstra and Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree

More information

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences GADTs Advanced functional programming - Lecture 7 Wouter Swierstra 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces:

More information

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences] GADTs AFP Summer School Alejandro Serrano 1 Today s lecture Generalized algebraic data types (GADTs) 2 A datatype data Tree a = Leaf Node (Tree a) a (Tree a) This definition introduces: 3 A datatype data

More information

IA014: Advanced Functional Programming

IA014: Advanced Functional Programming IA014: Advanced Functional Programming 8. GADT Generalized Algebraic Data Types (and type extensions) Jan Obdržálek obdrzalek@fi.muni.cz Faculty of Informatics, Masaryk University, Brno Motivation IA014

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

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

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

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

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

Advances in Programming Languages

Advances in Programming Languages T O Y H Advances in Programming Languages APL8: Multiparameter Type Classes, Constructor Classes Ian Stark School of Informatics The University of Edinburgh Thursday 4 February Semester 2 Week 4 E H U

More information

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CSCE 314 Programming Languages Functors, Applicatives, and Monads CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::

More 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

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group Type Classes in Haskell Tom Schrijvers Leuven Haskell User Group Haskell Research Team Partners Monads Type Classes GHC Folds Pattern Matching Equational Reasoning DSLs Advanced Types Adhoc Overloading

More information

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of

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

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

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 9 Algebraic Data Types Don Sannella University of Edinburgh Part I Algebraic types Everything is an algebraic type data Bool = False True data Season = Winter

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

Dependent Polymorphism. Makoto Hamana

Dependent Polymorphism. Makoto Hamana 1 Dependent Polymorphism Makoto Hamana Department of Computer Science, Gunma University, Japan http://www.cs.gunma-u.ac.jp/ hamana/ This Talk 2 [I] A semantics for dependently-typed programming [II] A

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

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, hi, 3.2), c 4, a 1, b 5} 9/10/2017 4 Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421a # true;; - : bool = true # false;; - : bool

More information

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#, Introduction to ML Based on materials by Vitaly Shmatikov slide 1 ML General-purpose, non-c-like, non-oo language Related languages: Haskell, Ocaml, F#, Combination of Lisp and Algol-like features (1958)

More information

Background Operators (1E) Young Won Lim 7/7/18

Background Operators (1E) Young Won Lim 7/7/18 Background Operators (1E) Copyright (c) 2016-2018 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

EECS 700 Functional Programming

EECS 700 Functional Programming EECS 700 Functional Programming Dr. Andy Gill University of Kansas February 16, 2010 1 / 41 Parsing A parser is a program that analyses a piece of text to determine its syntactic structure. The expression

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

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More 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

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

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

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

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

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

Background Type Classes (1B) Young Won Lim 6/28/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

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

State Monad (3D) Young Won Lim 9/25/17

State Monad (3D) Young Won Lim 9/25/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

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

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

Algebraic Types. Chapter 14 of Thompson

Algebraic Types. Chapter 14 of Thompson Algebraic Types Chapter 14 of Thompson Types so far Base types Int, Integer, Float, Bool, Char Composite types: tuples (t 1,t 2,,t n ) lists [t 1 ] functions (t 1 -> t 2 ) Algebraic types enumerated, product

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

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CMSC 330: Organization of Programming Languages. Functional Programming with Lists CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as

More information

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012 News CSE 130: Spring 2012 Programming Languages On webpage: Suggested HW #1 PA #1 (due next Fri 4/13) Lecture 2: A Crash Course in ML Please post questions to Piazza Ranjit Jhala UC San Diego Today: A

More information

Polymorphism Overview (1A) Young Won Lim 2/20/18

Polymorphism Overview (1A) Young Won Lim 2/20/18 Polymorphism Overview (1A) 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

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values. Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421d # true;; - : bool = true # false;; - : bool =

More information

Shared Subtypes. Subtyping Recursive Parameterized Algebraic Data Types

Shared Subtypes. Subtyping Recursive Parameterized Algebraic Data Types Shared Subtypes Subtyping Recursive Parameterized Algebraic Data Types Ki Yung Ahn kya@cs.pdx.edu Tim Sheard sheard@cs.pdx.edu Department of Computer Science Maseeh College of Engineering & Computer Science

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

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Carlos Varela Rensselaer Polytechnic Institute September 23,

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

Programming in Omega Part 1. Tim Sheard Portland State University

Programming in Omega Part 1. Tim Sheard Portland State University Programming in Omega Part 1 Tim Sheard Portland State University Tim Sheard Computer Science Department Portland State University Portland, Oregon PSU PL Research at Portland State University The Programming

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

Concepts of program design Exam January 31, 13:30 16:30

Concepts of program design Exam January 31, 13:30 16:30 Concepts of program design 2016 2017 Exam January 31, 13:30 16:30 Name: Student number: Please read the following instructions carefully: Fill in your name and student number above. Be prepared to identify

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

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

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

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

Standard prelude. Appendix A. A.1 Classes

Standard prelude. Appendix A. A.1 Classes Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from

More 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

Embedded Domain Specific Language Implementation using Dependent Types

Embedded Domain Specific Language Implementation using Dependent Types Embedded Domain Specific Language Implementation using Dependent Types Edwin Brady eb@cs.st-andrews.ac.uk University of St Andrews GPCE/SLE, Eindhoven, 10/10/10 GPCE/SLE, Eindhoven, 10/10/10 p.1/36 Introduction

More information

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create

More information

List Functions, and Higher-Order Functions

List Functions, and Higher-Order Functions List Functions, and Higher-Order Functions Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ List Functions, and Higher-Order

More information

CSc 372 Comparative Programming Languages

CSc 372 Comparative Programming Languages CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Introduc;on to Hakell Lesson 27! 1 The origins: ML programming

More information

Programming Languages

Programming Languages CSE 130 : Fall 2008 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1, sample for Quiz #1 on Thu PA #1 (due next Fri 10/10) No make-up quizzes

More information

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen Disjoint Unions and Higher-order list functions nsen 1 DTU Compute, Technical University of Denmark Disjoint Unions and Higher-order list functions MRH 27/09/2018 Overview Recap Disjoint union (or Tagged

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

Exercise 1 (2+2+2 points)

Exercise 1 (2+2+2 points) 1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers

More information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.

More information

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona 1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists

More information

Programming Languages

Programming Languages CSE 130: Spring 2010 Programming Languages Lecture 2: A Crash Course in ML Ranjit Jhala UC San Diego News On webpage: Suggested HW #1 PA #1 (due next Wed 4/9) Please post questions to WebCT Today: A crash

More information

Haskell Type Constraints Unleashed

Haskell Type Constraints Unleashed Haskell Type Constraints Unleashed Dominic Orchard University of Cambridge Tom Schrijvers KU Leuven Fun in the Afternoon, Standard Chartered. February 17, 2010 C τ Type classestype classes Data types Type

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

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

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

Type checking by theorem proving in IDRIS

Type checking by theorem proving in IDRIS Type checking by theorem proving in IDRIS p. 1 Type checking by theorem proving in IDRIS Scottish Theorem Proving, 10th February 2012 ecb10@st-andrews.ac.uk University of St Andrews Edwin Brady Type checking

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

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

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over

More information

Verifying the darcs patch code

Verifying the darcs patch code Verifying the darcs patch code David Roundy Oregon State University November 20 2006 The subject of this talk Darcs a revision control system based on a formalism for manipulating changes, which allows

More information

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca Lists Adrian Groza Department of Computer Science Technical University of Cluj-Napoca Recall... Parameter evaluation Call-by-value Call-by-name Call-by-need Functions Infix operators Local declarations,

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

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

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18) Advanced Programming Handout 7 Monads and Friends (SOE Chapter 18) The Type of a Type In previous chapters we discussed: Monomorphic types such as Int, Bool, etc. Polymorphic types such as [a], Tree a,

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

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor. Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin

More information

Programming in Haskell Aug Nov 2015

Programming in Haskell Aug Nov 2015 Programming in Haskell Aug Nov 2015 LECTURE 23 NOVEMBER 12, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Summary of IO Actions of type IO t1, t1 -> IO t2, t1 -> t2 -> IO t3 etc. As opposed to pure functions

More information

Many Holes in Hindley-Milner

Many Holes in Hindley-Milner Many Holes in Hindley-Milner Sam Lindley Laboratory for Foundations of Computer Science, University of Edinburgh Sam.Lindley@ed.ac.uk September 21, 2008 Plugging many-holed contexts : context(3) Plugging

More information

The Worker/Wrapper Transformation

The Worker/Wrapper Transformation The Worker/Wrapper Transformation Andy Gill 1 Graham Hutton 2 1 The University of Kansas 2 The University of Nottingham March 26th, 2009 Andy Gill, Graham Hutton The Worker/Wrapper Transformation March

More information

Type-indexed functions in Generic Haskell

Type-indexed functions in Generic Haskell Type-indexed functions in Generic Haskell Johan Jeuring September 15, 2004 Introduction Today I will talk about: a Types of polymorphism. b Type-indexed functions. c Dependencies. Read about b, and c in

More information

Programming Languages

Programming Languages CSE 130: Fall 2009 Programming Languages Lecture 2: A Crash Course in ML News On webpage: Suggested HW #1 PA #1 (due next Fri 10/9) Technical issues installing Ocaml - should be resolved soon! Ranjit Jhala

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML Announcements CSCI 334: Principles of Programming Languages Lecture 5: Fundamentals III & ML Instructor: Dan Barowy Claire Booth Luce info session tonight for women interested in summer research (hopefully

More information

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997

More information

Haskell Refresher Informatics 2D

Haskell Refresher Informatics 2D Haskell Purely functional! : Everything is a function Haskell Refresher Informatics 2D Kobby. K.A. Nuamah 30 January 2015 Main topics: Recursion Currying Higher-order functions List processing functions

More information

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2) Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes Henrik Nilsson University of Nottingham, UK What is the type of (==)? E.g. the following both work: 1 == 2 a == b I.e., (==) can be used to compare

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

Mini-ML. CS 502 Lecture 2 8/28/08

Mini-ML. CS 502 Lecture 2 8/28/08 Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of

More information

Lightweight Invariants with Full Dependent Types

Lightweight Invariants with Full Dependent Types Chapter 29 Lightweight Invariants with Full Dependent Types Edwin Brady 1, Christoph Herrmann 1, Kevin Hammond 1 Category: Position Paper Abstract: Dependent types allow a programmer to express invariant

More information

CS 209 Functional Programming

CS 209 Functional Programming CS 209 Functional Programming Lecture 03 - Intro to Monads Dr. Greg Lavender Department of Computer Science Stanford University "The most important thing in a programming language is the name. A language

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell: Declaring Types and Classes Dr. Hyunyoung Lee 1 Outline Declaring Data Types Class and Instance Declarations 2 Defining New Types Three constructs for defining types:

More information