2

So, I'm trying to get an example of GenProg working, a Haskell genetic programming library.

However, I'm getting various instance errors. My guess is that the example is written in slightly out-dated Haskell and just has to be minorly tweaked. The code makes sense, but I don't know enough about instances to easily rewrite it myself. I made a few tweaks.

Error:

genprogtest.hs:27:10: Could not deduce (MonadRandom-0.1.13:Control.Monad.Random.Class.MonadRandom (Rand StdGen)) arising from the superclasses of an instance declaration from the context (GenExpr E)

That is, the first line of the instance. (The actual example is here: https://hackage.haskell.org/package/genprog-0.1/docs/GenProg.html#9 )

{-# LANGUAGE DeriveDataTypeable, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses #-}
-- Just put them in one line, yo~

import GenProg
import GenProg.GenExpr
import Data.Generics
import Control.Monad
import Control.Monad.Random

data E = Plus E E
       | Minus E E
       | Times E E
       | Div E E
       | Const Int
       deriving (Typeable,Data,Eq,Show)

eval :: E -> Maybe Int
eval (Const c)     = Just c
eval (Plus e1 e2)  = liftM2 (+) (eval e1) (eval e2)
eval (Minus e1 e2) = liftM2 (-) (eval e1) (eval e2)
eval (Times e1 e2) = liftM2 (*) (eval e1) (eval e2)
eval (Div e1 e2) | ok        = liftM2 div x1 x2
                 | otherwise = Nothing
  where (x1,x2) = (eval e1,eval e2)
        ok = x2 /= Just 0 && liftM2 mod x1 x2 == Just 0

instance (GenExpr E) => GenProg (Rand StdGen) E where
  terminal    = Const `liftM` getRandomR (1,9)
  nonterminal = do
    r <- getRandomR (0,3)
    [liftM2 Plus terminal terminal,
     liftM2 Minus terminal terminal,
     liftM2 Times terminal terminal,
     liftM2 Div terminal terminal] !! r    

myFitness :: (GenProg.GenExpr.GenExpr E) => Int -> E -> Double
myFitness n e = error + size
  where error = realToFrac $ maybe maxBound (abs . (n-)) (eval e)
        size  = (realToFrac $ nodes e) / 100  
sclv
  • 38,665
  • 7
  • 99
  • 204
zariuq
  • 43
  • 5
  • Looks like you might have a conflicting version of a package installed (this happened to me the other day). Try installing it in a sandbox, or if you already have try deleting the sandbox and starting with a fresh one. – bheklilr Apr 29 '15 at 14:11
  • Hmm, my cabal version didn't seem to support sandoxes (as I saw on a tutorial). So I updated to the Haskell Platform 2014. Now cabalcomplains of compile errors when trying to install GenProg ... Does GenProg (or the example) work for others? (Say on version 7.8.3, 7.10.1, or...) – zariuq Apr 30 '15 at 00:52
  • 1
    Wow, I feel like an idiot :( Adding `import GenProg.GenExpr import GenProg.GenExpr.Data` Got it to work :< (In version 7.6.3 -- the package doesn't seem to compile in other versions :/) With the example in the GitHub version: https://github.com/jsnajder/genprog/blob/master/src/GenProg.hs (which, ugh, is slightly different from the Hackage version) – zariuq Apr 30 '15 at 08:01
  • For posterity, I found that _just_ the `import GenProg.GenExpr.Data` was sufficient (but necessary) – declension Sep 04 '17 at 16:41
  • @zariuq since you found an answer, you you add it and accept it to indicate this question was solved for future readers? – sclv Mar 17 '18 at 00:01

1 Answers1

0

Adding import GenProg.GenExpr.Data got it to work. (In version 7.6.3 -- the package doesn't seem to compile in other versions.) With the example in the GitHub version: github.com/jsnajder/genprog/blob/master/src/GenProg.hs (which, ugh, is slightly different from the Hackage version)

[Thanks for noting the question had been answered in comments.]

zariuq
  • 43
  • 5