4 Given a tree t, with values at the leaves, build a new tree with the
5 same structure and leaves labelled with distinct integers.
17 | Node (tree 'a) (tree 'a)
19 function labels (t : tree 'a) : list 'a = match t with
20 | Leaf x -> Cons x Nil
21 | Node l r -> labels l ++ labels r
25 forall x y : 'a. mem x (labels (Leaf y)) <-> x=y
28 forall x : 'a, l r : tree 'a.
29 mem x (labels (Node l r)) <-> (mem x (labels l) \/ mem x (labels r))
31 inductive same_shape (t1 : tree 'a) (t2 : tree 'b) =
33 forall x1 : 'a, x2 : 'b.
34 same_shape (Leaf x1) (Leaf x2)
36 forall l1 r1 : tree 'a, l2 r2 : tree 'b.
37 same_shape l1 l2 -> same_shape r1 r2 ->
38 same_shape (Node l1 r1) (Node l2 r2)
45 let fresh () ensures { !r = old !r + 1 /\ result = !r } =
48 let rec relabel (t : tree 'a) : tree int
50 ensures { same_shape t result /\ distinct (labels result) /\
52 (forall x:int. mem x (labels result) -> old !r < x <= !r) }
54 | Leaf _ -> Leaf (fresh ())
55 | Node l r -> Node (relabel l) (relabel r)