basic functionality, no generics

This commit is contained in:
Joeri Exelmans 2025-03-14 16:56:37 +01:00
commit a8260f2afb
17 changed files with 615 additions and 0 deletions

13
structures/list.js Normal file
View file

@ -0,0 +1,13 @@
import {Type, Function} from "../metacircular.js";
import { makeListModule } from "./list_common.js";
const Type_to_Type = {in: Type, out: Type};
const Type_to_Module = {in: Type, out: Module};
export const ModuleList = [
{i: getListType , t: Type_to_Type},
{i: Type_to_Type , t: Function},
{i: makeListModule, t: Type_to_Module},
{i: Type_to_Module, t: Function},
];

46
structures/list_common.js Normal file
View file

@ -0,0 +1,46 @@
import {Type, Function} from "../metacircular.js";
import {Int} from "../primitives/symbols.js";
// Global store of list types:
const listTypes = new Map();
export function getListType(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());
listTypes.set(elementType, type);
return type;
}
}
export const makeListModule = elementType => {
// List<elementType> type depends on elementType
// generating it another time, will give the same type (structurally equivalent):
const ListOfElement = getListType(elementType);
const emptyList = {l:[]};
const get = ls => i => ls.l[i];
const getBoundType = {in: Int, out: elementType}
const getType = {in: ListOfElement, out: getBoundType};
const push = ls => elem => ({l:ls.l.concat([elem])});
const pushBoundType = {in: elementType, out: ListOfElement};
const pushType = {in: ListOfElement, out: pushBoundType};
return [
{i: ListOfElement, t: Type},
{i: emptyList , t: ListOfElement},
{i: get , t: getType},
{i: getType , t: Function},
{i: getBoundType , t: Function},
{i: push , t: pushType},
{i: pushType , t: Function},
{i: pushBoundType, t: Function},
];
};

6
structures/module.js Normal file
View file

@ -0,0 +1,6 @@
import { getListType, makeListModule } from "./list_common.js";
import { Typed } from "../typed.js";
export const Module = getListType(Typed); // a Module is a list of Typeds
export const ModuleModule = makeListModule(Typed); // the module containing operations on Module