29 lines
1 KiB
JavaScript
29 lines
1 KiB
JavaScript
import { fnType, prodType } from "../type_registry.js";
|
||
import { Function } from "../metacircular.js";
|
||
|
||
// In JS, all products are encoded in the same way:
|
||
const constructor = left => right => ({left, right});
|
||
const getLeft = product => product.left;
|
||
const getRight = product => product.right;
|
||
|
||
// Given two types A and B, create the type (A × B).
|
||
export const makeProductType = (leftType, rightType) => {
|
||
const productType = prodType(leftType, rightType);
|
||
|
||
const leftFnType = fnType({in: productType, out: leftType});
|
||
const rightFnType = fnType({in: productType, out: rightType});
|
||
|
||
const constructorBoundType = fnType({in: rightType, out: productType});
|
||
const constructorType = fnType({in: leftType , out: constructorBoundType});
|
||
|
||
return [
|
||
{i: getLeft , t: leftFnType},
|
||
{i: getRight , t: rightFnType},
|
||
{i: leftFnType , t: Function},
|
||
{i: rightFnType, t: Function},
|
||
|
||
{i: constructor , t: constructorType},
|
||
{i: constructorType , t: Function},
|
||
{i: constructorBoundType, t: Function},
|
||
];
|
||
};
|