Using a monad to decompose a function into functions

Hi, I'm doing a bit of research into mobility models and I'm currently exploring implementation language choices for the simulator (yes, sadly it needs to be a custom one). I've been using Haskell here and there for some small tasks, and thought I should consider it as an implementation language for the simulator. While I already have an working implementation in Haskell, there is one thing that I would like to express in a more elegant way, but just can't figure out. The simulation algorithm requires expressing the node's mobility so that it is "stateless". The mobility model algorithm's type should be something like: mobility_model :: WorldState -> NodeState -> OtherInput -> (Action, NodeState) where Action can alter WorldState and the second NodeState is an altered input NodeState. I perform a form of speculative execution on mobility_model so sometimes I need to backtrack to a previous world and node state. This is all fairly simple stuff, and was just an introduction. What I do now is store an enum in NodeState and implement mobility_model as one big case statement. Yes, this is very imperative of me, I know. What I'd like to do is to express mobility_model, so that the code would look like: mobility_model world node input = do do_calculus emit_action if something then emit_action else emit_action do_calculus emit_action mobility_model world node input but I'd like to be able to alter world and node state before continuing from emit_action. I've tried to get this working by using the idea from http://www.haskell.org/pipermail/haskell/2005-April/015684.html but couldn't get the state-altering behavior I was looking for. I've also taken a look at http://monadicheadaches.blogspot.com/2008/01/python-25s-iterators-in-haskell..., the unified concurrency model and Control.Coroutine, but couldn't get the behavior I was going for. Thanks! Marcin Kosiba

2009/3/12 Marcin Kosiba
Hi, I'm doing a bit of research into mobility models and I'm currently exploring implementation language choices for the simulator (yes, sadly it needs to be a custom one). I've been using Haskell here and there for some small tasks, and thought I should consider it as an implementation language for the simulator. While I already have an working implementation in Haskell, there is one thing that I would like to express in a more elegant way, but just can't figure out. The simulation algorithm requires expressing the node's mobility so that it is "stateless". The mobility model algorithm's type should be something like: mobility_model :: WorldState -> NodeState -> OtherInput -> (Action, NodeState)
where Action can alter WorldState and the second NodeState is an altered input NodeState. I perform a form of speculative execution on mobility_model so sometimes I need to backtrack to a previous world and node state. This is all fairly simple stuff, and was just an introduction. What I do now is store an enum in NodeState and implement mobility_model as one big case statement. Yes, this is very imperative of me, I know. What I'd like to do is to express mobility_model, so that the code would look like:
mobility_model world node input = do do_calculus emit_action if something then emit_action else emit_action do_calculus emit_action mobility_model world node input
Hi, It seems you can use http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Mona... Just have a look at the exemple : tick :: State Int Int tick = do n <- get put (n+1) return n your code would become something like mobility_model :: OtherInput -> State (WorldState,NodeState) () mobility_model input = do world <- gets fst node <- gets snd .... let (world',node') = ... put (world',node') HTH, Thu
participants (2)
-
Marcin Kosiba
-
minh thu