Add product and sum types

This commit is contained in:
Joeri Exelmans 2025-03-17 17:54:42 +01:00
parent 6023efc295
commit 574651ccb7
18 changed files with 209 additions and 157 deletions

92
main.js
View file

@ -6,10 +6,11 @@ import {Int_to_Int_to_Int, ModuleInt, mulInt} from "./primitives/int.js";
import {Int} from "./primitives/symbols.js";
import { makeSquare } from "./lib/square.js";
import {makeIdFn} from "./lib/id.js";
import {ModuleLiterals} from "./lib/literals.js";
import {ModuleValues} from "./lib/values.js";
import {DefaultMap} from "./util.js";
import { ModuleModule } from "./structures/module.js";
import { ModuleList } from "./structures/list.js";
class Context {
constructor(mod) {
@ -24,46 +25,10 @@ class Context {
this.addInstance({i, t});
}
// console.log(this.instances.m);
// const callEveryFunction = ({i,t}, indent=0) => {
// for (const fntype of this.functionsFrom.getdefault(t)) {
// for (const fn of this.instances.getdefault(fntype)) {
// console.log(" ", fn, ':', fntype);
// const result = fn(i);
// const resultType = getOut(fntype);
// console.log(" ".repeat(indent), i, '->', result);
// if (this.types.getdefault(resultType).includes(Function)) {
// if (indent < 2) {
// callEveryFunction({i: result, t: resultType}, indent+1)
// }
// }
// }
// }
// };
// const callFunction = (typ, fns) => {
// console.log("Functions callable on", typ, ":");
// for (const fn of fns) {
// console.log(" ", fn);
// for (const FN of this.instances.getdefault(fn)) {
// console.log(" ", FN);
// for (const i of this.instances.getdefault(typ)) {
// const result = FN(i);
// const resultType = getOut(fn);
// console.log(" ", i, '->', result);
// if (this.types.getdefault(resultType).includes(Function)) {
// callFunction(resultType, )
// }
// }
// }
// }
// };
const callFunctionOnEveryValue = (fn, fnType, indent=1) => {
const inType = getIn(fnType);
const outType = getOut(fnType);
console.log();
console.log("--".repeat(indent), fn, ':', fnType);
for (const i of this.instances.getdefault(inType)) {
const result = fn(i);
@ -83,54 +48,6 @@ class Context {
}
}
// for (const [i, ts] of this.types.m.entries()) {
// for (const t of ts) {
// callEveryFunction({i,t});
// }
// }
// // Conformance check
// for (const fn of this.instances.getdefault(conformanceCheck)) {
// const t = fn.inType;
// for (const i of this.instances.getdefault(t)) {
// if (!fn.fn(i)) {
// console.warn(i, 'is not conform', 'to', t);
// }
// else {
// console.info('👍', i, 'conforms to', t);
// }
// }
// }
// const MAXDEPTH = 0;
// let calls = 0;
// const tryEveryFunction = ({i, t}, maxDepth) => {
// calls++;
// for (const fn of this.functionsFrom.getdefault(t)) {
// const outType = getOut(fn);
// // console.log(" ".repeat(MAXDEPTH-maxDepth) + 'calling', fn, 'on', i);
// const result = fn(i);
// console.log(" ".repeat(MAXDEPTH-maxDepth), result, 'should be of type', outType);
// if (outType === TypeLink && result.t === undefined) {
// console.warn(" ".repeat(MAXDEPTH-maxDepth) + ' ^ invalid')
// }
// if (outType === Int && (typeof result) !== "number") {
// console.warn(" ".repeat(MAXDEPTH-maxDepth) + ' ^ invalid')
// }
// this.addInstance({i: result, t: outType});
// if (maxDepth > 0) {
// tryEveryFunction({i: result, t: outType}, maxDepth-1);
// }
// }
// }
// for (const {i, t} of mod) {
// tryEveryFunction({i, t}, MAXDEPTH);
// }
// console.log("calls:", calls);
}
addInstance({i, t}) {
@ -165,6 +82,7 @@ const ctx = new Context([
...makeIdFn(Int),
...makeSquare({i: mulInt, t: Int_to_Int_to_Int}),
...makeSquare({i: mulDouble, t: Double_to_Double_to_Double}),
...ModuleLiterals,
...ModuleList,
...ModuleValues,
...ModuleModule,
]);