160 likes | 287 Views
600.429 FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES. Dr. John Peterson Western State Colorado University. Coming Up. Nov 26: Haskell Extensions Nov 28: Work Day Dec 3: Presentation #2 Dec 4: Paul Hudak’s talk @ 10:45
E N D
600.429FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University
Coming Up • Nov 26: Haskell Extensions • Nov 28: Work Day • Dec 3: Presentation #2 • Dec 4: Paul Hudak’s talk @ 10:45 • Dec 5: Presentation #2, Final turnin requirements, work day • Dec 13: Department seminar on reactive programming @ 10:45 • Dec 13: Final exam – detailed presentations
Presentation #2 This presentation will consist of a demo + a code presentation. Show us how you’re using Haskell. This will be an individual effort. Your presentation should be about 6 minutes and demonstrate something useful that you are using in your project. Presentation order will be posted soon!
Presentation 2 Topics • How to use Haskell to solve specific problems you have in your system • Haskell libraries of general interest • Features of Haskell that we haven’t covered (includes non-standard language extensions)
Your Presentation • Explain what problem you want to solve • Show and run code that deals with this problem – comment the code as needed • Create a wiki page with your presentation in it that will help others understand your code – use this are your slides • Take questions and be able to edit your demos
Type Families We have been working with single parameter type classes: Eq, Ord, Num, … What about operators that need to vary more than one type? (==) :: Eq a => a -> a -> Bool (+) :: Num a => a -> a -> a You can use two different type classes at once: foo :: (Num a, Eq b) => a -> a -> b But what if a and b are somehow related?
Type Families Recall our reactive engine: we defined > as (>*) :: Ord a => Behavior a -> Behavior a -> Behavior Bool. Why couldn’t we just use > instead? Note that you can use the module system to hide the “bad” version of > and redefine it for behaviors – what’s wrong with this?
A Different Ord Class class Ordable a where type Res a -- New stuff! (>) :: a -> a –> Res a The type synonym here is new – it defines a type which is local to a particular instance of Ordable instead of general to the program. This is a type synonym – it just gives a new name to an existing type
An Ord Instance instance OrdableInt where type Res Int = Bool (>) = (Prelude.>) This says that when comparing Int value, the result will be a boolean. Note that I needed to refer to the old definition of >.
Behavioral Ord instance Ord a => Ordable(Behavior a) where type Res (Behavior a) = Behavior Bool (>) = lift2 (Prelude.>) In this case, the result type is a behavior. Demo time!
Phantom Types What’s a phantom type? A type variable that doesn’t correspond to an actual value: data Phantom a = Phantom You can use phantom types to keep track of information about data. data List a b = List [a] x :: List Int Size2 map :: (a -> b) -> List a s -> List b s
Existential Types Type variables in Haskell normally are used with an implicit quantifier: for all. That is, data Tuple a b = Tuple a b works for all types a and b. When you add class constraints, you limit the type sets but this set is determined from “outside” the type.
Using Existentials A data type that captures a single showable value: data Showable = forall a. Show a => Showable a How is this different from data Show a => Showable a = Showable a
Existentials When you pattern match on a constructor that uses existentials, you get to use methods in the context: f :: Showable -> String f (Showable a) = show a
GADTs and DSLs Why are GADTs good for DSLs? We can use the phantom type to keep track of information about data. Example!
Quick Check http://wiki.western.edu/mcis/index.php/429/Week_6