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: Typeclass question (Brent Yorgey)
2. Re: how to define records containing finite length lists.
(Christian Maeder)
3. Eq a => [a]->[a] (I. J. Kennedy)
4. From Functional Dependencies to Type Families (Adolfo Builes)
5. Re: Eq a => [a]->[a] (Brent Yorgey)
6. From Functional Dependencies to Type Families (Adolfo Builes)
7. Re: From Functional Dependencies to Type Families
(Stephen Tetley)
----------------------------------------------------------------------
Message: 1
Date: Tue, 1 Dec 2009 14:24:17 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Typeclass question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Tue, Dec 01, 2009 at 10:48:50AM -0500, Patrick LeBoutillier wrote:
> Hi all,
>
> I want to make all types that are instances of Integral instances of
> another type class, i.e:
>
>
> {-# LANGUAGE FlexibleInstances #-}
>
> class Intable a where
> toInt :: a -> Int
>
> instance (Integral a) => Intable a where
> toInt l = fromIntegral l
This doesn't mean what you think it means. To do instance selection,
(GHC at least) only looks at the part AFTER the =>. So instead of
meaning "every type which is an instance of Integral should also be an
instance of Intable", it actually means "every type is an instance of
Intable; and oh yes, they had better be an instance of Integral too".
This can make a big difference. For example, this may look fine, but
it is not legal:
instance (Integral a) => Intable a where
toInt l = fromIntegral l
instance (Fractional a) => Intable a where
toInt l = round l
This looks like it should be OK as long as we never use a type which
is an instance of both Integral and Fractional (which is not likely).
However, these instances are actually overlapping since the part
before the => is not considered when deciding which instance to pick
for a particular type.
This also hints at an explanation for the error you got. When
resolving the use of a type class method, GHC looks at the structure
of the type in order to pick a type class instances to use. Once it
has chosen an instance (by comparing the type to the part of the
instance declaration to the right of the =>), it then considers any
additional constraints generated by the left-hand side of the =>, and
needs to choose a type class instance for each of those, and so on
recursively. The danger is that this process might not terminate; in
order to (conservatively) guard against this, GHC requires that any
types mentioned on the left-hand side of the => are structurally
smaller than types on the right-hand side; this ensures that the
process will eventually terminate since types are finite. If you want
to disable this check (and therefore introduce the possibility of an
inifnitely recursing type checker) you can enable UndecidableInstances
--- which isn't really as bad as some people make it out to be, but
best avoided unless you really understand why you want it.
It's hard to suggest what to do instead without knowing more about
what you are specifically trying to do (it seems that your example was
just a toy example to illustrate the problem), but you might consider
using newtype wrappers.
-Brent
------------------------------
Message: 2
Date: Wed, 02 Dec 2009 19:43:36 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: how to define records containing
finite length lists.
To: Srikanth K <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Srikanth K schrieb:
> Hi,
> I want to define a record say "Student". The 'C' equvivalent for the
> same could be
>
> struct Person {
> char name[10]
> int age }
>
> The closest I can find for doing such a thing in haskell appears to be
>
> data Person = Person{
> name :: [char]
> age :: Int
> }
you would use:
data Person = Person
{ name :: String
, age :: Int }
Note the "," and the capital first letter for types.
> I have not yet been able to find a suitable way to specify the
> constraint on the length of list(name in this case).
> So can someone let me know how we can impose this length constratint on
> the list and derive a new type.
>
> If standard haskell doesn't give this flexibility, are there any
> extensions in ghc to achieve the same.
The flexibility lies in not fixing the maximal size!
HTH Christian
------------------------------
Message: 3
Date: Wed, 2 Dec 2009 18:47:54 -0600
From: "I. J. Kennedy" <[email protected]>
Subject: [Haskell-beginners] Eq a => [a]->[a]
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
I am looking for a function
f::Eq a => [a]->[a]
that takes a list and returns the longest
initial segment of the list for which all
the elements are distinct.
For example f [2,3,6,4,3,5] = [2,3,6,4].
I didn't see anything that matched using Hoogle,
but I thought this might be a common enough
operation that this function might exists somewhere
in the standard packages.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091202/901cb706/attachment-0001.html
------------------------------
Message: 4
Date: Mon, 30 Nov 2009 17:32:02 -0500
From: Adolfo Builes <[email protected]>
Subject: [Haskell-beginners] From Functional Dependencies to Type
Families
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
> {-# OPTIONS -fglasgow-exts #-}
>
> module VarArg where
> import Data.FiniteMap -- for an example below
>
> class BuildList a r | r-> a where
> build' :: [a] -> a -> r
>
> instance BuildList a [a] where
> build' l x = reverse$ x:l
>
> instance BuildList a r => BuildList a (a->r) where
> build' l x y = build'(x:l) y
>
> --build :: forall r a. (BuildList a r) => a -> r
> build x = build' [] x
I'm trying to replace the code below to work with type families, I started
out replacing the definition of class with :
class BuildList r where
type Build r
build' :: [Build r] -> Build r -> r
follow by the instance for [a] resulting in
instance BuildList [a] where
type Build [a] = a
build' l x = reverse $ x:l
Until here, everything is working, and I'm able to do
> build' [2,3,4] 1 :: [Integer]
> [4,3,2,1]
then I move on to the next instance (a -> r) with
instance BuildList r => BuildList (a-> r) where
type Build (a -> r) = a
build' l x = \ y -> build'(x:l) y
But I get the following error
Couldn't match expected type `Build r' against inferred type `a'
`a' is a rigid type variable bound by
the instance declaration at /home/adolfo/foo.hs:347:35
In the first argument of `(:)', namely `x'
In the first argument of `build'', namely `(x : l)'
In the expression: build' (x : l) y
then I try with :
instance BuildList r => BuildList (a-> r) where
type Build (a -> r) = Build r
build' l x = \ y -> build'(x:l) y
And I get
Couldn't match expected type `a' against inferred type `Build r'
`a' is a rigid type variable bound by
the instance declaration at /home/adolfo/foo.hs:347:35
Expected type: [Build r]
Inferred type: [Build (a -> r)]
In the second argument of `(:)', namely `l'
In the first argument of `build'', namely `(x : l)'
I have been trying to figure out, which type should it be, but I haven't
found the correct one,
any ideas ?
Thanks
-
Adolfo Builes
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091130/38e9f0bc/attachment-0001.html
------------------------------
Message: 5
Date: Wed, 2 Dec 2009 21:46:43 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Eq a => [a]->[a]
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Dec 02, 2009 at 06:47:54PM -0600, I. J. Kennedy wrote:
> I am looking for a function
> f::Eq a => [a]->[a]
> that takes a list and returns the longest
> initial segment of the list for which all
> the elements are distinct.
>
> For example f [2,3,6,4,3,5] = [2,3,6,4].
>
> I didn't see anything that matched using Hoogle,
> but I thought this might be a common enough
> operation that this function might exists somewhere
> in the standard packages.
Not that I know of. Here's how I would implement it (although you may
enjoy trying to implement it yourself):
import qualified Data.Set as S
f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
where cums = scanl (flip S.insert) S.empty xs
It works by incrementally building up a list of sets of the elements
found in prefixes of the list (with scanl), then goes down the list
(takeWhile) checking that each element isn't already in the
corresponding set of elements.
-Brent
------------------------------
Message: 6
Date: Wed, 2 Dec 2009 22:06:43 -0500
From: Adolfo Builes <[email protected]>
Subject: [Haskell-beginners] From Functional Dependencies to Type
Families
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
> {-# OPTIONS -fglasgow-exts #-}
>
> module VarArg where
> import Data.FiniteMap -- for an example below
>
> class BuildList a r | r-> a where
> build' :: [a] -> a -> r
>
> instance BuildList a [a] where
> build' l x = reverse$ x:l
>
> instance BuildList a r => BuildList a (a->r) where
> build' l x y = build'(x:l) y
>
> --build :: forall r a. (BuildList a r) => a -> r
> build x = build' [] x
I'm trying to replace the code below to work with type families, I started
out replacing the definition of class with :
class BuildList r where
type Build r
build' :: [Build r] -> Build r -> r
follow by the instance for [a] resulting in
instance BuildList [a] where
type Build [a] = a
build' l x = reverse $ x:l
Until here, everything is working, and I'm able to do
> build' [2,3,4] 1 :: [Integer]
> [4,3,2,1]
then I move on to the next instance (a -> r) with
instance BuildList r => BuildList (a-> r) where
type Build (a -> r) = a
build' l x = \ y -> build'(x:l) y
But I get the following error
Couldn't match expected type `Build r' against inferred type `a'
`a' is a rigid type variable bound by
the instance declaration at /home/adolfo/foo.hs:347:35
In the first argument of `(:)', namely `x'
In the first argument of `build'', namely `(x : l)'
In the expression: build' (x : l) y
then I try with :
instance BuildList r => BuildList (a-> r) where
type Build (a -> r) = Build r
build' l x = \ y -> build'(x:l) y
And I get
Couldn't match expected type `a' against inferred type `Build r'
`a' is a rigid type variable bound by
the instance declaration at /home/adolfo/foo.hs:347:35
Expected type: [Build r]
Inferred type: [Build (a -> r)]
In the second argument of `(:)', namely `l'
In the first argument of `build'', namely `(x : l)'
I have been trying to figure out, which type should it be, but I haven't
found the correct one, any ideas ?
Thanks
-
Adolfo Builes
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091202/f4eb7a3c/attachment-0001.html
------------------------------
Message: 7
Date: Thu, 3 Dec 2009 09:38:48 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] From Functional Dependencies to Type
Families
To: Adolfo Builes <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Adolfo
This is quite a tricky one and I haven't got a proper answer
(hopefully someone will come along with one soon). But I've
an initial observation:
Longhand (i.e. without the type class - but relying on previously
defined instances) the type family version of buildlist on functions
is:
buildList_fun l x = \y -> build' (x:l) y
Asking for the type at the GHCi prompt gives:
buildList_fun
:: (BuildList r) => [Build r] -> Build r -> Build r -> r
Whereas for the fun dep version
build_fun l x y = build' (x:l) y
The type is:
build_fun :: (BuildList a r) => [a] -> a -> a -> r
Crucially the last part of the type is r.
Why you can't type your TF version seems to be because the answer type
of the continuation is fixed to a not r.
(the fun dep version is also a multi parameter class so you are
allowed a different answer type).
[I'm calling out to the gallery for help at this point!].
Best wishes
Stephen
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 2
****************************************