[Haskell-cafe] modelling problem

Kurt Schelfthout kurt.schelfthout at gmail.com
Fri Dec 8 05:11:35 EST 2006


Hi Haskell'ers,

I'm trying, more as a first excercise in Haskell than anything else, to  
code a simulation framework in Haskell. A simulation is a bunch of  
simulation state consisting of constituents (e.g. physical entities like a  
ball or properties like temperature), on which agents (e.g. simulated  
humans) in the system can do activities. Activities have a certain  
duration, and define how the simulation progresses.

The simulator framework should just define the basics to maintain a normal  
representation of time and of cause and effect, i.e. make sure that the  
state evolves through the application of the activities in the right  
order. In a later step I will add "laws", which transform activities when  
they overlap or conflict (e.g. two drive activities leading to collision).

I've run into the following problem.

I defined a class that allows to define activities on constituents:

class Activity a c where
     start       :: c -> a -> Time  --start of the activity (this isn't  
actually dependent on c, I guess)
     end         :: c -> a -> Time  --end of the activity
     delta       :: a -> Time -> c -> c --how the constituent is changed at  
the given time

Two parameter type class because some activities are only applicable to  
certain constituents:

instance Activity Paint Ball where
instance Activity Move Ball where
instance Activity Paint Wall where
but you can't move a wall for example, so no instance for Move Wall.

My question is:

How can I now represent the state of the simulation (i.e. all activites on  
all constituents). E.g. a list of activities won't do since the list is  
heterogeneous (i.e. [Paint Ball White, Move Ball (2,0)])
I know about existentials, but I'm at a loss at how to implement the  
"wrapper" datatype that is exemplified on  
http://www.haskell.org/hawiki/ExistentialTypes since my class has two  
parameters, in fact I'm at a loss at how to use existentials here  
completely.

Then, how could I go back from the "general" lists (or whatever datatype)  
of [a]'s and [c]'s, to a list of [([a],c)] of all activities a that are  
applicable to a certain constituent c? I can't seem to be able to use the  
Typeable class for example, since this can not "cast" to typeclasses, only  
to concrete types (I think...).

What I initially liked about this idea is that it can be encoded in the  
typesystem which activities apply to which consituents, but now it seems  
like I will have to encode it in the simulation framework more directly  
(i.e. giving each consituent a String name to encode its "type").

More straightforward ways of modelling this problem (avoiding multiple  
type class parameters and existentials :) )are also welcome.

thanks for any pointers,

Kurt


More information about the Haskell-Cafe mailing list