]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/library/datatypes/categories.ma
Setoids are now more pervasive.
[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 record equivalence_relation (A:Type) : Type ≝
18  { eq_rel:2> A → A → CProp;
19    refl: reflexive ? eq_rel;
20    sym: symmetric ? eq_rel;
21    trans: transitive ? eq_rel
22  }.
23
24 record setoid : Type ≝
25  { carr:> Type;
26    eq: equivalence_relation carr
27  }.
28
29 definition reflexive1 ≝ λA:Type.λR:A→A→CProp.∀x:A.R x x.
30 definition symmetric1 ≝ λC:Type.λlt:C→C→CProp. ∀x,y:C.lt x y → lt y x.
31 definition transitive1 ≝ λA:Type.λR:A→A→CProp.∀x,y,z:A.R x y → R y z → R x z.
32
33 record equivalence_relation1 (A:Type) : Type ≝
34  { eq_rel1:2> A → A → CProp;
35    refl1: reflexive1 ? eq_rel1;
36    sym1: symmetric1 ? eq_rel1;
37    trans1: transitive1 ? eq_rel1
38  }.
39
40 record setoid1: Type ≝
41  { carr1:> Type;
42    eq1: equivalence_relation1 carr1
43  }.
44
45 definition setoid1_of_setoid: setoid → setoid1.
46  intro;
47  constructor 1;
48   [ apply (carr s)
49   | constructor 1;
50     [ apply (eq_rel s);
51       apply (eq s)
52     | apply (refl s)
53     | apply (sym s)
54     | apply (trans s)]]
55 qed.
56
57 coercion setoid1_of_setoid.
58
59 (*
60 definition Leibniz: Type → setoid.
61  intro;
62  constructor 1;
63   [ apply T
64   | constructor 1;
65      [ apply (λx,y:T.cic:/matita/logic/equality/eq.ind#xpointer(1/1) ? x y)
66      | alias id "refl_eq" = "cic:/matita/logic/equality/eq.ind#xpointer(1/1/1)".
67        apply refl_eq
68      | alias id "sym_eq" = "cic:/matita/logic/equality/sym_eq.con".
69        apply sym_eq
70      | alias id "trans_eq" = "cic:/matita/logic/equality/trans_eq.con".
71        apply trans_eq ]]
72 qed.
73
74 coercion Leibniz.
75 *)
76
77 interpretation "setoid1 eq" 'eq x y = (eq_rel1 _ (eq1 _) x y).
78 interpretation "setoid eq" 'eq x y = (eq_rel _ (eq _) x y).
79 interpretation "setoid1 symmetry" 'invert r = (sym1 ____ r).
80 interpretation "setoid symmetry" 'invert r = (sym ____ r).
81 notation ".= r" with precedence 50 for @{'trans $r}.
82 interpretation "trans1" 'trans r = (trans1 _____ r).
83 interpretation "trans" 'trans r = (trans _____ r).
84
85 record unary_morphism (A,B: setoid1) : Type ≝
86  { fun_1:1> A → B;
87    prop_1: ∀a,a'. eq1 ? a a' → eq1 ? (fun_1 a) (fun_1 a')
88  }.
89
90 record binary_morphism (A,B,C:setoid) : Type ≝
91  { fun:2> A → B → C;
92    prop: ∀a,a',b,b'. eq ? a a' → eq ? b b' → eq ? (fun a b) (fun a' b')
93  }.
94
95 record binary_morphism1 (A,B,C:setoid1) : Type ≝
96  { fun1:2> A → B → C;
97    prop1: ∀a,a',b,b'. eq1 ? a a' → eq1 ? b b' → eq1 ? (fun1 a b) (fun1 a' b')
98  }.
99
100 notation "hbox(a break ⇒ b)" right associative with precedence 20 for @{ 'Imply $a $b }.
101 interpretation "unary morphism" 'Imply a b = (unary_morphism a b).
102
103 notation "† c" with precedence 90 for @{'prop1 $c }.
104 notation "l ‡ r" with precedence 90 for @{'prop $l $r }.
105 notation "#" with precedence 90 for @{'refl}.
106 interpretation "prop_1" 'prop1 c = (prop_1 _____ c).
107 interpretation "prop1" 'prop l r = (prop1 ________ l r).
108 interpretation "prop" 'prop l r = (prop ________ l r).
109 interpretation "refl1" 'refl = (refl1 ___).
110 interpretation "refl" 'refl = (refl ___).
111
112 definition CPROP: setoid1.
113  constructor 1;
114   [ apply CProp
115   | constructor 1;
116      [ apply Iff
117      | intros 1; split; intro; assumption
118      | intros 3; cases H; split; assumption
119      | intros 5; cases H; cases H1; split; intro;
120         [ apply (H4 (H2 H6)) | apply (H3 (H5 H6))]]]
121 qed.
122
123 definition if': ∀A,B:CPROP. A = B → A → B.
124  intros; apply (if ?? H); assumption.
125 qed.
126
127 notation ". r" with precedence 50 for @{'if $r}.
128 interpretation "if" 'if r = (if' __ r).
129
130 definition and_morphism: binary_morphism1 CPROP CPROP CPROP.
131  constructor 1;
132   [ apply And
133   | intros; split; intro; cases H2; split;
134      [ apply (if ?? H a1)
135      | apply (if ?? H1 b1)
136      | apply (fi ?? H a1)
137      | apply (fi ?? H1 b1)]]
138 qed.
139
140 interpretation "and_morphism" 'and a b = (fun1 ___ and_morphism a b).
141
142 definition or_morphism: binary_morphism1 CPROP CPROP CPROP.
143  constructor 1;
144   [ apply Or
145   | intros; split; intro; cases H2; [1,3:left |2,4: right]
146      [ apply (if ?? H a1)
147      | apply (fi ?? H a1)
148      | apply (if ?? H1 b1)
149      | apply (fi ?? H1 b1)]]
150 qed.
151
152 interpretation "or_morphism" 'or a b = (fun1 ___ or_morphism a b).
153
154 definition if_morphism: binary_morphism1 CPROP CPROP CPROP.
155  constructor 1;
156   [ apply (λA,B. A → B)
157   | intros; split; intros;
158      [ apply (if ?? H1); apply H2; apply (fi ?? H); assumption
159      | apply (fi ?? H1); apply H2; apply (if ?? H); assumption]]
160 qed.
161
162 (*
163 definition eq_morphism: ∀S:setoid. binary_morphism S S CPROP.
164  intro;
165  constructor 1;
166   [ apply (eq_rel ? (eq S))
167   | intros; split; intro;
168      [ apply (.= H \sup -1);
169        apply (.= H2);
170        assumption
171      | apply (.= H);
172        apply (.= H2);
173        apply (H1 \sup -1)]]
174 qed.
175 *)
176
177 record category : Type ≝
178  { objs:> Type;
179    arrows: objs → objs → setoid;
180    id: ∀o:objs. arrows o o;
181    comp: ∀o1,o2,o3. binary_morphism (arrows o1 o2) (arrows o2 o3) (arrows o1 o3);
182    comp_assoc: ∀o1,o2,o3,o4. ∀a12,a23,a34.
183     comp o1 o3 o4 (comp o1 o2 o3 a12 a23) a34 = comp o1 o2 o4 a12 (comp o2 o3 o4 a23 a34);
184    id_neutral_left: ∀o1,o2. ∀a: arrows o1 o2. comp ??? (id o1) a = a;
185    id_neutral_right: ∀o1,o2. ∀a: arrows o1 o2. comp ??? a (id o2) = a
186  }.
187
188 record category1 : Type ≝
189  { objs1:> Type;
190    arrows1: objs1 → objs1 → setoid1;
191    id1: ∀o:objs1. arrows1 o o;
192    comp1: ∀o1,o2,o3. binary_morphism1 (arrows1 o1 o2) (arrows1 o2 o3) (arrows1 o1 o3);
193    comp_assoc1: ∀o1,o2,o3,o4. ∀a12,a23,a34.
194     comp1 o1 o3 o4 (comp1 o1 o2 o3 a12 a23) a34 = comp1 o1 o2 o4 a12 (comp1 o2 o3 o4 a23 a34);
195    id_neutral_left1: ∀o1,o2. ∀a: arrows1 o1 o2. comp1 ??? (id1 o1) a = a;
196    id_neutral_right1: ∀o1,o2. ∀a: arrows1 o1 o2. comp1 ??? a (id1 o2) = a
197  }.
198
199 notation "'ASSOC'" with precedence 90 for @{'assoc}.
200 notation "'ASSOC1'" with precedence 90 for @{'assoc1}.
201
202 interpretation "category1 composition" 'compose x y = (fun1 ___ (comp1 ____) x y).
203 interpretation "category1 assoc" 'assoc1 = (comp_assoc1 ________).
204 interpretation "category composition" 'compose x y = (fun ___ (comp ____) x y).
205 interpretation "category assoc" 'assoc = (comp_assoc ________).