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

后端 未结 1 1184
误落风尘
误落风尘 2020-12-11 00:33

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>          


        
相关标签:
1条回答
  • 2020-12-11 01:07

    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.

    0 讨论(0)
提交回复
热议问题