Definition recompose (tt : (canonTree*canonTree)): canonTree :=
let (t1, t2) := tt in
let (x1, c1) := t1 in
let (x2, c2) := t2 in
match x1 as s return (canonicalTree s -> canonTree) with
| Leaf b1 =>
fun c3 : canonicalTree (Leaf b1) =>
match x2 as s return (canonicalTree s -> canonTree) with
| Leaf b2 =>
fun c4 : canonicalTree (Leaf b2) =>
match bool_dec b1 b2 with
| left e =>
let H :=
eq_rec b1
(fun b3 : bool => canonicalTree (Leaf b3) -> canonTree)
(fun _ : canonicalTree (Leaf b1) =>
exist (fun t : ShareTree => canonicalTree t) (Leaf b1) c3)
b2 e in
H c4
| right n =>
exist (fun t : ShareTree => canonicalTree t)
(Node (Leaf b1) (Leaf b2)) (compose_canon1 b1 b2 n c3 c4)
end
| Node x21 x22 =>
fun c4 : canonicalTree (Node x21 x22) =>
exist (fun t : ShareTree => canonicalTree t)
(Node (Leaf b1) (Node x21 x22)) (compose_canon2 b1 x21 x22 c3 c4)
end c2
| Node x11 x12 =>
fun c3 : canonicalTree (Node x11 x12) =>
match x2 as s return (canonicalTree s -> canonTree) with
| Leaf b2 =>
fun c4 : canonicalTree (Leaf b2) =>
exist (fun t : ShareTree => canonicalTree t)
(Node (Node x11 x12) (Leaf b2)) (compose_canon3 x11 x12 b2 c3 c4)
| Node x21 x22 =>
fun c4 : canonicalTree (Node x21 x22) =>
exist (fun t : ShareTree => canonicalTree t)
(Node (Node x11 x12) (Node x21 x22))
(compose_canon4 x11 x12 x21 x22 c3 c4)
end c2
end c1.
(*End Definition*)

Lemma tree_avg_combine : forall n t11 t12 t21 t22 t31 t32,
avg n (exist (fun t => canonicalTree t) (mkCanon t11) (mkCanon_correct _))
(exist (fun t => canonicalTree t) (mkCanon t21) (mkCanon_correct _))
= Some (exist (fun t => canonicalTree t) (mkCanon t31) (mkCanon_correct _)) ->
avg n (exist (fun t => canonicalTree t) (mkCanon t12) (mkCanon_correct _))
(exist (fun t => canonicalTree t) (mkCanon t22) (mkCanon_correct _))
= Some (exist (fun t => canonicalTree t) (mkCanon t32) (mkCanon_correct _)) ->
avg (S n) (exist (fun t => canonicalTree t) (mkCanon (Node t11 t12)) (mkCanon_correct _))
(exist (fun t => canonicalTree t) (mkCanon (Node t21 t22)) (mkCanon_correct _))
= Some (exist (fun t => canonicalTree t) (mkCanon (Node t31 t32)) (mkCanon_correct _)).