Ticket #6022 (new bug)

Opened 13 months ago

Last modified 8 weeks ago

GHC infers over-general types

Reported by: simonpj Owned by: simonpj
Priority: high Milestone: 7.8.1
Component: Compiler Version: 7.4.1
Keywords: Cc: hackage.haskell.org@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Consider

f x = x + head

GHC will (with no flags) will compile this almost-certainly-wrong program, giving f the type

    f :: forall a. Num ([a] -> a) => ([a] -> a) -> [a] -> a

There's nothing wrong with that type, and in principle someone might later define a suitable instance of Num, but (a) it's not Haskell 98, and (b) it's unlikely to be right, so we've deferred a type error from the definition site of f to the (perhaps much later) call site.

I think GHC should complain, and require a type signature if that's what you really want. This came up on  Stackoverflow

Change History

  Changed 8 months ago by igloo

  • milestone changed from 7.6.1 to 7.6.2

follow-up: ↓ 3   Changed 7 months ago by carter

While this is likely wrong code, it is perfectly reasonable to have vector function num instances! (its not overly general, its just not pedagogically helpful for new people starting in haskell)

eg given Num a , the instance Num (Vector a -> a), has a perfectly natural (and useful) definition. and thus a Num instance for [a]-> a is reasonable too!

however, maybe an alternative approach would be add a suppressable warning for when a type class constraint is used which has no known instances?

here are example "similar ways" to write the same function, and their respective types, in ghci 7.6.1

Prelude> let g x = (+) x $ head Prelude> :t g g :: Num ([a] -> a) => ([a] -> a) -> [a] -> a

Prelude> let h x y = x + head y Prelude> :t h h :: Num a => a -> [a] -> a

Prelude> let q x = (x +) . head Prelude> :t q q :: Num c => c -> [c] -> c

so it seems that the core of the issue in the case of the bad example in this ticket is a syntactical one of making the intended composition. Likewise, I think this isn't a bug, the type checker is giving a type that the user can look at and see is "wrong" for their code!

i think rejecting code with that Num instance constraint would be bad. Unless someone has a counter point, I think this issue should be closed.

in reply to: ↑ 2   Changed 7 months ago by carter

fixed the formatting

Prelude> let g x = (+) x $ head Prelude> :t g

g
Num ([a] -> a) => ([a] -> a) -> [a] -> a

Prelude> let h x y = x + head y

Prelude> :t h

h : Num a => a -> [a] -> a

Prelude> let q x = (x +) . head

Prelude> :t q

q : Num c => c -> [c] -> c

  Changed 7 months ago by maeder

Why is this not Haskell 98? Well, hugs rejects this:

- Cannot infer instance
*** Instance   : Num ([a] -> a)
*** Expression : f

But I don't know why, it works with "hugs -98". I cannot make ghc reject it, so ghc does not support Haskell 98!

In any case, I agree with "carter" that it should not be rejected (by default). If you ignore warnings like "no type signature" it is your own fault!

  Changed 6 months ago by igloo

  • owner set to simonpj
  • milestone changed from 7.6.2 to 7.8.1

  Changed 8 weeks ago by liyang

  • cc hackage.haskell.org@… added
Note: See TracTickets for help on using tickets.