rename some things

This commit is contained in:
Joeri Exelmans 2025-04-20 21:09:51 +02:00
parent e04cac4f7d
commit 8a4bd44f04
16 changed files with 92 additions and 60 deletions

View file

@ -1,3 +1,4 @@
import { getInst, getType } from "../primitives/dynamic.js";
import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolType, SymbolUnit } from "../primitives/types.js";
import { symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/types.js";
import { compareBools, compareNumbers, compareStrings, compareUnits } from "./primitives.js";
@ -17,11 +18,15 @@ const typeSymbolToCmp = new Map([
[symbolProduct, compareProducts],
[symbolSum , compareSums],
[symbolSet , compareSets],
])
]);
export const makeCompareFn = type => {
return type.params.reduce(
(acc, cur) => acc(makeCompareFn(cur)),
typeSymbolToCmp.get(type.symbol)
);
}
};
export const compareDynamic = x => y =>
compareTypes(getType(x))(getType(y))
|| makeCompareFn(getType(x))(getInst(x))(getInst(y));

View file

@ -3,7 +3,7 @@
import { compareNumbers } from "./primitives.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 { newProduct, getLeft, getRight } from "../structures/product.js";
import { match } from "../structures/sum.js";
// (a -> a -> Int) -> [a] -> [a] -> Int
@ -28,12 +28,12 @@ export const compareProducts = compareLeft => compareRight => x => y => {
// (a -> a -> Int) -> (b -> b -> Int) -> (a | b) -> (a | b) -> Int
export const compareSums = compareLeft => compareRight => x => y => {
return match(x)(constructorProduct
(leftValueX => match(y)(constructorProduct
return match(x)(newProduct
(leftValueX => match(y)(newProduct
(leftValueY => compareLeft(leftValueX)(leftValueY))
((rightValueY) => -1) // x is 'left' and y is 'right' => x < y
))
(rightValueX => match(y)(constructorProduct
(rightValueX => match(y)(newProduct
(leftValueY => 1) // x is 'right' and y is 'left' => x > y
(rightValueY => compareRight(rightValueX)(rightValueY))
))