can auto-generate comparison functions for composed types
This commit is contained in:
parent
0b262daf7f
commit
4d1fb81492
6 changed files with 72 additions and 35 deletions
|
|
@ -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 => {
|
||||
if (typeof(x) !== 'number' || typeof(y) !== 'number') {
|
||||
|
|
@ -21,9 +21,5 @@ export const compareBools = x => y => {
|
|||
return x - y;
|
||||
};
|
||||
|
||||
export const typeToCmp = new Map([
|
||||
[Int, compareNumbers],
|
||||
[Char, compareStrings],
|
||||
[Double, compareNumbers],
|
||||
[Boolean, compareBools],
|
||||
]);
|
||||
// The Unit-type has only one instance, which is equal to itself:
|
||||
export const compareUnits = x => y => 0;
|
||||
|
|
|
|||
25
compare/registry.js
Normal file
25
compare/registry.js
Normal 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)
|
||||
);
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
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 { constructorProduct, getLeft, getRight } from "../structures/product.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
|
||||
export const compareLists = compareElems => x => y => {
|
||||
|
|
|
|||
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
import { makeTypeConstructor } from "../type_constructor.js";
|
||||
|
||||
const SymbolInt = Symbol('Int');
|
||||
const SymbolBool = Symbol('Bool');
|
||||
const SymbolDouble = Symbol('Double');
|
||||
const SymbolByte = Symbol('Byte');
|
||||
const SymbolChar = Symbol('Char');
|
||||
const SymbolUnit = Symbol('Unit');
|
||||
const SymbolBottom = Symbol('⊥');
|
||||
const SymbolSymbol = Symbol('Symbol');
|
||||
const SymbolType = Symbol('Type');
|
||||
const symbolAny = Symbol('Any');
|
||||
const SymbolGenericType = Symbol('GenericType');
|
||||
export const SymbolInt = Symbol('Int');
|
||||
export const SymbolBool = Symbol('Bool');
|
||||
export const SymbolDouble = Symbol('Double');
|
||||
export const SymbolByte = Symbol('Byte');
|
||||
export const SymbolChar = Symbol('Char');
|
||||
export const SymbolUnit = Symbol('Unit');
|
||||
export const SymbolBottom = Symbol('⊥');
|
||||
export const SymbolSymbol = Symbol('Symbol');
|
||||
export const SymbolType = Symbol('Type');
|
||||
export const symbolAny = Symbol('Any');
|
||||
export const SymbolGenericType = Symbol('GenericType');
|
||||
|
||||
export const Int = makeTypeConstructor(SymbolInt)(0);
|
||||
export const Bool = makeTypeConstructor(SymbolBool)(0);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { makeCompareFn } from "../compare/registry.js";
|
||||
import { Bottom, Int, Unit } from "../primitives/types.js";
|
||||
import { unit } from "../primitives/unit.js";
|
||||
import { capitalizeFirstLetter } from "../util/util.js";
|
||||
|
|
@ -66,23 +67,36 @@ const variants = [
|
|||
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);
|
||||
|
||||
console.log(newPrice(10));
|
||||
console.log(newPrices([20,30]));
|
||||
console.log(newNotFound(unit));
|
||||
const price = newPrice(10);
|
||||
const prices = newPrices({l:[20,30]});
|
||||
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}`)
|
||||
(prices => `Prices: ${prices}`)
|
||||
(prices => `Prices: ${prices.l}`)
|
||||
(() => "Not found!");
|
||||
|
||||
console.log(matchVariant(newPrice(10)));
|
||||
console.log(matchVariant(newPrices([20,30])));
|
||||
console.log(matchVariant(newNotFound(unit)));
|
||||
console.log("observe the generated match function in action:")
|
||||
console.log(myEnumToString(price));
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
// 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 symbolFunction = Symbol('Function');
|
||||
export const fnType = makeTypeConstructor(symbolFunction)(2);
|
||||
|
||||
export const isFunction = type => getSymbol(type) === symbolFunction;
|
||||
|
|
@ -35,22 +35,22 @@ export const typedFnType = (instance, callback, typeOfType = Type) => {
|
|||
|
||||
// Sum type
|
||||
|
||||
const symbolSum = Symbol("Sum");
|
||||
export const symbolSum = Symbol("Sum");
|
||||
export const sumType = makeTypeConstructor(symbolSum)(2);
|
||||
|
||||
// Product type
|
||||
|
||||
const symbolProduct = Symbol("Product");
|
||||
export const symbolProduct = Symbol("Product");
|
||||
export const prodType = makeTypeConstructor(symbolProduct)(2);
|
||||
|
||||
// List type
|
||||
|
||||
const symbolList = Symbol('List');
|
||||
export const symbolList = Symbol('List');
|
||||
export const lsType = makeTypeConstructor(symbolList)(1);
|
||||
|
||||
// Set type
|
||||
|
||||
const symbolSet = Symbol('Set');
|
||||
export const symbolSet = Symbol('Set');
|
||||
export const setType = makeTypeConstructor(symbolSet)(1);
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue