6
我正在與Coq鬼混。具體來說,我試圖實施mergesort,然後證明它的工作原理。Coq中定點的侷限性?
我在執行的嘗試是:
Fixpoint sort ls :=
match ls with
| nil => nil
| cons x nil => cons x nil
| xs =>
let (left, right) := split xs nil nil
in merge (sort left) (sort right)
end.
,我得到的這個結果的錯誤是:
Error:
Recursive definition of sort is ill-formed.
In environment
sort : list nat -> list nat
ls : list nat
x : nat
l : list nat
y : nat
l0 : list nat
left : list nat
right : list nat
Recursive call to sort has principal argument equal to "left" instead of
one of the following variables: "l" "l0".
Recursive definition is:
"fun ls : list nat =>
match ls with
| nil => nil
| x :: nil => x :: nil
| x :: _ :: _ =>
let (left, right) := split ls nil nil in merge (sort left) (sort right)
end".
我的這些錯誤的解釋是,L和10是沒有LS它的頭,x和ls沒有x和x之後的元素(我想它決定調用y?)。我不會在這些列表中遞歸,而是在本地定義的列表上遞歸。
我只允許在模式匹配中直接發送的東西遞歸嗎?如果是的話,這似乎是一個嚴重的限制。有沒有辦法解決它?我猜測Coq不能說這個函數會終止。有什麼方法可以證明左右比xs小嗎?