Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jim-Holmstroem/7e8614b0472277ea1816bccd877ee3af to your computer and use it in GitHub Desktop.
Save Jim-Holmstroem/7e8614b0472277ea1816bccd877ee3af to your computer and use it in GitHub Desktop.
First try (not working): Functor over Functor as Functor
Prelude> data TwoD f g a = TwoD (f (g a)) deriving Show
Prelude> TwoD [Just 3]
TwoD [Just 3]
Prelude> :t TwoD [Just 3]
TwoD [Just 1] :: Num a => TwoD [] Maybe a
Prelude> :{
Prelude| instance (Functor f, Functor g) => Functor (TwoD f g) where
Prelude| fmap o = fmap (fmap o)
Prelude| :}
<interactive>:20:18:
Couldn't match type ‘a’ with ‘f0 a’
‘a’ is a rigid type variable bound by
the type signature for fmap :: (a -> b) -> TwoD f g a -> TwoD f g b
at <interactive>:20:3
Expected type: a -> b
Actual type: f0 a -> f0 b
Relevant bindings include
o :: a -> b (bound at <interactive>:20:8)
fmap :: (a -> b) -> TwoD f g a -> TwoD f g b
(bound at <interactive>:20:3)
In the first argument of ‘fmap’, namely ‘(fmap o)’
In the expression: fmap (fmap o)
<interactive>:20:18:
Couldn't match type ‘b’ with ‘f0 b’
‘b’ is a rigid type variable bound by
the type signature for fmap :: (a -> b) -> TwoD f g a -> TwoD f g b
at <interactive>:20:3
Expected type: a -> b
Actual type: f0 a -> f0 b
Relevant bindings include
o :: a -> b (bound at <interactive>:20:8)
fmap :: (a -> b) -> TwoD f g a -> TwoD f g b
(bound at <interactive>:20:3)
In the first argument of ‘fmap’, namely ‘(fmap o)’
In the expression: fmap (fmap o)
@Jim-Holmstroem
Copy link
Author

I had just forgot the unpacking of TwoD

@Jim-Holmstroem
Copy link
Author

There is also -XDeriveFunctor which does this by deriving (Functor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment