ca-patterns- Manipulate patterns in cellular automata, create and parse RLE files
Safe HaskellNone



The RLE (run length encoded) format is a common way of representing patterns in life-like cellular automata. RLE files consist of three sections:

  1. Zero or more comment lines beginning with #
  2. A header of the form x = [width], y = [height], rule = [rule]. [width] and [height] are natural numbers, and [rule] is the rule the pattern is meant to be run in, such as B36/S23. The "rule" field is optional.
  3. The content of the pattern. b represents a dead cell, o represents a live cell, and $ denotes the end of a row. A run of identical characters can be abbreviated with a number, e.g. 4o is short for oooo (hence the name "run length encoded"). This section must be terminated by a ! character.

A glider in the Game of Life could be represented like so:

#N glider
x = 3, y = 3, rule = B3/S23

See this LifeWiki article for more information.



type Rule = String Source #

A string representing a cellular automaton, such as "B36/S23" or "B3/S2-i34q".

parse :: Stream s Identity Char => s -> Maybe (Maybe Rule, Pattern) Source #

Parse an RLE file, returning a Rule (if it exists) and a Pattern. The argument can be String, Text, or any other type with a Stream instance.

Whitespace is allowed everywhere except in the middle of a number or rulestring, and there need not be a newline after the header. Also, text after the final ! character is ignored.

parseMany :: Stream s Identity Char => s -> Maybe [(Maybe Rule, Pattern)] Source #

Parse zero or more RLE files. The argument can be String, Text, or any other type with a Stream instance.

make :: (Semigroup s, IsString s) => Maybe Rule -> Pattern -> s Source #

Convert a Pattern into an RLE. If the first argument is Nothing, the generated RLE will have no "rule" field in its header.

printAll :: Maybe Rule -> [Pattern] -> IO () Source #

Convert a list of patterns into RLEs and print them. Example:

import qualified Text.RLE as RLE
import Data.CA.List (withDimensions)
main = RLE.printAll (Just "B3/S23") (withDimensions 4 4)

The program above will print the RLE of every possible pattern contained in a 4 by 4 square. This data can then be piped into another program such as apgsearch.