]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/library/datatypes/categories.ma
Preparing for 0.5.9 release.
[helm.git] / helm / software / matita / library / datatypes / categories.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "logic/cprop_connectives.ma".
16
17 definition Type0 := Type.
18 definition Type1 := Type.
19 definition Type2 := Type.
20 definition Type0_lt_Type1 := (Type0 : Type1).
21 definition Type1_lt_Type2 := (Type1 : Type2).
22
23 record equivalence_relation (A:Type) : Type ≝
24  { eq_rel:2> A → A → CProp;
25    refl: reflexive ? eq_rel;
26    sym: symmetric ? eq_rel;
27    trans: transitive ? eq_rel
28  }.
29
30 record setoid : Type1 ≝
31  { carr:> Type;
32    eq: equivalence_relation carr
33  }.
34
35 definition reflexive1 ≝ λA:Type.λR:A→A→CProp.∀x:A.R x x.
36 definition symmetric1 ≝ λC:Type.λlt:C→C→CProp. ∀x,y:C.lt x y → lt y x.
37 definition transitive1 ≝ λA:Type.λR:A→A→CProp.∀x,y,z:A.R x y → R y z → R x z.
38
39 record equivalence_relation1 (A:Type) : Type2 ≝
40  { eq_rel1:2> A → A → CProp;
41    refl1: reflexive1 ? eq_rel1;
42    sym1: symmetric1 ? eq_rel1;
43    trans1: transitive1 ? eq_rel1
44  }.
45
46 record setoid1: Type ≝
47  { carr1:> Type;
48    eq1: equivalence_relation1 carr1
49  }.
50
51 definition setoid1_of_setoid: setoid → setoid1.
52  intro;
53  constructor 1;
54   [ apply (carr s)
55   | constructor 1;
56     [ apply (eq_rel s);
57       apply (eq s)
58     | apply (refl s)
59     | apply (sym s)
60     | apply (trans s)]]
61 qed.
62
63 coercion setoid1_of_setoid.
64
65 (*
66 definition Leibniz: Type → setoid.
67  intro;
68  constructor 1;
69   [ apply T
70   | constructor 1;
71      [ apply (λx,y:T.cic:/matita/logic/equality/eq.ind#xpointer(1/1) ? x y)
72      | alias id "refl_eq" = "cic:/matita/logic/equality/eq.ind#xpointer(1/1/1)".
73        apply refl_eq
74      | alias id "sym_eq" = "cic:/matita/logic/equality/sym_eq.con".
75        apply sym_eq
76      | alias id "trans_eq" = "cic:/matita/logic/equality/trans_eq.con".
77        apply trans_eq ]]
78 qed.
79
80 coercion Leibniz.
81 *)
82
83 interpretation "setoid1 eq" 'eq t x y = (eq_rel1 ? (eq1 t) x y).
84 interpretation "setoid eq" 'eq t x y = (eq_rel ? (eq t) x y).
85 interpretation "setoid1 symmetry" 'invert r = (sym1 ???? r).
86 interpretation "setoid symmetry" 'invert r = (sym ???? r).
87 notation ".= r" with precedence 50 for @{'trans $r}.
88 interpretation "trans1" 'trans r = (trans1 ????? r).
89 interpretation "trans" 'trans r = (trans ????? r).
90
91 record unary_morphism (A,B: setoid1) : Type0 ≝
92  { fun_1:1> A → B;
93    prop_1: ∀a,a'. eq1 ? a a' → eq1 ? (fun_1 a) (fun_1 a')
94  }.
95
96 record binary_morphism (A,B,C:setoid) : Type0 ≝
97  { fun:2> A → B → C;
98    prop: ∀a,a',b,b'. eq ? a a' → eq ? b b' → eq ? (fun a b) (fun a' b')
99  }.
100
101 record binary_morphism1 (A,B,C:setoid1) : Type0 ≝
102  { fun1:2> A → B → C;
103    prop1: ∀a,a',b,b'. eq1 ? a a' → eq1 ? b b' → eq1 ? (fun1 a b) (fun1 a' b')
104  }.
105
106 notation "† c" with precedence 90 for @{'prop1 $c }.
107 notation "l ‡ r" with precedence 90 for @{'prop $l $r }.
108 notation "#" with precedence 90 for @{'refl}.
109 interpretation "prop_1" 'prop1 c = (prop_1 ????? c).
110 interpretation "prop1" 'prop l r = (prop1 ???????? l r).
111 interpretation "prop" 'prop l r = (prop ???????? l r).
112 interpretation "refl1" 'refl = (refl1 ???).
113 interpretation "refl" 'refl = (refl ???).
114
115 definition CPROP: setoid1.
116  constructor 1;
117   [ apply CProp
118   | constructor 1;
119      [ apply Iff
120      | intros 1; split; intro; assumption
121      | intros 3; cases H; split; assumption
122      | intros 5; cases H; cases H1; split; intro;
123         [ apply (H4 (H2 H6)) | apply (H3 (H5 H6))]]]
124 qed.
125
126 definition if': ∀A,B:CPROP. A = B → A → B.
127  intros; apply (if ?? H); assumption.
128 qed.
129
130 notation ". r" with precedence 50 for @{'if $r}.
131 interpretation "if" 'if r = (if' ?? r).
132
133 definition and_morphism: binary_morphism1 CPROP CPROP CPROP.
134  constructor 1;
135   [ apply And
136   | intros; split; intro; cases H2; split;
137      [ apply (if ?? H a1)
138      | apply (if ?? H1 b1)
139      | apply (fi ?? H a1)
140      | apply (fi ?? H1 b1)]]
141 qed.
142
143 interpretation "and_morphism" 'and a b = (fun1 ??? and_morphism a b).
144
145 definition or_morphism: binary_morphism1 CPROP CPROP CPROP.
146  constructor 1;
147   [ apply Or
148   | intros; split; intro; cases H2; [1,3:left |2,4: right]
149      [ apply (if ?? H a1)
150      | apply (fi ?? H a1)
151      | apply (if ?? H1 b1)
152      | apply (fi ?? H1 b1)]]
153 qed.
154
155 interpretation "or_morphism" 'or a b = (fun1 ??? or_morphism a b).
156
157 definition if_morphism: binary_morphism1 CPROP CPROP CPROP.
158  constructor 1;
159   [ apply (λA,B. A → B)
160   | intros; split; intros;
161      [ apply (if ?? H1); apply H2; apply (fi ?? H); assumption
162      | apply (fi ?? H1); apply H2; apply (if ?? H); assumption]]
163 qed.
164
165 (*
166 definition eq_morphism: ∀S:setoid. binary_morphism S S CPROP.
167  intro;
168  constructor 1;
169   [ apply (eq_rel ? (eq S))
170   | intros; split; intro;
171      [ apply (.= H \sup -1);
172        apply (.= H2);
173        assumption
174      | apply (.= H);
175        apply (.= H2);
176        apply (H1 \sup -1)]]
177 qed.
178 *)
179
180 record category : Type1 ≝
181  { objs:> Type;
182    arrows: objs → objs → setoid;
183    id: ∀o:objs. arrows o o;
184    comp: ∀o1,o2,o3. binary_morphism (arrows o1 o2) (arrows o2 o3) (arrows o1 o3);
185    comp_assoc: ∀o1,o2,o3,o4. ∀a12,a23,a34.
186     comp o1 o3 o4 (comp o1 o2 o3 a12 a23) a34 = comp o1 o2 o4 a12 (comp o2 o3 o4 a23 a34);
187    id_neutral_left: ∀o1,o2. ∀a: arrows o1 o2. comp ??? (id o1) a = a;
188    id_neutral_right: ∀o1,o2. ∀a: arrows o1 o2. comp ??? a (id o2) = a
189  }.
190
191 record category1 : Type2 ≝
192  { objs1:> Type;
193    arrows1: objs1 → objs1 → setoid1;
194    id1: ∀o:objs1. arrows1 o o;
195    comp1: ∀o1,o2,o3. binary_morphism1 (arrows1 o1 o2) (arrows1 o2 o3) (arrows1 o1 o3);
196    comp_assoc1: ∀o1,o2,o3,o4. ∀a12,a23,a34.
197     comp1 o1 o3 o4 (comp1 o1 o2 o3 a12 a23) a34 = comp1 o1 o2 o4 a12 (comp1 o2 o3 o4 a23 a34);
198    id_neutral_right1: ∀o1,o2. ∀a: arrows1 o1 o2. comp1 ??? (id1 o1) a = a;
199    id_neutral_left1: ∀o1,o2. ∀a: arrows1 o1 o2. comp1 ??? a (id1 o2) = a
200  }.
201
202 notation "'ASSOC'" with precedence 90 for @{'assoc}.
203 notation "'ASSOC1'" with precedence 90 for @{'assoc1}.
204
205 interpretation "category1 composition" 'compose x y = (fun1 ??? (comp1 ????) y x).
206 interpretation "category1 assoc" 'assoc1 = (comp_assoc1 ????????).
207 interpretation "category composition" 'compose x y = (fun ??? (comp ????) y x).
208 interpretation "category assoc" 'assoc = (comp_assoc ????????).
209
210 definition unary_morphism_setoid: setoid → setoid → setoid.
211  intros;
212  constructor 1;
213   [ apply (unary_morphism s s1);
214   | constructor 1;
215      [ intros (f g); apply (∀a. f a = g a);
216      | intros 1; simplify; intros; apply refl;
217      | simplify; intros; apply sym; apply H;
218      | simplify; intros; apply trans; [2: apply H; | skip | apply H1]]]
219 qed.
220
221 notation "hbox(a break ⇒ b)" right associative with precedence 20 for @{ 'Imply $a $b }.
222 interpretation "unary morphism" 'Imply a b = (unary_morphism_setoid a b).
223 interpretation "unary morphism" 'Imply a b = (unary_morphism a b).
224
225 definition SET: category1.
226  constructor 1;
227   [ apply setoid;
228   | apply rule (λS,T.unary_morphism_setoid S T);
229   | intros; constructor 1; [ apply (λx.x); | intros; assumption ]
230   | intros; constructor 1; [ intros; constructor 1; [ apply (λx. c1 (c x)); | intros;
231      apply († (†H));]
232   | intros; whd; intros; simplify; whd in H1; whd in H;
233     apply trans; [ apply (b (a' a1)); | lapply (prop_1 ?? b (a a1) (a' a1));
234      [ apply Hletin | apply (H a1); ]  | apply H1; ]]
235   | intros; whd; intros; simplify; apply refl;
236   | intros; simplify; whd; intros; simplify; apply refl;
237   | intros; simplify; whd; intros; simplify; apply refl;
238   ]
239 qed.
240
241 definition setoid_OF_SET: objs1 SET → setoid.
242  intros; apply o; qed.
243
244 coercion setoid_OF_SET.
245
246
247 definition prop_1_SET : 
248  ∀A,B:SET.∀w:arrows1 SET A B.∀a,b:A.eq1 ? a b→eq1 ? (w a) (w b).
249 intros; apply (prop_1 A B w a b H);
250 qed.
251
252 interpretation "SET dagger" 'prop1 h = (prop_1_SET ? ? ? ? ? h).