progress
This commit is contained in:
parent
6af72e525c
commit
145835ad5d
22 changed files with 153 additions and 90 deletions
|
|
@ -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)),
|
||||
|
|
|
|||
|
|
@ -1,41 +1,40 @@
|
|||
import { Bool, Int } from "../primitives/types.js";
|
||||
import { lsType } from "../structures/types.js";
|
||||
import { fnType } from "../structures/function.js";
|
||||
import { fnType, lsType } from "../structures/types.js";
|
||||
import { assign, makeGeneric, unify } from "./generics.js";
|
||||
import { pretty } from "../util.js";
|
||||
|
||||
// a -> Int
|
||||
const a_to_Int = makeGeneric(a => fnType({in: a, out: Int}));
|
||||
const a_to_Int = makeGeneric(a => fnType(a)(Int));
|
||||
// Bool -> Int
|
||||
const Bool_to_Int = makeGeneric(() => fnType({in: lsType(Bool), out: Int}));
|
||||
const Bool_to_Int = makeGeneric(() => fnType(lsType(Bool))(Int));
|
||||
console.log("should be: [Bool] -> Int")
|
||||
console.log(pretty(unify(a_to_Int, Bool_to_Int)));
|
||||
|
||||
// (a -> a) -> b
|
||||
const fnType2 = makeGeneric((a,b) => fnType({in: fnType({in: a, out: a}), out: b}));
|
||||
const fnType2 = makeGeneric((a,b) => fnType(fnType(a)(a))(b));
|
||||
// (Bool -> Bool) -> a
|
||||
const fnType3 = makeGeneric(a => fnType({in: fnType({in: Bool, out: Bool}), out: a}));
|
||||
const fnType3 = makeGeneric(a => fnType(fnType(Bool)(Bool))(a));
|
||||
console.log("should be: (Bool -> Bool) -> a");
|
||||
console.log(pretty(unify(fnType2, fnType3)));
|
||||
|
||||
// (a -> b) -> [a] -> [b]
|
||||
const mapFnType = makeGeneric((a,b) =>
|
||||
fnType({
|
||||
in: fnType({in: a, out: b}),
|
||||
out: fnType({in: lsType(a), out: lsType(b)}),
|
||||
}));
|
||||
fnType
|
||||
(fnType(a)(b))
|
||||
(fnType(lsType(a))(lsType(b))))
|
||||
// a -> a
|
||||
const idFnType = makeGeneric(a =>
|
||||
fnType({in: a, out: a}));
|
||||
fnType(a)(a));
|
||||
console.log("should be: [a] -> [a]");
|
||||
console.log(pretty(assign(mapFnType, idFnType)));
|
||||
|
||||
// (a -> Int) -> [a] -> a
|
||||
const weirdFnType = makeGeneric(a =>
|
||||
fnType({
|
||||
in: fnType({in: a, out: Int}),
|
||||
out: fnType({in: lsType(a), out: a}),
|
||||
}));
|
||||
fnType
|
||||
(fnType(a)(Int))
|
||||
(fnType
|
||||
(lsType(a))
|
||||
(a)))
|
||||
// we call this function with parameter of type (b -> b) ...
|
||||
// giving these substitutions:
|
||||
// a := b
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue