Transforming a function to point-free style changes its type

后端 未结 3 1175
独厮守ぢ
独厮守ぢ 2021-02-19 04:35

I\'m beginning Haskell... I tried to write the following trivial function in two different ways, letting Haskell decide the types, and the type system does something different i

3条回答
  •  攒了一身酷
    2021-02-19 05:21

    This is known as the monomorphism restriction.

    Basically, it means that top-level bindings that look like x = are forced to be non-polymorphic, unless you specify a type signature. Bindings with arguments, i.e. f x = are not affected. See the link for details as to why this restriction exists.

    Usually, you get an error message when the restriction is applied, but in this case GHCi is able to use type defaulting to change the type Num a => a to Integer.

    The easiest way to dodge it is to either use an explicit type signature, or put

    {-# LANGUAGE NoMonomorphismRestriction #-}
    

    at the top of your module, or run GHCi with -XNoMonomorphismRestriction.

提交回复
热议问题