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. Confused by type constraints (Guy)
2. Re: Data declarations (aditya siram)
3. Re: Confused by type constraints (Daniel Schoepe)
4. Re: Confused by type constraints (Guy)
5. Re: Confused by type constraints (Brandon Allbery)
6. Re: Data declarations (Brandon Allbery)
7. nub... almost (Christopher Howard)
8. Re: nub... almost (Christopher Howard)
9. Re: nub... almost (Benjamin Edwards)
10. Re: nub... almost (Iustin Pop)
----------------------------------------------------------------------
Message: 1
Date: Fri, 03 Jun 2011 16:04:05 +0300
From: Guy <[email protected]>
Subject: [Haskell-beginners] Confused by type constraints
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I have some code resembling
class Foo f where
foo :: a -> f a
data Bar f a = Foo f => Bar {bar :: f a}
instance Foo (Bar f) where
foo a = Bar $ foo a
GHC insists on Foo f => in the instance declaration. However, the definition of
Bar guarantees that this will always be
the case. Why do I have to state this explicitly?
------------------------------
Message: 2
Date: Fri, 3 Jun 2011 08:37:59 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Data declarations
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
> One more question, out of curiosity: In your above example, you could change
>
> ?data Populated = Populated
> ?data New = New
>
> to simply
>
> ?data Populated
> ?data New
>
> correct? You might as well, right, since your constructors take no
> values, and you are just using them as phantom types?
Yes I think that's correct. You might also look at GADT's [1] which
cover some of the same use-cases.
-deech
[1] http://www.haskell.org/haskellwiki/GADT
------------------------------
Message: 3
Date: Fri, 03 Jun 2011 15:55:39 +0200
From: Daniel Schoepe <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: Guy <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Fri, 03 Jun 2011 16:04:05 +0300, Guy <[email protected]> wrote:
> I have some code resembling
>
> class Foo f where
> foo :: a -> f a
>
> data Bar f a = Foo f => Bar {bar :: f a}
>
> instance Foo (Bar f) where
> foo a = Bar $ foo a
>
> GHC insists on Foo f => in the instance declaration. However, the definition
> of Bar guarantees that this will always be
> the case. Why do I have to state this explicitly?
The constraints in the data type declaration only affect the
constructors. It's a known issue, unfortunately there doesn't seem to be
a way around it.
See also:
http://www.haskell.org/haskellwiki/Data_declaration_with_constraint
Cheers,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110603/d1bb3792/attachment-0001.pgp>
------------------------------
Message: 4
Date: Fri, 03 Jun 2011 17:13:43 +0300
From: Guy <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 03/06/2011 16:55, Daniel Schoepe wrote:
> On Fri, 03 Jun 2011 16:04:05 +0300, Guy<[email protected]> wrote:
>> I have some code resembling
>>
>> class Foo f where
>> foo :: a -> f a
>>
>> data Bar f a = Foo f => Bar {bar :: f a}
>>
>> instance Foo (Bar f) where
>> foo a = Bar $ foo a
>>
>> GHC insists on Foo f => in the instance declaration. However, the
>> definition of Bar guarantees that this will always be
>> the case. Why do I have to state this explicitly?
>
> The constraints in the data type declaration only affect the
> constructors. It's a known issue, unfortunately there doesn't seem to be
> a way around it.
It seems to work for simpler examples. For example, this is OK
data X x = Show x => X x
s :: X x -> String
s (X x) = show x
I don't have to add a Show x => type constraint to s.
------------------------------
Message: 5
Date: Fri, 3 Jun 2011 11:37:06 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: Daniel Schoepe <[email protected]>
Cc: Guy <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Fri, Jun 3, 2011 at 09:55, Daniel Schoepe
<[email protected]> wrote:
> The constraints in the data type declaration only affect the
> constructors. It's a known issue, unfortunately there doesn't seem to be
> a way around it.
GADTs handle it as expected. The core problem is with how the Haskell
98 standard declared them to work; the Haskell standards committee has
proposed removing them altogether from non-GADT syntax since they're
worse than useless as is.
------------------------------
Message: 6
Date: Fri, 3 Jun 2011 11:44:52 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Data declarations
To: aditya siram <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Fri, Jun 3, 2011 at 09:37, aditya siram <[email protected]> wrote:
>> One more question, out of curiosity: In your above example, you could change
>>
>> ?data Populated = Populated
>> ?data New = New
>>
>> to simply
>>
>> ?data Populated
>> ?data New
>>
>> correct? You might as well, right, since your constructors take no
>> values, and you are just using them as phantom types?
>
> Yes I think that's correct. You might also look at GADT's [1] which
> cover some of the same use-cases.
The reason for the former is that the latter is not accepted by
Haskell 98, so it's a little less portable.
------------------------------
Message: 7
Date: Fri, 03 Jun 2011 23:37:32 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] nub... almost
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Weird request: For a certain application, the "nub" function from
Data.List is exactly what I need... almost. Nub removes duplicates,
keeping the /first/ occurrence of each element. However, I need a
function that removes duplicates, keeping the /second/ occurrence of
each element. There wouldn't happen to be a function already that does
this, would there?
I'm working with a custom type which is a member of Eq, but some of the
data in the type is not used in the comparison. So it is important which
of the two "duplicates" are actually saved.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 8
Date: Fri, 03 Jun 2011 23:49:01 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 06/03/2011 11:37 PM, Christopher Howard wrote:
> Weird request: For a certain application, the "nub" function from
> Data.List is exactly what I need... almost. Nub removes duplicates,
> keeping the /first/ occurrence of each element. However, I need a
> function that removes duplicates, keeping the /second/ occurrence of
> each element. There wouldn't happen to be a function already that does
> this, would there?
>
> I'm working with a custom type which is a member of Eq, but some of the
> data in the type is not used in the comparison. So it is important which
> of the two "duplicates" are actually saved.
>
I should qualify this: By "second" I mean "last". Of course, it is
possible for there to be more than one duplicate of any given element in
a list. E.g., if there are three identical elements in a list, I want
the third one, not the second one.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 9
Date: Sat, 4 Jun 2011 08:54:07 +0100
From: Benjamin Edwards <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I would probably just use Data.Set, insert all the elements in the list into
it, and then reconvert to a list. Done. Can provide code it you need it.
On 4 June 2011 08:49, Christopher Howard
<[email protected]>wrote:
> On 06/03/2011 11:37 PM, Christopher Howard wrote:
> > Weird request: For a certain application, the "nub" function from
> > Data.List is exactly what I need... almost. Nub removes duplicates,
> > keeping the /first/ occurrence of each element. However, I need a
> > function that removes duplicates, keeping the /second/ occurrence of
> > each element. There wouldn't happen to be a function already that does
> > this, would there?
> >
> > I'm working with a custom type which is a member of Eq, but some of the
> > data in the type is not used in the comparison. So it is important which
> > of the two "duplicates" are actually saved.
> >
>
> I should qualify this: By "second" I mean "last". Of course, it is
> possible for there to be more than one duplicate of any given element in
> a list. E.g., if there are three identical elements in a list, I want
> the third one, not the second one.
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110604/19656c9f/attachment-0001.htm>
------------------------------
Message: 10
Date: Sat, 4 Jun 2011 10:20:01 +0200
From: Iustin Pop <[email protected]>
Subject: Re: [Haskell-beginners] nub... almost
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Jun 03, 2011 at 11:49:01PM -0800, Christopher Howard wrote:
> On 06/03/2011 11:37 PM, Christopher Howard wrote:
> > Weird request: For a certain application, the "nub" function from
> > Data.List is exactly what I need... almost. Nub removes duplicates,
> > keeping the /first/ occurrence of each element. However, I need a
> > function that removes duplicates, keeping the /second/ occurrence of
> > each element. There wouldn't happen to be a function already that does
> > this, would there?
> >
> > I'm working with a custom type which is a member of Eq, but some of the
> > data in the type is not used in the comparison. So it is important which
> > of the two "duplicates" are actually saved.
> >
>
> I should qualify this: By "second" I mean "last". Of course, it is
> possible for there to be more than one duplicate of any given element in
> a list. E.g., if there are three identical elements in a list, I want
> the third one, not the second one.
In that case, can't you simply revert the list, nub it, then revert
again (i.e. reverse . nub . reverse)?
regards,
iustin
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 8
****************************************