progress with type classes, type inference still very ad-hoc
This commit is contained in:
parent
5283be608b
commit
c5ac55b0ff
10 changed files with 64 additions and 19 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
import { lsType } from "../structures/list_common.js";
|
import { lsType } from "../structures/list_common.js";
|
||||||
import { fnType } from "../metacircular.js";
|
import { fnType } from "../metacircular.js";
|
||||||
import { deepEqual, pretty } from "../util.js";
|
import { deepEqual, DefaultMap, pretty } from "../util.js";
|
||||||
|
import { numDictType } from "../typeclasses/num_type.js";
|
||||||
|
|
||||||
const genericTypeRegistry = new DefaultMap(underlyingType => ({generic: underlyingType}));
|
const genericTypeRegistry = new DefaultMap(underlyingType => ({ generic: underlyingType }));
|
||||||
|
|
||||||
// type constructor for generic kinds,
|
// type constructor for generic kinds,
|
||||||
// for instance:
|
// for instance:
|
||||||
|
|
@ -23,15 +24,15 @@ export const makeGeneric = callback => {
|
||||||
const c = Symbol('c');
|
const c = Symbol('c');
|
||||||
const d = Symbol('d');
|
const d = Symbol('d');
|
||||||
const e = Symbol('e');
|
const e = Symbol('e');
|
||||||
const type = callback(a,b,c,d,e);
|
const type = callback(a, b, c, d, e);
|
||||||
return {
|
return {
|
||||||
typeVars: occurring(type, new Set([a,b,c,d,e])),
|
typeVars: occurring(type, new Set([a, b, c, d, e])),
|
||||||
type,
|
type,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// From the given set of type variables, return only those that occur in the given type.
|
// From the given set of type variables, return only those that occur in the given type.
|
||||||
const occurring = (type, typeVars) => {
|
export const occurring = (type, typeVars) => {
|
||||||
if (typeVars.has(type)) {
|
if (typeVars.has(type)) {
|
||||||
return new Set([type]);
|
return new Set([type]);
|
||||||
}
|
}
|
||||||
|
|
@ -130,9 +131,24 @@ export const unify = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formalType.numDict !== undefined) {
|
if (formalType.numDict !== undefined) {
|
||||||
|
if (actualType.numDict === undefined) {
|
||||||
|
throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// both are NumDict type
|
||||||
|
const underlyingType = unify(
|
||||||
|
{typeVars: formalTypeVars, type: formalType.numDict},
|
||||||
|
{typeVars: actualTypeVars, type: actualType.numDict});
|
||||||
|
return {
|
||||||
|
substitutions: underlyingType.substitutions,
|
||||||
|
typeVars: new Set([
|
||||||
|
...actualTypeVars,
|
||||||
|
...formalTypeVars].filter(a => !underlyingType.substitutions.has(a))),
|
||||||
|
type: numDictType(underlyingType.type),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new Error("i don't know what to do :(")
|
throw new Error("i don't know what to do :(");
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
|
// export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
|
||||||
|
|
@ -167,4 +183,4 @@ export const assign = (genFnType, paramType) => {
|
||||||
typeVars: matchedInType.typeVars,
|
typeVars: matchedInType.typeVars,
|
||||||
type: substitutedOutType,
|
type: substitutedOutType,
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
|
||||||
2
generics/types.js
Normal file
2
generics/types.js
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -53,7 +53,6 @@ export const typedFnType = (instance, callback) => {
|
||||||
export const typedFnType2 = callback => {
|
export const typedFnType2 = callback => {
|
||||||
const fnTs = [];
|
const fnTs = [];
|
||||||
const wrappedFnType = ({ in: inType, out: outType }) => {
|
const wrappedFnType = ({ in: inType, out: outType }) => {
|
||||||
console.log('wrappedFnType called');
|
|
||||||
const fnT = fnType({ in: inType, out: outType });
|
const fnT = fnType({ in: inType, out: outType });
|
||||||
fnTs.push(fnT);
|
fnTs.push(fnT);
|
||||||
return fnT;
|
return fnT;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@inquirer/prompts": "^7.4.0"
|
"@inquirer/prompts": "^7.4.0"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { sumType } from "../type_registry.js";
|
|
||||||
import { prodType } from "./product.js";
|
import { prodType } from "./product.js";
|
||||||
import { fnType } from "../metacircular.js";
|
import { fnType } from "../metacircular.js";
|
||||||
import { Function, Type } from "../metacircular.js";
|
import { Function, Type } from "../metacircular.js";
|
||||||
import { Module } from "./list_types/module.js";
|
import { Module } from "./list_types/module.js";
|
||||||
|
import { DefaultMap } from "../util.js";
|
||||||
|
|
||||||
const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({ operator: "sum", leftType, rightType })));
|
const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({ operator: "sum", leftType, rightType })));
|
||||||
|
|
||||||
|
|
|
||||||
0
typeclasses/eq.js
Normal file
0
typeclasses/eq.js
Normal file
|
|
@ -1,10 +1,9 @@
|
||||||
import { makeGeneric } from "../generics/generics";
|
import { makeGeneric } from "../generics/generics.js";
|
||||||
import { addDouble, mulDouble } from "../primitives/double";
|
import { addDouble, mulDouble } from "../primitives/double.js";
|
||||||
import { addInt, mulInt } from "../primitives/int";
|
import { addInt, mulInt } from "../primitives/int.js";
|
||||||
import { typedFnType, typedFnType2 } from "../metacircular";
|
import { Type, typedFnType, typedFnType2 } from "../metacircular.js";
|
||||||
|
import { Double, Int } from "../primitives/symbols.js";
|
||||||
const numDictTypeRegistry = new DefaultMap(a => ({numDict: a}));
|
import { numDictType } from "./num_type.js";
|
||||||
export const numDictType = a => numDictTypeRegistry.getdefault(a, true);
|
|
||||||
|
|
||||||
export const getAdd = numDict => numDict.add;
|
export const getAdd = numDict => numDict.add;
|
||||||
export const getMul = numDict => numDict.mul;
|
export const getMul = numDict => numDict.mul;
|
||||||
|
|
@ -40,8 +39,8 @@ const DoubleNumDict = {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ModuleNumInstances = {l:[
|
export const ModuleNumInstances = {l:[
|
||||||
{i: IntNumDict , t: NumDict},
|
{i: IntNumDict , t: numDictType(Int)},
|
||||||
{i: DoubleNumDict, t: NumDict},
|
{i: DoubleNumDict, t: numDictType(Double)},
|
||||||
]};
|
]};
|
||||||
|
|
||||||
// mapping from type to type class implementation
|
// mapping from type to type class implementation
|
||||||
|
|
|
||||||
23
typeclasses/num.test.js
Normal file
23
typeclasses/num.test.js
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { assign } from "../generics/generics.js";
|
||||||
|
import { makeGeneric } from "../generics/generics.js";
|
||||||
|
import { fnType } from "../metacircular.js";
|
||||||
|
import { Double, Int } from "../primitives/symbols.js";
|
||||||
|
import { numDictType } from "./num_type.js";
|
||||||
|
|
||||||
|
const square = numDict => x => getMul(numDict)(x)(x);
|
||||||
|
|
||||||
|
const squareFnType = makeGeneric(a =>
|
||||||
|
fnType({
|
||||||
|
in: numDictType(a),
|
||||||
|
out: fnType({
|
||||||
|
in: a,
|
||||||
|
out: a,
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// should be: Int -> Int
|
||||||
|
console.log(assign(squareFnType, makeGeneric(() => numDictType(Int))));
|
||||||
|
|
||||||
|
// should be: Double -> Double
|
||||||
|
console.log(assign(squareFnType, makeGeneric(() => numDictType(Double))));
|
||||||
|
|
||||||
4
typeclasses/num_type.js
Normal file
4
typeclasses/num_type.js
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import { DefaultMap } from "../util.js";
|
||||||
|
|
||||||
|
const numDictTypeRegistry = new DefaultMap(a => ({ numDict: a }));
|
||||||
|
export const numDictType = a => numDictTypeRegistry.getdefault(a, true);
|
||||||
1
typeclasses/resolve.js
Normal file
1
typeclasses/resolve.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
// const resolveTypeClass = (mapping, )
|
||||||
Loading…
Add table
Add a link
Reference in a new issue