Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Aug 29, 2024
1 parent f5d7d1e commit d411f9d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

> - https://github.com/cicada-lang/propagator/issues/3
barometer-belief-system.test.ts -- test `BeliefSystem`
belief-system-monad.ts
barometer-belief-system.test.ts
`beliefSystemQuery`

# propagator 支持 dependencies for implicit search
Expand Down
115 changes: 115 additions & 0 deletions src/examples/barometer-belief-system.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { test } from "node:test"
import { BeliefSystem } from "../belief-system/index.js"
import { Belief } from "../belief/index.js"
import { put } from "../cell/index.js"
import { Interval } from "../interval/index.js"
import { similarTriangles } from "./barometer.js"

test("examples / barometer-belief-system", async () => {
const [barometerShadow, barometerHeight, buildingShadow, buildingHeight] =
similarTriangles()
put(buildingShadow, BeliefSystem([Belief(Interval(54.9, 55.1), ["shadows"])]))
put(barometerHeight, BeliefSystem([Belief(Interval(0.3, 0.32), ["shadows"])]))
put(
barometerShadow,
BeliefSystem([Belief(Interval(0.36, 0.37), ["shadows"])]),
)

// await run()

// assertBelief(buildingHeight.content, ["shadows"])
// assert(
// intervalAlmostEqual(
// buildingHeight.content.value,
// Interval(44.51, 48.97),
// 0.01,
// ),
// )

// const fallTime = Cell()
// fallDuration(fallTime, buildingHeight)
// put(fallTime, Belief(Interval(2.9, 3.3), ["lousy-fall-time"]))

// await run()

// assertBelief(fallTime.content, ["shadows"])
// assert(intervalAlmostEqual(fallTime.content.value, Interval(3, 3.16), 0.01))

// assertBelief(buildingHeight.content, ["shadows"])
// assert(
// intervalAlmostEqual(
// buildingHeight.content.value,
// Interval(44.51, 48.97),
// 0.01,
// ),
// )

// put(fallTime, Belief(Interval(2.9, 3.1), ["better-fall-time"]))

// await run()

// assertBelief(buildingHeight.content, ["shadows", "better-fall-time"])
// assert(
// intervalAlmostEqual(
// buildingHeight.content.value,
// Interval(44.51, 47.24),
// 0.01,
// ),
// )

// put(buildingHeight, Belief(45, ["superintendent"]))

// await run()

// assertBelief(buildingHeight.content, ["superintendent"])
// assert.deepStrictEqual(buildingHeight.content.value, 45)

// assertBelief(barometerHeight.content, [
// "superintendent",
// "better-fall-time",
// "shadows",
// ])
// assert(
// intervalAlmostEqual(
// barometerHeight.content.value,
// Interval(0.3, 0.3),
// 0.01,
// ),
// )

// assertBelief(barometerShadow.content, [
// "superintendent",
// "better-fall-time",
// "shadows",
// ])
// assert(
// intervalAlmostEqual(
// barometerShadow.content.value,
// Interval(0.36, 0.37),
// 0.01,
// ),
// )

// assertBelief(buildingShadow.content, ["shadows"])
// assert(
// intervalAlmostEqual(
// buildingShadow.content.value,
// Interval(54.9, 55.1),
// 0.01,
// ),
// )

// assertBelief(fallTime.content, ["superintendent"])
// assert(
// intervalAlmostEqual(fallTime.content.value, Interval(3.02, 3.03), 0.01),
// )

// {
// const [t, h] = fallDuration()
// put(h, 45)

// await run()

// assert(intervalAlmostEqual(t.content, Interval(3.02, 3.03), 0.01))
// }
})
8 changes: 4 additions & 4 deletions src/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ defineHandler(merge, [isInterval, isInterval], (content, increment) => {
return newInterval
})

defineHandler(merge, [isInterval, isNumber], (content, increment) =>
intervalContainsNumber(content, increment) ? increment : theMergeConflict,
)

defineHandler(merge, [isNumber, isInterval], (content, increment) =>
intervalContainsNumber(increment, content) ? content : theMergeConflict,
)

defineHandler(merge, [isInterval, isNumber], (content, increment) =>
intervalContainsNumber(content, increment) ? increment : theMergeConflict,
)

function isSimple(x: any): boolean {
return isNumber(x) || isInterval(x)
}
Expand Down

0 comments on commit d411f9d

Please sign in to comment.