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

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})},
];
};