forked from math-comp/cad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semialgebraic.v
2685 lines (2284 loc) · 93 KB
/
semialgebraic.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* (c) Copyright Microsoft Corporation and Inria. All rights reserved. *)
(*****************************************************************************)
(* This file formalises semi-algebraic sets and semi-algebraic functions. *)
(* Semi-algebraic sets are constructed by a quotient of formulae. *)
(* The main construction is the implementation of the abstract set interface *)
(* for semi-algebraic sets and functions. *)
(* *)
(*****************************************************************************)
Require Import ZArith Init.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import ssrfun ssrbool eqtype ssrnat seq choice fintype div.
From mathcomp Require Import tuple finfun generic_quotient bigop finset perm.
From mathcomp Require Import ssralg poly polydiv ssrnum mxpoly binomial finalg.
From mathcomp Require Import zmodp mxpoly mxtens qe_rcf ordered_qelim realalg.
From mathcomp Require Import matrix finmap order set.
From SemiAlgebraic Require Import auxresults.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Delimit Scope abstract_set_scope with set.
Local Open Scope abstract_set_scope.
Import GRing.Theory Num.Theory Num.Def.
Import ord.
Import Order.Theory Order.Syntax Order.Def.
Local Open Scope nat_scope.
Local Open Scope ring_scope.
Local Open Scope fset_scope.
Local Open Scope fmap_scope.
Local Open Scope quotient_scope.
Local Open Scope type_scope.
Reserved Notation "'{formula_' n F }"
(n at level 0, format "'{formula_' n F }").
Reserved Notation "{ 'SAset' F }"
(format "{ 'SAset' F }").
Reserved Notation "{ 'SAfun' T }"
(format "{ 'SAfun' T }").
Notation mnfset i j := (seq_fset (iota i j)).
Notation "f <==> g" := ((f ==> g) /\ (g ==> f))%oT (at level 0) : oterm_scope.
Section EquivFormula.
Variable T : Type.
Fixpoint term_fv (t : GRing.term T) : {fset nat} :=
match t with
| 'X_i => [fset i]
| t1 + t2 | t1 * t2 => term_fv t1 `|` term_fv t2
| - t1 | t1 *+ _ | t1 ^+ _ | t1^-1 => term_fv t1
| _ => fset0
end%T.
Fixpoint formula_fv (f : formula T) : {fset nat} :=
match f with
| Bool _ => fset0
| t1 == t2 | t1 <% t2 | t1 <=% t2 => term_fv t1 `|` term_fv t2
| Unit t1 => term_fv t1
| f1 /\ f2 | f1 \/ f2 | f1 ==> f2 => formula_fv f1 `|` formula_fv f2
| ~ f1 => formula_fv f1
| ('exists 'X_i, g) | ('forall 'X_i, g) => formula_fv g `\ i
end%oT.
Fixpoint gen_var_seq (s : seq nat) (f : formula T) := match s with
| [::] => f
| i::l => ('forall 'X_i, gen_var_seq l f)
end%oT.
Definition equiv_formula (f g : formula T) :=
gen_var_seq (enum_fset ((formula_fv f) `|` (formula_fv g))) (f <==> g)%oT.
Definition nvar n : pred_class := fun f :
formula T => (formula_fv f `<=` mnfset O n).
Record formulan n := MkFormulan
{
underlying_formula :> formula T;
_ : nvar n underlying_formula
}.
Canonical formulan_subType n :=
Eval hnf in [subType for @underlying_formula n].
End EquivFormula.
Notation "'{formula_' n T }" := (formulan T n).
Section EncodeFormula.
Variable T : Type.
Fixpoint encode_term (t : GRing.term T) := match t with
| 'X_i => GenTree.Node (2 * i) [::]
| x %:T => GenTree.Leaf x
| i%:R => GenTree.Node ((2 * i).+1) [::]
| t1 + t2 => GenTree.Node O ((encode_term t1)::(encode_term t2)::nil)
| - t => GenTree.Node O ((encode_term t)::nil)
| x *+ i => GenTree.Node ((2 * i).+2) ((encode_term x)::nil)
| t1 * t2 => GenTree.Node 1 ((encode_term t1)::(encode_term t2)::nil)
| t ^-1 => GenTree.Node 1 ((encode_term t)::nil)
| x ^+ i => GenTree.Node ((2 * i).+3) ((encode_term x)::nil)
end%T.
Fixpoint decode_term (t : GenTree.tree T) := match t with
| GenTree.Leaf x => x%:T
| GenTree.Node i s => match s with
| [::] => if (i %% 2)%N == O then GRing.Var T (i %/ 2) else ((i.-1) %/ 2)%:R
| e1::e2::l => if i == O then (decode_term e1) + (decode_term e2)
else (decode_term e1) * (decode_term e2)
| e::l => if i == O then - (decode_term e) else
if i == 1%N then (decode_term e)^-1 else
if (i %% 2)%N == O then (decode_term e) *+ ((i.-2) %/ 2)
else (decode_term e) ^+ ((i - 3) %/ 2)
end
end%T.
Lemma encode_termK : cancel encode_term decode_term.
Proof.
move=> t; elim: t.
+ by move=> n /=; rewrite modnMr eqxx mulKn.
+ by move=> r.
+ by move=> n /=; rewrite {1}mulnC -addn1 modnMDl mulKn.
+ by move=> t1 h1 t2 h2 /=; rewrite h1 h2.
+ by move=> t h /=; rewrite h.
+ by move=> t h n /=; rewrite -addn2 {1}mulnC modnMDl h mulKn.
+ by move=> t1 h1 t2 h2 /=; rewrite h1 h2.
+ by move=> t h /=; rewrite h.
+ by move=> t h n /=; rewrite -addn3 {1}mulnC modnMDl h addnK mulKn.
Qed.
Fixpoint encode_formula (f : formula T) := match f with
| Bool b => GenTree.Node b [::]
| t1 == t2 => GenTree.Node O [:: encode_term t1; encode_term t2]
| t1 <% t2 => GenTree.Node 1 ((encode_term t1)::(encode_term t2)::nil)
| t1 <=% t2 => GenTree.Node 2 ((encode_term t1)::(encode_term t2)::nil)
| Unit t => GenTree.Node O ((encode_term t)::nil)
| f1 /\ f2 => GenTree.Node 3 ((encode_formula f1)::(encode_formula f2)::nil)
| f1 \/ f2 => GenTree.Node 4 ((encode_formula f1)::(encode_formula f2)::nil)
| f1 ==> f2 => GenTree.Node 5 ((encode_formula f1)::(encode_formula f2)::nil)
| ~ f => GenTree.Node 1 ((encode_formula f)::nil)
| ('exists 'X_i, f) => GenTree.Node (2 * i).+2 ((encode_formula f)::nil)
| ('forall 'X_i, f) => GenTree.Node (2 * i).+3 ((encode_formula f)::nil)
end%oT.
Fixpoint decode_formula (t : GenTree.tree T) := match t with
| GenTree.Leaf x => Unit (Const x)
| GenTree.Node i s => match s with
| [::] => if i == O then Bool false else Bool true
| e1::e2::l => match i with
| O => (decode_term e1) == (decode_term e2)
| 1%N => (decode_term e1) <% (decode_term e2)
| 2 => (decode_term e1) <=% (decode_term e2)
| 3 => (decode_formula e1) /\ (decode_formula e2)
| 4 => (decode_formula e1) \/ (decode_formula e2)
| _ => (decode_formula e1) ==> (decode_formula e2)
end
| e::l => if i == O then Unit (decode_term e) else
if i == 1%N then Not (decode_formula e) else
if (i %% 2)%N == O
then ('exists 'X_((i.-2) %/ 2), decode_formula e)
else ('forall 'X_((i - 3) %/ 2), decode_formula e)
end
end%oT.
Lemma encode_formulaK : cancel encode_formula decode_formula.
Proof.
move=> f; elim: f.
+ by move=> b /=; case: b.
+ by move=> t1 t2 /=; rewrite !encode_termK.
+ by move=> t1 t2 /=; rewrite !encode_termK.
+ by move=> t1 t2 /=; rewrite !encode_termK.
+ by move=> t /=; rewrite !encode_termK.
+ by move=> f1 h1 f2 h2 /=; rewrite h1 h2.
+ by move=> f1 h1 f2 h2 /=; rewrite h1 h2.
+ by move=> f1 h1 f2 h2 /=; rewrite h1 h2.
+ by move=> f hf /=; rewrite hf.
+ by move=> i f hf /=; rewrite hf -addn2 {1}mulnC modnMDl mulKn /=.
+ by move=> i f hf /=; rewrite hf -addn3 {1}mulnC modnMDl /= addnK mulKn.
Qed.
End EncodeFormula.
Definition formula_eqMixin (T : eqType) := CanEqMixin (@encode_formulaK T).
Canonical formula_eqType (T : eqType) :=
EqType (formula T) (formula_eqMixin T).
Definition formulan_eqMixin (T : eqType) n := [eqMixin of {formula_n T} by <:].
Canonical formulan_eqType (T : eqType) n :=
EqType (formulan T n) (formulan_eqMixin T n).
Definition formula_choiceMixin (T : choiceType) :=
CanChoiceMixin (@encode_formulaK T).
Canonical formula_choiceType (T : choiceType) :=
ChoiceType (formula T) (formula_choiceMixin T).
Definition formulan_choiceMixin (T : choiceType) n :=
[choiceMixin of {formula_n T} by <:].
Canonical formulan_choiceType (T : choiceType) n :=
ChoiceType (formulan T n) (formulan_choiceMixin T n).
Section FormulaSubst.
Variable T : Type.
Lemma tsubst_id (t1 t2 : GRing.term T) (i : nat) :
i \notin (term_fv t1) -> GRing.tsubst t1 (i, t2)%oT = t1.
Proof.
move: t2; elim: t1.
- by move=> j t2 /=; rewrite in_fset1 eq_sym => /negbTE ->.
- by move=> x t2.
- by move=> j t2 h.
- move=> t1 h1 t2 h2 t3 /=.
rewrite in_fsetU negb_or => /andP [hi1 hi2].
by rewrite h1 // h2.
- by move=> t1 h1 t2 /= hi; rewrite h1.
- by move=> t1 h1 j hj /= hi; rewrite h1.
- move=> t1 h1 t2 h2 t3 /=.
rewrite in_fsetU negb_or => /andP [hi1 hi2].
by rewrite h1 // h2.
- by move=> t1 h1 t2 /= h2; rewrite h1.
- by move=> t1 h1 j t2 /= hi; rewrite h1.
Qed.
Lemma fsubst_id (f : formula T) (t : GRing.term T) (i : nat) :
i \notin (formula_fv f) -> fsubst f (i, t)%oT = f.
Proof.
move: t; elim: f.
- by move=> b t.
- move=> t1 t2 t3 /=.
rewrite in_fsetU negb_or => /andP [hi1 hi2].
by rewrite !tsubst_id.
- move=> t1 t2 t3 /=.
rewrite in_fsetU negb_or => /andP [hi1 hi2].
by rewrite !tsubst_id.
- move=> t1 t2 t3 /=.
rewrite in_fsetU negb_or => /andP [hi1 hi2].
by rewrite !tsubst_id.
- by move=> t1 t2 hi /=; rewrite tsubst_id.
- move=> f1 h1 f2 h2 t.
rewrite in_fsetU negb_or => /andP [hi1 hi2] /=.
by rewrite h1 // h2.
- move=> f1 h1 f2 h2 t.
rewrite in_fsetU negb_or => /andP [hi1 hi2] /=.
by rewrite h1 // h2.
- move=> f1 h1 f2 h2 t.
rewrite in_fsetU negb_or => /andP [hi1 hi2] /=.
by rewrite h1 // h2.
- by move=> f hf t /= hi; rewrite hf.
- move=> j f hf t /=.
have [<- | /negbTE neq_ij] := eqVneq i j; rewrite ?eqxx //.
rewrite eq_sym neq_ij => h; rewrite hf //.
move: h; apply: contra.
by rewrite in_fsetD1 neq_ij.
- move=> j f hf t /=.
have [<- | /negbTE neq_ij] := eqVneq i j; rewrite ?eqxx //.
rewrite eq_sym neq_ij => h; rewrite hf //.
move: h; apply: contra.
by rewrite in_fsetD1 neq_ij.
Qed.
End FormulaSubst.
Section RealDomainFormula.
Variable R : realDomainType.
Definition is_equiv (f g : formula R) := holds [::] (equiv_formula f g).
Fact nquantify_key : unit. Proof. exact: tt. Qed.
Definition nquantify (n k : nat) (Q : nat -> formula R -> formula R)
(f : formula R) :=
locked_with nquantify_key (iteri k (fun i f => (Q (n + k - i.+1)%N f)) f).
Lemma nquantSout (n k : nat) Q (f : formula R) :
nquantify n k.+1 Q f = Q n (nquantify n.+1 k Q f).
Proof.
rewrite /nquantify !unlock /= addnK; congr (Q _ _); apply: eq_iteri => i g.
by rewrite addnS addSn.
Qed.
Lemma nquantify0 (n : nat) Q (f : formula R) : nquantify n 0 Q f = f.
Proof. by rewrite /nquantify !unlock. Qed.
Lemma nquantify1 (n : nat) Q (f : formula R) : nquantify n 1 Q f = Q n f.
Proof. by rewrite nquantSout nquantify0. Qed.
Lemma nquantify_add (m n k : nat) Q (f : formula R) :
nquantify m (n + k) Q f = nquantify m n Q (nquantify (m + n) k Q f).
Proof.
elim: n => [|n IHn] in k m *;
rewrite ?(nquantify0, nquantSout, addn0, addSn) //=.
by rewrite IHn addnS addSn.
Qed.
Lemma nquantSin (n k : nat) Q (f : formula R) :
nquantify n k.+1 Q f = (nquantify n k Q (Q (n + k)%N f)).
Proof. by rewrite -addn1 nquantify_add nquantify1. Qed.
Lemma nforallP (k : nat) (e : seq R) (f : formula R) :
(forall v : k.-tuple R, holds (e ++ v) f)
<-> (holds e (nquantify (size e) k Forall f)).
Proof.
elim: k => [|k IHk] /= in e *.
rewrite nquantify0; split.
by move=> /(_ [tuple of [::]]); rewrite cats0.
by move=> hef v; rewrite tuple0 cats0.
rewrite nquantSout /=; split => holdsf; last first.
move=> v; case: (tupleP v) => x {v} v /=.
rewrite -cat_rcons -(rcons_set_nth _ 0%:R).
by move: v; apply/IHk; rewrite ?size_set_nth (maxn_idPl _).
move=> x; set e' := set_nth _ _ _ _.
have -> : (size e).+1 = size e' by rewrite size_set_nth (maxn_idPl _).
apply/IHk => v; suff -> : e' ++ v = e ++ [tuple of x :: v] by apply: holdsf.
by rewrite /e' /= rcons_set_nth cat_rcons.
Qed.
Lemma nexistsP (k : nat) (e : seq R) (f : formula R) :
(exists v : k.-tuple R, holds (e ++ v) f)
<-> (holds e (nquantify (size e) k Exists f)).
Proof.
elim: k => [|k IHk] /= in e *.
- rewrite nquantify0; split; first by move=> [v]; rewrite tuple0 cats0.
by exists [tuple of [::]]; rewrite cats0.
- rewrite nquantSout /=; split => [[v holdsf]|[x holdsf]].
+ case: (tupleP v) => x {v} v /= in holdsf *.
exists x; set e' := set_nth _ _ _ _.
have -> : (size e).+1 = size e' by rewrite size_set_nth (maxn_idPl _).
by apply/IHk; exists v; rewrite /e' /= rcons_set_nth cat_rcons.
+ move: holdsf; set e' := set_nth _ _ _ _.
have -> : (size e).+1 = size e' by rewrite size_set_nth (maxn_idPl _).
move/IHk => [v]; rewrite /e' /= rcons_set_nth cat_rcons.
by exists [tuple of x :: v].
Qed.
Lemma nforall_is_true (f : formula R) :
(forall (e : seq R), holds e f) ->
forall (n i : nat) (e : seq R), holds e (nquantify n i Forall f).
Proof.
move=> h n i; elim: i => [|i IHi] in n *;
by rewrite ?(nquantify0, nquantSout) /=.
Qed.
Lemma holds_rcons_zero (e : seq R) (f : formula R) :
holds (rcons e 0%:R) f <-> holds e f.
Proof.
split; apply: eq_holds=> // i; rewrite nth_rcons;
by have [| /ltnW h|->] := ltngtP _ (size _)=> //; rewrite ?nth_default.
Qed.
Lemma holds_cat_nseq (i : nat) (e : seq R) (f : formula R) :
holds (e ++ (nseq i 0)) f <-> holds e f.
Proof.
rewrite nseq_cat; move: e f; elim: i => // i ih e f.
by apply: (iff_trans _ (ih e f)); apply: holds_rcons_zero.
Qed.
Lemma monotonic_nforall (n k : nat) (e : seq R) (f g : formula R) :
(forall (e' : seq R), holds e' f -> holds e' g) ->
holds e (nquantify n k Forall f) -> holds e (nquantify n k Forall g).
Proof.
move: n e f g; elim: k => [k e f g | k ih n e f g h hf].
by rewrite !nquantify0; move/(_ e).
rewrite nquantSin.
apply: (ih n e ('forall 'X_(n + k), f)%oT);last by move: hf;rewrite nquantSin.
move=> e' nk_f x.
by apply: h; apply: nk_f.
Qed.
Lemma monotonic_nexist (n k : nat) (e : seq R) (f g : formula R) :
(forall (e' : seq R), holds e' f -> holds e' g) ->
holds e (nquantify n k Exists f) -> holds e (nquantify n k Exists g).
Proof.
move: n e f g; elim: k => [k e f g|k iH n e f g h hf].
by rewrite !nquantify0; move/(_ e).
rewrite nquantSin.
apply: (iH n e ('exists 'X_(n + k), f)%oT); last by move: hf; rewrite nquantSin.
move=> e' /= [x nk_f].
by exists x; apply: h; apply: nk_f.
Qed.
Lemma monotonic_forall_if (i : nat) (e : seq R) (f g : formula R) :
(forall (e' : seq R), holds e' f -> holds e' g) ->
holds e ('forall 'X_i, f) -> holds e ('forall 'X_i, g).
Proof.
move=> h; move: (@monotonic_nforall i 1 e f g).
by rewrite /nquantify [X in X -> _]/= !addnK !unlock => h'; apply: h'.
Qed.
Fact monotonic_forall_iff (i : nat) (e : seq R) (f g : formula R) :
(forall (e' : seq R), holds e' f <-> holds e' g) ->
holds e ('forall 'X_i, f) <-> holds e ('forall 'X_i, g).
Proof. by move=> h; split; apply: monotonic_forall_if=> e'; move/(h e'). Qed.
Lemma holds_nforall (n k : nat) (e : seq R) (f : formula R) :
holds e (nquantify n k Forall f) -> holds e f.
Proof.
move: e f; elim: k => [e f|k iHk e f h]; first by rewrite nquantify0.
apply: iHk; move: h; rewrite nquantSin. apply: monotonic_nforall.
by move=> e'; move/(_ e'`_(n + k)); rewrite set_nth_nth; move/holds_cat_nseq.
Qed.
Fact holds_forall (i : nat) (e : seq R) (f : formula R) :
holds e ('forall 'X_i, f) -> holds e f.
Proof.
by move=> h; apply: (@holds_nforall i 1); rewrite /nquantify /= addnK unlock.
Qed.
End RealDomainFormula.
Section RealClosedFieldFormula.
Variable F : rcfType. (* is also a realDomainType *)
Fact qf_form_elim (f : formula F) :
rformula f -> qf_form (@quantifier_elim _ (@wproj _) f).
Proof.
by move=> h; move/andP: (quantifier_elim_wf (@wf_QE_wproj _) h) => [qf_f _].
Qed.
Fact rform_elim (f : formula F) :
rformula f -> rformula (@quantifier_elim _ (@wproj _) f).
Proof.
by move=> h; move/andP: (quantifier_elim_wf (@wf_QE_wproj _) h) => [_ rform_f].
Qed.
Fact elim_rformP (f : formula F) (e : seq F) :
rformula f -> reflect (holds e f) (qf_eval e (@quantifier_elim _ (@wproj _) f)).
Proof.
move=> rform_f; apply: quantifier_elim_rformP => //.
- move=> i bc /= h.
by apply: wf_QE_wproj.
- move=> i bc /= e' h.
by apply: valid_QE_wproj.
Qed.
Fact rcf_sat_Bool (e : seq F) (b : bool) : rcf_sat e (Bool b) = b.
Proof. by []. Qed.
Fact rcf_sat_Equal (e : seq F) (t1 t2 : GRing.term F) :
rcf_sat e (t1 == t2) = (GRing.eval e t1 == GRing.eval e t2).
Proof. by apply/rcf_satP/idP => h; apply/eqP. Qed.
Fact rcf_sat_Lt (e : seq F) (t1 t2 : GRing.term F) :
rcf_sat e (t1 <% t2) = (GRing.eval e t1 < GRing.eval e t2).
Proof. by apply/rcf_satP/idP. Qed.
Fact rcf_sat_Le (e : seq F) (t1 t2 : GRing.term F) :
rcf_sat e (t1 <=% t2) = (GRing.eval e t1 <= GRing.eval e t2).
Proof. by apply/rcf_satP/idP. Qed.
Fact rcf_sat_Unit (e : seq F) (t : GRing.term F) :
rcf_sat e (Unit t) = (GRing.eval e t \is a GRing.unit).
Proof. by apply/rcf_satP/idP. Qed.
Fact rcf_sat_And (e : seq F) (f g : formula F) :
rcf_sat e (f /\ g) = (rcf_sat e f) && (rcf_sat e g).
Proof. by []. Qed.
Fact rcf_sat_Or (e : seq F) (f g : formula F) :
rcf_sat e (f \/ g) = (rcf_sat e f) || (rcf_sat e g).
Proof. by []. Qed.
Fact rcf_sat_Implies (e : seq F) (f g : formula F) :
rcf_sat e (f ==> g) = ((rcf_sat e f) ==> (rcf_sat e g)).
Proof.
by apply/rcf_satP/implyP=> /= hfg; move/rcf_satP=> h; apply/rcf_satP; apply: hfg.
Qed.
Fact rcf_sat_Not (e : seq F) (f : formula F): rcf_sat e (~ f) = ~~ (rcf_sat e f).
Proof. by []. Qed.
Lemma holds_Nfv_ex (e : seq F) (i : nat) (f : formula F) :
i \notin formula_fv f -> (holds e ('exists 'X_i, f) <-> holds e f).
Proof.
move=> hi; split => [[x /holds_fsubst holds_ef]| h].
by move: holds_ef; rewrite fsubst_id.
by exists 0; apply/holds_fsubst; rewrite fsubst_id.
Qed.
Lemma holds_Nfv_all (e : seq F) (i : nat) (f : formula F) :
i \notin formula_fv f -> (holds e ('forall 'X_i, f) <-> holds e f).
Proof.
move=> hi; split => [|holds_ef x].
by move/(_ 0)/holds_fsubst; rewrite fsubst_id.
by apply/holds_fsubst; rewrite fsubst_id.
Qed.
Fact holds_Exists (e : seq F) (i : nat) (f : formula F) :
holds e f -> holds e ('exists 'X_i, f).
Proof.
move => holds_ef.
have [lt_ie|le_ei] := ltnP i (size e); first by exists e`_i; rewrite set_nth_id.
by exists 0; rewrite set_nth_over //; apply/holds_rcons_zero/holds_cat_nseq.
Qed.
Definition simp_rcf_sat := (rcf_sat_Bool, rcf_sat_Equal, rcf_sat_Lt, rcf_sat_Le,
rcf_sat_Unit, rcf_sat_And, rcf_sat_Or,
rcf_sat_Implies, rcf_sat_Not).
Lemma rcf_sat_cat_nseq (i : nat) (e : seq F) (f : formula F) :
rcf_sat (e ++ nseq i 0) f = rcf_sat e f.
Proof.
apply/rcf_satP/rcf_satP; first by move/holds_cat_nseq.
by move=> h; apply/holds_cat_nseq.
Qed.
Lemma eval_fv (t : GRing.term F) (e : seq F):
term_fv t = fset0 -> GRing.eval e t = GRing.eval [::] t.
Proof.
move/eqP; move=> h; elim/last_ind: e => //.
move=> s x <-; move: h; elim: t => //=.
- by move=> i; rewrite neq_fset10.
- move=> t1 h1 t2 h2.
rewrite /= fsetU_eq0 => /andP [ht1 ht2].
by rewrite h1 // h2.
- by move=> t /= ih h; rewrite ih.
- by move=> t1 h1 t2 h2; rewrite h1.
- move=> t1 h1 t2 h2.
rewrite fsetU_eq0 => /andP [ht1 ht2].
by rewrite h1 // h2.
- by move=> t ih h; rewrite ih.
- by move=> t ih i h; rewrite ih.
Qed.
Lemma nfsetE (i j : nat) : (i \in mnfset O j) = (i < j)%N.
Proof.
move: i; elim: j => [|j ih] i; first by rewrite ltn0 seq_fsetE.
case: i => [|i]; first by rewrite ltnS seq_fsetE inE leq0n.
by rewrite seq_fsetE inE mem_iota.
Qed.
Lemma mnfsetE (k i j : nat) : (k \in mnfset i j) = (i <= k < i + j)%N.
Proof. by rewrite seq_fsetE mem_iota. Qed.
Lemma card_mnfset (i j : nat) : #|` (mnfset i j)| = j.
Proof. by rewrite cardfsE undup_id ?iota_uniq // size_iota. Qed.
Lemma mnfset_triangle (i j k : nat) :
mnfset i (j + k) = mnfset i j `|` mnfset (i + j) k.
Proof.
by apply/eqP/fset_eqP => x; rewrite inE !seq_fsetE iota_add mem_cat.
Qed.
Lemma mnfset_nSn (i j : nat) : mnfset i j.+1 = mnfset i j `|` [fset (i + j)%N].
Proof.
by apply/eqP/fset_eqP => x; rewrite inE !seq_fsetE -addn1 iota_add mem_cat.
Qed.
Lemma mnfsetU (i j k l : nat) :
let a := minn i k in
(i <= k + l -> k <= i + j ->
mnfset i j `|` mnfset k l = mnfset a ((maxn (i + j) (k + l)) - a))%N.
Proof.
move=> /= h1 h2.
apply/eqP/fset_eqP => x /=.
rewrite inE !seq_fsetE !mem_iota subnKC; last first.
by rewrite leq_max (leq_trans (geq_minr _ _)).
rewrite geq_min leq_max orb_andl.
have [lt_xi|leq_ix] := ltnP x i => //=.
by rewrite (leq_trans lt_xi) //; case (_ <= _)%N.
have [small_x|big_x] := ltnP x (i+j) => //=.
by rewrite (leq_trans h2).
Qed.
Lemma mnfset_bigop (a b : nat) :
\bigcup_(i in 'I_b) ([fset (a + (nat_of_ord i))%N]) = mnfset a b.
Proof.
apply/eqP/fset_eqP => i; rewrite seq_fsetE /= mem_iota; apply/bigfcupP/idP.
move=> [j hj]; first by rewrite in_fset1 =>/eqP ->;rewrite leq_addr /= ltn_add2l.
rewrite addnC; move/andP => [leq_ai].
rewrite -{1}(@subnK a i) // ltn_add2r => h; exists (Ordinal h) => //.
by rewrite in_fset1 addnC subnK.
Qed.
Lemma eq_mem_nil (T : eqType) (s : seq T) : reflect (s =i [::]) (s == [::]).
Proof.
apply: (iffP idP); first by move/eqP ->.
move=> h; apply/eqP/nilP; rewrite /nilp -all_pred0.
by apply/allP => /= x; rewrite h.
Qed.
Lemma eq_mem_sym (T : Type) (p1 p2 :mem_pred T) : p1 =i p2 -> p2 =i p1.
Proof. by move=> h x; rewrite h. Qed.
Lemma eq_iotar (a c b d : nat) : iota a b =i iota c d -> b = d.
Proof.
move=> eq_ab_cd; rewrite -(size_iota a b) -(size_iota c d).
by apply/eqP; rewrite -uniq_size_uniq ?iota_uniq.
Qed.
Lemma eq_iotal (b d a c : nat) : b != O -> iota a b =i iota c d -> a = c.
Proof.
case: b => // b _; case: d => [/eq_mem_nil//|d eq_ab_cd].
wlog suff hwlog : b d a c eq_ab_cd / (a <= c)%N.
by apply/eqP; rewrite eqn_leq (hwlog b d) ?(hwlog d b).
have := eq_ab_cd c; rewrite !in_cons eqxx /= mem_iota.
by case: ltngtP => [| /ltnW leq_ac|->].
Qed.
Arguments eq_iotal {_} _ {_ _} _ _.
Lemma eq_mnfsetr (a c b d : nat) : mnfset a b = mnfset c d -> b = d.
Proof.
move=> eq_ab_cd; apply: (@eq_iotar a c) => i.
by have /fsetP /(_ i) := eq_ab_cd; rewrite !seq_fsetE.
Qed.
Lemma eq_mnfsetl (b d a c: nat) : b != O -> mnfset a b = mnfset c d -> a = c.
Proof.
move=> b_neq0 eq_ab_cd; apply: (@eq_iotal b d) => // i.
by have /fsetP /(_ i) := eq_ab_cd; rewrite !seq_fsetE.
Qed.
Lemma mnfset_sub (a b c d : nat) :
b != O -> (mnfset a b `<=` mnfset c d) = ((c <= a) && (a + b <= c + d))%N.
Proof.
case: b => // b _; case: d.
- rewrite addn0; apply/idP/idP.
+ by move/fsubsetP/(_ a); rewrite !seq_fsetE in_fset0 inE eqxx; move/implyP.
+ move=> /andP [leq_ca leq__c].
by move: (leq_trans leq__c leq_ca); rewrite leqNgt addnS ltnS /= leq_addr.
- move=> d; apply/fsubsetP/idP; last first.
+ move/andP=> [leq_ca leq_bd] x; rewrite !mnfsetE; move/andP => [leq_ax lt_xb].
by rewrite (leq_trans leq_ca) // (leq_trans lt_xb).
+ move=> h.
apply/andP; split;
[move/(_ a) : h | move/(_ (a + b)%N) : h]; rewrite !mnfsetE.
- rewrite leqnn addnS ltnS leq_addr; move/implyP.
by rewrite implyTb => /andP [].
- rewrite /= addnS ltnS leq_addr leqnn.
by move/implyP; rewrite andbT => /andP [].
Qed.
Lemma m0fset (m : nat) : mnfset m 0 = fset0.
Proof. by apply/fsetP=> i; rewrite seq_fsetE !inE. Qed.
Lemma mnfset_eq (a b c d : nat) :
b != O -> (mnfset a b == mnfset c d) = ((a == c) && (b == d)).
Proof.
move: b d => [|b] [|d] // _.
by rewrite andbF; apply/eqP=>/fsetP/(_ a); rewrite !seq_fsetE !inE eqxx.
rewrite eqEfsubset !mnfset_sub // andbACA -!eqn_leq eq_sym.
by have [->|//] := altP (a =P c); rewrite eqn_add2l.
Qed.
Lemma seq_fset_nil (K : choiceType) : seq_fset [::] = (@fset0 K).
Proof. by apply/eqP; rewrite -cardfs_eq0 cardfsE. Qed.
Lemma seq_fset_cat (K : choiceType) (s1 s2 : seq K) :
seq_fset (s1 ++ s2) = (seq_fset s1) `|` (seq_fset s2).
Proof.
elim: s1 s2 => [s1|a s1 ih s2]; first by rewrite seq_fset_nil fset0U.
by rewrite cat_cons fset_cons ih fset_cons fsetUA.
Qed.
Lemma formula_fv_nforall (n k : nat) (f : formula F) :
(formula_fv (nquantify n k Forall f)) = (formula_fv f) `\` (mnfset n k).
Proof.
elim: k => [|k h] in f *.
by rewrite nquantify0 seq_fset_nil fsetD0.
by rewrite nquantSin h fsetDDl fsetUC -addn1 iota_add seq_fset_cat.
Qed.
Lemma formula_fv_nexists (n k : nat) (f : formula F) :
(formula_fv (nquantify n k Exists f)) = (formula_fv f) `\` (mnfset n k).
Proof.
elim: k => [|k h] in f *.
by rewrite nquantify0 seq_fset_nil fsetD0.
by rewrite nquantSin h fsetDDl fsetUC -addn1 iota_add seq_fset_cat.
Qed.
Lemma formula_fv_bigAnd (I : Type) (r : seq I) (P : pred I)
(E : I -> formula F) :
formula_fv (\big[And/True%oT]_(i <- r | P i) (E i)) =
\bigcup_(i <- r | P i) (formula_fv (E i)).
Proof. exact: big_morph. Qed.
Lemma formula_fv_bigOr (I : Type) (r : seq I) (P : pred I) (E : I -> formula F) :
formula_fv (\big[Or/False%oT]_(i <- r | P i) (E i)) =
\bigcup_(i <- r | P i) (formula_fv (E i)).
Proof. exact: big_morph. Qed.
Lemma formula_fv_bigU (a : nat) (E : 'I_a -> formula F) :
formula_fv (\big[And/True%oT]_(i < a) (E i)) =
\bigcup_(i in 'I_a) (formula_fv (E i)).
Proof. exact: big_morph. Qed.
Definition is_independent (i : nat) (f : formula F) :=
forall (e : seq F), holds e ('forall 'X_i, f) <-> holds e f.
Lemma independent (f : formula F) (i : nat) :
i \notin (formula_fv f) -> is_independent i f.
Proof. by rewrite /is_independent; case: f => *; apply: holds_Nfv_all. Qed.
Section Var_n.
Variable n : nat.
(* We define a relation in formulae *)
Definition equivf (f g : formula F) :=
rcf_sat [::] (nquantify O n Forall ((f ==> g) /\ (g ==> f))).
Lemma equivf_refl : reflexive equivf.
Proof. by move=> f; apply/rcf_satP; apply: nforall_is_true => e /=. Qed.
Lemma equivf_sym : symmetric equivf.
Proof.
move=> f g; rewrite /equivf; move: [::] => e.
move: O => i; move: (f ==> g)%oT (g ==> f)%oT => f' g' {f} {g}.
move: i e; elim: n=> [i e | n' iHn' i e].
by rewrite !nquantify0; apply/rcf_satP/rcf_satP => [[fg gf] | [gf fg]]; split.
rewrite !nquantSout /=.
apply/rcf_satP/rcf_satP => /= [h x | h x];
move/(_ x)/rcf_satP : h => h; apply/rcf_satP.
+ by rewrite -iHn'.
+ by rewrite iHn'.
Qed.
Lemma equivf_trans : transitive equivf.
Proof.
move=> f g h; rewrite /equivf; move: [::] => e.
move: e O; elim: n => [|m ih] e i.
+ rewrite !nquantify0; move/rcf_satP => [h1 h2]; move/rcf_satP => [h3 h4].
apply/rcf_satP; split => [Hg | Hh].
- by apply: h3; apply: h1.
- by apply: h2; apply: h4.
+ rewrite !nquantSout.
move/rcf_satP => fg; move/rcf_satP => hf; apply/rcf_satP => x.
apply/rcf_satP; apply: ih; apply/rcf_satP.
- exact: fg.
- exact: hf.
Qed.
(* we show that equivf is an equivalence *)
Canonical equivf_equiv := EquivRel equivf equivf_refl equivf_sym equivf_trans.
(* equiv_formula *)
Definition sub_equivf :=
(@sub_r _ _ [subType of {formula_n _}] equivf_equiv).
Definition SAtype := {eq_quot sub_equivf}.
Definition SAtype_of of phant (F ^ n) := SAtype.
Identity Coercion id_type_of : SAtype_of >-> SAtype.
Local Notation "{ 'SAset' }" := (SAtype_of (Phant (F ^ n))).
Coercion SAtype_to_form (A : SAtype) : {formula_n _} := repr A.
(* we recover some structure for the quotient *)
Canonical SAset_quotType := [quotType of SAtype].
Canonical SAset_eqType := [eqType of SAtype].
Canonical SAset_choiceType := [choiceType of SAtype].
Canonical SAset_eqQuotType := [eqQuotType sub_equivf of SAtype].
Canonical SAset_of_quotType := [quotType of {SAset}].
Canonical SAset_of_eqType := [eqType of {SAset}].
Canonical SAset_of_choiceType := [choiceType of {SAset}].
Canonical SAset_of_eqQuotType := [eqQuotType sub_equivf of {SAset}].
Lemma fsubset_formulan_fv (f : {formula_n F}) : formula_fv f `<=` mnfset O n.
Proof. by move: f => [f hf]. Qed.
End Var_n.
End RealClosedFieldFormula.
Notation "{ 'SAset' F }" := (SAtype_of (Phant F)) : type_scope.
Section SemiAlgebraicSet.
Variable F : rcfType. (* is also a realDomainType *)
Lemma formula_fv0 (f : {formula_0 F}) : formula_fv f = fset0.
Proof.
by apply/eqP; move: (fsubset_formulan_fv f); rewrite -fsubset0 seq_fset_nil.
Qed.
Lemma in_fv_formulan (n : nat) (f : {formula_n F}) (i : nat) :
i \in formula_fv f -> (i < n)%N.
Proof.
by rewrite -nfsetE; move/fsubsetP => -> //; rewrite fsubset_formulan_fv.
Qed.
Lemma nvar_formulan (n : nat) (f : {formula_n F}) : nvar n f.
Proof. by move: f => [f hf]. Qed.
Section Interpretation.
Lemma set_nth_rcons (i : nat) (e : seq F) (x y : F) :
(i < size e)%N -> set_nth 0 (rcons e y) i x = rcons (set_nth 0 e i x) y.
Proof.
move: i x y; elim: e => //.
move=> a e ihe i; elim: i => //.
move=> i ihi x y /=.
by rewrite ltnS => lt_ie; rewrite ihe.
Qed.
Fact fv_nforall (m n i : nat) (f : formula F) :
(m <= i < m+n)%N -> i \notin formula_fv (nquantify m n Forall f).
Proof.
move=> Hi.
rewrite formula_fv_nforall in_fsetD negb_and negbK mnfsetE.
by apply/orP; left.
Qed.
Fact fv_nexists (m n i : nat) (f : formula F) :
(m <= i < m+n)%N -> i \notin formula_fv (nquantify m n Exists f).
Proof.
move=> Hi.
rewrite formula_fv_nexists in_fsetD negb_and negbK mnfsetE.
by apply/orP; left.
Qed.
Variable n : nat.
Definition ngraph (x : 'rV[F]_n) := [tuple x ord0 i | i < n].
Definition interp := fun (f : {formula_n F}) =>
[pred v : 'rV[F]_n | rcf_sat (ngraph v) f].
Definition pred_of_SAset (s : {SAset F^n}) :
pred ('rV[F]_n) := interp (repr s).
Canonical Structure SAsetPredType := Eval hnf in mkPredType pred_of_SAset.
End Interpretation.
End SemiAlgebraicSet.
Section SemiAlgebraicSet2.
Variable F : rcfType.
Lemma cat_ffun_id (n m : nat) (f : 'rV[F]_(n + m)) :
row_mx (\row_(i < n) (f ord0 (lshift _ i)))
(\row_(j < m) (f ord0 (rshift _ j))) = f.
Proof.
apply/rowP => i; rewrite mxE.
case: splitP=> [] j /esym eq_i; rewrite mxE;
by congr (f _); apply/val_inj/eq_i.
Qed.
Section Interpretation2.
Variable n : nat.
(* recover {formulan} structure on formula *)
Lemma and_formulan (f1 f2 : {formula_n F}) : nvar n (f1 /\ f2)%oT.
Proof. by rewrite /nvar fsubUset !fsubset_formulan_fv. Qed.
Canonical Structure formulan_and (f1 f2 : {formula_n F})
:= MkFormulan (and_formulan f1 f2).
Lemma implies_formulan (f1 f2 : {formula_n F}) : nvar n (f1 ==> f2)%oT.
Proof. by rewrite /nvar fsubUset !fsubset_formulan_fv. Qed.
Canonical Structure formulan_implies (f1 f2 : {formula_n F}) :=
MkFormulan (implies_formulan f1 f2).
Lemma bool_formulan (b : bool) : @nvar F n (Bool b).
Proof. by rewrite /nvar fsub0set. Qed.
Canonical Structure formulan_bool (b : bool) := MkFormulan (bool_formulan b).
Lemma or_formulan (f1 f2 : {formula_n F}) : nvar n (f1 \/ f2)%oT.
Proof. by rewrite /nvar fsubUset !fsubset_formulan_fv. Qed.
Canonical Structure formulan_or (f1 f2 : {formula_n F}) :=
MkFormulan (or_formulan f1 f2).
Lemma not_formulan (f : {formula_n F}) : nvar n (~ f)%oT.
Proof. by rewrite /nvar fsubset_formulan_fv. Qed.
Canonical Structure formulan_not (f : {formula_n F}) :=
MkFormulan (not_formulan f).
Lemma exists_formulan (i : nat) (f : {formula_n F}) :
nvar n ('exists 'X_i, f)%oT.
Proof.
by rewrite /nvar (fsubset_trans (@fsubD1set _ _ _)) // fsubset_formulan_fv.
Qed.
Canonical Structure formulan_exists (i : nat) (f : {formula_n F})
:= MkFormulan (exists_formulan i f).
Lemma forall_formulan (i : nat) (f : {formula_n F}) : nvar n ('forall 'X_i, f)%oT.
Proof.
by rewrite /nvar (fsubset_trans (@fsubD1set _ _ _)) // fsubset_formulan_fv.
Qed.
Canonical Structure formulan_forall (i : nat) (f : {formula_n F})
:= MkFormulan (forall_formulan i f).
Lemma eq_fsetD (K : choiceType) (A B C : finSet K) :
(A `\` B == C) = fdisjoint C B && ((C `<=` A) && (A `<=` B `|` C)).
Proof. by rewrite eqEfsubset fsubDset fsubsetD andbCA andbA andbC. Qed.
Lemma fset1D1 (K : choiceType) (a' a : K) :
[fset a] `\ a' = if (a' == a) then fset0 else [fset a].
Proof.
apply/fsetP=> b; have [->|] := altP (a' =P a); rewrite ?inE;
by have [//->|]// := altP (b =P a); rewrite ?andbF // eq_sym => ->.
Qed.
Lemma term_fv_tsubst (i : nat) (x : F) (t : GRing.term F) :
term_fv (GRing.tsubst t (i, (x%:T)%oT)) = (term_fv t) `\ i.
Proof.
elim: t => //=; rewrite ?fset0D //;
do ?by move=> t1 h1 t2 h2; rewrite fsetDUl ![in LHS](h1, h2).
move=> j; have [->| /negbTE neq_ij] := eqVneq j i.
by rewrite eqxx //= fsetDv.
by rewrite neq_ij /= mem_fsetD1 // inE eq_sym neq_ij.
Qed.
Lemma formula_fv_fsubst (i : nat) (x : F) (f : formula F) :
formula_fv (fsubst f (i, (x%:T)%oT)) = (formula_fv f) `\ i.
Proof.
elim: f.
+ by move=> b; rewrite fset0D.
+ by move=> t1 t2 /=; rewrite !term_fv_tsubst fsetDUl.
+ by move=> t1 t2 /=; rewrite !term_fv_tsubst fsetDUl.
+ by move=> t1 t2 /=; rewrite !term_fv_tsubst fsetDUl.
+ by move=> t /=; rewrite !term_fv_tsubst.
+ by move=> f1 h1 f2 h2 /=; rewrite fsetDUl h1 h2.
+ by move=> f1 h1 f2 h2 /=; rewrite fsetDUl h1 h2.
+ by move=> f1 h1 f2 h2 /=; rewrite fsetDUl h1 h2.
+ by move=> f hf.
+ move=> j f /= hf; rewrite fun_if hf.
have [->| /negbTE neq_ij] := eqVneq j i.
by rewrite eqxx // fsetDDl //= fsetUid.
by rewrite neq_ij !fsetDDl fsetUC.
+ move=> j f h /=.
rewrite fun_if h.
have [->| /negbTE neq_ij] := eqVneq j i.
by rewrite eqxx // fsetDDl //= fsetUid.
by rewrite neq_ij !fsetDDl fsetUC.
Qed.
Lemma fsubst_formulan (i : nat) (x : F) (f : {formula_n F}) :
nvar n (fsubst f (i, (x%:T)%oT))%oT.
Proof.
rewrite /nvar formula_fv_fsubst.
by rewrite (fsubset_trans (@fsubD1set _ _ _)) // fsubset_formulan_fv.
Qed.
Canonical Structure formulan_fsubst (i : nat) (x : F) (f : {formula_n F}) :=
MkFormulan (fsubst_formulan i x f).
End Interpretation2.
Lemma holds_take (n : nat) (f : {formula_n F}) (e : seq F) :
holds (take n e) f <-> holds e f.
Proof.
move: n f; elim/last_ind : e => // e x iHe n' f.
rewrite -{2}(@rcons_set_nth _ _ 0) take_rcons.
have [lt_en'|leq_n'e] := ltnP (size e) n'.
by rewrite take_oversize ?rcons_set_nth // ltnW.
apply: (iff_trans _ (@holds_fsubst _ _ _ _ _)).
apply: (iff_trans _ (@iHe _ _ )) => /=.
by rewrite fsubst_id // (contra (@in_fv_formulan _ _ _ _)) // -leqNgt .
Qed.
Variable n : nat.
Definition same_row_env (e1 e2 : seq F) :=
\row_(i < n) e1`_(val i) =2 (\row_(i < n) e2`_(val i) : 'rV[F]_n).
Lemma eqn_holds e1 e2 (f : {formula_n F}) :
same_row_env e1 e2 -> holds e1 f -> holds e2 f.
Proof.
rewrite /same_row_env => h; move/holds_take => h'.
apply/holds_take; apply: (eq_holds _ h') => i.
have [lt_in | leq_ni] := ltnP i n; last first.
by rewrite ? nth_default ?size_take // ?(leq_trans (geq_minl _ _)).
rewrite !nth_take //.
by move/(_ ord0 (Ordinal lt_in)) : h; rewrite !mxE.
Qed.
Fact closed_nforall_formulan (f : {formula_n F}) :
formula_fv (nquantify O n Forall f) == fset0.
Proof. by rewrite formula_fv_nforall fsetD_eq0 fsubset_formulan_fv. Qed.
Fact closed_nexists_formulan (f : {formula_n F}) :
formula_fv (nquantify O n Exists f) == fset0.
Proof. by rewrite formula_fv_nexists fsetD_eq0 fsubset_formulan_fv. Qed.
Lemma set_nthP (x : n.-tuple F) (i : 'I_n) (y : F) :
size (set_nth 0 x i y) == n.
Proof. by rewrite size_set_nth size_tuple; apply/eqP/maxn_idPr. Qed.
Canonical set_nth_tuple (x : n.-tuple F) (i : 'I_n) (y : F) :=
Tuple (set_nthP x i y).
Definition ngraph_tnth k (t : k.-tuple F) :
ngraph (\row_(i < k) (nth 0 t i)) = t.
Proof.