简体   繁体   English

Haskell-适用/单实例

[英]Haskell - Applicative/Monad instances

I've seen that the type of pure 1 is (Num a, Applicative f) => fa , which is quite obvious. 我已经看到pure 1的类型是(Num a, Applicative f) => fa ,这很明显。 So if I would like to make it a Maybe Int : 因此,如果我想将其设为Maybe Int

Prelude> pure 1 :: Maybe Int
Just 1

What about this? 那这个呢?

Prelude> pure 1
1
Prelude> return 1
1

What is going on? 到底是怎么回事? Why doesn't it complain about not knowing which instance to choose?? 为什么不抱怨不知道选择哪个实例呢?

Edit 编辑

I think that this behaviour actually has nothing to do with monads or applicatives, but this was the context I came to it... 我认为这种行为实际上与monads或appadatives无关,但这就是我所涉及的上下文...

That's a peculiarity of ghci. 那是ghci的特点。 It has special handling for IO actions, which aren't just “computed” and printed (you can't print an IO action anyway), but executed and the result printed. 它具有对IO操作的特殊处理,这些操作不仅可以“计算”和打印(无论如何也不能打印IO操作),而是可以执行并打印结果 So, 所以,

Prelude> pure 1 :: IO Integer
1

As per behzad.nouri's comment, here's the relevant manual section . 根据behzad.nouri的评论, 这是相关的手册部分


Provided it's IO a with Show a and not a~() , otherwise only the action is executed. 如果提供的IO a Show a而不是a~() ,否则执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM