Réf. : Re: [Haskell-cafe] GHCi and State

2010-06-25 Thread corentin . dupont
Hello,
thank you for your answer.

Brandon,
Indeed I think that i should write my own interpreter for a first version
of the game. I would be very instructive.

But then, i'd like that the player could use the full power of Haskell to
write their own rules during game play.
Neat functions like map, filter etc. could be used by the player to write
rules.
Perhaps Hint is good for me.

 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?)

The code passed is not that arbitrary, it must have type Rule.
This type would enforce certain constructions, and actions...

Roman,
for GHCi, i will try an IORef. Too bad i allready coded it using StateT
GameState IO () extensively through the code ;)

Corentin



   
 Brandon S 
 Allbery KF8NH 
 allb...@ece.cmu Pour
 .edu haskell-cafe@haskell.org
 Envoyé par :   cc
 haskell-cafe-bou  
 n...@haskell.orgObjet
   Re: [Haskell-cafe] GHCi and State
   
 25/06/2010 11:30  
   
   
   
   




-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 6/25/10 05:07 , corentin.dup...@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]  allb...@kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allb...@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-
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Réf. : Re: [Haskell-cafe] GHCi and State

2010-06-25 Thread Tillmann Rendel

Hi Corentin,

corentin.dup...@ext.mpsa.com schrieb:

for GHCi, i will try an IORef. Too bad i allready coded it using StateT
GameState IO () extensively through the code ;)


That shouldn't be a problem, you could switch back and forth in 
submitRule, approximately like this:


  startGame :: IO (Rule - IO ())
  startGame = do
gameState - newIORef initialState
return (\rule - wrapStateT (submitRule rule) gameState)

  wrapStateT ::  StateT s IO a - IORef s - IO a
  wrapStateT action ref = do
state - readIORef ref
(answer, state) - runStateT action state
writeIORef ref state
return answer

Now you can start a game with

  mySubmitRule - startGame

and use mySubmitRule at the ghci prompt afterwards.

  Tillmann
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe