haskell中多个类型变量的顺序是什么? [英] What is the rule of the order of multiple type variables in haskell?

查看:147
本文介绍了haskell中多个类型变量的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,ParsecT在其定义中有多个类型变量。

  newtype ParsecT suma 
= ParsecT {unParser :: forall b。
国家s
- > (a - > State s u - > ParseError - > m b)
- > (ParseError - > m b)
- > (a - > State s u - > ParseError - > m b)
- > (ParseError - > m b)
- > mb
}

我们可以这样做吗?

  newtype ParsecT masu  - 只有suma的顺序变为mas u。 
= ParsecT {unParser :: forall b。
国家s
- > (a - > State s u - > ParseError - > m b)
- > (ParseError - > m b)
- > (a - > State s u - > ParseError - > m b)
- > (ParseError - > m b)
- > mb
}

我想知道是否有关于类型顺序的规则或原则在这种情况下, a 是最后一个因为我们希望 ParsecT sum __ 成为一个monad,那么,我们的解析器查找的内容可能取决于他们之前找到的内容等等。如果 u 最后一次出现,我们就不能写入

  instance Monad m = > Monad(ParsecT sum)where ... 

m 是倒数第二,因为我们希望 ParsecT su 是一个'monad变换器'。

  class MonadTrans t其中
lift :: ma - > tma

实例MonadTrans(ParsecT su)其中...

如果我们首先放置 m ,这个实例是不可能的。对于 s u

$ b的顺序似乎没有任何类似的原因。 $ b

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.

解决方案

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.

这篇关于haskell中多个类型变量的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆