functors from partially applied function type

微笑、不失礼 提交于 2021-02-10 10:10:34

问题


there is a quesion in Programming in Haskell that says: Complete the following declaration:

instance Functor ((->) a) where

Now as Functor Thing has a type definition of:

instance Functor Thing where    
   --fmap::(a -> b) -> Thing a -> Thing b 

I was wondering if this reduction makes sense:

instance Functor ((->) a) where
    -- fmap::(a -> b) -> ((->) a) a -> ((->) a) b 
    -- therefore
    -- fmap::(a -> b) -> a -> a -> (a -> b) 
    -- therefore
    -- fmap::b -> b

-- update --- I missed brackets, it should have been

instance Functor ((->) a) where
    -- fmap::(a -> b) -> ((->) a) a -> ((->) a) b 
    -- therefore
    -- fmap::(a -> b) -> (a -> a) -> (a -> b) 
    -- therefore
    -- I should be returning a function of a -> b

回答1:


No, because the a in your instance declaration is not the same a as the one in the type of fmap. You need to assign a type variable in your instance declaration that avoids "capturing" the a in the type of fmap:

instance Functor ((->) r) where
  fmap :: (a -> b) -> (r -> a) -> (r -> b)


来源:https://stackoverflow.com/questions/45317467/functors-from-partially-applied-function-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!