rename some things

This commit is contained in:
Joeri Exelmans 2025-04-20 21:09:51 +02:00
parent e04cac4f7d
commit 8a4bd44f04
16 changed files with 92 additions and 60 deletions

View file

@ -1,7 +1,7 @@
import { Bottom } from "../primitives/types.js";
import { capitalizeFirstLetter } from "../util/util.js";
import { constructorProduct, getLeft, getRight } from "./product.js";
import { constructorLeft, constructorRight, match } from "./sum.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.
@ -34,7 +34,7 @@ export const makeMatchFn = variants => {
const [_, ...remainingVariants] = variants;
return sum => handler => {
return (
match(sum)(constructorProduct
match(sum)(newProduct
(leftValue => eatParameters(remainingVariants.length, handler(leftValue)))
(rightValue => makeMatchFn(remainingVariants)(rightValue))
));
@ -48,10 +48,10 @@ export const makeConstructors = variants => {
const [variant, ...remainingVariants] = variants;
const name = getLeft(variant);
const ctorName = `new${capitalizeFirstLetter(name)}`;
const constructor = { [ctorName]: val => constructorLeft(val) }[ctorName];
const constructor = { [ctorName]: val => newLeft(val) }[ctorName];
return [
constructor,
...makeConstructors(remainingVariants).map(ctor =>
({[ctor.name]: val => constructorRight(ctor(val))}[ctor.name])),
({[ctor.name]: val => newRight(ctor(val))}[ctor.name])),
];
}

View file

@ -1,9 +1,9 @@
import { fnType, typedFnType } from "./types.js";
import { typedFnType } from "./types.js";
import { Char, GenericType, Type } from "../primitives/types.js";
import { Int } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
import { lsType } from "./types.js";
import { Typed } from "../typed.js"
import { Dynamic } from "../primitives/dynamic.js"
// 'normal' implementation
export const emptyList = {l:[]};
@ -22,7 +22,7 @@ export const fold = ls => callback => initial => {
}
export const String = lsType(Char); // alias
export const Module = lsType(Typed);
export const Module = lsType(Dynamic);
export const ModuleList = {l:[
// Type -> Type

View file

@ -9,7 +9,7 @@ import { typedFnType } from "./types.js";
import { prodType } from "./types.js";
// In JS, all products are encoded in the same way:
export const constructorProduct = l => r => ({l, r});
export const newProduct = l => r => ({l, r});
export const getLeft = product => product.l;
export const getRight = product => product.r;
@ -26,7 +26,7 @@ export const ModuleProduct = {l: [
),
// a -> b -> (a, b)
...typedFnType(constructorProduct, fnType =>
...typedFnType(newProduct, fnType =>
makeGeneric((a, b) =>
fnType
(a)

View file

@ -1,7 +1,7 @@
import { Unit } from "../primitives/types.js";
import { unit } from "../primitives/unit.js";
import { capitalizeFirstLetter } from "../util/util.js";
import { constructorProduct, getLeft, getRight } from "./product.js";
import { newProduct, getLeft, getRight } from "./product.js";
import { fnType, prodType } from "./types.js";
// 'fields' is an array of (name: string, type: Type) pairs.
@ -26,7 +26,7 @@ export const makeConstructor = fields => {
return nextParam => {
const wrappedName = 'wrapped_' + ret.name;
const newRet = {
[wrappedName]: inner => constructorProduct(nextParam)(ret(inner)),
[wrappedName]: inner => newProduct(nextParam)(ret(inner)),
}[wrappedName];
return internal(nParams-1, newRet);
}

View file

@ -8,8 +8,8 @@ import { typedFnType } from "./types.js";
import { makeGeneric } from "../generics/generics.js";
import { sumType } from "./types.js";
export const constructorLeft = left => ({t: "L", v: left }); // 't': tag, 'v': value
export const constructorRight = right => ({t: "R", v: right});
export const newLeft = left => ({t: "L", v: left }); // 't': tag, 'v': value
export const newRight = right => ({t: "R", v: right});
// signature:
// sum-type -> (leftType -> resultType, rightType -> resultType) -> resultType
@ -31,7 +31,7 @@ export const ModuleSum = {l:[
),
// a -> a | b
...typedFnType(constructorLeft, fnType =>
...typedFnType(newLeft, fnType =>
makeGeneric((a, b) =>
fnType
(a)
@ -39,7 +39,7 @@ export const ModuleSum = {l:[
), GenericType),
// b -> a | b
...typedFnType(constructorRight, fnType =>
...typedFnType(newRight, fnType =>
makeGeneric((a, b) =>
fnType
(b)