objectives type classes
play

Objectives Type Classes Describe the concept of polymorphism . Dr. - PowerPoint PPT Presentation

} int inc ( int i) { return i + 1.0; double inc ( double i) { } return i + 1; Objectives Type Classes Objectives Type Classes Objectives Type Classes Describe the concept of polymorphism . Dr. Mattox Beckman Show how to declare


  1. } int inc ( int i) { return i + 1.0; double inc ( double i) { } return i + 1; Objectives Type Classes Objectives Type Classes Objectives Type Classes ◮ Describe the concept of polymorphism . Dr. Mattox Beckman ◮ Show how to declare instances of a type class. ◮ Understand the Eq , Ord , Show , and Read type classes. University of Illinois at Urbana-Champaign Department of Computer Science Objectives Type Classes Objectives Type Classes Polymorphism Overloading ◮ 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

  2. In an equation for `it' : it = x == y not (x /= y) -- Minimal complete definition: -- (==) or (/=) x /= y = not (x == y) x == y = data Foo = Foo Int Eq a 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 where ( == ), ( /= ) :: a -> a -> Bool class public class List< E > { public class Shape { public int loc_x , loc_y ; } | Nil } } public List < E > next ; public E data ; Objectives Type Classes Objectives Type Classes Inheritance Parametric Polymorphism public class Square extends Shape { data Cons a = Cons a ( Cons a) public int width , height ; Objectives Type Classes Objectives Type Classes The Eq Type Class Using Eq ◮ If you try to compare these ...

  3. show ( Foo i) = "Foo " ++ show i :: a -> a -> a ( Eq a) => Ord a where compare :: a -> a -> Ordering ( < ), ( <= ), ( > ), ( >= ) :: a -> a -> Bool max, min max x y = if x <= y then y else x min x y = if x <= y then x else y else if x <= y then LT else GT x < y = case compare x y of { LT -> True ; _ -> False } x <= y = case compare x y of { GT -> False ; _ -> True } class class _ -> False } *Main> x == y instance Show Foo where instance Eq Foo where ( == ) ( Foo i) ( Foo j) = i == j -- other way ... *Main> let x = Foo 10 *Main> let y = Foo 10 True Show a deriving ( Show , Eq ) -- one way ... instance Show Foo where :: a show where deriving Eq x > Objectives Type Classes Objectives Type Classes Use an Instance tl;dc ◮ Too long! Didn’t Code! ◮ Let Haskell do the work. ◮ Now if you try to compare these ... data Foo = Foo Int Objectives Type Classes Objectives Type Classes The Ord Typeclass The Show Typeclass -> String compare x y = if x == y then EQ data Foo = Foo Int y = case compare x y of { GT -> True ; x >= y = case compare x y of { LT -> False ; _ -> True }

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

Recommend


More recommend