从软件基础第 1 卷的逻辑一章中,我们看到了列表反转的尾递归定义。它是这样的:
Fixpoint rev_append {X} (l1 l2 : list X) : list X :=
match l1 with
| [] => l2
| x :: l1' => rev_append l1' (x :: l2)
end.
Definition tr_rev {X} (l : list X) : list X :=
rev_append l [].
然后,我们被要求证明它们的等价性tr_rev
,rev
很明显它们是相同的。不过,我很难完成归纳。如果社区提供有关如何处理此案例的任何提示,将不胜感激。
据我所知,这是:
Theorem tr_rev_correct : forall X, @tr_rev X = @rev X.
Proof.
intros X. (* Introduce the type *)
apply functional_extensionality. (* Apply extensionality axiom *)
intros l. (* Introduce the list *)
induction l as [| x l']. (* start induction on the list *)
- reflexivity. (* base case for the empty list is trivial *)
- unfold tr_rev. (* inductive case seems simple too. We unfold the definition *)
simpl. (* simplify it *)
unfold tr_rev in IHl'. (* unfold definition in the Hypothesis *)
rewrite <- IHl'. (* rewrite based on the hypothesis *)
至此,我有了这一套假设和目标:
X : Type
x : X
l' : list X
IHl' : rev_append l' [ ] = rev l'
============================
rev_append l' [x] = rev_append l' [ ] ++ [x]
现在,[] ++ [x]
显然与它相同[x]
但simpl
无法简化它,我无法想出一个Lemma
可以帮助我的方法。我确实证明了app_nil_l
(即forall (X : Type) (x : X) (l : list X), [] ++ [x] = [x].
),但是当我尝试用它重写时,app_nil_l
它会重写等式的两边。
我可以将其定义为公理,但我觉得那是作弊:-p
谢谢