[Haskell-cafe] Type inference doesn't always calculate the most generic type?

21 views
Skip to first unread message

Christophe Delord

unread,
May 28, 2015, 4:54:48 PM5/28/15
to haskel...@haskell.org
Hello,

I'm new here. Sorry if this question has already been asked.

I have noticed something strange about type inference.

Prelude> :t abs
abs :: Num a => a -> a
Prelude> let mapabs = map abs
Prelude> :t mapabs
mapabs :: [Integer] -> [Integer]
Prelude> :t (map abs)
(map abs) :: Num b => [b] -> [b]

I think that "mapabs" and "map abs" should have the same generic type.

In a program I can force the type like this:

mapabs = map abs

mapabs' :: (Num a) => [a] -> [a]
mapabs' = map abs

but mapabs still have the type [Integer] -> [Integer]

Are there any reason for this?

Regards,
Christophe Delord.
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Brandon Allbery

unread,
May 28, 2015, 4:56:16 PM5/28/15
to Christophe Delord, haskell-cafe
On Thu, May 28, 2015 at 4:54 PM, Christophe Delord <has...@cdsoft.fr> wrote:
Prelude> let mapabs = map abs

This is the monomorphism restriction.
https://wiki.haskell.org/Monomorphism_restriction

--
brandon s allbery kf8nh                               sine nomine associates
allb...@gmail.com                                  ball...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

Francesco Ariis

unread,
May 28, 2015, 4:58:13 PM5/28/15
to haskel...@haskell.org
On Thu, May 28, 2015 at 10:54:38PM +0200, Christophe Delord wrote:
> Hello,
>
> I'm new here. Sorry if this question has already been asked.
>
> I have noticed something strange about type inference.
>
> Prelude> :t abs
> abs :: Num a => a -> a
> Prelude> let mapabs = map abs
> Prelude> :t mapabs
> mapabs :: [Integer] -> [Integer]
> Prelude> :t (map abs)
> (map abs) :: Num b => [b] -> [b]

I just fired up ghci:

λ> let mapabs = map abs
λ> :t mapabs
mapabs :: Num b => [b] -> [b]

ghc 7.10.1

Brandon Allbery

unread,
May 28, 2015, 5:00:22 PM5/28/15
to haskell-cafe
On Thu, May 28, 2015 at 4:54 PM, Francesco Ariis <fa...@ariis.it> wrote:
I just fired up ghci:

    λ> let mapabs = map abs
    λ> :t mapabs
    mapabs :: Num b => [b] -> [b]

ghc 7.10.1

Sufficiently recent ghc disables the monomorphism restriction inside ghci (only --- compiled code, including code compiled by ghci, as opposed to code entered at the prompt, still uses it).
Try ":seti".

Tikhon Jelvis

unread,
May 28, 2015, 5:01:56 PM5/28/15
to haskell-cafe
The monomorphism restriction is turned off by default in ghci since version 7.8.1, so you'll see this behavior with newer versions of GHC and the original behavior with older ones.

Christophe Delord

unread,
May 28, 2015, 6:02:13 PM5/28/15
to haskel...@haskell.org
Le Thu, 28 May 2015 14:01:50 -0700,
Tikhon Jelvis <tik...@jelv.is> a écrit :

> The monomorphism restriction is turned off by default in ghci since
> version 7.8.1, so you'll see this behavior with newer versions of GHC
> and the original behavior with older ones.


I'm using an old version (7.6.3) on Debian. I'll try to install a newer
version.

Thank you for all these answers.

Reply all
Reply to author
Forward
0 new messages