簡體   English   中英

如何定義我自己的(遞歸)Coq符號?

[英]How to define my own (recursive) Coq notations?

我現在開始與Ensemble合作很多,因為它們更靈活。 為了幫助我,我試圖定義一些方便的符號。 以下相對容易,例如:

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。

上面的遞歸表示法不起作用的原因是綁定器(在這種情況下是xy只能在右側的兩個特定位置之一中使用[ 見手冊 ]:

  • fun [ ] => ... term,或者
  • 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_Eex_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.

請注意集合運算符的新優先級,它們與現有優先級相關更有意義[ 參見手冊 ]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM