"Combining" two mealy machines
A Mealy machine consists of a state set $S$, an initial state $s \in S$, and a transition function $A \times S \to B \times S$ (which takes as input a letter from the alphabet $A$ and a current state $S$, and produces an output letter drawn from $B$ and an updated state from $S$). 

We can write this as a type declaration in Ocaml like so:

type ('a,'b) mealy = M : 's * ('a * 's -&gt; 'b * 's) -&gt; ('a,'b) mealy


Then the compose operation in looks like this: 

let compose : type a b c. (a,b) mealy -&gt; (b,c) mealy -&gt; (a,c) mealy = 
  fun (M(s, f)) (M(t, g)) -&gt; 
    let state = (s,t) in 
    let trans (a, (s, t)) = 
      let (b, s') = f(a, s) in
      let (c, t') = g(b, t) in 
      (c, (s',t')) 
    in
    M(state, trans)


So the state of the composite machine are the products of the two state sets, and the new initial state is the pairing of the two initial states, and the composite transition function feeds the input to the first transition function and uses its output as the input to the second transition function.