[Haskell-cafe] Increasing Haskell modularity

Dominique Devriese dominique.devriese at cs.kuleuven.be
Fri Oct 3 11:05:05 UTC 2014


2014-10-02 16:21 GMT+02:00 Oleg <oleg at okmij.org>:
> However, given
>
> f2 :: Show a => a -> String
> f2 x =
>   let instance Show Int where show _ = "one"
>   in show x
>
> what should (f2 (1::Int)) return?

I haven't studied this in detail (yet :)), but can't we solve this
coherence problem by requiring a bit of additional type annotations
from the programmer? Specifically, I would expect the problem to
go away if we require the user to explicitly state the resulting type
of the "let instance" expression. The syntax could look like

  let instance Show Int where show _ = "one"
  in show x as Show a => String

where the specification "Show a => String" would imply that the Show a
instance is propagated outwards and the Show Int instance is unused.
If the programmer instead wrote

  let instance Show Int where show _ = "one"
  in show x as String

That would imply that the compiler should try to resolve the required
"Show a" constraint, but I would then expect an error saying that
"Show a" cannot be derived from "Show Int", because we don't apply
unification to constraints (or at least, GHC doesn't, and it seems
like a good idea not to).

Such an additional annotation is a bit more work for the programmer,
but perhaps that might be acceptable for this indeed sorely missed
feature?

Regards,
Dominique


More information about the Haskell-Cafe mailing list