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 make : 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
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 make : char -> Terminal.color option -> t
160 val to_string : t -> string
163 type t = { color : Terminal.color option
167 let make 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 init : unit -> Cell.t
200 val transition : self:Cell.State.t
201 -> neighbors:Cell.State.t list
212 val random : unit -> t
214 val is_alive : t -> bool
216 val to_cell : t -> Cell.t
218 val of_cell_state : Cell.State.t -> t
220 val next : t -> live_neighbors:int -> t
226 match Random.int 2 with
231 let is_alive = function
235 let to_pheno = function
236 | D -> PhenoType.make ' ' None
237 | A -> PhenoType.make 'o' (Some `white)
239 let of_cell_state = function
240 | Cell.State.Dead -> D
241 | Cell.State.Alive Cell.State.Friendly -> A
242 | Cell.State.Alive Cell.State.Neutral -> A
243 | Cell.State.Alive Cell.State.Hostile -> D
245 let to_cell_state = function
246 | D -> Cell.State.Dead
247 | A -> Cell.State.Alive Cell.State.Neutral
250 { Cell.state = t |> to_cell_state
251 ; Cell.pheno = t |> to_pheno
254 let next t ~live_neighbors =
256 | A when live_neighbors < 2 -> D
257 | A when live_neighbors < 4 -> A
258 | A when live_neighbors > 3 -> D
259 | D when live_neighbors = 3 -> A
265 State.random |- State.to_cell
268 List.map ~f:State.of_cell_state
269 |- List.filter ~f:State.is_alive
272 let transition ~self ~neighbors =
273 self |> State.of_cell_state
274 |> State.next ~live_neighbors:(count_of_live neighbors)
279 module ForestFire : RULE =
285 val is_burning : t -> bool
287 val random : unit -> t
289 val to_cell : t -> Cell.t
291 val of_cell_state : Cell.State.t -> t
293 val next : t -> burning_neighbors:int -> t
298 let is_burning = function
304 match Random.int 3 with
310 let to_pheno = function
311 | E -> PhenoType.make ' ' None
312 | T -> PhenoType.make 'T' (Some `green)
313 | B -> PhenoType.make '#' (Some `red)
315 let of_cell_state = function
316 | Cell.State.Dead -> E
317 | Cell.State.Alive Cell.State.Friendly -> T
318 | Cell.State.Alive Cell.State.Neutral -> E
319 | Cell.State.Alive Cell.State.Hostile -> B
321 let to_cell_state = function
322 | E -> Cell.State.Dead
323 | T -> Cell.State.Alive Cell.State.Friendly
324 | B -> Cell.State.Alive Cell.State.Hostile
327 { Cell.state = t |> to_cell_state
328 ; Cell.pheno = t |> to_pheno
331 let f = 0.000001 (* Probability of spontaneous ignition *)
332 let p = 0.1 (* Probability of spontaneous growth *)
335 (Random.float 1.0) <= p
337 let next t ~burning_neighbors =
338 match t, burning_neighbors with
339 | E, _ when is_probable p -> T
341 | T, 0 when is_probable f -> B
342 | T, _ when burning_neighbors > 0 -> B
348 State.random |- State.to_cell
350 let count_of_burning =
351 List.map ~f:State.of_cell_state
352 |- List.filter ~f:State.is_burning
355 let transition ~self ~neighbors =
356 self |> State.of_cell_state
357 |> State.next ~burning_neighbors:(count_of_burning neighbors)
369 -> rules: (module RULE) list
375 type cell = { data : Cell.t
376 ; rule : (module RULE)
379 type t = { grid : cell Matrix.t
380 ; interval : Time.Span.t
384 let make ~rows:rs ~columns:ks ~interval ~rules =
385 let n = List.length rules in
387 let rule = List.nth_exn rules (Random.int n) in
388 let module Rule = (val rule : RULE) in
390 ; data = Rule.init ()
394 { grid = Matrix.map ~f:init (Matrix.make ~rs ~ks ())
395 ; interval = Time.Span.of_float interval
396 ; bar = String.make ks '-'
399 let cell_to_string cell =
400 PhenoType.to_string cell.data.Cell.pheno
405 Matrix.print t.grid ~to_string:cell_to_string;
410 Matrix.mapi t.grid ~f:(
411 fun point {rule; data} ->
412 let module Rule = (val rule : RULE) in
413 let neighbors = Matrix.get_neighbors t.grid point in
416 ~self:data.Cell.state
417 ~neighbors:(List.map neighbors ~f:(fun c -> c.data.Cell.state))
426 Time.pause t.interval;
431 let main interval () =
433 let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
435 [ (module Life : RULE)
436 ; (module ForestFire : RULE)
439 Automaton.loop (Automaton.make ~rows:(rows - 3) ~columns ~interval ~rules)
443 let summary = "Polymorphic Cellular Automata" in
444 let spec = Command.Spec.(empty
445 +> flag "-i" (optional_with_default 0.1 float)
446 ~doc:" Induced interval between generations."
449 Command.basic ~summary spec main
452 let () = Command.run spec