This commit is contained in:
Joeri Exelmans 2025-03-24 17:28:07 +01:00
parent 6af72e525c
commit 145835ad5d
22 changed files with 153 additions and 90 deletions

View file

@ -1,5 +1,5 @@
import { makeGeneric } from "../generics/generics";
import { Type } from "../type";
import { Type } from "../primitives/types";
import { typedFnType } from "../structures/types";
import { Bool, Byte, Char, Double, Int } from "../primitives/types";
import { deepEqual } from "../util";
@ -32,10 +32,10 @@ const eq = x => y => deepEqual(x,y);
const EqDict = {eq};
export const EqInstances = new Map([
[Int, EqDict],
[Bool, EqDict],
[Int , EqDict],
[Bool , EqDict],
[Double, EqDict],
[Byte, EqDict],
[Char, EqDict],
[Type, EqDict],
[Byte , EqDict],
[Char , EqDict],
[Type , EqDict],
]);

View file

@ -1,7 +1,7 @@
import { makeGeneric } from "../generics/generics.js";
import { addDouble, mulDouble } from "../primitives/double.js";
import { addInt, mulInt } from "../primitives/int.js";
import { Type } from "../type.js";
import { Type } from "../primitives/types.js";
import { typedFnType, typedFnType2 } from "../structures/types.js";
import { Double, Int } from "../primitives/types.js";
import { numDictType } from "./num_type.js";
@ -10,14 +10,15 @@ export const getAdd = numDict => numDict.add;
export const getMul = numDict => numDict.mul;
// getAdd and getMul have same (generic) type:
// NumDict a -> a -> a -> a
const [getAddMulFnType, typesOfFns] = typedFnType2(fnType =>
makeGeneric(a => fnType({
in: numDictType(a),
out: fnType({
in: a,
out: fnType({in: a, out: a}),
}),
})));
makeGeneric(a =>
fnType
(numDictType(a))
(fnType
(a)
(fnType(a)(a))
)));
export const ModuleNum = {l:[
...typedFnType(numDictType, fnType => fnType({in: Type, out: Type})),
@ -51,4 +52,4 @@ export const ModuleNumInstances = {l:[
export const NumInstances = new Map([
[Int , IntNumDict ],
[Double, DoubleNumDict],
]);
]);

View file

@ -1,29 +1,28 @@
import { assign } from "../generics/generics.js";
import { makeGeneric } from "../generics/generics.js";
import { fnType } from "../structures/function.js";
import { Double, Int } from "../primitives/types.js";
import { fnType } from "../structures/types.js";
import { pretty } from "../util.js";
import { getMul, NumInstances } from "./num.js";
import { numDictType } from "./num_type.js";
const square = numDict => x => getMul(numDict)(x)(x);
// NumDict a -> a -> a
const squareFnType = makeGeneric(a =>
fnType({
in: numDictType(a),
out: fnType({
in: a,
out: a,
}),
}));
fnType
(numDictType(a))
(fnType(a)(a))
);
console.log("should be: Int -> Int");
console.log(assign(squareFnType, makeGeneric(() => numDictType(Int))));
console.log(pretty(assign(squareFnType, makeGeneric(() => numDictType(Int)))));
console.log("should be: Double -> Double");
console.log(assign(squareFnType, makeGeneric(() => numDictType(Double))));
console.log(pretty(assign(squareFnType, makeGeneric(() => numDictType(Double)))));
// to call 'square' we need:
// - the type of our argument (=Int)
// - access to a mapping from types to their typeclass instantiation
// - to know that our argument is 'Int'
console.log("");
console.log(square(NumInstances.get(Int))(42n)); // 1764n

View file

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