CA-DSL: A DSL for designing and visualizing cellular automata
Jordan Zedeck <zedeck.j@northeastern.edu> Nate White <natewhite345@gmail.com>
Cellular Automata are a beautiful demonstration of how complexity can emerge from simple rules. This library helps visualize and explore them.
(require ca-dsl) | package: ca-dsl |
1 Types
1.1 Posn
procedure
(posn-scale n posn) → Posn
n : Integer posn : Posn
1.2 States
syntax
(define-states states-name : type (state-val ...))
A type named type representing the union of all states
A list states-name containing all states
Individual bindings for each state-val
The library comes with a predefined state type:
type
value
value
1.3 Core Types
C: The type of cells (e.g., Posn for cartesian grids)
O: The type of offsets (e.g., Posn for cartesian neighborhoods)
S: The type of states (e.g., AliveOrDead)
type constructor
(StateMap C S)
type constructor
(Topology C O)
type constructor
(Neighborhood O)
type constructor
(Rule C O S)
type constructor
(ActiveFilter C)
type constructor
(ColorMap S)
struct
(struct World C (state-map topology active-filter) #:transparent) state-map : (StateMap C S) topology : (Topology C O) active-filter : (ActiveFilter C)
type constructor
(Renderer C O S)
1.4 Specialized Types
The library provides specialized types for common cellular automata patterns:
type constructor
(2DWorld S)
type constructor
(2DRenderer S)
type
type
type
2 Run
3 Rule
This section documents the macros exported by the rule module, which allow for defining cellular automata rules in various formats.
syntax
(rule #:cell-type cell-type #:offset-type offset-type #:state-type state-type #:neighborhood neighborhood clause ...)
#:cell-type - The type used to represent cells in the grid
#:offset-type - The type used to represent offsets within the neighborhood
#:state-type - The type used to represent cell states
#:neighborhood - An expression that evaluates to a Neighborhood of offsets
clause ... - One or more transition rules that determine state changes
from-state is the initial state
to-state is the resulting state
condition ... are optional conditions that must be satisfied for the transition to occur
Also supported is chaining multiple state transitions controlled by the same condition using (state1->state2->state3 ...) condition ... as well as specifying any state using _.
count in state
(count₁ count₂ ...) in state
- Tests if the number of neighbors in state matches any of the countsall in state
- Tests if all neighbors are in statesome in state
- Tests if at least one neighbor is in stateBoolean operators to join anything above in order of highest to lowest precedence: not, and/nand, xor, or/nor, implies
syntax
(moore-rule #:state-type state-type clause ...)
syntax
(lifelike [(born condition ...)] [(survive condition ...)])
Dead cells become alive if they satisfy the born conditions
Living cells stay alive if they satisfy the survive conditions
All other cells become or stay dead
This provides a concise way to define various "Life-like" cellular automata such as Conway’s Game of Life, HighLife, Day & Night, and others.
4 Renderer
This section documents the functions and constants exported by the renderer module, which provides visualization capabilities for cellular automata.
procedure
(make-2d-renderer color-map [ #:cell-width-px cell-width-px #:origin-px origin-px]) → (Renderer Posn O S) color-map : (ColorMap S) cell-width-px : Positive-Integer = 25 origin-px : Posn = (Posn 0 0)
color-map - A function that maps states to colors for visualization
#:cell-width-px - The width of each cell in pixels (default: 25)
#:origin-px - The position of the top-left corner of the viewport in the world coordinate system (default: (0,0))
The returned function has type (World Posn O S) -> Image, which renders the current state of the world to an image.
Shows cells with states as colored squares based on the color-map
Shows cells not in the state map as gray squares with an "x"
Draws a grid of cell outlines in black
Crops the view to show only what fits within the window dimensions
5 Colormaps
This section documents the functions and constants exported by the colormaps module, which provides color mapping capabilities for visualizing cellular automata states.
5.1 Predefined Colors
value
BLACK : Color
value
WHITE : Color
value
RED : Color
value
GREEN : Color
value
BLUE : Color
value
YELLOW : Color
value
PURPLE : Color
value
PINK : Color
value
ORANGE : Color
value
GRAY : Color
value
TRANSPARENT : Color
value
COLOR_LIST : (Listof Color)
5.2 Colormap Functions
procedure
(colormap-alive-or-dead state) → Color
state : AliveOrDead
procedure
(make-default-colormap states) → (ColorMap S)
states : (Listof S)
Raises an error if asked to map a state not in the original list.
procedure
(make-grayscale-colormap minimum maximum) → (ColorMap Integer)
minimum : Integer maximum : Integer
minimum - Maps to black (RGB: 0,0,0)
maximum - Maps to white (RGB: 255,255,255)
Values between minimum and maximum are mapped to proportional shades of gray. Values less than minimum are clamped to black, and values greater than maximum are clamped to white.
6 Neighborhoods
This section documents the functions exported by the neighborhoods module, which provides predefined neighborhood patterns for cellular automata.
procedure
(moore-neighborhood-outline distance) → (Neighborhood Posn)
distance : Positive-Integer
The function returns a set containing positions that are exactly distance units away from the center (in a square pattern). This includes all positions along the perimeter of a square with side length 2 * distance + 1.
distance - The distance from the center to the outline positions
For example, (moore-neighborhood-outline 1) returns a set containing the 8 positions that are exactly 1 unit away from the center, which forms the standard Moore neighborhood used in cellular automata like Conway’s Game of Life.
procedure
(moore-neighborhood [distance]) → (Neighborhood Posn)
distance : Positive-Integer = 1
The function returns a set containing all positions within distance units from the center (in a square pattern, using the Chebyshev distance).
distance - The maximum distance from the center (default: 1)
(moore-neighborhood) returns the standard 8-cell Moore neighborhood
(moore-neighborhood 2) returns a 24-cell extended Moore neighborhood
The function works by unioning the outlines at each layer from 1 to distance.
7 Grid Utilities
This library provides utilities for creating and manipulating grid-based cellular automata and other grid-based simulations.
procedure
(rect-custom width height state-fn) → hash?
width : integer? height : integer? state-fn : (-> posn? any/c)
The function state-fn is called with each position in the rectangle to determine the state for that cell.
The function state-generator is called for each cell to generate its state.
procedure
(rect-solid width height state) → hash?
width : integer? height : integer? state : any/c
All cells in the rectangle will have the same state value.
procedure
(overlay/statemaps topology offset statemap ...) → hash? topology : any/c offset : any/c statemap : hash?
Arguments alternate between an absolute position and then a statemap to be placed with its lower left corner at that absolute position. Similar in functionality to overlay/xy in the htdp2/image library.
procedure
(biased-random-select weighted-sequence) → (-> any/c)
weighted-sequence : (listof (cons/c any/c exact-nonnegative-integer?))
weighted-sequence is a list of pairs, where each pair consists of a value and its corresponding weight (a non-negative integer). The returned function will select values with probability proportional to their weights.
syntax
(path : type-id (x-expr y-expr) segment ...)
segment = (state-expr magnitude-expr direction state-expr ...) direction = up | down | left | right
type-id - The type name for the states in the path
x-expr y-expr - The starting position of the path
state-expr - The state for the current segment
magnitude-expr - The length of the current segment
direction - The direction of the current segment (up, down, left, or right)
Multiple segments can be chained together to create complex paths.
(path : AliveOrDead (-2 3) ('dead 3 up 5 right 3 down 1 left))
8 Grid Topologies
This library provides functions for creating and manipulating different grid topologies for cellular automata and other grid-based simulations.
procedure
(cartesian-topology pos offset) → posn?
pos : posn? offset : posn?
Returns a new position that is the sum of the input position and offset.
procedure
(truncate-topology topology predicate)
→ (-> any/c any/c (or/c any/c void?)) topology : (-> any/c any/c (or/c any/c void?)) predicate : (-> any/c any/c any/c boolean?)
Returns a new topology function that respects the given predicate.
procedure
(modify-topology topology modifier ...)
→ (-> any/c any/c (or/c any/c void?)) topology : (-> any/c any/c (or/c any/c void?)) modifier : (-> any/c (or/c any/c void?))
Returns a new topology function with the modifiers applied.
procedure
(make-finite-cartesian-topology max-x max-y) → (-> posn? posn? (or/c posn? void?)) max-x : exact-positive-integer? max-y : exact-positive-integer?
Outputs cells from the cartesian topology which have x values or y values with absolute values greater than the given max-x or max-y are turned to Void.
procedure
(in-cartesian-region point max-point [ #:origin origin]) → boolean? point : posn? max-point : posn? origin : posn? = (posn 0 0)
A point is in the region if its coordinates are both greater than or equal to the origin’s coordinates and less than or equal to the max-point’s coordinates.
procedure
(make-wrapping-cartesian-topology x-min x-max y-min y-max) → (-> posn? posn? posn?) x-min : integer? x-max : integer? y-min : integer? y-max : integer?
In this topology, if a coordinate exceeds the maximum value, it wraps around to the minimum value, and vice versa.
procedure
(init-2d-world max-x max-y state-initializer) → any/c max-x : exact-positive-integer? max-y : exact-positive-integer? state-initializer : (-> posn? any/c)
The state-initializer function is used to set the starting State of each Cell in the StateMap of the world.
syntax
(define-2d-world world : state-type #:state-map statemap-expr #:active-filter active-filter-expr #:topology topology-expr)
statemap-expr : hash?
active-filter-expr : (-> posn? boolean?)
topology-expr : (-> posn? posn? (or/c posn? void?))
world - The identifier to bind the new world to
state-type - The type of state values in the world
statemap-expr - Expression producing the state map for the world
active-filter-expr - Optional expression producing an active filter function
topology-expr - Optional expression producing a topology function
The #:active-filter and #:topology parameters are optional.