4 let (|-) g f x = f (g x)
14 val string_with_color : string -> color -> string
16 val clear : unit -> unit
18 val reset : unit -> unit
26 let ansi_code_clear = "\027[2J" (* Clear screen *)
27 let ansi_code_reset = "\027[1;1H" (* Reset cursor position *)
29 let string_of_color = function
30 | `green -> "\027[0;32m"
31 | `red -> "\027[1;31m"
32 | `white -> "\027[1;37m"
34 let string_with_color s c =
35 sprintf "%s%s\027[0m" (string_of_color c) s
38 print_string ansi_code_clear
41 print_string ansi_code_reset
49 type t = {r : int; k : int}
54 val create : rs:int -> ks:int -> 'a -> 'a t
56 val get_neighbors : 'a t -> Point.t -> 'a list
58 val map : 'a t -> f:('a -> 'b) -> 'b t
60 val mapi : 'a t -> f:(Point.t -> 'a -> 'b) -> 'b t
62 val iter : 'a t -> f:(Point.t -> 'a -> unit) -> unit
64 val print : 'a t -> to_string:('a -> string) -> unit
69 type t = {r : int; k : int}
83 let all = [ NW ; N ; NE
91 | NW -> {r = -1; k = -1}
92 | N -> {r = -1; k = 0}
93 | NE -> {r = -1; k = 1}
94 | W -> {r = 0; k = -1}
96 | SW -> {r = 1; k = -1}
98 | SE -> {r = 1; k = 1}
101 type 'a t = 'a array array
103 let create ~rs ~ks x =
104 Array.make_matrix ~dimx:rs ~dimy:ks x
111 f {Point.r; Point.k} x
115 let print t ~to_string =
118 Array.iter r ~f:(fun x -> printf "%s" (to_string x));
123 Array.map t ~f:(Array.map ~f:(fun x -> f x))
130 f {Point.r; Point.k} x
134 let get t {Point.r; Point.k} =
137 let is_within_bounds t {Point.r; Point.k} =
139 | [||] -> assert false
141 r >= 0 && r < Array.length t &&
142 k >= 0 && k < Array.length t.(0)
144 let neighborhood t point =
145 List.map Direction.all ~f:Direction.to_offset
146 |> List.map ~f:(fun offset_point -> Point.(point + offset_point))
147 |> List.filter ~f:(is_within_bounds t)
149 let get_neighbors t point =
150 List.map (neighborhood t point) ~f:(get t)
158 val create : char -> Terminal.color option -> t
160 val to_string : t -> string
163 type t = { color : Terminal.color option
167 let create character color =
170 let to_string = function
171 | {color=None; character} ->
172 String.of_char character
173 | {color=Some c; character} ->
174 Terminal.string_with_color (String.of_char character) c
182 type intention = Friendly
186 type t = Alive of intention
190 type t = { state : State.t
191 ; pheno : PhenoType.t
198 val create : unit -> Cell.t
200 val transition : self:Cell.State.t
201 -> neighbors:Cell.State.t list
212 val of_int : int -> t
214 val to_int : t -> int
216 val to_cell : t -> Cell.t
218 val of_cell_state : Cell.State.t -> t
220 val next : t -> live_neighbors:int -> t
225 let of_int = function
230 let to_int = function
234 let to_pheno = function
235 | D -> PhenoType.create ' ' None
236 | A -> PhenoType.create 'o' (Some `white)
238 let of_cell_state = function
239 | Cell.State.Dead -> D
240 | Cell.State.Alive Cell.State.Friendly -> A
241 | Cell.State.Alive Cell.State.Neutral -> A
242 | Cell.State.Alive Cell.State.Hostile -> D
244 let to_cell_state = function
245 | D -> Cell.State.Dead
246 | A -> Cell.State.Alive Cell.State.Neutral
249 { Cell.state = t |> to_cell_state
250 ; Cell.pheno = t |> to_pheno
253 let next t ~live_neighbors =
255 | A when live_neighbors < 2 -> D
256 | A when live_neighbors < 4 -> A
257 | A when live_neighbors > 3 -> D
258 | D when live_neighbors = 3 -> A
264 Random.int 2 |> State.of_int |> State.to_cell
267 List.map ~f:State.of_cell_state
268 |- List.map ~f:State.to_int
269 |- List.fold_left ~f:(+) ~init:0
271 let transition ~self ~neighbors =
272 self |> State.of_cell_state
273 |> State.next ~live_neighbors:(count_of_live neighbors)
278 module ForestFire : RULE =
284 val is_burning : t -> bool
286 val of_int : int -> t
288 val to_int : t -> int
290 val to_cell : t -> Cell.t
292 val of_cell_state : Cell.State.t -> t
294 val next : t -> burning_neighbors:int -> t
299 let is_burning = function
304 let of_int = function
310 let to_int = function
315 let to_pheno = function
316 | E -> PhenoType.create ' ' None
317 | T -> PhenoType.create 'T' (Some `green)
318 | B -> PhenoType.create '#' (Some `red)
320 let of_cell_state = function
321 | Cell.State.Dead -> E
322 | Cell.State.Alive Cell.State.Friendly -> T
323 | Cell.State.Alive Cell.State.Neutral -> E
324 | Cell.State.Alive Cell.State.Hostile -> B
326 let to_cell_state = function
327 | E -> Cell.State.Dead
328 | T -> Cell.State.Alive Cell.State.Friendly
329 | B -> Cell.State.Alive Cell.State.Hostile
332 { Cell.state = t |> to_cell_state
333 ; Cell.pheno = t |> to_pheno
336 let f = 0.000001 (* Probability of spontaneous ignition *)
337 let p = 0.1 (* Probability of spontaneous growth *)
340 (Random.float 1.0) <= p
342 let next t ~burning_neighbors =
343 match t, burning_neighbors with
344 | E, _ when is_probable p -> T
346 | T, 0 when is_probable f -> B
347 | T, _ when burning_neighbors > 0 -> B
353 Random.int 3 |> State.of_int |> State.to_cell
355 let count_of_burning =
356 List.map ~f:State.of_cell_state
357 |- List.filter ~f:State.is_burning
358 |- List.map ~f:State.to_int
359 |- List.fold_left ~f:(+) ~init:0
361 let transition ~self ~neighbors =
362 self |> State.of_cell_state
363 |> State.next ~burning_neighbors:(count_of_burning neighbors)
372 val create : rows:int
375 -> rules: (module RULE) list
381 type cell = { data : Cell.t
382 ; rule : (module RULE)
385 type t = { grid : cell Matrix.t
386 ; interval : Time.Span.t
390 let create ~rows:rs ~columns:ks ~interval ~rules =
391 let n = List.length rules in
393 let rule = List.nth_exn rules (Random.int n) in
394 let module Rule = (val rule : RULE) in
396 ; data = Rule.create ()
400 { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ())
401 ; interval = Time.Span.of_float interval
402 ; bar = String.make ks '-'
405 let cell_to_string cell =
406 PhenoType.to_string cell.data.Cell.pheno
411 Matrix.print t.grid ~to_string:cell_to_string;
416 Matrix.mapi t.grid ~f:(
417 fun point {rule; data} ->
418 let module Rule = (val rule : RULE) in
419 let neighbors = Matrix.get_neighbors t.grid point in
422 ~self:data.Cell.state
423 ~neighbors:(List.map neighbors ~f:(fun c -> c.data.Cell.state))
432 Time.pause t.interval;
437 let main interval () =
439 let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
441 [ (module Life : RULE)
442 ; (module ForestFire : RULE)
445 Automaton.loop (Automaton.create ~rows:(rows - 3) ~columns ~interval ~rules)
449 let summary = "Polymorphic Cellular Automata" in
450 let spec = Command.Spec.(empty
451 +> flag "-i" (optional_with_default 0.1 float)
452 ~doc:" Induced interval between generations."
455 Command.basic ~summary spec main
458 let () = Command.run spec