Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

operators - What does the => sign mean in Haskell?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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 ()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...