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

Similar documents
CS 320: Concepts of Programming Languages

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

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

Abstract Types, Algebraic Types, and Type Classes

Course year Typeclasses and their instances

Introduction to Programming: Lecture 10

CSCE 314 Programming Languages

1 The number systems in Haskell 98

Haskell An Introduction

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Haskell Making Our Own Types and Typeclasses

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

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

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 520. Principles of Programming Languages 11: Haskell Basics

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

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

CSc 372 Comparative Programming Languages

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Advanced features of Functional Programming (Haskell)

CS 440: Programming Languages and Translators, Spring 2019 Mon

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

A tour of the Haskell Prelude

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

Haskell Scripts. Yan Huang

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

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

CS 320: Concepts of Programming Languages

CSC324 Principles of Programming Languages

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

User-Defined Algebraic Data Types

CSC324 Principles of Programming Languages

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

Tree Equality: The Problem

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

CSCE 314 Programming Languages

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

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

Programming Languages Fall 2013

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

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

Programming in Haskell Aug-Nov 2015

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Haskell Types COMP360

Introduction to Functional Programming using Haskell

Monad Background (3A) Young Won Lim 10/5/17

The Monad.Reader Issue 20

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

An introduction introduction to functional functional programming programming using usin Haskell

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

Set Haskell Exercises

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

More fun with recursion

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

Basic types and definitions. Chapter 3 of Thompson

Overloading, Type Classes, and Algebraic Datatypes

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

CS 360: Programming Languages Lecture 12: More Haskell

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

Higher Order Functions in Haskell

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Background Constructors (1A) Young Won Lim 7/9/18

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

Haskell Revision and Exercises

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Programming Paradigms

Standard prelude. Appendix A. A.1 Classes

CS 11 Haskell track: lecture 1

Functional Programming in Haskell Part I : Basics

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

CIS 194: Homework 2. Due Monday January 28. Log file parsing. Something has gone terribly wrong! Files you will need: Log.hs, error.log, sample.

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

Generic programming for the masses

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

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

CS 457/557: Functional Languages

CSc 372 Comparative Programming Languages

Haskell Overview IV (4A) Young Won Lim 10/13/16

Ordered Pairs, Products, Sets versus Lists, Lambda Abstraction, Database Query

Advances in Programming Languages

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

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

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Types COMP360

Lecture 19: Functions, Types and Data Structures in Haskell

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

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

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

CIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the

BASIC ELEMENTS OF A COMPUTER PROGRAM

Transcription:

1/20 CSc 372 Comparative Programming Languages 18 : Haskell Type Classes Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/20 Type Classes Type classes allow us to specify that a particular type has certain operations defined for it. We ve seen the Eq class already, it states that an instance of this class has to have == defined.

3/20 The Eq Class Consider this definition if Three Valued Logic: data TVL = True False Unknown We would like to be able to compare values of TVL for equality, so we declare it as an instance of the Eq class: instance Eq TVL where True == True = True False == False = True == = False This requires us to define the behavior of == for all values of TVL.

4/20 The Eq Class... The Eq class is defined in the standard Prelude: class Eq a where (==) :: a > a > Bool (/=) :: a > a > Bool

5/20 The Eq Class... Now that we know what it means for two values of type TVL to be equal, we can search in a list using the elem function: > Unknown elem [ False,True,Unknown, False ] False > True elem [ False,True,Unknown, False ] True In ghci the :info command will show you definitions of a name: > : info Eq class Eq a where (==) :: a > a > Bool (/=) :: a > a > Bool

6/20 The Show Class We often want to control the way the values of a particular type is displayed. To do this, create an instance of the Show class: class Show a where showsprec :: Int > a > ShowS show :: a > String showlist : : [ a] > ShowS Here we define how to print the values from the TVL class: instance Show TVL where show True = T show False = F show Unknown =?

7/20 The Show Class... We only have to define the show function, the others have default implementations defined in terms of show. Now a list, for example, of TVL values will print the way we want to: > [False,True,Unknown, False ] [F,T,?,F]

8/20 The Enum Class Class Enum defines operations on sequentially ordered types: class Enum a where succ, pred :: a > a toenum :: Int > a fromenum :: a > Int enumfrom :: a > [a] enumfromthen :: a > a > [a] enumfromto :: a > a > [a] enumfromthento :: a > a > a > [a]

9/20 The Enum Class... We just have to define the fromenum and toenum operations: instance Enum TVL where fromenum True = 0 fromenum False = 1 fromenum Unknown = 2 toenum 0 = True toenum 1 = False toenum 2 = Unknown We now have access to all the functions in the class: > [True.. Unknown] [T,F,?] > succ True

10/20 The Ord Class The Ord class is used for totally ordered datatypes: data Ordering = LT EQ GT class Eq a => Ord a where compare :: a > a > Ordering ( <) :: a > a > Bool ( <=) :: a > a > Bool ( >) :: a > a > Bool ( >=) :: a > a > Bool max :: a > a > a min :: a > a > a We only need to define <=, the rest are added automatically.

11/20 The Ord Class... In our case, since we ve already defined TVL as being an instance of Enum, declaring it an instance of Ord is easy, just define <= in terms of fromenum: instance Ord TVL where c <= c = fromenum c <= fromenum c Now we can sort, for example: > sort [ False,True,Unknown, False ] [T,F,F,?]

12/20 The Read Class... The Read class is approximately the opposite of the Show class: it converts a string to an element of a type. instance Read TVL where readsprec T = [(True, )] readsprec F = [( False, )] readsprec? = [(Unknown, )] Examples: > read T :: TVL T > read? :: TVL?

13/20 Defining a Type Class There s nothing stopping us from creating our own type classes. Here s the Shape class that requires two functions area and circumference to be defined: class Shape a where area :: a > Float circumference :: a > Float Here we define our own polygon data type: data Poly = Triangle Float Float Float Rectangle Float Float deriving Show

14/20 Defining a Type Class... And now we can make Poly an instance of the Shape class: instance Shape Poly where area ( Triangle a b c) = sqrt (p (p a) (p b) (p c)) where p = (a+b+c)/2 area ( Rectangle a b) = a b circumference ( Triangle a b c) = a+b+c circumference ( Rectangle a b) = a+b

15/20 Exercise I Consider this definition of a tree: data Tree a = Branch ( Tree a) ( Tree a) Leaf a Here are some examples of trees: t1 = Leaf 1 t2 = Leaf 2 t3 = Branch ( Leaf 1) ( Leaf 2) t4 = Branch ( Leaf 1) ( Leaf 3) t5 = Branch ( Leaf 1) (Branch (Branch ( Leaf 2) ( Leaf 3)) (Leaf 4))

16/20 Exercise I... Make Tree an instance of the Eq class so that we can compare trees for equality. Examples: > t1==t1 True > t1==t2 False > t3==t4 False > t5==t5 True

Exercise II Consider this data type for complex numbers: data Complex = Complex Int Int deriving Show Instead of this ugly printing > (Complex 4 5) Complex 4 5 we want complex numbers to be printed in standard notation: > (Complex 4 5) 4+5i To accomplish this, make Complex an instance of the Show class, instance Show Complex where show (Complex re im) =... 17/20

18/20 Exercise II... We would like to be able to convert a string representation of a complex number into the Complex datatype: >(read 4 + 5 i ):: Complex 4+5i For simplicity, we assume the + is always surrounded by whitespace. To allow this conversion, override the readsprec function in the Read typeclass: instance Read Complex where readsprec s = [((Complex re im), )] where re =... im =...

19/20 Exercise II... With Complex both an instance of the Read and the Show class, write a main program that prompts the user for a complex number, and then prints it out: Enter a complex number: 4 + 5 i You entered: 4+5 i

20/20 Acknowledgments http://www.haskell.org/tutorial/classes.html http://scienceblogs.com/goodmath/2007/01/16/haskell-the-basics-of-type-cla-1/