MultiParamTypeClasses confusion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

MultiParamTypeClasses confusion

Derek McLoughlin
Hi,

In Chapter 6 of "Beginning Haskell" by Apress there's a couple of
classes introduced for vectors and things that can be vectorized (not
related to Data.Vector)

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

class Vector v where
    distance :: v -> v -> Double

instance Vector (Double, Double) where
    distance (a,b) (c,d) = sqrt $ (c - a) * (c - a) + (d - b) * (d - b)

class Vectorizable e v where
    toVector :: e -> v

instance Vectorizable (Double, Double) (Double, Double) where
    toVector = id

x = 1.0 :: Double
y = 10.0 :: Double

While I understand how to use the "distance" function:

ghci> distance (x, x) (y, y)
12.727922061357855

... and I can see how "toVector" is used in their code

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?

Derek.

Reply | Threaded
Open this post in threaded view
|

MultiParamTypeClasses confusion

Brandon Allbery
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>

Reply | Threaded
Open this post in threaded view
|

MultiParamTypeClasses confusion

Derek McLoughlin
Thanks for that. I see that I can now use:

ghci> let z  = toVector (y, y) :: (Double, Double)
ghci> z
(10.0,10.0)

On 10 March 2014 13:12, Brandon Allbery <allbery.b at gmail.com> wrote:

> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>