replace 'prompt' example by 'environment'

This commit is contained in:
Joeri Exelmans 2025-05-09 14:53:43 +02:00
parent f8008aa25d
commit b1c2e7836d
15 changed files with 317 additions and 517 deletions

View file

@ -1,7 +1,9 @@
import { inspect } from "node:util";
import { eqType, getSymbol } from "../primitives/type.js";
import { zip } from "../util/util.js";
import { pretty, prettyT } from '../util/pretty.js';
import { isTypeVar, TYPE_VARS } from "../primitives/typevars.js";
import { inspectType } from "../meta/type_constructor.js";
// helper for creating generic types
// for instance, the type:
@ -150,6 +152,7 @@ const __unify = (fType, aType, fStack=[], aStack=[]) => {
type: {
symbol: fType.symbol,
params: unifiedParams,
[inspect.custom]: inspectType,
},
};
};
@ -177,6 +180,7 @@ export const substitute = (type, substitutions, stack=[]) => {
}
return substitute(param, substitutions, [...stack, parent]);
}),
[inspect.custom]: inspectType,
};
};

View file

@ -2,11 +2,13 @@ import { getHumanReadableName } from "../primitives/symbol.js";
import { inspect } from "util";
import { prettyT } from "../util/pretty.js";
export const inspectType = function(depth, options, inspect){ return options.stylize(prettyT(this), 'date'); }
const __makeTypeConstructor = (symbol, nAry, params) => {
if (nAry === 0) {
return {
symbol, params,
[inspect.custom](depth, options, inspect){ return options.stylize(prettyT(this), 'null'); },
[inspect.custom]: inspectType,
};
}
// only for debugging, do we give the function a name

View file

@ -6,6 +6,6 @@ const mkType = getDefaultTypeParser();
export const ModuleDouble = [
newDynamic(addDouble)(mkType("Double -> Double -> Double")),
newDynamic(mulDouble)(mkType("Double -> Double -> Double") ),
newDynamic(eqDouble)(mkType("Double -> Double -> Bool") ),
newDynamic(mulDouble)(mkType("Double -> Double -> Double")),
newDynamic(eqDouble )(mkType("Double -> Double -> Bool" )),
];

View file

@ -1,5 +1,6 @@
import { inspect } from "node:util";
import { assignFn } from "../generics/generics.js";
import { select } from "@inquirer/prompts";
function inspectDynamic(_depth, options, inspect) {
return `${inspect(this.i, options)} :: ${inspect(this.t, options)}`;

View file

@ -2,29 +2,29 @@ import { newDynamic } from "./dynamic.js";
import { SymbolInt, UUID, SymbolBool, SymbolDouble, SymbolByte, SymbolChar, SymbolUnit, SymbolBottom, SymbolUUID, SymbolType, SymbolTop, Type, Int, Bool, Double, Byte, Char, Unit, Bottom, Top, SymbolDynamic, Dynamic } from "./primitive_types.js";
export const ModulePrimitiveSymbols = [
newDynamic(SymbolInt )(UUID ),
newDynamic(SymbolBool )(UUID ),
newDynamic(SymbolDouble )(UUID ),
newDynamic(SymbolByte )(UUID ),
newDynamic(SymbolChar )(UUID ),
newDynamic(SymbolUnit )(UUID ),
newDynamic(SymbolBottom )(UUID ),
newDynamic(SymbolUUID )(UUID ),
newDynamic(SymbolType )(UUID ),
newDynamic(SymbolTop )(UUID ),
newDynamic(SymbolDynamic )(UUID ),
newDynamic(SymbolInt )(UUID),
newDynamic(SymbolBool )(UUID),
newDynamic(SymbolDouble )(UUID),
newDynamic(SymbolByte )(UUID),
newDynamic(SymbolChar )(UUID),
newDynamic(SymbolUnit )(UUID),
newDynamic(SymbolBottom )(UUID),
newDynamic(SymbolUUID )(UUID),
newDynamic(SymbolType )(UUID),
newDynamic(SymbolTop )(UUID),
newDynamic(SymbolDynamic)(UUID),
];
export const ModulePrimitiveTypes = [
newDynamic(Int )(Type ),
newDynamic(Bool )(Type ),
newDynamic(Double )(Type ),
newDynamic(Byte )(Type ),
newDynamic(Char )(Type ),
newDynamic(Unit )(Type ),
newDynamic(Bottom )(Type ),
newDynamic(UUID )(Type ),
newDynamic(Type )(Type ),
newDynamic(Top )(Type ),
newDynamic(Dynamic )(Type ),
newDynamic(Int )(Type),
newDynamic(Bool )(Type),
newDynamic(Double )(Type),
newDynamic(Byte )(Type),
newDynamic(Char )(Type),
newDynamic(Unit )(Type),
newDynamic(Bottom )(Type),
newDynamic(UUID )(Type),
newDynamic(Type )(Type),
newDynamic(Top )(Type),
newDynamic(Dynamic)(Type),
];

View file

@ -3,4 +3,4 @@ import { compareTypes } from "../compare/type.js";
export const getSymbol = type => type.symbol;
export const getParams = type => type.params;
export const eqType = t1 => t2 => compareTypes(t1, t2) === 0;
export const eqType = t1 => t2 => compareTypes(t1)(t2) === 0;

View file

@ -1,7 +1,3 @@
import { inspect } from "node:util";
export const unit = {
[inspect.custom](depth, options, inspect){ return '()'; }
};
export const unit = {};
export const eqUnit = _ => _ => true;

View file

@ -18,6 +18,13 @@ export const set = dict => key => value => new RBTreeWrapper(dict.tree.remove(ke
export const remove = dict => key => new RBTreeWrapper(dict.tree.remove(key), inspectDict);
export const length = dict => dict.tree.length;
export const fold = callback => initial => dict => {
let acc = initial;
for (const iter=dict.tree.begin; iter !== undefined && iter.valid; iter.next()) {
acc = callback(acc)(iter.key)(iter.value);
}
}
export const first = dict => dict.tree.begin;
export const last = dict => dict.tree.end;

View file

@ -1,6 +1,6 @@
import { makeTypeParser } from "../parser/type_parser.js";
import { makeTypeConstructor } from "../meta/type_constructor.js";
import { emptyDict, first, has, last, length, read, remove, set } from "./dict.js";
import { emptyDict, first, fold, has, last, length, read, remove, set } from "./dict.js";
import { newDynamic } from "../primitives/dynamic.js";
export const symbolDictIterator = 'DictIterator__d9d175b6bfd1283f00851a99787d0499';
@ -13,12 +13,13 @@ const mkType = makeTypeParser({
});
export const ModuleDict = [
newDynamic(emptyDict )(mkType("(a -> a -> Int) -> (a => b)") ),
newDynamic(has )(mkType("(a => b) -> a -> Bool")),
newDynamic(set )(mkType("(a => b) -> a -> b -> (a => b)")),
newDynamic(remove )(mkType("(a => b) -> a -> (a => b)")),
newDynamic(length )(mkType("(a => b) -> Int")),
newDynamic(first )(mkType("(a => b) -> (a |=>| b)")),
newDynamic(last )(mkType("(a => b) -> (a |=>| b)")),
newDynamic(read )(mkType("(a |=>| b) -> (Unit + ((a*b) * (a |=>| b)))")),
newDynamic(emptyDict)(mkType("(a -> a -> Int) -> (a => b) ")),
newDynamic(has )(mkType("(a => b) -> a -> Bool ")),
newDynamic(set )(mkType("(a => b) -> a -> b -> (a => b) ")),
newDynamic(remove )(mkType("(a => b) -> a -> (a => b) ")),
newDynamic(length )(mkType("(a => b) -> Int ")),
newDynamic(fold )(mkType("(c -> a -> b -> c) -> c -> (a => b) -> c ")),
newDynamic(first )(mkType("(a => b) -> (a |=>| b) ")),
newDynamic(last )(mkType("(a => b) -> (a |=>| b) ")),
newDynamic(read )(mkType("(a |=>| b) -> (Unit + ((a*b) * (a |=>| b)))")),
];

View file

@ -7,7 +7,7 @@ export const push = ls => elem => ls.concat([elem]);
export const pop = ls => ls.pop();
export const map = ls => fn => ls.map(elem => fn(elem));
export const length = ls => ls.length;
export const fold = ls => callback => initial => {
export const fold = callback => initial => ls => {
let acc = initial;
for (let i=0; i<ls.length; i++) {
acc = callback(acc)(ls[i]);

View file

@ -12,5 +12,5 @@ export const ModuleList = [
newDynamic(pop )(mkType("[a] -> a")),
newDynamic(map )(mkType("[a] -> (a -> b) -> [b]")),
newDynamic(length )(mkType("[a] -> Int")),
newDynamic(fold )(mkType("[a] -> (b -> a -> b) -> b -> b")),
newDynamic(fold )(mkType("(b -> a -> b) -> b -> [a] -> b")),
];

View file

@ -19,11 +19,10 @@ export const add = set => key => set.tree.get(key) === true ? set : new RBTreeWr
export const remove = set => key => new RBTreeWrapper(set.tree.remove(key), inspectSet);
export const length = set => set.tree.length;
export const fold = set => callback => initial => {
export const fold = callback => initial => set => {
let acc = initial;
let iter = set.tree.begin;
while (iter !== undefined && iter.valid) {
acc = callback(acc, iter.key);
for (const iter=set.tree.begin; iter !== undefined && iter.valid; iter.next()) {
acc = callback(acc)(iter.key);
}
return acc;
};

View file

@ -8,17 +8,18 @@ export const symbolSetIterator = 'SetIterator__f6b0ddd78ed41c58e5a442f2681da011'
const setIterator = makeTypeConstructor(symbolSetIterator)(1);
const mkType = makeTypeParser({
// extra syntax to represent a set iterator:
extraBracketOperators: [['<', ['>', setIterator]]],
});
export const ModuleSet = [
newDynamic(emptySet )(mkType("(a -> a -> Int) -> {a}") ),
newDynamic(has )(mkType("{a} -> a -> Bool")),
newDynamic(add )(mkType("{a} -> a -> {a}")),
newDynamic(remove )(mkType("{a} -> a -> {a}")),
newDynamic(length )(mkType("{a} -> Int")),
newDynamic(fold )(mkType("{a} -> (b -> a -> b) -> b")),
newDynamic(first )(mkType("{a} -> <a>")),
newDynamic(last )(mkType("{a} -> <a>")),
newDynamic(read )(mkType("<a> -> (Unit + (a * <a>))")),
newDynamic(emptySet)(mkType("(a -> a -> Int) -> {a}" )),
newDynamic(has )(mkType("{a} -> a -> Bool" )),
newDynamic(add )(mkType("{a} -> a -> {a}" )),
newDynamic(remove )(mkType("{a} -> a -> {a}" )),
newDynamic(length )(mkType("{a} -> Int" )),
newDynamic(fold )(mkType("(b -> a -> b) -> b -> {a} -> b")),
newDynamic(first )(mkType("{a} -> <a>" )),
newDynamic(last )(mkType("{a} -> <a>" )),
newDynamic(read )(mkType("<a> -> (Unit + (a * <a>))" )),
];