Compare commits
2 commits
97b4e83379
...
a826f475c3
| Author | SHA1 | Date | |
|---|---|---|---|
| a826f475c3 | |||
| e892ade34d |
9 changed files with 198 additions and 19 deletions
172
generic.js
Normal file
172
generic.js
Normal file
|
|
@ -0,0 +1,172 @@
|
||||||
|
import { Bool, Int } from "./primitives/symbols.js";
|
||||||
|
import { fnType, lsType } from "./type_registry.js";
|
||||||
|
import { deepEqual } from "./util.js";
|
||||||
|
|
||||||
|
// let nextSymbol = 'a';
|
||||||
|
|
||||||
|
// export const makeGeneric = callback => {
|
||||||
|
// const typeParam = Symbol(nextSymbol);
|
||||||
|
// nextSymbol = String.fromCharCode(nextSymbol.charCodeAt(0) + 1);
|
||||||
|
// return {
|
||||||
|
// typeParam,
|
||||||
|
// type: callback(typeParam),
|
||||||
|
// };
|
||||||
|
// };
|
||||||
|
|
||||||
|
|
||||||
|
import { inspect } from 'node:util';
|
||||||
|
|
||||||
|
function pretty(obj) {
|
||||||
|
return inspect(obj, {colors: true});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const matchGeneric = (
|
||||||
|
{typeVars: formalTypeVars, type: formalType},
|
||||||
|
{typeVars: actualTypeVars, type: actualType},
|
||||||
|
) => {
|
||||||
|
if (deepEqual(formalType, actualType)) {
|
||||||
|
return {
|
||||||
|
substitutions: new Map(),
|
||||||
|
typeVars: new Set([
|
||||||
|
...actualTypeVars,
|
||||||
|
...formalTypeVars]),
|
||||||
|
type: actualType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formalTypeVars.has(formalType)) {
|
||||||
|
// simplest case: substitute formal type param
|
||||||
|
return {
|
||||||
|
substitutions: new Map([[formalType, actualType]]),
|
||||||
|
typeVars: new Set([
|
||||||
|
...actualTypeVars,
|
||||||
|
...formalTypeVars].filter(a => a !== formalType)),
|
||||||
|
type: actualType,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formalType.in !== undefined) {
|
||||||
|
// function type
|
||||||
|
if (actualType.in === undefined) {
|
||||||
|
throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// both are function type
|
||||||
|
const inType = matchGeneric({typeVars: formalTypeVars, type: formalType.in}, {typeVars: actualTypeVars, type: actualType.in});
|
||||||
|
const outType = matchGeneric({typeVars: formalTypeVars, type: formalType.out}, {typeVars: actualTypeVars, type: actualType.out});
|
||||||
|
// check for conflicts between 'in' and 'out' subsitutions
|
||||||
|
for (const [typeVar, actual] of inType.substitutions) {
|
||||||
|
if (outType.substitutions.has(typeVar)) {
|
||||||
|
if (!deepEqual(actual, outType.substitutions.get(typeVar))) {
|
||||||
|
throw new Error(`conflicting assignment for ${pretty(typeVar)}: ${pretty(a)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// merge substitutions
|
||||||
|
const newSubstitutions = new Map([
|
||||||
|
...inType.substitutions,
|
||||||
|
...outType.substitutions,
|
||||||
|
]);
|
||||||
|
const newTypeVars = new Set([
|
||||||
|
...actualTypeVars,
|
||||||
|
...formalTypeVars].filter(a => !newSubstitutions.has(a)));
|
||||||
|
return {
|
||||||
|
substitutions: newSubstitutions,
|
||||||
|
typeVars: newTypeVars,
|
||||||
|
type: fnType({in: inType.type, out: outType.type}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formalType.listOf !== undefined) {
|
||||||
|
// list type
|
||||||
|
if (actualType.listOf === undefined) {
|
||||||
|
throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// both are list type
|
||||||
|
const elementType = matchGeneric(
|
||||||
|
{typeVars: formalTypeVars, type: formalType.listOf},
|
||||||
|
{typeVars: actualTypeVars, type: actualType.listOf});
|
||||||
|
return {
|
||||||
|
substitutions: elementType.substitutions,
|
||||||
|
typeVars: new Set([
|
||||||
|
...actualTypeVars,
|
||||||
|
...formalTypeVars].filter(a => !elementType.substitutions.has(a))),
|
||||||
|
type: lsType(elementType.type),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error("i don't know what to do :(")
|
||||||
|
};
|
||||||
|
|
||||||
|
export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
|
||||||
|
return matchGeneric({typeVars, type: formalType}, {typeVars: new Set(), type: actualType});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const substitute = (type, substitutions) => {
|
||||||
|
if (substitutions.has(type)) {
|
||||||
|
return substitutions.get(type);
|
||||||
|
}
|
||||||
|
if (type.listOf !== undefined) {
|
||||||
|
// list type
|
||||||
|
return lsType(substitute(type.listOf, substitutions));
|
||||||
|
}
|
||||||
|
if (type.in !== undefined) {
|
||||||
|
// function type
|
||||||
|
return fnType({
|
||||||
|
in: substitute(type.in, substitutions),
|
||||||
|
out: substitute(type.out, substitutions),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
throw new Error("i don't know what to do :(");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const assign = (genFnType, paramType) => {
|
||||||
|
const matchedInType = matchGeneric({
|
||||||
|
typeVars: genFnType.typeVars,
|
||||||
|
type: genFnType.type.in,
|
||||||
|
}, paramType);
|
||||||
|
const substitutedOutType = substitute(genFnType.type.out, matchedInType.substitutions);
|
||||||
|
return {
|
||||||
|
typeVars: matchedInType.typeVars,
|
||||||
|
type: substitutedOutType,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const a = Symbol('a');
|
||||||
|
const formal = {
|
||||||
|
typeVars: new Set([a]),
|
||||||
|
type: fnType({in: a, out: Int}),
|
||||||
|
};
|
||||||
|
const actual = fnType({in: lsType(Bool), out: Int});
|
||||||
|
console.log(matchConcrete(formal, actual));
|
||||||
|
|
||||||
|
|
||||||
|
const b = Symbol('b');
|
||||||
|
const c = Symbol('c');
|
||||||
|
const formal2 = {
|
||||||
|
typeVars: new Set([a, b]),
|
||||||
|
type: fnType({in: fnType({in: a, out: a}), out: b}),
|
||||||
|
}
|
||||||
|
const actual2 = {
|
||||||
|
typeVars: new Set([c]),
|
||||||
|
type: fnType({in: fnType({in: Bool, out: Bool}), out: c}),
|
||||||
|
}
|
||||||
|
console.log(matchGeneric(formal2, actual2));
|
||||||
|
|
||||||
|
|
||||||
|
const mapFnType = {
|
||||||
|
typeVars: new Set([a, b]),
|
||||||
|
type: fnType({
|
||||||
|
in: fnType({in: a, out: b}),
|
||||||
|
out: fnType({in: lsType(a), out: lsType(b)}),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const idFnType = {
|
||||||
|
typeVars: new Set([c]),
|
||||||
|
type: fnType({in: c, out: c}),
|
||||||
|
};
|
||||||
|
|
||||||
|
// should be: listOf(c) -> listOf(c)
|
||||||
|
console.log(assign(mapFnType, idFnType));
|
||||||
|
|
@ -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 {Type, Function} from "../metacircular.js";
|
||||||
import { Byte } from "../primitives/symbols.js";
|
import { Byte } from "../primitives/symbols.js";
|
||||||
|
|
||||||
export const Serializable = Symbol('Serializable');
|
export const Serializable = Symbol('Serializable');
|
||||||
|
|
||||||
const ListOfByte = getListType(Byte);
|
const ListOfByte = lsType(Byte);
|
||||||
|
|
||||||
const serializeFnType = fnType({in: Serializable, out: ListOfByte});
|
const serializeFnType = fnType({in: Serializable, out: ListOfByte});
|
||||||
const deserializeFnType = fnType({in: ListOfByte, out: Serializable});
|
const deserializeFnType = fnType({in: ListOfByte, out: Serializable});
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,15 @@ import {Int, Bool, Double, Byte} from "../primitives/symbols.js";
|
||||||
import { makeListModule } from "../structures/list_common.js";
|
import { makeListModule } from "../structures/list_common.js";
|
||||||
import { makeProductType } from "../structures/product.js";
|
import { makeProductType } from "../structures/product.js";
|
||||||
import { makeSumType } from "../structures/sum.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 ListOfDoubleModule = makeListModule(Double);
|
||||||
|
|
||||||
const ListOfListOfDouble = getListType(ListOfDouble);
|
const ListOfListOfDouble = lsType(ListOfDouble);
|
||||||
const ListOfListOfDoubleModule = makeListModule(ListOfDouble);
|
const ListOfListOfDoubleModule = makeListModule(ListOfDouble);
|
||||||
|
|
||||||
const ListOfByte = getListType(Byte);
|
const ListOfByte = lsType(Byte);
|
||||||
const ListOfByteModule = makeListModule(Byte);
|
const ListOfByteModule = makeListModule(Byte);
|
||||||
|
|
||||||
export const ModuleValues = [
|
export const ModuleValues = [
|
||||||
|
|
|
||||||
|
|
@ -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 {Type, Function} from "../metacircular.js";
|
||||||
import { makeListModule } from "./list_common.js";
|
import { makeListModule } from "./list_common.js";
|
||||||
import { Module } from "./module.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});
|
const Type_to_Module = fnType({in: Type, out: Module});
|
||||||
|
|
||||||
export const ModuleList = [
|
export const ModuleList = [
|
||||||
{i: getListType , t: Type_to_Type},
|
{i: lsType , t: Type_to_Type},
|
||||||
{i: Type_to_Type , t: Function},
|
{i: Type_to_Type , t: Function},
|
||||||
|
|
||||||
{i: makeListModule, t: Type_to_Module},
|
{i: makeListModule, t: Type_to_Module},
|
||||||
|
|
|
||||||
|
|
@ -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 {Type, Function} from "../metacircular.js";
|
||||||
import {Int, Byte} from "../primitives/symbols.js";
|
import {Int, Byte} from "../primitives/symbols.js";
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ const byteListImpl = {
|
||||||
export const makeListModule = elementType => {
|
export const makeListModule = elementType => {
|
||||||
// List<elementType> type depends on elementType
|
// List<elementType> type depends on elementType
|
||||||
// generating it another time, will give the same type (structurally equivalent):
|
// 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 getFnType1 = fnType({in: Int , out: elementType});
|
||||||
const getFnType = fnType({in: ListOfElement, out: getFnType1});
|
const getFnType = fnType({in: ListOfElement, out: getFnType1});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { makeListModule } from "./list_common.js";
|
import { makeListModule } from "./list_common.js";
|
||||||
import { Typed } from "../typed.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
|
export const ModuleModule = makeListModule(Typed); // the module containing operations on Module
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { fnType, prodType } from "../type_registry.js";
|
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:
|
// In JS, all products are encoded in the same way:
|
||||||
const constructor = left => right => ({left, right});
|
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).
|
// Given two types A and B, create the type (A × B).
|
||||||
export const makeProductType = (leftType, rightType) => {
|
export const makeProductType = (leftType, rightType) => {
|
||||||
const productType = prodType(leftType, rightType);
|
const pType = prodType(leftType, rightType);
|
||||||
|
|
||||||
const leftFnType = fnType({in: productType, out: leftType});
|
const leftFnType = fnType({in: pType, out: leftType});
|
||||||
const rightFnType = fnType({in: productType, out: rightType});
|
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});
|
const constructorType = fnType({in: leftType , out: constructorBoundType});
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
{i: pType, t: Type},
|
||||||
|
|
||||||
{i: getLeft , t: leftFnType},
|
{i: getLeft , t: leftFnType},
|
||||||
{i: getRight , t: rightFnType},
|
{i: getRight , t: rightFnType},
|
||||||
{i: leftFnType , t: Function},
|
{i: leftFnType , t: Function},
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import { Module } from "./module.js";
|
||||||
const constructorLeft = left => ({variant: "L", value: left });
|
const constructorLeft = left => ({variant: "L", value: left });
|
||||||
const constructorRight = right => ({variant: "R", value: right});
|
const constructorRight = right => ({variant: "R", value: right});
|
||||||
|
|
||||||
|
// signature:
|
||||||
|
// sum-type -> (leftType -> resultType, rightType -> resultType) -> resultType
|
||||||
const match = sum => handlers => sum.variant === "L"
|
const match = sum => handlers => sum.variant === "L"
|
||||||
? handlers.left(sum.value)
|
? handlers.left(sum.value)
|
||||||
: handlers.right(sum.value);
|
: handlers.right(sum.value);
|
||||||
|
|
@ -31,6 +33,7 @@ export const makeSumType = (leftType, rightType) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
{i: sType , t: Type},
|
||||||
{i: constructorLeft , t: constructorLeftType},
|
{i: constructorLeft , t: constructorLeftType},
|
||||||
{i: constructorRight , t: constructorRightType},
|
{i: constructorRight , t: constructorRightType},
|
||||||
{i: constructorLeftType , t: Function},
|
{i: constructorLeftType , t: Function},
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,16 @@ export const fnType = ({in: inType, out: outType}) => {
|
||||||
|
|
||||||
// Global store of list types:
|
// Global store of list types:
|
||||||
const listTypes = new Map();
|
const listTypes = new Map();
|
||||||
export function getListType(elementType) {
|
export const lsType = (elementType) => {
|
||||||
if (listTypes.has(elementType)) {
|
if (listTypes.has(elementType)) {
|
||||||
// only generate each list type once
|
// 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.
|
// this would not be necessary if we could define our own equality and hash functions on objects in JavaScript.
|
||||||
return listTypes.get(elementType);
|
return listTypes.get(elementType);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const type = Symbol('ListOf:' + elementType.toString());
|
const type = {
|
||||||
|
listOf: elementType,
|
||||||
|
};
|
||||||
listTypes.set(elementType, type);
|
listTypes.set(elementType, type);
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue