Add product and sum types
This commit is contained in:
parent
6023efc295
commit
574651ccb7
18 changed files with 209 additions and 157 deletions
29
structures/product.js
Normal file
29
structures/product.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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},
|
||||
];
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue