From Undecidability.L.Prelim Require Import ARS MoreBase.
Definition loopSum {X} {Y} :=
fix loopSum n (f:X -> X + Y) (x:X) {struct n}:=
match n with
0 => None
| S n => match f x with
| inl x' => loopSum n f x'
| inr y => Some y
end
end.
Arguments loopSum _ _ !_.
Definition loopSum_mono X Y f x y n n':
n <= n' -> @loopSum X Y n f x = Some y -> loopSum n' f x = Some y.

Lemma loopSum_sound_rel X Y {R:X->X->Prop} k n x y (f: X -> X + Y):
(forall x y, R x y -> f x = inl y) ->
pow R k x y ->
loopSum (k+n) f x = loopSum n f y.