Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 238x 238x 238x 238x 238x 238x 1x 807x 807x 291x 291x 807x 1x 807x 807x 807x 807x 807x 807x 146x 146x 146x 146x 807x 146x 146x 807x 807x 1x 172x 172x 172x 172x 172x 172x 172x 172x 172x 19x 19x 165x 30x 30x 153x 123x 123x 123x 123x 172x 172x 1x 216x 216x 216x 1x 1x 7x 7x 7x 7x 24x 18x 18x 24x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 118x 118x 118x 118x 1x 1x 1x 1x 1x 1x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 167x 8x 8x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 6x 5x 5x 5x 6x 3x 3x 1x 1x 1x 44x 43x 43x 43x 43x 44x 32x 32x 1x 1x 1x 1x 1x 1x 1x 20x 20x 32x 2x 2x 2x 2x 2x 2x 2x 2x 2x 32x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 32x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 5x 5x 5x 13x 13x 5x 5x 1x 1x 1x 3x 3x 3x 2x 2x 2x 2x 2x 2x 5x 5x 2x 2x | import type {
DistancePair,
EqualityFact,
AngleEqualityFact,
Citation,
AngleMeasure,
ProofFact,
} from './facts'
import {
distancePairKey,
distancePairsEqual,
angleMeasureKey,
angleMeasuresEqual,
isAngleFact,
} from './facts'
// ── Union-Find internals ──
// The UF is a derived cache over a store's facts. It's encapsulated here
// via a WeakMap — callers never see or interact with it directly.
interface UFNode {
parent: string
rank: number
/** The fact ID that caused this node to merge with its parent (null for roots) */
mergeFact: number | null
}
interface UnionFind {
nodes: Map<string, UFNode>
}
/** Module-private: each FactStore has an associated UF, invisible to callers. */
const ufMap = new WeakMap<FactStore, UnionFind>()
function getUf(store: FactStore): UnionFind {
const uf = ufMap.get(store)
if (!uf)
throw new Error(
'FactStore has no associated union-find — was it created via createFactStore()?'
)
return uf
}
function ufEnsure(uf: UnionFind, key: string) {
if (!uf.nodes.has(key)) {
uf.nodes.set(key, { parent: key, rank: 0, mergeFact: null })
}
}
function ufFind(uf: UnionFind, key: string): string {
ufEnsure(uf, key)
let node = uf.nodes.get(key)!
// Path compression
const path: string[] = []
while (node.parent !== key) {
path.push(key)
key = node.parent
node = uf.nodes.get(key)!
}
for (const p of path) {
uf.nodes.get(p)!.parent = key
}
return key
}
function ufUnion(uf: UnionFind, a: string, b: string, factId: number): boolean {
const rootA = ufFind(uf, a)
const rootB = ufFind(uf, b)
if (rootA === rootB) return false // already in same set
const nodeA = uf.nodes.get(rootA)!
const nodeB = uf.nodes.get(rootB)!
if (nodeA.rank < nodeB.rank) {
nodeA.parent = rootB
nodeA.mergeFact = factId
} else if (nodeA.rank > nodeB.rank) {
nodeB.parent = rootA
nodeB.mergeFact = factId
} else {
nodeB.parent = rootA
nodeB.mergeFact = factId
nodeA.rank += 1
}
return true
}
function ufConnected(uf: UnionFind, a: string, b: string): boolean {
return ufFind(uf, a) === ufFind(uf, b)
}
/** Get all keys in the same set as the given key */
function ufGetSet(uf: UnionFind, key: string): string[] {
const root = ufFind(uf, key)
const result: string[] = []
for (const [k] of uf.nodes) {
if (ufFind(uf, k) === root) {
result.push(k)
}
}
return result
}
// ── FactStore ──
export interface FactStore {
facts: EqualityFact[]
angleFacts: AngleEqualityFact[]
nextId: number
}
export function createFactStore(): FactStore {
const store: FactStore = { facts: [], angleFacts: [], nextId: 1 }
ufMap.set(store, { nodes: new Map() })
return store
}
/**
* Add an equality fact and run incremental C.N.1 transitivity via union-find merge.
* Mutates the store in place and returns the newly created facts.
*/
export function addFact(
store: FactStore,
left: DistancePair,
right: DistancePair,
citation: Citation,
statement: string,
justification: string,
atStep: number
): EqualityFact[] {
const uf = getUf(store)
const keyL = distancePairKey(left)
const keyR = distancePairKey(right)
// Don't add duplicate facts
if (ufConnected(uf, keyL, keyR)) {
return []
}
const fact: EqualityFact = {
id: store.nextId,
left,
right,
citation,
statement,
justification,
atStep,
}
store.facts.push(fact)
store.nextId++
ufUnion(uf, keyL, keyR, fact.id)
return [fact]
}
/**
* Add an angle equality fact via union-find merge.
* Mutates the store in place and returns the newly created facts.
*/
export function addAngleFact(
store: FactStore,
left: AngleMeasure,
right: AngleMeasure,
citation: Citation,
statement: string,
justification: string,
atStep: number
): AngleEqualityFact[] {
const uf = getUf(store)
const keyL = angleMeasureKey(left)
const keyR = angleMeasureKey(right)
// Don't add duplicate facts
if (ufConnected(uf, keyL, keyR)) {
return []
}
const fact: AngleEqualityFact = {
id: store.nextId,
left,
right,
citation,
statement,
justification,
atStep,
}
store.angleFacts.push(fact)
store.nextId++
ufUnion(uf, keyL, keyR, fact.id)
return [fact]
}
/** Are these two angles known-equal? */
export function queryAngleEquality(store: FactStore, a: AngleMeasure, b: AngleMeasure): boolean {
if (angleMeasuresEqual(a, b)) return true
const uf = getUf(store)
const keyA = angleMeasureKey(a)
const keyB = angleMeasureKey(b)
if (!uf.nodes.has(keyA) || !uf.nodes.has(keyB)) return false
return ufConnected(uf, keyA, keyB)
}
/** Are these two distances known-equal? */
export function queryEquality(store: FactStore, a: DistancePair, b: DistancePair): boolean {
if (distancePairsEqual(a, b)) return true
const uf = getUf(store)
const keyA = distancePairKey(a)
const keyB = distancePairKey(b)
// If either key hasn't been registered, they can't be equal
if (!uf.nodes.has(keyA) || !uf.nodes.has(keyB)) return false
return ufConnected(uf, keyA, keyB)
}
/**
* Create a fresh FactStore and replay the given facts via addFact()/addAngleFact().
* This rebuilds the union-find from scratch — necessary because the
* WeakMap-encapsulated UF can't be cloned.
*/
export function rebuildFactStore(facts: ProofFact[]): FactStore {
const store = createFactStore()
for (const fact of facts) {
if (isAngleFact(fact)) {
addAngleFact(
store,
fact.left,
fact.right,
fact.citation,
fact.statement,
fact.justification,
fact.atStep
)
} else {
addFact(
store,
fact.left,
fact.right,
fact.citation,
fact.statement,
fact.justification,
fact.atStep
)
}
}
return store
}
/**
* Merge proof facts into an existing fact store (idempotent).
* Facts already connected in the union-find are silently skipped.
* Use after a replayConstruction to restore author-declared facts
* that the replay doesn't know about.
*/
export function mergeProofFacts(store: FactStore, proofFacts: ProofFact[]): void {
for (const fact of proofFacts) {
if (isAngleFact(fact)) {
addAngleFact(
store,
fact.left,
fact.right,
fact.citation,
fact.statement,
fact.justification,
fact.atStep
)
} else {
addFact(
store,
fact.left,
fact.right,
fact.citation,
fact.statement,
fact.justification,
fact.atStep
)
}
}
}
/** All distances known-equal to this one */
export function getEqualDistances(store: FactStore, dp: DistancePair): DistancePair[] {
const uf = getUf(store)
const key = distancePairKey(dp)
if (!uf.nodes.has(key)) return [dp]
const setKeys = ufGetSet(uf, key)
return setKeys.map((k) => {
const [a, b] = k.split('|')
return { a, b }
})
}
/** All angles known-equal to this one */
export function getEqualAngles(store: FactStore, am: AngleMeasure): AngleMeasure[] {
const uf = getUf(store)
const key = angleMeasureKey(am)
if (!uf.nodes.has(key)) return [am]
const setKeys = ufGetSet(uf, key)
// Only return keys that are angle keys (∠ prefix)
return setKeys
.filter((k) => k.startsWith('∠'))
.map((k) => {
const [vertex, ray1, ray2] = k.slice(1).split('|')
return { vertex, ray1, ray2 }
})
}
|