Ticket #3887: qgen2.hs

File qgen2.hs, 1.1 KB (added by osaunders, 3 years ago)
Line 
1{-# LANGUAGE -XNoMonomorphismRestriction #-}
2
3import Language.Haskell.Exts
4import qualified Language.Haskell.Interpreter as HI
5import Control.Monad.CatchIO
6import Random
7import Data.List
8import Toolbox
9
10randNames :: (RandomGen rg) => Int -> rg -> ([] Name, rg)
11randNames 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
16randIntE :: (RandomGen rg) => rg -> (Exp, rg)
17randIntE rg1 = let (n, rg2) = randomR (1, 20) rg1 in (intE n, rg2)
18
19boundAs :: Name -> Exp -> Decl
20boundAs = nameBind (SrcLoc "RandGen" 0 0)
21
22q1 :: (RandomGen rg) => rg -> (Decl, rg)
23q1 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)
29pie = do HI.set [HI.installedModulesInScope HI.:= True]
30         HI.runInterpreter (HI.eval "10")