Reputation: 43
I want to create a data structure that comprises of defined functions based on: fold, Boolean operations, numeric operations (arithmetic and comparisons), and basic string operations. I'll be trying to evolve these class of functions. Here I am getting an error with MonadRandom.
Here is my code:
-- The following language extensions need to be enabled:
-- DeriveDataTypeable, FlexibleInstances, MultiParamTypeClasses
{-# LANGUAGE FlexibleContexts, DeriveDataTypeable, FlexibleInstances, MultiParamTypeClasses, StandaloneDeriving, ExistentialQuantification , GADTs #-}
{-# LANGUAGE RankNTypes #-}
import GenProg
import GenProg.GenExpr
import GenProg.GenExpr.Data
import Data.Generics
import Control.Monad
import Control.Monad.Random
import Data.Fixed
import Data.Typeable
import Data.Data
class IsGenExp t where
mutate :: (MonadRandom m) => t -> m t
crossOver :: (MonadRandom m) => t -> t -> m t
randomGen :: (MonadRandom m) => m t
data GenExp = forall t . IsGenExp t => GenExp t
data IntExp = IntExp Int
data FloatExp = FloatExp Float
data ListExp = ListExp [GenExp]
data ArithExp = Add GenExp GenExp | Sub GenExp GenExp | Mul GenExp GenExp
data CompExp = Eq GenExp GenExp | Lt GenExp GenExp | Gt GenExp GenExp
instance IsGenExp IntExp where
-- randomGen = (getRandomR (IntExp 1,IntExp 3))
randomGen = do
r <- getRandomR (0, 100)
return r
And the error I get:
GADT.hs:33:10:
Could not deduce (Random IntExp) arising from a use of `getRandomR'
from the context (MonadRandom m)
bound by the type signature for
randomGen :: MonadRandom m => m IntExp
at GADT.hs:32:4-12
In a stmt of a 'do' block: r <- getRandomR (0, 100)
In the expression:
do { r <- getRandomR (0, 100);
return r }
In an equation for `randomGen':
randomGen
= do { r <- getRandomR (0, 100);
return r }
GADT.hs:33:22:
Could not deduce (Num IntExp) arising from the literal `0'
from the context (MonadRandom m)
bound by the type signature for
randomGen :: MonadRandom m => m IntExp
at GADT.hs:32:4-12
In the expression: 0
In the first argument of `getRandomR', namely `(0, 100)'
In a stmt of a 'do' block: r <- getRandomR (0, 100)
Failed, modules loaded: none.
How do I fix this?
Upvotes: 1
Views: 73
Reputation: 52049
Try:
instance IsGenExp IntExp where
randomGen = do
r <- getRandomR (0, 100)
return $ IntExp r
Note:
r
is a numberIntExp r
is an IntExp
return $ IntExp r
is a m IntExp
for the monad type m
Upvotes: 1