dope2/structures/product.js

29 lines
1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 left = product => product.left;
const right = 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: left , t: leftFnType},
{i: right , t: rightFnType},
{i: leftFnType , t: Function},
{i: rightFnType, t: Function},
{i: constructor , t: constructorType},
{i: constructorType , t: Function},
{i: constructorBoundType, t: Function},
];
};