Send Beginners mailing list submissions to
[email protected]
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
[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. Random Generator (Shishir Srivastava)
2. Re: Random Generator (Brandon Allbery)
3. Re: Random Generator (David McBride)
4. Re: Why does QuickCheck insist on this class constraint?
(Chadda? Fouch?)
5. Re: Random Generator (Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Wed, 1 Apr 2015 16:08:42 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Random Generator
Message-ID:
<cale5rtsm0wwc9cjra2qbjj+drohrisbysom3grpdpyf3j4k...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
I am trying to use the output value from the random function to generate
the random generator for the next value in list.
Below is the code -
----
import System.Random
myrandoms :: (RandomGen g, Random a) => g -> [a]
myrandoms gen = let (value, newGen) = random gen in value:myrandoms
(mkStdGen (value::Int))
----
however the compilation fails when the module is loaded -
[1 of 1] Compiling Main ( myrandoms.hs, interpreted )
myrandoms.hs:3:80:
Could not deduce (a ~ Int)
from the context (RandomGen g, Random a)
bound by the type signature for
myrandoms :: (RandomGen g, Random a) => g -> [a]
at myrandoms.hs:2:14-48
`a' is a rigid type variable bound by
the type signature for
myrandoms :: (RandomGen g, Random a) => g -> [a]
at myrandoms.hs:2:14
Relevant bindings include
value :: a (bound at myrandoms.hs:3:22)
myrandoms :: g -> [a] (bound at myrandoms.hs:3:1)
In the first argument of `mkStdGen', namely `(value :: Int)'
In the first argument of `myrandoms', namely
`(mkStdGen (value :: Int))'
----------
Even though I am converting my 'value' parameter to Int in my new
generator, I am unable to see the error behind this.
Please can someone explain or even better provide a fix.
Thanks,
Shishir
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150401/8c08eba7/attachment-0001.html>
------------------------------
Message: 2
Date: Wed, 1 Apr 2015 11:19:47 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Random Generator
Message-ID:
<cakfcl4v+htqgzxorzjnrtr4ccn+zrzcl-el9sat46ddpbay...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Apr 1, 2015 at 11:08 AM, Shishir Srivastava <
[email protected]> wrote:
> myrandoms :: (RandomGen g, Random a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen (value::Int))
>
You have declared a function that says that it can deal with any type `a`
that the *caller* chooses, then provided an implementation that only
supports Int.
Note that :: does not do conversion (as you said "Even though I am
converting my 'value' parameter to Int"); it declares that the type of
`value` *is* Int. Other types will be inferred to match, and this fails at
`value:` in a context which wants the type to be a caller-specified `a`,
not Int.
(I don't think you can coerce an unknown type `a` to Int given only the
context `Random a`. You must find a different way to implement this.)
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150401/daf8b62f/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 1 Apr 2015 11:32:48 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Random Generator
Message-ID:
<can+tr42t0vwahb793iqkkdsyidigu-oo0tt1eoyvgvck9ey...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
mkStdGen only accepts Ints as seeds. But your random function, as you
typed it, can return any type of random. You either have to restrict your
random function to returning ints, like so:
myrandoms :: (RandomGen g) => g -> [Int]
myrandoms gen = let (value, newGen) = random gen in value:myrandoms
(mkStdGen value)
Or you have to find a way to convert any Random a into an Int (not
possible), or put another constraint on it, such that you can return all
the types you might want that you have the ability to turn into ints, for
example:
myrandoms :: (RandomGen g, Random a, Intable a) => g -> [a]
myrandoms gen = let (value, newGen) = random gen in value:myrandoms
(mkStdGen $ convertToInt value)
class Intable a where
convertToInt :: a -> Int
instance Intable Int where convertToInt = id
instance Intable Integer where convertToInt = fromIntegral
instance Intable Char where convertToInt s = undefined -- something
Which is obviously tedious, but may be worthwhile depending on your
application.
On Wed, Apr 1, 2015 at 11:08 AM, Shishir Srivastava <
[email protected]> wrote:
> Hi,
>
> I am trying to use the output value from the random function to generate
> the random generator for the next value in list.
>
> Below is the code -
>
> ----
> import System.Random
> myrandoms :: (RandomGen g, Random a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen (value::Int))
> ----
>
> however the compilation fails when the module is loaded -
>
> [1 of 1] Compiling Main ( myrandoms.hs, interpreted )
>
> myrandoms.hs:3:80:
> Could not deduce (a ~ Int)
> from the context (RandomGen g, Random a)
> bound by the type signature for
> myrandoms :: (RandomGen g, Random a) => g -> [a]
> at myrandoms.hs:2:14-48
> `a' is a rigid type variable bound by
> the type signature for
> myrandoms :: (RandomGen g, Random a) => g -> [a]
> at myrandoms.hs:2:14
> Relevant bindings include
> value :: a (bound at myrandoms.hs:3:22)
> myrandoms :: g -> [a] (bound at myrandoms.hs:3:1)
> In the first argument of `mkStdGen', namely `(value :: Int)'
> In the first argument of `myrandoms', namely
> `(mkStdGen (value :: Int))'
>
> ----------
>
> Even though I am converting my 'value' parameter to Int in my new
> generator, I am unable to see the error behind this.
>
> Please can someone explain or even better provide a fix.
>
> Thanks,
> Shishir
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150401/df151122/attachment-0001.html>
------------------------------
Message: 4
Date: Wed, 1 Apr 2015 22:43:47 +0200
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Why does QuickCheck insist on this
class constraint?
Message-ID:
<canfjzryeta_glx6rugjxssu_pjt2-ccjlo+gn7epytk6f0z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Why are you putting those type signatures everywhere ? Won't your code
compile without them ? It seems to me that absent a strange Change type,
all those things can be inferred... Are you doing this as an exercise or
because you think it is more readable ?
--
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150401/69dcd987/attachment-0001.html>
------------------------------
Message: 5
Date: Wed, 1 Apr 2015 23:04:05 +0200
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Random Generator
Message-ID:
<CANfjZRY0KF49BOBGEC56n_84bk+1RQYh-=uvaxe7oykj97-...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Apr 1, 2015 at 5:32 PM, David McBride <[email protected]> wrote:
> mkStdGen only accepts Ints as seeds. But your random function, as you
> typed it, can return any type of random. You either have to restrict your
> random function to returning ints, like so:
>
> myrandoms :: (RandomGen g) => g -> [Int]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen value)
>
*Is there any good reason we're not using newGen for its intended purpose
here and instead weakening our randomness, maybe extremely (imagine if a
list a Bool is asked for...) ???*
>
> Or you have to find a way to convert any Random a into an Int (not
> possible), or put another constraint on it, such that you can return all
> the types you might want that you have the ability to turn into ints, for
> example:
>
> myrandoms :: (RandomGen g, Random a, Intable a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen $ convertToInt value)
>
> class Intable a where
> convertToInt :: a -> Int
>
> instance Intable Int where convertToInt = id
> instance Intable Integer where convertToInt = fromIntegral
> instance Intable Char where convertToInt s = undefined -- something
>
> Which is obviously tedious, but may be worthwhile depending on your
> application.
>
>
If this was really the way Shishir wanted to go, I would suggest simply
reusing the Enum typeclass rather than creating a new Intable typeclass,
since : fromEnum :: (Enum a) => a -> Int
Still a very bad and puzzling idea by the way...
--
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150401/acd49579/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 82, Issue 2
****************************************