[Haskell-cafe] User-Controlled Defaulting Proposals?

Andrew Gibiansky andrew.gibiansky at gmail.com
Tue Dec 30 17:45:43 UTC 2014

Mostly, I am curious about general efforts in the area.

I personally would like a mechanism by which type class instances may be
used for type inference. Specifically, I wish there were some mechanism by
which I could say, if there is only one instance of a typeclass available,
choose it and proceed with type inference. This is somewhat what defaulting
currently allows. Here is some code that I would like to "emulate"

class FromInt a where
  fromInt :: Int -> a

instance FromInt String where
  fromInt = show

instance FromInt Int where
  fromInt = id

class Go a where
  go :: a -> IO ()

instance Go Int where
  go = print

-- No type signature necessary anywhere
main = go $ fromInt 3

Here, I would like `fromInt 3` to be defaulted to `Int`, as that is the
only possible instance that `Go` allows; once it is defaulted, then type
inference can continue as normal, choosing the appropriate `FromInt`
instance. (Of course, I do not mean for the code as written above to have
backwards incompatible behaviour; instead probably some syntactic changes
would indicate that "Go" is somehow defaultable, etc etc)

Note that this proposal is far from "fleshed out" and I don't have any
suggestions for how Haskell might move in such a direction in the immediate
future; I am more just curious whether work has been done in this area or
what other options may be. Using typeclass instances for type resolution
would allow clever design to get rid of "ambiguous variable" errors, and
allow us to write cleaner code without as many type annotations (which is
what defaulting allows for numeric literals).

-- Andrew

On Tue, Dec 30, 2014 at 8:20 AM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:

> I wish Haskell gave typeclass authors more control over defaulting.
> If you could flesh out your case more, social momentum will build more
> rapidly to push things through.
> What concrete problem are you trying to solve with finer-grained
> defaulting?
> -- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141230/fa1638ea/attachment.html>

More information about the Haskell-Cafe mailing list