Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  how do typeclasses work again? (Sylvain Henry)
   2. Re:  how do typeclasses work again? (Nicholls, Mark)


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

Message: 1
Date: Sat, 11 Feb 2017 14:36:36 +0100
From: Sylvain Henry <sylv...@haskus.fr>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] how do typeclasses work again?
Message-ID: <e5792f17-58f7-8c61-11a2-5077ac803...@haskus.fr>
Content-Type: text/plain; charset=utf-8; format=flowed

On 11/02/2017 09:50, Nicholls, Mark wrote:
> I don't understand the difference
>
> forall a isx. (Is isx a, Show a) => isx -> String
> and
> (Is isx a, Show a) => isx -> String
I have used the `forall` explicitly only to fix the order of the type 
parameters (`a` and `isx`) so that we are sure to set the type of `a` 
when we write (using TypeApplications): foo4 @Y (X,Y)

In the second declaration, the `forall` is implicit.

> I think my understanding of type classes is naïve, I just thought it meant 
> that secretly a dictionary was being passed.
Yes your understanding is correct. The issue here is that the compiler 
doesn't know the type of `a`, hence it can't select and pass the 
appropriate instances.

> the compiler would identify the specific dictionary from the call site
Even at call site, the compiler can't infer the `a` type from the `isx` 
type (nor from the return type of `foo4`).

Do you want the `a` type to be dependent on the `isx` type? I.e., to 
only be allowed to define a single `Is isx a` instance for each `isx` type.

--
Sylvain


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

Message: 2
Date: Sun, 12 Feb 2017 10:05:53 +0000
From: "Nicholls, Mark" <nicholls.m...@vimn.com>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <beginners@haskell.org>
Subject: Re: [Haskell-beginners] how do typeclasses work again?
Message-ID:
        
<e7e7fdf32472ff48bb0e4d9dc4283d0e8bd09...@mtvne-exmb02.mtvne.ad.viacom.com>
        
Content-Type: text/plain; charset="windows-1252"

I'm naively capable of messing around with type families...so I know how to 
define the types statically...that's not really what I want...that's too strong.

I think I'm trying to work in the universe of typeclasses and not data 
types....in my OO head these two things overlap (if you see what I mean)....in 
Haskell they are distinct...which I'm beginning to feel makes type inference 
easy....but is actually quite "weak".

so lets start again...

> class Is isx x where
>   apply ::(x -> y) -> isx -> y

> instance Is x x where
>   apply f = f

i.e. lets create our tuple instance like this!

> instance (Is m x) => Is (m,y) x where
>   apply f (m,y) = apply f m 

> data X = X deriving (Show)
> data Y = Y deriving (Show)

> foo4 :: forall a isx. (Is isx a, Show a) => isx -> String
> foo4 = apply (\(i :: a) -> show i)

now...this line said....

> main = print (foo4 @X (X,Y))

and that works!....which I think is what I want....in an OO world this feels 
like a "cast"....where Ive said (X,Y) <: X....I'm getting the compiler to 
extract fst for me...I'm lazy.

so lets tell the compiler it could do snd for me.

> instance (Is m x) => Is (y,m) x where
>   apply f (y,m) = apply f m 

gives....."Duplicate instance declarations "

which is unfortunate as I wanted to then write

> main = print (foo4 @Y (X,Y))

to "cast" to Y....which feels perfectly reasonable

then I look this up on the interweb...and magically found some noob has been 
here before!
"noob “Duplicate instance declarations” (again)"

that noob was me!...about a year ago...and someone said I was misunderstanding 
how to define these sort of recursive structures....and It should be done in 
the class declaration...which doesn't seem to work in this case...as I want to 
do something recursive 

the answer said.....

"Haskell requires that there be only one instance for each class and type. Thus 
is determined only from the part to the right of the =>. "

ok, I buy that...IF I want to guarantee Haskell to automatically derive type 
class dictionaries that functional restriction is perfectly reasonable...as 
long as its resolved at some point.

I tbink (naively)...Haskell is saying...
"if I match things against (x,y) I've got 2 instance declarations....so how do 
I decide which one?"

what I'm saying is....that's fine but I'm telling you which one at the call 
site....using these "@" things...

so what's the problem?...the functional restriction is too restrictive.












________________________________________
From: Beginners [beginners-boun...@haskell.org] on behalf of Sylvain Henry 
[sylv...@haskus.fr]
Sent: 11 February 2017 13:36
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] how do typeclasses work again?

On 11/02/2017 09:50, Nicholls, Mark wrote:
> I don't understand the difference
>
> forall a isx. (Is isx a, Show a) => isx -> String
> and
> (Is isx a, Show a) => isx -> String
I have used the `forall` explicitly only to fix the order of the type
parameters (`a` and `isx`) so that we are sure to set the type of `a`
when we write (using TypeApplications): foo4 @Y (X,Y)

In the second declaration, the `forall` is implicit.

> I think my understanding of type classes is naïve, I just thought it meant 
> that secretly a dictionary was being passed.
Yes your understanding is correct. The issue here is that the compiler
doesn't know the type of `a`, hence it can't select and pass the
appropriate instances.

> the compiler would identify the specific dictionary from the call site
Even at call site, the compiler can't infer the `a` type from the `isx`
type (nor from the return type of `foo4`).

Do you want the `a` type to be dependent on the `isx` type? I.e., to
only be allowed to define a single `Is isx a` instance for each `isx` type.

--
Sylvain
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
CONFIDENTIALITY NOTICE

This e-mail (and any attached files) is confidential and protected by copyright 
(and other intellectual property rights). If you are not the intended recipient 
please e-mail the sender and then delete the email and any attached files 
immediately. Any further use or dissemination is prohibited.

While MTV Networks Europe has taken steps to ensure that this email and any 
attachments are virus free, it is your responsibility to ensure that this 
message and any attachments are virus free and do not affect your systems / 
data.

Communicating by email is not 100% secure and carries risks such as delay, data 
corruption, non-delivery, wrongful interception and unauthorised amendment. If 
you communicate with us by e-mail, you acknowledge and assume these risks, and 
you agree to take appropriate measures to minimise these risks when e-mailing 
us.

MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon 
Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be 
Viacom, Viacom International Media Networks and VIMN and Comedy Central are all 
trading names of MTV Networks Europe.  MTV Networks Europe is a partnership 
between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for 
service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.



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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 104, Issue 8
*****************************************

Reply via email to