[英]agda: How do I tell the type system that two types are equal?
假設我的類型依賴於 Nat
data MyType : (i : ℕ) → Set where
cons : (i : ℕ) → MyType i
和一個功能:
combine : {i₁ i₂ : ℕ} → MyType i₁ → MyType i₂ → MyType (i₁ ⊔ i₂)
現在我正在嘗試編寫一個函數:
create-combined : (i : ℕ) → MyType i
create-combined i = combine {i} {i} (cons i) (cons i)
不幸的是,我收到錯誤消息:
i ⊔ i != i of type ℕ
when checking that the inferred type of an application
MyType (i ⊔ i)
matches the expected type
MyType i
我知道我可以證明i ⊔ i ≡ i
,但我不知道如何將該證明提供給類型系統以解決此問題。
我如何讓它編譯?
您可以使用Relation.Binary.PropositionalEquality
中的subst
。
你給它:
i ⊔ i ≡ i
的證明MyType
combine {i} {i} (cons i) (cons i)
或者,您也可以在證明中使用rewrite
關鍵字,這通常是首選。
這是應用於向量連接的兩種可能性的(人工)示例:
module ConsVec where
open import Data.Vec
open import Data.Nat
open import Data.Nat.Properties
open import Relation.Binary.PropositionalEquality
_++₁_ : ∀ {a} {A : Set a} {m n} → Vec A m → Vec A n → Vec A (n + m)
_++₁_ {m = m} {n} v₁ v₂ = subst (Vec _) (+-comm m n) (v₁ ++ v₂)
_++₂_ : ∀ {a} {A : Set a} {m n} → Vec A m → Vec A n → Vec A (n + m)
_++₂_ {m = m} {n} v₁ v₂ rewrite sym (+-comm m n) = v₁ ++ v₂
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.