Why can I not make a (Functor f) => ConcreteType -> f String into an instance Functor ((->) ConcreteType)?

85 views Asked by At

The way I have my types constructed, I believe this would follow the Functor law which states that there should be an identity function for which fmap returns the original functor.

Code:

-- apply a style function to a shell prompt functor
-- e.g.
-- bold & fgColor red `style` gitCurrentBranch
style :: (String -> ShellPromptType -> String) -> ShellPromptSegment String
           -> ShellPromptType -> ShellPromptSegment String
style f segment = \shType -> (flip f) shType <$> segment

-- this is fine
style' :: (String -> ShellPromptType -> String) 
            -> (ShellPromptType -> ShellPromptSegment String)
            -> ShellPromptType -> ShellPromptSegment String
style' f makeSegment = flip f >>= \g shellType -> fmap g $ makeSegment shellType

-- this apparently is not.  Compiler complains that it wants the type (String -> String) -> ShellPromptType -> b
-- for my lambda function there, but it gets (String -> String) -> ShellPromptType -> ShellPromptSegment String
-- instead.  I guess 'b' is not allowed to be a functor?
instance Functor ((->) ShellPromptType) where
  fmap f makeSegment = ((flip f) :: ShellPromptType -> String -> String)
                        >>= ((\g shellType -> fmap g $ makeSegment shellType) 
                              :: (String -> String) -> ShellPromptType -> (ShellPromptSegment String))

Error message:

LambdaLine/Shells/ShellPromptSegment.hs|81 col 30 error| Couldn't match type `ShellPromptType -> String'
||               with `ShellPromptSegment String'
|| Expected type: (String -> String) -> ShellPromptType -> b
||   Actual type: (String -> String)
||                -> ShellPromptType -> ShellPromptSegment String
|| In the second argument of `(>>=)', namely
||   `((\ g shellType -> fmap g $ makeSegment shellType) ::
||       (String -> String)
||       -> ShellPromptType -> (ShellPromptSegment String))'
|| In the expression:
||   ((flip f) :: ShellPromptType -> String -> String)
||   >>=
||     ((\ g shellType -> fmap g $ makeSegment shellType) ::
||        (String -> String)
||        -> ShellPromptType -> (ShellPromptSegment String))
|| In an equation for `fmap':
||     fmap f makeSegment
||       = ((flip f) :: ShellPromptType -> String -> String)
||         >>=
||           ((\ g shellType -> fmap g $ makeSegment shellType) ::
||              (String -> String)
||              -> ShellPromptType -> (ShellPromptSegment String))
LambdaLine/Shells/ShellPromptSegment.hs|81 col 56 error| Couldn't match type `[Char]' with `ShellPromptSegment String'
|| Expected type: ShellPromptSegment String
||   Actual type: a
|| In the return type of a call of `makeSegment'
|| In the second argument of `($)', namely `makeSegment shellType'
|| In the expression: fmap g $ makeSegment shellType
2

There are 2 answers

0
Silvio Mayolo On BEST ANSWER

You've overspecialized.

The definition of a functor is as follows:

class Functor f where
    fmap :: (a -> b) -> f a -> f b

The idea is that it takes a normal function and raises it into some context. But it's more than that: the idea is that it takes any normal function and raises it into the context. For the list functor, fmap can take any function and perform it over a list of the appropriate types.

What you've done is always return the same type out of your functor, which defeats the purpose of it being a functor and thus is not allowed by Haskell.

1
Ilmo Euro On

Consider the definition of Functor:

class Functor f where
    fmap :: (a -> b) -> f a -> f b

The class or the method doesn't constrain the types a and b, so any fmap you define must be applicable to any types a and b. You could define your own type class, e.g.:

class MyFunctor f where
    myfmap :: (String -> String) -> f String -> f String

but that wouldn't be Functor.