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

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, )