Module Transform.
Definition basic (op: Instruction) : bool :=
match op with
| (([], [a], _), ([b], [], _)) => true
| (([a], [], _), ([], [b], _)) => true
| _ => false
end.
Definition weight_Instruction (op: Instruction) : nat :=
if basic op then 0 else
match op with
| ((l, r, _), (l', _, _)) => 1 + length (l ++ l' ++ l ++ r)
end.
Definition weight (M: SMN) : nat :=
fold_right (fun op w => w + weight_Instruction op) 0 M.
Fixpoint fresh_State (M: SMN) : State :=
match M with
| [] => 1
| ((_, _, x), (_, _, y)) :: M => 1 + x + y + fresh_State M
end.

Lemma minimize_weight_length {M: SMN} {a r b r' x y} :
In (([], a :: r, x), ([], b :: r', y)) M ->
weight_Instruction (([], a :: r, x), ([], b :: r', y)) <> 0 ->
1 <= length r ->
confluent M -> length_preserving M ->
{ M' : SMN | weight M' < weight M /\ (confluent M' /\ length_preserving M' /\ ((exists NM, bounded M NM) <-> (exists NM', bounded M' NM'))) }.