Section Prod2List.
Variable A : Type.
Variable f : A -> nat.
(* In the product the sum of the leaves is multiplied by the integer and added to the weight of the tree *)
Definition prod2list l1 l2 :=
fold_left plus
(map2 (fun a b => a * sum_leaves f b + weight_tree f b) l1 l2) 0.
Proof using.
intros l1 l2 l3 l4 H; unfold prod2list in |- *.
rewrite map2_app; auto.
rewrite fold_left_app.
rewrite plus_comm.
apply sym_equal.
repeat
rewrite
fold_left_eta with (f := plus) (f1 := fun a b : nat => a + (fun x => x) b);
auto.
apply sym_equal; rewrite <- fold_plus_split with (f := fun x : nat => x);
auto.
apply plus_comm.
Qed.

(* Permuting two choosen elements lower the product *)
Theorem prod2list_le_l :
forall a b c d l1 l2 l3 l4 l5 l6,
length l1 = length l4 ->
length l2 = length l5 ->
length l3 = length l6 ->
sum_leaves f c <= sum_leaves f d ->
a <= b ->
prod2list (l1 ++ a :: l2 ++ b :: l3) (l4 ++ d :: l5 ++ c :: l6) <=
prod2list (l1 ++ a :: l2 ++ b :: l3) (l4 ++ c :: l5 ++ d :: l6).