progress and some refactoring

This commit is contained in:
Joeri Exelmans 2025-03-31 15:35:02 +02:00
parent d236eca5e5
commit d8ca2f3999
25 changed files with 376 additions and 163 deletions

63
structures/versioned.js Normal file
View file

@ -0,0 +1,63 @@
import { makeGeneric } from "../generics/generics.js";
import { Bool } from "../primitives/types.js";
import { makeTypeConstructor } from "../type_constructor.js";
import { getEq } from "../typeclasses/eq.js";
import { eqDictType } from "../typeclasses/eq_type.js";
import { fnType, setType } from "./types.js";
const symbolVersioned = Symbol("Versioned");
export const versionedType = makeTypeConstructor(symbolVersioned, 1);
export const constructor = parents => value => {
return { parents, alternatives };
}
const constructorType = makeGeneric(a =>
fnType
(a)
(setType(versionedType(a)))
);
// const getValue = v =>
const eq = eqDict => vA => vB => {
return getEq(eqDict)(vA.value,vB.value) // compare values
&& (vA.parents.symmetricDifference(vB.parents).size === 0); // compare parents
}
// EqDict a -> Versioned a -> Versioned a -> Bool
const eqVersioned = makeGeneric(a =>
fnType
(eqDictType(a))
(fnType
(versionedType(a))
(fnType
(versionedType(a))
(Bool)
)
)
);
// EqDict a -> Versioned a -> Versioned a -> Versioned a -> Versioned a
export const mergeThreeWay = eqDict => vLCA => vA => vB => {
if (eq(eqDict)(vLCA)(vA)) {
return vB; // vB successor of vA
}
if (eq(eqDict)(vLCA)(vB)) {
return vA; // vA successor of vB
}
return mergeConcurrent(vLCA)(vA)(vB);
};
export const mergePrimitives = vA => vB => vA.union(vB);
// Versioned a -> Versioned a -> Versioned a
export const mergePrimitivesType = makeGeneric(a =>
fnType
(versionedType(a))
(fnType
(versionedType(a))
(versionedType(a))
)
);