Skip to content

Commit d26def5

Browse files
committed
unify functions to fetch normalized implications
1 parent 8ea18e9 commit d26def5

6 files changed

Lines changed: 185 additions & 283 deletions

File tree

databases/catdat/scripts/deduce-structure-properties.ts

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ import { type Database, SqliteError } from 'better-sqlite3'
77
import { get_client, is_subset } from './utils/helpers'
88
import {
99
get_structures,
10-
get_normalized_implications,
1110
get_properties_dict,
1211
get_property_assignments,
1312
is_dual_structure,
1413
type StructureMeta,
15-
type NormalizedImplication,
1614
type PropertyMeta,
1715
} from './utils/deduction'
18-
import { get_contradiction_string, get_proof_string } from './utils/implications'
16+
import {
17+
get_contradiction_string,
18+
get_normalized_functor_implications,
19+
get_proof_string,
20+
type NormalizedImplication,
21+
} from './utils/implications'
1922
import type { StructureType } from './config'
2023

2124
/**
@@ -31,8 +34,8 @@ export function get_deduced_satisfied_properties(
3134
stop_when_found?: string
3235
},
3336
type: StructureType,
34-
// used for source and target properties of a functor
35-
associated_satisfied_properties?: Record<string, Set<string>>,
37+
source_props?: Set<string>,
38+
target_props?: Set<string>,
3639
) {
3740
const found = new Set<string>()
3841
const proofs: Record<string, string> = {}
@@ -49,15 +52,21 @@ export function get_deduced_satisfied_properties(
4952

5053
if (!is_valid) continue
5154

52-
if (implication.associated_assumptions) {
53-
const is_applicable = Object.keys(
54-
implication.associated_assumptions,
55-
).every((key) => {
56-
return is_subset(
57-
implication.associated_assumptions?.[key] ?? new Set(),
58-
associated_satisfied_properties?.[key] ?? new Set(),
59-
)
60-
})
55+
if (implication.source_assumptions) {
56+
const is_applicable = is_subset(
57+
implication.source_assumptions,
58+
source_props ?? new Set(),
59+
)
60+
61+
if (!is_applicable) continue
62+
}
63+
64+
if (implication.target_assumptions) {
65+
const is_applicable = is_subset(
66+
implication.target_assumptions,
67+
target_props ?? new Set(),
68+
)
69+
6170
if (!is_applicable) continue
6271
}
6372

@@ -101,8 +110,8 @@ export function get_deduced_unsatisfied_properties(
101110
stop_when_found?: string
102111
},
103112
type: StructureType,
104-
// used for source and target properties of a functor
105-
associated_satisfied_properties?: Record<string, Set<string>>,
113+
source_props?: Set<string>,
114+
target_props?: Set<string>,
106115
) {
107116
const found = new Set<string>()
108117
const proofs: Record<string, string> = {}
@@ -122,15 +131,19 @@ export function get_deduced_unsatisfied_properties(
122131
})
123132
if (!is_valid) continue
124133

125-
if (implication.associated_assumptions) {
126-
const is_applicable = Object.keys(
127-
implication.associated_assumptions,
128-
).every((key) => {
129-
return is_subset(
130-
implication.associated_assumptions?.[key] ?? new Set(),
131-
associated_satisfied_properties?.[key] ?? new Set(),
132-
)
133-
})
134+
if (implication.source_assumptions) {
135+
const is_applicable = is_subset(
136+
implication.source_assumptions,
137+
source_props ?? new Set(),
138+
)
139+
if (!is_applicable) continue
140+
}
141+
142+
if (implication.target_assumptions) {
143+
const is_applicable = is_subset(
144+
implication.target_assumptions,
145+
target_props ?? new Set(),
146+
)
134147
if (!is_applicable) continue
135148
}
136149

@@ -257,7 +270,8 @@ function deduce_satisfied_properties(
257270
implications,
258271
{ properties_dict },
259272
type,
260-
structure.associated_satisfied_properties,
273+
structure.source_props,
274+
structure.target_props,
261275
)
262276

263277
for (const p of found) satisfied_properties.add(p)
@@ -287,7 +301,8 @@ function deduce_unsatisfied_properties(
287301
implications,
288302
{ properties_dict },
289303
type,
290-
structure.associated_satisfied_properties,
304+
structure.source_props,
305+
structure.target_props,
291306
)
292307

293308
for (const p of found) unsatisfied_properties.add(p)
@@ -398,8 +413,9 @@ export function deduce_properties_for_structures(type: StructureType) {
398413

399414
delete_deduced_properties(db, type)
400415

401-
const implications = get_normalized_implications(db, type)
416+
const implications = get_normalized_functor_implications(db, type)
402417
const structures = get_structures(db, type)
418+
403419
const properties_dict = get_properties_dict(db, type)
404420
const get_assigned_properties = get_property_assignments(db, structures, type)
405421

databases/catdat/scripts/redundancies.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { get_client } from './utils/helpers'
2-
import {
3-
get_categories,
4-
get_normalized_category_implications,
5-
type NormalizedCategoryImplication,
6-
} from './utils/categories'
2+
import { get_categories } from './utils/categories'
73
import {
84
get_deduced_satisfied_properties,
95
get_deduced_unsatisfied_properties,
@@ -12,8 +8,12 @@ import {
128
get_property_assignments_by_deduction,
139
type StructureMeta,
1410
} from './utils/deduction'
15-
import { get_functors, get_normalized_functor_implications } from './utils/functors'
11+
import { get_functors } from './utils/functors'
1612
import type { StructureType } from './config'
13+
import {
14+
get_normalized_functor_implications,
15+
NormalizedImplication,
16+
} from './utils/implications'
1717

1818
const db = get_client()
1919

@@ -36,11 +36,7 @@ function check_redundancies() {
3636
function check_redundant_property_assignments(type: StructureType) {
3737
console.info(`\n--- Check redundant ${type} property assignments ---`)
3838

39-
// TODO: refactor this when > 2 types of structures are available
40-
const implications =
41-
type === 'category'
42-
? get_normalized_category_implications(db)
43-
: get_normalized_functor_implications(db)
39+
const implications = get_normalized_functor_implications(db, type)
4440

4541
const structures: StructureMeta[] =
4642
type === 'category' ? get_categories(db) : get_functors(db)
@@ -60,7 +56,8 @@ function check_redundant_property_assignments(type: StructureType) {
6056
assignments[structure.id].satisfied.non_deduced,
6157
implications,
6258
ignore_dict[structure.id],
63-
structure.associated_satisfied_properties,
59+
structure.source_props,
60+
structure.target_props,
6461
)
6562

6663
if (redundant_satisfied_property) {
@@ -81,7 +78,8 @@ function check_redundant_property_assignments(type: StructureType) {
8178
assignments[structure.id].unsatisfied.non_deduced,
8279
implications,
8380
ignore_dict[structure.id],
84-
structure.associated_satisfied_properties,
81+
structure.source_props,
82+
structure.target_props,
8583
)
8684

8785
if (redundant_unsatisfied_property) {
@@ -112,9 +110,10 @@ function check_redundant_property_assignments(type: StructureType) {
112110
*/
113111
function get_redundant_satisfied_property(
114112
satisfied_properties: Set<string>,
115-
implications: NormalizedCategoryImplication[],
113+
implications: NormalizedImplication[],
116114
ignored: Set<string> = new Set(),
117-
associated_satisfied_properties?: Record<string, Set<string>>,
115+
source_props?: Set<string>,
116+
target_props?: Set<string>,
118117
) {
119118
for (const p of [...satisfied_properties]) {
120119
if (ignored.has(p)) continue
@@ -124,7 +123,8 @@ function get_redundant_satisfied_property(
124123
implications,
125124
{ stop_when_found: p },
126125
'category',
127-
associated_satisfied_properties,
126+
source_props,
127+
target_props,
128128
)
129129
if (deduced_satisfied_properties.has(p)) return p
130130
satisfied_properties.add(p)
@@ -141,9 +141,10 @@ function get_redundant_satisfied_property(
141141
function get_redundant_unsatisfied_property(
142142
satisfied_properties: Set<string>,
143143
unsatisfied_properties: Set<string>,
144-
implications: NormalizedCategoryImplication[],
144+
implications: NormalizedImplication[],
145145
ignored: Set<string> = new Set(),
146-
associated_satisfied_properties?: Record<string, Set<string>>,
146+
source_props?: Set<string>,
147+
target_props?: Set<string>,
147148
) {
148149
for (const p of [...unsatisfied_properties]) {
149150
if (ignored.has(p)) continue
@@ -154,7 +155,8 @@ function get_redundant_unsatisfied_property(
154155
implications,
155156
{ stop_when_found: p },
156157
'category',
157-
associated_satisfied_properties,
158+
source_props,
159+
target_props,
158160
)
159161
if (deduced_unsatisfied_properties.has(p)) return p
160162
unsatisfied_properties.add(p)
Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,14 @@
11
import { type Database } from 'better-sqlite3'
2-
import { parse_json_set } from './helpers'
3-
4-
type CategoryMeta = {
5-
id: string
6-
name: string
7-
dual: string | null
8-
}
9-
10-
export type NormalizedCategoryImplication = {
11-
id: string
12-
assumptions: Set<string>
13-
conclusion: string
14-
}
2+
import type { StructureMeta } from './deduction'
153

164
/**
175
* Returns the list of categories saved in the database.
186
*/
19-
export function get_categories(db: Database) {
7+
export function get_categories(db: Database): StructureMeta[] {
208
return db
21-
.prepare<never[], CategoryMeta>(
9+
.prepare<never[], StructureMeta>(
2210
`SELECT id, name, dual_category_id as dual
2311
FROM categories_view ORDER BY lower(name)`,
2412
)
2513
.all()
2614
}
27-
28-
/**
29-
* Implications have the form
30-
* P_1 + ... + P_n ===> Q_1 + ... + Q_m
31-
* or
32-
* P_1 + ... + P_n <===> Q_1 + ... + Q_m.
33-
* This function decomposes them into normalized implications,
34-
* which have the form
35-
* P_1 + ... + P_n ===> Q.
36-
*/
37-
export function get_normalized_category_implications(
38-
db: Database,
39-
): NormalizedCategoryImplication[] {
40-
// TODO: This needs to be unified with the functor case.
41-
42-
const all_implications_db = db
43-
.prepare<
44-
never[],
45-
{
46-
id: string
47-
is_equivalence: 0 | 1
48-
assumptions: string
49-
conclusions: string
50-
}
51-
>(
52-
`SELECT
53-
i.id,
54-
i.is_equivalence,
55-
(
56-
SELECT json_group_array(property_id)
57-
FROM implication_properties ip
58-
WHERE ip.implication_id = i.id
59-
AND ip.structure = 'self'
60-
AND ip.kind = 'assumption'
61-
) as assumptions,
62-
(
63-
SELECT json_group_array(property_id)
64-
FROM implication_properties ip
65-
WHERE ip.implication_id = i.id
66-
AND ip.structure = 'self'
67-
AND ip.kind = 'conclusion'
68-
) as conclusions
69-
FROM implications i
70-
WHERE i.type = 'category'
71-
GROUP BY i.id`,
72-
)
73-
.all()
74-
75-
const implications: NormalizedCategoryImplication[] = []
76-
77-
for (const impl of all_implications_db) {
78-
const assumptions = parse_json_set<string>(impl.assumptions)
79-
const conclusions = parse_json_set<string>(impl.conclusions)
80-
81-
for (const conclusion of conclusions) {
82-
implications.push({ id: impl.id, assumptions, conclusion })
83-
}
84-
85-
if (impl.is_equivalence) {
86-
for (const assumption of assumptions) {
87-
implications.push({
88-
id: impl.id,
89-
assumptions: conclusions,
90-
conclusion: assumption,
91-
})
92-
}
93-
}
94-
}
95-
96-
return implications
97-
}

databases/catdat/scripts/utils/deduction.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Database } from 'better-sqlite3'
2-
import { get_categories, get_normalized_category_implications } from './categories'
3-
import { get_functors, get_normalized_functor_implications } from './functors'
2+
import { get_categories } from './categories'
3+
import { get_functors } from './functors'
44
import { StructureType } from '../config'
55

66
/**
@@ -10,16 +10,8 @@ export type StructureMeta = {
1010
id: string
1111
name: string
1212
dual?: string | null
13-
// used for source and target properties of a functor
14-
associated_satisfied_properties?: Record<string, Set<string>>
15-
}
16-
17-
export type NormalizedImplication = {
18-
id: string
19-
assumptions: Set<string>
20-
conclusion: string
21-
// used for source and target assumptions of a functor in an implication
22-
associated_assumptions?: Record<string, Set<string>>
13+
source_props?: Set<string>
14+
target_props?: Set<string>
2315
}
2416

2517
export type PropertyMeta = {
@@ -38,18 +30,6 @@ export function get_structures(db: Database, type: StructureType): StructureMeta
3830
throw new Error('Unsupported type')
3931
}
4032

41-
/**
42-
* Returns the list of normalized implications saved in the database of a given type.
43-
*/
44-
export function get_normalized_implications(
45-
db: Database,
46-
type: StructureType,
47-
): NormalizedImplication[] {
48-
if (type === 'category') return get_normalized_category_implications(db)
49-
if (type === 'functor') return get_normalized_functor_implications(db)
50-
throw new Error('Unsupported type')
51-
}
52-
5333
/**
5434
* Returns a dictionary of properties saved in the database.
5535
*/

0 commit comments

Comments
 (0)