ndmitchell / hlint

Haskell source code suggestions
Other
1.46k stars 195 forks source link

Incorrect eta-reduction suggestion in the presence of RankNTypes #514

Open RyanGlScott opened 6 years ago

RyanGlScott commented 6 years ago

hlint warns about the following code:

{-# LANGUAGE RankNTypes #-}
module Foo where

type A = forall a. a -> a

f :: A -> ()
f x = undefined x
$ hlint Foo.hs 
Foo.hs:7:1: Warning: Eta reduce
Found:
  f x = undefined x
Perhaps:
  f = undefined

1 hint

This suggestion is incorrect, however, since using f = undefined will cause the code to no longer typecheck:

$ /opt/ghc/8.4.3/bin/ghc Foo.hs 
[1 of 1] Compiling Foo              ( Foo.hs, Foo.o )

Foo.hs:7:5: error:
    • Cannot instantiate unification variable ‘a0’
      with a type involving foralls: A -> ()
        GHC doesn't yet support impredicative polymorphism
    • In the expression: undefined
      In an equation for ‘f’: f = undefined
  |
7 | f = undefined
  |     ^^^^^^^^^
ndmitchell commented 6 years ago

GHC should really get around to supporting impredicative polymorphism :)

To work around it you can name the variable x as mr, for monomorphism-restriction, which HLint knows is special. I refer to rank2 in https://github.com/ndmitchell/hlint#bugs-and-limitations, but I guess I need to widen what it says.