diff --git a/examples/environment.js b/examples/environment.js deleted file mode 100644 index 7837f09..0000000 --- a/examples/environment.js +++ /dev/null @@ -1,19 +0,0 @@ -// Little demo that demonstrates interactive exploration of types, instances and transformations. - -import { module2Env } from "../lib/environment/env.js"; -import { newDynamic } from "../lib/primitives/dynamic.js"; -import { Dynamic } from "../lib/primitives/primitive_types.js"; -import { TYPE_VARS } from "../lib/primitives/typevars.js"; -import { ModuleStd } from "../lib/stdlib.js"; -import { emptyList } from "../lib/structures/list.js"; -import { lsType } from "../lib/structures/type_constructors.types.js"; -import { listInstances, transform } from "./prompt/prompt.js"; - -const env = module2Env(ModuleStd); - -await listInstances(env, Dynamic); - -// await transform( -// env, -// newDynamic(emptyList)(lsType(_ => TYPE_VARS[0])), -// ); diff --git a/examples/prompt/prompt.js b/examples/prompt/prompt.js deleted file mode 100644 index 201359c..0000000 --- a/examples/prompt/prompt.js +++ /dev/null @@ -1,242 +0,0 @@ -// The functions in this module prompt the user for actions to perform on types, instances, etc. - -import { input, number, select } from "@inquirer/prompts"; - -import { contains, getCompatibleInputTypes, getEnabledFunctions, getInstances, growEnv } from "../../lib/environment/env.js"; -import { getInst, getType, newDynamic } from "../../lib/primitives/dynamic.js"; -import { Bool, Double, Int, symbolDynamic, Type, UUID } from "../../lib/primitives/primitive_types.js"; -import { eqType, getSymbol } from "../../lib/primitives/type.js"; -import { add, fold as foldSet } from "../../lib/structures/set.js"; -import { symbolFunction } from "../../lib/structures/type_constructors.js"; -import { pretty, prettyT } from "../../lib/util/pretty.js"; -import { genUUID } from "../../lib/util/random.js"; - -const defaultSelectOptions = { - pageSize: 13, -}; - -export const addToEnv = async (env, dynamic) => { - if (contains(env)(dynamic)) { - return env; - } - const name = await input({ - message: `enter name for ${pretty(dynamic)} (or leave empty to skip this):`, - }); - if (name === '') { - return env; - } - const newEnv = growEnv(env)(name)(dynamic); - return newEnv; -} - -export const proxyDynamic = async (env, dynamic) => { - const type = getType(dynamic) - if (eqType(type)(Type)) { - return listTypeOptions(env, getInst(dynamic)); - } - if (getSymbol(type) === symbolFunction) { - return listFunctionOptions(env, dynamic); - } - if (getSymbol(type) === symbolDynamic) { - return proxyDynamic(env, getInst(dynamic)); - } - return listInstanceOptions(env, dynamic); -}; - -export const selectInstance = async (env, type, msg) => { - const instances = getInstances(env)(type); - const choices = foldSet(acc => instance => { - return [...acc, { - value: instance, - name: pretty(instance), - }]; - })([ - "(go back)", - "(new)", - ])(instances); - const choice = await select({ - message: `${msg} ${pretty(type)}:`, - choices, - ...defaultSelectOptions, - }); - if (choice === "(go back)") { - return; - } - if (choice === "(new)") { - return createInstance(type); - } - return choice; -}; - -export const listInstances = async (env, type) => { - const instance = await selectInstance(env, type, "instances of"); - if (instance === undefined) { - return; - } - const dynamic = newDynamic(instance)(type); - const newEnv = await addToEnv(env, dynamic); - await proxyDynamic(newEnv, dynamic); - return await listInstances(env, type); -}; - -export const listTypeOptions = async (env, type) => { - const choice = await select({ - message: `type ${pretty(type)}:`, - choices: [ - "(go back)", - "new", - "list instances", - "treat as instance", - ], - ...defaultSelectOptions, - }); - if (choice === "(go back)") { - return; - } - if (choice === "new") { - const i = await createInstance(type); - if (i !== undefined) { - const dynamic = newDynamic(i)(type); - const newEnv = await addToEnv(env, dynamic); - await proxyDynamic(newEnv, dynamic); - } - } - if (choice === "list instances") { - await listInstances(env, type); - } - if (choice === "treat as instance") { - await listInstanceOptions(env, newDynamic(type)(Type)); - } - return await listTypeOptions(env, type); -}; - -export const listInstanceOptions = async (env, dynamic) => { - const choice = await select({ - message: `instance ${pretty(dynamic)}:`, - choices: [ - "(go back)", - "transform", - "get type", - ], - ...defaultSelectOptions, - }); - if (choice === "(go back)") { - return; - } - if (choice === "transform") { - await transform(env, dynamic); - } - if (choice === "get type") { - await listTypeOptions(env, getType(dynamic)); - } - return await listInstanceOptions(env, dynamic); -}; - -export const listFunctionOptions = async (env, dynamic) => { - const choice = await select({ - message: `function ${pretty(dynamic)}:`, - choices: [ - "(go back)", - "call", - ], - ...defaultSelectOptions, - }); - if (choice === "(go back)") { - return; - } - if (choice === "call") { - await call(env, dynamic); - } -}; - -export const createInstance = async (type) => { - if (eqType(type)(Int)) { - const n = await number({ - message: `enter an integer (leave empty to go back):`, - step: 1, // only integers - }); - if (n === undefined) { - return; - } - return BigInt(n); - } - if (eqType(type)(Double)) { - const n = await number({ - message: `enter a number (leave empty to go back):`, - step: 'any', - }); - return n; - } - if (eqType(type)(Bool)) { - const b = await select({ - message: `select:`, - choices: [ - {value: false, name: 'false'}, - {value: true, name: 'true'}, - ], - ...defaultSelectOptions, - }); - return b; - } - if (eqType(type)(UUID)) { - const descr = await input({message: "enter UUID description:"}); - return descr + '__' + genUUID(16); - } - console.log("no prompt handler for creating new", prettyT(type)); -}; - -export const transform = async (env, dynamic) => { - const enabled = getEnabledFunctions(env)(dynamic); - const choice = await select({ - message: `select transformation:`, - choices: [ - "(go back)", - ...enabled.map(({fun, outType}) => ({ - value: {fun, outType}, - name: `${getInst(fun).name} ${pretty(getInst(dynamic))} :: ${pretty(outType)}`, - })), - ], - ...defaultSelectOptions, - }); - - if (choice === "(go back)") { - return; - } - const {fun, outType} = choice; - const outValue = getInst(fun)(getInst(dynamic)); - await proxyDynamic(env, newDynamic(outValue)(outType)); - return transform(env, dynamic); -}; - -export const call = async (env, funDynamic) => { - const funType = getType(funDynamic); - const inTypes = getCompatibleInputTypes(env)(funType); - const choice = inTypes.length === 1 - ? inTypes[0] - : await select({ - message: `types assignable to ${pretty(funType.params[0](funType))}:`, - choices: [ - "(go back)", - ...inTypes.map(({inType, outType}) => ({ - value: {inType, outType}, - name: pretty(inType), - })), - ], - ...defaultSelectOptions, - }); - - if (choice === "(go back)") { - return; - } - const inValue = await selectInstance(env, choice.inType, "select input value of type"); - if (inValue === undefined) { - return; - } - const inDynamic = newDynamic(inValue)(choice.inType); - const envWithInput = await addToEnv(env, inDynamic); - const outValue = getInst(funDynamic)(inValue); - const outDynamic = newDynamic(outValue)(choice.outType) - const envWithOutput = await addToEnv(envWithInput, outDynamic); - await proxyDynamic(envWithOutput, outDynamic); - return call(envWithOutput, funDynamic); -};