![](/img/trans.png)
[英]Differences in “prose” and “dot notation” when invoking a method with (explicit) implicit parameters
[英]Using Implicit Type Class Parameters in Coq Notation
我正試圖把我的腦袋包裹在Coq中的類型類中(我過去曾經涉足它,但我與經驗豐富的用戶相去甚遠)。 作為練習,我正在嘗試編寫一個小組理論庫。 這就是我想出來的:
Class Group {S : Type} {op : S → S → S} := {
id : S;
inverse : S → S;
id_left {x} : (op id x) = x;
id_right {x} : (op x id) = x;
assoc {x y z} : (op (op x y) z) = (op x (op y z));
right_inv {x} : (op x (inverse x)) = id;
}.
我特別喜歡隱含的S
和op
參數(假設我正確理解它們)。
為反轉做一些表示法很容易:
Notation "- x" := (@inverse _ _ _ x)
(at level 35, right associativity) : group_scope.
現在,我想讓x * y
成為(op xy)
的簡寫。 使用部分時,這很簡單:
Section Group.
Context {S} {op} { G : @Group S op }.
(* Reserved at top of file *)
Notation "x * y" := (op x y) : group_scope.
(* ... *)
End Group.
但是,由於這是在一個部分中聲明的,因此其他地方無法訪問該表示法。 如果可能的話,我想在全球范圍內聲明符號。 我遇到的問題(與inverse
)是因為op
是Group
的隱式參數,它實際上並不存在於全局范圍內的任何地方(所以我不能通過它來引用它(@op _ _ _ xy)
)。 這個問題告訴我,我要么使用類型錯誤,要么不理解如何將符號與隱式變量集成。 有人能指出我正確的方向嗎?
基於Anton Trunov的回復 ,我能夠編寫以下內容,其工作原理如下:
Reserved Notation "x * y" (at level 40, left associativity).
Class alg_group_binop (S : Type) := alg_group_op : S → S → S.
Delimit Scope group_scope with group.
Infix "*" := alg_group_op: group_scope.
Open Scope group_scope.
Class Group {S : Type} {op : alg_group_binop S} : Type := {
id : S;
inverse : S → S;
id_left {x} : id * x = x;
id_right {x} : x * id = x;
assoc {x y z} : (x * y) * z = x * (y * z);
right_inv {x} : x * (inverse x) = id;
}.
以下是PierreCastéran和Matthieu Sozeau如何在Coq (第3.9.2節)中的類型類和關系的溫和介紹中解決這個問題:
來自同上的解決方案。 包括聲明用於表示二元運算符的單例類型類:
Class monoid_binop (A:Type) := monoid_op : A -> A -> A.
注意 :與多字段類類型不同,
monoid_op
不是構造函數,而是透明常量,使得monoid_op f
可以被δβ減少為f
。現在可以聲明中綴表示法:
Delimit Scope M_scope with M. Infix "*" := monoid_op: M_scope. Open Scope M_scope.
我們現在可以給出
Monoid
的新定義,使用類型monoid_binop A
而不是A → A → A
,以及中綴符號x * y
而不是monoid_op xy
:Class Monoid (A:Type) (dot : monoid_binop A) (one : A) : Type := { dot_assoc : forall xyz:A, x*(y*z) = x*y*z; one_left : forall x, one * x = x; one_right : forall x, x * one = x }.
皮埃爾·卡斯特蘭(PierreCastéran)和馬蒂烏·索佐(Matthiu Sozeau)就是這樣處理的原因。
但不會
Definition group_op {S op} {G : @Group S op} := op.
Infix "*" := group_op.
還在這里工作? (我只嘗試了兩個非常基本的測試用例。)
這將使您無需更改Group
定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.