dope2/structures/product.js

31 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, Type } 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 pType = prodType(leftType, rightType);
const leftFnType = fnType({in: pType, out: leftType});
const rightFnType = fnType({in: pType, out: rightType});
const constructorBoundType = fnType({in: rightType, out: pType});
const constructorType = fnType({in: leftType , out: constructorBoundType});
return {l:[
{i: pType, t: Type},
{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},
]};
};