1

I'm learning Haskell and I've been experimenting with partial application. I tried to pertially apply sortBy. I don't undestand the type of the resulting function. And how should it be done properly?

let mf = sortBy compare
:t mf
mf :: [()] -> [()]
jira
  • 3,890
  • 3
  • 22
  • 32
  • possible duplicate of [What is the monomorphism restriction?](http://stackoverflow.com/questions/32496864/what-is-the-monomorphism-restriction) – Bakuriu Sep 10 '15 at 08:35

2 Answers2

8

This is because of the dreaded monomorphism restriction and ghci's defaulting behaviour. This should solve it:

λ> :set -XNoMonomorphismRestriction
λ> import Data.List (sortBy)
λ> let mf = sortBy compare
λ> :t mf
mf :: Ord a => [a] -> [a]

The short story being because of monomorphic restriction, the compiler will try to reduce your function definition to a single type (in your case [()] -> [()]). But without the restriction, you get a polymorphic type (Ord a => [a] -> [a]) as constrained by the typeclass.

Sibi
  • 47,472
  • 16
  • 95
  • 163
4

You're seeing an artifact the monomorphism restriction.

Prelude Data.List> :set -XNoMonomorphismRestriction 
Prelude Data.List> let mf = sortBy compare
Prelude Data.List> :t mf
mf :: Ord a => [a] -> [a]
Don Stewart
  • 137,316
  • 36
  • 365
  • 468