9 val string_with_color : string -> color -> string
11 val clear : unit -> unit
13 val reset : unit -> unit
19 let ansi_code_clear = "\027[2J" (* Clear screen *)
20 let ansi_code_reset = "\027[1;1H" (* Reset cursor position *)
22 let string_of_color = function
23 | `green -> "\027[0;32m"
24 | `red -> "\027[1;31m"
26 let string_with_color s c =
27 sprintf "%s%s\027[0m" (string_of_color c) s
30 print_string ansi_code_clear
33 print_string ansi_code_reset
37 module type MATRIX = sig
39 type t = {r : int; k : int}
44 val create : rs:int -> ks:int -> 'a -> 'a t
46 val get_neighbors : 'a t -> Point.t -> 'a list
48 val map : 'a t -> f:('a -> 'b) -> 'b t
50 val mapi : 'a t -> f:(Point.t -> 'a -> 'b) -> 'b t
52 val iter : 'a t -> f:(Point.t -> 'a -> unit) -> unit
54 val print : 'a t -> to_string:('a -> string) -> unit
57 module Matrix : MATRIX = struct
59 type t = {r : int; k : int}
67 module Direction = struct
72 let all = [ NW ; N ; NE
80 | NW -> {r = -1; k = -1}
81 | N -> {r = -1; k = 0}
82 | NE -> {r = -1; k = 1}
83 | W -> {r = 0; k = -1}
85 | SW -> {r = 1; k = -1}
87 | SE -> {r = 1; k = 1}
90 type 'a t = 'a array array
92 let create ~rs ~ks x =
93 Array.make_matrix ~dimx:rs ~dimy:ks x
100 f {Point.r; Point.k} x
104 let print t ~to_string =
107 Array.iter r ~f:(fun x -> printf "%s" (to_string x));
112 Array.map t ~f:(Array.map ~f:(fun x -> f x))
119 f {Point.r; Point.k} x
123 let get t {Point.r; Point.k} =
126 let is_within_bounds t {Point.r; Point.k} =
128 | [||] -> assert false
130 r >= 0 && r < Array.length t &&
131 k >= 0 && k < Array.length t.(0)
133 let neighborhood t point =
134 List.map Direction.all ~f:Direction.to_offset
135 |> List.map ~f:(fun offset_point -> Point.(point + offset_point))
136 |> List.filter ~f:(is_within_bounds t)
138 let get_neighbors t point =
139 List.map (neighborhood t point) ~f:(get t)
148 module State = struct
153 module PhenoType : sig
156 val create : char -> Terminal.color option -> t
158 val to_string : t -> string
160 type t = { color : Terminal.color option
164 let create character color =
167 let to_string = function
168 | {color=None; character} ->
169 String.of_char character
170 | {color=Some c; character} ->
171 Terminal.string_with_color (String.of_char character) c
176 type t = { msg : Msg.t
177 ; pheno : PhenoType.t
183 module type RULE = sig
184 val create : unit -> Cell.t
186 val transition : state:State.t -> inputs:Msg.t list -> Cell.t
190 module Life : RULE = struct
193 let state_of_string : (string -> state) = function
198 let state_of_int : (int -> state) = function
203 let int_of_state : (state -> int) = function
207 let string_of_state : (state -> string) = function
211 let msg_of_state : (state -> Msg.t) =
214 let pheno_of_state : (state -> PhenoType.t) = function
215 | D -> PhenoType.create ' ' None
216 | A -> PhenoType.create 'o' None
219 msg |> state_of_string |> int_of_state
221 let next state ~live_neighbors =
223 | A when live_neighbors < 2 -> D
224 | A when live_neighbors < 4 -> A
225 | A when live_neighbors > 3 -> D
226 | D when live_neighbors = 3 -> A
230 let cell_of_state s =
231 { Cell.msg = s |> msg_of_state
232 ; Cell.pheno = s |> pheno_of_state
233 ; Cell.state = s |> string_of_state
237 Random.int 2 |> state_of_int |> cell_of_state
239 let live_neighbors inputs =
241 |> List.filter ~f:(function "D" | "A" -> true | _ -> false)
242 |> List.map ~f:int_of_msg
243 |> List.fold_left ~init:0 ~f:(+)
245 let transition ~state ~inputs =
248 |> next ~live_neighbors:(live_neighbors inputs)
253 module ForestFire : RULE = struct
254 type state = E | T | B
256 let string_of_state : (state -> string) = function
261 let msg_of_state : (state -> Msg.t) =
264 let pheno_of_state : (state -> PhenoType.t) = function
265 | E -> PhenoType.create ' ' None
266 | T -> PhenoType.create 'T' (Some `green)
267 | B -> PhenoType.create '#' (Some `red)
269 let cell_of_state s =
270 { Cell.msg = s |> msg_of_state
271 ; Cell.pheno = s |> pheno_of_state
272 ; Cell.state = s |> string_of_state
275 let state_of_string : (string -> state) = function
281 let state_of_int : (int -> state) = function
288 Random.int 3 |> state_of_int |> cell_of_state
290 let f = 0.000001 (* Probability of spontaneous ignition *)
291 let p = 0.1 (* Probability of spontaneous growth *)
294 (Random.float 1.0) <= p
296 let next state ~burning_neighbors =
297 match state, burning_neighbors with
298 | E, _ when is_probable p -> T
300 | T, 0 when is_probable f -> B
301 | T, _ when burning_neighbors > 0 -> B
305 let burning_neighbors inputs =
307 |> List.filter_map ~f:(function "B" -> Some 1 | _ -> None)
308 |> List.fold_left ~init:0 ~f:(+)
310 let transition ~state ~inputs =
313 |> next ~burning_neighbors:(burning_neighbors inputs)
318 module Automaton : sig
321 val create : rows:int
324 -> rules: (module RULE) list
329 type cell = { data : Cell.t
330 ; rule : (module RULE)
333 type t = { grid : cell Matrix.t
334 ; interval : Time.Span.t
338 let create ~rows:rs ~columns:ks ~interval ~rules =
339 let n = List.length rules in
341 let rule = List.nth_exn rules (Random.int n) in
342 let module Rule = (val rule : RULE) in
344 ; data = Rule.create ()
348 { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ())
349 ; interval = Time.Span.of_float interval
350 ; bar = String.make ks '-'
353 let cell_to_string cell =
354 PhenoType.to_string cell.data.Cell.pheno
359 Matrix.print t.grid ~to_string:cell_to_string;
364 Matrix.mapi t.grid ~f:(
365 fun point {rule; data} ->
366 let module Rule = (val rule : RULE) in
367 let neighbors = Matrix.get_neighbors t.grid point in
370 ~state:data.Cell.state
371 ~inputs:(List.map neighbors ~f:(fun cell -> cell.data.Cell.msg))
380 Time.pause t.interval;
387 let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
388 let interval = 0.1 in
390 [ (module Life : RULE)
391 ; (module ForestFire : RULE)
394 Automaton.loop (Automaton.create ~rows:(rows - 3) ~columns ~interval ~rules)
398 let summary = "Polymorphic Cellular Automata" in
399 let spec = Command.Spec.empty in
400 Command.basic ~summary spec main
403 let () = Command.run spec