From e04cac4f7d23e6d7b121c913398476700cc8139c Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Sun, 20 Apr 2025 11:30:28 +0200 Subject: [PATCH] move function --- compare/primitives.js | 8 -------- compare/registry.js | 4 +++- compare/type.js | 7 +++++++ 3 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 compare/type.js diff --git a/compare/primitives.js b/compare/primitives.js index e4b7088..d5e2638 100644 --- a/compare/primitives.js +++ b/compare/primitives.js @@ -1,7 +1,5 @@ // Total ordering of primitive types -import { getSymbol } from "../type_constructor.js"; - export const compareNumbers = x => y => { if (typeof(x) !== 'number' || typeof(y) !== 'number') { throw new Error(`was only meant to compare numbers! got ${x} and ${y}`); @@ -26,12 +24,6 @@ export const compareBools = x => y => { // The Unit-type has only one instance, which is equal to itself: export const compareUnits = x => y => 0; -export const compareTypes = x => y => { - return compareSymbols(getSymbol(x))(getSymbol(y)) - || compareNumbers(x.params.length)(y.params.length) - || x.params.reduce((acc, _, i) => acc || compareTypes(x.params[i])(y.params[i]), 0) -} - // Note: dirty assumption that every symbol has unique description. // This will be fixed once we move from symbols to real UUIDs. export const compareSymbols = a => b => (a !== b) && compareStrings(a.description)(b.description); diff --git a/compare/registry.js b/compare/registry.js index c7156ff..9852e01 100644 --- a/compare/registry.js +++ b/compare/registry.js @@ -1,7 +1,8 @@ -import { SymbolBool, SymbolChar, SymbolDouble, SymbolInt, SymbolUnit } from "../primitives/types.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"; import { compareLists, compareProducts, compareSets, compareSums } from "./structures.js"; +import { compareTypes } from "./type.js"; const typeSymbolToCmp = new Map([ [SymbolInt , compareNumbers], @@ -9,6 +10,7 @@ const typeSymbolToCmp = new Map([ [SymbolDouble, compareNumbers], [SymbolBool , compareBools], [SymbolUnit , compareUnits], + [SymbolType , compareTypes], // these functions take extra comparison callbacks: [symbolList , compareLists], diff --git a/compare/type.js b/compare/type.js new file mode 100644 index 0000000..a91f841 --- /dev/null +++ b/compare/type.js @@ -0,0 +1,7 @@ +import { getParams, getSymbol } from "../type_constructor.js"; +import { compareSymbols } from "./primitives.js"; +import { compareLists } from "./structures.js"; + +export const compareTypes = x => y => + compareSymbols(getSymbol(x))(getSymbol(y)) + || compareLists(compareTypes)(getParams(x))(getParams(y));