refactor code: move everything from type_registry to "most appropriate" modules
This commit is contained in:
parent
4ca60784aa
commit
5283be608b
22 changed files with 160 additions and 164 deletions
|
|
@ -1,6 +1,21 @@
|
|||
import { fnType, lsType } from "../type_registry.js";
|
||||
import { lsType } from "../structures/list_common.js";
|
||||
import { fnType } from "../metacircular.js";
|
||||
import { deepEqual, pretty } from "../util.js";
|
||||
|
||||
const genericTypeRegistry = new DefaultMap(underlyingType => ({generic: underlyingType}));
|
||||
|
||||
// type constructor for generic kinds,
|
||||
// for instance:
|
||||
// a -> a -> Bool
|
||||
// is typed by
|
||||
// genericType(Function)
|
||||
export const genericType = underlyingType => genericTypeRegistry.getdefault(underlyingType, true);
|
||||
|
||||
// constructor for generic types
|
||||
// for instance, the type:
|
||||
// a -> a -> Bool
|
||||
// is created by
|
||||
// makeGeneric(a => fnType({in: a, out: fnType({in: a, out: Bool})}))
|
||||
export const makeGeneric = callback => {
|
||||
// type variables to make available:
|
||||
const a = Symbol('a');
|
||||
|
|
@ -32,14 +47,11 @@ const occurring = (type, typeVars) => {
|
|||
return new Set();
|
||||
}
|
||||
|
||||
// merge_int(1, 2) => conflict(1,2)
|
||||
// merge_list_of_int([1], [2]) => [conflict(1,2)]
|
||||
// Currently very ad-hoc.
|
||||
|
||||
// merge {i: [1], t: List_of_Int} ->
|
||||
|
||||
// Thanks to Hans for pointing out that this algorithm exactly like "Unification" in Prolog:
|
||||
// Thanks to Hans for pointing out that this algorithm exactly like "Unification" in Prolog (hence the function name):
|
||||
// https://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/node12.html
|
||||
export const matchGeneric = (
|
||||
export const unify = (
|
||||
{typeVars: formalTypeVars, type: formalType},
|
||||
{typeVars: actualTypeVars, type: actualType},
|
||||
) => {
|
||||
|
|
@ -71,8 +83,8 @@ export const matchGeneric = (
|
|||
}
|
||||
else {
|
||||
// both are function type
|
||||
const inType = matchGeneric({typeVars: formalTypeVars, type: formalType.in}, {typeVars: actualTypeVars, type: actualType.in});
|
||||
const outType = matchGeneric({typeVars: formalTypeVars, type: formalType.out}, {typeVars: actualTypeVars, type: actualType.out});
|
||||
const inType = unify({typeVars: formalTypeVars, type: formalType.in}, {typeVars: actualTypeVars, type: actualType.in});
|
||||
const outType = unify({typeVars: formalTypeVars, type: formalType.out}, {typeVars: actualTypeVars, type: actualType.out});
|
||||
// check for conflicts between 'in' and 'out' subsitutions
|
||||
for (const [typeVar, actual] of inType.substitutions) {
|
||||
if (outType.substitutions.has(typeVar)) {
|
||||
|
|
@ -104,7 +116,7 @@ export const matchGeneric = (
|
|||
}
|
||||
else {
|
||||
// both are list type
|
||||
const elementType = matchGeneric(
|
||||
const elementType = unify(
|
||||
{typeVars: formalTypeVars, type: formalType.listOf},
|
||||
{typeVars: actualTypeVars, type: actualType.listOf});
|
||||
return {
|
||||
|
|
@ -116,12 +128,16 @@ export const matchGeneric = (
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (formalType.numDict !== undefined) {
|
||||
|
||||
}
|
||||
throw new Error("i don't know what to do :(")
|
||||
};
|
||||
|
||||
export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
|
||||
return matchGeneric({typeVars, type: formalType}, {typeVars: new Set(), type: actualType});
|
||||
};
|
||||
// export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
|
||||
// return unify({typeVars, type: formalType}, {typeVars: new Set(), type: actualType});
|
||||
// };
|
||||
|
||||
export const substitute = (type, substitutions) => {
|
||||
if (substitutions.has(type)) {
|
||||
|
|
@ -142,7 +158,7 @@ export const substitute = (type, substitutions) => {
|
|||
}
|
||||
|
||||
export const assign = (genFnType, paramType) => {
|
||||
const matchedInType = matchGeneric({
|
||||
const matchedInType = unify({
|
||||
typeVars: genFnType.typeVars,
|
||||
type: genFnType.type.in,
|
||||
}, paramType);
|
||||
|
|
|
|||
|
|
@ -1,20 +1,21 @@
|
|||
import { Bool, Int } from "../primitives/symbols.js";
|
||||
import { fnType, lsType } from "../type_registry.js";
|
||||
import { assign, makeGeneric, matchConcrete, matchGeneric } from "./generics.js";
|
||||
import { lsType } from "../structures/list_common.js";
|
||||
import { fnType } from "../metacircular.js";
|
||||
import { assign, makeGeneric, unify } from "./generics.js";
|
||||
|
||||
// a -> Int
|
||||
const a_to_Int = makeGeneric(a => fnType({in: a, out: Int}));
|
||||
// Bool -> Int
|
||||
const Bool_to_Int = fnType({in: lsType(Bool), out: Int});
|
||||
const Bool_to_Int = makeGeneric(() => fnType({in: lsType(Bool), out: Int}));
|
||||
// should be: Bool -> Int
|
||||
console.log(matchConcrete(a_to_Int, Bool_to_Int));
|
||||
console.log(unify(a_to_Int, Bool_to_Int));
|
||||
|
||||
// (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}));
|
||||
// should be: (Bool -> Bool) -> a
|
||||
console.log(matchGeneric(fnType2, fnType3));
|
||||
console.log(unify(fnType2, fnType3));
|
||||
|
||||
// (a -> b) -> [a] -> [b]
|
||||
const mapFnType = makeGeneric((a,b) =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue