【问题标题】:How to define my own (recursive) Coq notations?如何定义我自己的(递归)Coq 符号?
【发布时间】:2013-06-04 14:15:31
【问题描述】:

我现在已经开始大量使用Ensembles,因为它们更灵活。为了帮助我解决这个问题,我试图定义一些方便的符号。下面比较简单,例如:

Notation "a ∈ S" := (@In _ S a)  (at level 80).

我可以为其他二元集运算符添加类似的一堆。

但是我在使用这样的符号时遇到了很多麻烦:

Notation "∀ x ∈ S , P" := (forall x, (x ∈ S) -> P)  (at level 90).

它被接受,但每当我尝试使用它时,我都会收到此错误:

语法错误:在 [constr:operconstr level 200] 之后应为“∈”(在 [constr:operconstr] 中)。

问题 1:我做错了什么?

对于加分,你能告诉我如何为它定义一个递归符号吗?我已经尝试过了,但它似乎给了我一系列全新的问题。这是我的尝试,对库定义的直接编辑:

Notation "∀ x .. y ∈ S , P" :=
  (forall x, (x ∈ S) -> .. (forall y, (y ∈ S) -> P) ..)
  (at level 200, x binder, y binder, right associativity).

我不明白为什么 Coq.Unicode.Utf8_core 中的库版本应该解析而我的不应该,但是:

错误:找不到递归模式的开始位置。

问题 2:见问题 1。

【问题讨论】:

    标签: recursion operators coq


    【解决方案1】:

    上述递归符号不起作用的原因是活页夹(在本例中为 x 和 y)只能在右手的两个特定位置之一使用边[see manual]:

    • 在 fun [ ] => ... 术语中的活页夹位置,或
    • 在 forall [ ], ... 术语中的活页夹位置。

    因此,我不能再次将它们用作术语。这对我来说似乎有些武断,因为活页夹是在其绑定上下文中的术语。但是,您可以使用 fun 路由做任何您想做的事情:

    Definition all_in_E `(E: Ensemble T, P: T → Prop) : T → Prop :=
      (λ x: T, (x ∈ E) → (P x)).
    
    Notation "∀ x .. y ∈ S , P" :=
      ( all ( all_in_E S ( fun x => .. ( all ( all_in_E S ( fun y => P ))) .. )))
      (at level 200, x closed binder, y closed binder, right associativity).
    
    Definition ex_in_E `(E: Ensemble T, P: T → Prop) : T → Prop :=
      (λ x: T, (x ∈ E) ∧ (P x)).
    
    Notation "∃ x .. y ∈ S , P" :=
      ( ex ( ex_in_E S ( fun x => .. ( ex ( ex_in_E S ( fun y => P ))) .. )))
      (at level 200, x closed binder, y closed binder, right associativity).
    

    函数all_in_E 和ex_in_E 接受一个谓词(一个fun),并在给定集合E 中添加一个成员资格条件。这需要很长时间,但它确实有效。

    这是一个完整的带有示例的代码块:

    Require Export Coq.Unicode.Utf8.
    Require Export Coq.Sets.Ensembles.
    
    Generalizable All Variables.
    
    Notation "a ∈ S" := (@In _ S a)            (at level 70, no   associativity).
    Notation "A ∪ B" := (@Union _ A B)         (at level 50, left associativity).
    Notation "A ∩ B" := (@Intersection _ A B)  (at level 40, left associativity).
    
    Definition all_in_E `(E: Ensemble T, P: T → Prop) : T → Prop :=
      (λ x: T, (x ∈ E) → (P x)).
    
    Notation "∀ x .. y ∈ S , P" :=
      ( all ( all_in_E S ( fun x => .. ( all ( all_in_E S ( fun y => P ))) .. )))
      (at level 200, x closed binder, y closed binder, right associativity).
    
    Definition ex_in_E `(E: Ensemble T, P: T → Prop) : T → Prop :=
      (λ x: T, (x ∈ E) ∧ (P x)).
    
    Notation "∃ x .. y ∈ S , P" :=
      ( ex ( ex_in_E S ( fun x => .. ( ex ( ex_in_E S ( fun y => P ))) .. )))
      (at level 200, x closed binder, y closed binder, right associativity).
    
    Section TestingEnsembleQuantifiers.
      Definition A_nat := Full_set nat.
      Definition E_nat := Empty_set nat.
      Definition F_nat := Singleton _ 5.
    
      Require Import Coq.Arith.Gt.
    
      Example exists_in_intersection: ∃ x ∈ A_nat ∩ F_nat , x = 5.
        unfold ex_in_E.
        exists 5.
        split ; trivial.
        split.
        apply Full_intro.
        apply In_singleton.
      Qed.
    
      Example forall_in_union: ∀ x ∈ F_nat ∪ E_nat, x ≥ 5.
        unfold all_in_E, all.
        intros.
        destruct H ; destruct H.
        auto with arith.
      Qed.
    End TestingEnsembleQuantifiers.
    

    另请注意集合运算符的新优先级,与现有优先级 [see manual] 相比,它更有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      相关资源
      最近更新 更多