Skip to content

Commit ad9bd01

Browse files
committed
refactor fetching functions
1 parent 4d13582 commit ad9bd01

29 files changed

Lines changed: 351 additions & 399 deletions

File tree

databases/catdat/scripts/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ export const PLURALS = {
55
functor: 'functors',
66
} as const
77

8-
// TODO: move this to the schema
98
export const STRUCTURE_MAPS = [
109
['source', 'functor', 'category'],
1110
['target', 'functor', 'category'],
1211
] as const
1312

14-
// TODO: move this to the schema
1513
export const STRUCTURES_WITH_DUALS: StructureType[] = ['category']

src/lib/commons/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ export type ImplicationDisplay = Replace<
120120
}
121121
>
122122

123+
export type NormalizedImplication = {
124+
id: string
125+
assumptions: Set<string>
126+
conclusion: string
127+
}
128+
123129
export type SearchResults = {
124130
contradiction: string[] | null
125131
satisfied_properties: string[]

src/lib/server/consistency.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { is_subset } from './utils'
22
import type { SqliteError } from 'better-sqlite3'
3-
import {
4-
get_normalized_implications,
5-
stringify_implication,
6-
type NormalizedImplication,
7-
} from './implications'
8-
import type { StructureType } from '$lib/commons/types'
3+
import type { NormalizedImplication, StructureType } from '$lib/commons/types'
4+
import { get_normalized_implications } from './fetchers/implications'
95

106
// TODO: If possible, remove the code duplication with deduction and redundancy scripts.
117

@@ -107,3 +103,7 @@ function build_shortest_proof(
107103

108104
return proof
109105
}
106+
107+
function stringify_implication(implication: NormalizedImplication) {
108+
return `${[...implication.assumptions].join(' ∧ ')}${implication.conclusion}`
109+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type {
2+
CategorySpecificDisplay,
3+
SpecialMorphism,
4+
SpecialObject,
5+
StructureShort,
6+
} from '$lib/commons/types'
7+
import sql from 'sql-template-tag'
8+
import { batch, query } from '$lib/server/db.catdat'
9+
import { error } from '@sveltejs/kit'
10+
11+
export function fetch_category(id: string) {
12+
const { results, err } = batch<
13+
[CategorySpecificDisplay, SpecialObject, SpecialMorphism]
14+
>([
15+
// specific information for the category
16+
sql`
17+
SELECT c.objects, c.morphisms
18+
FROM categories c
19+
WHERE c.id = ${id}
20+
`,
21+
// special objects
22+
sql`
23+
SELECT s.type, s.description
24+
FROM special_objects s
25+
INNER JOIN special_object_types t ON t.type = s.type
26+
WHERE s.category_id = ${id}
27+
ORDER BY t.id
28+
`,
29+
// special morphisms
30+
sql`
31+
SELECT t.type, s.description, s.proof
32+
FROM special_morphism_types t
33+
LEFT JOIN special_morphisms s
34+
ON s.type = t.type AND s.category_id = ${id}
35+
ORDER BY t.id
36+
`,
37+
])
38+
39+
if (err) error(500, 'Could not load category')
40+
41+
const [categories, special_objects, special_morphisms] = results
42+
43+
if (!categories.length) error(404, `Could not find category with ID '${id}'`)
44+
45+
return {
46+
...categories[0],
47+
special_objects,
48+
special_morphisms,
49+
}
50+
}
51+
52+
export function fetch_categories_with_missing_morphisms() {
53+
const { rows, err } = query<StructureShort & { count: number }>(
54+
sql`
55+
SELECT s.id, s.name, COUNT(*) AS count
56+
FROM structures s
57+
JOIN special_morphism_types t
58+
LEFT JOIN special_morphisms m
59+
ON m.category_id = s.id AND m.type = t.type
60+
WHERE s.type = 'category' AND m.type IS NULL
61+
GROUP BY s.id
62+
ORDER BY lower(s.name);
63+
`,
64+
)
65+
66+
if (err) error(500, 'Failed to load data')
67+
68+
return rows
69+
}
Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { error, type RequestEvent } from '@sveltejs/kit'
1+
import { error } from '@sveltejs/kit'
22
import { query } from '$lib/server/db.catdat'
33
import { render_nested_formulas } from '$lib/server/formulas'
44
import { MAX_STRUCTURES_COMPARE } from '$lib/commons/compare.utils'
55
import type { ComparisonResult, StructureType } from '$lib/commons/types'
66
import { PLURALS } from '$lib/commons/structures'
7+
import { to_placeholders } from '$lib/server/utils'
78

8-
export function compare_handler(
9-
event: RequestEvent,
9+
export function fetch_comparison_result(
10+
compared_ids: string[],
1011
type: StructureType,
12+
callback: () => void,
1113
): ComparisonResult {
12-
if (!event.params.ids) error(400, `No ${type} selected for comparison`)
13-
14-
const compared_ids = event.params.ids?.split('/')
15-
1614
if (!compared_ids.length) error(400, `No ${type} selected for comparison`)
1715

1816
if (compared_ids.length > MAX_STRUCTURES_COMPARE) {
@@ -22,8 +20,6 @@ export function compare_handler(
2220
)
2321
}
2422

25-
const placeholders = compared_ids.map(() => '?').join(', ')
26-
2723
const { rows, err: err_cat } = query<{
2824
id: string
2925
name: string
@@ -32,7 +28,8 @@ export function compare_handler(
3228
sql: `
3329
SELECT id, name, notation
3430
FROM structures
35-
WHERE type = ? AND id in (${placeholders})`,
31+
WHERE type = ?
32+
AND id in (${to_placeholders(compared_ids)})`,
3633
values: [type, ...compared_ids],
3734
})
3835

@@ -69,7 +66,7 @@ export function compare_handler(
6966

7067
values.push(type)
7168

72-
const stmt = `
69+
const comparison_query = `
7370
SELECT
7471
p.id AS property_id,
7572
${select_columns}
@@ -79,18 +76,15 @@ export function compare_handler(
7976
ORDER BY lower(p.id)`
8077

8178
const { rows: comparison_objects, err } = query<Record<string, string>>({
82-
sql: stmt,
79+
sql: comparison_query,
8380
values,
8481
})
8582

86-
if (err) error(500, 'Could not load properties')
83+
if (err) error(500, 'Could not load comparison table')
8784

8885
const comparison_table = comparison_objects.map((obj) => Object.values(obj))
8986

90-
event.setHeaders({
91-
// shared cache for 30min
92-
'cache-control': 'public, max-age=0, s-maxage=1800',
93-
})
87+
callback()
9488

9589
return {
9690
structures: render_nested_formulas(structures),

src/lib/server/fetchers/content.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { PropertyShort, StructureShort } from '$lib/commons/types'
2+
import { batch } from '$lib/server/db.catdat'
3+
import { error } from '@sveltejs/kit'
4+
import sql from 'sql-template-tag'
5+
6+
export function fetch_category_references(content_id: string) {
7+
const { results, err: err_categories } = batch<
8+
[StructureShort, PropertyShort, { id: string }]
9+
>([
10+
sql`
11+
SELECT DISTINCT s.id, s.name
12+
FROM property_assignments pa
13+
INNER JOIN structures s ON s.id = pa.structure_id
14+
WHERE
15+
pa.type = 'category'
16+
AND pa.proof LIKE '%/content/' || ${content_id} || '%'
17+
18+
`,
19+
sql`
20+
SELECT id, relation FROM properties
21+
WHERE
22+
type = 'category'
23+
AND description LIKE '%/content/' || ${content_id} || '%'
24+
`,
25+
sql`
26+
SELECT id FROM implications
27+
WHERE
28+
type = 'category'
29+
AND proof LIKE '%/content/' || ${content_id} || '%'
30+
`,
31+
])
32+
33+
if (err_categories) error(500, 'Could not load context')
34+
35+
const [categories, category_properties, category_implications] = results
36+
37+
return { categories, category_properties, category_implications }
38+
}

src/lib/server/fetchers/functor.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { FunctorSpecificDisplay } from '$lib/commons/types'
2+
import sql from 'sql-template-tag'
3+
import { query } from '$lib/server/db.catdat'
4+
import { error } from '@sveltejs/kit'
5+
6+
export function fetch_functor(id: string) {
7+
const { rows: functors, err } = query<FunctorSpecificDisplay>(
8+
// specific information for the functor
9+
sql`
10+
SELECT
11+
f.source,
12+
f.target,
13+
source.name AS source_name,
14+
source.notation AS source_notation,
15+
target.name AS target_name,
16+
target.notation AS target_notation,
17+
la.id AS left_adjoint,
18+
la.name AS left_adjoint_name,
19+
la.notation AS left_adjoint_notation,
20+
ra.id AS right_adjoint,
21+
ra.name AS right_adjoint_name,
22+
ra.notation AS right_adjoint_notation
23+
FROM functors f
24+
INNER JOIN structures AS source ON source.id = f.source
25+
INNER JOIN structures AS target ON target.id = f.target
26+
LEFT JOIN structures AS la ON la.id = f.left_adjoint
27+
LEFT JOIN functors AS rf ON rf.left_adjoint = f.id
28+
LEFT JOIN structures AS ra ON ra.id = rf.id
29+
WHERE f.id = ${id}
30+
`,
31+
)
32+
33+
if (err) error(500, 'Could not load functor')
34+
35+
if (!functors.length) error(404, `Could not find functor with ID '${id}'`)
36+
37+
return functors[0]
38+
}

src/lib/server/fetchers/implication.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@ export function fetch_implication(type: StructureType, id: string) {
1818
target_assumptions,
1919
conclusions
2020
FROM implications_view
21-
WHERE
22-
type = ${type}
23-
AND id = ${id}
21+
WHERE id = ${id}
2422
`,
2523
sql`
26-
SELECT s.id, s.name
27-
FROM structures s
28-
WHERE s.type = ${type}
29-
AND EXISTS (
30-
SELECT 1 FROM property_assignments cp
31-
WHERE
32-
cp.type = ${type}
33-
AND cp.structure_id = s.id
34-
AND cp.proof LIKE '%/' || ${type} || '-implication/' || ${id} || '%'
35-
)
24+
SELECT DISTINCT s.id, s.name
25+
FROM property_assignments pa
26+
INNER JOIN structures s ON s.id = pa.structure_id
27+
WHERE
28+
pa.type = ${type}
29+
AND pa.proof LIKE '%/' || ${type} || '-implication/' || ${id} || '%'
3630
`,
3731
])
3832

src/lib/server/fetchers/implications.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { query } from '$lib/server/db.catdat'
22
import sql from 'sql-template-tag'
33
import { error } from '@sveltejs/kit'
4-
import type { ImplicationDB, StructureType } from '$lib/commons/types'
4+
import type {
5+
ImplicationDB,
6+
NormalizedImplication,
7+
StructureType,
8+
} from '$lib/commons/types'
59
import { display_implication } from '$lib/server/transforms'
10+
import { parse_json_set } from '$lib/server/utils'
611

712
export function fetch_implications(type: StructureType) {
813
const { rows, err } = query<ImplicationDB>(sql`
@@ -27,3 +32,63 @@ export function fetch_implications(type: StructureType) {
2732

2833
return { implications }
2934
}
35+
36+
/**
37+
* List of normalized implications of a given type that have,
38+
* in the case of functors, no source and no target assumptions,
39+
* i.e. those that are universally true. Only those are relevant
40+
* for the consistency check.
41+
*/
42+
export function get_normalized_implications(type: StructureType) {
43+
// TODO: remove duplication with deduction script
44+
45+
const { rows, err } = query<{
46+
id: string
47+
is_equivalence: 0 | 1
48+
assumptions: string
49+
source_assumptions: string
50+
target_assumptions: string
51+
conclusions: string
52+
}>(
53+
sql`
54+
SELECT
55+
id,
56+
is_equivalence,
57+
assumptions,
58+
source_assumptions,
59+
target_assumptions,
60+
conclusions
61+
FROM implications_view
62+
WHERE type = ${type}
63+
`,
64+
)
65+
66+
if (err) return { implications: null, err }
67+
68+
const implications: NormalizedImplication[] = []
69+
70+
for (const impl of rows) {
71+
const assumptions = parse_json_set<string>(impl.assumptions)
72+
const source_assumptions = parse_json_set<string>(impl.source_assumptions)
73+
const target_assumptions = parse_json_set<string>(impl.target_assumptions)
74+
const conclusions = parse_json_set<string>(impl.conclusions)
75+
76+
if (source_assumptions.size > 0 || target_assumptions.size > 0) continue
77+
78+
for (const conclusion of conclusions) {
79+
implications.push({ id: impl.id, assumptions, conclusion })
80+
}
81+
82+
if (impl.is_equivalence) {
83+
for (const assumption of assumptions) {
84+
implications.push({
85+
id: impl.id,
86+
assumptions: conclusions,
87+
conclusion: assumption,
88+
})
89+
}
90+
}
91+
}
92+
93+
return { implications, err: null }
94+
}

0 commit comments

Comments
 (0)