Reputation: 9825
What is the best way to remove ad-hoc polymorshism in haskell ?
80% of the time, I don't need fmap
to be polymorphic in Functor f
, I actually know which instance I apply it to. Replacing it with a specific instance gives me :
What is the best way to apply, like in category theory, a functor F to a morphism in haskell using its name ?
-- F is a functor : it maps objects of * to objects of *
data F r = Z | Suc r
-- F is a functor : it maps arrows of * to arrows of *
-- generic fmap will be found for this type, I inherit much code for free, great
instance Functor F where
fmap f Z = Z
fmap f (Suc n) = Suc (f n)
-- But I care writing code specific for this functor only
-- Applies F for arrows of *
fmapF = fmap :: (a -> b) -> (F a -> F b)
-- an arrow in *, aka a function -- (and also a value as * is CCC)
f = id :: a -> a
-- works for values F a, not any functor f
r = fmapF f Z :: F a
r' = fmap f "hi" -- as opposed to
Upvotes: 1
Views: 109
Reputation: 116174
I think you actually want
{-# LANGUAGE TypeApplications #-}
r = fmap @ F f Z
The @ F
part specifies that we want the fmap
of functor F
.
Upvotes: 7