Skip to content

Commit

Permalink
ambr Contradiction MergeConflict
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 24, 2024
1 parent d1e75b4 commit 025864d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
7 changes: 4 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
>
> - https://github.com/cicada-lang/propagator/issues/2
`coercing`
`detectMergeConflict`
`applyAfter` (`coercing`)

fix `isContradiction` for `Supported`
fix `isMergeConflict` for `Supported`

- `isContradiction` should be a generic
- `isMergeConflict` should be a generic

test about "a justified-intervals anomaly"

Expand Down
4 changes: 2 additions & 2 deletions src/cell/Cell.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { merge, type Contradiction } from "../merge/index.js"
import { merge, type MergeConflict } from "../merge/index.js"
import type { Propagator } from "../propagator/index.js"
import { nothing, type Nothing } from "./Nothing.js"

export type Merge<T> = (
content: T | Nothing,
increment: any,
) => T | Nothing | Contradiction
) => T | Nothing | MergeConflict

export type Cell<T> = {
"@type": "Cell"
Expand Down
4 changes: 2 additions & 2 deletions src/cell/put.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isContradiction } from "../merge/index.js"
import { isMergeConflict } from "../merge/index.js"
import { schedule } from "../scheduler/index.js"
import { type Cell } from "./Cell.js"

Expand All @@ -7,7 +7,7 @@ export function put<T>(cell: Cell<T>, increment?: T): void {
if (newContent === cell.content) {
return
}
if (isContradiction(newContent)) {
if (isMergeConflict(newContent)) {
console.error({
who: "addContent",
message: "Ack! Inconsistency!",
Expand Down
4 changes: 2 additions & 2 deletions src/dependency/supportedMerge.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { implies, merge, type Contradiction } from "../merge/index.js"
import { implies, merge, type MergeConflict } from "../merge/index.js"
import { setIsSubsetOf, setUnion } from "../utils/Set.js"
import { Supported } from "./Supported.js"

export function supportedMerge<A, B>(
content: Supported<A>,
increment: Supported<B>,
): Supported<A | B> | Contradiction {
): Supported<A | B> | MergeConflict {
const mergedValue = merge(content.value, increment.value)

// 这里的 cases 可以写成更对称的样子,
Expand Down
11 changes: 0 additions & 11 deletions src/merge/Contradiction.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/merge/MergeConflict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type MergeConflict = {
"@type": "MergeConflict"
}

export const theMergeConflict: MergeConflict = {
"@type": "MergeConflict",
}

export function isMergeConflict(x: any): x is MergeConflict {
return x === theMergeConflict
}
2 changes: 1 addition & 1 deletion src/merge/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./Contradiction.js"
export * from "./MergeConflict.js"
export * from "./merge.js"
10 changes: 5 additions & 5 deletions src/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
isInterval,
} from "../interval/index.js"
import { isNumber } from "../utils/isNumber.js"
import { theContradiction } from "./Contradiction.js"
import { theMergeConflict } from "./MergeConflict.js"

// # The contract of `merge`
// The contract of the generic function merge is that it takes two
Expand Down Expand Up @@ -63,23 +63,23 @@ defineHandler(merge, [isAnything, isNothing], (content, increment) => content)
defineHandler(merge, [isNothing, isAnything], (content, increment) => increment)

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

defineHandler(merge, [isInterval, isInterval], (content, increment) => {
const newInterval = intervalIntersect(content, increment)
if (intervalEqual(newInterval, content)) return content
if (intervalEqual(newInterval, increment)) return increment
if (intervalIsEmpty(newInterval)) return theContradiction
if (intervalIsEmpty(newInterval)) return theMergeConflict
return newInterval
})

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

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

function isSimple(x: any): boolean {
Expand Down

0 comments on commit 025864d

Please sign in to comment.