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: instances of different kinds (Brandon S Allbery KF8NH)
2. Re: instances of different kinds (Brandon S Allbery KF8NH)
3. Re: randomize the order of a list (Heinrich Apfelmus)
4. Re: instances of different kinds (Greg)
5. Re: instances of different kinds (J?rgen Doser)
6. randomize the order of a list (A Smith)
----------------------------------------------------------------------
Message: 1
Date: Fri, 27 Aug 2010 23:15:15 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] instances of different kinds
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 8/27/10 22:58 , Greg wrote:
>
> I'm still having a hard time finding a way to make this work, even given the
> fine suggestions from Tobias and Jürgen. I suspect there's some piece of
> information that the compiler can't make sense of that I'm just not seeing--
> a case of it insisting on doing what I say instead of what I mean... =)
>
> I guess the problem I'm having is finding a way to treat parametric and
> non-parametric types interchangeably. The syntax doesn't seem to exist that
> will allow me to say:
>
> div2pi :: (Floating a) => a -> a -- for non parametric types (ie. Float)
> and
> div2pi :: (Floating b) => a b -> b -- for parametric types (ie. Foo Float)
>
>
> In addition, I'm having a hard time understanding the errors I'm getting
> from constructs like this:
>
> data Foo a = Foo a
>
> class TwoPi a where
> div2pi :: (Floating b) => a -> b
You were told about this already: because "b" is only mentioned in the
result of div2pi, it must be able to return *any* "b" that has a Floating
instance.
But then you say
> instance (Floating a) => TwoPi (Foo a) where
> div2pi (Foo x) = x / (2*pi)
An instance applies to a *specific* type; thus, this instance declaration
forces a *specific* type on the result of div2pi, when the class declaration
says it must be able to return *any* type.
Expanding the types, the class declaration says
> div2pi :: forall b. (Floating a, Floating b) => a -> b
but the instance declares
> div2pi :: (Floating a) => a -> a
The instance doesn't conform to the class definition; it includes a
constraint that the class does not, as the class insists that the type of
the result must be independent of the type of the argument, while the
instance insists that they must be identical.
Perhaps the correct question is "what exactly are you trying to do?"
- --
brandon s. allbery [linux,solaris,freebsd,perl] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkx4f0IACgkQIn7hlCsL25WexQCfTWxZxqchxvAB0Dm1wWwYQIrq
sBQAoLeaz8Lq6ydO5WJPu679WyNEu8w0
=BnBu
-----END PGP SIGNATURE-----
------------------------------
Message: 2
Date: Fri, 27 Aug 2010 23:21:48 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] instances of different kinds
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 8/27/10 22:58 , Greg wrote:
> I guess the problem I'm having is finding a way to treat parametric and
> non-parametric types interchangeably. The syntax doesn't seem to exist that
> will allow me to say:
>
> div2pi :: (Floating a) => a -> a -- for non parametric types (ie. Float)
> and
> div2pi :: (Floating b) => a b -> b -- for parametric types (ie. Foo Float)
It occurs to me that what you really want *may* be:
> class (Floating a, Floating b) => TwoPi a b | a -> b where
> div2pi :: a -> b
which says that, given a specific type a, the compiler can infer a specific
type b corresponding to it, without specifying what that type is (leaving
the instance declaration to do so).
- --
brandon s. allbery [linux,solaris,freebsd,perl] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkx4gMwACgkQIn7hlCsL25UaKwCgpe6hlJQ0yCDL2GtbO7EsrePe
Cp4An3mEUz6PH23v9z0SHbmP6ikmm/yL
=YIZH
-----END PGP SIGNATURE-----
------------------------------
Message: 3
Date: Sat, 28 Aug 2010 10:51:47 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: randomize the order of a list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Gaius Hammond wrote:
> I am trying to randomly reorder a list (e.g. shuffle a deck of cards) .
> My initial approach is to treat it as an array, generate a list of
> unique random numbers between 0 and n - 1, then use those numbers as new
> indexes.
For another approach, see also
http://apfelmus.nfshost.com/articles/random-permutations.html
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Sat, 28 Aug 2010 03:44:38 -0700
From: Greg <[email protected]>
Subject: Re: [Haskell-beginners] instances of different kinds
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Aug 27, 2010, at 8:15 PM, Brandon S Allbery KF8NH wrote:
> You were told about this already:
No doubt I'm being quite dense here. I think your re-explanation may have
clicked though:
> because "b" is only mentioned in the
> result of div2pi, it must be able to return *any* "b" that has a Floating
> instance.
>
> But then you say
>> instance (Floating a) => TwoPi (Foo a) where
>> div2pi (Foo x) = x / (2*pi)
>
> An instance applies to a *specific* type; thus, this instance declaration
> forces a *specific* type on the result of div2pi, when the class declaration
> says it must be able to return *any* type.
>
> Expanding the types, the class declaration says
>
>> div2pi :: forall b. (Floating a, Floating b) => a -> b
>
> but the instance declares
>
>> div2pi :: (Floating a) => a -> a
>
> The instance doesn't conform to the class definition; it includes a
> constraint that the class does not, as the class insists that the type of
> the result must be independent of the type of the argument, while the
> instance insists that they must be identical.
I've been working on the assumption, perhaps despite attempts to teach me
otherwise, that the class is essentially the interface to the rest of the
world. That is, the class is the guarantee. The instance, then, can do no
less than the class interface has guaranteed. In this case, for example, I've
been assuming that the type variable 'b' in my class definition meant "div2pi
will result in a value of a type which is an instance of the Floating class".
Then, since my instance method resulted in a Float, which is an instance of the
Floating class, everything should be happy. If my instance method resulted in
a Double, it would be equally happy.
I think what you're saying is that not only can an instance do no less than the
class has guaranteed, it can do no *more*-- meaning the instance can't further
restrict the return type even if that return type still conforms to the class
definition. In this case returning a Float breaks the class contract because
I've gone and said what type of Floating type will be returned.
The class definition doesn't mean "div2pi can return any type of Floating
value", it means "div2pi *will* return any type of floating value".
I have a harder time working out why returning a "(Floating a) => a" is seen as
different. If I understand what Jürgen was saying, it's because the "a" in
this case is specifically the "a" in "Foo a", which nails it down again. If
this is right, then my class definition:
> class TwoPi a where
> div2pi :: (Floating b) => a -> b
is essentially impossible to conform to because b is completely untethered to
anything else in the code and not all "(Floating b)"'s are created equal. I
think the intent of the functional dependency in the suggestion you provided in
your second email is essentially to tether b to something. Unfortunately if
chokes in the second, non-Foo, instance.
Am I getting this right? Rereading Jürgen's response, this seems to fit with
his explanation as well-- I just didn't grasp it.
I think I've mistakenly been thinking of instances as more like subclasses, but
that's apparently not quite right (thus the "instance" keyword, I suppose).
>
> Perhaps the correct question is "what exactly are you trying to do?"
>
I'm literally just trying to understand the type system and its syntax. It's
the part of Haskell that feels most foreign, and I'm trying to work through the
implications.
Typeclasses provide a mechanism to abstract operations over multiple types, as
you mentioned in the thread for my last question. What I'm trying to figure
out now is what kinds of types they can be abstracted over. I'm looking to get
the result: "((5.6,Foo 9.8),(0.8912676813146139,1.5597184423005745))"
>From code that looks kind of like this :
data Foo a = Foo a deriving (Show)
x :: Float
x= 5.6
y :: Foo Double
y= Foo 9.8
class {-something-} TwoPi {-something-} where
div2pi :: {-something-}
instance {-something-} TwoPi Foo where
div2pi (Foo b) = b / (2*pi)
instance TwoPi Float where
div2pi a = a / (2*pi)
main = do
print ((x,y),(div2pi x, div2pi y))
Thanks--
Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100828/7d6f1199/attachment-0001.html
------------------------------
Message: 5
Date: Sat, 28 Aug 2010 14:11:01 +0200
From: J?rgen Doser <[email protected]>
Subject: Re: [Haskell-beginners] instances of different kinds
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
El sáb, 28-08-2010 a las 03:44 -0700, Greg escribió:
>
[...]
>
> The class definition doesn't mean "div2pi can return any type of
> Floating value", it means "div2pi *will* return any type of floating
> value".
>
I would say it like this: div2pi does not return a type of Floating
value of its own choosing, it is able to return every type of Floating
value (the concrete type is then chosen by the context in each case
where div2pi is used).
>
[...]
> If this is right, then my class definition:
>
>
> > class TwoPi a where
> > div2pi :: (Floating b) => a -> b
>
>
> is essentially impossible to conform to because b is completely
> untethered to anything else in the code and not all "(Floating b)"'s
> are created equal.
Actually, it is possible, because of functions like realToFrac, which
return a type-class polymorphic value. Unfortunately, I have botched up
the example in my post. Sorry for that. Corrected code is below.
> Typeclasses provide a mechanism to abstract operations over multiple
> types, as you mentioned in the thread for my last question. What I'm
> trying to figure out now is what kinds of types they can be abstracted
> over. I'm looking to get the result: "((5.6,Foo
> 9.8),(0.8912676813146139,1.5597184423005745))"
>
>
> From code that looks kind of like this :
>
>
> data Foo a = Foo a deriving (Show)
>
> x :: Float
> x= 5.6
>
> y :: Foo Double
> y= Foo 9.8
>
> class {-something-} TwoPi {-something-} where
> div2pi :: {-something-}
>
> instance {-something-} TwoPi Foo where
> div2pi (Foo b) = b / (2*pi)
>
> instance TwoPi Float where
> div2pi a = a / (2*pi)
>
> main = do
> print ((x,y),(div2pi x, div2pi y))
>
This works:
data Foo a = Foo a deriving Show
x :: Float
x= 5.6
y :: Foo Double
y= Foo 9.8
class TwoPi a where
div2pi :: (Floating b) => a -> b
instance (Real a, Floating a) => TwoPi (Foo a) where
div2pi (Foo a) = realToFrac a / (2*pi)
instance TwoPi Float where
div2pi a = realToFrac a / (2*pi)
main = print ((x,y),(div2pi x, div2pi y))
*Main> main
((5.6,Foo 9.8),(0.8912676661364157,1.5597184423005745))
>
The (Real a) restriction in the instance definition for Foo a is
necessary. If a would be Complex Double, for example, there is no way
you can sensibly expect a Float return value.
>
Jürgen
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 6
Date: Sat, 28 Aug 2010 14:49:25 +0100
From: A Smith <[email protected]>
Subject: [Haskell-beginners] randomize the order of a list
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
I've abeen recommended on good authority(my son, a Cambridge Pure maths
graduate, and Perl/Haskell expert) , and backed by a Google search that
Fisher-Yates shuffle is the one to use, as it produces total unbiased
results with every combination equally possible.
As with most things with computers,don't reinvent the eheel, it's almost
certainly been done before by someone brighter that you, Fisher,Yates. &
Knuth!
--
Andrew Smith B.Sc(Hons),MBA
Edinburgh,Scotland
On 27 August 2010 21:02, Gaius Hammond <[email protected]> wrote:
> Hi all,
>
>
>
> I am trying to randomly reorder a list (e.g. shuffle a deck of cards) . My
> initial approach is to treat it as an array, generate a list of unique
> random numbers between 0 and n - 1, then use those numbers as new indexes. I
> am using a function to generate random numbers in the State monad as
> follows:
>
>
>
> randIntâ· Int â State StdGen Int
> randInt x = do g â get
> (v,g') â return $ randomR (0, x) g
> put g'
> return v
>
>
>
> This is pretty much straight from the documentation. My function for the
> new indexes is:
>
>
>
> -- return a list of numbers 0 to x-1 in random order
> randIndexâ· Int â StdGen â ([Int], StdGen)
> randIndex x = runState $ do
> let randIndex' acc r
> | (length acc â¡ x) = acc
> | (r `elem` acc) ⨠(r â¡ (â1)) = do
> r' â randInt (x â 1)
> randIndex' acc r'
> | otherwise = do
> r' â randInt (x â 1)
> randIndex' r:acc r'
> in
> randIndex' [] (â1)
>
>
>
> This fails to compile on
>
>
>
>
> Couldn't match expected type `[a]'
> against inferred type `State StdGen b'
> In a stmt of a 'do' expression: r' <- randInt (x - 1)
> In the expression:
> do { r' <- randInt (x - 1);
> randIndex' acc r' }
>
>
>
>
> I can see what's happening here - it's treating randIndex' as the second
> argument to randInt instead of invisibly putting the State in there. Or am I
> going about this completely the wrong way?
>
>
> Thanks,
>
>
>
> G
>
>
>
>
> _______________________________________________
> 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/20100828/7a69544d/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 26, Issue 54
*****************************************