90 likes | 142 Views
Explore the concepts of type classes, instances, constraints, superclasses, and built-in classes in functional programming using Haskell. Learn how to utilize domain-specific languages effectively. Connect with Dr. John Peterson from Western State Colorado University to delve into this exciting area of study.
E N D
600.429FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University
Old Homework • http://wiki.western.edu/mcis/index.php/429/Week_1 • Questions?
Type Classes • A type class specifies a set of abstract operations that can be associated with a particular type class Similar a where isSimilar:: a -> a -> Bool notSimilar :: a -> a -> BoolnotSimilar x y = not $ isSimilarx y
Instances An instance associates a type with a class: instance Similar Int where isSimilarx y = abs (x-y) < 2 How is this similar to Java? Different?
Qualifiers When you use a function in a class, this brings the class name into the type signature: find :: (Similar a) => a -> [a] -> Bool find v = not . null . filter (\x -> isSimilar v x)
Constraints in instances instance (Similar a, Similar b) => Similar (a, b) where isSimilar (x1, y1) (x2, y2) = isSimilar x1 x2 || isSimilar y1 y2
Superclasses class Similar a => SuperSim a where reallySimilar :: a -> a -> Bool instance SuperSimInt where reallySimilar x y = isSimilarx y && x < y
Built-in Classes • Off to haskell.org
Derived Instances • Automatic code generation! data Foo = Foo IntInt deriving Show