Skip to content

Commit 4f57399

Browse files
committed
complete revision of schema
1 parent d26def5 commit 4f57399

3 files changed

Lines changed: 130 additions & 183 deletions

File tree

databases/catdat/schema/001_structures.sql

Lines changed: 115 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,50 @@ INSERT INTO structure_types (type) VALUES ('category'), ('functor');
99
-- STRUCTURES
1010

1111
CREATE TABLE structures (
12-
id TEXT PRIMARY KEY,
12+
name TEXT NOT NULL,
1313
type TEXT NOT NULL,
14-
name TEXT NOT NULL UNIQUE,
14+
long_name TEXT NOT NULL,
1515
notation TEXT NOT NULL,
1616
description TEXT,
1717
nlab_link TEXT CHECK (nlab_link IS NULL OR nlab_link like 'https://%'),
18-
FOREIGN KEY (type) REFERENCES structure_types (type) ON DELETE RESTRICT,
19-
UNIQUE (id, type)
18+
PRIMARY KEY (name, type),
19+
UNIQUE (long_name, type),
20+
FOREIGN KEY (type) REFERENCES structure_types (type) ON DELETE RESTRICT
2021
);
2122

22-
CREATE UNIQUE INDEX idx_structures_lower_id_unique ON structures (lower(id));
23+
CREATE UNIQUE INDEX idx_structures_lower_id_unique ON structures (lower(name), type);
2324

2425
CREATE INDEX idx_structures_by_type ON structures (type);
2526

27+
-- STRUCTURE MAPS
28+
29+
CREATE TABLE structure_maps (
30+
name TEXT NOT NULL,
31+
input_type TEXT NOT NULL,
32+
output_type TEXT NOT NULL,
33+
PRIMARY KEY (name, input_type, output_type),
34+
FOREIGN KEY (input_type) REFERENCES structure_types (type) ON DELETE RESTRICT,
35+
FOREIGN KEY (output_type) REFERENCES structure_types (type) ON DELETE RESTRICT
36+
);
37+
38+
INSERT INTO structure_maps (name, input_type, output_type) VALUES
39+
('source', 'functor', 'category'),
40+
('target', 'functor', 'category'),
41+
('left_adjoint', 'functor', 'functor');
42+
43+
CREATE TABLE structure_map_values (
44+
name TEXT NOT NULL,
45+
input TEXT NOT NULL,
46+
input_type TEXT NOT NULL,
47+
output TEXT NOT NULL,
48+
output_type TEXT NOT NULL,
49+
PRIMARY KEY (name, input, input_type, output, output_type),
50+
FOREIGN KEY (name, input_type, output_type)
51+
REFERENCES structure_maps (name, input_type, output_type) ON DELETE CASCADE,
52+
FOREIGN KEY (input, input_type) REFERENCES structures (name, type) ON DELETE CASCADE,
53+
FOREIGN KEY (output, output_type) REFERENCES structures (name, type) ON DELETE CASCADE
54+
);
55+
2656
-- TAGS
2757

2858
CREATE TABLE tags (
@@ -36,46 +66,47 @@ CREATE TABLE tags (
3666
CREATE INDEX idx_tags_by_type ON tags (type);
3767

3868
CREATE TABLE tag_assignments (
39-
structure_id TEXT NOT NULL,
69+
structure TEXT NOT NULL,
4070
tag TEXT NOT NULL,
4171
type TEXT NOT NULL,
42-
PRIMARY KEY (structure_id, tag),
43-
FOREIGN KEY (structure_id, type) REFERENCES structures (id, type) ON DELETE CASCADE,
72+
PRIMARY KEY (structure, tag),
73+
FOREIGN KEY (structure, type) REFERENCES structures (name, type) ON DELETE CASCADE,
4474
FOREIGN KEY (tag, type) REFERENCES tags (tag, type) ON DELETE CASCADE
4575
);
4676

4777
-- RELATED AND DUAL STRUCTURES
4878

4979
CREATE TABLE related_structures (
50-
structure_id TEXT NOT NULL,
51-
related_structure_id TEXT NOT NULL,
52-
PRIMARY KEY (structure_id, related_structure_id),
53-
FOREIGN KEY (structure_id) REFERENCES structures (id) ON DELETE CASCADE,
54-
FOREIGN KEY (related_structure_id) REFERENCES structures (id) ON DELETE CASCADE
55-
-- intentionally we do not save the type here
80+
structure TEXT NOT NULL,
81+
related_structure TEXT NOT NULL,
82+
type TEXT NOT NULL,
83+
PRIMARY KEY (structure, related_structure, type),
84+
FOREIGN KEY (structure, type) REFERENCES structures (name, type) ON DELETE CASCADE,
85+
FOREIGN KEY (related_structure, type) REFERENCES structures (name, type) ON DELETE CASCADE
5686
);
5787

5888
CREATE TABLE dual_structures (
59-
structure_id TEXT NOT NULL,
60-
dual_structure_id TEXT NOT NULL,
89+
structure TEXT NOT NULL,
90+
dual_structure TEXT NOT NULL,
6191
type TEXT NOT NULL,
62-
PRIMARY KEY (structure_id, dual_structure_id),
63-
FOREIGN KEY (structure_id, type) REFERENCES structures (id, type) ON DELETE CASCADE,
64-
FOREIGN KEY (dual_structure_id, type) REFERENCES structures (id, type) ON DELETE CASCADE,
65-
UNIQUE (structure_id),
66-
UNIQUE (dual_structure_id)
92+
PRIMARY KEY (structure, dual_structure, type),
93+
FOREIGN KEY (structure, type) REFERENCES structures (name, type) ON DELETE CASCADE,
94+
FOREIGN KEY (dual_structure, type) REFERENCES structures (name, type) ON DELETE CASCADE,
95+
UNIQUE (structure),
96+
UNIQUE (dual_structure)
6797
);
6898

6999
-- COMMENTS
70100

71101
CREATE TABLE structure_comments (
72102
id INTEGER PRIMARY KEY,
73-
structure_id TEXT NOT NULL,
103+
structure TEXT NOT NULL,
104+
type TEXT NOT NULL,
74105
comment TEXT NOT NULL,
75-
FOREIGN KEY (structure_id) REFERENCES structures (id) ON DELETE CASCADE
106+
FOREIGN KEY (structure, type) REFERENCES structures (name, type) ON DELETE CASCADE
76107
);
77108

78-
CREATE INDEX idx_comments_by_structure ON structure_comments (structure_id);
109+
CREATE INDEX idx_comments_by_structure ON structure_comments (structure, type);
79110

80111
-- PROPERTIES OF STRUCTURES
81112

@@ -85,50 +116,51 @@ CREATE TABLE relations (
85116
);
86117

87118
CREATE TABLE properties (
88-
id TEXT PRIMARY KEY,
119+
name TEXT NOT NULL,
89120
type TEXT NOT NULL,
90121
relation TEXT NOT NULL,
91122
description TEXT NOT NULL CHECK (length(description) > 0),
92123
nlab_link TEXT CHECK (nlab_link IS NULL OR nlab_link like 'https://%'),
93124
invariant_under_equivalences INTEGER NOT NULL DEFAULT TRUE,
94-
UNIQUE (id, type),
125+
CHECK (invariant_under_equivalences in (TRUE, FALSE)),
126+
PRIMARY KEY (name, type),
95127
FOREIGN KEY (type) REFERENCES structure_types (type) ON DELETE RESTRICT,
96128
FOREIGN KEY (relation) REFERENCES relations (relation) ON DELETE RESTRICT
97129
);
98130

99-
CREATE UNIQUE INDEX idx_properties_lower_id_unique ON properties (lower(id));
131+
CREATE UNIQUE INDEX idx_properties_lower_name_unique ON properties (lower(name), type);
100132

101133
CREATE INDEX idx_properties_by_type ON properties (type);
102134

103135
-- RELATED AND DUAL PROPERTIES
104136

105137
CREATE TABLE related_properties (
106-
property_id TEXT NOT NULL,
107-
related_property_id TEXT NOT NULL,
108-
PRIMARY KEY (property_id, related_property_id),
109-
FOREIGN KEY (property_id) REFERENCES properties (id) ON DELETE CASCADE,
110-
FOREIGN KEY (related_property_id) REFERENCES properties (id) ON DELETE CASCADE
111-
-- intentionally we do not save the type here
138+
property TEXT NOT NULL,
139+
related_property TEXT NOT NULL,
140+
type TEXT NOT NULL,
141+
PRIMARY KEY (property, related_property, type),
142+
FOREIGN KEY (property, type) REFERENCES properties (name, type) ON DELETE CASCADE,
143+
FOREIGN KEY (related_property, type) REFERENCES properties (name, type) ON DELETE CASCADE
112144
);
113145

114146
CREATE TABLE dual_properties (
115-
property_id TEXT NOT NULL,
116-
dual_property_id TEXT NOT NULL,
147+
property TEXT NOT NULL,
148+
dual_property TEXT NOT NULL,
117149
type TEXT NOT NULL,
118-
PRIMARY KEY (property_id, dual_property_id),
119-
FOREIGN KEY (property_id, type) REFERENCES properties (id, type) ON DELETE CASCADE,
120-
FOREIGN KEY (dual_property_id, type) REFERENCES properties (id, type) ON DELETE CASCADE,
121-
UNIQUE (property_id),
122-
UNIQUE (dual_property_id)
150+
PRIMARY KEY (property, dual_property, type),
151+
FOREIGN KEY (property, type) REFERENCES properties (name, type) ON DELETE CASCADE,
152+
FOREIGN KEY (dual_property, type) REFERENCES properties (name, type) ON DELETE CASCADE,
153+
UNIQUE (property, type),
154+
UNIQUE (dual_property, type)
123155
);
124156

125157
-- PROPERTY ASSIGNMENTS
126158

127159
CREATE TABLE property_assignments (
128160
id INTEGER PRIMARY KEY,
129161
type TEXT NOT NULL,
130-
structure_id TEXT NOT NULL,
131-
property_id TEXT NOT NULL,
162+
structure TEXT NOT NULL,
163+
property TEXT NOT NULL,
132164
is_satisfied INTEGER
133165
-- we use is_satisfied = NULL for undecidable properties
134166
CHECK (is_satisfied in (TRUE, FALSE, NULL)),
@@ -137,18 +169,18 @@ CREATE TABLE property_assignments (
137169
CHECK (is_deduced in (TRUE, FALSE)),
138170
check_redundancy INTEGER NOT NULL DEFAULT TRUE
139171
CHECK (check_redundancy in (TRUE, FALSE)),
140-
UNIQUE (structure_id, property_id),
141-
FOREIGN KEY (structure_id, type) REFERENCES structures (id, type) ON DELETE CASCADE,
142-
FOREIGN KEY (property_id, type) REFERENCES properties (id, type) ON DELETE CASCADE
172+
UNIQUE (structure, property, type),
173+
FOREIGN KEY (structure, type) REFERENCES structures (name, type) ON DELETE CASCADE,
174+
FOREIGN KEY (property, type) REFERENCES properties (name, type) ON DELETE CASCADE
143175
);
144176

145-
CREATE INDEX idx_assignment_by_property ON property_assignments (property_id);
177+
CREATE INDEX idx_assignment_by_property ON property_assignments (property, type);
146178
CREATE INDEX idx_assignment_by_type ON property_assignments (type);
147179

148180
-- IMPLICATIONS BETWEEN PROPERTIES
149181

150182
CREATE TABLE implications (
151-
id TEXT PRIMARY KEY,
183+
name TEXT NOT NULL,
152184
type TEXT NOT NULL,
153185
proof TEXT NOT NULL CHECK (length(proof) > 0),
154186
is_equivalence INTEGER NOT NULL DEFAULT FALSE
@@ -157,49 +189,44 @@ CREATE TABLE implications (
157189
CHECK (is_deduced in (TRUE, FALSE)),
158190
dualized_from TEXT,
159191
CHECK (dualized_from IS NULL OR is_deduced = TRUE),
160-
UNIQUE (id, type),
192+
PRIMARY KEY (name, type),
161193
FOREIGN KEY (type) REFERENCES structure_types (type) ON DELETE RESTRICT,
162-
FOREIGN KEY (dualized_from, type) REFERENCES implications (id, type) ON DELETE CASCADE
194+
FOREIGN KEY (dualized_from, type) REFERENCES implications (name, type) ON DELETE CASCADE
163195
);
164196

165-
CREATE UNIQUE INDEX idx_implications_lower_id_unique ON implications (lower(id));
197+
CREATE UNIQUE INDEX idx_implications_lower_name_unique ON implications (lower(name), type);
166198

167-
CREATE TABLE implication_properties (
168-
implication_id TEXT NOT NULL,
169-
property_id TEXT NOT NULL,
199+
CREATE TABLE assumptions (
200+
implication TEXT NOT NULL,
201+
property TEXT NOT NULL,
170202
type TEXT NOT NULL,
171-
kind TEXT NOT NULL
172-
CHECK (kind in ('assumption', 'conclusion')),
173-
structure TEXT NOT NULL
174-
CHECK (structure in ('self', 'source', 'target')),
175-
PRIMARY KEY (implication_id, property_id, kind, structure),
176-
FOREIGN KEY (implication_id, type) REFERENCES implications (id, type) ON DELETE CASCADE,
177-
FOREIGN KEY (property_id) REFERENCES properties (id) ON DELETE CASCADE
178-
);
179-
180-
CREATE INDEX idx_implication_property ON implication_properties (property_id);
181-
182-
CREATE TRIGGER trg_implication_properties_type_structure_check
183-
BEFORE INSERT ON implication_properties
184-
BEGIN
185-
SELECT
186-
CASE
187-
WHEN NEW.type = 'category'
188-
AND NEW.structure != 'self'
189-
THEN RAISE(ABORT, 'Category implications must use structure = self')
190-
191-
WHEN NEW.type = 'category'
192-
AND (SELECT type FROM properties WHERE id = NEW.property_id) != 'category'
193-
THEN RAISE(ABORT, 'Category implications require category properties')
194-
195-
WHEN NEW.type = 'functor'
196-
AND NEW.structure = 'self'
197-
AND (SELECT type FROM properties WHERE id = NEW.property_id) != 'functor'
198-
THEN RAISE(ABORT, 'Functor self-structure requires functor properties')
199-
200-
WHEN NEW.type = 'functor'
201-
AND NEW.structure IN ('source', 'target')
202-
AND (SELECT type FROM properties WHERE id = NEW.property_id) != 'category'
203-
THEN RAISE(ABORT, 'Functor source/target require category properties')
204-
END;
205-
END;
203+
PRIMARY KEY (implication, property, type),
204+
FOREIGN KEY (implication, type) REFERENCES implications (name, type) ON DELETE CASCADE,
205+
FOREIGN KEY (property, type) REFERENCES properties (name, type) ON DELETE CASCADE
206+
);
207+
208+
CREATE TABLE conclusions (
209+
implication TEXT NOT NULL,
210+
property TEXT NOT NULL,
211+
type TEXT NOT NULL,
212+
PRIMARY KEY (implication, property, type),
213+
FOREIGN KEY (implication, type) REFERENCES implications (name, type) ON DELETE CASCADE,
214+
FOREIGN KEY (property, type) REFERENCES properties (name, type) ON DELETE CASCADE
215+
);
216+
217+
CREATE TABLE mapped_assumptions (
218+
name TEXT NOT NULL,
219+
implication TEXT NOT NULL,
220+
implication_type TEXT NOT NULL,
221+
property TEXT NOT NULL,
222+
property_type TEXT NOT NULL,
223+
FOREIGN KEY (implication, implication_type)
224+
REFERENCES implications (name, type)
225+
ON DELETE CASCADE,
226+
FOREIGN KEY (property, property_type)
227+
REFERENCES properties (name, type)
228+
ON DELETE CASCADE,
229+
FOREIGN KEY (name, implication_type, property_type)
230+
REFERENCES structure_maps (name, input_type, output_type)
231+
ON DELETE CASCADE
232+
);
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
CREATE TABLE categories (
2-
id TEXT PRIMARY KEY,
2+
name TEXT PRIMARY KEY,
3+
type NOT NULL DEFAULT 'category' CHECK (type = 'category'),
34
objects TEXT NOT NULL,
45
morphisms TEXT NOT NULL,
5-
FOREIGN KEY (id) REFERENCES structures (id) ON DELETE CASCADE
6+
FOREIGN KEY (name, type) REFERENCES structures (name, type) ON DELETE CASCADE
67
);
78

8-
CREATE TRIGGER categories_type_check
9-
BEFORE INSERT ON categories
10-
FOR EACH ROW
11-
BEGIN
12-
SELECT CASE
13-
WHEN (SELECT type FROM structures WHERE id = NEW.id) != 'category'
14-
THEN RAISE(ABORT, 'invalid category id')
15-
END;
16-
END;
17-
189
CREATE VIEW categories_view AS
1910
SELECT
20-
s.id, s.name, s.notation, s.description, s.nlab_link,
21-
c.objects, c.morphisms, d.dual_structure_id as dual_category_id
11+
s.name, s.name, s.notation, s.description, s.nlab_link,
12+
c.objects, c.morphisms, d.dual_structure as dual_category
2213
FROM categories c
23-
INNER JOIN structures s ON s.id = c.id
24-
LEFT JOIN dual_structures d on d.structure_id = c.id
14+
INNER JOIN structures s ON s.name = c.name
15+
LEFT JOIN dual_structures d ON d.structure = c.name AND d.type = c.type
2516
;
2617

2718
CREATE TABLE special_object_types (
@@ -32,15 +23,15 @@ CREATE TABLE special_object_types (
3223
);
3324

3425
CREATE TABLE special_objects (
35-
category_id TEXT NOT NULL,
26+
category TEXT NOT NULL,
3627
type TEXT NOT NULL,
3728
description TEXT NOT NULL,
38-
PRIMARY KEY (category_id, type),
29+
PRIMARY KEY (category, type),
3930
FOREIGN KEY (type) REFERENCES special_object_types (type) ON DELETE RESTRICT,
40-
FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE
31+
FOREIGN KEY (category) REFERENCES categories (name) ON DELETE CASCADE
4132
);
4233

43-
CREATE INDEX idx_special_objects_by_category ON special_objects (category_id);
34+
CREATE INDEX idx_special_objects_by_category ON special_objects (category);
4435

4536
CREATE TABLE special_morphism_types (
4637
id INTEGER PRIMARY KEY,
@@ -50,13 +41,13 @@ CREATE TABLE special_morphism_types (
5041
);
5142

5243
CREATE TABLE special_morphisms (
53-
category_id TEXT NOT NULL,
44+
category TEXT NOT NULL,
5445
type TEXT NOT NULL,
5546
description TEXT NOT NULL,
5647
proof TEXT NOT NULL,
57-
PRIMARY KEY (category_id, type),
48+
PRIMARY KEY (category, type),
5849
FOREIGN KEY (type) REFERENCES special_morphism_types (type) ON DELETE RESTRICT,
59-
FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE
50+
FOREIGN KEY (category) REFERENCES categories (name) ON DELETE CASCADE
6051
);
6152

62-
CREATE INDEX idx_special_morphisms_by_category ON special_morphisms (category_id);
53+
CREATE INDEX idx_special_morphisms_by_category ON special_morphisms (category);

0 commit comments

Comments
 (0)