我試圖從內存中重新實現一個來自CPDT的示例。我寫道:與類型族匹配時的Coq類型錯誤
Inductive myType : Set := MyNat | MyBool.
Definition typeDenote (t : myType) : Set :=
match t with
| MyNat => nat
| MyBool => bool
end.
Inductive unaryOp : myType -> myType -> Set :=
| Twice : unaryOp MyNat MyNat.
Definition twice (n:nat) : nat := n + n.
Definition tunaryDenote (a b : myType) (t : unaryOp a b)
: typeDenote a -> typeDenote b :=
match t with
| Twice => twice
end.
產生的錯誤是:
Toplevel input, characters 125-130
> | Twice => twice
> ^^^^^
Error: In environment
a : myType
b : myType
t : unaryOp a b
The term "twice" has type "nat -> nat" while it is expected to have type
"forall H : typeDenote ?141, typeDenote ?142"
我不明白此錯誤消息。我認爲一旦Twice : unaryOp MyNat MyNat
上的匹配成功,Coq推斷a
和b
是MyNat
s,因此typeDenote a -> typeDenote b ≡ nat -> nat
,使得twice
成爲返回值的完美候選。我哪裏錯了?
最後你的分析看起來非常好,所以我已經嘗試了一段代碼,但Coq完全沒有抱怨(使用Coq v8.4pl6和v8.5pl2進行檢查)。 –