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 cannotCompareTypeVarInstances = _ => _ => 0; 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)}'`); })() ); };