clean up module generic a bit
This commit is contained in:
parent
a826f475c3
commit
fc1a588e23
1 changed files with 46 additions and 40 deletions
86
generic.js
86
generic.js
|
|
@ -2,16 +2,35 @@ import { Bool, Int } from "./primitives/symbols.js";
|
||||||
import { fnType, lsType } from "./type_registry.js";
|
import { fnType, lsType } from "./type_registry.js";
|
||||||
import { deepEqual } from "./util.js";
|
import { deepEqual } from "./util.js";
|
||||||
|
|
||||||
// let nextSymbol = 'a';
|
export const makeGeneric = callback => {
|
||||||
|
// type variables to make available:
|
||||||
|
const a = Symbol('a');
|
||||||
|
const b = Symbol('b');
|
||||||
|
const c = Symbol('c');
|
||||||
|
const d = Symbol('d');
|
||||||
|
const e = Symbol('e');
|
||||||
|
const type = callback(a,b,c,d,e);
|
||||||
|
return {
|
||||||
|
typeVars: occurring(type, new Set([a,b,c,d,e])),
|
||||||
|
type,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// export const makeGeneric = callback => {
|
const occurring = (type, typeVars) => {
|
||||||
// const typeParam = Symbol(nextSymbol);
|
if (typeVars.has(type)) {
|
||||||
// nextSymbol = String.fromCharCode(nextSymbol.charCodeAt(0) + 1);
|
return new Set([type]);
|
||||||
// return {
|
}
|
||||||
// typeParam,
|
if (type.in !== undefined) {
|
||||||
// type: callback(typeParam),
|
// function type
|
||||||
// };
|
return new Set([
|
||||||
// };
|
...occurring(type.in, typeVars),
|
||||||
|
...occurring(type.out, typeVars)]);
|
||||||
|
}
|
||||||
|
if (type.listOf !== undefined) {
|
||||||
|
return occurring(type.listOf, typeVars);
|
||||||
|
}
|
||||||
|
return new Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
import { inspect } from 'node:util';
|
import { inspect } from 'node:util';
|
||||||
|
|
@ -119,7 +138,7 @@ export const substitute = (type, substitutions) => {
|
||||||
out: substitute(type.out, substitutions),
|
out: substitute(type.out, substitutions),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
throw new Error("i don't know what to do :(");
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const assign = (genFnType, paramType) => {
|
export const assign = (genFnType, paramType) => {
|
||||||
|
|
@ -134,39 +153,26 @@ export const assign = (genFnType, paramType) => {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const a = Symbol('a');
|
// a -> Int
|
||||||
const formal = {
|
const a_to_Int = makeGeneric(a => fnType({in: a, out: Int}));
|
||||||
typeVars: new Set([a]),
|
const Bool_to_Int = fnType({in: lsType(Bool), out: Int});
|
||||||
type: fnType({in: a, out: Int}),
|
console.log(matchConcrete(a_to_Int, Bool_to_Int));
|
||||||
};
|
|
||||||
const actual = fnType({in: lsType(Bool), out: Int});
|
|
||||||
console.log(matchConcrete(formal, actual));
|
|
||||||
|
|
||||||
|
// (a -> a) -> b
|
||||||
|
const fnType2 = makeGeneric((a,b) => fnType({in: fnType({in: a, out: a}), out: b}));
|
||||||
|
// (Bool -> Bool) -> a
|
||||||
|
const fnType3 = makeGeneric(a => fnType({in: fnType({in: Bool, out: Bool}), out: a}));
|
||||||
|
console.log(matchGeneric(fnType2, fnType3));
|
||||||
|
|
||||||
const b = Symbol('b');
|
// (a -> b) -> [a] -> [b]
|
||||||
const c = Symbol('c');
|
const mapFnType = makeGeneric((a,b) =>
|
||||||
const formal2 = {
|
fnType({
|
||||||
typeVars: new Set([a, b]),
|
|
||||||
type: fnType({in: fnType({in: a, out: a}), out: b}),
|
|
||||||
}
|
|
||||||
const actual2 = {
|
|
||||||
typeVars: new Set([c]),
|
|
||||||
type: fnType({in: fnType({in: Bool, out: Bool}), out: c}),
|
|
||||||
}
|
|
||||||
console.log(matchGeneric(formal2, actual2));
|
|
||||||
|
|
||||||
|
|
||||||
const mapFnType = {
|
|
||||||
typeVars: new Set([a, b]),
|
|
||||||
type: fnType({
|
|
||||||
in: fnType({in: a, out: b}),
|
in: fnType({in: a, out: b}),
|
||||||
out: fnType({in: lsType(a), out: lsType(b)}),
|
out: fnType({in: lsType(a), out: lsType(b)}),
|
||||||
}),
|
}));
|
||||||
};
|
// a -> a
|
||||||
const idFnType = {
|
const idFnType = makeGeneric(a =>
|
||||||
typeVars: new Set([c]),
|
fnType({in: a, out: a}));
|
||||||
type: fnType({in: c, out: c}),
|
|
||||||
};
|
|
||||||
|
|
||||||
// should be: listOf(c) -> listOf(c)
|
// should be: [a] -> [a]
|
||||||
console.log(assign(mapFnType, idFnType));
|
console.log(assign(mapFnType, idFnType));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue