Section eval.
Variables (T : Type) (idx : T) (op : T -> T -> T).
Inductive env := Empty | ENode of T & env & env.
Definition pos := fix loop (e : env) p {struct e} :=
match e, p with
| ENode t _ _, 1%positive => t
| ENode t e _, (p~0)%positive => loop e p
| ENode t _ e, (p~1)%positive => loop e p
| _, _ => idx
end.
Definition set_pos (f : T -> T) := fix loop e p {struct p} :=
match e, p with
| ENode t e e', 1%positive => ENode (f t) e e'
| ENode t e e', (p~0)%positive => ENode t (loop e p) e'
| ENode t e e', (p~1)%positive => ENode t e (loop e' p)
| Empty, 1%positive => ENode (f idx) Empty Empty
| Empty, (p~0)%positive => ENode idx (loop Empty p) Empty
| Empty, (p~1)%positive => ENode idx Empty (loop Empty p)
end.

Lemma pos_set_pos (f : T -> T) e (p p' : positive) :
pos (set_pos f e p) p' = if p == p' then f (pos e p) else pos e p'.