If you are new to this package, consider checking out CellAutomata.LifeLike first. It is written as an introduction to this module.
If you want to create automatas with more then one state, but that still operates on a 2D-Grid, than this package is the right one for you.
First start by writing your own state type. As an example, lets try to simulate an ant that always follows the right wall.
Our state will now be the following
type State
= Wall
| Up
| Down
| Left
| RightThe grid is the model of this module.
You might want to write your own view function for it or else you can't see what the automata has done.
In your head you should think of this as a grid, where some cells are filled in.
In fact, only the filled cells are stored in the Dict.
Cells have the type Maybe state where state should be a custom type.
The position is the unique identifier for any cell.
For our purpose we use (x,y) coordinates.
Note: The south of (0,0) is (0,y) while the north is (0,-y).
RuleExpressions give us a very flexible way of talking about neighbors.
Saying something is Anything, it means its value is ignored.
The Neighborhood of a cell consists of the 8 surounding cells.
If some neighbor may have any value (that is most often the case),
its best to use the anyNeighborhood template and start from there.
This template helps defining a Neighborhood.
For example, if we would want to only consider the north neighbor, we might specify it the following way
{ anyNeighborhood
| north = Exactly a
}A rule consist of the following elements:
This is the main function.
It has a wierd type, but thats because it is meant to be used with Dict.update:
List.range 0 12
|> List.foldl
(\x g ->
List.range 0 10
|> List.foldl
(\y ->
Dict.update
( x, y )
( ( x, y )
|> step automata grid
)
)
g
)
gridThe Automata type can be seen as a config type.
Its stores all information to specify the behaviour of a cell automata.
Sometimes more then one automata should act on to the same Grid.
For this reason it is its own type.
This function uses no symmetry, this means every possible combination must be specified.
automataWithoutSymmetry =
automataWithCustomSymmetry noSymmetryThis function is not useful in practice. Most often you want at least rotational or mirrored symmetry. This function is only included for demonstration purposes.
Checkout CellAutomata.LifeLike for a more detailed description.
Our example with the ant, can now be implemented the following way:
[ { from = Just Up, neighbors = anyNeighborhood, to = Nothing }
, { from = Just Down, neighbors = anyNeighborhood, to = Nothing }
, { from = Just Left, neighbors = anyNeighborhood, to = Nothing }
, { from = Just Right, neighbors = anyNeighborhood, to = Nothing }
, { from = Nothing, to = Just Up, neighbors = { anyNeighborhood | south = Exactly <| Just Up, southEast = Exactly <| Just Wall } }
, { from = Nothing, to = Just Right, neighbors = { anyNeighborhood | south = Exactly <| Just Wall, west = Exactly <| Just Up } }
, { from = Nothing, to = Just Right, neighbors = { anyNeighborhood | west = Exactly <| Just Right, southWest = Exactly <| Just Wall } }
, { from = Nothing, to = Just Down, neighbors = { anyNeighborhood | west = Exactly <| Just Wall, north = Exactly <| Just Right } }
, { from = Nothing, to = Just Down, neighbors = { anyNeighborhood | north = Exactly <| Just Right, northWest = Exactly <| Just Wall } }
, { from = Nothing, to = Just Left, neighbors = { anyNeighborhood | north = Exactly <| Just Wall, east = Exactly <| Just Down } }
, { from = Nothing, to = Just Left, neighbors = { anyNeighborhood | east = Exactly <| Just Right, northEast = Exactly <| Just Wall } }
, { from = Nothing, to = Just Up, neighbors = { anyNeighborhood | east = Exactly <| Just Wall, south = Exactly <| Just Left } }
]
|> automataWithoutSymmetry orderThis code is on purpose more complicated as it should be. If possible one should always use a custom symmetry.
A symmetry is just a function that determines if a rule is sucessfully applied. If so, it returns the new state.
Every possible way the neighbors might be arranged needs its own rule.
Pattern may be horizontally mirrored
The first argument is a function (state -> state) that states how the values of
the state can be mirrored (horizontally).
Use the identity function if you do not see a need in specifing the first arguement.
As example, given the state
State = Up
| Down
| Left
| RightWe can specify a symmetry the following way
horMirrorSymmetry
(\state ->
case state of
Up ->
Down
Down ->
Up
a ->
a
)Pattern may be vertically mirrored
The first argument is a function (state -> state) that states how the values of the state can be mirrored (vertically). Use the identity function if you dont see a need in specifing the first arguement.
As example, given the state
State = Up
| Down
| Left
| RightWe can specify the symmetry the following way
vertMirrorSymmetry
(\state ->
case state of
Left ->
Right
Right ->
Left
a ->
a
)Pattern may be rotated in any position.
The first argument is a function (state -> state) that states how the values of
the state can be rotated (clock-wise).
Use the identity function if you dont see a need in specifing the first arguement.
As example, given the state
State = North
| NorthEast
| East
| SouthEast
| South
| SouthWest
| West
| NorthWestWe can specify the symmetry the following way
rot45Symmetry
(\state ->
case state of
North ->
NorthEast
NorthEast ->
East
East ->
SouthEast
SouthEast ->
South
South ->
SouthWest
SouthWest ->
West
South ->
NorthWest
NorthWest ->
North
)Pattern may be rotated in 90,180 and 270 degree angles.
The first argument is a function (state -> state) that states how the values of
the state can be rotated (clock-wise).
Use the identity function if you dont see a need in specifing the first arguement.
As example, given the state
State = Left
| Right
| Up
| DownWe can specify the symmetry the following way
rot90Symmetry
(\state ->
case state of
Left ->
Down
Down ->
Right
Right ->
Up
Up ->
Left
)With this function we can now add our own symmetry.
The ant example can now be implemented the following way:
let
rotState : State -> State
rotState state =
case state of
Up ->
Right
Right ->
Down
Down ->
Left
Left ->
Up
a ->
a
in
( List.concat
[ [Up,Left,Right,Down]
|> List.map
(\dir ->
{ from = Just dir
, neighbors = anyNeighborhood
, to = Nothing
}
)
, [ { from = Nothing
, to = Just Up
, neighbors =
{ anyNeighborhood
| south = Exactly <| Just Up
, southEast = Exactly <| Just Wall
}
}
, { from = Nothing
, to = Just Right
, neighbors =
{ anyNeighborhood
| south = Exactly <| Just Wall
, west = Exactly <| Just Up }
}
, { from = Nothing
, to = Just Right
, neighbors =
{ anyNeighborhood
| southWest = Exactly <| Just Wall
, west = Exactly <| Just Down }
}
]
]
)
|> automata (rot90Symmetry rotState) orderTransforms a neighborhood.
It takes a function a -> b in order to transform a Neighborhood a to Neighborhood b
Every state needs a defined order. (A function that gives each state a unique identifier)
For our ant example we define the following order: