What is going on with the types in this ghci session?

北城余情 提交于 2019-11-27 06:36:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/11003535/what-is-going-on-with-the-types-in-this-ghci-session

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!