turn the function for creating new types (or type constructors) into a DOPE function

This commit is contained in:
Joeri Exelmans 2025-03-31 17:35:30 +02:00
parent d8ca2f3999
commit a0e3aa0cb3
12 changed files with 112 additions and 58 deletions

View file

@ -9,7 +9,7 @@ import { getSymbol, makeTypeConstructor } from "../type_constructor.js";
// It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions.
// This same pattern is repeated throughout the code for all non-nullary type constructors (list, sum, product, ...)
const symbolFunction = Symbol('Function');
export const fnType = makeTypeConstructor(symbolFunction, 2);
export const fnType = makeTypeConstructor(symbolFunction)(2);
export const isFunction = type => getSymbol(type) === symbolFunction;
@ -40,25 +40,26 @@ export const typedFnType2 = callback => {
// Sum type
const symbolSum = Symbol("Sum");
export const sumType = makeTypeConstructor(symbolSum, 2);
export const sumType = makeTypeConstructor(symbolSum)(2);
// Product type
const symbolProduct = Symbol("Product");
export const prodType = makeTypeConstructor(symbolProduct, 2);
export const prodType = makeTypeConstructor(symbolProduct)(2);
// List type
const symbolList = Symbol('List');
export const lsType = makeTypeConstructor(symbolList, 1);
export const lsType = makeTypeConstructor(symbolList)(1);
// Set type
const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet, 1);
export const setType = makeTypeConstructor(symbolSet)(1);
// Pretty print type
export function prettyT(type) {
// console.log("pretty:", type);
if (type.typeVars) {
if (type.typeVars.size > 0) {
return `${[...type.typeVars].map(prettyT).join(", ")}: ${prettyT(type.type)}`;