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