import { Dictionary, IdentitySet, Record, Value, is, } from 'preserves'; //--------------------------------------------------------------------------- export type Assertion = Value; export type Handle = number; export type ExitReason = null | { ok: true } | { ok: false, err: Error }; export type LocalAction = (t: Turn) => void; export interface Entity { assert(turn: Turn, assertion: Assertion, handle: Handle): void; retract(turn: Turn, handle: Handle): void; message(turn: Turn, body: Assertion): void; sync(turn: Turn, peer: Ref): void; } export interface Ref { readonly relay: Actor; readonly target: Partial; readonly attenuation?: Attenuation; } export type Attenuation = Array; // array of stages, each a list of alternatives export type RewriteStage = Array; export type Rewrite = { pattern: Pattern, template: Template }; export const _CRec = Symbol.for('rec'); export const CRec = Record.makeConstructor<{label: Assertion, arity: number}, Ref>()( _CRec, ['label', 'arity']); export const _CArr = Symbol.for('arr'); export const CArr = Record.makeConstructor<{arity: number}, Ref>()( _CArr, ['arity']); export const _CDict = Symbol.for('dict'); export const CDict = Record.makeConstructor<{}, Ref>()( _CDict, []); export type ConstructorSpec = | Record | Record | Record; export const _PDiscard = Symbol.for('_'); export const PDiscard = Record.makeConstructor<{}, Ref>()( _PDiscard, []); export const _PBind = Symbol.for('bind'); export const PBind = Record.makeConstructor<{name: string, pattern: Pattern}, Ref>()( _PBind, ['name', 'pattern']); export const _PAnd = Symbol.for('and'); export const PAnd = Record.makeConstructor<{patterns: Array}, Ref>()( _PAnd, ['patterns']); export const _PNot = Symbol.for('not'); export const PNot = Record.makeConstructor<{pattern: Pattern}, Ref>()( _PNot, ['pattern']); export const _Lit = Symbol.for('lit'); export const Lit = Record.makeConstructor<{value: Assertion}, Ref>()( _Lit, ['value']); export const _PCompound = Symbol.for('compound'); export const PCompound = Record.makeConstructor<{ctor: ConstructorSpec, members: Dictionary}, Ref>()( _PCompound, ['ctor', 'members']); export type Pattern = | Record | Record | Record | Record | Record | Record], Ref>; export const _TRef = Symbol.for('ref'); export const TRef = Record.makeConstructor<{name: string}, Ref>()( _TRef, ['name']); export const _TCompound = Symbol.for('compound'); export const TCompound = Record.makeConstructor<{ctor: ConstructorSpec, members: Dictionary