(* 
 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.

(* Specifications: *)
Theorem prod2list_app : forall l1 l2 l3 l4, length l1 = length l2 -> prod2list (l1 ++ l3) (l2 ++ l4) = prod2list l1 l2 + prod2list l3 l4.
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).
Theorem prod2list_le_r : 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 d <= sum_leaves f c -> b <= a -> prod2list (l1 ++ a :: l2 ++ b :: l3) (l4 ++ d :: l5 ++ c :: l6) <= prod2list (l1 ++ a :: l2 ++ b :: l3) (l4 ++ c :: l5 ++ d :: l6).
Theorem prod2list_eq : forall a b c l1 l2 l3 l4 l5 l6, length l1 = length l4 -> length l2 = length l5 -> length l3 = length l6 -> prod2list (l1 ++ a :: l2 ++ a :: l3) (l4 ++ b :: l5 ++ c :: l6) = prod2list (l1 ++ a :: l2 ++ a :: l3) (l4 ++ c :: l5 ++ b :: l6).
Theorem prod2list_reorder : forall a b b1 l1 l2 l3 l4 l5, length l1 = length l3 -> length l2 = length l4 -> (forall b, In b l1 -> b <= a) -> (forall b, In b l2 -> b <= a) -> permutation (l3 ++ b :: l4) (b1 :: l5) -> ordered (sum_order f) (b1 :: l5) -> exists l6 : _, (exists l7 : _, length l1 = length l6 /\ length l2 = length l7 /\ permutation (b1 :: l5) (l6 ++ b1 :: l7) /\ prod2list (l1 ++ a :: l2) (l6 ++ b1 :: l7) <= prod2list (l1 ++ a :: l2) (l3 ++ b :: l4)).
Theorem prod2list_reorder2 : forall a b c b1 c1 l1 l2 l3 l4 l5, length l1 = length l3 -> length l2 = length l4 -> (forall b, In b l1 -> b <= a) -> (forall b, In b l2 -> b <= a) -> permutation (l3 ++ b :: c :: l4) (b1 :: c1 :: l5) -> ordered (sum_order f) (b1 :: c1 :: l5) -> exists l6 : _, (exists l7 : _, length l1 = length l6 /\ length l2 = length l7 /\ permutation (b1 :: c1 :: l5) (l6 ++ b1 :: c1 :: l7) /\ prod2list (l1 ++ a :: a :: l2) (l6 ++ b1 :: c1 :: l7) <= prod2list (l1 ++ a :: a :: l2) (l3 ++ b :: c :: l4)).