type mode = Normal | Insert type t = mode type state = int type 'a state_monad = state -> 'a * state let run (f : 'a state_monad) (s : state) : 'a = f s |> fst let return (a : 'a) : 'a state_monad = fun s -> (a, s) let ( >>= ) (f : 'a state_monad) (g : 'a -> 'b state_monad) : 'b state_monad = fun s -> let a, s' = f s in g a s' let draw () : unit state_monad = return () let get_keypress () : char state_monad = return 'a' let handle_key (_ : char) : unit state_monad = return () let rec loop () = () |> draw >>= get_keypress >>= handle_key >>= loop let test = run (loop ()) 0