FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

Similar documents
User-Defined Algebraic Data Types

CSC324 Principles of Programming Languages

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

CS 320: Concepts of Programming Languages

Introduction to Programming: Lecture 10

Course year Typeclasses and their instances

Overloading, Type Classes, and Algebraic Datatypes

Haskell An Introduction

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

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

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

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Haskell Types COMP360

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

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.

CS 360: Programming Languages Lecture 12: More Haskell

Inductive Data Types

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Advanced features of Functional Programming (Haskell)

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

Advances in Programming Languages

CSc 372 Comparative Programming Languages

Standard prelude. Appendix A. A.1 Classes

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

Functional Programming TDA 452, DIT 142

Programming in Haskell Aug-Nov 2015

Topic 7: Algebraic Data Types

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

CSCE 314 Programming Languages

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Abstract Types, Algebraic Types, and Type Classes

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

A general introduction to Functional Programming using Haskell

CSC324 Principles of Programming Languages

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

INTRODUCTION TO HASKELL

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

Programming Language Concepts, CS2104 Lecture 7

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

CSCE 314 Programming Languages

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)

Type Processing by Constraint Reasoning

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CMSC 330: Organization of Programming Languages

Lecture 2: List algorithms using recursion and list comprehensions

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

Haskell Making Our Own Types and Typeclasses

1 The number systems in Haskell 98

CS 457/557: Functional Languages

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

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

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

Topic 9: Type Checking

Topic 9: Type Checking

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Exercises on ML. Programming Languages. Chanseok Oh

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Functional Programming TDA 452, DIT 142

Lecture 4: Higher Order Functions

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CS 11 Haskell track: lecture 1

A tour of the Haskell Prelude

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

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

Functional Programming Mid-term exam Tuesday 3/10/2017

GHCi: Getting started (1A) Young Won Lim 6/3/17

Functional Programming for Logicians - Lecture 1

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Introduction to OCaml

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

Principles of Programming Languages, 3

GHCi: Getting started (1A) Young Won Lim 5/31/17

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

Principles of Programming Languages (H)

Algebraic Types. Chapter 14 of Thompson

Programming Paradigms

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

Introduction to Programming, Aug-Dec 2006

GHCi: Getting started (1A) Young Won Lim 5/26/17

Topic 1: Introduction

Haskell through HUGS THE BASICS

COMP 202 Java in one week

Structural polymorphism in Generic Haskell

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

Parallel Haskell on MultiCores and Clusters

Input And Output of C++

CIS552: Advanced Programming

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Transcription:

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 = {... -2, -1, 0, 1, 2, 3,... } Static type checking Haskell checks type of each expression when compile. static = compile time (v.s. dynamic = run time) Each expression has a proper type. Type inference Haskell tries to infer type of a given expression. Type inference may fail when information is not enough. main = print f f = read "80" main = print f f::int f = read "80"

3 Type Declaration var 1, var 2,..., var n :: type Explicitly declaring type of variables Help type inference Express your intention Easy to debug defaultlines::int ul, ol, li::string -> String Type declaration of an expression Declare type of an expression inside an expression luckynumber = (7 :: Int) unluckynumber = (13 :: Integer)

4 Polymorphic Type Type may have type variables Polymorphic type type with type variables length :: [a] -> Int zip :: [a] -> [b] -> [(a,b)] Type variables can be instantiated to any types.

5 Algebraic Data Type New type can be declared using data declaration. data T v 1 v 2... = D A t A1 t A2... D B t B1 t B2... D C t C1 t C2...... type variables new type constructor types data constructors A value of type T can be created by D A, D B, D C,... Type name and data construct name need to start with a capital letter.

6 Example data Anchor = A String String A new type Anchor is declared. A is the data constructor of type Anchor. A has two String fields. A :: String -> String -> Anchor href = A "http://www.sfc.keio.ac.jp/" "SFC Home Page" Use data construct pattern to access fields. compileanchor (A url label) =...

7 Field Label Provide label to fields of data constructors. data Anchor = A { aurl :: String, alabel :: String } Labels can be used in data construct patters to access fields. compileanchor (A { aurl = u, alabel = l }) =... anchorurl (A { aurl = u }) = u Field labels can be used as selectors to access fields. aurl :: Anchor -> String alabel :: Anchor -> String href = A "http://www.sfc.keio.ac.jp/" "SFC Home Page" main = do print (alabel href)

8 Field Label (cont.) Field label can be used to create a new value by changing some fields of an existing value. data Anchor = A { aurl :: String, alabel :: String } href = A "http://www.sfc.keio.ac.jp/" "SFC Home Page" main = do print href print (href { alabel = "that" }) outputs A "http://www.sfc.keio.ac.jp/" "SFC Home Page" outputs A "http://www.sfc.keio.ac.jp/" "that"

9 Creating Polymorphic Data Type Use type variable to create polymorphic data type data Stack a = MkStack [a] type variable MkStack [True, False] -- Stack Bool MkStack ['a', 'b', 'c'] -- Stack Char MkStack ["aa", "bb"] -- Stack String

10 Enumeration Type Use to create enumeration type data OpenMode = ReadOnly WriteOnly ReadWrite Type OpenMode can be created by three data constructors. Type OpenMode has three values: ReadOnly WriteOnly ReadWrite Bool is an enumeration type. data Bool = True False

Union Type Param Int PTItem 11 Text String Similar to union in C programming language data PTItem = Param Int Text String Values of PTItem are either Param with integer or Text with String. Text "daikon" Param 5 istext::ptitem -> Bool istext (Text _) = True istext (Param _) = False text::ptitem -> String text (Text s) = s text (Param _) = "(param)"

12 Recursive Type Type declaration may refer itself. Empty Stack a Push a data Stack a = Empty Push a (Stack a) Creating values of Stack a Emtpy Push 1 Empty Push 2 (Push 1 Empty) Push 3 (Push 2 (Push 1 Empty)) Accessing values of Stack a isempty::stack a -> Bool isempty Empty = True isempty (Push ) = False top::stack a -> a top (Push x _) = x pop::stack a -> Stack a pop (Push _ s) = s

13 type Declaration type T v 1 v 2... = t type constructor type variables type Creating a type by renaming an existing type No data constructor type MyList a = [a] MyList a is just an alias of [a]. Any functions for [a] can be used for MyList a.

14 newtype Declaration newtype T v 1 v 2... = D t type constructor type variables Creating a type by using an existing type Has data constructor newtype StackNT a = MKStackNT [a] data StackNT a = MKStackNT [a] type data constructor Similar to data declaration with only one data constructor Representation inside Haskell is simpler for newtype. StackNT a is represented as just [a].

15 Type Class Restriction to polymorphism sort :: [a] -> [a] sort cannot sort arbitrary list, but only with order relation. Type class (or just class) set of types A type in a class needs to implement certain class methods. Example: Ord class Values of a Ord class type can be compared. sort::(ord a) => [a] -> [a] (Ord a) => specifies the restriction to type variable a. a needs to be a type on Ord class.

16 Inheritance Classes may have inheritance relation. Example: Eq class Eq class is a super class of Ord class Eq class has (==) as a class method Eq class Inherit Ord class super class class methods: (==),... sub class class methods: (<=), (>=),...

Class Declaration Eq class class Eq a where (==), (/=) :: a -> a -> Bool -- declaration of class methods 17 x == y = not (x /= y) -- default implementation of (==) x /= y = not (x == y) -- default implementation of (/=) Ord class (Eq as super class) class (Eq a) => (Ord a) where compare :: a -> a -> Ordering (<), (<=), (>), (>=) :: a -> a -> Bool min, max :: a -> a -> a compare x y x == y = EQ x <= y = LT otherwise = GT x <= y = compare x y /= GT x < y = compare x y == LT x >= y = compare x y /= LT x > y = compare x y == GT max x y x <= y = y otherwise = x min x y x <= y = x otherwise = y

18 instance Declaration Declaring a type is an instance of a class data Anchor = A String String instance Eq Anchor where (A u l) == (A u' l') = (u == u') && (l == l') deriving declaration If the implementation is natural and clear, let system implements them. Available for: Eq, Ord, Enum, Bounded, Show, Read data Anchor = A String String deriving (Eq, Show)

19 Important Classes Eq All except IO, (->) Show All except IO, (->) Read All except IO, (->) Ord All except IO, IOError, (->) Enum (), Bool, Char, Ordering, Int, Integer, Float, Double Num Int, Integer, Float, Double Real Int, Integer, Float, Double Bounded Int, Char, Bool, (), Ordering, tuples Fractional Float, Double Functor IO, [], Maybe Integral Int, Integer RealFrac Float, Double Floating Float, Double Monad IO, [], Maybe RealFloat Float, Double MonadPlus IO, [], Maybe

Example: Rational Number A rational number consists of two numbers: numerator and denominator Declare the data type as a pair of integers 20 data Rat = Rat Integer Integer main = print $ Rat 2 3 Cannot print by default. print is only available when show method is defined. print::show a => a -> IO () data Rat = Rat Integer Integer deriving Show main = print $ Rat 2 3 This works, but it shows `Rat 2 3' as it is. Define show method: data Rat = Rat Integer Integer instance Show Rat where show (Rat x y) = show x ++ "/" ++ show y main = print $ Rat 2 3

21 Rational Number (cont.) Use fields data Rat = Rat { num::integer, den::integer } instance Show Rat where show x = show (num x) ++ "/" ++ show (den x) main = do print $ Rat { num = 2, den = 3 } print $ Rat 2 3 Define arithmetic operations. data Rat = Rat { num::integer, den::integer } instance Show Rat where show x = show (num x) ++ "/" ++ show (den x) add::rat -> Rat -> Rat add x y = Rat { num = num x * den y + num y * den x, den = den x * den y } main = print $ add (Rat 1 2) (Rat 1 6)

Rational Number (cont.) Would like to use (+) and (*) Make it an instance of Num class. 22 class Num a where (+) :: a -> a -> a (*) :: a -> a -> a negate :: a -> a abs :: a -> a signum :: a -> a frominteger :: Integer -> a -- or (-):: a -> a -> a -- abs x * signum x == x needs to hold Implement above 6 methods to make Rat an instance of Num class. instance Num Rat where x + y = Rat { num = num x * den y + num y * den x, den = den x * den y } x * y = Rat { num = num x * num y, den = den x * den y } negate x = x { num = - num x } abs x = Rat { num = abs (num x), den = abs (den x) } signum (Rat n d) n == 0 = frominteger 0 n > 0 && d > 0 n < 0 && d < 0 = frominteger 1 otherwise = frominteger (-1) frominteger x = Rat { num = x, den = 1 }

Exercise 9-1 rat.hs import System.Environment import Data.Ratio 23 data Rat = Rat { num::integer, den::integer } instance Show Rat where show x = show (num x) ++ "/" ++ show (num y) instance Num Rat where... instance Fractional Rat where x / y =... fromrational x = Rat { num = numerator x, den = denominator x } main = do args <- getargs x <- return (read (args!! 0)::Integer) y <- return (read (args!! 1)::Integer) u <- return (read (args!! 2)::Integer) v <- return (read (args!! 3)::Integer) print $ Rat x y + Rat u v print $ Rat x y - Rat u v print $ Rat x y * Rat u v print $ Rat x y / Rat u v Complete the implementation of rational number: Reduce fractions to irreducible ones: 3 1 6 2 Make denominators always positive: 2 2 3 3 If the result is integer, print is as integer: 2 2 1 Make is an instance of Fractional. %./rat 2 3 4 5 22/15-2/15 8/15 5/6 %./rat 1 3 2 3 1-1/3 2/9 1/2 %