Skip to content

Commit

Permalink
propagator model 中的 put 应该被改为 patch
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 17, 2024
1 parent c4c40fa commit 8b91c17
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 64 deletions.
14 changes: 7 additions & 7 deletions src/belief/Belief.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert"
import { test } from "node:test"
import { Cell, put } from "../cell/index.js"
import { Cell, patch } from "../cell/index.js"
import { Interval, intervalAlmostEqual } from "../interval/index.js"
import { run } from "../scheduler/index.js"
import { Belief, beliefEqual, isBelief } from "./index.js"
Expand All @@ -15,9 +15,9 @@ test("dependency / a justified-intervals anomaly", async () => {
// A,B,C: [ ]

const interval = Cell()
put(interval, Belief(Interval(0, 100), ["A"]))
put(interval, Belief(Interval(50, 200), ["B"]))
put(interval, Belief(Interval(25, 75), ["C"]))
patch(interval, Belief(Interval(0, 100), ["A"]))
patch(interval, Belief(Interval(50, 200), ["B"]))
patch(interval, Belief(Interval(25, 75), ["C"]))

await run()

Expand All @@ -34,9 +34,9 @@ test("dependency / a justified-intervals anomaly", async () => {
// The order matters.

const interval = Cell()
put(interval, Belief(Interval(25, 75), ["C"]))
put(interval, Belief(Interval(0, 100), ["A"]))
put(interval, Belief(Interval(50, 200), ["B"]))
patch(interval, Belief(Interval(25, 75), ["C"]))
patch(interval, Belief(Interval(0, 100), ["A"]))
patch(interval, Belief(Interval(50, 200), ["B"]))

await run()

Expand Down
2 changes: 1 addition & 1 deletion src/cell/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./addPropagator.js"
export * from "./Cell.js"
export * from "./put.js"
export * from "./patch.js"
2 changes: 1 addition & 1 deletion src/cell/put.ts → src/cell/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { merge } from "../merge/index.js"
import { schedule } from "../scheduler/index.js"
import { type Cell } from "./Cell.js"

export function put<T>(cell: Cell<T>, increment?: any): void {
export function patch<T>(cell: Cell<T>, increment?: any): void {
const newContent = merge(cell.content, increment)
if (newContent === cell.content) {
return
Expand Down
20 changes: 13 additions & 7 deletions src/examples/barometer-belief-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
kickOut,
} from "../belief-system/index.js"
import { Belief, beliefEqual } from "../belief/index.js"
import { Cell, put } from "../cell/index.js"
import { Cell, patch } from "../cell/index.js"
import { Interval, intervalAlmostEqual, isInterval } from "../interval/index.js"
import { theMergeConflict } from "../merge-conflict/index.js"
import { isNothing } from "../nothing/index.js"
Expand All @@ -25,9 +25,15 @@ 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(
patch(
buildingShadow,
BeliefSystem([Belief(Interval(54.9, 55.1), ["shadows"])]),
)
patch(
barometerHeight,
BeliefSystem([Belief(Interval(0.3, 0.32), ["shadows"])]),
)
patch(
barometerShadow,
BeliefSystem([Belief(Interval(0.36, 0.37), ["shadows"])]),
)
Expand All @@ -53,7 +59,7 @@ test("examples / barometer-belief-system", async () => {

const fallTime = Cell()
fallDuration(fallTime, buildingHeight)
put(fallTime, BeliefSystem([Belief(Interval(2.9, 3.1), ["fall-time"])]))
patch(fallTime, BeliefSystem([Belief(Interval(2.9, 3.1), ["fall-time"])]))

await run()

Expand Down Expand Up @@ -241,7 +247,7 @@ test("examples / barometer-belief-system", async () => {
// Now, if we give the superintendent a barometer, we can add her
// input to the totality of our knowledge about this building

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

await run()

Expand Down Expand Up @@ -418,7 +424,7 @@ test("examples / barometer-belief-system", async () => {
// know; and since the system maintains dependency information, it
// can even tell us which premises lead to trouble.

put(buildingHeight, Belief(Interval(46, 50), ["pressure"]))
patch(buildingHeight, Belief(Interval(46, 50), ["pressure"]))

await run()

Expand Down
16 changes: 8 additions & 8 deletions src/examples/barometer-belief.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import assert from "node:assert"
import { test } from "node:test"
import { Belief, beliefEqual, isBelief } from "../belief/index.js"
import { Cell, put } from "../cell/index.js"
import { Cell, patch } from "../cell/index.js"
import { Interval, intervalAlmostEqual } from "../interval/index.js"
import { run } from "../scheduler/index.js"
import { fallDuration, similarTriangles } from "./barometer.js"

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

await run()

Expand All @@ -28,7 +28,7 @@ test("examples / barometer-belief", async () => {

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

await run()

Expand All @@ -50,7 +50,7 @@ test("examples / barometer-belief", async () => {
),
)

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

await run()

Expand All @@ -65,7 +65,7 @@ test("examples / barometer-belief", async () => {
),
)

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

await run()

Expand Down Expand Up @@ -130,7 +130,7 @@ test("examples / barometer-belief", async () => {

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

await run()

Expand Down
16 changes: 8 additions & 8 deletions src/examples/barometer.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assert from "node:assert"
import { test } from "node:test"
import { Cell, put } from "../cell/index.js"
import { Cell, patch } from "../cell/index.js"
import { Interval, intervalAlmostEqual } from "../interval/index.js"
import { run } from "../scheduler/index.js"
import { fallDuration, similarTriangles } from "./barometer.js"

test("examples / barometer / fallDuration", async () => {
const [fallTime, buildingHeight] = fallDuration()
put(fallTime, Interval(2.9, 3.1))
patch(fallTime, Interval(2.9, 3.1))

await run()

Expand All @@ -18,15 +18,15 @@ test("examples / barometer / fallDuration", async () => {

test("examples / barometer / fallDuration / Interval + Number", async () => {
const [fallTime, buildingHeight] = fallDuration()
put(fallTime, Interval(2.9, 3.1))
patch(fallTime, Interval(2.9, 3.1))

await run()

assert(
intervalAlmostEqual(buildingHeight.content, Interval(41.16, 47.24), 0.01),
)

put(buildingHeight, 45)
patch(buildingHeight, 45)

await run()

Expand All @@ -36,9 +36,9 @@ test("examples / barometer / fallDuration / Interval + Number", async () => {
test("examples / barometer / similarTriangles & fallDuration", async () => {
const [barometerShadow, barometerHeight, buildingShadow, buildingHeight] =
similarTriangles()
put(buildingShadow, Interval(54.9, 55.1))
put(barometerHeight, Interval(0.3, 0.32))
put(barometerShadow, Interval(0.36, 0.37))
patch(buildingShadow, Interval(54.9, 55.1))
patch(barometerHeight, Interval(0.3, 0.32))
patch(barometerShadow, Interval(0.36, 0.37))

await run()

Expand All @@ -48,7 +48,7 @@ test("examples / barometer / similarTriangles & fallDuration", async () => {

const fallTime = Cell()
fallDuration(fallTime, buildingHeight)
put(fallTime, Interval(2.9, 3.1))
patch(fallTime, Interval(2.9, 3.1))

await run()

Expand Down
10 changes: 5 additions & 5 deletions src/examples/celsius.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert"
import { test } from "node:test"
import { put } from "../cell/index.js"
import { patch } from "../cell/index.js"
import { run } from "../scheduler/index.js"
import {
celsiusKelvin,
Expand All @@ -10,7 +10,7 @@ import {

test("examples / celsius / fahrenheitToCelsius", async () => {
const [f, c] = fahrenheitToCelsius()
put(f, 77)
patch(f, 77)

await run()

Expand All @@ -20,7 +20,7 @@ test("examples / celsius / fahrenheitToCelsius", async () => {
test("examples / celsius / fahrenheitCelsius", async () => {
{
const [f, c] = fahrenheitCelsius()
put(f, 77)
patch(f, 77)

await run()

Expand All @@ -29,7 +29,7 @@ test("examples / celsius / fahrenheitCelsius", async () => {

{
const [f, c] = fahrenheitCelsius()
put(c, 25)
patch(c, 25)

await run()

Expand All @@ -40,7 +40,7 @@ test("examples / celsius / fahrenheitCelsius", async () => {
test("examples / celsius / celsiusKelvin", async () => {
const [f, c] = fahrenheitCelsius()
const k = celsiusKelvin(c)
put(f, 77)
patch(f, 77)

await run()

Expand Down
6 changes: 3 additions & 3 deletions src/examples/heron.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assert from "node:assert"
import { test } from "node:test"
import { Cell, put } from "../cell/index.js"
import { Cell, patch } from "../cell/index.js"
import { run } from "../scheduler/index.js"
import { heronStep } from "./heron.js"

test("examples / heron / heronStep", async () => {
const [x, guess, betterGuess] = heronStep()
put(x, 2)
put(guess, 1.4)
patch(x, 2)
patch(guess, 1.4)

await run()

Expand Down
8 changes: 4 additions & 4 deletions src/propagator/definePrimitive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cell, addPropagator, put } from "../cell/index.js"
import { Cell, addPropagator, patch } from "../cell/index.js"
import { naryFmap } from "../monad/index.js"
import { schedule } from "../scheduler/index.js"
import type { MaybePromise } from "../utils/MaybePromise.js"
Expand Down Expand Up @@ -40,14 +40,14 @@ export function definePrimitive<A extends number>(
const output = args[args.length - 1]

watch(inputs, async () => {
put(output, await liftedFn(...inputs))
patch(output, await liftedFn(...inputs))
})
} else if (args.length === arity - 1) {
const inputs = args
const output = Cell()

watch(inputs, async () => {
put(output, await liftedFn(...inputs))
patch(output, await liftedFn(...inputs))
})

return output
Expand All @@ -57,7 +57,7 @@ export function definePrimitive<A extends number>(
const output = Cell()

watch(inputs, async () => {
put(output, await liftedFn(...inputs))
patch(output, await liftedFn(...inputs))
})

return [...paddings, output]
Expand Down
Loading

0 comments on commit 8b91c17

Please sign in to comment.