07. haskell Membership

Download 07. haskell Membership

Post on 29-Aug-2014

201 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li> Membership Sebastian RettigIn Haskell normally you understand aa function by reading In Haskell normally you understand function by reading Function Name ++ Parameter Types + Result Type. Function Name Parameter Types + Result Type. </li> <li> Functional Programming No Variables Functions only, eventually stored in Modules Behavior do not change, once defined Function called with same parameter calculates always the same result Function definitions (Match Cases) Recursion (Memory) </li> <li> Haskell Features Pure Functional Programming Language Lazy Evaluation Pattern Matching and Guards List Comprehension Type Polymorphism </li> <li> Static Type System type of every expression is known at compile time use operation with not compatible types program wont compile saver code </li> <li> Nice to remember (1) Types: starts with uppercase letter e.g.: Bool Int String [Int] (Bool, Char) Integer </li> <li> Nice to remember (2) Typevariables to define generic types e.g.: maxList :: [a] -&gt; a fst :: (a,b) -&gt; a snd :: (a,b) -&gt; b Typevariables a and b can contain every type (including the same type) </li> <li> Nice to remember (3) GHCi Commands (Interpreter): :t returns the function header (type) :t tail tail :: [a] -&gt; [a] :t 2 == 4 2 == 4 :: Bool :t "HELLO!" "HELLO!" :: [Char] :i returns the function definition (interface) :i tail tail :: [a] -&gt; [a] -- Defined in GHC.List </li> <li> Nice to remember (4) Typeclasses: define properties of the types like an interface Eq can be compared Ord can be ordered (&gt;, =, Float -&gt; Shape moveLeft (Circle x y r) m = (Circle (x-m) y r) moveLeft (Rectangle x1 y1 x2 y2) m = (Rectangle (x1-m) y1 (x2-m) y2) </li> <li> Our own Type (2) and called it in GHCi and got the following Exception: Main&gt; moveLeft (Circle 1 2 3) 2 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 </li> <li> Remember : Static Type System type of every expression is known at compile time use operation with not compatible types program wont compile saver code QUESTION: Would this Code compile? </li> <li> Remember : Static Type System type of every expression is known at compile time use operation with not compatible types program wont compile saver code QUESTION: Would this Code compile? YES! </li> <li> Membership of a Typeclass What happens? GHCi wants to print out (String) the result the Typeclass Show converts a type to String Type must be part of the Typeclass Show two ways to solve this: inherit from existing implementation of types you use implement the specific typeclass functions by yourself </li> <li> Inherit Membership (1) in the last session, we used the simple way and derived the Memberships Show, Eq and Ord from Float Type we are using in our Type: data Shape = Circle Float Float Float | Rectangle Float Float Float Float deriving (Show, Eq, Ord) and we check our new Typeclass memberships with (:i): data Shape = Circle Float Float Float | Rectangle Float Float Float Float -- Defined at type.hs:3:6-10 instance Eq Shape -- Defined at type.hs:3:91-92 instance Ord Shape -- Defined at type.hs:3:95-97 instance Show Shape -- Defined at type.hs:3:85-88 </li> <li> Inherit Membership (2) and we can now use: maxList :: (Ord a) =&gt; [a] -&gt; a maxList [(Circle 1 2 5), (Circle 2 3 4)] returns: Circle 2.0 3.0 4.0 Ord, Eq &amp; Show implementation of Float works BUT: the result is not correct, why? Ord, Eq Implementation of Float compares only two values the first value, if equal second, if equal third but we need to compare the third value (radius) only </li> <li> Implement Membership (1) to solve this, we have to implement Ord and Eq by our own first, we have to find the functions of the typeclass to implement : :i Eq class Eq a where (==) :: a -&gt; a -&gt; Bool (/=) :: a -&gt; a -&gt; Bool -- Defined in GHC.Classes if we look in prelude.hs, we can see the implementation of these functions </li> <li> Implement Membership (2) class keyword defines a new typeclass class Eq a where (==) :: a -&gt; a -&gt; Bool (/=) :: a -&gt; a -&gt; Bool x == y = not (x /= y) x /= y = not (x == y) a is the type variable we have to implement the membership of Eq for our Type Shape </li> <li> Implement Membership (3) instance keyword defines a membership instance instance Eq Shape where (Circle _ _ r) == (Circle _ _ r) = r == r (Rectangle x1 y1 x2 y2) == (Rectangle x1 y1 x2 y2) = (x1 - x2) == (x1 - x2) &amp;&amp; (y1 - y2) == (y1 - y2) _ == _ = False we dont need to implement (/=), why? </li> <li> Implement Membership (4) and for the Ord Typeclass: :i Ord class Eq a =&gt; Ord a where compare :: a -&gt; a -&gt; Ordering ( a -&gt; Bool (&gt;=) :: a -&gt; a -&gt; Bool (&gt;) :: a -&gt; a -&gt; Bool ( a -&gt; Bool max :: a -&gt; a -&gt; a min :: a -&gt; a -&gt; a -- Defined in GHC.Classes </li> <li> Implement Membership (5) instance Ord Shape where a = surface b a &lt; b = not (a &gt;= b) a &gt; b = not (a :t Just "Hey" Just "Hey" :: Maybe [Char] Question: What is the result of: :t Just 6 :t Nothing </li> <li> Type Parameters (2) type parameter can also contain typeclass cntraints e.g.: data (Ord a) =&gt; Maybe a = Nothing | Just a BUT please avoid such design! every function must use this constraints even if the do not ord anything better to set constraints in every function header, where you need the constraint max :: (Ord a) =&gt; a -&gt; a -&gt; a </li> <li> Record Syntax for a better structure of your type data Car = Car { company :: String , model :: String , year :: String } deriving (Show) instead of: data Car = Car String String String deriving (Show) </li> <li> Record Syntax (2) to use in code: ghci&gt; Car {company="Ford", model="Mustang", year=1967} result: Car {company = "Ford", model = "Mustang", year = 1967} can also contain type parameters: data Car a b c = Car { company :: a , model :: b , year :: c } deriving (Show) </li> <li> Type Synonyms alias for a defined type for better reading and context understanding to define Type Synonym, use the type keyword type String = [Char] type Name = String type Phonenumber = String type Phonebook = [(Name, Phonenumber)] can also contain parameters type IntMap v = Map Int v </li> <li> Sources[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15)[2] The Hugs User-Manual ( http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15) </li> </ul>