| 1 | {-# LANGUAGE -XNoMonomorphismRestriction #-} |
|---|
| 2 | |
|---|
| 3 | import Language.Haskell.Exts |
|---|
| 4 | import qualified Language.Haskell.Interpreter as HI |
|---|
| 5 | import Control.Monad.CatchIO |
|---|
| 6 | import Random |
|---|
| 7 | import Data.List |
|---|
| 8 | import Toolbox |
|---|
| 9 | |
|---|
| 10 | randNames :: (RandomGen rg) => Int -> rg -> ([] Name, rg) |
|---|
| 11 | randNames qty randGen | qty > length namePool = error "namePool insufficent" |
|---|
| 12 | | otherwise = (take qty . drop startFromI $ cycle namePool, nextRandGen) |
|---|
| 13 | where namePool = map name ["doe", "ray", "zim", "foo", "dib", "kaz", "bar"] |
|---|
| 14 | (startFromI, nextRandGen) = randomR (iRng namePool) randGen |
|---|
| 15 | |
|---|
| 16 | randIntE :: (RandomGen rg) => rg -> (Exp, rg) |
|---|
| 17 | randIntE rg1 = let (n, rg2) = randomR (1, 20) rg1 in (intE n, rg2) |
|---|
| 18 | |
|---|
| 19 | boundAs :: Name -> Exp -> Decl |
|---|
| 20 | boundAs = nameBind (SrcLoc "RandGen" 0 0) |
|---|
| 21 | |
|---|
| 22 | q1 :: (RandomGen rg) => rg -> (Decl, rg) |
|---|
| 23 | q1 rg1 = (var `boundAs` val, rg3) |
|---|
| 24 | where (var:_, rg2) = randNames 1 rg1 |
|---|
| 25 | (val, rg3) = randIntE rg2 |
|---|
| 26 | |
|---|
| 27 | --pie :: (Control.Monad.CatchIO.MonadCatchIO m, Functor m, HI.MonadInterpreter m) |
|---|
| 28 | -- => m (Either HI.InterpreterError String) |
|---|
| 29 | pie = do HI.set [HI.installedModulesInScope HI.:= True] |
|---|
| 30 | HI.runInterpreter (HI.eval "10") |
|---|