Re: [Haskell-cafe] GHCi and State

2010-06-25 Thread Brandon S Allbery KF8NH
-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 
> 
> 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


Re: [Haskell-cafe] GHCi and State

2010-06-25 Thread Roman Beslik

 On 25.06.10 12:07, corentin.dup...@ext.mpsa.com wrote:

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?
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
You can store a set of rules in IORef or another IO-mutable type. I 
think "you are in the IO monad" is pretty vague. Obviously, GHCi runs 
pure computations also.


--
Best regards,
  Roman Beslik.

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


Re: [Haskell-cafe] GHCi and State

2010-06-28 Thread Martin Hilbig

hi,

On 25.06.2010 11:07, corentin.dup...@ext.mpsa.com wrote:



Another couple of reflexions (sorry for monopolizing):

1. Since i am making a Nomic game, players will have to submit rules. These
rules will be written in a sub-set of haskell.
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?


check out hint, a nice wrapper around the ghc api [1].

have fun
martin

[1]: http://hackage.haskell.org/package/hint


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

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?
submitRule would have a type more or less like that (GameState contains the
legislation):

submitRule :: Rule ->  StateT GameState IO ()


Thanks for  your attention! I know this is a bit confused!

Best,
Corentin


___
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: [Haskell-cafe] GHCi and State

2010-06-28 Thread Henning Thielemann



On 25.06.2010 11:07, corentin.dup...@ext.mpsa.com wrote:


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


You can use IORefs for this purpose. However you have to initialize the 
IORef before running the first action and then you must pass the IORef to 
every subsequent action.

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


Re: [Haskell-cafe] GHCi and State

2010-07-02 Thread Matthias Görgens
Hi Corentin,

Interesting.  Have you thought about following the example of XMonad instead?

The analogy could goes as follows:
XMonad's configuration file (~/.xmonad/xmonad.hs) <=> Your rules.
XMonad's state <=> Your state.
Editing the config file <=> Changing the rules.

Of course you normally edit the config file while XMonad is running,
and in theory XMonad could do funny things to your Emacs, i.e. require
you to vote before letting any edit-command through.

When you change your configuration file, you send a specific
key-sequence to XMonad (Alt-q is the default), and XMonad serializes
it's state into a string, compiles the new config file, and then
replaces itself with the new instance, handing over it's state.

No mutable-state-in-IO trickery necessary.

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


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