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
|
module Total.LogRel
import Syntax.PreorderReasoning
import Total.Reduction
import Total.Term
%prefix_record_projections off
public export
LogRel :
{ctx : SnocList Ty} ->
(P : {ctx, ty : _} -> Term ctx ty -> Type) ->
{ty : Ty} ->
(t : Term ctx ty) ->
Type
LogRel p {ty = N} t = p t
LogRel p {ty = ty ~> ty'} t =
(p t,
{ctx' : SnocList Ty} ->
(thin : ctx `Thins` ctx') ->
(u : Term ctx' ty) ->
LogRel p u ->
LogRel p (App (wkn t thin) u))
export
escape :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
{ty : Ty} ->
{0 t : Term ctx ty} ->
LogRel P t ->
P t
escape {ty = N} pt = pt
escape {ty = ty ~> ty'} (pt, app) = pt
public export
record PreserveHelper
(P : {ctx, ty : _} -> Term ctx ty -> Type)
(R : {ctx, ty : _} -> Term ctx ty -> Term ctx ty -> Type) where
constructor MkPresHelper
app :
{0 ctx : SnocList Ty} ->
{ty, ty' : Ty} ->
{0 t, u : Term ctx (ty ~> ty')} ->
{ctx' : SnocList Ty} ->
(thin : ctx `Thins` ctx') ->
(v : Term ctx' ty) ->
R t u ->
R (App (wkn t thin) v) (App (wkn u thin) v)
pres :
{0 ctx : SnocList Ty} ->
{ty : Ty} ->
{0 t, u : Term ctx ty} ->
P t ->
(0 _ : R t u) ->
P u
%name PreserveHelper help
export
backStepHelper :
{0 P : {ctx, ty : _} -> Term ctx ty -> Type} ->
(forall ctx, ty. {0 t, u : Term ctx ty} -> P t -> (0 _ : u > t) -> P u) ->
PreserveHelper P (flip (>))
backStepHelper pres =
MkPresHelper
(\thin, v, step => AppCong1 (wknStep step))
pres
export
preserve :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
{R : {ctx, ty : _} -> Term ctx ty -> Term ctx ty -> Type} ->
{ty : Ty} ->
{0 t, u : Term ctx ty} ->
PreserveHelper P R ->
LogRel P t ->
(0 _ : R t u) ->
LogRel P u
preserve help {ty = N} pt prf = help.pres pt prf
preserve help {ty = ty ~> ty'} (pt, app) prf =
(help.pres pt prf, \thin, v, rel => preserve help (app thin v rel) (help.app thin v prf))
data AllLogRel : (P : {ctx, ty : _} -> Term ctx ty -> Type) -> Terms ctx' ctx -> Type where
Base :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
{0 thin : ctx `Thins` ctx'} ->
AllLogRel P (Base thin)
(:<) :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
AllLogRel P sub ->
LogRel P t ->
AllLogRel P (sub :< t)
%name AllLogRel allRels
index :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
((i : Elem ty ctx') -> LogRel P (Var i)) ->
{sub : Terms ctx' ctx} ->
AllLogRel P sub ->
(i : Elem ty ctx) ->
LogRel P (index sub i)
index f Base i = f (index _ i)
index f (allRels :< rel) Here = rel
index f (allRels :< rel) (There i) = index f allRels i
Valid :
(P : {ctx, ty : _} -> Term ctx ty -> Type) ->
{ctx : SnocList Ty} ->
{ty : Ty} ->
(t : Term ctx ty) ->
Type
Valid p t =
{ctx' : SnocList Ty} ->
(sub : Terms ctx' ctx) ->
(allRel : AllLogRel p sub) ->
LogRel p (subst t sub)
public export
record ValidHelper (P : {ctx, ty : _} -> Term ctx ty -> Type) where
constructor MkValidHelper
var : forall ctx. {ty : Ty} -> (i : Elem ty ctx) -> LogRel P (Var i)
abs : forall ctx, ty. {ty' : Ty} -> {0 t : Term (ctx :< ty) ty'} -> LogRel P t -> P (Abs t)
zero : forall ctx. P {ctx} Zero
suc : forall ctx. {0 t : Term ctx N} -> P t -> P (Suc t)
rec :
{ctx : SnocList Ty} ->
{ty : Ty} ->
{0 t : Term ctx N} ->
{u : Term ctx ty} ->
{v : Term ctx (ty ~> ty)} ->
LogRel P t ->
LogRel P u ->
LogRel P v ->
LogRel P (Rec t u v)
step : forall ctx, ty. {0 t, u : Term ctx ty} -> P t -> (0 _ : u > t) -> P u
wkn : forall ctx, ctx', ty.
{0 t : Term ctx ty} ->
P t ->
(thin : ctx `Thins` ctx') ->
P (wkn t thin)
%name ValidHelper help
wknRel :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
ValidHelper P ->
{ty : Ty} ->
{0 t : Term ctx ty} ->
LogRel P t ->
(thin : ctx `Thins` ctx') ->
LogRel P (wkn t thin)
wknRel help {ty = N} pt thin = help.wkn pt thin
wknRel help {ty = ty ~> ty'} (pt, app) thin =
( help.wkn pt thin
, \thin', u, rel => rewrite wknHomo t thin thin' in app (thin' . thin) u rel
)
wknAllRel :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
ValidHelper P ->
{ctx : SnocList Ty} ->
{sub : Terms ctx' ctx} ->
AllLogRel P sub ->
(thin : ctx' `Thins` ctx'') ->
AllLogRel P (wknAll sub thin)
wknAllRel help Base thin = Base
wknAllRel help (allRels :< rel) thin = wknAllRel help allRels thin :< wknRel help rel thin
export
allValid :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
{ctx : SnocList Ty} ->
{ty : Ty} ->
ValidHelper P ->
(t : Term ctx ty) ->
Valid P t
allValid help (Var i) sub allRels = index help.var allRels i
allValid help (Abs t) sub allRels =
let valid = allValid help t in
(let
rec =
valid
(wknAll sub (Drop $ id @{termsLen sub}) :< Var Here)
(wknAllRel help allRels (Drop $ id @{termsLen sub}) :< help.var Here)
in
help.abs rec
, \thin, u, rel =>
let
eq :
(subst
(wkn (subst t (wknAll sub (Drop $ id @{termsLen sub}) :< Var Here)) (Keep thin))
(Base (id @{length _}) :< u) =
subst t (wknAll sub thin :< u))
eq =
rewrite lenUnique (termsLen sub) (length ctx') in
Calc $
|~ subst (wkn (subst t (wknAll sub (Drop id) :< Var Here)) (Keep thin)) (Base id :< u)
~~ subst (subst t (wknAll sub (Drop id) :< Var Here)) (restrict (Base id :< u) (Keep thin))
...(substWkn (subst t (wknAll sub (Drop id) :< Var Here)) (Keep thin) (Base id :< u))
~~ subst (subst t (wknAll sub (Drop id) :< Var Here)) (Base thin :< u)
...(cong (subst (subst t (wknAll sub (Drop id) :< Var Here)) . (:< u) . Base) $ identityLeft thin)
~~ subst t ((Base thin :< u) . wknAll sub (Drop id) :< u)
...(substHomo t (wknAll sub (Drop id) :< Var Here) (Base thin :< u))
~~ subst t (Base (thin . id) . sub :< u)
...(cong (subst t . (:< u)) $ compWknAll sub (Base thin :< u) (Drop id))
~~ subst t (Base thin . sub :< u)
...(cong (subst t . (:< u) . (. sub) . Base) $ identityRight thin)
~~ subst t (wknAll sub thin :< u)
...(cong (subst t . (:< u)) $ baseComp thin sub)
in
preserve
(backStepHelper help.step)
(valid (wknAll sub thin :< u) (wknAllRel help allRels thin :< rel))
(replace {p = (App (wkn (subst (Abs t) sub) thin) u >)}
eq
(AppBeta (length _)))
)
allValid help (App t u) sub allRels =
let (pt, app) = allValid help t sub allRels in
let rel = allValid help u sub allRels in
rewrite sym $ wknId @{termsLen sub} (subst t sub) in
app (id @{termsLen sub}) (subst u sub) rel
allValid help Zero sub allRels = help.zero
allValid help (Suc t) sub allRels =
let pt = allValid help t sub allRels in
help.suc pt
allValid help (Rec t u v) sub allRels =
let relt = allValid help t sub allRels in
let relu = allValid help u sub allRels in
let relv = allValid help v sub allRels in
help.rec relt relu relv
export
allRel :
{P : {ctx, ty : _} -> Term ctx ty -> Type} ->
{ctx : SnocList Ty} ->
{ty : Ty} ->
ValidHelper P ->
(t : Term ctx ty) ->
P t
allRel help t =
rewrite sym $ substId @{length ctx} t in escape (allValid help t (Base $ id @{length ctx}) Base)
|