import { getInst, getType } from "../primitives/dynamic.js"; import { SymbolBool, SymbolBottom, SymbolByte, SymbolChar, SymbolDouble, SymbolDynamic, SymbolInt, SymbolUUID, SymbolType, SymbolUnit, SymbolOrdering } from "../primitives/primitive_types.js"; import { UNBOUND_SYMBOLS } from "../primitives/typevars.js"; import { symbolDictIterator } from "../structures/dict.types.js"; import { symbolSetIterator } from "../structures/set.types.js"; import { symbolDict, symbolFunction, symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/type_constructors.js"; import { prettyT } from "../util/pretty.js"; import { compareBools, compareDoubles, compareInts, compareOrderings, compareStrings, compareSymbols, compareUnits } from "./primitives.js"; import { compareDictIterators, compareDicts, compareFunctions, compareLists, compareProducts, compareSetIterators, compareSets, compareSums } from "./structures.js"; import { compareTypes } from "./type.js"; 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 , compareInts ], [SymbolBool , compareBools ], [SymbolDouble , compareDoubles ], [SymbolByte , compareDoubles ], [SymbolChar , compareStrings ], [SymbolUnit , compareUnits ], [SymbolBottom , _ => _ => { throw new Error("Bottom!"); }], [SymbolUUID , compareSymbols ], // [SymbolGenericType, ?] TODO [SymbolType , compareTypes ], [SymbolDynamic , compareDynamic ], [SymbolOrdering, compareOrderings], // these functions take extra comparison callbacks: [symbolFunction , compareFunctions ], [symbolSum , compareSums ], [symbolProduct , compareProducts ], [symbolList , compareLists ], [symbolSet , compareSets ], [symbolDict , compareDicts ], [symbolSetIterator , compareSetIterators ], [symbolDictIterator, compareDictIterators], // 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)}'`); })() ); };