[Haskell-cafe] GHCi and State

Brandon S Allbery KF8NH allbery at ece.cmu.edu
Fri Jun 25 05:30:32 EDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6/25/10 05:07 , corentin.dupont at ext.mpsa.com wrote:
> Instead of writing my own reader/interpreter, i'd like to use GHC to compil
> them on the fly, and then add them to the current "legislation".
> What would you suggest me to do that? Any pointers?

GHC API.  This is likely biting off more than you want to chew, though;
it'll probably be easier to write your own interpreter,

> 2. For now, the game is more or less playable in GHCi. But my concern is:
> When you use GHCi, you are in the IO monad, right? How to had state to this
> monad?

  runStateT nomicGame initialState :: IO (a,GameState)
  -- nomicGame :: StateT GameState IO a
  -- initialState :: GameState
  -- use evalStateT instead of runStateT if all you want is the result,
  -- or execStateT if all you want is the final state.
  -- if you want neither:
  --   _ <- runStateT ...

> I would like that the player can compose his rule in GHCi, and when he is
> done, he can submit it in GHCi with something like:
> 
> *Nomic> submitRule <myrule>
> 
> And then the game takes the rule, possibly modify the current legislation,
> and give the hand back to GHCi.
> So the "current legislation" has to be a state of the GHCi's loop. Is this
> possible at all?

Use an IORef to contain the state, if you really want to go this way.  I
wouldn't; take a look at the lambdabot source for the pitfalls of passing
arbitrary user-provided code to GHCi (or GHC API), and how to avoid them.

(In particular, if you're using GHC to parse your rules, what stops the user
code from mangling the GameState on you?)

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allbery at kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwkdygACgkQIn7hlCsL25WfhgCgo2qfkoA0yBaXsrjQNT+xePSb
vJMAnjLQnOtaByKXSsFvLuclcFt7vhEg
=jnru
-----END PGP SIGNATURE-----


More information about the Haskell-Cafe mailing list