Skip to content

Commit

Permalink
fix belief-system-monad.ts by the paper
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 6, 2024
1 parent ba5f4b7 commit 5146c54
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> https://github.com/cicada-lang/propagator/issues/9
implementation `MergeConflict` without `call/cc`
remove `checkConsistent` -- implementation `MergeConflict` without `call/cc`

# 4.4 Dependencies Improve Search

Expand Down
6 changes: 0 additions & 6 deletions src/belief-system/assimilateBelief.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Belief } from "../belief/Belief.js"
import { detectMergeConflict } from "../merge-conflict/detectMergeConflict.js"
import { type Nothing, isNothing } from "../nothing/Nothing.js"
import { log } from "../utils/log.js"
import { BeliefSystem } from "./BeliefSystem.js"
import { isStrongerBelief } from "./isStrongerBelief.js"

Expand Down Expand Up @@ -31,9 +29,5 @@ export function assimilateBelief<A>(
(oldBelief) => !isStrongerBelief(belief, oldBelief),
)

if (detectMergeConflict(belief)) {
log({ newSystem: BeliefSystem([...strongerOldBeliefs, belief]) })
}

return BeliefSystem([...strongerOldBeliefs, belief])
}
5 changes: 1 addition & 4 deletions src/belief-system/checkConsistent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { log } from "console"
import { detectMergeConflict } from "../merge-conflict/detectMergeConflict.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()
}
}
10 changes: 5 additions & 5 deletions src/examples/barometer-belief-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,13 @@ test("examples / barometer-belief-system", async () => {
await run()

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

// kickOut("pressure")
kickOut("pressure")

// await run()
await run()

// log(beliefSystemQuery(barometerHeight.content))
// log(beliefSystemQuery(buildingHeight.content))
log(beliefSystemQuery(buildingHeight.content))
log(beliefSystemQuery(barometerHeight.content))
})
71 changes: 55 additions & 16 deletions src/monad/belief-system-monad.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import { BeliefSystem, isBeliefSystem } from "../belief-system/index.js"
import { Belief } from "../belief/index.js"
import {
BeliefSystem,
beliefSystemQuery,
isBeliefSystem,
} from "../belief-system/index.js"
import { Belief, isBelief } from "../belief/index.js"
import { defineHandler } from "../generic/index.js"
import { isFunction } from "../utils/isFunction.js"
import { flatten, fmap } from "./monad.js"

defineHandler(fmap, [isFunction, isBeliefSystem], (f, ma: BeliefSystem<any>) =>
BeliefSystem(ma.beliefs.map((belief) => fmap(f, belief))),
// If the input is a belief system, query it, operate on that, and
// pack the result up into a (one-item) belief system.
//
// The only choice made here is that we are following the philosophy
// set out in the main text of operating on the strongest possible
// consequences of what we currently believe (as found by the query)
// rather than on all variations of everything we might believe.

defineHandler(
fmap,
[isFunction, isBeliefSystem],
(f, ma: BeliefSystem<any>) => {
const strongestBelief = beliefSystemQuery(ma)
return BeliefSystem([fmap(f, strongestBelief)])
},
)

defineHandler(
Expand All @@ -15,16 +32,38 @@ defineHandler(
isBeliefSystem(mma) &&
mma.beliefs.some((belief) => isBeliefSystem(belief.value)),
],
(mma: BeliefSystem<any>) =>
flatten(
BeliefSystem(
mma.beliefs.flatMap((belief) =>
isBeliefSystem(belief.value)
? belief.value.beliefs.map((innerBelief) =>
flatten(Belief(innerBelief, belief.reasons)),
)
: [belief],
),
),
),
(mma: BeliefSystem<any>) => {
const beliefs = mma.beliefs.flatMap((belief) => {
if (isBeliefSystem(belief.value)) {
return belief.value.beliefs.map((innerBelief) =>
flatten(Belief(innerBelief, belief.reasons)),
)
} else {
return [belief]
}
})

return flatten(BeliefSystem(beliefs))
},
)

// But we also need to extend the code for flattening a belief,
// because we want to do something interesting if we find a belief
// with a belief system inside (namely incorporate the reasons of that
// belief into the beliefs of that belief system).
//
// This handler also uses query to consider only the strongest belief
// of the inner belief system.

defineHandler(
flatten,
[(mma) => isBelief(mma) && isBeliefSystem(mma.value)],
(mma: Belief<BeliefSystem<any>>) => {
const innerBeliefSystem = mma.value
const strongestBelief = beliefSystemQuery(innerBeliefSystem)
const newBeliefSystem = BeliefSystem(
flatten(Belief(strongestBelief, mma.reasons)),
)
return flatten(newBeliefSystem)
},
)

0 comments on commit 5146c54

Please sign in to comment.