From c1ac3c0d60392640be0a817b8c01be9aa89d4e9e Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Sun, 20 Apr 2025 11:24:05 +0200 Subject: [PATCH] add type comparison function --- compare/primitives.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compare/primitives.js b/compare/primitives.js index cac01e7..e4b7088 100644 --- a/compare/primitives.js +++ b/compare/primitives.js @@ -1,5 +1,7 @@ // 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}`); @@ -23,3 +25,13 @@ 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);