progress with type classes, type inference still very ad-hoc

This commit is contained in:
Joeri Exelmans 2025-03-20 18:34:58 +01:00
parent 5283be608b
commit c5ac55b0ff
10 changed files with 64 additions and 19 deletions

View file

@ -1,6 +1,7 @@
import { lsType } from "../structures/list_common.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 }));
@ -31,7 +32,7 @@ export const makeGeneric = callback => {
};
// 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)) {
return new Set([type]);
}
@ -130,9 +131,24 @@ export const unify = (
}
if (formalType.numDict !== undefined) {
if (actualType.numDict === undefined) {
throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`);
}
throw new Error("i don't know what to do :(")
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 :(");
};
// export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
@ -167,4 +183,4 @@ export const assign = (genFnType, paramType) => {
typeVars: matchedInType.typeVars,
type: substitutedOutType,
};
}
};

2
generics/types.js Normal file
View file

@ -0,0 +1,2 @@

View file

@ -53,7 +53,6 @@ export const typedFnType = (instance, callback) => {
export const typedFnType2 = callback => {
const fnTs = [];
const wrappedFnType = ({ in: inType, out: outType }) => {
console.log('wrappedFnType called');
const fnT = fnType({ in: inType, out: outType });
fnTs.push(fnT);
return fnT;

View file

@ -1,4 +1,5 @@
{
"type": "module",
"dependencies": {
"@inquirer/prompts": "^7.4.0"
}

View file

@ -1,8 +1,8 @@
import { sumType } from "../type_registry.js";
import { prodType } from "./product.js";
import { fnType } from "../metacircular.js";
import { Function, Type } from "../metacircular.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 })));

0
typeclasses/eq.js Normal file
View file

View file

@ -1,10 +1,9 @@
import { makeGeneric } from "../generics/generics";
import { addDouble, mulDouble } from "../primitives/double";
import { addInt, mulInt } from "../primitives/int";
import { typedFnType, typedFnType2 } from "../metacircular";
const numDictTypeRegistry = new DefaultMap(a => ({numDict: a}));
export const numDictType = a => numDictTypeRegistry.getdefault(a, true);
import { makeGeneric } from "../generics/generics.js";
import { addDouble, mulDouble } from "../primitives/double.js";
import { addInt, mulInt } from "../primitives/int.js";
import { Type, typedFnType, typedFnType2 } from "../metacircular.js";
import { Double, Int } from "../primitives/symbols.js";
import { numDictType } from "./num_type.js";
export const getAdd = numDict => numDict.add;
export const getMul = numDict => numDict.mul;
@ -40,8 +39,8 @@ const DoubleNumDict = {
}
export const ModuleNumInstances = {l:[
{i: IntNumDict , t: NumDict},
{i: DoubleNumDict, t: NumDict},
{i: IntNumDict , t: numDictType(Int)},
{i: DoubleNumDict, t: numDictType(Double)},
]};
// mapping from type to type class implementation

23
typeclasses/num.test.js Normal file
View 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
View 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
View file

@ -0,0 +1 @@
// const resolveTypeClass = (mapping, )