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,822
  • 3
  • 20
  • 31
  • 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
  • 46,064
  • 16
  • 89
  • 154
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
  • 136,266
  • 35
  • 360
  • 464