branching and very basic merging of slots
This commit is contained in:
parent
614e6c0fdb
commit
3978f7f835
32 changed files with 684 additions and 420 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { typedFnType } from "./types.js";
|
||||
import { fnType, typedFnType } from "./types.js";
|
||||
import { Char, GenericType, Type } from "../primitives/types.js";
|
||||
import { Int } from "../primitives/types.js";
|
||||
import { makeGeneric } from "../generics/generics.js";
|
||||
|
|
@ -11,6 +11,7 @@ const emptyListType = makeGeneric(a => lsType(a));
|
|||
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])});
|
||||
const map = ls => fn => ({ l: ls.l.map(elem => fn(elem)) });
|
||||
|
||||
export const String = lsType(Char); // alias
|
||||
export const Module = lsType(Typed);
|
||||
|
|
@ -60,4 +61,14 @@ export const ModuleList = {l:[
|
|||
(lsType(a))
|
||||
)
|
||||
), GenericType),
|
||||
|
||||
// [a] -> (a -> b) -> [b]
|
||||
...typedFnType(map, fnType =>
|
||||
makeGeneric((a, b) =>
|
||||
fnType
|
||||
(lsType(a))
|
||||
(fnType
|
||||
(fnType(a)(b))
|
||||
(lsType(b))
|
||||
)), GenericType),
|
||||
]};
|
||||
|
|
|
|||
|
|
@ -1,76 +0,0 @@
|
|||
import { SymbolT, Type } from "../primitives/types.js";
|
||||
import { makeTypeConstructor } from "../type_constructor.js";
|
||||
import { Module, String } from "./list.js";
|
||||
import { prodType, fnType, lsType } from "./types.js";
|
||||
|
||||
function capitalizeFirstLetter(val) {
|
||||
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
|
||||
}
|
||||
|
||||
export const createStruct = (typeVars, symbol, fields) => {
|
||||
const makeConstructor = (remainingFields, obj={}) => {
|
||||
if (remainingFields.length===0) {
|
||||
return obj;
|
||||
}
|
||||
const {left: fieldName} = remainingFields[remainingFields.length-1];
|
||||
return v => makeConstructor(
|
||||
remainingFields.slice(0,-1),
|
||||
Object.assign({[fieldName]: v}, obj));
|
||||
};
|
||||
const constructor = makeConstructor(fields);
|
||||
|
||||
const type = makeTypeConstructor(symbol)(typeVars.size);
|
||||
const types = [ type ];
|
||||
const recordFnType = inType => outType => {
|
||||
const fnT = fnType(inType)(outType);
|
||||
types.push(fnT);
|
||||
return fnT;
|
||||
}
|
||||
|
||||
const makeConstructorType = (remainingFields, type) => {
|
||||
if (remainingFields.length===0) {
|
||||
return type;
|
||||
}
|
||||
const {right: fieldType} = remainingFields[remainingFields.length-1];
|
||||
return recordFnType(makeConstructorType(remainingFields.slice(0,-1)))(fieldType);
|
||||
};
|
||||
const constructorType = makeConstructorType(fields);
|
||||
|
||||
const functions = [
|
||||
["constructor", constructor, constructorType],
|
||||
...fields.map(({left: fieldName, right: fieldType}) => {
|
||||
const getterName = 'get'+capitalizeFirstLetter(fieldName);
|
||||
const getter = {
|
||||
// stupid trick to give the JS-function a computed name.
|
||||
// only important for debugging, so it says [Function: getAge] instead of [Function (anonymous)]:
|
||||
[getterName]: obj => obj[fieldName],
|
||||
}[getterName];
|
||||
if (typeVars.has(fieldType)) {
|
||||
// getterFnType = recordFnType(type)(fieldType)
|
||||
}
|
||||
const getterFnType = recordFnType(type)(fieldType);
|
||||
return [fieldName, getter, getterFnType];
|
||||
}),
|
||||
];
|
||||
|
||||
const module = {l:[
|
||||
{i: type, t: Type},
|
||||
|
||||
...functions.flatMap(([_, getter, getterFnType]) => [
|
||||
{i: getter , t: getterFnType},
|
||||
]),
|
||||
|
||||
...types.map(type => ({i: type, t: Type})),
|
||||
]};
|
||||
|
||||
return {
|
||||
module,
|
||||
constructor,
|
||||
functions: Object.fromEntries(functions),
|
||||
};
|
||||
};
|
||||
|
||||
export const createNominalADTModuleFnType =
|
||||
fnType(SymbolT)
|
||||
(fnType(lsType(prodType(String)(Type)))
|
||||
(Module));
|
||||
|
|
@ -4,9 +4,9 @@ import { typedFnType } from "./types.js";
|
|||
import { prodType } from "./types.js";
|
||||
|
||||
// In JS, all products are encoded in the same way:
|
||||
const constructor = left => right => ({left, right});
|
||||
const getLeft = product => product.left;
|
||||
const getRight = product => product.right;
|
||||
export const constructorProduct = l => r => ({l, r});
|
||||
export const getLeft = product => product.l;
|
||||
export const getRight = product => product.r;
|
||||
|
||||
export const ModuleProduct = {l: [
|
||||
// binary type constructor
|
||||
|
|
@ -21,7 +21,7 @@ export const ModuleProduct = {l: [
|
|||
),
|
||||
|
||||
// a -> b -> (a, b)
|
||||
...typedFnType(constructor, fnType =>
|
||||
...typedFnType(constructorProduct, fnType =>
|
||||
makeGeneric((a, b) =>
|
||||
fnType
|
||||
(a)
|
||||
|
|
|
|||
|
|
@ -1,39 +1,60 @@
|
|||
import { setType, typedFnType } from "./types.js";
|
||||
import { Bool, GenericType, Type } from "../primitives/types.js";
|
||||
import { fnType, setType } from "./types.js";
|
||||
import { Int } from "../primitives/types.js";
|
||||
import { makeGeneric } from "../generics/generics.js";
|
||||
|
||||
// 'normal' implementation
|
||||
const emptySet = new Set();
|
||||
const emptySetType = makeGeneric(a => setType(a));
|
||||
const has = set => elem => set.has(elem);
|
||||
const add = set => elem => new Set([...set, elem]);
|
||||
import createRBTree from "functional-red-black-tree";
|
||||
import { inspect } from "node:util";
|
||||
|
||||
export class RBTreeWrapper {
|
||||
constructor(tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
[inspect.custom](depth, options, inspect) {
|
||||
const entries = [];
|
||||
this.tree.forEach((key,val) => {entries.push(`${inspect(key)} => ${inspect(val)}`);});
|
||||
return `RBTree(${this.tree.length}) {${entries.join(', ')}}`;
|
||||
}
|
||||
}
|
||||
|
||||
// (a -> a -> Int) -> Set(a)
|
||||
export const emptySet = compareFn => new RBTreeWrapper(createRBTree((x, y) => compareFn(x)(y)));
|
||||
|
||||
// const emptySetType = makeGeneric(a => fnType(fnType(a)(fnType(a)(Int)))(setType(a)));
|
||||
|
||||
export const has = set => key => set.tree.get(key) === true;
|
||||
export const add = set => key => set.tree.get(key) === true ? set : new RBTreeWrapper(set.tree.insert(key, true));
|
||||
export const remove = set => key => new RBTreeWrapper(set.tree.remove(key));
|
||||
|
||||
export const forEach = set => fn => {
|
||||
set.tree.forEach(key => { fn(key); });
|
||||
};
|
||||
|
||||
export const ModuleSet = {l:[
|
||||
// Type -> Type
|
||||
...typedFnType(setType, fnType =>
|
||||
fnType
|
||||
/* in */ (Type)
|
||||
/* out */ (Type)
|
||||
),
|
||||
// // Type -> Type
|
||||
// ...typedFnType(setType, fnType =>
|
||||
// fnType
|
||||
// /* in */ (Type)
|
||||
// /* out */ (Type)
|
||||
// ),
|
||||
|
||||
{i: emptySet , t: emptySetType},
|
||||
{i: emptySetType, t: GenericType },
|
||||
// {i: emptySet , t: emptySetType},
|
||||
// {i: emptySetType, t: GenericType },
|
||||
|
||||
...typedFnType(has, fnType =>
|
||||
makeGeneric(a =>
|
||||
fnType
|
||||
/* in */ (setType(a))
|
||||
/* out */ (fnType
|
||||
/* in */ (a)
|
||||
/* out */ (Bool)
|
||||
)), GenericType),
|
||||
// ...typedFnType(has, fnType =>
|
||||
// makeGeneric(a =>
|
||||
// fnType
|
||||
// /* in */ (setType(a))
|
||||
// /* out */ (fnType
|
||||
// /* in */ (a)
|
||||
// /* out */ (Bool)
|
||||
// )), GenericType),
|
||||
|
||||
...typedFnType(add, fnType =>
|
||||
makeGeneric(a =>
|
||||
fnType
|
||||
/* in */ (setType(a))
|
||||
/* out */ (fnType
|
||||
/* in */ (a)
|
||||
/* out */ (setType(a))
|
||||
)), GenericType),
|
||||
// ...typedFnType(add, fnType =>
|
||||
// makeGeneric(a =>
|
||||
// fnType
|
||||
// /* in */ (setType(a))
|
||||
// /* out */ (fnType
|
||||
// /* in */ (a)
|
||||
// /* out */ (setType(a))
|
||||
// )), GenericType),
|
||||
]};
|
||||
|
|
|
|||
65
structures/struct.js
Normal file
65
structures/struct.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { Unit } from "../primitives/types.js";
|
||||
import { unit } from "../primitives/unit.js";
|
||||
import { constructorProduct, getLeft, getRight } from "./product.js";
|
||||
import { fnType, prodType } from "./types.js";
|
||||
|
||||
function capitalizeFirstLetter(val) {
|
||||
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
|
||||
}
|
||||
|
||||
// [{l: "x", r: Double}, {l: "y", r: Double}] => (Double × (Double × Unit))
|
||||
export const structType = fields => {
|
||||
if (fields.length === 0) {
|
||||
return Unit;
|
||||
}
|
||||
const [field, ...rest] = fields;
|
||||
const fieldType = getRight(field);
|
||||
return prodType(fieldType)(structType(rest));
|
||||
};
|
||||
|
||||
export const makeConstructor = fields => {
|
||||
const internal = (nParams, ret) => {
|
||||
if (nParams === 0) {
|
||||
const result = ret(unit);
|
||||
return result;
|
||||
}
|
||||
return nextParam => {
|
||||
const wrappedName = 'wrapped_' + ret.name;
|
||||
const newRet = {
|
||||
[wrappedName]: inner => constructorProduct(nextParam)(ret(inner)),
|
||||
}[wrappedName];
|
||||
return internal(nParams-1, newRet);
|
||||
}
|
||||
};
|
||||
const id = x => x;
|
||||
return internal(fields.length, id);
|
||||
};
|
||||
|
||||
export const makeConstructorType = type => fields => {
|
||||
if (fields.length === 0) {
|
||||
return type;
|
||||
}
|
||||
const [field, ...rest] = fields;
|
||||
const fieldType = getRight(field);
|
||||
return fnType(fieldType)(makeConstructorType(rest));
|
||||
};
|
||||
|
||||
export const makeGetters = fields => {
|
||||
if (fields.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const [field, ...rest] = fields;
|
||||
const fieldName = getLeft(field);
|
||||
const getterName = `get${capitalizeFirstLetter(fieldName)}`;
|
||||
return [
|
||||
{ [getterName]: obj => getLeft(obj) }[getterName],
|
||||
...makeGetters(rest).map(getter => ({[getter.name]: obj => getter(getRight(obj))}[getter.name])),
|
||||
];
|
||||
};
|
||||
|
||||
export const makeGettersTypes = type => fields => {
|
||||
return fields.map(field => {
|
||||
const fieldType = getRight(field);
|
||||
return fnType(type)(fieldType);
|
||||
});
|
||||
};
|
||||
|
|
@ -69,13 +69,13 @@ export function prettyT(type) {
|
|||
}
|
||||
}
|
||||
if (type.symbol === symbolFunction) {
|
||||
return `${prettyT(type.params[0])} -> ${prettyT(type.params[1])}`;
|
||||
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])})`;
|
||||
return `(${prettyT(type.params[0])} × ${prettyT(type.params[1])})`;
|
||||
}
|
||||
if (type.symbol === symbolSum) {
|
||||
return `(${prettyT(type.params[0])} | ${prettyT(type.params[1])})`;
|
||||
|
|
|
|||
|
|
@ -1,68 +0,0 @@
|
|||
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 => alternatives => {
|
||||
return { parents, alternatives };
|
||||
}
|
||||
|
||||
const constructorType = makeGeneric(a =>
|
||||
fnType
|
||||
(setType(versionedType(a)))
|
||||
(fnType
|
||||
(setType(a))
|
||||
(versionedType(a))
|
||||
)
|
||||
);
|
||||
|
||||
const initial = x => ({ parents: new Set(), alternatives: new Set(x) });
|
||||
|
||||
const initialFnType = makeGeneric(a => fnType(a)(versionedType(a)));
|
||||
|
||||
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))
|
||||
)
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue