can auto-generate comparison functions for composed types

This commit is contained in:
Joeri Exelmans 2025-04-17 16:43:28 +02:00
parent 0b262daf7f
commit 4d1fb81492
6 changed files with 72 additions and 35 deletions

View file

@ -1,4 +1,4 @@
import { Char, Double, Int } from "../primitives/types.js"; import { Char, Double, Int, Unit } from "../primitives/types.js";
export const compareNumbers = x => y => { export const compareNumbers = x => y => {
if (typeof(x) !== 'number' || typeof(y) !== 'number') { if (typeof(x) !== 'number' || typeof(y) !== 'number') {
@ -21,9 +21,5 @@ export const compareBools = x => y => {
return x - y; return x - y;
}; };
export const typeToCmp = new Map([ // The Unit-type has only one instance, which is equal to itself:
[Int, compareNumbers], export const compareUnits = x => y => 0;
[Char, compareStrings],
[Double, compareNumbers],
[Boolean, compareBools],
]);

25
compare/registry.js Normal file
View file

@ -0,0 +1,25 @@
import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolUnit } from "../primitives/types.js";
import { symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/types.js";
import { compareBools, compareNumbers, compareStrings, compareUnits } from "./primitives.js";
import { compareLists, compareProducts, compareSets, compareSums } from "./structures.js";
const typeSymbolToCmp = new Map([
[SymbolInt , compareNumbers],
[SymbolChar , compareStrings],
[SymbolDouble, compareNumbers],
[SymbolBool , compareBools],
[SymbolUnit , compareUnits],
// these functions take extra comparison callbacks:
[symbolList , compareLists],
[symbolProduct, compareProducts],
[symbolSum , compareSums],
[symbolSet , compareSets],
])
export const makeCompareFn = type => {
return type.params.reduce(
(acc, cur) => acc(makeCompareFn(cur)),
typeSymbolToCmp.get(type.symbol)
);
}

View file

@ -1,8 +1,10 @@
import { compareNumbers } from "./primitives.js" import { compareNumbers } from "./primitives.js"
import { length as lengthLs } from "../structures/list.js"; import { get, length as lengthLs } from "../structures/list.js";
import { read, length as lengthSet } from "../structures/set.js"; import { read, length as lengthSet } from "../structures/set.js";
import { constructorProduct, getLeft, getRight } from "../structures/product.js"; import { constructorProduct, getLeft, getRight } from "../structures/product.js";
import { match } from "../structures/sum.js"; import { match } from "../structures/sum.js";
import { makeGeneric } from "../generics/generics.js";
import { lsType } from "../structures/types.js";
// (a -> a -> Int) -> [a] -> [a] -> Int // (a -> a -> Int) -> [a] -> [a] -> Int
export const compareLists = compareElems => x => y => { export const compareLists = compareElems => x => y => {

View file

@ -2,17 +2,17 @@
import { makeTypeConstructor } from "../type_constructor.js"; import { makeTypeConstructor } from "../type_constructor.js";
const SymbolInt = Symbol('Int'); export const SymbolInt = Symbol('Int');
const SymbolBool = Symbol('Bool'); export const SymbolBool = Symbol('Bool');
const SymbolDouble = Symbol('Double'); export const SymbolDouble = Symbol('Double');
const SymbolByte = Symbol('Byte'); export const SymbolByte = Symbol('Byte');
const SymbolChar = Symbol('Char'); export const SymbolChar = Symbol('Char');
const SymbolUnit = Symbol('Unit'); export const SymbolUnit = Symbol('Unit');
const SymbolBottom = Symbol('⊥'); export const SymbolBottom = Symbol('⊥');
const SymbolSymbol = Symbol('Symbol'); export const SymbolSymbol = Symbol('Symbol');
const SymbolType = Symbol('Type'); export const SymbolType = Symbol('Type');
const symbolAny = Symbol('Any'); export const symbolAny = Symbol('Any');
const SymbolGenericType = Symbol('GenericType'); export const SymbolGenericType = Symbol('GenericType');
export const Int = makeTypeConstructor(SymbolInt)(0); export const Int = makeTypeConstructor(SymbolInt)(0);
export const Bool = makeTypeConstructor(SymbolBool)(0); export const Bool = makeTypeConstructor(SymbolBool)(0);

View file

@ -1,3 +1,4 @@
import { makeCompareFn } from "../compare/registry.js";
import { Bottom, Int, Unit } from "../primitives/types.js"; import { Bottom, Int, Unit } from "../primitives/types.js";
import { unit } from "../primitives/unit.js"; import { unit } from "../primitives/unit.js";
import { capitalizeFirstLetter } from "../util/util.js"; import { capitalizeFirstLetter } from "../util/util.js";
@ -66,23 +67,36 @@ const variants = [
constructorProduct("not_found")(Unit), constructorProduct("not_found")(Unit),
]; ];
const myEnum = enumType(variants); const myEnumType = enumType(variants);
console.log(prettyT(myEnum)); console.log("observe the type that was generated:");
console.log(prettyT(myEnumType));
const [newPrice, newPrices, newNotFound] = makeConstructors(variants); const [newPrice, newPrices, newNotFound] = makeConstructors(variants);
console.log(newPrice(10)); const price = newPrice(10);
console.log(newPrices([20,30])); const prices = newPrices({l:[20,30]});
console.log(newNotFound(unit)); const notFound = newNotFound(unit);
const myMatchFn = makeMatchFn(variants); console.log("observe the encoding of different variant instances:");
console.log(price);
console.log(prices);
console.log(notFound);
const matchVariant = x => myMatchFn(x) const myEnumToString = x => makeMatchFn(variants)(x)
(price => `Price: ${price}`) (price => `Price: ${price}`)
(prices => `Prices: ${prices}`) (prices => `Prices: ${prices.l}`)
(() => "Not found!"); (() => "Not found!");
console.log(matchVariant(newPrice(10))); console.log("observe the generated match function in action:")
console.log(matchVariant(newPrices([20,30]))); console.log(myEnumToString(price));
console.log(matchVariant(newNotFound(unit))); console.log(myEnumToString(prices));
console.log(myEnumToString(notFound));
const compareMyEnum = makeCompareFn(myEnumType);
console.log(compareMyEnum(price)(prices)); // smaller
console.log(compareMyEnum(prices)(price)); // bigger
console.log(compareMyEnum(notFound)(price)); // bigger
console.log(compareMyEnum(prices)(prices)); // equal
console.log(compareMyEnum(newPrice(5))(newPrice(6))); // smaller

View file

@ -8,7 +8,7 @@ import { getSymbol, makeTypeConstructor } from "../type_constructor.js";
// The registry ensures that we never accidentally create more than one JS object for the same function type. // The registry ensures that we never accidentally create more than one JS object for the same function type.
// It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions. // 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, ...) // This same pattern is repeated throughout the code for all non-nullary type constructors (list, sum, product, ...)
const symbolFunction = Symbol('Function'); export const symbolFunction = Symbol('Function');
export const fnType = makeTypeConstructor(symbolFunction)(2); export const fnType = makeTypeConstructor(symbolFunction)(2);
export const isFunction = type => getSymbol(type) === symbolFunction; export const isFunction = type => getSymbol(type) === symbolFunction;
@ -35,22 +35,22 @@ export const typedFnType = (instance, callback, typeOfType = Type) => {
// Sum type // Sum type
const symbolSum = Symbol("Sum"); export const symbolSum = Symbol("Sum");
export const sumType = makeTypeConstructor(symbolSum)(2); export const sumType = makeTypeConstructor(symbolSum)(2);
// Product type // Product type
const symbolProduct = Symbol("Product"); export const symbolProduct = Symbol("Product");
export const prodType = makeTypeConstructor(symbolProduct)(2); export const prodType = makeTypeConstructor(symbolProduct)(2);
// List type // List type
const symbolList = Symbol('List'); export const symbolList = Symbol('List');
export const lsType = makeTypeConstructor(symbolList)(1); export const lsType = makeTypeConstructor(symbolList)(1);
// Set type // Set type
const symbolSet = Symbol('Set'); export const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet)(1); export const setType = makeTypeConstructor(symbolSet)(1);