tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

contracts.ts (1758B)


      1 import { Graph, type NodeId } from './graph.js';
      2 import { list, text } from './codecs.js';
      3 // The portable annotation lowerer emits matchResult, withContract and err.
      4 // Exported library trees already close over their own transitive dependencies.
      5 // Keep general base names (id/pair/etc.) free for user definitions.
      6 export const CONTRACT_BINDINGS: Readonly<Record<string, string>> = Object.freeze({
      7   matchResult: 'base.matchResult', withContract: 'base.withContract', err: 'base.err',
      8   ok: 'base.ok', check: 'base.check', checkContract: 'base.checkContract',
      9   contractOk: 'base.contractOk', contractErr: 'base.contractErr',
     10   anyC: 'base.anyC', neverC: 'base.neverC', guardC: 'base.guardC',
     11   'nat?': 'base.nat?', 'nonZero?': 'base.nonZero?', 'bool?': 'base.bool?',
     12   andC: 'base.andC', orC: 'base.orC', notC: 'base.notC', mapC: 'base.mapC', bindC: 'base.bindC',
     13   listOf: 'base.listOf', nonEmptyListOf: 'base.nonEmptyListOf', pairOf: 'base.pairOf',
     14   fnContract: 'base.fnContract', fn2: 'base.fn2',
     15   bindResult: 'base.bindResult', mapResult: 'base.mapResult', resultMapErr: 'base.resultMapErr',
     16   resultIsOk: 'base.resultIsOk', resultIsErr: 'base.resultIsErr',
     17   pureE: 'base.pureE', bindE: 'base.bindE', exceptE: 'base.exceptE',
     18   pureM: 'base.pureM', bindM: 'base.bindM', checkM: 'base.checkM',
     19   liftM: 'base.liftM', runM: 'base.runM', handleM: 'base.handleM',
     20 });
     21 export function contractEnvironment(graph: Graph, exports: ReadonlyMap<string, NodeId>): NodeId {
     22   return list(graph, Object.entries(CONTRACT_BINDINGS).map(([name, exported]) => {
     23     const value = exports.get(exported);
     24     if (value === undefined) throw new Error(`Compiler package is missing contract export ${exported}`);
     25     return graph.fork(text(graph, name), value);
     26   }));
     27 }