parser for types + start moving all types to separate modules

This commit is contained in:
Joeri Exelmans 2025-05-06 23:41:12 +02:00
parent 8eec5b9239
commit 1d826ea8d4
11 changed files with 277 additions and 88 deletions

View file

@ -3,52 +3,8 @@
// A Product-type always has only two fields, called "left" and "right".
// Product-types of more fields (called Structs) can be constructed by nesting Product-types.
import { makeGeneric } from "../generics/generics.js";
import { GenericType, Type } from "../primitives/types.js";
import { typedFnType } from "./types.js";
import { prodType } from "./types.js";
// In JS, all products are encoded in the same way:
export const newProduct = l => r => ({l, r});
export const getLeft = product => product.l;
export const getRight = product => product.r;
export const ModuleProduct = {l: [
// binary type constructor
// Type -> Type -> Type
...typedFnType(prodType, fnType =>
fnType
(Type)
(fnType
(Type)
(Type)
)
),
// a -> b -> (a, b)
...typedFnType(newProduct, fnType =>
makeGeneric((a, b) =>
fnType
(a)
(fnType
(b)
(prodType(() => a)(() => b))
)
), GenericType),
// (a, b) -> a
...typedFnType(getLeft, fnType =>
makeGeneric((a, b) =>
fnType
(prodType(() => a)(() => b))
(a)
), GenericType),
// (a, b) -> b
...typedFnType(getRight, fnType =>
makeGeneric((a, b) =>
fnType
(prodType(() => a)(() => b))
(b)
), GenericType),
]};

View file

@ -0,0 +1,27 @@
import { makeGeneric } from "../generics/generics.js";
import { Type, GenericType } from "../primitives/types.js";
import { newProduct, getLeft, getRight } from "./product.js";
import { typedFnType, prodType } from "./types.js";
export const ModuleProduct = {
l: [
// binary type constructor
// Type -> Type -> Type
...typedFnType(prodType, fnType => fnType(Type)(fnType(Type)(Type)
)
),
// a -> b -> (a, b)
...typedFnType(newProduct, fnType => makeGeneric((a, b) => fnType(a)(fnType(b)(prodType(() => a)(() => b))
)
), GenericType),
// (a, b) -> a
...typedFnType(getLeft, fnType => makeGeneric((a, b) => fnType(prodType(() => a)(() => b))(a)
), GenericType),
// (a, b) -> b
...typedFnType(getRight, fnType => makeGeneric((a, b) => fnType(prodType(() => a)(() => b))(b)
), GenericType),
]
};

View file

@ -1,7 +1,3 @@
import { fnType, setType } from "./types.js";
import { Int } from "../primitives/types.js";
import { makeGeneric } from "../generics/generics.js";
import createRBTree from "functional-red-black-tree";
import { inspect } from "node:util";
@ -9,6 +5,7 @@ export class RBTreeWrapper {
constructor(tree) {
this.tree = tree;
}
// pretty print to console
[inspect.custom](depth, options, inspect) {
const entries = [];
this.tree.forEach((key,val) => {entries.push(`${inspect(key)} => ${inspect(val)}`);});
@ -19,7 +16,6 @@ export class RBTreeWrapper {
// (a -> a -> Int) -> Set(a)
export const emptySet = compareFn => new RBTreeWrapper(createRBTree((x, y) => compareFn(x)(y)));
// const emptySetType = makeGeneric(a => fnType(() => fnType(a)(fnType(a)(Int)))(() => set(() => a)));
export const has = set => key => set.tree.get(key) === true;
export const add = set => key => set.tree.get(key) === true ? set : new RBTreeWrapper(set.tree.insert(key, true));
@ -42,33 +38,3 @@ export const read = iter => ifNotDone => ifDone => {
export const forEach = set => fn => {
set.tree.forEach(key => { fn(key); });
};
export const ModuleSet = {l:[
// // Type -> Type
// ...typedFnType(setType, fnType =>
// fnType
// /* in */ (Type)
// /* out */ (Type)
// ),
// {i: emptySet , t: emptySetType},
// {i: emptySetType, t: GenericType },
// ...typedFnType(has, fnType =>
// makeGeneric(a =>
// fnType
// /* in */ (set(() => a))
// /* out */ (fnType
// /* in */ (a)
// /* out */ (Bool)
// )), GenericType),
// ...typedFnType(add, fnType =>
// makeGeneric(a =>
// fnType
// /* in */ (set(() => a))
// /* out */ (fnType
// /* in */ (a)
// /* out */ (set(() => a))
// )), GenericType),
]};

31
structures/set.types.js Normal file
View file

@ -0,0 +1,31 @@
import { makeGeneric } from "../generics/generics.js";
import { Int } from "../primitives/types.js";
import { emptySet, has, add } from "./set.js";
import { fnType, setType } from "./types.js";
const emptySetType = makeGeneric(a =>
fnType
// comparison function:
(_ => fnType
(_ => a)
(_ => fnType(_ => a)(_ => Int)))
// the set:
(_ => setType(_ => a))
);
export const ModuleSet = {
l: [
// Type -> Type
...typedFnType(setType, fnType => fnType(_ => Type)(_ => Type)
),
{ i: emptySet, t: emptySetType },
{ i: emptySetType, t: GenericType },
...typedFnType(has, fnType => makeGeneric(a => fnType(_ => setType(_ => a))(_ => fnType(_ => a)(_ => Bool)
)), GenericType),
...typedFnType(add, fnType => makeGeneric(a => fnType(setType(_ => a))(fnType(a)(setType(_ => a))
)), GenericType),
]
};

View file

@ -52,3 +52,8 @@ export const lsType = makeTypeConstructor(symbolList)(1);
export const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet)(1);
// Dict type
export const symbolDict = Symbol('Dict');
export const dictType = makeTypeConstructor(symbolDict)(2);