3

I was told that every function in Haskell can have only one formal parameter, and defining a function with multiple parameters is just a syntactic sugar. Does that mean a multi-parameter function can be desugared into a nested lambda expression without losing or gaining any property?

I assumed that the answer would be yes. When I actually tried with the two functions below, however, I encountered something that looks mysterious to noob's eyes.

-- aFile.hs

f x y = x + y

g = \x -> \y -> x + y

I defined f and g in a file both without type annotations, expecting the type of both functions will be Num a => a -> a -> a. The actual result, however, was puzzling:

Prelude> :load aFile.hs
Ok, modules loaded: Main.

*Main> :type f
f :: Num a => a -> a -> a

*Main> :type g
g :: Integer -> Integer -> Integer

*Main> let h = \x -> \y -> x + y

*Main> :type h
h :: Num a => a -> a -> a

Somehow g is not polymorphic. On what ground Integer is inferred here? Does this result mean that the multi-parameter function definition is not a syntactic sugar?

Even more confusingly, h, which is the same as g but entered directly into the REPL, has the expected polymorphic type. How come g and h are different?

Donghui Kim
  • 31
  • 1
  • 4
  • On the topic of the DMR, I would suggest that you use a modern version of haskell if you continue exploring haskell. The behavior you are using only happens in very, very old and outdated versions of ghc, and a lot of questions you might have about it might be pretty outdated, too :) a lot of work has also been done over the past several years to fix things that might be "mysterious to noob's eyes", too. – Justin L. May 08 '16 at 04:44
  • The other piece in this puzzle is (I think) that your version of `ghci` implicitly uses `:set -XNoMonomorphjsmRestriction` (as recent ghci's do) but that your `ghc` is using it. This explains the difference between `g` and `h` You can investigate the behavior by toggling between `{-#LANGUAGE MonomorphismRestriction#-}` and `{-#LANGUAGE NoMonomorphismRestriction#-}` in your file, and `:set -XMonomorphismRestriction` in ghci – Michael May 08 '16 at 04:49
  • 1
    @JustinL. But I thought I'm using the latest version.. :) Isn't this modern enough? _The Glorious Glasgow Haskell Compilation System, version 7.10.3_ What does DMR stand for, by the way? – Donghui Kim May 08 '16 at 05:43
  • You were right, @Michael. ghci was implicitly setting _NoMonomorphismRestriction_. Thanks so much! – Donghui Kim May 08 '16 at 06:02
  • @dhK ah, my mistake, i misread your question :) I thought you were seeing the DMR on for ghci, but it looks like it came from a source file instead. my apologies! – Justin L. May 08 '16 at 09:15

0 Answers0