From 6af72e525c3b8ab5579fa04631a2e1244703291b Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Mon, 24 Mar 2025 08:25:53 +0100 Subject: [PATCH] progress --- generics/generics.test.js | 6 +++++- main.js | 14 ++++++++++++-- structures/list.js | 16 ++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/generics/generics.test.js b/generics/generics.test.js index afb1dc7..0917773 100644 --- a/generics/generics.test.js +++ b/generics/generics.test.js @@ -36,5 +36,9 @@ const weirdFnType = makeGeneric(a => in: fnType({in: a, out: Int}), out: fnType({in: lsType(a), out: a}), })); +// we call this function with parameter of type (b -> b) ... +// giving these substitutions: +// a := b +// b := Int console.log("should be: [Int] -> Int"); -console.log(pretty(assign(weirdFnType, idFnType))); \ No newline at end of file +console.log(pretty(assign(weirdFnType, idFnType))); diff --git a/main.js b/main.js index 4dc5327..3f6efc6 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,7 @@ import { Type } from './type.js'; class Context { constructor(mod) { + this.mod = mod; this.types = new DefaultMap(() => new Set()); // instance to type this.instances = new DefaultMap(() => new Set()); // type to instance @@ -26,11 +27,18 @@ class Context { this.functionsTo .getdefault(t.params[1], true).add(fn); } } - } + } + } + + addToCtx({i, t}) { + return new Context({l:[ + ...this.mod.l, + {i, t}, + ]}) } } -const ctx = new Context({l:[ +let ctx = new Context({l:[ ...ModuleStd.l, ...ModulePoint.l, ]}); @@ -257,6 +265,8 @@ async function transform(i, t) { async function apply(i, fn, fnT) { const result = fn(i); const resultType = fnT.params[1]; + // update context with newly produced value + ctx = ctx.addToCtx({i: result, t: resultType}); const {strI: strResult, strT: strResultType} = prettyIT({i: result, t: resultType}); console.log(`result = ${strResult} :: ${strResultType}`); return instanceOrTypeOrFnOptions({i: result, t: resultType}); diff --git a/structures/list.js b/structures/list.js index 8cffbf2..e7c7901 100644 --- a/structures/list.js +++ b/structures/list.js @@ -1,4 +1,4 @@ -import { typedFnType } from "./types.js"; +import { fnType, typedFnType } from "./types.js"; import { Type } from "../type.js"; import { Int } from "../primitives/types.js"; import { makeGeneric } from "../generics/generics.js"; @@ -8,7 +8,7 @@ import { lsType } from "./types.js"; const emptyList = {l:[]}; const get = ls => i => ls.l[i]; const put = ls => i => elem => ({l: ls.l.with(Number(i), elem)}); -// const push = ls => elem => ({l:ls.l.concat([elem])}); +const push = ls => elem => ({l:ls.l.concat([elem])}); export const ModuleList = {l:[ // Type -> Type @@ -43,4 +43,16 @@ export const ModuleList = {l:[ /* out */ (lsType(a)) ) ))), + + // [a] -> a -> [a] + ...typedFnType(push, fnType => + makeGeneric(a => + fnType + (lsType(a)) + (fnType + (a) + (lsType(a)) + ) + ) + ), ]};