{-# LANGUAGE -XNoMonomorphismRestriction #-}

import Language.Haskell.Exts
import qualified Language.Haskell.Interpreter as HI
import Control.Monad.CatchIO
import Random
import Data.List
import Toolbox

randNames :: (RandomGen rg) => Int -> rg -> ([] Name, rg)
randNames qty randGen | qty > length namePool = error "namePool insufficent"
                         | otherwise = (take qty . drop startFromI $ cycle namePool, nextRandGen)
  where namePool = map name ["doe", "ray", "zim", "foo", "dib", "kaz", "bar"]
        (startFromI, nextRandGen) = randomR (iRng namePool) randGen

randIntE :: (RandomGen rg) => rg -> (Exp, rg)
randIntE rg1 = let (n, rg2) = randomR (1, 20) rg1 in (intE n, rg2)

boundAs :: Name -> Exp -> Decl
boundAs = nameBind (SrcLoc "RandGen" 0 0)

q1 :: (RandomGen rg) => rg -> (Decl, rg)
q1 rg1 = (var `boundAs` val, rg3)
  where (var:_, rg2) = randNames 1 rg1
        (val, rg3) = randIntE rg2

--pie :: (Control.Monad.CatchIO.MonadCatchIO m, Functor m, HI.MonadInterpreter m)
--    => m (Either HI.InterpreterError String)
pie = do HI.set [HI.installedModulesInScope HI.:= True]
         HI.runInterpreter (HI.eval "10")