coqterminationtotality

Decreasing argument (and what is a Program Fixpoint)


Consider the following fixpoint:

Require Import Coq.Lists.List.
Import ListNotations.

Inductive my_type: Type:=
| Left: my_type
| Right: my_type
.

Fixpoint decrease (which: my_type) (left right: list my_type) : list my_type :=
match which with
| Left =>
  match left with
  | [] => []
  | a::tl => decrease a tl right
  end
| Right =>
  match right with
  | [] => []
  | a::tl => decrease a left tl
  end
end.

Coq rejects the following fixpoint because it can not guess the decreasing fixpoint (sometimes the left list looses its head, sometimes it is the right one).

This answer shows that one can solve this by using a Program Fixpoint and specifying a {measure ((length left)+(length right))}.

My questions are:


Solution