[Haskell-cafe] Type inference for lambda function

Richard Eisenberg lists at richarde.dev
Tue Oct 26 13:53:57 UTC 2021



> On Oct 21, 2021, at 2:48 PM, David Feuer <david.feuer at gmail.com> wrote:
> 
> This is the dreaded monomorphism restriction, which is turned on by default in modules but turned off by default in GHCi. Because your function is not "syntactically" a function (i.e., there are no arguments to the left of the = sign), and has no type signature, the type checker insists on it having a monomorphic type. Unless there's a use of it in the module forcing it to a particular type, the defaulting rules come into play, and a variable with a `Num` constraint defaults, by default, to `Integer`.

Indeed -- but I will offer a tiny nuance: the type checker only insists that the type has no constraints, not that it is fully monomorphic. That is, writing `myId = id` will produce a polymorphic myId because there are no constraints on id, while `myPlus = (+)` will be monomorphic because of the constraint on (+).

Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20211026/6fdea18c/attachment.html>


More information about the Haskell-Cafe mailing list