[Haskell-beginners] MultiParamTypeClasses confusion

Brandon Allbery allbery.b at gmail.com
Mon Mar 10 13:12:13 UTC 2014


On Mon, Mar 10, 2014 at 8:59 AM, Derek McLoughlin <
derek.mcloughlin at gmail.com> wrote:

> ghci> distance (x, x) $ toVector (y, y)
> 12.727922061357855
>
> I don't understand why this doesn't work:
>
> ghci> let z = toVector (y, y)
> interactive>:32:9:
>     No instance for (Vectorizable (Double, Double) v0)
>       arising from a use of `toVector'
>     The type variable `v0' is ambiguous
>     Possible fix: add a type signature that fixes these type variable(s)
>     Note: there is a potential instance available:
>       instance Vectorizable (Double, Double) (Double, Double)
>         -- Defined at vector.hs:13:10
>     Possible fix:
>       add an instance declaration for (Vectorizable (Double, Double) v0)
>     In the expression: toVector (y, y)
>     In an equation for `z': z = toVector (y, y)
>
> It seems odd that "toVector" works when used as an argument to
> "distance" but not when used in a let expression.
>
> Can anyone explain?
>

When you use them together, Haskell can infer the correct type for `v0`
from the inferred type of `distance` by applying defaulting: the type of
`sqrt` introduces a constraint that is subject to defaulting. If you
separate them, it can no longer determine a concrete type for your `let`,
as multiparameter type classes are not subject to defaulting (and can't
be). Also, Haskell cannot conclude from the existence of a single possible
instance that it should use that instance: the meaning of your expression
would then change if you imported a module which defined a new instance.

The requirement for a concrete type comes from the monomorphism
restriction, which is applied to your `let` because `z` has no parameters.

http://www.haskell.org/haskellwiki/Monomorphism_restriction

Many people turn off the monomorphism restriction in ghci to avoid this
kind of issue.

    :set -XNoMonomoprhismRestriction

which you can put in ~/.ghci so that it is the default for new ghci
sessions.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140310/779b510e/attachment.html>


More information about the Beginners mailing list