Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not

Download Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not

Post on 14-Dec-2015

213 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

<ul><li>Slide 1</li></ul> <p>Haskell Chapter 7 Slide 2 Topics Defining data types Exporting types Type parameters Derived instances Type synonyms Either Type classes Not covered record syntax recursive data structures subclassing parameterized types as instances of type classes Yes-No type class (emulate JavaScript-like behavior) Functor type class Slide 3 Define a new data type data Bool = False | True data keyword indicates a new data type (Bool is not new, just an example) False and True are value constructors | is or Bool can be False or True Type name and value constructors must start with capital letter Value constructors are actually functions that return a value of a data type Slide 4 Shape example -- Circle parms coordinates and radius -- Rectangle parms upper left and lower right coordinates data Shape = Circle Float Float Float | Rectangle Float Float Float Float *Main&gt; :t Circle Circle :: Float -&gt; Float -&gt; Float -&gt; Shape *Main&gt; :t Rectangle Rectangle :: Float -&gt; Float -&gt; Float -&gt; Float -&gt; Shape Slide 5 Shape continued area :: Shape -&gt; Float area (Circle _ _ r) = pi * r ^2 area (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs y2 - y1) Notice the pattern match against constructor *Main&gt; let c = Circle 3 4 5 *Main&gt; area c 78.53982 But we dont know how to show a circle (yet) *Main&gt; c :15:1: No instance for (Show Shape) arising from a use of `print' Possible fix: add an instance declaration for (Show Shape) In a stmt of an interactive GHCi command: print it remember $ is function application Slide 6 Updating shape to be displayed data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show) *Main&gt; let c = Circle 3 4 5 *Main&gt; c Circle 3.0 4.0 5.0 Slide 7 Improving Shape with a Point data type data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) area :: Shape -&gt; Float area (Circle _ r) = pi * r ^2 area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs y2 - y1) *Main&gt; area (Rectangle (Point 0 0) (Point 100 100)) 10000.0 Slide 8 Another method for Shapes nudge :: Shape -&gt; Float -&gt; Float -&gt; Shape nudge (Circle (Point x y) r) dx dy = Circle (Point (x+dx) (y+dy)) r nudge (Rectangle (Point x1 y1) (Point x2 y2)) dx dy = Rectangle (Point (x1+dx) (y1+dy)) (Point (x2+dx) (y2+dy)) *Main&gt; nudge (Circle (Point 34 34) 10) 5 10 Circle (Point 39.0 44.0) 10.0 Slide 9 Exporting shape module Shapes ( Point(..), Shape(..), area, nudge ) where Shape(..) exports all value constructors (in this case Circle and Rectangle). Makes it easy to add more shapes later (OCP) If just export Shape, not Shape(..), cant pattern match (Programming Languages: support for encapsulation) Slide 10 Type Parameters A value constructors takes some parameters, produces a new value (e.g., Circle takes 3 values, returns a circle value) A type constructor takes a type as a parameter, returns a new type Maybe type constructor defined as: data Maybe a = Nothing | Just a Cant have just Maybe needs to be Maybe something Why is this useful? Strong typing. In Java, a function might return a Point or null. This is not possible in Haskell. So if a function may or may not produce a value, then the return type is Maybe something (e.g., Maybe Point) Slide 11 Usage Due to type inference, often dont pass parameters to type constructors explicitly If a value is Just a, Haskell infers type as Maybe Char Concrete type either doesnt take any type parameters, e.g., Int, Bool OR has filled type parameters (Maybe Char) List type takes a parameter, returns concrete type [Int] is an example Slide 12 Just 84 Just 84 *Main&gt; :t Just "Haha" Just "Haha" :: Maybe [Char] *Main&gt; :t Just 84 "&gt;Just 84 Just 84 *Main&gt; :t Just "Haha" Just "Haha" :: Maybe [Char] *Main&gt; :t Just 84 Just 84 :: Num a =&gt; Maybe a *Main&gt; :t Nothing Nothing :: Maybe a *Main&gt; Just 10 :: Maybe Double Just 10.0"&gt;Just 84 Just 84 *Main&gt; :t Just "Haha" Just "Haha" :: Maybe [Char] *Main&gt; :t Just 84 " title="Play with it *Main&gt; Just "Haha" Just "Haha" *Main&gt; Just 84 Just 84 *Main&gt; :t Just "Haha" Just "Haha" :: Maybe [Char] *Main&gt; :t Just 84 "&gt; Play with it *Main&gt; Just "Haha" Just "Haha" *Main&gt; Just 84 Just 84 *Main&gt; :t Just "Haha" Just "Haha" :: Maybe [Char] *Main&gt; :t Just 84 Just 84 :: Num a =&gt; Maybe a *Main&gt; :t Nothing Nothing :: Maybe a *Main&gt; Just 10 :: Maybe Double Just 10.0 Slide 13 Type class \= Java class In Java, we use a class as a blueprint to create objects In Haskell, you use type classes to make a data type, then think about how it can act We do this with deriving If types of all fields are part of a type class, then our new type can be part of a type class data Person = Person { name :: String, age :: Int } deriving (Eq, Show) Slide 14 Example *Shapes&gt; let frodo = Person {name = "Frodo Baggins", age = 43} *Shapes&gt; let bilbo = Person {name = "Bilbo Baggins", age = 100} *Shapes&gt; bilbo Person {name = "Bilbo Baggins", age = 100} *Shapes&gt; bilbo == frodo False *Shapes&gt; let imposter = Person {name = "Frodo Baggins", age = 43} *Shapes&gt; frodo == imposter True Slide 15read "Saturday" :: Day Saturday *Chap7&gt; Saturday == Sunday False *Chap7&gt; Monday `compare` Wednesday LT *Chap7&gt; Saturday &gt; Friday True *Chap7&gt; minBound :: Day Monday *Chap7&gt; maxBound :: Day Sunday *Chap7&gt; let weekend = [Friday.. Sunday] *Chap7&gt; weekend [Friday,Saturday,Sunday]"&gt; Enum example data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Show, Ord, Read, Bounded, Enum) *Chap7&gt; Wednesday Wednesday *Chap7&gt; show Wednesday "Wednesday" *Chap7&gt; read "Saturday" :: Day Saturday *Chap7&gt; Saturday == Sunday False *Chap7&gt; Monday `compare` Wednesday LT *Chap7&gt; Saturday &gt; Friday True *Chap7&gt; minBound :: Day Monday *Chap7&gt; maxBound :: Day Sunday *Chap7&gt; let weekend = [Friday.. Sunday] *Chap7&gt; weekend [Friday,Saturday,Sunday] Slide 16 Type Synonyms [Char] and String are type synonyms type PhoneNumber = String type Name = String type PhoneBook = [(Name, PhoneNumber)] inPhoneBook :: Name -&gt; PhoneNumber -&gt; PhoneBook -&gt; Bool inPhoneBook name pnumber pbook = (name, pnumber) `elem` pbook Not covered: parameterized type synonyms Slide 17:t Right 'a' Right 'a' :: Either a Char *Chap7&gt; :t Left True Left True :: Either Bool b"&gt; Could be Either Use to encapsulate a value of one type or another Is defined as: data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show) *Chap7&gt; Right 20 Right 20 *Chap7&gt; Left "Whoa" Left "Whoa" *Chap7&gt; :t Right 'a' Right 'a' :: Either a Char *Chap7&gt; :t Left True Left True :: Either Bool b Slide 18 Either with try The try functions try :: Exception e =&gt; IO a -&gt; IO (Either e a)SourceExceptionIO EitherSource Similar to catch, but returns an Either result which is (Right a) if no exception of type e was raised, or (Left ex) if an exception of type e was raised and its value is ex. If any other type of exception is raised than it will be propogated up to the next enclosing exception handler.catchEitherRightLeft try a = catch (Right `liftM` a) (return. Left) http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Control-Exception.html#7 Slide 19 Usage Maybe can be used if function might return a result or fail Either can be used if there are multiple possible results Youll explore this more in the homework Slide 20 Type Classes 102 Type classes are sort of like interfaces: they define behavior Types that can behave that way are made instances (just means they can use the functions associated with that type class) class Eq a where (==) :: a -&gt; a -&gt; Bool (/=) :: a -&gt; a -&gt; Bool x == y = not (x /= y) x /= y = not (x == y) note the mutual recursion only need to implement one! Slide 21 Using a type class data TrafficLight = Red | Yellow | Green instance Eq TrafficLight where Red == Red = True Green == Green = True Yellow == Yellow = True _ == _ = False instance Show TrafficLight where show Red = "Red Light" show Green = "Green Light" show Yellow = "Yellow Light" Slide 22 Using the Traffic Light *Chap7&gt; Red Red Light *Chap7&gt; Red Red Light *Chap7&gt; Red == Red True *Chap7&gt; Red == Green False *Chap7&gt; Red `elem` [Red, Green, Yellow] True </p>