Type Processing by Constraint Reasoning

Size: px
Start display at page:

Download "Type Processing by Constraint Reasoning"

Transcription

1 , Martin Sulzmann, Jeremy Wazny 8th November 2006

2 Chameleon Chameleon is Haskell-style language treats type problems using constraints gives expressive error messages has a programmable type system Developers: Martin Sulzmann, Jeremy Wazny Examples taken from Chameleon Caveat Previous versions of Chameleon supported some features No single version shows has all the behaviour we illustrate

3 Hindley/Milner Types Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Functional Dependencies

4 What are Types Good for? Type systems are important tools in the design, analysis, and verification of programming languages. Well-typed programs dont go wrong for a reasonable class or errors. Type systems need to Check types! (Preferably) Infer types (Definitely) Explain type errors!

5 Type Systems are Getting More Complex Fortran types Hindley/Milner types: Higher-order, polymorphic : controlled ad-hoc polymorphism Multiparameter : relations among types Constructor Classes: classes with higher kinds Functional Dependencies: classes with improvement rules Existential Types: objects Lexically Scoped Annotations: more expressive type declarations Guarded Abstract Data Types: types differ per constructor Extended Abstract Data Types: difference controlled by classes...

6 Thesis Understanding types be first mapping them to constraints: improves understanding inference = satisfiability checking = implication tests eases implementation allows more useful error handling makes type systems easier to extend

7 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Hindley/Milner Types Types t ::= variable a function arrow t t base types e.g. Char, Bool constructor types e.g. [t] (list of t) Type scheme σ ::= t ā.t Primitive Constraint p ::= t = t True Constraint C ::= p C C

8 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Hindley/Milner Type Inference f u = g u t False g x y z = if x then z else y Inferring type for g t x = Bool {t x Bool} t ift = t z, t ift = t y {t x Bool, t ift t z, t y t z } t g = t x t y t z t ift {t g Bool t z t z t z } Inferring type for f t g = Bool t 1 t 1 t 1 {t g Bool t 1 t 1 t 1 } t g = t u t gu {t gu t 1 t 1 t 1, t u Bool} t gu = Char t gut {t gut Char Char, t 1 Char} t gut = Bool t body FAILS

9 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Hindley/Milner Type Inference f u = g u t False g x y z = if x then z else y Error message: Couldn t match Char against Bool Expected type: Char Inferred type: Bool In the third argument of g, namely False In the definition of f : f u = g u t False Fixed order of evaluation Only substitutions are maintained

10 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Hindley/Milner Type Inference f u = g u t False g x y z = if x then z else y Potential corrected versions of the program: f u = g u True False g x y z = if x then z else y f u = g u t False g x y z = if z then x else y f u = g u t f g x y z = if x then z else y Only the last correction changes the indicated location!

11 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Locations Collect constraints (not substitutions) Remember locations of constraints Locations l (integers) Justification J ::= ɛ l [l,... l] Primitive constraint p ::= (t = t) J True Location annotated program f u = (((g 1 u 2 ) 3 t 4 ) 5 False 6 ) 7 g x y z = (if 11 x 8 then z 9 else y 10 ) 12 Locations Annotated Type Constraints: (t 1 = t g ) 1, (t 2 = t u ) 2, (t 1 = t 2 t 3 ) 3, (t 4 = Char) 4, (t 3 = t 4 t 5 ) 5, (t 6 = Bool) 6, (t 5 = t 6 t 7 ) 7, t f = t u t 7, (t 8 = t x ) 8, (t 8 = Bool) 11, (t 9 = t z ) 9, (t 10 = t y ) 10, (t 12 = t 9 ) 12, (t 12 = t 10 ) 12, t g = t x t y t z t 12.

12 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Type Error = Unsatisfiable Constraint Failure of unification = unsatisfiable constraint Minimal unsatisfiable subset: E of constraint D E is unsatisfiable: = E all strict subsets of E are satisfiable: e E. = (E {e}) Minimal unsatisfiable subset = minimal reason for type error!

13 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Displaying Type Errors Determine a minimal unsatisfiable subset For example: (t 1 = t g ) 1, (t 1 = t 2 t 3 ) 3, (t 4 = Char) 4, (t 3 = t 4 t 5 ) 5, (t 6 = Bool) 6, (t 5 = t 6 t 7 ) 7, (t 9 = t z ) 9, (t 10 = t y ) 10, (t 12 = t 9 ) 12, (t 12 = t 10 ) 12, t g = t x t y t z t 12. Highlight locations of the minimal unsatisfiable subset f u = g u t False g x y z = if x then z else y Not restricted to a single function definition! At least one location highlighted must be changed!

14 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Multiple Minimal Unsatisfiable Subsets Type information is usually highly redundant. f x = if x then (toupper x) else (tolower x) where toupper,tolower :: Char -> Char f x = if x then (toupper x) else (tolower x) f x = if x then (toupper x) else (tolower x) Locations appearing in all minimal unsatisfiable subsets are more likely to be in error! You may be able to remove all errors changing one f x = if x then (toupper x) else (tolower x)

15 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Better Error Messages Choose a location from the minimal unsatisfiable subset E Remove the constraints for that location from E (satisfiable) Find conflicting types (via E) at that location Assign a colour to each conflicting type Find the locations causing each conflicting type Report the error in terms of locations and conflicting types Highlight the causes of each conflicting type

16 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Better Error Messages Example Choose a location: 12 Remove constraints for location: (t 12 = t 9 ) 12, (t 12 = t 10 ) 12 Find conflicting types at location: t 9 = Bool, t 10 = Char Assign a colour to each conflicting type: t 9, t 10 Find the locations causing each conflicting type: t 9 : [1,3,5,6,9], t 10 : [1,3,4,10] Report the error in terms of locations and conflicting types Problem : Then and else branch must have same type Types : Bool Char Conflict: f u = g u t False g x y z = if x then z else y

17 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Find Locations that Cause a Type? How do we find a minimal set of constraints C that cause another constraint c to hold? Implication: = C c Analogous to finding minimal unsatisfiable subset. Start with E := C Delete a constraint p E: E := E {p} If = E c continue, otherwise replace Stop when no constraint p E can be deleted.

18 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages A Type Error may not be just Two Conflicting Types! f a b True = error " a " f c True z = error " b " f x y z = if z then x else y GHC: Couldn t match Char against Bool Expected type: Char Inferred type: Bool In the definition of f : f x y z = if z then x else y Chameleon: Problem : Definition clauses not unifiable Types : Char -> a -> b -> c d -> Bool -> e -> f g -> g -> h -> i Conflict: f a b True = error " a " f c True z = error " b " f x y z = if z then x else y

19 Hindley Milner Type Inference Locations + Minimal Unsatisfiable Constraints Better Error Messages Failure of Type Checking Function type declaration as well as definition h :: a -> (a,b) h x = (x,x) Inferred type: h :: c -> (c,c) Find variable bound in the declared type w.r.t. inferred type b Find minimal implicant of binding b = a Highlight it h.hs:2: ERROR: Inferred type does not subsume declared Declared: forall a,b. a -> (a,b) Inferred: forall a. a -> (a,a) Problem : The variable b makes the declared type too h x = (x, x)

20 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors : Controlled Overloading A class of types that all supply the same interface: class declarations defined interface class Num a where (+), (-), (*) :: a -> a -> a integer constant :: a instance declarations define members of class instance Num Int where (+) = iplus; (-) = iminus;... instance Num Float where (+) = fplus; (-) = fminus;... Allows overloading (like coercion) e.g :: Float

21 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors : Controlled Overloading A class of types that all supply the same interface: class declarations defined interface class Eq a where (==) :: a -> a -> Bool instance declarations define members of class instance Eq Int where (==) = eqint instance Eq Bool where (==) = eqbool Overloading eqlist:: Eq a => [a] -> [a] -> Bool eqlist [] [] = True eqlist (x:xs) (y:ys) = (x == y) && eqlist xs ys eqlist = False

22 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Superclasses Classes and instances can require that the members have instances of other classes Class heirarchy class Eq a => Ord a where (>) :: a -> a -> Bool Members of Ord must be members of Eq Instances can also be constrained instance Eq a => Eq [a] where (==) = eqlist

23 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Multi-Parameter Single parameter type classes implicitly define sets Multi-parameter type classes define relationships among types class Convert a b where upcast :: a -> b downcast :: b -> Maybe a instance Convert Int Float where upcast = frominteger downcast f = if (ceiling f == floor f) then Just (floor f) else Nothing Clearly classes define constraints!

24 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Primitive constaint p ::= (t = t) J U t True Type scheme σ ::= t ā.c t Class decl cd ::= class (C U ā) l where [m :: (C t) l ] Instance decl id ::= instance (C U t) l where [m = e] We need the ability to Check satisfiability of constraints Check implication of constraints Simplify constraints Check classes and instances agree!

25 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Constraint Handling Rules (CHRs) Lightweight theorem prover for constraints Maps a set (conjunction) of constraints to an equivalent set of constraints Simplification rules: c 1,..., c n d 1,..., d m Replace lhs by rhs Eq Int <=> True Eq Int is replaced by True (proved) Eq Float <=> True Eq Float is replaced by True (proved) Eq [a] <=> Eq a To prove Eq [a] prove Eq a Propagation rules: c 1,..., c n = d 1,..., d m Add rhs to lhs Ord a ==> Eq a Proving Ord a also requires proving Eq a Derivation Ord [Int] Ord [Int], Eq [Int] Ord [Int], Eq Int Ord [Int]

26 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Constraint Handling Rules with Justifications Constraint operations need to maintain justifications: Applying a rule c 1,..., c n d 1,..., d m Find matching set c 1,... c n Find equations E that force the match (minimal implicant) Collect justifications J for c 1,... c n and E Remove c 1,... c n Add d 1,..., d m extending justifications by J. Applying Eq [a] <=> (Eq a) 15 to (t 1 = Int) 1, (t 2 = [t 1 ]) 2, (t 3 = t 2 t 4 ) 4, (t 3 = t 5 t 6 ) 6, (Eq t 5 ) 7. minimal implicant of a.t 5 = [a] is (t 2 = [t 1 ]) 2, (t 3 = t 2 t 4 ) 4, (t 3 = t 5 t 6 ) 6 J = [2, 4, 6, 7] Result (t1 = Int) 1, (t 2 = [t 1 ]) 2, (t 3 = t 2 t 4 ) 4, (t 3 = t 5 t 6 ) 6, (Eq t 1 ) [2,4,6,7,15].

27 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors CHR Algorithms Restrictions on CHR programs Confluent: all derivations for C lead to the same result Terminating: all derivations terminate Range-restricted: all vars in d 1,..., d m appear in c 1,..., c n Single-headed simplification: n = 1 for simplification rules Under these restrictions: Key Results The rules define a canonical form: If P = C 1 C 2 then C 1 D and C 2 D Satisfiability is decidable Minimal unsatisfiable subsets can be determined Implication is decidable Minimal implicants can be determined

28 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Representing Typing Problems with CHRs Use CHRs to represent types f u = (((g 1 u 2 ) 3 t 4 ) 5 False 6 ) 7 class (True => Eq a) 13 where (==) :: (a -> a -> Bool) 14 instance (Eq a => Eq [a]) 15 where (==) = eqlist 16 is represented as F t f <=> (G t 1 ) 1, (t 2 = t u ) 2, (t 1 = t 2 t 3 ) 3, (t 4 = Char) 4, (t 3 = t 4 t 5 ) 5, (t 6 = Bool) 6, (t 5 = t 6 t 7 ) 7, t f = t u t 7 Eq a ==> True 13 (==) t e <=> (Eq a) 14, (t = a a Bool) 14 Eq [a] <=> (Eq a) 15

29 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Type Processing using CHRs Type inference: F tf D Inferred type: tf D For example G t g (t 8 = t x) 8, (t 8 = Bool) 11, (t 9 = t z) 9, (t 10 = t y ) 10, (t 12 = t 9) 12, (t 12 = t 10) 12, t g = t x t y t z t 12 Inferred type: t 12.t g = Bool t 12 t 12 Type checking: f :: C t F tf D 1 t f = t, C D 2 = ( tf D 2 ) ( tf D 1 )

30 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Type Errors with Classes Failure of inference: only possible through unsatisfiable equational constraints Report as before Failure of checking Also possible to have have unmatched type class constraints Extend to report reason (minimal implicant) for these New kinds of errors Missing instance errors Ambiguity errors Incompatible class and instance declarations

31 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Missing Instance Error Classic beginners mistake: sum [] = [] -- should be 0 sum (x:xs) = x + sum xs Inferred type: sum :: Num [a] => [[a] -> [a] In Haskell 98, each type class constraint appearing in a functions type must be of the form U a where a is a type variable. For Num [a] find a minimal implicant of Num t t.t = [t ] Highlight the locations of the minimal implicant sum.hs:4: ERROR: Missing instance Instance:Num [a]: sum [] = [] -- should be 0 sum (x:xs) = x + sum xs

32 Constraint Handling Rules Representing Typing Problems with CHRs Type Class Errors Ambiguity Error f x y z = show (if x then read y else read z) where read :: Read a => [Char] -> a and show :: Show a => a -> [Char] Inferred type: f :: forall a.(read a, Show a) => Bool -> [Char] -> [Char] -> [Char] a does not appear in type part. Ambiguous Highlight the positions where a appears in type Ambiguity can be resolved at these locations f x y z = show (if x then read y else read z) For example f x y z = show (if x then (read y)::int else read z)

33 Functional Dependencies Functional Dependencies class Collects ce e ce -> e where empty :: ce insert :: e -> ce -> ce Collection type ce functionally defines element type e Without this empty :: Collects ce e => ce is ambiguous!

34 Bibliography Hindley/Milner Types Functional Dependencies Theory P. J. Stuckey and M. Sulzmann. A theory of overloading. ACM TOPLAS, EADTs M. Sulzmann, J. Wazny, and P.J. Stuckey. A framework for extended algebraic data types. FLOPS 2006, Better error messages P.J. Stuckey, M. Sulzmann, and J. Wazny. Improving type error diagnosis ACM Haskell Workshop, Functional dependencies G.J. Duck, S. Peyton-Jones, P.J. Stuckey, and M. Sulzmann. Sound and decidable type inference for functional dependencies. ESOP 2004, Type errors P.J. Stuckey, M. Sulzmann, and J. Wazny. Interactive type debugging in Haskell ACM Haskell Workshop, More

Type Processing by Constraint Reasoning

Type Processing by Constraint Reasoning Type Processing by Constraint Reasoning Peter J. Stuckey 1,2, Martin Sulzmann 3, and Jeremy Wazny 2 1 NICTA Victoria Laboratory 2 Department of Computer Science and Software Engineering University of Melbourne,

More information

Functional dependencies versus type families and beyond

Functional dependencies versus type families and beyond Functional dependencies versus type families and beyond Martin Sulzmann National University of Singapore Joint work with Tom Schrijvers Functional dependencies versus type families and beyond p.1 What

More information

Simple Unification-based Type Inference for GADTs

Simple Unification-based Type Inference for GADTs Simple Unification-based Type Inference for GADTs Stephanie Weirich University of Pennsylvania joint work with Dimitrios Vytiniotis, Simon Peyton Jones and Geoffrey Washburn Overview Goal: Add GADTs to

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

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

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

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018 Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data

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

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

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

Simon Peyton Jones Microsoft Research August 2013

Simon Peyton Jones Microsoft Research August 2013 Simon Peyton Jones Microsoft Research August 2013 reverse :: a. [a] -> [a] xs :: [Bool] foo :: [Bool] foo = reverse xs Instantiate reverse with a unification variable, standing for an as-yet-unknown type.

More information

User-Defined Algebraic Data Types

User-Defined Algebraic Data Types 72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

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

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

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

Confluence for Non-Full Functional Dependencies

Confluence for Non-Full Functional Dependencies Chapter 2 Confluence for Non-Full Functional Dependencies Tom Schrijvers 1, Martin Sulzmann 2 Category: Research Paper Abstract: Previous work on type inference for functional dependencies demands that

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

Where is ML type inference headed?

Where is ML type inference headed? 1 Constraint solving meets local shape inference September 2005 2 Types are good A type is a concise description of the behavior of a program fragment. Typechecking provides safety or security guarantees.

More information

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell: Haskell Programs We re covering material from Chapters 1-2 (and maybe 3) of the textbook. Haskell Fundamentals Prof. Susan Older A Haskell program is a series of comments and definitions. Each comment

More information

Katholieke Universiteit Leuven Department of Computer Science

Katholieke Universiteit Leuven Department of Computer Science Type inference for GADTs via Herbrand constraint abduction Martin Sulzmann Tom Schrijvers Peter J. Stuckey Report CW 507, January 2008 Katholieke Universiteit Leuven Department of Computer Science Celestijnenlaan

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

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

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

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

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping Overview Declarative Languages D7012E Lecture 4: The Haskell type system Fredrik Bengtsson / Johan Nordlander Overloading & polymorphism Type classes instances of type classes derived type classes Type

More information

Principal Type Inference for GHC-Style Multi-Parameter Type Classes

Principal Type Inference for GHC-Style Multi-Parameter Type Classes Principal Type Inference for GHC-Style Multi-Parameter Type Classes Martin Sulzmann 1, Tom Schrijvers 2, and Peter J. Stuckey 3 1 School of Computing, National University of Singapore S16 Level 5, 3 Science

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

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella Haskell & functional programming, some slightly more advanced stuff Matteo Pradella pradella@elet.polimi.it IEIIT, Consiglio Nazionale delle Ricerche & DEI, Politecnico di Milano PhD course @ UniMi - Feb

More information

Type Classes and Instance Chains: A Relational Approach. John Garrett Morris

Type Classes and Instance Chains: A Relational Approach. John Garrett Morris Type Classes and Instance Chains: A Relational Approach by John Garrett Morris A dissertation submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science

More information

Topic 9: Type Checking

Topic 9: Type Checking Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

More information

Topic 9: Type Checking

Topic 9: Type Checking Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

More information

Simplifying and Improving Qualified Types

Simplifying and Improving Qualified Types Simplifying and Improving Qualified Types Mark P. Jones Yale University, Department of Computer Science P.O. Box 208285, New Haven, CT 06520-8285. jones-mark@cs.yale.edu Research Report YALEU/DCS/RR-1040,

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

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More 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

Type Inference for Multi-Parameter Type Classes

Type Inference for Multi-Parameter Type Classes Type Inference for Multi-Parameter Type Classes Martin Sulzmann 1 and Peter J. Stuckey 2 1 School of Computing, National University of Singapore S16 Level 5, 3 Science Drive 2, Singapore 117543 sulzmann@comp.nus.edu.sg

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

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

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

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

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 360: Programming Languages Lecture 12: More Haskell

CS 360: Programming Languages Lecture 12: More Haskell CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due

More information

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction Topics Chapter 15 Functional Programming Reduction and Currying Recursive definitions Local definitions Type Systems Strict typing Polymorphism Classes Booleans Characters Enumerations Tuples Strings 2

More information

To What Extend Is Type Inference for Parameteric Polymorphism Possible in the Presence of Ad-Hoc and Subtype Polymorphism

To What Extend Is Type Inference for Parameteric Polymorphism Possible in the Presence of Ad-Hoc and Subtype Polymorphism To What Extend Is Type Inference for Parameteric Polymorphism Possible in the Presence of Ad-Hoc and Subtype Polymorphism Daniel Marty University of Applied Sciences Rapperswil Supervised by Prof. Dr.

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

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read. EDAF40 2nd June 2017 14:00-19:00 WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read. DO NOT WRITE WITH OTHER COLOUR THAN BLACK - coloured text

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

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

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

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

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

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages From Trees to Type Classes Mark P Jones Portland State University 1 Trees:! " There are many kinds of tree data structure.! " For example: data BinTree a = Leaf a BinTree

More information

Lecture 2: List algorithms using recursion and list comprehensions

Lecture 2: List algorithms using recursion and list comprehensions Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns

More information

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited) Type system EDAN40: Functional Programming Types and Type Classes (revisited) Jacek Malec Dept. of Computer Science, Lund University, Sweden April 3rd, 2017 In programming languages, a type system is a

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

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

Arbitrary-rank polymorphism in (GHC) Haskell

Arbitrary-rank polymorphism in (GHC) Haskell Arbitrary-rank polymorphism in (GHC) Haskell CAS 743 Stephen Forrest 20 March 2006 Damas-Milner Type System A Damas-Milner type system (also called Hindley-Milner) is a traditional type system for functional

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

Higher Order Functions in Haskell

Higher Order Functions in Haskell Higher Order Functions in Haskell Evan Misshula 2018-09-10 Outline Curried Functions Curried comparison Example partial application partial application of a string function Returned functions ZipWith flip

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-19: Type

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

Functional Logic Overloading

Functional Logic Overloading Functional Logic Overloading Matthias Neubauer Peter Thiemann Universität Freiburg {neubauer,thiemann}@informatik.uni-freiburg.de Martin Gasbichler Michael Sperber Universität Tübingen {gasbichl,sperber}@informatik.uni-tuebingen.de

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

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 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,

More information

Abstract Types, Algebraic Types, and Type Classes

Abstract Types, Algebraic Types, and Type Classes Informatics 1 Functional Programming Lectures 13 and 14 Monday 9 and Tuesday 10 November 2009 Abstract Types, Algebraic Types, and Type Classes Philip Wadler University of Edinburgh Reminders Tutorial

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

Idris: Implementing a Dependently Typed Programming Language

Idris: Implementing a Dependently Typed Programming Language Idris: Implementing a Dependently Typed Programming Language Edwin Brady University of St Andrews ecb10@st-andrews.ac.uk @edwinbrady Type Inference and Automated Proving, Dundee, 12th May 2015 1 / 25 Idris

More information

Simon Peyton Jones Microsoft Research. August 2012

Simon Peyton Jones Microsoft Research. August 2012 Simon Peyton Jones Microsoft Research August 2012 Typecheck Desugar Source language Intermediate language Haskell Massive language Hundreds of pages of user manual Syntax has dozens of data types 100+

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result. Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

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

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

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

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

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

Simon Peyton Jones Microsoft Research August 2012

Simon Peyton Jones Microsoft Research August 2012 Simon Peyton Jones Microsoft Research August 2012 A functional language Purely functional Lazy Statically typed Designed 1988-1990 By a committee For research, teaching, and practical use Geeks Practitioners

More information

Course year Typeclasses and their instances

Course year Typeclasses and their instances Course year 2016-2017 Typeclasses and their instances Doaitse Swierstra and Atze Dijkstra with extra s Utrecht University September 29, 2016 1. The basics 2 Overloading versus parametric polymorphism 1

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

Introduction to Haskell

Introduction to Haskell Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming

More information

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe! Lecture 7: Haskell CSC 131 Fall, 2014 Kim Bruce According to Larry Wall (designer of PERL): a language by geniuses for geniuses He s wrong at least about the latter part though you might agree when we

More information

Advanced Functional Programming

Advanced Functional Programming Advanced Functional Programming Tim Sheard Portland State University Lecture 2: More about Type Classes Implementing Type Classes Higher Order Types Multi-parameter Type Classes Tim Sheard 1 Implementing

More information

Advanced Functional Programming

Advanced Functional Programming Advanced Functional Programming Tim Sheard Polymorphism Hindley-Milner Polymorphism Rank 2 polymorphism Tim Sheard 1 Polymorphism A function is polymorphic if it can work on any kind of argument. f x =

More information

DETECTING NON-TERMINATION IN CONSTRAINT HANDLING RULES

DETECTING NON-TERMINATION IN CONSTRAINT HANDLING RULES DETECTING NON-TERMINATION IN CONSTRAINT HANDLING RULES DETECTING NON-TERMINATION IN CONSTRAINT HANDLING RULES By ERSHAD RAHIMIKIA, B.SOFTWARE ENGINEERING A Thesis Submitted to the School of Graduate Studies

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

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

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

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

Static Analysis of Haskell

Static Analysis of Haskell Static Analysis of Haskell Neil Mitchell http://ndmitchell.com Static Analysis is getting insights at compile time Full branch coverage Terminates Doesn t rely on a test suite Types are static analysis.

More information

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino 1 FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS Tatsuya Hagino hagino@sfc.keio.ac.jp 2 Static Type Checking and Type Inference Type a set of values Bool = { True, False } Char = { 'a', 'b',... } Int = {...

More information

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

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

First-Class Type Classes

First-Class Type Classes First-Class Type Classes Matthieu Sozeau Joint work with Nicolas Oury LRI, Univ. Paris-Sud - Démons Team & INRIA Saclay - ProVal Project Gallium Seminar November 3rd 2008 INRIA Rocquencourt Solutions for

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

Polymorphic Algebraic Data Type Reconstruction

Polymorphic Algebraic Data Type Reconstruction Polymorphic Algebraic Data Type Reconstruction K.U.Leuven, Belgium {toms,maurice}@cs.kuleuven.be Namur, Belgium - December 11-15, 2006 Overview 2/39 1 2 3 4 5 6 7 Overview 3/39 1 2 3 4 5 6 7 4/39 Types

More information

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler

More information

Tracing Ambiguity in GADT Type Inference

Tracing Ambiguity in GADT Type Inference Tracing Ambiguity in GADT Type Inference ML Workshop 2012, Copenhagen Jacques Garrigue & Didier Rémy Nagoya University / INRIA Garrigue & Rémy Tracing ambiguity 1 Generalized Algebraic Datatypes Algebraic

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information