dope2/typeclasses/num.test.js
2025-03-22 10:33:35 +01:00

29 lines
939 B
JavaScript

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 { getMul, NumInstances } from "./num.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,
}),
}));
console.log("should be: Int -> Int");
console.log(assign(squareFnType, makeGeneric(() => numDictType(Int))));
console.log("should be: Double -> Double");
console.log(assign(squareFnType, makeGeneric(() => numDictType(Double))));
// to call 'square' we need:
// - 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