@informalsystems/quint
Version:
Core tool for the Quint specification language
116 lines • 3.99 kB
JavaScript
;
/* ----------------------------------------------------------------------------------
* Copyright 2022 Informal Systems
* Licensed under the Apache License, Version 2.0.
* See LICENSE in the project root for license information.
* --------------------------------------------------------------------------------- */
Object.defineProperty(exports, "__esModule", { value: true });
exports.substitutionsToString = exports.entityToString = exports.effectComponentToString = exports.effectSchemeToString = exports.effectToString = void 0;
const substitutions_1 = require("./substitutions");
/**
* Formats the string representation of an effect
*
* @param e the effect to be formatted
*
* @returns a string with the pretty printed effect
*/
function effectToString(e) {
switch (e.kind) {
case 'concrete': {
const output = e.components.map(effectComponentToString);
if (output.length > 0) {
return output.join(' & ');
}
else {
return 'Pure';
}
}
case 'variable':
return e.name;
case 'arrow': {
const params = e.params.map(effectToString);
const result = effectToString(e.result);
return `(${params.join(', ')}) => ${result}`;
}
}
}
exports.effectToString = effectToString;
/**
* Formats the string representation of an effect scheme
*
* @param e the effect scheme to be formatted
*
* @returns a string with the formatted effect scheme
*/
function effectSchemeToString(e) {
const effectNames = Array.from(e.effectVariables);
const entityNames = Array.from(e.entityVariables);
const vars = [];
const effectSubs = effectNames.map((name, i) => {
vars.push(`e${i}`);
return { kind: 'effect', name, value: { kind: 'variable', name: `e${i}` } };
});
const entitySubs = entityNames.map((name, i) => {
vars.push(`v${i}`);
return { kind: 'entity', name: name, value: { kind: 'variable', name: `v${i}` } };
});
const subs = effectSubs.concat(entitySubs);
const effect = (0, substitutions_1.applySubstitution)(subs, e.effect);
if (effect.isLeft()) {
throw new Error('Unexpected error while formatting effect scheme');
}
else {
const varsString = vars.length > 0 ? `∀ ${vars.join(', ')} . ` : '';
return `${varsString}${effectToString(effect.value)}`;
}
}
exports.effectSchemeToString = effectSchemeToString;
function effectComponentToString(c) {
switch (c.kind) {
case 'read':
return `Read[${entityToString(c.entity)}]`;
case 'update':
return `Update[${entityToString(c.entity)}]`;
case 'temporal':
return `Temporal[${entityToString(c.entity)}]`;
}
}
exports.effectComponentToString = effectComponentToString;
/**
* Formats the string representation of effect entities
*
* @param v the Entity to be formatted
*
* @returns a string with the pretty printed entity
*/
function entityToString(v) {
switch (v.kind) {
case 'concrete':
return v.stateVariables.map(v => `'${v.name}'`).join(', ');
case 'variable':
return v.name;
case 'union':
return v.entities.map(entityToString).join(', ');
}
}
exports.entityToString = entityToString;
/**
* Formats the string representation of substitutions
*
* @param s the Substitution to be formatted
*
* @returns a string with the pretty printed substitution
*/
function substitutionsToString(subs) {
const subsString = subs.map(s => {
switch (s.kind) {
case 'effect':
return `${s.name} |-> ${effectToString(s.value)}`;
case 'entity':
return `${s.name} |-> ${entityToString(s.value)}`;
}
});
return `[${subsString}]`;
}
exports.substitutionsToString = substitutionsToString;
//# sourceMappingURL=printing.js.map