这个ghci会话中的类型是怎么回事? [英] What is going on with the types in this ghci session?

查看:60
本文介绍了这个ghci会话中的类型是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,创建一个简单的add函数:

  Prelude> let add xy = x + y 

请注意,它适用于整数和浮点数:

  Prelude>添加3 4 
7
Prelude>添加2.5 1.3
3.8

现在创建一个apply函数。它与 $ (但不是中缀)完全相同。它的作用就像一个没有操作的添加:

  Prelude>让适用f x = f x 
前奏>申请加入3 4
7
Prelude> apply add 2.5 1.3
3.8

好的,现在make add' add'相同,但使用 apply

  Prelude>让add'= apply add 
Prelude>添加'3 4
7
前奏>添加'2.5 1.3

< interactive>:1:9:
在< interactive>的文字'1.3'中没有出现(小数整数)
的实例:1 :9-11
可能的修正:添加一个实例声明(小数整数)
在'add''的第二个参数中,即'1.3'
在表达式中:add'2.5 1.3
在`it'的定义中:it = add'2.5 1.3

Wat。



以下是这些类型:

  Prelude> :t add 
add ::(Num a)=> a - > a - > a
Prelude> :t apply add
apply add ::(Num t)=> t - > t - > t
前奏> :t add'
add':: Integer - >整数 - >整数
前奏>

为什么 add'有不同的类型而不是 apply add



这是ghci的怪胎吗? (和我怎么能区分这个区别?)

解决方案

这是单态限制。当你用一个简单的模式绑定(只有名称,没有任何函数参数)并且没有类型签名来定义一个值时,它会得到一个单态类型。任何类型的变量都会根据违约规则进行消歧 a>,如果不成功,你会得到一个类型错误。

在这种情况下, Num 约束类型变量默认为整数



您可以使用$ b $关闭单态限制b

  ghci> :set -XNoMonomorphismRestriction 

或与 -XnoMonomorphismRestriction 标志。


I'm learning Haskell, and I was playing around in ghci when I came across something very puzzling.

First, create a simple add function:

Prelude> let add x y = x + y

Note that it works with ints and floats:

Prelude> add 3 4
7
Prelude> add 2.5 1.3
3.8

Now create an apply function. It's identical to $ (but not infix). It works like a no-op on add:

Prelude> let apply f x = f x
Prelude> apply add 3 4
7
Prelude> apply add 2.5 1.3
3.8

Ok, now make add' which is the same as add' but using apply:

Prelude> let add' = apply add
Prelude> add' 3 4
7
Prelude> add' 2.5 1.3

<interactive>:1:9:
    No instance for (Fractional Integer)
      arising from the literal `1.3' at <interactive>:1:9-11
    Possible fix: add an instance declaration for (Fractional Integer)
    In the second argument of `add'', namely `1.3'
    In the expression: add' 2.5 1.3
    In the definition of `it': it = add' 2.5 1.3

Wat.

Here are the types:

Prelude> :t add
add :: (Num a) => a -> a -> a
Prelude> :t apply add
apply add :: (Num t) => t -> t -> t
Prelude> :t add'
add' :: Integer -> Integer -> Integer
Prelude> 

Why does add' have a different type than apply add?

Is this a ghci oddity, or is this true in Haskell in general? (And how can I tell the difference?)

解决方案

It's the Monomorphism restriction. When you define a value with a simple pattern binding (just the name, without any function arguments) and without a type signature, it gets a monomorphic type. Any type variables are tried to be disambiguated according to the defaulting rules, if that doesn't succeed you get a type error.

In this case, the Num constrained type variable gets defaulted to Integer.

You can turn off the monomorphism restriction with

ghci> :set -XNoMonomorphismRestriction

or with the -XnoMonomorphismRestriction flag on the command line.

这篇关于这个ghci会话中的类型是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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