summaryrefslogtreecommitdiff
path: root/src/Inky/Data/SnocList/Thinning.idr
blob: 60666d260100dae1cd6b72e778200d75d566ae04 (plain)
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
module Inky.Data.SnocList.Thinning

import Data.DPair
import Data.Nat
import Inky.Data.List
import Inky.Data.SnocList
import Inky.Data.SnocList.Var
import Inky.Data.SnocList.Quantifiers
import Inky.Decidable.Maybe

--------------------------------------------------------------------------------
-- Thinnings -------------------------------------------------------------------
--------------------------------------------------------------------------------

public export
data Thins : SnocList a -> SnocList a -> Type where
  Id : sx `Thins` sx
  Drop : sx `Thins` sy -> sx `Thins` sy :< x
  Keep : sx `Thins` sy -> sx :< x `Thins` sy :< x

%name Thins f, g, h

-- Basics

public export
indexPos : (f : sx `Thins` sy) -> (pos : Elem x sx) -> Elem x sy
indexPos Id pos = pos
indexPos (Drop f) pos = There (indexPos f pos)
indexPos (Keep f) Here = Here
indexPos (Keep f) (There pos) = There (indexPos f pos)

public export
index : (f : sx `Thins` sy) -> Var sx -> Var sy
index f i = toVar (indexPos f i.pos)

public export
(.) : sy `Thins` sz -> sx `Thins` sy -> sx `Thins` sz
Id . g = g
Drop f . g = Drop (f . g)
Keep f . Id = Keep f
Keep f . Drop g = Drop (f . g)
Keep f . Keep g = Keep (f . g)

-- Basic properties

export
indexPosInjective :
  (f : sx `Thins` sy) -> {i : Elem x sx} -> {j : Elem y sx} ->
  (0 _ : elemToNat (indexPos f i) = elemToNat (indexPos f j)) -> i = j
indexPosInjective Id prf = toNatInjective prf
indexPosInjective (Drop f) prf = indexPosInjective f (injective prf)
indexPosInjective (Keep f) {i = Here} {j = Here} prf = Refl
indexPosInjective (Keep f) {i = There i} {j = There j} prf =
  thereCong (indexPosInjective f $ injective prf)

export
indexPosComp :
  (f : sy `Thins` sz) -> (g : sx `Thins` sy) -> (pos : Elem x sx) ->
  indexPos (f . g) pos = indexPos f (indexPos g pos)
indexPosComp Id g pos = Refl
indexPosComp (Drop f) g pos = cong There (indexPosComp f g pos)
indexPosComp (Keep f) Id pos = Refl
indexPosComp (Keep f) (Drop g) pos = cong There (indexPosComp f g pos)
indexPosComp (Keep f) (Keep g) Here = Refl
indexPosComp (Keep f) (Keep g) (There pos) = cong There (indexPosComp f g pos)

-- Congruence ------------------------------------------------------------------

export
infix 6 ~~~

public export
data (~~~) : sx `Thins` sy -> sx `Thins` sz -> Type where
  IdId : Id ~~~ Id
  IdKeep : Id ~~~ f -> Id ~~~ Keep f
  KeepId : f ~~~ Id -> Keep f ~~~ Id
  DropDrop : f ~~~ g -> Drop f ~~~ Drop g
  KeepKeep : f ~~~ g -> Keep f ~~~ Keep g

%name Thinning.(~~~) prf

export
(.indexPos) : f ~~~ g -> (pos : Elem x sx) -> elemToNat (indexPos f pos) = elemToNat (indexPos g pos)
(IdId).indexPos pos = Refl
(IdKeep prf).indexPos Here = Refl
(IdKeep prf).indexPos (There pos) = cong S $ prf.indexPos pos
(KeepId prf).indexPos Here = Refl
(KeepId prf).indexPos (There pos) = cong S $ prf.indexPos pos
(DropDrop prf).indexPos pos = cong S $ prf.indexPos pos
(KeepKeep prf).indexPos Here = Refl
(KeepKeep prf).indexPos (There pos) = cong S $ prf.indexPos pos

export
(.index) :
  {0 f, g : sx `Thins` sy} -> f ~~~ g -> (i : Var sx) -> index f i = index g i
prf.index ((%%) n {pos}) = irrelevantEq $ toVarCong $ toNatInjective $ prf.indexPos pos

-- Useful for Parsers ----------------------------------------------------------

public export
(<><) : ctx1 `Thins` ctx2 -> LengthOf bound -> ctx1 <>< bound `Thins` ctx2 <>< bound
f <>< Z = f
f <>< S len = Keep f <>< len

public export
dropFish : ctx1 `Thins` ctx2 -> LengthOf bound -> ctx1 `Thins` ctx2 <>< bound
dropFish f Z = f
dropFish f (S len) = dropFish (Drop f) len

public export
dropPos : (pos : Elem x ctx) -> dropElem ctx pos `Thins` ctx
dropPos Here = Drop Id
dropPos (There pos) = Keep (dropPos pos)

public export
dropAll : LengthOf sy -> sx `Thins` sx ++ sy
dropAll Z = Id
dropAll (S len) = Drop (dropAll len)

public export
keepAll : LengthOf sz -> sx `Thins` sy -> sx ++ sz `Thins` sy ++ sz
keepAll Z f = f
keepAll (S len) f = Keep (keepAll len f)

public export
append :
  sx `Thins` sz -> sy `Thins` sw ->
  {auto len : LengthOf sw} ->
  sx ++ sy `Thins` sz ++ sw
append f Id = keepAll len f
append f (Drop g) {len = S len} = Drop (append f g)
append f (Keep g) {len = S len} = Keep (append f g)

public export
assoc : LengthOf sz -> sx ++ (sy ++ sz) `Thins` (sx ++ sy) ++ sz
assoc Z = Id
assoc (S len) = Keep (assoc len)

public export
growLengthOf : sx `Thins` sy -> LengthOf sx -> LengthOf sy
growLengthOf Id len = len
growLengthOf (Drop f) len = S (growLengthOf f len)
growLengthOf (Keep f) (S len) = S (growLengthOf f len)

public export
thinLengthOf : sx `Thins` sy -> LengthOf sy -> LengthOf sx
thinLengthOf Id len = len
thinLengthOf (Drop f) (S len) = thinLengthOf f len
thinLengthOf (Keep f) (S len) = S (thinLengthOf f len)

public export
thinAll : sx `Thins` sy -> All p sy -> All p sx
thinAll Id env = env
thinAll (Drop f) (env :< px) = thinAll f env
thinAll (Keep f) (env :< px) = thinAll f env :< px

public export
splitL :
  {0 sx, sy, sz : SnocList a} ->
  LengthOf sz ->
  sx `Thins` sy ++ sz ->
  Exists (\sxA => Exists (\sxB => (sx = sxA ++ sxB, sxA `Thins` sy, sxB `Thins` sz)))
splitL Z f = Evidence sx (Evidence [<] (Refl, f, Id))
splitL (S len) Id = Evidence sy (Evidence sz (Refl, Id, Id))
splitL (S len) (Drop f) =
  let Evidence sxA (Evidence sxB (Refl, g, h)) = splitL len f in
  Evidence sxA (Evidence sxB (Refl, g, Drop h))
splitL (S len) (Keep f) =
  let Evidence sxA (Evidence sxB (Refl, g, h)) = splitL len f in
  Evidence sxA (Evidence (sxB :< _) (Refl, g, Keep h))

public export
splitR :
  {0 sx, sy, sz : SnocList a} ->
  LengthOf sy ->
  sx ++ sy `Thins` sz ->
  Exists (\sxA => Exists (\sxB => (sz = sxA ++ sxB, sx `Thins` sxA, sy `Thins` sxB)))
splitR Z f = Evidence sz (Evidence [<] (Refl, f, Id))
splitR (S len) Id = Evidence sx (Evidence sy (Refl, Id, Id))
splitR (S len) (Drop f) =
  let Evidence sxA (Evidence sxB (Refl, g, h)) = splitR (S len) f in
  Evidence sxA (Evidence (sxB :< _) (Refl, g, Drop h))
splitR (S len) (Keep f) =
  let Evidence sxA (Evidence sxB (Refl, g, h)) = splitR len f in
  Evidence sxA (Evidence (sxB :< _) (Refl, g, Keep h))

-- Strengthening ---------------------------------------------------------------

public export
data Skips : sx `Thins` sy -> Elem y sy -> Type where
  Here : Drop f `Skips` Here
  Also : f `Skips` i -> Drop f `Skips` There i
  There : f `Skips` i -> Keep f `Skips` There i

%name Skips skips

public export
strengthen :
  (f : sx `Thins` sy) -> (i : Elem y sy) ->
  Proof (Elem y sx) (\j => i = indexPos f j) (f `Skips` i)
strengthen Id i = Just i `Because` Refl
strengthen (Drop f) Here = Nothing `Because` Here
strengthen (Drop f) (There i) =
  map id (\_, prf => cong There prf) Also $
  strengthen f i
strengthen (Keep f) Here = Just Here `Because` Refl
strengthen (Keep f) (There i) =
  map There (\_, prf => cong There prf) There $
  strengthen f i

export
skipsDropPos : (i : Elem x sx) -> dropPos i `Skips` j -> i ~=~ j
skipsDropPos Here Here = Refl
skipsDropPos (There i) (There skips) = thereCong (skipsDropPos i skips)

------------------------ -------------------------------------------------------
-- Renaming Coalgebras ---------------------------------------------------------
--------------------------------------------------------------------------------

public export
interface Rename (0 a : Type) (0 t : SnocList a -> Type) where
  rename :
    {0 sx, sy : SnocList a} -> sx `Thins` sy ->
    t sx -> t sy
  0 renameCong :
    {0 sx, sy : SnocList a} -> {0 f, g : sx `Thins` sy} -> f ~~~ g ->
    (x : t sx) -> rename f x = rename g x
  0 renameId : {0 sx : SnocList a} -> (x : t sx) -> rename Id x = x