-
Notifications
You must be signed in to change notification settings - Fork 2
/
SATnat.v
323 lines (275 loc) · 8.34 KB
/
SATnat.v
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
(** Saturated sets constructions related to natural numbers: interpreting constructors,
dependent pattern-matching and fixpoint. Does not support size annotations because
the definition of (fNAT k) requires that the branch associated to the successor
has to be defined for *all* numbers, not just those smaller than k. *)
Set Implicit Arguments.
Require Import basic Lambda Can Sat.
Require Import Models.
(** An abstract strong normalization model of natural numbers, set-
theoretical interpretation. *)
Module Type SimpleNats (Import S:Sets).
(** N is the type of natural numbers including neutral values.
Nbot is a decidable subet of N. *)
Parameter N Nbot : X.
Parameter N_Nbot : N ⊆ Nbot.
Parameter Ndec : forall n, n ∈ Nbot -> n∈N \/ ~n∈N.
Parameter zero : X.
Parameter succ : X -> X.
Parameter succ_morph : Proper (eqX==>eqX) succ.
Existing Instance succ_morph.
(** Constructors produce non-neutral values *)
Parameter zero_typ : zero ∈ N.
Parameter succ_typ : forall n, n ∈ Nbot -> succ n ∈ N.
End SimpleNats.
(** A functor producing the realizability familes associated to natural
numbers, given the set-theoretical intrepretation. *)
Module Make (Import S:Sets)(Import N:SimpleNats S).
(** Quantification over families *)
Definition piFAM F :=
depSAT (fun P:X->SAT => Proper (eqX==>eqSAT)P) F.
Lemma piFAM_ax t F :
inSAT t (piFAM F) <-> forall P, Proper (eqX==>eqSAT)P->inSAT t (F P).
split; intros.
apply depSAT_elim with (1:=H)(x:=P)(2:=H0).
apply depSAT_intro; auto.
apply sat_sn with (F (fun _ => snSAT)).
apply H.
do 2 red; reflexivity.
Qed.
Instance piFAM_morph : Proper (((eqX==>eqSAT)==>eqSAT)==>eqSAT) piFAM.
do 2 red; intros.
apply interSAT_morph.
apply indexed_relation_id; intros (P,Pm); simpl; auto.
Qed.
(** * Functional applying constructors of Nat to A *)
Definition fNAT (A:X->SAT) (k:X) :=
piFAM(fun P =>
prodSAT (P zero)
(prodSAT (depSAT (fun n => n ∈ Nbot) (fun n => prodSAT (A n) (prodSAT (P n) (P (succ n)))))
(P k))).
Instance fNAT_morph : Proper ((eqX==>eqSAT)==>eqX==>eqSAT) fNAT.
do 3 red; intros.
apply piFAM_morph.
red; intros.
apply prodSAT_morph.
apply H1; reflexivity.
apply prodSAT_morph; auto.
apply interSAT_morph.
apply indexed_relation_id; intros.
apply prodSAT_morph; auto with *.
apply prodSAT_morph; auto with *.
Qed.
Lemma fNAT_def t A k :
inSAT t (fNAT A k) <->
forall P f g,
Proper (eqX==>eqSAT) P ->
inSAT f (P zero) ->
inSAT g (depSAT(fun n=>n ∈ Nbot)(fun n => prodSAT (A n) (prodSAT (P n) (P (succ n))))) ->
inSAT (App2 t f g) (P k).
intros.
unfold fNAT.
rewrite piFAM_ax.
apply fa_morph; intros P.
split; intros.
apply prodSAT_elim with (2:=H1) in H; trivial.
apply prodSAT_elim with (1:=H); trivial.
apply prodSAT_intro'; intros f satf.
apply prodSAT_intro'; intros g satg.
apply H; trivial.
Qed.
Lemma fNAT_mono A B :
(forall k, k ∈ Nbot -> inclSAT (A k) (B k)) ->
forall k, inclSAT (fNAT A k) (fNAT B k).
unfold fNAT; intros.
apply interSAT_mono; intro P.
apply prodSAT_mono; auto with *.
apply prodSAT_mono; auto with *.
apply interSAT_mono; intros (n,tyn); simpl.
apply prodSAT_mono; auto with *.
apply H; trivial.
Qed.
(** * Realizability relation of Nat: fixpoint of fNAT *)
(** cNAT is the intersection of all families that are post-fixpoint (that is,
P s.t. fNAT P included in P). Here we require k to be well-typed (k ∈ N),
but we could do otherwise. Note that k∈N instead of k∈Nbot ensures that
cNAT is neuSAT for neutral values. *)
Definition cNAT n :=
depSAT (fun P => Proper (eqX==>eqSAT) P /\
forall k, k ∈ N -> inclSAT (fNAT P k) (P k)) (fun P => P n).
Instance cNAT_morph : Proper (eqX==>eqSAT) cNAT.
do 2 red; intros.
apply interSAT_morph.
apply indexed_relation_id; intros (P,(Pm,Pind)); simpl; auto.
Qed.
Lemma cNAT_post k :
k ∈ N ->
inclSAT (fNAT cNAT k) (cNAT k).
intros tyk t tsat.
unfold cNAT.
apply depSAT_intro; intros.
apply sat_sn in tsat; trivial.
apply H; trivial.
revert t tsat.
apply fNAT_mono.
clear k tyk; intros k tyk t tsat.
eapply depSAT_elim with (F:=fun P => P k); [apply tsat|].
exact H.
Qed.
Definition fNAT' A k := condSAT (k∈N) (fNAT A k).
Lemma cNAT_pre_strict k :
k ∈ N ->
inclSAT (cNAT k) (fNAT cNAT k).
intros tyk t tsat.
apply condSAT_smaller with (P:=k ∈ N).
eapply depSAT_elim
with (F:=fun P => P k) (1:=tsat) (x:=fNAT' cNAT).
split.
do 2 red; intros; apply condSAT_morph.
rewrite H; reflexivity.
apply fNAT_morph; trivial; apply cNAT_morph.
clear; intros k tyk.
transitivity (fNAT cNAT k).
apply fNAT_mono; clear; intros k tyk.
destruct Ndec with (1:=tyk) as [tyk'|tyk'].
unfold fNAT'; rewrite condSAT_ok; auto.
apply cNAT_post; trivial.
apply condSAT_neutral; trivial.
unfold fNAT'; rewrite condSAT_ok; auto with *.
Qed.
Lemma cNAT_out k S :
~ k ∈ N ->
inclSAT (cNAT k) S.
intros notn t tsat.
unfold cNAT, depSAT in tsat.
pose (P:= fun k => condSAT (k∈N) (cNAT k)).
assert (Pm : Proper (eqX==>eqSAT) P).
do 2 red; intros.
apply condSAT_morph.
rewrite H; reflexivity.
apply cNAT_morph; trivial.
assert (Ppost:forall k, k ∈ N -> inclSAT (fNAT P k) (P k)).
clear.
intros.
transitivity (fNAT cNAT k).
apply fNAT_mono; clear; intros k tyk.
unfold P.
apply condSAT_smaller.
red; intros; unfold P.
rewrite condSAT_ok; trivial.
apply cNAT_post; trivial.
specialize interSAT_elim with (1:=tsat)
(x:=exist (fun P=>Proper(eqX==>eqSAT)P/\
forall k,k∈N->inclSAT(fNAT P k)(P k)) P (conj Pm Ppost)).
simpl.
apply condSAT_neutral; red; auto with *.
Qed.
Lemma cNAT_pre k :
k ∈ Nbot ->
inclSAT (cNAT k) (fNAT cNAT k).
intros tyk t tsat.
destruct Ndec with (1:=tyk).
apply cNAT_pre_strict; trivial.
revert tsat; apply cNAT_out; trivial.
Qed.
(** Fixpoint equation *)
Lemma cNAT_eq : forall k, k ∈ N -> eqSAT (cNAT k) (fNAT cNAT k).
split.
apply cNAT_pre; apply N_Nbot; trivial.
apply cNAT_post; trivial.
Qed.
(** * Constructors *)
(** Interp of 0 *)
Definition ZE := Abs (Abs (Ref 1)).
Lemma ZE_iota t1 t2 :
redp (App2 ZE t1 t2) t1.
unfold ZE.
eapply t_trans;[apply redp_app_l;apply t_step;apply red1_beta; reflexivity|].
unfold subst; simpl.
apply t_step.
apply red1_beta.
unfold subst; rewrite simpl_subst; trivial.
rewrite lift0; trivial.
Qed.
Lemma fNAT_ZE : forall A, inSAT ZE (fNAT A zero).
intros.
rewrite fNAT_def; intros.
unfold ZE.
eapply inSAT_context.
intros.
apply inSAT_exp; auto.
exact H2.
unfold subst; simpl subst_rec.
apply inSAT_exp.
apply sat_sn in H1; auto.
unfold subst; rewrite simpl_subst; auto.
rewrite lift0; auto.
Qed.
(** ZE realizes 0 *)
Lemma cNAT_ZE : inSAT ZE (cNAT zero).
rewrite cNAT_eq.
apply fNAT_ZE.
apply zero_typ.
Qed.
(** Interp of successor. Unlike in system F the function corresponding to the successor
expects two arguments: the predecessor and the usual result of recursive call.
S(n) is (fun f g => g n (n f g)) instead of the usual (fun f g => g (n f g)).
*)
Definition SU := Abs (Abs (Abs
(App2 (Ref 0) (Ref 2) (App2 (Ref 2) (Ref 1) (Ref 0))))).
Lemma SU_iota n t1 t2 :
redp (App2 (App SU n) t1 t2) (App2 t2 n (App2 n t1 t2)).
unfold SU.
eapply t_trans.
do 2 apply redp_app_l.
apply t_step; apply red1_beta; reflexivity.
unfold subst; simpl.
eapply t_trans.
apply redp_app_l.
apply t_step; apply red1_beta; reflexivity.
unfold subst; simpl.
rewrite simpl_subst; auto.
apply t_step; apply red1_beta.
unfold subst; simpl.
rewrite simpl_subst; auto.
rewrite simpl_subst; auto.
do 3 rewrite lift0.
reflexivity.
Qed.
Lemma fNAT_SU : forall A n t,
n ∈ Nbot ->
inSAT t (A n) ->
inSAT t (fNAT A n) ->
inSAT (App SU t) (fNAT A (succ n)).
intros A n t nty H H0.
unfold SU.
apply inSAT_exp;[auto|].
unfold subst; simpl subst_rec.
rewrite fNAT_def; intros.
eapply inSAT_context.
intros.
apply inSAT_exp; [right|].
apply sat_sn in H2; trivial.
eexact H4.
unfold subst; simpl subst_rec.
apply inSAT_exp; auto.
unfold subst; simpl subst_rec.
repeat rewrite simpl_subst; auto.
repeat rewrite lift0.
apply prodSAT_elim with (P n).
apply prodSAT_elim with (2:=H).
eapply (depSAT_elim _ H3); trivial.
rewrite fNAT_def in H0.
apply H0; trivial.
Qed.
(** SU realizes the successor *)
Lemma cNAT_SU n t :
n ∈ Nbot ->
inSAT t (cNAT n) ->
inSAT (App SU t) (cNAT (succ n)).
intros.
rewrite cNAT_eq.
apply fNAT_SU; trivial.
apply cNAT_pre; trivial.
apply succ_typ; trivial.
Qed.
End Make.