data Id a = Id a
data Const a b = Const a
the functor instance of above is
instance Functor Id where
fmap f (Id x ) = Id (f x )
instance Functor (Const a) where
fmap f (Const x ) = Const x
f
somehow did not apply in the const
, const (fx)
I am confusing about how it work because at least it involves one variable.
Suppose we have f :: Bool -> Int
and
x :: Const String Bool
x = Const "some string here"
Now, fmap fx
must have type Const String Int
, and the only reasonable choice for its result y
is
y :: Const String Int
y = Const "some string here"
Note how x
and y
are roughly the same value, but they belong to distinct types. Further, in order to compute y
we do not have to use f
in any way, since x
does not have any Bool
inside, and y
does not have any Int
inside. f
is then irrelevant.
Note that type Const ab
is isomorphic to a
, whatever type b
is. That's why it is named Const
: it does not really use its second argument. Its second argument still matters since it causes Const ab
and Const a b'
to be distinct types, even if both are isomorphic to a
.
Const
instance just ignores the f
function and doesn't change its value. Id
instance applies f
function to its value and returns new Id
with new value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.