type classes
play

Type Classes Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Objectives Type Classes Type Classes Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives Type Classes Objectives Describe the concept of polymorphism . Show how to declare instances


  1. Objectives Type Classes Type Classes Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Objectives Type Classes Objectives ◮ Describe the concept of polymorphism . ◮ Show how to declare instances of a type class. ◮ Understand the Eq , Ord , Show , and Read type classes.

  3. Objectives Type Classes Polymorphism ◮ We often want to use the same operation on things of different type . ◮ How can we do that? ◮ Overloading – C++ - like languages ◮ Inheritance – Object oriented languages ◮ Parameterized Types – Hindley Milner typed languages (Haskell, SML, etc.); C++ (templates), Java (generics) ◮ Type Classes – Haskell

  4. int inc ( int i) { } double inc ( double i) { } Objectives Type Classes Overloading return i + 1; return i + 1.0;

  5. public class Shape { } public int width , height ; } Objectives Type Classes Inheritance public int loc_x , loc_y ; public class Square extends Shape {

  6. public class List< E > { public E data ; public List < E > next ; } Objectives Type Classes Parametric Polymorphism data Cons a = Cons a ( Cons a) | Nil

  7. class Eq a where -- Minimal complete definition: -- (==) or (/=) x /= y = not (x == y) x == y = not (x /= y) Objectives Type Classes The Eq Type Class ( == ), ( /= ) :: a -> a -> Bool

  8. data Foo = Foo Int x = Foo 10 y = Foo 10 *Main> x == y < interactive >: 1 : 3 : No instance for ( Eq Foo ) arising from a use of ` == ' Possible fix : add an instance declaration for ( Eq Foo ) In the expression : x == y In an equation for `it' : it = x == y Objectives Type Classes Using Eq ◮ If you try to compare these ...

  9. instance Eq Foo where ( == ) ( Foo i) ( Foo j) = i == j *Main> let x = Foo 10 *Main> let y = Foo 10 *Main> x == y True Objectives Type Classes Use an Instance ◮ Now if you try to compare these ...

  10. deriving Eq data Foo = Foo Int Objectives Type Classes tl;dc ◮ Too long! Didn’t Code! ◮ Let Haskell do the work.

  11. min x y = if x <= y then x else y else GT max x y = if x <= y then y else x _ -> False } y = case compare x y of { GT -> True ; x > x <= y = case compare x y of { GT -> False ; _ -> True } _ -> False } y = case compare x y of { LT -> True ; x < else if x <= y then LT :: a -> a -> a max, min ( < ), ( <= ), ( > ), ( >= ) :: a -> a -> Bool compare where ( Eq a) => Ord a class Objectives Type Classes The Ord Typeclass :: a -> a -> Ordering compare x y = if x == y then EQ x >= y = case compare x y of { LT -> False ; _ -> True }

  12. class Show a where show :: a instance Show Foo where -- one way ... deriving ( Show , Eq ) -- other way ... instance Show Foo where show ( Foo i) = "Foo " ++ show i Objectives Type Classes The Show Typeclass -> String data Foo = Foo Int

  13. {-# LANGUAGE ViewPatterns #-} import Data.List instance Read Foo where *Main> let x = "Foo 10" *Main> read it :: Foo Foo 10 Objectives Type Classes The Read Typeclass read (stripPrefix "Foo " -> Just i) = Foo (read i) ◮ Sample run ...

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend