Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 在ghci中调用泛型函数的结果时显示_Haskell_Typeclass - Fatal编程技术网

Haskell 在ghci中调用泛型函数的结果时显示

Haskell 在ghci中调用泛型函数的结果时显示,haskell,typeclass,Haskell,Typeclass,当使用特定类型类的函数,但不指定所需的具体类型时,我对GHCI中的这一点有点困惑。考虑下面的代码: pure (1+) <*> pure 1 > 2 好的,很公平,这些类型看起来很合理,但是从来没有指定任何类型类实例,那么当我编写pure/时,GHCI/Haskell如何知道调用什么函数呢 其他语言的直觉告诉我们,这应该是一个错误。有点像在OOP语言中尝试静态调用实例方法(显然不一样,但我有这种感觉) 这是怎么回事?这是因为ghci有两个特点: ,它将Num b=>b解析为I

当使用特定类型类的函数,但不指定所需的具体类型时,我对GHCI中的这一点有点困惑。考虑下面的代码:

pure (1+) <*> pure 1
> 2
好的,很公平,这些类型看起来很合理,但是从来没有指定任何类型类实例,那么当我编写
pure
/
时,GHCI/Haskell如何知道调用什么函数呢

其他语言的直觉告诉我们,这应该是一个错误。有点像在OOP语言中尝试静态调用实例方法(显然不一样,但我有这种感觉)


这是怎么回事?

这是因为ghci有两个特点:

  • ,它将
    Num b=>b
    解析为
    Integer
    (请注意
    1
    实际上是
    from Integer 1
    ,您可以定义(但不推荐)一些数值数据类型,其中
    from Integer 1+from Integer 1==k
    显示k==“3”
    ,因此这很重要):
  • 整个ghci在IO monad中运行,因此
    Applicative f=>f
    被解析为
    IO
    。如果表达式的类型为
    C1 f=>f a
    ,并且
    IO
    不是该类型类的实例
    C1
    ,则ghci将引发歧义错误

  • 这是由于ghci的两个特点:

  • ,它将
    Num b=>b
    解析为
    Integer
    (请注意
    1
    实际上是
    from Integer 1
    ,您可以定义(但不推荐)一些数值数据类型,其中
    from Integer 1+from Integer 1==k
    显示k==“3”
    ,因此这很重要):
  • 整个ghci在IO monad中运行,因此
    Applicative f=>f
    被解析为
    IO
    。如果表达式的类型为
    C1 f=>f a
    ,并且
    IO
    不是该类型类的实例
    C1
    ,则ghci将引发歧义错误

  • 这是默认类型。当ghci在IO monad中运行时,
    f
    也解析为
    IO
    。谢谢!你介意发布一个答案让我接受吗?这是默认类型。当ghci在IO monad中运行时,
    f
    也解析为
    IO
    。谢谢!你介意把答案贴出来让我接受吗?
    pure (1+) <*> pure 1 :: (Num b, Applicative f) => f b