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. Re:  Type classes vs Java interfaces (Bob Hutchison)
   2. Re:  Type classes vs Java interfaces (Phil Scott)
   3. Re:  Type classes vs Java interfaces (Bryan Vicknair)
   4.  f . g or f g  or f $ g? (Martin Drautzburg)
   5. Re:  f . g or f g  or f $ g? (Emanuel Koczwara)
   6. Re:  f . g or f g  or f $ g? (Emanuel Koczwara)
   7. Re:  f . g or f g  or f $ g? (Emanuel Koczwara)
   8. Re:  How to interact with a process (Martin Drautzburg)
   9. Re:  How to interact with a process (Martin Drautzburg)


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

Message: 1
Date: Fri, 1 Feb 2013 08:25:43 -0500
From: Bob Hutchison <[email protected]>
Subject: Re: [Haskell-beginners] Type classes vs Java interfaces
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252


On 2013-01-31, at 6:36 PM, Mateusz Kowalczyk <[email protected]> wrote:

> Greetings,
> 
> I often wonder how one would explain type classes to someone coming from
> an environment such as Java. Whenever I think about type classes, I seem
> to think of them as Java interfaces. Bah, even the bottom of [1] states
>> Haskell classes are roughly similar to a Java interface. Like an
> interface declaration, a Haskell class declaration defines a protocol
> for using an object rather than defining an object itself.
> 
> Is there more to this `roughly similar' statement? Syntax is an obvious
> difference but beyond that, I can't think of anything I can do with a
> Haskell type class that I wouldn't be able to do with similar amount of
> effort with a Java interface, except for the fact that the interface
> would look absolutely disgusting syntax wise.
> 
> Any insight appreciated.

Well I have limited insight but?

Type classes can provide default implementations, which is not possible in Java.

Type classes in a type signature describe or constrain a type and but are not 
themselves types. Among other things, this means in Haskell that collections 
must be homogeneous in their actual type, it's not sufficient to be 
"homogeneous in a type class". There are extensions to GHC that make this 
possible [1] but there are limitations and the usage has its detractors [2]. In 
Java you can have collections of objects that conform to a given interface even 
if they are of different classes. Personally, I find Haskell's restriction 
counter-intuitive but the sense of surprise and  limitation is diminishing as I 
use the language.

Cheers,
Bob

[1] -- 
http://www.haskell.org/haskellwiki/Heterogenous_collections#Existential_types
[2] -- 
https://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/

> 
> 
> [1] - http://www.haskell.org/tutorial/classes.html
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

Message: 2
Date: Fri, 1 Feb 2013 14:37:57 +0000
From: Phil Scott <[email protected]>
Subject: Re: [Haskell-beginners] Type classes vs Java interfaces
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Fri, Feb 01, 2013 at 08:25:43AM -0500, Bob Hutchison wrote:
> 
> On 2013-01-31, at 6:36 PM, Mateusz Kowalczyk <[email protected]> wrote:
> 
> > Greetings,
> > 
> > I often wonder how one would explain type classes to someone coming from
> > an environment such as Java. Whenever I think about type classes, I seem
> > to think of them as Java interfaces. Bah, even the bottom of [1] states
> >> Haskell classes are roughly similar to a Java interface. Like an
> > interface declaration, a Haskell class declaration defines a protocol
> > for using an object rather than defining an object itself.
> > 
> > Is there more to this `roughly similar' statement? Syntax is an obvious
> > difference but beyond that, I can't think of anything I can do with a
> > Haskell type class that I wouldn't be able to do with similar amount of
> > effort with a Java interface, except for the fact that the interface
> > would look absolutely disgusting syntax wise.
> > 
> > Any insight appreciated.
> 
> Well I have limited insight but?
> 
> Type classes can provide default implementations, which is not possible in 
> Java.
> 
> Type classes in a type signature describe or constrain a type and but are not 
> themselves types. Among other things, this means in Haskell that collections 
> must be homogeneous in their actual type, it's not sufficient to be 
> "homogeneous in a type class". There are extensions to GHC that make this 
> possible [1] but there are limitations and the usage has its detractors [2]. 
> In Java you can have collections of objects that conform to a given interface 
> even if they are of different classes. Personally, I find Haskell's 
> restriction counter-intuitive but the sense of surprise and  limitation is 
> diminishing as I use the language.

Right, I like this point.

I don't like to think of Haskell type-classes as Java interfaces, even though I 
came from a C++/Java background. First and foremost, I like to think of 
type-classes as a way to do operator overloading, and after that I start 
thinking about all the other things you can do with them.

Java interfaces don't smell much like type-classes to me. First of all, when 
you call a method on an object in Java whose type is given by an interface, you 
can't generally tell which implementing method will be called: it depends on 
the runtime type of the object (dynamic dispatch, in other words). Haskell 98 
type-classes don't have this property.

The thing to note is that Java interfaces allow you to assign multiple types to 
the same value. Every value has the type of its class, but then it also gets 
the type of any interfaces it happens to implement. But, in Haskell, every 
value has exactly *one* type. That's why you can't mix and match them in a 
collection. And it's why you can determine at compiletime rather than runtime 
which implementation of a typeclass will be used at which points of the code.

Some of the things you would do with Java interfaces would be done in other 
ways in Haskell, using higher-order functions and algebraic data-types, so 
that's another reason not to think too heavily about interfaces when 
approaching Haskell. In general, I find it doesn't help me or help my friends 
when they try to map ideas from languages like Java into Haskell. They end up 
writing non-idiomatic Haskell programs.

Like I say, I think the first place to start when trying to understand 
type-classes is to think about operator overloading. This is a familiar idea in 
programming, and type-classes were invented initially to solve it.

> 
> Cheers,
> Bob
> 
> [1] -- 
> http://www.haskell.org/haskellwiki/Heterogenous_collections#Existential_types
> [2] -- 
> https://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/
> 
> > 
> > 
> > [1] - http://www.haskell.org/tutorial/classes.html
> > 
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> 
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 230 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130201/064a6db6/attachment-0001.pgp>

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

Message: 3
Date: Fri, 1 Feb 2013 06:45:56 -0800
From: Bryan Vicknair <[email protected]>
Subject: Re: [Haskell-beginners] Type classes vs Java interfaces
To: The Haskell-Beginners Mailing List - Discussion of
        primarilybeginner-level topics related to Haskell
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=utf-8



On Feb 1, 2013, at 5:25 AM, Bob Hutchison <[email protected]> wrote:

> 
> On 2013-01-31, at 6:36 PM, Mateusz Kowalczyk <[email protected]> wrote:
> 
>> Greetings,
>> 
>> I often wonder how one would explain type classes to someone coming from
>> an environment such as Java. Whenever I think about type classes, I seem
>> to think of them as Java interfaces. Bah, even the bottom of [1] states
>>> Haskell classes are roughly similar to a Java interface. Like an
>> interface declaration, a Haskell class declaration defines a protocol
>> for using an object rather than defining an object itself.
>> 
>> Is there more to this `roughly similar' statement? Syntax is an obvious
>> difference but beyond that, I can't think of anything I can do with a
>> Haskell type class that I wouldn't be able to do with similar amount of
>> effort with a Java interface, except for the fact that the interface
>> would look absolutely disgusting syntax wise.
>> 
>> Any insight appreciated.
> 
> Well I have limited insight but?
> 
> Type classes can provide default implementations, which is not possible in 
> Java.
> 
> Type classes in a type signature describe or constrain a type and but are not 
> themselves types. Among other things, this means in Haskell that collections 
> must be homogeneous in their actual type, it's not sufficient to be 
> "homogeneous in a type class". There are extensions to GHC that make this 
> possible [1] but there are limitations and the usage has its detractors [2]. 
> In Java you can have collections of objects that conform to a given interface 
> even if they are of different classes. Personally, I find Haskell's 
> restriction counter-intuitive but the sense of surprise and  limitation is 
> diminishing as I use the language.
> 

As I understand them, typeclasses allow you to add a type to a class without 
modifying the type definition at all, so they are more flexible than Java 
interfaces in that way.

I don't do much java, but I don't think you can define a class' implementation 
of an interface outside of the class.  In Haskell, I can define a typeclass 
that suites my domain, and add another author's type as an instance, and the 
author doesn't have to know about it.

Bryan Vicknair


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

Message: 4
Date: Fri, 1 Feb 2013 20:42:56 +0100
From: Martin Drautzburg <[email protected]>
Subject: [Haskell-beginners] f . g or f g  or f $ g?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="us-ascii"

Hello all

I frequently get confused over f . g vs f g. I do understand the following

With:

g :: a->b
f :: b ->c

f.g :: a->c

However 

f g 

is a type error, because then f would have to accept a function (a->b) as its 
first parameter, but it only accepts a b.

Is there a way to easily remember when to use f.g and when to use f g without 
having to do this type algebra.

-- 
Martin



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

Message: 5
Date: Fri, 01 Feb 2013 20:53:33 +0100
From: Emanuel Koczwara <[email protected]>
Subject: Re: [Haskell-beginners] f . g or f g  or f $ g?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <1359748413.23527.21.camel@emanuel-Dell-System-Vostro-3750>
Content-Type: text/plain; charset="UTF-8"

Hi,

> Hello all
> 
> I frequently get confused over f . g vs f g. I do understand the following
> 
> With:
> 
> g :: a->b
> f :: b ->c
> 
> f.g :: a->c
> 
> However 
> 
> f g 
> 
> is a type error, because then f would have to accept a function (a->b) as its 
> first parameter, but it only accepts a b.
> 
> Is there a way to easily remember when to use f.g and when to use f g without 
> having to do this type algebra.
> 

Function composition is different than function application. Are you
mixing these concepts?

Emanuel





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

Message: 6
Date: Fri, 01 Feb 2013 20:56:28 +0100
From: Emanuel Koczwara <[email protected]>
Subject: Re: [Haskell-beginners] f . g or f g  or f $ g?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <1359748588.23527.25.camel@emanuel-Dell-System-Vostro-3750>
Content-Type: text/plain; charset="UTF-8"

Hi,

  One more thing, you should know exactly when to use function
composition or function application. You need firt to udenrstund what
function application is.

Emanuel





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

Message: 7
Date: Fri, 01 Feb 2013 21:04:13 +0100
From: Emanuel Koczwara <[email protected]>
Subject: Re: [Haskell-beginners] f . g or f g  or f $ g?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <1359749053.23527.27.camel@emanuel-Dell-System-Vostro-3750>
Content-Type: text/plain; charset="UTF-8"

Hi,

Dnia 2013-02-01, pi? o godzinie 20:42 +0100, Martin Drautzburg pisze:
> Hello all
> 
> I frequently get confused over f . g vs f g. I do understand the following
> 
> With:
> 
> g :: a->b
> f :: b ->c
> 
> f.g :: a->c
> 
> However 
> 
> f g 
> 
> is a type error, because then f would have to accept a function (a->b) as its 
> first parameter, but it only accepts a b.
> 
> Is there a way to easily remember when to use f.g and when to use f g without 
> having to do this type algebra.
> 

  Maybe this could be a hint, consider this:

  f . g == (.) f g

Emanuel





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

Message: 8
Date: Fri, 1 Feb 2013 21:57:38 +0100
From: Martin Drautzburg <[email protected]>
Subject: Re: [Haskell-beginners] How to interact with a process
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-15"

On Wednesday, 30. January 2013 23:51:03 David McBride wrote:
> Then the answer depends on how you are going about playing midi.
> 
> 1. Is the api call for this playMidiFile :: FilePath -> IO (), where it
> waits until the file finishes before it returns?
> You may have to spawn a thread (forkIO) before you play, save it's threadId
> in some sort of state and then attempt to kill it from the input thread
> when the time comes.

Yes, my player is a function from Something to IO(), so forkIO is what I was 
looking for.


-- 
Martin



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

Message: 9
Date: Fri, 1 Feb 2013 22:17:06 +0100
From: Martin Drautzburg <[email protected]>
Subject: Re: [Haskell-beginners] How to interact with a process
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-15"

Okay I tried forkIO in the following way:

playBg = do 
       t <- forkIO play
       putStrLn "playing"

And indeed it  plays in background. 

But can't seem to get the killing to work. 

playBg = do 
       t <- forkIO play
       putStrLn "playing"
       c <- getChar
       putStrLn "interrrupted"
       killThread t 

When I enter "asd" while playing I see:

*Main> playBg
playing
asd
interrrupted

But it just goes on playing. Can a thread refuse to get killed, or am I doing 
something stupid?



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

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


End of Beginners Digest, Vol 56, Issue 3
****************************************

Reply via email to