Documente Academic
Documente Profesional
Documente Cultură
Introduction
Introduction
Type classes
Generic programming/structural polymorphism
Declarative programming and bidirectional parsers
Why Care?
Developer productivity
Code readability
Program correctness
Reusability
Type Classes
A Quick Primer
A Quick Primer
We introduce type classes by assosciating a set of methods
with types.
class Num a where
(+), (-), (*) :: a -> a -> a
A Quick Primer
We introduce type classes by assosciating a set of methods
with types.
class Num a where
(+), (-), (*) :: a -> a -> a
A Quick Primer
We introduce type classes by assosciating a set of methods
with types.
class Num a where
(+), (-), (*) :: a -> a -> a
Examples of summarisation
Ordering
If the types have an ordering and bounds, we can compute the
greatest and least elements of a list.
newtype Min a = Min a
newtype Max a = Max a
instance (Ord a, Bounded a) => Monoid (Min a) where
mappend (Min a) (Min b) = Min (min a b)
mempty = Min maxBound
Examples of summarisation
Monoids Compose
If we want to find the maximum and minimum, we can
summarise with two monoids at once:
instance (Monoid a, Monoid b) => Monoid (a, b) where
(a,b) mappend (c, d) =
(mappend a mappend c, mappend b mappend d)
mempty = (mempty, mempty)
> summarise (map (\x -> (Min x, Max x)) [1..10])
(Min 1, Max 10)
f (xn )
f 0 (xn )
f (xn )
f 0 (xn )
I
I
f (xn )
f 0 (xn )
In Haskell!
findZero f = iterate go
where go xn = let (fxn,fxn) = diff f xn
in xn - fxn / fxn
Whats Happening?
data ForwardDouble = FD (Double, Double)
instance Num ForwardDouble where
FD (a,da) + FD (b,db) = FD (a + b, da + db)
FD (a,da) * FD (b,db) = FD (a * b, a*db + b*da)
...
Whats Happening?
data ForwardDouble = FD (Double, Double)
instance Num ForwardDouble where
FD (a,da) + FD (b,db) = FD (a + b, da + db)
FD (a,da) * FD (b,db) = FD (a * b, a*db + b*da)
...
calculatePowerOutput :: Floating a => a -> a -> a -> a
-- We can choose a ~ ForwardDouble
calculatePowerOutput
:: ForwardDouble
-> ForwardDouble
-> ForwardDouble
-> ForwardDouble
Much More
Type classes can do a lot, much more than I can cover in one
talk.
I
I
GHC Generics
GHC Generics
I
I
I
Example derivation
Maybe
data Maybe a = Nothing | Just a
Rep (Maybe a)
:+:
U1
Rec0 a
Rec0 String
:*:
Rep Talk
:*:
Rec0 String
Rec0 String
:*:
:*:
Rec0 String
Rec0 String
Rec0 String
I
I
:*:
Instance resolution
:*:
:*:
Instance resolution
:*:
:*:
Instance resolution
:*:
[("talkAuthor", "Ollie")]
:*:
I
I
I
I
More
I
I
generic-sop
instant-generics
RepLib
Bidirectional Parsing
I
Example: web-routes-boomerang
Example: web-routes-boomerang
Example: web-routes-boomerang
This generates basic combinators for each constructor:
> :t rHome
rHome :: Boomerang e tok r (HackageSiteMap :- r)
Example: web-routes-boomerang
I
I
I
I
Example: web-routes-boomerang
We can parse URLs from strings:
> parse "/"
Home
Example: web-routes-boomerang
We can parse URLs from strings:
> parse "/"
Home
> parse "/author"
parse error at path segment 2, character 0:
unexpected end of input; expecting any string
while parsing ["author"]
Example: web-routes-boomerang
We can parse URLs from strings:
> parse "/"
Home
> parse "/author"
parse error at path segment 2, character 0:
unexpected end of input; expecting any string
while parsing ["author"]
> parse "/author/EdKmett"
AuthorPackages "EdKmett"
Example: web-routes-boomerang
We can parse URLs from strings:
> parse "/"
Home
> parse "/author"
parse error at path segment 2, character 0:
unexpected end of input; expecting any string
while parsing ["author"]
> parse "/author/EdKmett"
AuthorPackages "EdKmett"
Resources
I
I
I