split 'environment' example up into library and interactive prompt

This commit is contained in:
Joeri Exelmans 2025-05-09 16:05:12 +02:00
parent 255bc475d7
commit 77dfc8b182
3 changed files with 308 additions and 261 deletions

60
lib/environment/env.js Normal file
View file

@ -0,0 +1,60 @@
import { makeCompareFn } from "../compare/dynamic.js";
import { compareTypes } from "../compare/type.js";
import { getInst, getType, newDynamic } from "../primitives/dynamic.js";
import { Dynamic, Type } from "../primitives/primitive_types.js";
import { getSymbol } from "../primitives/type.js";
import { emptyDict, get, get as getDict, set as setDict } from "../structures/dict.js";
import { fold as foldList } from "../structures/list.js";
import { add, emptySet, fold as foldSet } from "../structures/set.js";
import { symbolFunction } from "../structures/type_constructors.js";
const addEntry = env => i => t => {
const setOfInstances = getInstances(env)(t);
return setDict(env)(t)(add(setOfInstances)(i));
};
export const growEnv = env => dynamic => {
const t = getType(dynamic);
const typeDictWithEntry = addEntry(env )(getInst(dynamic))(t );
const typeDictWithType = addEntry(typeDictWithEntry)(t )(Type );
const typeDictWithDynamic = addEntry(typeDictWithType )(dynamic )(Dynamic);
return typeDictWithDynamic
};
export const module2Env = module => {
return foldList(typeDict => dynamic => {
try {
return growEnv(typeDict)(dynamic);
} catch (e) {
console.log('warning:', e.message);
return typeDict;
}
})(emptyDict(compareTypes))(module);
};
export const getInstances = env => type => {
return getDict(env)(type) || emptySet(makeCompareFn(type));
};
export const getTypes = env => {
return getDict(env)(Type) || emptySet(compareTypes);
};
export const getFunctions = env => {
const types = getTypes(env);
return foldSet(types => type => {
if (getSymbol(type) === symbolFunction) {
return [
...types,
...foldSet
(functions => fn => [
...functions,
newDynamic(fn)(type),
])
([])
(getInstances(env)(type)),
];
}
return types;
})([])(types);
};