Why does Haskell's “flip id” has this type?

后端 未结 1 1307
耶瑟儿~
耶瑟儿~ 2020-12-13 23:31

I\'m curious about the expression flip id (It\'s not homework: I found it in the getOpt documentation).

I wonder why it has this type:

相关标签:
1条回答
  • 2020-12-14 00:17

    The id function has this type:

    id :: a -> a
    

    You get an instance of this type, when you replace a by a -> b:

    id :: (a -> b) -> (a -> b)
    

    which, because of currying, is the same as:

    id :: (a -> b) -> a -> b
    

    Now apply flip to this and you get:

    flip id :: a -> (a -> b) -> b
    

    In the case of id (+) the instance is:

    id :: (Num a) => (a -> a) -> (a -> a)
    

    Now flip id gives you:

    flip id :: (Num a) => a -> (a -> a) -> a
    

    Side note: This also shows you how ($) is the same as id, just with a more restricted type:

    ($) :: (a -> b) -> a -> b
    ($) f x = f x
    -- unpoint:
    ($) f   = f
    -- hence:
    ($)     = id
    
    0 讨论(0)
提交回复
热议问题