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

38
lib/id.js Normal file
View file

@ -0,0 +1,38 @@
import {Function} from "../metacircular.js";
// import {Typed} from "../typed.js";
// import {Bool} from "../primitives/symbols.js";
// import {deepEqual} from "../util.js";
// import {Conformable, conformanceCheck} from "../typeclasses/conformable.js";
// // Identity function
// const idBoundToType = Symbol('idBoundToType');
// const id = type => x => x;
// const idFn = {name: "id", inType: Type, outType: {inType: Type, outType: Type}, fn: id};
// const idIsConform = {
// name: "isConform",
// inType: idBoundToType,
// outType: Bool,
// fn: fn => deepEqual(fnIn(fn), fnOut(fn))
// }
// export const ModuleId = [
// {i: idBoundToType, t: Type},
// {i: idFn, t: Function},
// ... makeTypedFnInOut({f: idBoundToType, inType: Type, outType: Type}),
// {i: idBoundToType, t: Conformable},
// {i: idIsConform, t: conformanceCheck},
// ];
// generates explicitly typed id-function
export const makeIdFn = typ => {
const Typ_to_Typ = {in: typ, out: typ};
const id = x => x;
return [
{i: id , t: Typ_to_Typ},
{i: Typ_to_Typ, t: Function},
];
};

30
lib/literals.js Normal file
View file

@ -0,0 +1,30 @@
import {Int, Bool, Double} from "../primitives/symbols.js";
import { getListType, makeListModule } from "../structures/list_common.js";
const ListOfBool = getListType(Bool);
const ListOfBoolModule = makeListModule(Bool);
const ListOfInt = getListType(Int);
const ListOfIntModule = makeListModule(Int);
const ListOfListOfInt = getListType(ListOfInt);
const ListOfListOfIntModule = makeListModule(ListOfInt);
export const ModuleLiterals = [
{i: 0n, t: Int},
{i: 42n, t: Int},
{i: false, t: Bool},
{i: 3.14159265359, t: Double},
{i: {l:[42n, 43n]}, t: ListOfInt},
// {i: [[42n, 43n]], t: ListOfListOfInt},
// i'm lazy
...ListOfIntModule,
// ...ListOfBoolModule,
// ...ListOfListOfIntModule,
];

49
lib/square.js Normal file
View file

@ -0,0 +1,49 @@
import {Function, getIn, getOut} from "../metacircular.js";
import {Module} from "../structures/module.js";
import { deepEqual } from "../util.js";
import { Typed } from "../typed.js";
// import {Num, NumDict, getType, getMul} from "../typeclasses/num.js";
// const squareBoundToType = Symbol('squareBoundToType'); // the type of e.g., squareOfInt
// // returning a function + its signature is the only way to make the outType of square depend on its input
// const square = numDict => {
// const typ = getType(numDict);
// const mul = getMul(numDict);
// return {
// name: "squareOf:" + typ.toString(),
// inType: typ,
// outType: typ,
// fn: x => mul(x)(x),
// };
// };
// export const ModuleSquare = [
// {i: squareBoundToType, t: Type},
// {i: {name: "square", inType: NumDict, outType: squareBoundToType, fn: square}, t: Function},
// ...makeTypedFnInOut({f: squareBoundToType, inType: Num, outType: Num}),
// ];
export const makeSquare = ({i: mul, t: mulFunction}) => {
const numType = getIn(mulFunction);
const boundMulFunction = getOut(mulFunction);
if (!deepEqual(getOut(boundMulFunction), numType) || !deepEqual(getIn(boundMulFunction), numType)) {
console.log(getOut(boundMulFunction), getIn(boundMulFunction), numType);
throw new Error("invalid signature");
}
const square = x => mul(x)(x);
const squareFunction = {in: numType, out: numType};
return [
{i: square , t: squareFunction},
{i: squareFunction, t: Function},
];
};
const makeSquareType = {in: Typed, out: Module};
export const ModuleSquare = [
{i: makeSquare , t: makeSquareType},
{i: makeSquareType, t: Function},
];