Skip to content

[Chapter 4] Is it possible to have an instance of Functor for "Trap e" of "Secret e a"? #576

@IceAsteroid

Description

@IceAsteroid

Hi, guys, greetings!

This is a very confusing part of typeclass to me.

I can define an instance for "Secret e a", which the "Trap e" is being fixed.

But I can't figure out the way to also define a Functor instance for the first type variable, which is the value "Trap e" of "Secret e a"

For example:

data Secret e a
    = Trap e
    | Reward a
    deriving (Show, Eq)

-- This one for the value "Reward a" works.
-- instance Functor (Secret e) where
--   fmap :: (a -> b) -> Secret e a -> Secret e b
--   fmap _ (Trap a) = Trap a
--   fmap f (Reward a) = Reward (f a)

But how do I possibly have the value "Trap e" also to work for fmap, is it for some reason that such cases are not necessary?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions