environment includes mapping -> instance

This commit is contained in:
Joeri Exelmans 2025-05-10 10:00:31 +02:00
parent 1780920438
commit 7ffe5c4ed4
2 changed files with 75 additions and 36 deletions

View file

@ -4,43 +4,60 @@ import { assignFn, UnifyError } from "../generics/generics.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 { emptyDict, get, set } from "../structures/dict.js";
import { fold as foldList } from "../structures/list.js";
import { add, emptySet, fold as foldSet } from "../structures/set.js";
import { add, emptySet, fold as foldSet, has } from "../structures/set.js";
import { symbolFunction } from "../structures/type_constructors.js";
import { emptyTrie, insert } from "../util/trie.js";
const addEntry = env => i => t => {
const setOfInstances = getInstances(env)(t);
return setDict(env)(t)(add(setOfInstances)(i));
const addEntry = typeDict => i => t => {
const setOfInstances = _getInstances(typeDict)(t);
return set(typeDict)(t)(add(setOfInstances)(i));
};
export const growEnv = env => dynamic => {
export const emptyEnv = {
typeDict: emptyDict(compareTypes),
name2dyn: emptyTrie,
};
export const growEnv = ({typeDict, name2dyn}) => name => dynamic => {
const t = getType(dynamic);
const typeDictWithEntry = addEntry(env )(getInst(dynamic))(t );
const typeDictWithEntry = addEntry(typeDict )(getInst(dynamic))(t );
const typeDictWithType = addEntry(typeDictWithEntry)(t )(Type );
const typeDictWithDynamic = addEntry(typeDictWithType )(dynamic )(Dynamic);
return typeDictWithDynamic
return {
typeDict: typeDictWithDynamic,
name2dyn: insert(name2dyn)(name)(dynamic),
};
};
export const module2Env = module => {
return foldList(typeDict => dynamic => {
return foldList(env => ([name, dynamic]) => {
try {
return growEnv(typeDict)(dynamic);
return growEnv(env)(name)(dynamic);
} catch (e) {
console.log('skip:', e.message);
return typeDict;
return env;
}
})(emptyDict(compareTypes))(module);
})(emptyEnv)(module);
};
export const getInstances = env => type => {
return getDict(env)(type) || emptySet(makeCompareFn(type));
const _getInstances = typeDict => type => {
return get(typeDict)(type) || emptySet(makeCompareFn(type));
};
export const getInstances = env => type => _getInstances(env.typeDict)(type);
export const getTypes = env => {
return getDict(env)(Type) || emptySet(compareTypes);
return get(env.typeDict)(Type) || emptySet(compareTypes);
};
export const contains = env => dynamic => {
return has
(getInstances(env)(getType(dynamic)))
(getInst(dynamic));
}
export const getFunctions = env => {
const types = getTypes(env);
return foldSet(types => type => {
@ -53,7 +70,7 @@ export const getFunctions = env => {
newDynamic(fn)(type),
])
([])
(getInstances(env)(type)),
(_getInstances(env.typeDict)(type)),
];
}
return types;
@ -91,4 +108,4 @@ export const getCompatibleInputTypes = env => funType => {
}
})([])(allTypes);
return inTypes;
}
};