diff --git a/interfaces/serializable.js b/interfaces/serializable.js index 1c1d2f7..abac7f6 100644 --- a/interfaces/serializable.js +++ b/interfaces/serializable.js @@ -1,10 +1,10 @@ -import { fnType, getListType } from "../type_registry.js"; +import { fnType, lsType } from "../type_registry.js"; import {Type, Function} from "../metacircular.js"; import { Byte } from "../primitives/symbols.js"; export const Serializable = Symbol('Serializable'); -const ListOfByte = getListType(Byte); +const ListOfByte = lsType(Byte); const serializeFnType = fnType({in: Serializable, out: ListOfByte}); const deserializeFnType = fnType({in: ListOfByte, out: Serializable}); diff --git a/lib/values.js b/lib/values.js index ec46389..ea9df41 100644 --- a/lib/values.js +++ b/lib/values.js @@ -2,15 +2,15 @@ import {Int, Bool, Double, Byte} from "../primitives/symbols.js"; import { makeListModule } from "../structures/list_common.js"; import { makeProductType } from "../structures/product.js"; import { makeSumType } from "../structures/sum.js"; -import { getListType, prodType, sumType } from "../type_registry.js"; +import { lsType, prodType, sumType } from "../type_registry.js"; -const ListOfDouble = getListType(Double); +const ListOfDouble = lsType(Double); const ListOfDoubleModule = makeListModule(Double); -const ListOfListOfDouble = getListType(ListOfDouble); +const ListOfListOfDouble = lsType(ListOfDouble); const ListOfListOfDoubleModule = makeListModule(ListOfDouble); -const ListOfByte = getListType(Byte); +const ListOfByte = lsType(Byte); const ListOfByteModule = makeListModule(Byte); export const ModuleValues = [ diff --git a/structures/list.js b/structures/list.js index b2f528a..f1d5e9c 100644 --- a/structures/list.js +++ b/structures/list.js @@ -1,4 +1,4 @@ -import { fnType, getListType } from "../type_registry.js"; +import { fnType, lsType } from "../type_registry.js"; import {Type, Function} from "../metacircular.js"; import { makeListModule } from "./list_common.js"; import { Module } from "./module.js"; @@ -7,7 +7,7 @@ const Type_to_Type = fnType({in: Type, out: Type}); const Type_to_Module = fnType({in: Type, out: Module}); export const ModuleList = [ - {i: getListType , t: Type_to_Type}, + {i: lsType , t: Type_to_Type}, {i: Type_to_Type , t: Function}, {i: makeListModule, t: Type_to_Module}, diff --git a/structures/list_common.js b/structures/list_common.js index fe8762a..f1dbc45 100644 --- a/structures/list_common.js +++ b/structures/list_common.js @@ -1,4 +1,4 @@ -import { fnType, getListType } from "../type_registry.js"; +import { fnType, lsType } from "../type_registry.js"; import {Type, Function} from "../metacircular.js"; import {Int, Byte} from "../primitives/symbols.js"; @@ -22,7 +22,7 @@ const byteListImpl = { export const makeListModule = elementType => { // List type depends on elementType // generating it another time, will give the same type (structurally equivalent): - const ListOfElement = getListType(elementType); + const ListOfElement = lsType(elementType); const getFnType1 = fnType({in: Int , out: elementType}); const getFnType = fnType({in: ListOfElement, out: getFnType1}); diff --git a/structures/module.js b/structures/module.js index 5f1e6ac..528206f 100644 --- a/structures/module.js +++ b/structures/module.js @@ -1,7 +1,7 @@ import { makeListModule } from "./list_common.js"; import { Typed } from "../typed.js"; -import { getListType } from "../type_registry.js"; +import { lsType } from "../type_registry.js"; -export const Module = getListType(Typed); // a Module is a list of Typeds +export const Module = lsType(Typed); // a Module is a list of Typeds export const ModuleModule = makeListModule(Typed); // the module containing operations on Module diff --git a/structures/product.js b/structures/product.js index 9814a06..9dfec02 100644 --- a/structures/product.js +++ b/structures/product.js @@ -1,5 +1,5 @@ import { fnType, prodType } from "../type_registry.js"; -import { Function } from "../metacircular.js"; +import { Function, Type } from "../metacircular.js"; // In JS, all products are encoded in the same way: const constructor = left => right => ({left, right}); @@ -8,15 +8,17 @@ 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 pType = prodType(leftType, rightType); - const leftFnType = fnType({in: productType, out: leftType}); - const rightFnType = fnType({in: productType, out: rightType}); + const leftFnType = fnType({in: pType, out: leftType}); + const rightFnType = fnType({in: pType, out: rightType}); - const constructorBoundType = fnType({in: rightType, out: productType}); + const constructorBoundType = fnType({in: rightType, out: pType}); const constructorType = fnType({in: leftType , out: constructorBoundType}); return [ + {i: pType, t: Type}, + {i: getLeft , t: leftFnType}, {i: getRight , t: rightFnType}, {i: leftFnType , t: Function}, diff --git a/structures/sum.js b/structures/sum.js index 85bbf27..8d38982 100644 --- a/structures/sum.js +++ b/structures/sum.js @@ -5,6 +5,8 @@ import { Module } from "./module.js"; const constructorLeft = left => ({variant: "L", value: left }); const constructorRight = right => ({variant: "R", value: right}); +// signature: +// sum-type -> (leftType -> resultType, rightType -> resultType) -> resultType const match = sum => handlers => sum.variant === "L" ? handlers.left(sum.value) : handlers.right(sum.value); @@ -31,6 +33,7 @@ export const makeSumType = (leftType, rightType) => { }; return [ + {i: sType , t: Type}, {i: constructorLeft , t: constructorLeftType}, {i: constructorRight , t: constructorRightType}, {i: constructorLeftType , t: Function}, diff --git a/type_registry.js b/type_registry.js index dbc0ec8..2d6b880 100644 --- a/type_registry.js +++ b/type_registry.js @@ -20,14 +20,16 @@ export const fnType = ({in: inType, out: outType}) => { // Global store of list types: const listTypes = new Map(); -export function getListType(elementType) { +export const lsType = (elementType) => { if (listTypes.has(elementType)) { // only generate each list type once // this would not be necessary if we could define our own equality and hash functions on objects in JavaScript. return listTypes.get(elementType); } else { - const type = Symbol('ListOf:' + elementType.toString()); + const type = { + listOf: elementType, + }; listTypes.set(elementType, type); return type; }