Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  MultiParamTypeClasses confusion (Derek McLoughlin)
   2. Re:  MultiParamTypeClasses confusion (Brandon Allbery)
   3. Re:  MultiParamTypeClasses confusion (Derek McLoughlin)
   4.  Questions too basic for this mailing list? (Kim-Ee Yeoh)


----------------------------------------------------------------------

Message: 1
Date: Mon, 10 Mar 2014 12:59:04 +0000
From: Derek McLoughlin <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] MultiParamTypeClasses confusion
Message-ID:
        <CAAw9fm=icejvp5lkw-6ie8ctr_vr0gcwv073l0logfkvrfx...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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.


------------------------------

Message: 2
Date: Mon, 10 Mar 2014 09:12:13 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] MultiParamTypeClasses confusion
Message-ID:
        <cakfcl4uyuzyutq050wt7y5dk8tdo1kjlmvo60-gkstaivqd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Mar 10, 2014 at 8:59 AM, Derek McLoughlin <
[email protected]> 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
[email protected]                                  [email protected]
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-0001.html>

------------------------------

Message: 3
Date: Mon, 10 Mar 2014 13:21:52 +0000
From: Derek McLoughlin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] MultiParamTypeClasses confusion
Message-ID:
        <CAAw9fmm8gn=dx5ayzdmdpwsozvxjfzqbrmejszusqts0jsn...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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 <[email protected]> wrote:
> On Mon, Mar 10, 2014 at 8:59 AM, Derek McLoughlin
> <[email protected]> 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
> [email protected]                                  [email protected]
> unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 4
Date: Tue, 11 Mar 2014 17:04:37 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Questions too basic for this mailing
        list?
Message-ID:
        <capy+zdqs_qs1t8owhzm5s+hrycfwvzcgu84yt_k3dhzz8dr...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

One of the attributes of the haskell community that I take pride in is the
balance between democracy and upholding standards.

High technical standards such as correctness, ease-of-reasoning, brevity
are valued, as well as high community standards such as humor, empathy,
addressing the other with dignity.

All the above, coupled with an openness to the world-at-large, is the kit
and kaboodle of the accidental haskeller.

So when the response to a newcomer boils down to "take your basic questions
elsewhere", I wonder about these values and I wonder about the community's
openness.

I for one regret losing the opportunity of exploring very interesting
questions about syntax, even elementary syntax, a common obstacle faced by
many.

Consider how Haskell syntax was designed in a rare confluence of events
requiring tremendous buy-in (and patience, and empathy, and openness) from
a superficially similar but actually a very diverse group with vigorous,
opposing opinions.

In an environment that turns inimical to such basic, foundational values --
whither progress?

I feel the loss deeply.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140311/06d6be7e/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 69, Issue 15
*****************************************

Reply via email to