什么= =>标志在Haskell中的意思? [英] What does the => sign mean in Haskell?

查看:122
本文介绍了什么= =>标志在Haskell中的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我无法在任何地方找到答案。我试着用谷歌搜索Haskell等号箭头,我没有得到任何结果。假设我们有以下功能:

  sendMessage :: MonadM e m =>消息 - > m()
sendMessage message = do
mClient< - getMessageClient
liftIO $ send mClient message

e和m在哪里被使用?它们被传递给Message对象(函数?)然后作为单一类型输出,m()?



我认为这不会有帮助,因为我

解决方案

首先:如果你想知道这样和那样的操作符不要问StackOverflow,问问 Hayoo



但事实上Hayoo对于 => 没有用处,因为与Haskell中的其他几乎不同,这是内置语法,而不是

您的签名

  sendMessage: :MonadM em =>消息 - > m()

表示如下: sendMessage 消息 - > m(),其中 m 可以是任何具有 MonadM 类型的类。

这可能对你没有多大帮助,因为实际上 MonadM 是一个相当复杂的类型类。最好考虑一个更简单的例子:

  sum :: Num n => [n]  - > n 

这意味着:和的类型是 [n] - > n ,其中 n 可以是任何数字类型,它是 Num 类,即类型支持减法,乘法,明显 addition 等。实际上,语法是

  sum ::∀n的简写。 Num n => [n]  - > n 

表示所有 类型 n 符合约束 Num n ,函数 sum 具有签名 [n] - > n



您可以使用任何具体的数字类型实例化这样的多态函数:例如

  sum :: [Int]  - >在你的例子中,你可能将它实例化为类似于



  sendMessage :: Message  - > MessageT IO()


For some reason I can't find the answer to this anywhere. I tried Googling "Haskell equal sign arrow" and I'm not getting any results. Let's say we have the following function:

sendMessage :: MonadM e m => Message -> m ()
sendMessage message = do
    mClient  <- getMessageClient
    liftIO $ send mClient message

Where exactly are e and m getting used? Are they being passed into the Message object (function?) and then outputted as a single type, m ()?

I don't think it helps that I'm very new to Haskell, but any help is appreciated here.

解决方案

First off: if you want to know what such and such operator does, don't ask StackOverflow, ask Hayoo!

But in fact Hayoo is no use for => in particular because, unlike almost everything else in Haskell, this is built-in syntax and not an operator that's defined in some library.

Your signature

sendMessage :: MonadM e m => Message -> m ()

means the following: the type of sendMessage is Message -> m (), where m can be any monad that has an instance of the MonadM type class.

That probably doesn't help you much, because in fact MonadM is a rather involved type class. Better consider a simpler example:

sum :: Num n => [n] -> n

This means: the type of sum is [n] -> n, where n can be any number type that's an instance of the Num class, i.e. the class of types supporting subtraction, multiplication, obviously addition etc.. Actually the syntax is shorthand for

sum :: ∀ n . Num n => [n] -> n

meaning that for all types n which fulfill the constraint Num n, the function sum has the signature [n] -> n.

You can instantiate such a polymorphic function with any concrete number type: e.g.

sum :: [Int] -> Int

In your example you'd probably instantiate it to something like

sendMessage :: Message -> MessageT IO ()

这篇关于什么= =&gt;标志在Haskell中的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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