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

View file

@ -1,5 +1,5 @@
import { fnType, typedFnType } from "./types.js";
import { Type } from "../primitives/types.js";
import { typedFnType } from "./types.js";
import { Char, Type } from "../primitives/types.js";
import { Int } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
import { lsType } from "./types.js";
@ -10,6 +10,8 @@ const get = ls => i => ls.l[i];
const put = ls => i => elem => ({l: ls.l.with(Number(i), elem)});
const push = ls => elem => ({l:ls.l.concat([elem])});
export const String = lsType(Char); // alias
export const ModuleList = {l:[
// Type -> Type
...typedFnType(lsType, fnType =>
@ -55,4 +57,6 @@ export const ModuleList = {l:[
)
)
),
// {i: String, t: Type}, // alias
]};

13
structures/nominal.js Normal file
View file

@ -0,0 +1,13 @@
import { Any } from "../typed.js";
import { String } from "./list.js";
import { sumType, prodType, fnType } from "./types.js";
export const createNominalADT = symbol => variants => {
};
export const createNominalADTFnType =
fnType
(Any)
();

38
structures/set.js Normal file
View file

@ -0,0 +1,38 @@
import { setType, typedFnType } from "./types.js";
import { Bool, Type } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
// 'normal' implementation
const emptySet = new Set();
const has = set => elem => set.has(elem);
const add = set => elem => new Set([...set, elem]);
export const ModuleList = {l:[
// Type -> Type
...typedFnType(setType, fnType =>
fnType
/* in */ (Type)
/* out */ (Type)
),
{i: emptySet, t: makeGeneric(a => setType(a))},
...typedFnType(has, fnType =>
makeGeneric(a =>
fnType
/* in */ (setType(a))
/* out */ (fnType
/* in */ (a)
/* out */ (Bool)
))),
...typedFnType(add, fnType =>
makeGeneric(a =>
fnType
/* in */ (setType(a))
/* out */ (fnType
/* in */ (a)
/* out */ (setType(a))
))),
]};

View file

@ -1,21 +1,17 @@
// to break up dependency cycles, type constructors are defined in their own JS module
import { Type } from "../primitives/types.js";
import { DefaultMap } from "../util.js";
import { getSymbol, makeTypeConstructor } from "../type_constructor.js";
// Function type
// The registry ensures that we never accidentally create more than one JS object for the same function type.
// It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions.
// This same pattern is repeated throughout the code for all non-nullary type constructors (list, sum, product, ...)
export const symbolFunction = Symbol('Function');
const fnTypeRegistry = new DefaultMap(inType => new DefaultMap(outType => ({
symbol: symbolFunction,
params: [inType, outType],
})));
// type constructor
export const fnType = inType => outType => fnTypeRegistry.getdefault(inType, true).getdefault(outType, true);
const symbolFunction = Symbol('Function');
export const fnType = makeTypeConstructor(symbolFunction, 2);
export const isFunction = type => getSymbol(type) === symbolFunction;
// Convenience function. Wrapper around function below.
export const typedFnType = (instance, callback) => {
@ -41,35 +37,48 @@ export const typedFnType2 = callback => {
];
};
// Sum type
export const symbolSum = Symbol("Sum");
const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({
symbol: symbolSum,
params: [leftType, rightType],
})));
// type constructor
export const sumType = leftType => rightType => sumTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
const symbolSum = Symbol("Sum");
export const sumType = makeTypeConstructor(symbolSum, 2);
// Product type
export const symbolProduct = Symbol("Product");
const productTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({
symbol: symbolProduct,
params: [leftType, rightType],
})));
// type constructor
export const prodType = leftType => rightType => productTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
const symbolProduct = Symbol("Product");
export const prodType = makeTypeConstructor(symbolProduct, 2);
// List type
export const symbolList = Symbol('List');
const listTypeRegistry = new DefaultMap(elementType => ({
symbol: symbolList,
params: [elementType],
}));
// type constructor
export const lsType = elementType => listTypeRegistry.getdefault(elementType, true);
const symbolList = Symbol('List');
export const lsType = makeTypeConstructor(symbolList, 1);
// Set type
const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet, 1);
// Pretty print type
export function prettyT(type) {
if (type.typeVars) {
if (type.typeVars.size > 0) {
return `${[...type.typeVars].map(prettyT).join(", ")}: ${prettyT(type.type)}`;
}
return prettyT(type.type);
}
if (type.symbol === symbolFunction) {
return `${prettyT(type.params[0])} -> ${prettyT(type.params[1])}`;
}
if (type.symbol === symbolList) {
return `[${prettyT(type.params[0])}]`;
}
if (type.symbol === symbolProduct) {
return `(${prettyT(type.params[0])}, ${prettyT(type.params[1])})`;
}
if (type.symbol === symbolSum) {
return `(${prettyT(type.params[0])} | ${prettyT(type.params[1])})`;
}
if (type.params.length === 0) {
return type.symbol.description;
}
return `${type.symbol.description}(${type.params.map(prettyT).join(", ")})`;
}

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))
)
);