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

Similar documents
CS 320: Concepts of Programming Languages

CSC324 Principles of Programming Languages

User-Defined Algebraic Data Types

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

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Advances in Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

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

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

Haskell An Introduction

Abstract Types, Algebraic Types, and Type Classes

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.

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

CSCE 314 Programming Languages

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

A general introduction to Functional Programming using Haskell

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

Advanced features of Functional Programming (Haskell)

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

Introduction to Programming and 4Algorithms Abstract Types. Uwe R. Zimmer - The Australian National University

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

Functional Programming TDA 452, DIT 142

CS 11 Haskell track: lecture 1

CS 320: Concepts of Programming Languages

Course year Typeclasses and their instances

Programming with Math and Logic

INTRODUCTION TO HASKELL

Standard prelude. Appendix A. A.1 Classes

Lecture 19: Functions, Types and Data Structures in Haskell

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

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

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

Advanced Functional Programming

CSCE 314 Programming Languages Functors, Applicatives, and Monads

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 360: Programming Languages Lecture 12: More Haskell

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

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

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

1 The number systems in Haskell 98

Types and Type Inference

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

An introduction introduction to functional functional programming programming using usin Haskell

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

Type Processing by Constraint Reasoning

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

Functional Programming TDA 452, DIT 142

Principles of Programming Languages (H)

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

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

Principles of Programming Languages

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

CSCE 314 Programming Languages

CS 440: Programming Languages and Translators, Spring 2019 Mon

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Lecture 4: Higher Order Functions

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

INTRODUCTION TO FUNCTIONAL PROGRAMMING

A tour of the Haskell Prelude

Exercise 1 ( = 22 points)

Monad (1A) Young Won Lim 6/26/17

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)

Introduction to Functional Programming in Haskell 1 / 56

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Monad (3A) Young Won Lim 8/9/17

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

Typed Racket: Racket with Static Types

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

An introduction to functional programming. July 23, 2010

Applicatives Comparisons (2C) Young Won Lim 3/6/18

CS 457/557: Functional Languages

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Typing Haskell in Haskell

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

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

Imperative languages

Topic 1: Introduction

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell

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

CSE 341 Section 5. Winter 2018

Monad (1A) Young Won Lim 6/9/17

Applicative, traversable, foldable

Principles of Programming Languages, 3

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

Applicative, traversable, foldable

CS 209 Functional Programming

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

College Functors, Applicatives

The Typed Racket Guide

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

Higher Order Functions in Haskell

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

Polymorphism and Type Inference

Transcription:

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 collection of rules that assign a property called type to various constructs a computer program consists of, such as variables, expressions, functions or modules. A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute. Jacek Malec, http://rss.cs.lth.se 1(35) Jacek Malec, http://rss.cs.lth.se 2(35) Haskell type system Type theory Motivation: Static type system Type inferencing, thus run-time errors are rare Workflow: edit and typecheck instead of: edit and test run Actually, you will encounter run-time errors, too Hindley-Milner type system type inference algorithm W origins: Haskell Curry, typed lambda calculus, 1958 (wait a couple of lectures:-) deduces most general type, even without type annotations (1969, Hindley) complete (1982, Milner and Damas) normally linear time, suitable for large programs for bounded nesting of let-bindings: polynomial for pathological inputs: exponential (1990) Jacek Malec, http://rss.cs.lth.se 3(35) Jacek Malec, http://rss.cs.lth.se 4(35)

Three kinds of type declarations Qualified types type Name = String type synonym data Season = Spring Summer Autumn Winter algebraic datatype newtype Name = Nm String renamed datatype Same as data with a single unary constructor. Better performance as there is no runtime bookkeeping of a separate type. > :type elem elem :: (Eq a) => a -> [a] -> Bool Qualification needed here to ensure that equality test is defined. Uses type classes. E.g. > elem sin [sin,cos,tan,cot] causes a type error. Jacek Malec, http://rss.cs.lth.se 5(35) Jacek Malec, http://rss.cs.lth.se 6(35) Type classes A structured way to introduce overloaded (or polymorphic) functions class Example a where f1 :: a -> a -> String f2 :: a -> a f3 :: a Usage: create instances instance Example Int where f1 x y = show $ (+) x y f2 = (+1) f3 = 0 Class and instance declaration Class: class Graphical a where shape :: a -> Graphics Instances: instance Graphical Box where shape = boxdraw -- assumed to be previously defined instance Graphical a => Graphical [a] where shape = (foldr1 overgraphic). (map shape) Jacek Malec, http://rss.cs.lth.se 7(35) Jacek Malec, http://rss.cs.lth.se 8(35)

Class inheritance Another example data Eq a => Set a = NilSet ConsSet a (Set a) class Graphical a => Enclosing a where encloses :: Point -> a -> Bool Multiple constraints: (Eq a, Show a) =>... Multiple inheritance: class (Eq a, Show a) => EqShow a Jacek Malec, http://rss.cs.lth.se 9(35) Introduces two (data) constructors NilSet and ConsSet with types > :t NilSet NilSet :: Set a > :t ConsSet ConsSet :: Eq a => a -> Set a -> Set a Type inference will ensure that ConsSet can only be applied to values typed as instances of Eq. f (ConsSet a s) = a > :t f f:: Eq a => Set a -> a Jacek Malec, http://rss.cs.lth.se 10(35) Default definitions Derived instances class Eq a where (==), (!=) :: a -> a -> Bool x!= y = not (x==y) x == y = not (x!=y) data Season = Spring Summer Autumn Winter deriving (Eq, Ord, Enum, Show, Read) notwinter = [Spring..Autumn] From Prelude only Eq, Ord, Enum, Bounded, Show and Read can be derived. Jacek Malec, http://rss.cs.lth.se 11(35) Jacek Malec, http://rss.cs.lth.se 12(35)

Derived instances Haskell vs. Java data Season = Spring Summer Autumn Winter deriving (Eq, Ord, Enum, Show, Read) notwinter = [Spring..Autumn] From Prelude only Eq, Ord, Enum, Bounded, Show and Read can be derived. Classes defined by standard libraries may also be derivable. See generic classes in GHC (but not in pure Haskell 2010). Haskell types, Java classes Haskell class, Java interface Java: A class implements an interface Haskell: A type is an instance of a class Java: An object is an instance of a class Haskell: An expression has a type Jacek Malec, http://rss.cs.lth.se 12(35) Jacek Malec, http://rss.cs.lth.se 13(35) Type class example A note Consider the following class (taken from the Prelude): class Functor f where fmap :: (a -> b) -> f a -> f b The fmap function generalizes the map function used previously. instance Functor [] where fmap = map Functor laws (not enforced by Haskell): fmap id = id fmap (f.g) = (fmap f). (fmap g) The laws mean that fmap does not alter the structure of the functor Jacek Malec, http://rss.cs.lth.se 14(35) Jacek Malec, http://rss.cs.lth.se 15(35)

Type class examples Type class examples Other instances: instance Functor Maybe where fmap f (Just x) = Just (f x) fmap f Nothing = Nothing data Tree a = Leaf a Branch (Tree a) (Tree a) instance Functor Tree where fmap f (Leaf x) = Leaf (f x) fmap f (Branch t1 t2) = Branch (fmap f t1) (fmap f t2) Other instances: instance Functor Maybe where fmap f (Just x) = Just (f x) fmap f Nothing = Nothing data Tree a = Leaf a Branch (Tree a) (Tree a) instance Functor Tree where fmap f (Leaf x) = Leaf (f x) fmap f (Branch t1 t2) = Branch (fmap f t1) (fmap f t2) Note: higher-order class definitions Here Maybe and Tree, not Maybe a or Tree a, is a functor! Jacek Malec, http://rss.cs.lth.se 16(35) Jacek Malec, http://rss.cs.lth.se 16(35) One more important type class Requirements on monadic types class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a m >> k = m >>= \_ -> k fail s = error s All instances of Monad should obey the following laws: return a >>= k = k a m >>= return = m m >>= (\x -> k x >>= h) = (m >>= k) >>= h Instances of both Monad and Functor should satisfy also: fmap f xs = xs >>= return. f Jacek Malec, http://rss.cs.lth.se 17(35) Jacek Malec, http://rss.cs.lth.se 18(35)

Field labelling Field labelling Type definitions data C = F Int Int Bool and data C = F { f1, f2 :: Int, f3 :: Bool} are exactly the same (except that we get deconstructor functions) Type definitions data C = F Int Int Bool and data C = F { f1, f2 :: Int, f3 :: Bool} are exactly the same (except that we get deconstructor functions) Note that in pattern matching notation F {} matches every use of type F. Jacek Malec, http://rss.cs.lth.se 19(35) Jacek Malec, http://rss.cs.lth.se 19(35) Type renaming Numbers in Haskell newtype Age = Age Int or newtype Age = Age {unage :: Int} Note 1: Just one field possible! Note 2: the second variant brings into scope two functions, constructor and deconstructor: All numeric types are instances of the Num class. class (Eq a, Show a) => Num a where (+), (-), (*) :: a -> a -> a negate, abs, signum :: a -> a frominteger :: Integer -> a Age :: Int -> Age unage :: Age -> Int Jacek Malec, http://rss.cs.lth.se 20(35) Jacek Malec, http://rss.cs.lth.se 21(35)

Haskell 98 predefined type classes Numeric type classes Taken from Prelude Jacek Malec, http://rss.cs.lth.se 22(35) Jacek Malec, http://rss.cs.lth.se 23(35) Main numeric types Main numeric classes Integer Integral Arbitrary-precision integers Int Integral Fixed-precision integers (Integral a) => Ratio a RealFrac Rational numbers Rational = Ratio Integer Float RealFloat Floating-point, single precision Double RealFloat Floating-point, double precision Num (Eq, Show) (+), (-), (*),... Fractional (/) Floating exp, log, sin, cos,... Real torational Integral quot, rem, mod,... RealFrac (Fractional) round, truncate RealFloat (Floating) exponent, significand,... Jacek Malec, http://rss.cs.lth.se 24(35) Jacek Malec, http://rss.cs.lth.se 25(35)

Extended Example: MyNatural Functions on MyNatural numeric type based on Peano s definition data MyNatural = Zero Succ MyNatural deriving (Eq, Show) Some values of this type: two = Succ $ Succ Zero three = Succ two natplus Zero y = y natplus (Succ x) y = Succ (natplus x y) natminus x Zero = x natminus Zero y = error "Negative Natural" natminus (Succ x) (Succ y) = natminus x y Jacek Malec, http://rss.cs.lth.se 26(35) Jacek Malec, http://rss.cs.lth.se 27(35) Functions on MyNatural Making MyNatural a number nattimes Zero y = Zero nattimes (Succ x) y = natplus y (nattimes x y) natsignum Zero = Zero natsignum (Succ x) = Succ Zero integertonat 0 = Zero integertonat (x+1) = Succ (integertonat x) instance Num MyNatural where (+) = natplus (-) = natminus (*) = nattimes negate = error "Negative natural" abs x = x signum = natsignum frominteger = integertonat Jacek Malec, http://rss.cs.lth.se 28(35) Jacek Malec, http://rss.cs.lth.se 29(35)

Better output Another example: ListNatural shownat n = show (intvalue n) where intvalue Zero = 0 intvalue (Succ x) = 1 + intvalue x instance Show MyNatural where show = shownat and remove previous deriving of Show! Natural numbers corresponding to lists (of nothing) type ListNatural = [()] For example: twol = [(),()] threel = [(),(),()] What is: (:)? What is: (++)? What is: map (const ())? Jacek Malec, http://rss.cs.lth.se 30(35) Jacek Malec, http://rss.cs.lth.se 31(35) ListNatural, Exercise Church numbers 1 What do these functions do? f1 x y = foldr (:) x y f2 x y = foldr (const (f1 x)) [] y f3 x y = foldr (const (f2 x)) [()] y 2 Continue this definition: instance Num ListNatural where... Note: requires ListNatural to be declared as a newtype! type ChurchNatural a = (a -> a) -> (a -> a) zeroc, onec, twoc :: ChurchNatural a zeroc f = id -- zeroc = const id onec f = f -- onec = id twoc f = f.f Jacek Malec, http://rss.cs.lth.se 32(35) Jacek Malec, http://rss.cs.lth.se 33(35)

Church numbers Church numbers succc n f = f.(n f) threec = succc twoc plusc x y f = (x f).(y f) timesc x y = x.y expc x y = y x showc x = show $ (x (+1)) 0 pc = showc $ plusc twoc threec tc = showc $ timesc twoc threec xc = showc $ expc twoc threec Jacek Malec, http://rss.cs.lth.se 34(35) Jacek Malec, http://rss.cs.lth.se 35(35)