簡體   English   中英

Coq:定義類型類實例

[英]Coq: Defining a type class instance

在下面的開發中,我在嘗試定義單方法類型類的實例時遇到一個奇怪的錯誤:

Universe ARG. Definition ARG := Type@{ARG}.
Universe ARG0. Definition ARG0 := Type@{ARG0}.
Universe ARG1. Definition ARG1 := Type@{ARG1}.
Universe ARG2. Definition ARG2 := Type@{ARG2}.
Constraint ARG<ARG0, ARG0<ARG1, ARG1<ARG2.

Inductive SR: ARG := Phy | Sen | Inf | Lim.
Parameter CA: Prop.
Parameter X: SR -> CA -> ARG -> ARG.
Parameter X': SR -> CA -> ARG -> ARG0.
Parameter XP: SR -> CA -> ARG -> ARG1.
Parameter XP': SR -> CA -> ARG -> ARG2.
Inductive tri:Set := one | two | three.

Definition iX' (t:tri): SR -> CA -> ARG -> ARG2 := match t with one => X' | two => XP | three => XP' end.
Parameter gk:> forall (b:SR)(d:CA)(c:ARG), X' b d c -> iX' one b d c.
Parameter gl:> forall (b:SR)(d:CA)(c:ARG), XP b d c -> iX' two b d c.
Parameter gm:> forall (b:SR)(d:CA)(c:ARG), XP' b d c -> iX' three b d c.

Definition iX'bsko {b:tri}{s:SR}{k:CA}{o:ARG} := iX' b s k o.
Parameter foo: forall {b:tri}{s:SR}{k:CA}{o:ARG}, iX' b s k o.
Fail Check foo: forall {b:tri}{s:SR}{k:CA}{o:ARG}, iX' b s k o. (*Why?*)
Check foo: iX'bsko.
Class CONN := p5 (x y z:ARG): x -> y -> z.
Instance cco: CONN := fun x y iX'bsko (_:x) (_:y) => foo.
(* Error: "foo" has type "iX' ?b@{y0:=x0; y1:=y0} ?s@{y0:=x0; y1:=y0} ?k@{y0:=x0; y1:=y0} ?o@{y0:=x0; y1:=y0}"
while it is expected to have type "iX'bsko". *)

錯誤的原因似乎是foo沒有類型iX'bsko ,而foo: iX'bsko類型檢查上方的 2 行。 我該如何解決這個問題?

要回答您的評論(*Why?*) ,問題是foo意味着@foo _ _ _ _ 以下成功:

Check @foo: forall {b:tri}{s:SR}{k:CA}{o:ARG}, iX' b s k o.

為了回答您的問題,您通過使用局部不透明變量對全局iX'bsko進行遮蔽,從而使自己陷入iX'bsko

如果你改變

Instance cco: CONN := fun x y iX'bsko (_:x) (_:y) => foo.

Instance cco: CONN := fun x y not_really_iX'bsko' (_:x) (_:y) => foo.

你得到

Error:
In environment
x : ARG
y : ARG
not_really_iX'bsko : ARG
x0 : x
y0 : y
The term "foo" has type
 "iX' ?b@{y0:=x0; y1:=y0} ?s@{y0:=x0; y1:=y0} ?k@{y0:=x0; y1:=y0}
    ?o@{y0:=x0; y1:=y0}" while it is expected to have type
 "not_really_iX'bsko".

這並不奇怪。 CONN是所有forall xyz : Type@{ARG}, x -> y -> z的類型forall xyz : Type@{ARG}, x -> y -> z 這種類型沒有居民:

Lemma no_conn : CONN -> False.
Proof. exact (fun cco => cco True True False I I). Qed.

也許您打算將xyz參數改為CONN ,編寫這樣的內容?

Class CONN (x y z:ARG) := p5 : x -> y -> z.
Instance cco x y : CONN x y iX'bsko := fun (_:x) (_:y) => foo.

請注意,這會失敗並顯示更明確的錯誤消息:

The term "iX'bsko" has type "ARG2" while it is expected to have type
"ARG" (universe inconsistency).

如果你這樣做

Class CONN (x y z:ARG2) := p5 : x -> y -> z.
Instance cco x y : CONN x y iX'bsko := fun (_:x) (_:y) => foo.

然后你得到

Error: Cannot infer the implicit parameter b of iX'bsko whose type is
"tri" in environment:
x, y : ARG2

暫無
暫無

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

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