-
Notifications
You must be signed in to change notification settings - Fork 1
/
Orn.agda
264 lines (197 loc) · 8.93 KB
/
Orn.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
module Orn where
module plain where
data PlainDesc : Set₁ where
-- sort of like Poly/Container
arg : (A : Set) → (A → PlainDesc) → PlainDesc
rec : PlainDesc → PlainDesc
ret : PlainDesc
data Two : Set where
one two : Two
-- ( Two ▹ λ x → ...)
NatPlain : PlainDesc
NatPlain = arg Two λ {one → ret
; two → rec ret}
open import Data.Product
open import Data.Unit
open import Data.Bool
-- extension of a container
⦅_⦆ : PlainDesc → Set → Set
⦅ arg A D ⦆ X = Σ A λ a → ⦅ D a ⦆ X
⦅ rec D ⦆ X = X × ⦅ D ⦆ X
⦅ ret ⦆ X = ⊤
-- W type
data PlainData (D : PlainDesc) : Set where
⟨_⟩ : ⦅ D ⦆ (PlainData D) → PlainData D
_ : ⦅ NatPlain ⦆ Bool
_ = two , false , tt
ℕ : Set
ℕ = PlainData NatPlain
z : ℕ
z = ⟨ (one , tt) ⟩
s : ℕ → ℕ
s = λ n → ⟨ (two , n , tt) ⟩
module indexed where
{-
data PlainDesc : Set₁ where
-- sort of like Poly/Container
arg : (A : Set) → (A → PlainDesc) → PlainDesc
rec : PlainDesc → PlainDesc
ret : PlainDesc
-}
-- Indexed container like thing
data Desc ( I : Set ) : Set₁ where
arg : (A : Set) → (A → Desc I) → Desc I
-- Is A used for anything other than cardinality?
-- It determines the "branching factor" of a contstructor..
-- But does it ever pass data along?
rec : I → (Desc I) → Desc I
ret : I → Desc I
open import Data.Unit
open import Data.Bool
open import Data.Product
open import Agda.Builtin.Equality public
module depthTwo where
Ty : Desc ⊤
Ty = arg Bool λ{ false → arg Bool λ{ false → rec tt (ret tt)
; true → rec tt (ret tt)}
; true → arg Bool λ{ false → rec tt (ret tt)
; true → rec tt (ret tt)}}
-- can recreate ℕ by just using the trivial index ⊤
{-
NatPlain : PlainDesc
NatPlain = arg Bool λ {false → ret
; true → rec ret}
-}
NatDesc : Desc ⊤
NatDesc = arg Bool λ { false → rec tt (ret tt)
; true → ret tt}
⦅_⦆ : {I : Set} → Desc I → (I → Set) → I → Set
⦅ arg A D ⦆ R i = Σ A λ a → ⦅ D a ⦆ R i
⦅ rec h D ⦆ R i = R h × ⦅ D ⦆ R i -- the index only changes here
⦅ ret o ⦆ R i = o ≡ i
-- 𝒲 types
data Data {I : Set}(D : Desc I) : I → Set where
⟨_⟩ : ∀{ i : I} → ⦅ D ⦆ (Data D) i → Data D i
ℕ : Set
ℕ = Data NatDesc tt
z : ℕ
z = ⟨ true , refl ⟩
s : ℕ → ℕ
s n = ⟨ (false , n , refl) ⟩
-- pattenr zero = z ..?
VecDesc : Set → Desc ℕ
VecDesc X = arg Bool λ{false → ret z -- for arguments that aren't indexes, here X, don't case split
; true → arg X λ{_ → arg ℕ λ{⟨ false , n , refl ⟩ → rec n (ret (s n))
; ⟨ true , refl ⟩ → ret z}} }
Vec : Set → ℕ → Set
Vec X n = Data (VecDesc X) n
nil : {X : Set} → Vec X z
nil = ⟨ false , refl ⟩
cons : {X : Set}{n : ℕ} → X → Vec X n → Vec X (s n)
cons {n = n} x xs = ⟨ true , x , s n , xs , refl ⟩
_ : Vec Bool (s (s z))
_ = cons true (cons false nil)
module indexedAlgebras where
open indexed
open Desc
open import Data.Product using (_,_)
open import Data.Bool
open import Data.Unit
-- something something map of fibers?
_⊆_ : {I : Set} → (I → Set) → (I → Set) → Set
_⊆_ {I} X Y = ∀(i : I) → X i → Y i
-- x → y ⇒ F x → F y
map :{I : Set}{X Y : I → Set} → (D : Desc I) → X ⊆ Y → ⦅ D ⦆ X ⊆ ⦅ D ⦆ Y
map (arg A D) f i (a , d) = a , map (D a) f i d
map (rec h D) f i (x , d) = f h x , map D f i d
map (ret o) f i refl = refl
-- F X → X
Alg : {I : Set} → Desc I → (I → Set) → Set
Alg D X = ⦅ D ⦆ X ⊆ X
-- (alg : F X → X) → Fix F → X
{-# TERMINATING #-} -- how similar is this to proof algebras? ...
fold : {I : Set}{X : I → Set} → (D : Desc I) → Alg D X → Data D ⊆ X
fold D ϕ = λ{ i ⟨ x ⟩ → ϕ i (map D (fold D ϕ) i x)}
adda : ℕ → Alg NatDesc λ{ tt → ℕ }
adda y = λ{tt (false , x , refl) → s x
; tt (true , refl) → y}
_+_ : ℕ → ℕ → ℕ
x + y = fold NatDesc (adda y) tt x
_ : (s (s z)) + (s (s (s z))) ≡ (s (s (s (s (s z)))))
_ = refl
con : ∀{m : ℕ}{X : Set} → Vec X m → Alg (VecDesc X) (λ n → Vec X (n + m))
con ys ⟨ false , n , refl ⟩ (true , x , ⟨ fst , snd₁ ⟩ , snd) = cons x {! snd !}
con ys ⟨ true , refl ⟩ _ = ys
_++_ : ∀{n m : ℕ}{X : Set} → Vec X n → Vec X m → Vec X (n + m)
_++_ {X} xs ys = {! fold VecDesc !}
module Ornaments where
-- cool way to represent a fiber !
-- definitional equality rather than propositional equality
-- for f : J → I and i : I,
-- Σ[ j ∈ J ] (f j ≡ i)
data Inv {I J : Set} (e : J → I) : I → Set where
inv : (j : J) → Inv e (e j)
-- example
open import Data.Nat renaming (ℕ to 𝕟)
open import Data.Bool
open import Data.Unit
open indexed
open Desc renaming (arg to darg ; rec to drec ; ret to dret)
evenb : 𝕟 → Bool
evenb zero = true
evenb (suc zero) = false
evenb (suc (suc x)) = evenb x
_ : Inv evenb true
_ = inv 4 -- but inv 3 fails to typecheck!
open indexed using (Desc)
data Orn {I J : Set} (e : J → I) : Desc I → Set₁ where
arg : {A : Set}{D : A → Desc I} →
((a : A) → Orn e (D a)) → Orn e (arg A D)
rec : {h : I}{D : Desc I} →
Inv e h → Orn e D → Orn e (rec h D)
ret : {o : I} →
Inv e o → Orn e (ret o)
new : {D : Desc I} →
(A : Set) → ((a : A) → Orn e D) → Orn e D
! : {X : Set} → X → ⊤
! _ = tt
ListOrn : Set → Orn ! NatDesc
ListOrn X = arg λ{ false → new X λ{ _ → rec (inv tt) (ret (inv tt)) }
; true → ret (inv tt)}
orn : {I J : Set}{e : J → I}{D : Desc I} → Orn e D → Desc J
orn (arg {A = A} O) = darg A (λ a → orn (O a))
orn (rec (inv j) O) = drec j (orn O)
orn (ret (inv j)) = dret j
orn (new A O) = darg A (λ a → orn (O a))
data Foo : Set where
foo : Foo
_ : Desc ⊤
_ = orn (ListOrn Foo)
open indexedAlgebras
open import Function
open import Data.Product
open import Data.Bool
𝓧 : {I J : Set}{e : J → I}{D : Desc I}{O : Orn e D} → Alg (orn O) (Data D ∘ e)
𝓧 {O = O} j os = ⟨ erase O j os ⟩ where
erase : {I J : Set}{e : J → I}{D : Desc I} {R : I → Set} → (O : Orn e D) → (⦅ orn O ⦆ ( R ∘ e)) ⊆ ((⦅ D ⦆ R) ∘ e )
erase (arg O) j (a , os) = a , erase (O a) j os
erase (rec (inv h) O) j (r , os) = r , erase O j os
erase (ret (inv j)) j refl = refl
erase (new A O) j (a , os) = erase (O a) j os
∥_∥ : {I J : Set}{e : J → I}{D : Desc I}{O : Orn e D}{j : J} → Data (orn O) j → Data D (e j)
∥_∥ {O = O}{j = j} = (fold (orn O) 𝓧) j
ex₁ : Data (orn (ListOrn Foo)) tt
ex₁ = ⟨ (false , foo , (⟨ (true , refl) ⟩ , refl)) ⟩
t : Data NatDesc tt
t = ∥_∥ {O = ListOrn Foo} ex₁ -- length funtion on lists
t' : Data NatDesc tt
t' = s z
_ : t ≡ t'
_ = refl
-- vector to list
VecOrn : Set → Orn (λ j → tt) NatDesc
VecOrn = {! !}
VecDesc' : Set → Desc ℕ
VecDesc' X = arg Bool λ{false → ret z -- for arguments that aren't indexes, here X, don't case split
; true → arg X λ{_ → arg ℕ λ{⟨ false , n , refl ⟩ → rec n (ret (s n))
; ⟨ true , refl ⟩ → ret z}} }