reorganize directory and file structure
This commit is contained in:
parent
1d826ea8d4
commit
48390b8556
99 changed files with 1155 additions and 1629 deletions
|
|
@ -1,57 +0,0 @@
|
|||
import { Bottom } from "../primitives/types.js";
|
||||
import { capitalizeFirstLetter } from "../util/util.js";
|
||||
import { newProduct as newProduct, getLeft, getRight } from "./product.js";
|
||||
import { newLeft, newRight, match } from "./sum.js";
|
||||
import { sumType } from "./types.js";
|
||||
|
||||
// 'variants' is an array of (name: string, type: Type) pairs.
|
||||
// e.g., the list of variants:
|
||||
// [ { l: "price" , r: Int },
|
||||
// { l: "prices" , r: [Int] },
|
||||
// { l: "not_found", r: Unit } ]
|
||||
// results in the type:
|
||||
// (Int | ([Int] | (Unit | ⊥)))
|
||||
export const enumType = variants => {
|
||||
if (variants.length === 0) {
|
||||
return Bottom; // empty enum is equal to Bottom-type (cannot be instantiated)
|
||||
}
|
||||
const [variant, ...rest] = variants;
|
||||
const variantType = getRight(variant);
|
||||
return sumType(() => variantType)(() => enumType(rest));
|
||||
};
|
||||
|
||||
const eatParameters = (numParams, result) => {
|
||||
if (numParams === 0) {
|
||||
return result;
|
||||
}
|
||||
else return () => eatParameters(numParams-1, result);
|
||||
}
|
||||
|
||||
export const makeMatchFn = variants => {
|
||||
if (variants.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const [_, ...remainingVariants] = variants;
|
||||
return sum => handler => {
|
||||
return (
|
||||
match(sum)(newProduct
|
||||
(leftValue => eatParameters(remainingVariants.length, handler(leftValue)))
|
||||
(rightValue => makeMatchFn(remainingVariants)(rightValue))
|
||||
));
|
||||
};
|
||||
};
|
||||
|
||||
export const makeConstructors = variants => {
|
||||
if (variants.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const [variant, ...remainingVariants] = variants;
|
||||
const name = getLeft(variant);
|
||||
const ctorName = `new${capitalizeFirstLetter(name)}`;
|
||||
const constructor = { [ctorName]: val => newLeft(val) }[ctorName];
|
||||
return [
|
||||
constructor,
|
||||
...makeConstructors(remainingVariants).map(ctor =>
|
||||
({[ctor.name]: val => newRight(ctor(val))}[ctor.name])),
|
||||
];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue