dope2/lib/square.js

50 lines
1.7 KiB
JavaScript

import {Function, getIn, getOut} from "../metacircular.js";
import {Module} from "../structures/module.js";
import { deepEqual } from "../util.js";
import { Typed } from "../typed.js";
import { fnType } from "../type_registry.js";
// import {Num, NumDict, getType, getMul} from "../typeclasses/num.js";
// const squareBoundToType = Symbol('squareBoundToType'); // the type of e.g., squareOfInt
// // returning a function + its signature is the only way to make the outType of square depend on its input
// const square = numDict => {
// const typ = getType(numDict);
// const mul = getMul(numDict);
// return {
// name: "squareOf:" + typ.toString(),
// inType: typ,
// outType: typ,
// fn: x => mul(x)(x),
// };
// };
// export const ModuleSquare = [
// {i: squareBoundToType, t: Type},
// {i: {name: "square", inType: NumDict, outType: squareBoundToType, fn: square}, t: Function},
// ...makeTypedFnInOut({f: squareBoundToType, inType: Num, outType: Num}),
// ];
export const makeSquare = ({i: mul, t: mulFunction}) => {
const numType = getIn(mulFunction);
const boundMulFunction = getOut(mulFunction);
if (!deepEqual(getOut(boundMulFunction), numType) || !deepEqual(getIn(boundMulFunction), numType)) {
console.log(getOut(boundMulFunction), getIn(boundMulFunction), numType);
throw new Error("invalid signature");
}
const square = x => mul(x)(x);
const squareFunction = fnType({in: numType, out: numType});
return {l:[
{i: square , t: squareFunction},
{i: squareFunction, t: Function},
]};
};
const makeSquareType = fnType({in: Typed, out: Module});
export const ModuleSquare = {l:[
{i: makeSquare , t: makeSquareType},
{i: makeSquareType, t: Function},
]};