What is the rule of the order of multiple type variables in haskell?

[亡魂溺海] 提交于 2020-01-03 07:17:13

问题


For example, ParsecT has multiple type variables in its definition.

newtype ParsecT s u m a
    = ParsecT {unParser :: forall b .
                 State s u
              -> (a -> State s u -> ParseError -> m b) 
              -> (ParseError -> m b)                   
              -> (a -> State s u -> ParseError -> m b) 
              -> (ParseError -> m b)                   
              -> m b
             } 

Can we do it like this ?

newtype ParsecT m a s u     -- Only the order of s u m a is changed to m a s u.
    = ParsecT {unParser :: forall b .
                 State s u
              -> (a -> State s u -> ParseError -> m b) 
              -> (ParseError -> m b)                   
              -> (a -> State s u -> ParseError -> m b) 
              -> (ParseError -> m b)                   
              -> m b
             }

I am wondering whether there is a rule or principle about the order of type variables when we define a newtype.


回答1:


In this case, a is last because we want ParsecT s u m __ to be a monad, that way, what our parsers look for can depend on what they found before, and so forth. If u came last we couldn't write

 instance Monad m => Monad (ParsecT s u m) where ...

m is next-to-last because we want ParsecT s u to be a 'monad transformer'

 class MonadTrans t where 
     lift :: m a -> t m a 

 instance MonadTrans (ParsecT s u) where ...

If we put the m first, this instance wouldn't be possible. There doesn't seem to be any similar reason for the ordering of s and u.



来源:https://stackoverflow.com/questions/16860473/what-is-the-rule-of-the-order-of-multiple-type-variables-in-haskell

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