fix error in static merge algorithm. progress with merging of dictionary-operations

This commit is contained in:
Joeri Exelmans 2025-06-16 14:34:24 +02:00
parent 5d028fe030
commit d5821c9cb9
4 changed files with 115 additions and 71 deletions

View file

@ -15,7 +15,7 @@ export const emptyDict = compareFn => RBTreeWrapper.new((x, y) => compareFn(x)(y
export const has = dict => key => dict.tree.get(key) !== undefined; export const has = dict => key => dict.tree.get(key) !== undefined;
export const get = dict => key => dict.tree.get(key); export const get = dict => key => dict.tree.get(key);
export const set = dict => key => value => new RBTreeWrapper(dict.tree.remove(key).insert(key, value), inspectDict); export const set = key => value => dict => new RBTreeWrapper(dict.tree.remove(key).insert(key, value), inspectDict);
export const remove = dict => key => new RBTreeWrapper(dict.tree.remove(key), inspectDict); export const remove = dict => key => new RBTreeWrapper(dict.tree.remove(key), inspectDict);
export const length = dict => dict.tree.length; export const length = dict => dict.tree.length;

View file

@ -15,7 +15,7 @@ const mkType = makeTypeParser({
export const ModuleDict = [ export const ModuleDict = [
["emptyDict", newDynamic(emptyDict)(mkType("(a -> a -> Ordering) -> (a => b) "))], ["emptyDict", newDynamic(emptyDict)(mkType("(a -> a -> Ordering) -> (a => b) "))],
["has" , newDynamic(has )(mkType("(a => b) -> a -> Bool "))], ["has" , newDynamic(has )(mkType("(a => b) -> a -> Bool "))],
["set" , newDynamic(set )(mkType("(a => b) -> a -> b -> (a => b) "))], ["set" , newDynamic(set )(mkType("a -> b -> (a => b) -> (a => b) "))],
["remove" , newDynamic(remove )(mkType("(a => b) -> a -> (a => b) "))], ["remove" , newDynamic(remove )(mkType("(a => b) -> a -> (a => b) "))],
["length" , newDynamic(length )(mkType("(a => b) -> Int "))], ["length" , newDynamic(length )(mkType("(a => b) -> Int "))],
["fold" , newDynamic(fold )(mkType("(c -> a -> b -> c) -> c -> (a => b) -> c "))], ["fold" , newDynamic(fold )(mkType("(c -> a -> b -> c) -> c -> (a => b) -> c "))],

View file

@ -1,22 +1,23 @@
import { emptySet, add, length, forEach, has, remove } from "../../structures/set.js"; import { emptySet, add, length, forEach, has, remove } from "../../structures/set.js";
import { getDepth, Slot } from "./value_slot.js"; import { getDepth, Slot } from "./value_slot.js";
import { compareSlot } from "../compare.js"; import { compareSlot } from "./compare.js";
import { makeTypeParser } from "../../parser/type_parser.js"; import { makeTypeParser } from "../../parser/type_parser.js";
import { newDynamic } from "../../primitives/dynamic.js"; import { newDynamic } from "../../primitives/dynamic.js";
const emptySetOfSlots = emptySet(compareSlot); const emptySetOfSlots = compareElems => emptySet(compareSlot(compareElems));
export const findLCA = slotA => slotB => { export const findLCA = compareElems => slotA => slotB => {
if (compareSlot(slotA)(slotB) === 0) { console.log('compareSlot...', slotA, slotB);
if (compareSlot(compareElems)(slotA)(slotB) === 0) {
return slotA; return slotA;
} }
if (getDepth(slotA) > getDepth(slotB)) { if (getDepth(slotA) > getDepth(slotB)) {
// we can assume that slotA.variant === "write" // we can assume that slotA.variant === "write"
return findLCA(slotA.value.slot)(slotB); return findLCA(compareElems)(slotA.value.slot)(slotB);
} }
else { else {
// we can assume that slotB.variant === "write" // we can assume that slotB.variant === "write"
return findLCA(slotA)(slotB.value.slot); return findLCA(compareElems)(slotA)(slotB.value.slot);
} }
}; };
@ -24,26 +25,27 @@ export const findLCA = slotA => slotB => {
// {slotA} if slotA is younger // {slotA} if slotA is younger
// {slotB} if slotB is younger // {slotB} if slotB is younger
// {slotA, slotB} if they are concurrent (conflicting) // {slotA, slotB} if they are concurrent (conflicting)
export const merge = slotA => slotB => { export const merge = compareElems => slotA => slotB => {
const lca = findLCA(slotA)(slotB); const lca = findLCA(compareElems)(slotA)(slotB);
if (compareSlot(lca)(slotA) === 0) { const emptySet = emptySetOfSlots(compareElems);
return add(emptySetOfSlots)(slotB); if (compareSlot(compareElems)(lca)(slotA) === 0) {
return add(emptySet)(slotB);
} }
if (compareSlot(lca)(slotB) === 0) { if (compareSlot(compareElems)(lca)(slotB) === 0) {
return add(emptySetOfSlots)(slotA); return add(emptySet)(slotA);
} }
const setWithA = add(emptySetOfSlots)(slotA); const setWithA = add(emptySet)(slotA);
const setWithAandB = add(setWithA)(slotB); const setWithAandB = add(setWithA)(slotB);
return setWithAandB; return setWithAandB;
}; };
export const mergeN = slots => { export const mergeN = compareElems => slots => {
let result = slots; let result = slots;
forEach(slots)(slotA => { forEach(slots)(slotA => {
forEach(slots)(slotB => { forEach(slots)(slotB => {
// compare all non-identical pairs // compare all non-identical pairs
if (compareSlot(slotA)(slotB) !== 0) { if (compareSlot(compareElems)(slotA)(slotB) !== 0) {
const m = merge(slotA)(slotB); const m = merge(compareElems)(slotA)(slotB);
if (length(m) === 1) { if (length(m) === 1) {
// if in the pair, one is an ancestor of the other, // if in the pair, one is an ancestor of the other,
// only keep the other // only keep the other

View file

@ -2,87 +2,129 @@ import { newDynamic } from "../lib/primitives/dynamic.js";
import { Char, Int } from "../lib/primitives/primitive_types.js"; import { Char, Int } from "../lib/primitives/primitive_types.js";
import { fnType } from "../lib/structures/type_constructors.types.js"; import { fnType } from "../lib/structures/type_constructors.types.js";
import { pretty } from "../lib/util/pretty.js"; import { pretty } from "../lib/util/pretty.js";
import { newLiteral, newSlot, read, transform, write } from "../lib/versioning/value_slot.js"; import { newLiteral, newSlot, read, transform, write } from "../lib/versioning/static/value_slot.js";
import { merge, mergeN } from "../lib/versioning/merge.js"; import { merge, mergeN } from "../lib/versioning/static/merge.js";
import { union } from "../lib/structures/set.js"; import { union } from "../lib/structures/set.js";
import { emptyDict, set } from "../lib/structures/dict.js";
import { compareInts, compareStrings } from "../lib/compare/primitives.js";
import { compareDicts } from "../lib/compare/structures.js";
const inc = x => x + 1n; const inc = x => x + 1n;
const incLiteral = newLiteral(newDynamic(inc)(fnType(_=>Int)(_=>Int))); const incLiteral = newLiteral(newDynamic(inc)(fnType(_=>Int)(_=>Int)));
console.log("##############"); // {
console.log("## Counting ##"); // console.log("##############");
console.log("##############"); // console.log("## Counting ##");
// console.log("##############");
const counterOneSlot = write // const counterOneSlot = write
(newSlot("1d61577e704613f4e48b164852aedd20")) // (newSlot("1d61577e704613f4e48b164852aedd20"))
(newLiteral(newDynamic(1n)(Int))); // (newLiteral(newDynamic(1n)(Int)));
console.log(pretty({counterOneSlot})); // console.log(pretty({counterOneSlot}));
const valueOne = read(counterOneSlot); // const valueOne = read(counterOneSlot);
console.log(pretty({valueOne})); // console.log(pretty({valueOne}));
const onePlusOne = transform // const onePlusOne = transform
(valueOne) // (valueOne)
(incLiteral); // (incLiteral);
console.log(pretty({onePlusOne})); // console.log(pretty({onePlusOne}));
const onePlusOnePlusOne = transform // const onePlusOnePlusOne = transform
(onePlusOne) // (onePlusOne)
(incLiteral); // (incLiteral);
console.log(pretty({onePlusOnePlusOne})); // console.log(pretty({onePlusOnePlusOne}));
// }
// // console.log("#############");
// // console.log("## Summing ##");
// // console.log("#############");
// // const priceSlot = newSlot(Symbol('price'))(newLiteral(20.66));
// // const taxSlot = newSlot(Symbol('tax'))(newLiteral(4.34));
// // const total =
// // transform(read(priceSlot))(
// // transform(read(taxSlot))(
// // newLiteral(tax => price => price + tax)));
// // console.log(pretty({total}))
// // const totalPlusOne = transform(total)(newLiteral(inc));
// // console.log(pretty({totalPlusOne}));
// // verifyValue(totalPlusOne);
// // console.log("getReadDependencies(totalPlusOne):", getReadDependencies(totalPlusOne));
// {
// console.log("###############");
// console.log("## Branching ##");
// console.log("###############");
// console.log("#############"); // const initialSlot = newSlot("Slot__4a029b3d758bcd1fffbf495531c95537");
// console.log("## Summing ##");
// console.log("#############");
// const priceSlot = newSlot(Symbol('price'))(newLiteral(20.66)); // const slotA = write(initialSlot)(newLiteral(newDynamic("a")(Char)));
// const taxSlot = newSlot(Symbol('tax'))(newLiteral(4.34)); // const slotB = write(slotA )(newLiteral(newDynamic("b")(Char)));
// const slotC = write(initialSlot)(newLiteral(newDynamic("c")(Char)));
// const total = // const slotAB = merge(slotA)(slotB);
// transform(read(priceSlot))( // const slotAC = merge(slotA)(slotC);
// transform(read(taxSlot))( // const slotBC = merge(slotB)(slotC);
// newLiteral(tax => price => price + tax)));
// console.log(pretty({total})) // console.log({slotAB, slotAC, slotBC});
// const totalPlusOne = transform(total)(newLiteral(inc)); // const un = union(slotAC)(slotBC);
// console.log(pretty({totalPlusOne})); // const merged = mergeN(un);
// verifyValue(totalPlusOne); // console.log("merged:", merged);
// }
// console.log("getReadDependencies(totalPlusOne):", getReadDependencies(totalPlusOne)); {
console.log("#################");
console.log("## Dict insert ##");
console.log("#################");
const dictSlot = write
(newSlot("MyDictSlot__d2e87499d9cf46b5c86d420c2057f52f"))
(newLiteral(emptyDict(compareStrings)));
const insertedX = write(dictSlot)
(transform
(read(dictSlot)) // input
(transform // fn (set "x" => 1n)
(newLiteral(1n))
(transform
(newLiteral("x"))
(newLiteral(set))
)));
const insertedY = write(dictSlot)
(transform
(read(dictSlot)) // input
(transform // fn (set "y" => 2n)
(newLiteral(2n))
(transform
(newLiteral("y"))
(newLiteral(set))
)));
console.log("###############"); const merged = merge
console.log("## Branching ##"); (compareDicts(compareStrings)(compareInts))
console.log("###############"); (insertedX)
(insertedY);
const initialSlot = newSlot("Slot__4a029b3d758bcd1fffbf495531c95537"); // is a conflict, but shouldn't be one...
// can we add the 'domain knowledge' that the composition of two non-key-overlapping dict.set-operations is commutative?
const slotA = write(initialSlot)(newLiteral(newDynamic("a")(Char))); console.log({merged});
const slotB = write(slotA )(newLiteral(newDynamic("b")(Char))); }
const slotC = write(initialSlot)(newLiteral(newDynamic("c")(Char)));
const slotAB = merge(slotA)(slotB);
const slotAC = merge(slotA)(slotC);
const slotBC = merge(slotB)(slotC);
console.log({slotAB, slotAC, slotBC});
const un = union(slotAC)(slotBC);
const merged = mergeN(un);
console.log("merged:", merged);
// // console.log(compareSlots(intCompare)(fiveSlot)(fiveSlot)); // // console.log(compareSlots(intCompare)(fiveSlot)(fiveSlot));
// // console.log(compareSlots(intCompare)(sixSlot)(sixSlot)); // // console.log(compareSlots(intCompare)(sixSlot)(sixSlot));