[英]Parametrized Inductive Types in Agda
我只是在閱讀工作中的依賴類型 。 在參數化類型的介紹中,作者在本聲明中提到了這一點
data List (A : Set) : Set where
[] : List A
_::_ : A → List A → List A
List
的類型是Set → Set
, A
成為兩個構造函數的隱式參數,即。
[] : {A : Set} → List A
_::_ : {A : Set} → A → List A → List A
好吧,我試着改寫它有點不同
data List : Set → Set where
[] : {A : Set} → List A
_::_ : {A : Set} → A → List A → List A
這可悲的是(我收集這是因為在構造函數parametrised在我努力學習阿格達兩天左右的時間,但不工作Set₀
等List A
必須在Set₁
)。
實際上,接受以下內容
data List : Set₀ → Set₁ where
[] : {A : Set₀} → List A
_::_ : {A : Set₀} → A → List A → List A
但是,我不再能夠使用{A : Set} → ... → List (List A)
(這是完全可以理解的)。
所以我的問題: List (A : Set) : Set
和List : Set → Set
?之間的實際區別是什么?
謝謝你的時間!
我冒昧地重命名數據類型。 第一個(在Set
上編入索引)將被稱為ListI
,第二個ListP
Set
為參數:
data ListI : Set → Set₁ where
[] : {A : Set} → ListI A
_∷_ : {A : Set} → A → ListI A → ListI A
data ListP (A : Set) : Set where
[] : ListP A
_∷_ : A → ListP A → ListP A
在數據類型中,參數在冒號之前,冒號之后的參數稱為指示。 構造函數可以以相同的方式使用,您可以應用隱式集:
nilI : {A : Set} → ListI A
nilI {A} = [] {A}
nilP : {A : Set} → ListP A
nilP {A} = [] {A}
模式匹配時會出現差異。 對於索引版本,我們有:
null : {A : Set} → ListI A → Bool
null ([] {A}) = true
null (_∷_ {A} _ _) = false
ListP
無法做到這ListP
:
-- does not work
null′ : {A : Set} → ListP A → Bool
null′ ([] {A}) = true
null′ (_∷_ {A} _ _) = false
錯誤消息是
The constructor [] expects 0 arguments, but has been given 1
when checking that the pattern [] {A} has type ListP A
ListP
也可以使用虛擬模塊定義,如ListD
:
module Dummy (A : Set) where
data ListD : Set where
[] : ListD
_∷_ : A → ListD → ListD
open Dummy public
也許有點令人驚訝, ListD
等於ListP
。 我們不能在Dummy
的參數上進行模式匹配:
-- does not work
null″ : {A : Set} → ListD A → Bool
null″ ([] {A}) = true
null″ (_∷_ {A} _ _) = false
這給出了與ListP
相同的錯誤消息。
ListP
是參數化數據類型的一個例子,它比ListI
更簡單,它是一個歸納族:它“取決於”指標,盡管在這個例子中是一個微不足道的方式。
歸納家族並沒有真正定義,但在維基中詳細闡述了似乎需要歸納家庭的典型例子:
data Term (Γ : Ctx) : Type → Set where
var : Var Γ τ → Term Γ τ
app : Term Γ (σ → τ) → Term Γ σ → Term Γ τ
lam : Term (Γ , σ) τ → Term Γ (σ → τ)
忽略Type索引,由於lam
構造函數,無法使用Dummy
-module方式編寫此類型的簡化版本。
另一個很好的參考是Peter Dybjer從1997年開始的歸納家族 。
快樂的阿格達編碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.