-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibFun.v
More file actions
261 lines (201 loc) · 7.91 KB
/
LibFun.v
File metadata and controls
261 lines (201 loc) · 7.91 KB
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
(* This file is extracted from the TLC library.
http://github.com/charguer/tlc
DO NOT EDIT. *)
(** DEPRECATED?
\o notation for composition is used in LibFixDemos
*)
(**************************************************************************
* TLC: A library for Coq *
* Functions *
**************************************************************************)
Set Implicit Arguments.
From SLF Require Import LibTactics LibLogic LibContainer LibSet.
(* This will be Import-ed only in the relevant sections *)
From SLF Require LibList.
Generalizable Variables A.
(* ********************************************************************** *)
(* ================================================================= *)
(** ** Indentity function *)
Definition id {A} (x : A) :=
x.
(* **********************************************************************
Constant functions *)
Definition const {A B} (v : B) : A -> B :=
fun _ => v.
Definition const1 :=
@const.
Definition const2 {A1 A2 B} (v:B) : A1->A2->B :=
fun _ _ => v.
Definition const3 {A1 A2 A3 B} (v:B) : A1->A2->A3->B :=
fun _ _ _ => v.
Definition const4 {A1 A2 A3 A4 B} (v:B) : A1->A2->A3->A4->B :=
fun _ _ _ _ => v.
Definition const5 {A1 A2 A3 A4 A5 B} (v:B) : A1->A2->A3->A4->A5->B :=
fun _ _ _ _ _ => v.
(* **********************************************************************
Function application *)
Definition apply {A B} (f : A -> B) (x : A) :=
f x.
Definition apply_to (A : Type) (x : A) (B : Type) (f : A -> B) :=
f x.
(* **********************************************************************
Function composition *)
Definition compose {A B C} (g : B -> C) (f : A -> B) :=
fun x => g (f x).
Declare Scope fun_scope.
Notation "f1 \o f2" := (compose f1 f2)
(at level 49, right associativity) : fun_scope.
Section Combinators.
Open Scope fun_scope.
Variables (A B C D : Type).
Lemma compose_id_l : forall (f:A->B),
id \o f = f.
Proof using. intros. apply~ fun_ext_1. Qed.
Lemma compose_id_r : forall (f:A->B),
f \o id = f.
Proof using. intros. apply~ fun_ext_1. Qed.
Lemma compose_assoc : forall (f:C->D) (g:B->C) (h:A->B),
(f \o g) \o h = f \o (g \o h).
Proof using. intros. apply~ fun_ext_1. Qed.
Lemma compose_eq_l : forall (f:B->C) (g1 g2:A->B),
g1 = g2 ->
f \o g1 = f \o g2.
Proof using. intros. subst~. Qed.
Lemma compose_eq_r : forall (f:A->B) (g1 g2:B->C),
g1 = g2 ->
g1 \o f = g2 \o f.
Proof using. intros. subst~. Qed.
(** Composition of [LibList.map] behaves well. **)
(* Could not be put in [LibList] because of circular dependencies. *)
Import LibList.
Lemma list_map_compose : forall A B C (f : A -> B) (g : B -> C) l,
LibList.map g (LibList.map f l) = LibList.map (g \o f) l.
Proof using.
introv. induction l.
reflexivity.
rew_listx. fequals~.
Qed.
End Combinators.
(** Tactic for simplifying function compositions *)
(* --TODO: not used; might become deprecated *)
Hint Rewrite compose_id_l compose_id_r compose_assoc : rew_compose.
Tactic Notation "rew_compose" :=
autorewrite with rew_compose.
Tactic Notation "rew_compose" "in" "*" :=
autorewrite with rew_compose in *.
Tactic Notation "rew_compose" "in" hyp(H) :=
autorewrite with rew_compose in H.
(* ********************************************************************** *)
(* ================================================================= *)
(** ** Function update *)
(** [fupdate f a b x] is like [f] except that it returns [b] for input [a] *)
Definition fupdate A B (f : A -> B) (a : A) (b : B) : A -> B :=
fun x => If (x = a) then b else f x.
Lemma fupdate_eq : forall A B (f:A->B) a b x,
fupdate f a b x = If (x = a) then b else f x.
Proof using. auto. Qed.
Lemma fupdate_same : forall A B (f:A->B) a b,
fupdate f a b a = b.
Proof using. intros. unfold fupdate. case_if*. Qed.
Lemma fupdate_neq : forall A B (f:A->B) a b x,
x <> a ->
fupdate f a b x = f x.
Proof using. intros. unfold fupdate. case_if*. Qed.
(* Opaque fupdate. -- could be added in the future *)
(* ********************************************************************** *)
(* ================================================================= *)
(** ** Function image *)
Section FunctionImage.
Open Scope set_scope.
Import LibList.
Definition image A B (f : A -> B) (E : set A) : set B :=
\set{ y | exists_ x \in E, y = f x }.
Lemma in_image_prove_eq : forall A B x (f : A -> B) (E : set A),
x \in E -> f x \in image f E.
Proof using. introv N. unfold image. rew_set. exists* x. Qed.
Lemma in_image_prove : forall A B x y (f : A -> B) (E : set A),
x \in E -> y = f x -> y \in image f E.
Proof using. intros. subst. applys* in_image_prove_eq. Qed.
Lemma in_image_inv : forall A B y (f : A -> B) (E : set A),
y \in image f E -> exists x, x \in E /\ y = f x.
Proof using. introv N. unfolds image. rew_set in N. auto. Qed.
Lemma finite_image : forall A B (f : A -> B) (E : set A),
finite E ->
finite (image f E).
Proof using.
introv M. lets (L&H): finite_inv_list_covers M.
applys finite_of_list_covers (LibList.map f L). introv N.
lets (y&Hy&Ey): in_image_inv (rm N). subst x. applys* mem_map.
Qed.
Lemma image_covariant : forall A B (f : A -> B) (E F : set A),
E \c F ->
image f E \c image f F.
Proof using.
introv. do 2 rewrite incl_in_eq. introv M N.
lets (y&Hy&Ey): in_image_inv (rm N). applys* in_image_prove.
Qed.
Lemma image_union : forall A B (f : A -> B) (E F : set A),
image f (E \u F) = image f E \u image f F.
Proof using.
Hint Resolve in_image_prove.
introv. apply in_extens. intros x. iff N.
lets (y&Hy&Ey): in_image_inv (rm N). rewrite in_union_eq in Hy.
rewrite in_union_eq. destruct* Hy.
rewrite in_union_eq in N. destruct N as [N|N].
lets (y&Hy&Ey): in_image_inv (rm N). applys* in_image_prove.
rewrite in_union_eq. eauto.
lets (y&Hy&Ey): in_image_inv (rm N). applys* in_image_prove.
rewrite in_union_eq. eauto.
Qed.
Lemma image_singleton : forall A B (f : A -> B) (x : A),
image f \{x} = \{f x}.
Proof using.
intros. apply in_extens. intros z. rewrite in_single_eq. iff N.
lets (y&Hy&Ey): in_image_inv (rm N). rewrite in_single_eq in Hy. subst~.
applys* in_image_prove. rewrite~ @in_single_eq. typeclass.
Qed.
End FunctionImage.
Hint Resolve finite_image : finite.
(* ********************************************************************** *)
(* ================================================================= *)
(** ** Function preimage *)
Section FunctionPreimage.
Open Scope set_scope.
Definition preimage A B (f : A -> B) (E : set B) : set A :=
\set{ x | exists_ y \in E, y = f x }.
End FunctionPreimage.
(* ********************************************************************** *)
(* ================================================================= *)
(** ** Function iteration *)
Fixpoint applyn A n (f : A -> A) x :=
match n with
| O => x
| S n' => f (applyn n' f x)
end.
Lemma applyn_fix : forall A n f (x : A),
applyn (S n) f x = applyn n f (f x).
Proof using. introv. induction~ n. simpls. rewrite~ IHn. Qed.
Lemma applyn_comp : forall A n m f (x : A),
applyn n f (applyn m f x) = applyn (n + m) f x.
Proof using.
introv. gen m; induction n; introv; simpls~.
rewrite~ IHn.
Qed.
Lemma applyn_nested : forall A n m f (x : A),
applyn n (applyn m f) x = applyn (n * m) f x.
Proof using.
introv. gen m. induction n; introv; simpls~.
rewrite IHn. rewrite~ applyn_comp.
Qed.
Lemma applyn_altern : forall A B (f : A -> B) (g : B -> A) x n,
applyn n (fun x => f (g x)) (f x) =
f (applyn n (fun x => g (f x)) x).
Proof using. introv. gen x. induction~ n. introv. repeat rewrite applyn_fix. autos~. Qed.
Lemma applyn_ind : forall A (P : A -> Prop) (f : A -> A) x n,
(forall x, P x -> P (f x)) ->
P x ->
P (applyn n f x).
Proof using. introv I. induction n; introv Hx; autos*. Qed.
(* --TODO: rename applyn to iter *)
(* --TODO: migrate iteration of functionals from LibFix to here *)
(* 2021-01-25 13:22 *)