pretty print enhancements + comparison of generic functions

This commit is contained in:
Joeri Exelmans 2025-05-08 22:59:01 +02:00
parent bbac7858ae
commit 9e1f679dba
15 changed files with 93 additions and 45 deletions

View file

@ -1,6 +1,8 @@
import { getInst, getType } from "../primitives/dynamic.js";
import { SymbolBool, SymbolBottom, SymbolByte, SymbolChar, SymbolDouble, SymbolDynamic, SymbolInt, SymbolUUID, SymbolType, SymbolUnit } from "../primitives/primitive_types.js";
import { UNBOUND_SYMBOLS } from "../primitives/typevars.js";
import { symbolDict, symbolFunction, symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/type_constructors.js";
import { prettyT } from "../util/pretty.js";
import { compareBools, compareNumbers, compareStrings, compareSymbols, compareUnits } from "./primitives.js";
import { compareDicts, compareFunctions, compareLists, compareProducts, compareSets, compareSums } from "./structures.js";
import { compareTypes } from "./type.js";
@ -9,6 +11,8 @@ export const compareDynamic = x => y =>
compareTypes(getType(x))(getType(y))
|| makeCompareFn(getType(x))(getInst(x))(getInst(y));
const cannotCompareTypeVarInstances = _ => _ => { throw new Error("Cannot compare instance of type variables"); }
const typeSymbolToCmp = new Map([
[SymbolInt , compareNumbers],
[SymbolBool , compareBools],
@ -17,7 +21,7 @@ const typeSymbolToCmp = new Map([
[SymbolChar , compareStrings],
[SymbolUnit , compareUnits],
[SymbolBottom , _ => _ => { throw new Error("Bottom!"); }],
[SymbolUUID , compareSymbols],
[SymbolUUID , compareSymbols],
// [SymbolGenericType, ?] TODO
[SymbolType , compareTypes],
[SymbolDynamic, compareDynamic],
@ -29,11 +33,20 @@ const typeSymbolToCmp = new Map([
[symbolList , compareLists],
[symbolSet , compareSets],
[symbolDict , compareDicts],
// even though we cannot compare typevar instances,
// we still need to give a function or shit will break
// types that contain typevars are never instantiated,
// EXCEPT:
// - generic functions (but for function comparison we don't look at the in/out types)
// - empty list, empty set, empty dict are all generic, but for each type there is only one empty element, so it never needs to be compared to another one
...UNBOUND_SYMBOLS.map(uuid => [uuid, cannotCompareTypeVarInstances]),
]);
export const makeCompareFn = type => {
return type.params.reduce(
(acc, cur) => acc(makeCompareFn(cur(type))),
typeSymbolToCmp.get(type.symbol)
|| (() => { throw new Error(`don't know how to compare instances of '${prettyT(type)}'`); })()
);
};