1 Require Export Standard.
3 Section Fin_Definitions.
7 Definition Fin: N -> Set := (NatFam [_]Set Empty [_](LOr One)).
9 (* Constructors: first element, next element *)
10 Definition ffe: (n:N) (Fin (succ n)).
12 MApply '(in_l One (Fin n) tt).
15 Definition fne: (n:N) (Fin n) -> (Fin (succ n)).
17 MApply '(in_r One (Fin n)).
21 Definition finrec: (n:N; P:(Fin (succ n))->Set) (P (ffe n)) -> ((a:(Fin n))(P (fne n a))) -> (p:(Fin (succ n))) (P p).
23 MElim 'p '(when One (Fin n)).
29 Definition fpe: (n:N) (Fin (succ (succ n))) -> (Fin (succ n)).
31 MElim 'H '(finrec (succ n)).
35 (* Injection of (Fin (succ n)) in N *)
36 Definition fn: (n:N) (Fin (succ n)) -> N := (natrec [m](Fin (succ m))->N [_]zero [m;p](finrec (succ m) [_]N zero [a](succ (p a)))).
48 (* Surjection of N in (Fin (succ n)): (ffe n) returned if m >= n *)
49 Definition nf: (n,m:N) (Fin (succ n)) := (natrec [m]N->(Fin (succ m)) [_](ffe zero) [n';p](natrec [_](Fin (succ (succ n'))) (ffe (succ n')) [m';p'](fne (succ n') (p m')))).
51 (* Left injection of (Fin m) -> (Fin (add m n)) *)
52 Definition fil: (m,n:N) (Fin m) -> (Fin (add m n)) :=
53 (natrec [m](n:N) (Fin m) -> (Fin (add m n))
54 [n](efq [_](Fin (add zero n)))
55 [m';_](natrec [n](Fin (succ m')) -> (Fin (add (succ m') n))
56 [a]a [n';_;a](nf (add (succ m') n') (fn m' a)))).
58 (* Right injection of (Fin n) -> (Fin (add m n)) *)
59 Definition fir: (m,n:N) (Fin n) -> (Fin (add m n)) :=
60 [m](natrec [n](Fin n) -> (Fin (add m n))
62 [n';_;a](nf (add m n') (add (fn n' a) m))).
66 Section explicit_finite_domain_functions.
68 Definition xfdf_list: (S:Set; n:N) ((Fin n) -> S) -> (List S) := [S](natrec [m]((Fin m)->S)->(List S) [_](empty S) [m;p;f](fr S (p [k](f (fne m k))) (f (ffe m)))).
70 Definition xfdf_add': (S:Set; m:N) ((Fin (succ m))->S) -> (n:N) ((Fin (succ n))->S) -> (Fin (add_three (succ m) n one)) -> S :=
71 [S;m;v;n;w;a](ite [_]S (w (nf n (sub (fn (add (succ m) n) a) (succ m))))
72 (v (nf m (fn (add (succ m) n) a)))
73 (b_le (fn (add (succ m) n) a) m)).
75 Definition xfdf_add: (S:Set; m:N) ((Fin m)->S) -> (n:N) ((Fin n)->S) -> (Fin (add m n)) -> S :=
76 [S](natrec [m]((Fin m)->S) -> (n:N) ((Fin n)->S) -> (Fin (add m n)) -> S
77 [_](natrec [n]((Fin n)->S) -> (Fin (add zero n)) -> S
78 [w]w [n';_;w;a](w (nf n' (fn (add zero n') a))))
79 [m';_;v](natrec [n]((Fin n)->S) -> (Fin (add (succ m') n)) -> S
80 [_]v [n';_](xfdf_add' S m' v n'))).
82 End explicit_finite_domain_functions.
84 Section finite_domain_functions.
86 Definition FDF: Set -> Set := [S](Sigma N [n](Fin n)->S).
88 Definition fdf_i: (S:Set; n:N) ((Fin n)->S) -> (FDF S) := [S](pair N [n](Fin n)->S).
90 Definition fdf_e: (S:Set; P:(FDF S)->Set) ((n:N; f:(Fin n)->S) (P (fdf_i S n f))) -> (v:(FDF S))(P v) :=
91 [S:Set](psplit N [n](Fin n)->S).
93 Definition fdf_n: (S:Set) (FDF S) -> N := [S](fdf_e S [_]N [a;_]a).
95 Definition fdf_f: (S:Set; v:(FDF S)) (Fin (fdf_n S v)) -> S := [S](fdf_e S [p](Fin (fdf_n S p))->S [_;p]p).
97 Definition fdf_empty: (S:Set) (FDF S) := [S](fdf_i S zero (efq [_]S)).
99 Definition fdf_fr: (S:Set) (FDF S) -> S -> (FDF S) := [S;v;a](fdf_i S (succ (fdf_n S v)) (when One (Fin (fdf_n S v)) [_]S [_]a (fdf_f S v))).
101 Definition lfdf: (S:Set) (List S) -> (FDF S) := [S](listrec S [_](FDF S) (fdf_empty S) [_](fdf_fr S)).
103 Definition fdfl: (S:Set) (FDF S) -> (List S) := [S](fdf_e S [_](List S) (xfdf_list S)).
105 Definition fdf_add: (S:Set) (FDF S) -> (FDF S) -> (FDF S) := [S;v;w](fdf_i S (add (fdf_n S v) (fdf_n S w)) (xfdf_add S (fdf_n S v) (fdf_f S v) (fdf_n S w) (fdf_f S w))).
107 End finite_domain_functions.
115 (* Immediate result used for reference
116 Theorem fpe_fne: (n:N; a:(Fin (succ n))) (Id (Fin (succ n)) (fpe n (fne (succ n) a)) a).
118 Theorem fne_ini: (n:N; a,b:(Fin n)) (Id (Fin (succ n)) (fne n a) (fne n b)) -> (Id (Fin n) a b).
124 MApply '(id_repl (Fin (succ n0)) a (fpe n0 (fne (succ n0) a))).
125 MApply '(id_repl (Fin (succ n0)) b (fpe n0 (fne (succ n0) b))).
126 MApply '(id_comp (Fin (succ (succ n0))) (Fin (succ n0)) (fpe n0)).
129 Theorem fn_le: (n:N; a:(Fin (succ n))) (LE (fn n a) n).
131 Elim n using natrec; Intros.
133 MElim 'a '(finrec (succ n0)).
135 MApply 'le_comp_succ.
139 Theorem nfn': (n:N; a:(Fin (succ n)); m:N) (LE m n) -> (Id (Fin (succ n)) (nf n m) a) -> (Id N (fn n a) m).
146 Elim a using (finrec (succ n')).
150 MApply '(lor_p4 One (Fin (succ n')) tt (nf n' n0)).
157 MApply '(lor_p4 One (Fin (succ n')) tt a').
162 Change (Id N (fn n' a') n0).
165 Change (Id (Fin (succ (succ n'))) (fne (succ n') (nf n' n0)) (fne (succ n') a')) in H2.
169 Theorem nfn: (n:N; m:N) (LE m n) -> (Id N (fn n (nf n m)) m).
174 Theorem fnf_fne: (n:N; a:(Fin n)) (Id (Fin (succ n)) (nf n (fn n (fne n a))) (fne n a)).
176 Elim n using natrec; Intros.
178 Elim a using (finrec n0); Intros.
181 MApply '(id_comp (Fin (succ n0))).
185 Theorem fnf: (n:N; a:(Fin (succ n))) (Id (Fin (succ n)) (nf n (fn n a)) a).
187 Elim a using (finrec n); Intros.
194 Section xfdf_results.
196 Theorem xfdfl_lin: (S:Set; n:N; a:(Fin n); v:(Fin n)->S) (LIn S (v a) (xfdf_list S n v)).
198 Elim n using natrec; Intros.
202 MElim 'a '(finrec n0).
205 MApply '(H a0 [k:(Fin n0)](v (fne n0 k))).
208 Theorem xfdfa_fil: (S:Set; m:N; a:(Fin m); v:(Fin m)->S; n:N; w:(Fin n)->S) (Id S (xfdf_add S m v n w (fil m n a)) (v a)).
219 MCut '(LE (fn m' a) (add (succ m') n')).
220 MApply '(le_trans m').
222 MApply '(id_repl N (add (succ m') n') (add_three m' n' one)).
224 MApply '(add_succ m' n').
225 MApply '(le_add m' (succ n')).
227 MApply '(id_repl Boole (b_le (fn (add (succ m') n') (nf (add (succ m') n') (fn m' a))) m') true).
229 Fold (LE (fn (add (succ m') n') (nf (add (succ m') n') (fn m' a))) m').
230 MApply '(id_repl N (fn (add (succ m') n') (nf (add (succ m') n') (fn m' a))) (fn m' a)).
235 MApply '(id_comp (Fin (succ m'))).
236 MApply '(id_repl N (fn (add (succ m') n') (nf (add (succ m') n') (fn m' a))) (fn m' a)).
242 Theorem xfdfa_fir: (S:Set; m:N; v:(Fin m)->S; n:N; a:(Fin n); w:(Fin n)->S) (Id S (xfdf_add S m v n w (fir m n a)) (w a)).
251 MApply '(id_comp (Fin (succ n'))).
252 MApply '(id_repl N (fn (add zero n') (nf (add zero n') (fn n' a))) (fn n' a)).
255 MApply '(le_trans n').
257 MApply '(id_repl N (add zero n') n').
258 MApply '(add_comm n' zero).
266 (MSimpl; Apply succ_fold). (**)
267 MCut '(LE (succ (add (fn n' a) m')) (add (succ m') n')).
268 MApply '(id_repl N (add (succ m') n') (succ (add m' n'))).
271 MApply 'le_comp_succ.
272 MApply '(id_repl N (add m' n') (add n' m')).
277 MApply '(id_repl Boole (b_le (fn (add (succ m') n') (nf (add (succ m') n') (succ (add (fn n' a) m')))) m') false).
279 Fold (GT (fn (add (succ m') n') (nf (add (succ m') n') (succ (add (fn n' a) m')))) m').
281 MApply '(id_repl N (fn (add (succ m') n') (nf (add (succ m') n') (succ (add (fn n' a) m')))) (succ (add (fn n' a) m'))).
284 MApply 'le_comp_succ.
285 MApply '(id_repl N (add (fn n' a) m') (add m' (fn n' a))).
290 MApply '(id_comp (Fin (succ n'))).
291 MApply '(id_repl N (sub (pred (fn (add (succ m') n') (nf (add (succ m') n') (succ (add (fn n' a) m'))))) m') (fn n' a)).
295 MApply '(id_repl N (add m' (fn n' a)) (add (fn n' a) m')).