Skip to content

Commit

Permalink
remove current implementation of MergeConflict
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 6, 2024
1 parent 24ee2ec commit 6f3d5d9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 37 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
`barometer-belief.test` -- 在测试中增加书中的叙述 -- 与对 belief-system 的测试相同

# 4.3 Dependencies Explain Contradictions

> https://github.com/cicada-lang/propagator/issues/9
修复对 `MergeConflict` 的使用 -- 如何处理原实现中的 `call/cc`
implementation `MergeConflict` without `call/cc`

# 4.4 Dependencies Improve Search

Expand Down Expand Up @@ -37,6 +35,8 @@

# later

[later] `barometer-belief.test` -- 在测试中增加书中的叙述 -- 与对 belief-system 的测试相同

[later] barometer-belief-system.test.ts -- 理解我们的测试中与书中有差异的地方

- 书中第一个测试只有:
Expand Down
7 changes: 1 addition & 6 deletions src/belief-system/assimilateBeliefSystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type Nothing, isNothing } from "../nothing/Nothing.js"
import { assimilateBelief } from "./assimilateBelief.js"
import type { BeliefSystem } from "./BeliefSystem.js"

Expand All @@ -8,12 +7,8 @@ import type { BeliefSystem } from "./BeliefSystem.js"

export function assimilateBeliefSystem<A, B>(
base: BeliefSystem<A>,
target: BeliefSystem<B> | Nothing,
target: BeliefSystem<B>,
): BeliefSystem<A | B> {
if (isNothing(target)) {
return base
}

return target.beliefs.reduce<BeliefSystem<A | B>>(
(result, belief) => assimilateBelief(result, belief),
base,
Expand Down
8 changes: 3 additions & 5 deletions src/belief-system/checkConsistent.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { detectMergeConflict } from "../merge-conflict/detectMergeConflict.js"
import { isRunning } from "../scheduler/run.js"

import { clearScheduledPropagators } from "../scheduler/schedule.js"
import { log } from "../utils/log.js"

export function checkConsistent(value: any): void {
return

if (detectMergeConflict(value)) {
const who = "checkConsistent"
const message = "Inconsistent value"
log({ who, message, value })
clearScheduledPropagators()

if (isRunning) {
throw new Error(`[${who}] ${message}`)
}
}
}
7 changes: 5 additions & 2 deletions src/belief/beliefMerge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { MergeConflict } from "../merge-conflict/index.js"
import { merge } from "../merge/index.js"
import { setIsSubsetOf, setUnion } from "../utils/set/index.js"
import { Belief } from "./Belief.js"
Expand All @@ -24,7 +23,7 @@ import { Belief } from "./Belief.js"
export function beliefMerge<A, B>(
content: Belief<A>,
increment: Belief<B>,
): Belief<A | B> | MergeConflict {
): Belief<A | B> {
const mergedValue = merge(content.value, increment.value)

// 下面刚好是偏序关系中比较两个元素的四种可能:
Expand All @@ -45,6 +44,10 @@ export function beliefMerge<A, B>(
}
}

// if (isMergeConflict(mergedValue)) {
// console.log({ who: "beliefMerge", increment, content })
// }

if (mergedValue === increment.value) {
return increment
}
Expand Down
16 changes: 6 additions & 10 deletions src/cell/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import { schedule } from "../scheduler/index.js"
import { type Cell } from "./Cell.js"

export function put<T>(cell: Cell<T>, increment?: any): void {
try {
const newContent = merge(cell.content, increment)
if (newContent === cell.content) {
return
}

cell.content = newContent
schedule(cell.propagators)
} catch (error) {
console.error({ who: "put", error })
const newContent = merge(cell.content, increment)
if (newContent === cell.content) {
return
}

cell.content = newContent
schedule(cell.propagators)
}
8 changes: 8 additions & 0 deletions src/examples/barometer-belief-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ test("examples / barometer-belief-system", async () => {

await run()

log(beliefSystemQuery(buildingHeight.content))
log(buildingHeight.content)
log(beliefSystemQuery(barometerHeight.content))

// kickOut("pressure")

// await run()

// log(beliefSystemQuery(barometerHeight.content))
// log(beliefSystemQuery(buildingHeight.content))
})
1 change: 1 addition & 0 deletions src/monad/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./belief-monad.js"
export * from "./belief-system-monad.js"
export * from "./merge-conflict-monad.js"
export * from "./monad.js"
export * from "./nothing-monad.js"
9 changes: 9 additions & 0 deletions src/monad/merge-conflict-monad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineHandler } from "../generic/index.js"
import { isMergeConflict } from "../merge-conflict/index.js"
import { nothing } from "../nothing/index.js"
import { isFunction } from "../utils/isFunction.js"
import { flatten, fmap } from "./monad.js"

defineHandler(fmap, [isFunction, isMergeConflict], (f, ma) => nothing)

defineHandler(flatten, [isMergeConflict], (mma) => nothing)
15 changes: 4 additions & 11 deletions src/scheduler/run.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import type { Job } from "./Scheduler.js"
import { globalScheduler } from "./initializeScheduler.js"

export let isRunning = false
export async function run(): Promise<void> {
try {
isRunning = true
while (globalScheduler.jobs.length > 0) {
// 我们用「先进先出」的方式来处理 job 的 queue。
const job = globalScheduler.jobs.shift() as Job
await job.propagator()
}
isRunning = false
} catch (error) {
console.error({ who: "run", error })
while (globalScheduler.jobs.length > 0) {
// 我们用「先进先出」的方式来处理 job 的 queue。
const job = globalScheduler.jobs.shift() as Job
await job.propagator()
}
}

0 comments on commit 6f3d5d9

Please sign in to comment.