+module State = struct
+ type t = string
+end
+
+
+module PhenoType = struct
+ type t = string
+end
+
+
+module Cell = struct
+ type t = { msg : Msg.t
+ ; pheno : PhenoType.t
+ ; state : State.t
+ }
+end
+
+
+module type RULE = sig
+ val create : unit -> Cell.t
+
+ val transition : state:State.t -> inputs:Msg.t list -> Cell.t
+end
+
+
+module Conway : RULE = struct
+ type state = D | A
+
+ let state_of_string : (string -> state) = function
+ | "D" -> D
+ | "A" -> A
+ | _ -> assert false
+
+ let state_of_int : (int -> state) = function
+ | 0 -> D
+ | 1 -> A
+ | _ -> assert false
+
+ let int_of_state : (state -> int) = function
+ | D -> 0
+ | A -> 1
+
+ let string_of_state : (state -> string) = function
+ | D -> "D"
+ | A -> "A"
+
+ let msg_of_state : (state -> Msg.t) =
+ string_of_state
+
+ let pheno_of_state : (state -> PhenoType.t) = function
+ | D -> " "
+ | A -> "o"
+
+ let int_of_msg msg =
+ msg |> state_of_string |> int_of_state
+
+ let next state ~live_neighbors =
+ match state with
+ | A when live_neighbors < 2 -> D
+ | A when live_neighbors < 4 -> A
+ | A when live_neighbors > 3 -> D
+ | D when live_neighbors = 3 -> A
+ | A -> A
+ | D -> D
+
+ let cell_of_state s =
+ { Cell.msg = s |> msg_of_state
+ ; Cell.pheno = s |> pheno_of_state
+ ; Cell.state = s |> string_of_state
+ }
+
+ let create () =
+ Random.int 2 |> state_of_int |> cell_of_state
+
+ let live_neighbors inputs =
+ inputs |> List.map ~f:int_of_msg |> List.fold_left ~init:0 ~f:(+)
+
+ let transition ~state ~inputs =
+ state
+ |> state_of_string
+ |> next ~live_neighbors:(live_neighbors inputs)
+ |> cell_of_state
+end
+
+
+module Automaton : sig