Fixpoint reduce (q : queue E) : Z :=
  match q with
  | empty => 0%Z
  | input d q' => (reduce q' + f d)%Z
  end.

Lemma reduce_append : forall (q1 : queue E) (d : E) (q2 : queue E), reduce (append E q1 (input E d q2)) = (reduce (append E q1 q2) + f d)%Z.