卡在关于正则表达式的简单证明上



我正在尝试使用 Coq 形式化正则表达式 (RE) 上的一些属性。但是,我有一些麻烦来证明一个相当简单的属性:

对于所有字符串 s,如果 s 采用 (epsilon)* RE 的语言,则 s = ",其中 epsilon 和 * 表示空字符串 RE 和 Kleene 星形 操作。

这似乎是归纳/反转策略的明显应用,但我无法使其起作用。

带有问题引理的最小工作代码在以下要点中。 关于我应该如何进行的任何提示将不胜感激。

编辑

我的一次尝试是这样的:

Lemma star_lemma : forall s, s <<- (#1 ^*) -> s = "".
Proof.  
intros s H.
inverts* H.
inverts* H2.
inverts* H1.
inverts* H1.
inverts* H2.
simpl in *.
-- stuck here

这给我留下了以下目标:

s' : string
H4 : s' <<- (#1 ^*)
============================
s' = ""

至少对我来说,使用归纳法似乎可以完成证明,因为我可以在归纳假设中使用 H4 来完成证明,但是当我开始使用

induction H

而不是

inverts* H

我有一些(至少对我来说)毫无意义的目标。在 Idris/Agda 中,这种证明只是通过模式匹配和递归来执行 s <<- (#1 ^*)。我的观点是如何在 Coq 中进行这样的递归。

这是原始问题的一种可能解决方案:

Lemma star_lemma : forall s,
s <<- (#1 ^*) -> s = "".
Proof.
refine (fix star_lemma s prf {struct prf} : s = "" := _).
inversion_clear prf; subst.
inversion_clear H; subst.
- now inversion H0.
- inversion_clear H0; subst. inversion_clear H; subst.
rewrite (star_lemma s' H1).
reflexivity.
Qed.

主要思想是在上下文中引入一个类似于典型 Idris 证明中的递归调用的术语。rememberdependent induction的方法不能很好地工作(没有修改in_regex),因为它们引入了不可能满足方程作为归纳假设的前提。

注意:检查此引理可能需要一段时间(在我的机器上,Coq 8.5pl3 大约需要 8.5 秒)。我认为这是因为inversion策略往往会产生大的证明条款。

这个问题已经困扰了我一个星期,我终于找到了一个我觉得优雅的解决方案。

我已经读到,当一个归纳原理不适合你的需要时,你可以写并证明另一个更适合你的问题的原理。这就是我在这种情况下所做的。我们想要的是使用本答案中给出的更自然的定义时获得的定义。通过这样做,我们可以保持相同的定义(例如,如果更改它意味着太多更改)并更轻松地对其进行推理。

这是归纳原理的证明(我使用一节来精确地指定隐含的论点,因为否则我会观察到它们奇怪的行为,但这里根本不需要节机制)。

Section induction_principle.
Context (P : string -> regex -> Prop)
(H_InEps : P "" #1)
(H_InChr : forall c, P (String c "") ($ c))
(H_InCat : forall {e e' s s' s1}, s <<- e -> P s e -> s' <<- e' ->
P s' e' -> s1 = s ++ s' -> P s1 (e @ e'))
(H_InLeft : forall {s e e'}, s <<- e -> P s e -> P s (e :+: e'))
(H_InRight : forall {s' e e'}, s' <<- e' -> P s' e' -> P s' (e :+: e'))
(H_InStar_Eps : forall e, P "" (e ^*))
(H_InStar_Cat : forall {s1 s2 e}, s1 <<- e -> s2 <<- (e ^*) ->
P s1 e -> P s2 (e ^*) -> P (s1++s2) (e ^*)).
Arguments H_InCat {_ _ _ _ _} _ _ _ _ _.
Arguments H_InLeft {_ _ _} _ _.
Arguments H_InRight {_ _ _} _ _.
Arguments H_InStar_Cat {_ _ _} _ _ _ _.
Definition in_regex_ind2 : forall (s : string) (r : regex), s <<- r -> P s r.
Proof.
refine (fix in_regex_ind2 {s r} prf {struct prf} : P s r :=
match prf with
| InEps => H_InEps
| InChr c => H_InChr c
| InCat prf1 prf2 eq1 =>
H_InCat prf1 (in_regex_ind2 prf1) prf2 (in_regex_ind2 prf2) eq1
| InLeft _ prf => H_InLeft prf (in_regex_ind2 prf)
| InRight _ prf => H_InRight prf (in_regex_ind2 prf)
| InStar prf => _
end).
inversion prf; subst.
- inversion H1. apply H_InStar_Eps.
- inversion H1; subst.
apply H_InStar_Cat; try assumption; apply in_regex_ind2; assumption.
Qed.
End induction_principle.

事实证明,这个证明的Qed不是即时的(可能是由于inversion产生了像这个答案中的大项),而是用了不到 1 秒的时间(也许是因为引理更抽象)。

star_lemma变得几乎微不足道(一旦我们知道remember技巧),就像自然定义一样。

Lemma star_lemma : forall s, s <<- (#1 ^*) -> s = "".
Proof.
intros s H. remember (#1 ^*) as r.
induction H using in_regex_ind2; try discriminate.
- reflexivity.
- inversion Heqr; subst.
inversion H. rewrite IHin_regex2 by reflexivity. reflexivity.
Qed.

我稍微修改了你的in_regex谓词的定义:

Inductive in_regex : string -> regex -> Prop :=
| InEps
: "" <<- #1
| InChr
: forall c
, (String c EmptyString) <<- ($ c)
| InCat
:  forall e e' s s' s1
,  s <<- e
-> s' <<- e'
-> s1 = s ++ s'
-> s1 <<- (e @ e')
| InLeft
:  forall s e e'
,  s <<- e
-> s <<- (e :+: e')
| InRight
:  forall s' e e'
,  s' <<- e'
-> s' <<- (e :+: e')
| InStarLeft
: forall e
, "" <<- (e ^*)
| InStarRight
:  forall s s' e
,  s <<- e
-> s' <<- (e ^*)
-> (s ++ s') <<- (e ^*)
where "s '<<-' e" := (in_regex s e).

并且可以证明您的引理:

Lemma star_lemma : forall s, s <<- (#1 ^*) -> s = "".
Proof.
intros s H.
remember (#1 ^*) as r.
induction H; inversion Heqr; clear Heqr; trivial.
subst e.
rewrite IHin_regex2; trivial.
inversion H; trivial.
Qed.

一些解释是必要的。

  1. 我对H进行了归纳.理由是:如果我有s <<- (#1 ^*)证明,那么这个证明必须具有以下形式......

  2. 该策略remember创建一个新的假设Heqr,结合inversion将有助于摆脱不可能给出此证明的情况(事实上,所有案例都减去结论中^*的情况)。

  3. 不幸的是,这种推理路径不适用于您对in_regex谓词的定义,因为它会给归纳假设创建一个不满足的条件。这就是为什么我也修改了你的归纳谓词。

  4. 改进的归纳试图给出一个更基本的定义,即在(e ^*)中。从语义上讲,我认为这是等效的。

我有兴趣阅读有关原始问题的证明。

最新更新