import { DefaultMap } from "./util.js"; // The registry ensures that we never accidentally create more than one JS object for the same function type. // It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions. // This same pattern is repeated throughout the code for all non-nullary type constructors (list, sum, product, ...) const fnTypeRegistry = new DefaultMap(inType => new DefaultMap(outType => ({ in: inType, out: outType }))); // type constructor for function types export const fnType = ({ in: inType, out: outType }) => fnTypeRegistry.getdefault(inType, true).getdefault(outType, true); export const Type = Symbol('Type'); export const Function = Symbol('Function'); // Implementation of 'in' and 'out' functions, // to get input/output type of a function signature: export const getIn = fn => fn.in; export const getOut = fn => fn.out; // a module is just a set of typed objects // each 'typed object' is implicitly an instance of TypeLink (defined below) export const ModuleMetaCircular = {l:[ // TODO? maybe follow Lean so // Type.{0} : Type.{1} // Type.{1} : Type.{2} // ... // see: https://lean-lang.org/functional_programming_in_lean/functor-applicative-monad/universes.html // Type : Type {i: Type, t: Type}, // Function : Type {i: Function, t: Type}, // (Function -> Type) : Function {i: fnType({in: Function, out: Type}), t: Function}, {i: getIn , t: fnType({in: Function, out: Type})}, {i: getOut, t: fnType({in: Function, out: Type})}, ]}; // Wrapper around function below. export const typedFnType = (instance, callback) => { const [t, typesOfFns] = typedFnType2(callback); const res = [ { i: instance, t }, ...typesOfFns, ]; return res; }; // Create a function type, and also create Type-links for the function type (being typed by Function) and for all the nested function types. Saves a lot of code writing. export const typedFnType2 = callback => { const fnTs = []; const wrappedFnType = ({ in: inType, out: outType }) => { console.log('wrappedFnType called'); const fnT = fnType({ in: inType, out: outType }); fnTs.push(fnT); return fnT; }; const t = callback(wrappedFnType); // force evaluation return [ t, fnTs.map(fnT => ({ i: fnT, t: Function })), ]; };