This commit is contained in:
Joeri Exelmans 2025-03-24 17:28:07 +01:00
parent 6af72e525c
commit 145835ad5d
22 changed files with 153 additions and 90 deletions

View file

@ -8,10 +8,11 @@ import { pretty, zip } from "../util.js";
// makeGeneric(a => fnType({in: a, out: fnType({in: a, out: Bool})}))
export const makeGeneric = callback => {
// type variables to make available:
const typeVars = ['a', 'b', 'c', 'd', 'e'].map(letter => ({
symbol: Symbol(letter),
params: [],
}));
const typeVars = ['a', 'b', 'c', 'd', 'e'].map(
letter => ({
symbol: Symbol(letter),
params: [],
}));
const type = callback(...typeVars);
return {
typeVars: occurring(type, new Set(typeVars)),
@ -28,6 +29,7 @@ export const occurring = (type, typeVars) => {
return new Set(type.params.flatMap(p => [...occurring(p, typeVars)]));
}
// Merge 2 substitution-mappings, uni-directional.
const mergeOneWay = (m1, m2) => {
const m1copy = new Map(m1);
const m2copy = new Map(m2);
@ -41,6 +43,7 @@ const mergeOneWay = (m1, m2) => {
return [true, m1copy, m2copy, new Set()]; // stable
}
// Merge 2 substitution-mappings, bi-directional.
export const mergeTwoWay = (m1, m2) => {
// check for conflicts:
for (const [typeVar, actual] of m1) {
@ -69,10 +72,12 @@ export const unify = (
{typeVars: formalTypeVars, type: formalType},
{typeVars: actualTypeVars, type: actualType},
) => {
// console.log("unify", {formalTypeVars, formalType, actualTypeVars, actualType});
// console.log("unify", pretty({formalTypeVars, formalType, actualTypeVars, actualType}));
if (formalTypeVars.has(formalType)) {
// simplest case: formalType is a type paramater
// => substitute with actualType
// console.log("assign actual to formal");
return {
substitutions: new Map([[formalType, actualType]]),
typeVars: new Set([
@ -84,6 +89,7 @@ export const unify = (
}
if (actualTypeVars.has(actualType)) {
// same as above, but in the other direction
// console.log("assign formal to actual");
return {
substitutions: new Map([[actualType, formalType]]),
typeVars: new Set([
@ -98,6 +104,7 @@ export const unify = (
throw new Error(`cannot unify ${pretty(formalType.symbol)} and ${pretty(actualType.symbol)}`);
}
else {
// console.log("symbols match - unify recursively", formalType.symbol);
const unifiedParams = zip(formalType.params, actualType.params).map(([formalParam, actualParam]) => unify({typeVars: formalTypeVars, type: formalParam}, {typeVars: actualTypeVars, type: actualParam}));
const [unifiedSubstitusions, deleted] = unifiedParams.reduce(([substitutionsSoFar, deletedSoFar], cur) => {
// console.log('merging', substitutionsSoFar, cur.substitutions);
@ -124,6 +131,12 @@ export const substitute = (type, substitutions) => {
// type IS a type var to be substituted:
return substitutions.get(type);
}
if (type.params.length === 0) {
// Attention: there's a reason why we have this special case.
// Types are compared by object ID, so we don't want to create a new object for a type that takes no type parameters (then the newly create type would differ).
// Should fix this some day.
return type;
}
return {
symbol: type.symbol,
params: type.params.map(p => substitute(p, substitutions)),