Skip to content

Commit

Permalink
rename belief.reason to belief.reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 1, 2024
1 parent 120f06c commit 0b4931c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rename assertBelief to assertBeliefReasons

# propagator 支持 dependencies for alternate worldviews

> - https://github.com/cicada-lang/propagator/issues/3
Expand Down
8 changes: 4 additions & 4 deletions src/belief-system/beliefSystemMerge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Belief, isBelief, type Reason } from "../belief/index.js"
import { Belief, isBelief, type Reasons } from "../belief/index.js"
import { isNothing, nothing, type Nothing } from "../cell/index.js"
import { implies, merge, type MergeConflict } from "../merge/index.js"
import { setIsSubsetOf } from "../utils/Set.js"
Expand Down Expand Up @@ -30,7 +30,7 @@ function beliefSystemAssimilate<A, B>(
}

function subsumes<A, B>(x: Belief<A>, y: Belief<B>): boolean {
return implies(x.value, y.value) && setIsSubsetOf(x.reason, y.reason)
return implies(x.value, y.value) && setIsSubsetOf(x.reasons, y.reasons)
}

function beliefSystemAssimilateOne<A, B>(
Expand Down Expand Up @@ -59,10 +59,10 @@ function strongestConsequence<A>(
}

function isBeliefBelieved<A>(belief: Belief<A>): boolean {
return Array.from(belief.reason).every(isReasonEntryBelieved)
return Array.from(belief.reasons).every(isReasonEntryBelieved)
}

const globelReasonEntryBlackList: Reason = new Set()
const globelReasonEntryBlackList: Reasons = new Set()

function isReasonEntryBelieved(entry: string): boolean {
return !globelReasonEntryBlackList.has(entry)
Expand Down
28 changes: 16 additions & 12 deletions src/belief/Belief.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import assert from "node:assert"
import { isNonNullObject } from "../utils/isNonNullObject.js"
import { log } from "../utils/log.js"

export type Reason = Set<string>
export type ReasonLike = Reason | Array<string>
export type Reason = string
export type Reasons = Set<Reason>
export type ReasonLike = Reasons | Array<Reason>

export function toReason(x: ReasonLike): Reason {
export function toReason(x: ReasonLike): Reasons {
if (x instanceof Array) {
return new Set(x)
}
Expand All @@ -16,16 +17,19 @@ export function toReason(x: ReasonLike): Reason {
export type Belief<T> = {
"@type": "Belief"
value: T
reason: Reason
reasons: Reasons
}

export function Belief<T>(value: T, reason: Reason | Array<string>): Belief<T> {
reason = toReason(reason)
export function Belief<T>(
value: T,
reasons: Reasons | Array<string>,
): Belief<T> {
reasons = toReason(reasons)

return {
"@type": "Belief",
value,
reason,
reasons,
}
}

Expand All @@ -40,7 +44,7 @@ export function toBelief(x: any): Belief<any> {

export function assertBelief(
target: any,
reason?: Reason | Array<string>,
reasons?: Reasons | Array<string>,
): asserts target is Belief<any> {
if (!isBelief(target)) {
const message = `Assertion fails.`
Expand All @@ -49,15 +53,15 @@ export function assertBelief(
who: "assertBelief",
message,
target,
reason,
reasons,
})

throw new Error(`[assertBelief] ${message}`)
}

if (reason !== undefined) {
reason = toReason(reason)
if (reasons !== undefined) {
reasons = toReason(reasons)

assert.deepStrictEqual(target.reason, reason)
assert.deepStrictEqual(target.reasons, reasons)
}
}
4 changes: 2 additions & 2 deletions src/belief/beliefMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function beliefMerge<A, B>(
if (implies(increment.value, mergedValue)) {
// 倾向于 content,除非 increment 真的有更多信息。
// 更小的 reason 集合,代表拥有更多的信息(更精确的依赖关系)。
if (setIsSubsetOf(content.reason, increment.reason)) {
if (setIsSubsetOf(content.reasons, increment.reasons)) {
return content
} else {
return increment
Expand All @@ -31,6 +31,6 @@ export function beliefMerge<A, B>(
return increment
}

const mergedReason = setUnion(content.reason, increment.reason)
const mergedReason = setUnion(content.reasons, increment.reasons)
return Belief(mergedValue, mergedReason)
}
4 changes: 2 additions & 2 deletions src/monads/belief-monad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setUnion } from "../utils/Set.js"
import { isFunction } from "../utils/isFunction.js"

defineHandler(fmap, [isFunction, isBelief], (f, ma) =>
Belief(bind(ma.value, f), ma.reason),
Belief(bind(ma.value, f), ma.reasons),
)

defineHandler(join, [isBelief], (mma) => mma)
Expand All @@ -16,5 +16,5 @@ defineHandler(
(mma) => nothing,
)
defineHandler(join, [(mma) => isBelief(mma) && isBelief(mma.value)], (mma) =>
join(Belief(mma.value.value, setUnion(mma.reason, mma.value.reason))),
join(Belief(mma.value.value, setUnion(mma.reasons, mma.value.reasons))),
)
2 changes: 1 addition & 1 deletion src/monads/belief-system-monad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defineHandler(
mma.beliefs.flatMap((belief) =>
isBeliefSystem(belief.value)
? belief.value.beliefs.map((innerBelief) =>
join(Belief(innerBelief, belief.reason)),
join(Belief(innerBelief, belief.reasons)),
)
: [belief],
),
Expand Down

0 comments on commit 0b4931c

Please sign in to comment.