Add product and sum types

This commit is contained in:
Joeri Exelmans 2025-03-17 17:54:42 +01:00
parent 6023efc295
commit 574651ccb7
18 changed files with 209 additions and 157 deletions

View file

@ -1,6 +1,7 @@
import { fnType } from "../function_registry.js";
import { fnType, getListType } from "../type_registry.js";
import {Type, Function} from "../metacircular.js";
import { makeListModule } from "./list_common.js";
import { Module } from "./module.js";
const Type_to_Type = fnType({in: Type, out: Type});
const Type_to_Module = fnType({in: Type, out: Module});

View file

@ -1,22 +1,7 @@
import { fnType } from "../function_registry.js";
import { fnType, getListType } from "../type_registry.js";
import {Type, Function} from "../metacircular.js";
import {Int} from "../primitives/symbols.js";
// Global store of list types:
const listTypes = new Map();
export function getListType(elementType) {
if (listTypes.has(elementType)) {
// only generate each list type once
// this would not be necessary if we could define our own equality and hash functions on objects in JavaScript.
return listTypes.get(elementType);
}
else {
const type = Symbol('ListOf:'+elementType.toString());
listTypes.set(elementType, type);
return type;
}
}
export const makeListModule = elementType => {
// List<elementType> type depends on elementType
// generating it another time, will give the same type (structurally equivalent):

View file

@ -1,5 +1,6 @@
import { getListType, makeListModule } from "./list_common.js";
import { makeListModule } from "./list_common.js";
import { Typed } from "../typed.js";
import { getListType } from "../type_registry.js";
export const Module = getListType(Typed); // a Module is a list of Typeds

29
structures/product.js Normal file
View file

@ -0,0 +1,29 @@
import { fnType, prodType } from "../type_registry.js";
import { Function } from "../metacircular.js";
// In JS, all products are encoded in the same way:
const constructor = left => right => ({left, right});
const left = product => product.left;
const right = product => product.right;
// Given two types A and B, create the type (A × B).
export const makeProductType = (leftType, rightType) => {
const productType = prodType(leftType, rightType);
const leftFnType = fnType({in: productType, out: leftType});
const rightFnType = fnType({in: productType, out: rightType});
const constructorBoundType = fnType({in: rightType, out: productType});
const constructorType = fnType({in: leftType, out: constructorBoundType});
return [
{i: left , t: leftFnType},
{i: right , t: rightFnType},
{i: leftFnType , t: Function},
{i: rightFnType, t: Function},
{i: constructor , t: constructorType},
{i: constructorType , t: Function},
{i: constructorBoundType, t: Function},
];
};

41
structures/sum.js Normal file
View file

@ -0,0 +1,41 @@
import { fnType, prodType, sumType } from "../type_registry.js";
import { Function, Type } from "../metacircular.js";
import { Module } from "./module.js";
const constructorLeft = left => ({variant: "L", value: left });
const constructorRight = right => ({variant: "R", value: right});
const match = sum => handlers => sum.variant === "L"
? handlers.left(sum.value)
: handlers.right(sum.value);
// Given two types A and B, create the type (A + B).
export const makeSumType = (leftType, rightType) => {
const sType = sumType(leftType, rightType);
const constructorLeftType = fnType({in: leftType , out: sType});
const constructorRightType = fnType({in: rightType, out: sType});
// For each possible return type, the match function has a different signature.
// We thus define a function that creates a properly typed match function.
const makeMatchFn = returnType => {
const handlersType = prodType(
fnType({in: leftType , out: returnType}), // handler function for left variant
fnType({in: rightType, out: returnType}), // handler function for right variant
);
const matchFnType = fnType({in: sType, out: fnType({in: handlersType, out: returnType})});
return [
{i: match , t: matchFnType},
{i: matchFnType, t: Function},
];
};
return [
{i: constructorLeft , t: constructorLeftType},
{i: constructorRight , t: constructorRightType},
{i: constructorLeftType , t: Function},
{i: constructorRightType, t: Function},
{i: makeMatchFn, t: fnType({in: Type, out: Module})},
];
};