I was hasty with my words.  Exceptions in haskell have always been my cryptonite.  If you are handling the only known exception properly then you are doing everything right.

On Tue, Dec 13, 2016 at 6:41 AM, Ovidiu Deac <ovidiudeac@gmail.com> wrote:
Thanks! It works.

Why is this a "quick and dirty" fix and what would be the "clean" fix?

On Mon, Dec 12, 2016 at 8:15 PM, David McBride <toad3k@gmail.com> wrote:
The problem is with
  Left err -> throwIO err

Because of the type of 'runCRand', we know err is an instance of ContainsGenError e0, but which one?  We need a concrete error type before we can run this code.  Looking at the docs there seems to be only one instance of ContainsGenError, GenError, so a quick an dirty solution would be to change it to

  Left err -> throwIO (err :: GenError) -- should work

But keep in mind, if there were any other ContainsGenError instances, like from an external library that is adding a new type of random generator to this library that fails in a new way, you would not be catching that.


On Mon, Dec 12, 2016 at 12:52 PM, Ovidiu Deac <ovidiudeac@gmail.com> wrote:
I have to produce a crypto random UUID.

I haven't found simple examples. and I used the one from hre (see type CRand) http://hackage.haskell.org/package/monadcryptorandom-0.7.0/docs/Control-Monad-CryptoRandom.html#v:getCRandomR

My attempt is the following:

cryptoRandomUUID :: IO UUID.UUID
cryptoRandomUUID = do
  g <- newGenIO:: IO SystemRandom
  case runCRand impl g of
    Left err -> throwIO err
    Right (v, g') -> return v

  where impl = do
          w1 <- getCRandom
          w2 <- getCRandom
          w3 <- getCRandom
          w4 <- getCRandom
          return $ UUID.fromWords w1 w2 w3 w4

...but the compilation fails miserably with:

    • Ambiguous type variable ‘e0’ arising from a use of ‘runCRand’
      prevents the constraint ‘(ContainsGenError e0)’ from being solved.
      Relevant bindings include
        impl :: CRandT
                  SystemRandom e0 Data.Functor.Identity.Identity UUID.UUID
          (bound at src/Party.hs:75:9)
      Probable fix: use a type annotation to specify what ‘e0’ should be.
      These potential instance exist:
        instance ContainsGenError GenError
          -- Defined in ‘Control.Monad.CryptoRandom’
    • In the expression: runCRand impl g
      In a stmt of a 'do' block:
        case runCRand impl g of {
          Left err -> throwIO err
          Right (v, g') -> return v }
      In the expression:
        do { g <- newGenIO :: IO SystemRandom;
             case runCRand impl g of {
               Left err -> throwIO err
               Right (v, g') -> return v } }
      ...

What's the problem here?
Are there some good examples for generating crypto-randoms?

Thanks!

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners