stsys
Version:
String rewriting system (semi-Thue system)
166 lines (160 loc) • 4.62 kB
JavaScript
const fs = require('fs');
const createProver = require('./index').createProver;
const fileNames = [];
const options = {
logger: console.log,
logLevel: 0,
ordering: null,
preorder: null,
goals: null,
statistics: false
};
const outOpts = {
showAxioms: false,
showRules: false,
showGoals: false
};
function readSpec(filename)
{
const spec = {
axioms: [],
goals: [],
preorder: null,
ordering: null
};
fs.readFileSync(filename,'utf-8').split('\n').forEach(function(line){
line = line.trim();
if (line.length === 0 || line.charAt(0) === '%')
return;
if (line.indexOf('=') > 0)
{
if (spec.preorder)
spec.goals.push(line);
else
spec.axioms.push(line);
}
else
{
let m = /^ordering:\s*(lex|lpo|syl-l|syl-r)\s+(.+)$/.exec(line);
if (m)
{
spec.ordering = m[1];
spec.preorder = m[2].split(/ *> */);
}
}
});
return spec;
}
if (process.argv.length < 3)
{
console.log('\nUSAGE:', process.argv0, process.argv[1], '[<options>] <file1> ...');
console.log('\nOptions:');
console.log(' -l<n> : set log level <n> (an integer ranging from 0 to 6)');
console.log(' -la : list axioms');
console.log(' -lg : list goals upon termination (if there are any goals)');
console.log(' -lr : list final set of rules');
console.log(' -ls : collect and list statistics');
console.log(' -o<N> : use ordering <N>, where <N> is lpo, lex, syl-l, or syl-r');
console.log(' -p : pre-reduce critical pairs (prior to adding them to the set of critical pairs)');
console.log(' -r<n> : set <n> as the maximal number of generated rules');
console.log(' -t<n> : timeout after <n> milliseconds');
console.log();
process.exit(1);
}
for (let i = 2; i < process.argv.length; i++)
{
let arg = process.argv[i];
if (process.argv[i].charAt(0) === '-')
{
switch (arg.charAt(1))
{
case 'l':
if (arg.charAt(2) === 's')
options.statistics = true;
else if (arg.charAt(2) === 'a')
outOpts.showAxioms = true;
else if (arg.charAt(2) === 'g')
outOpts.showGoals = true;
else if (arg.charAt(2) === 'r')
outOpts.showRules = true;
else
options.logLevel = arg.substring(2) * 1;
break;
case 'o':
options.ordering = arg.substring(2);
break;
case 'p':
options.preRedCps = true;
break;
case 'r':
options.maxRuleGen = arg.substring(2) * 1;
break;
case 't':
options.timeout = arg.substring(2) * 1;
break;
default:
console.log('Ignoring unknown option:', arg);
}
}
else
fileNames.push(arg);
}
if (fileNames.length === 0)
console.log('No file names');
else
{
console.log('RUNNING...');
fileNames.forEach(function(fn){
const spec = readSpec(fn);
const opt = Object.create(options);
if (!opt.ordering)
opt.ordering = spec.ordering;
opt.preorder = spec.preorder;
opt.goals = spec.goals;
console.log('\n=====', fn, '=====');
try
{
let prover = createProver(spec.axioms);
if (outOpts.showAxioms)
{
let axioms = prover.getAxioms();
console.log('Axioms (' + axioms.length + '):');
axioms.forEach(axiom => console.log(' ', axiom));
}
prover.run(opt);
console.log('Done:', prover.getState());
console.log('Time elapsed:', prover.getTimeElapsed());
if (outOpts.showRules)
{
let rules = prover.getRules();
console.log('Rules (' + rules.length + '):');
rules.forEach(rule => console.log(' ', rule));
}
if (outOpts.showGoals && prover.getTotalGoalsCount() > 0)
{
console.log('Goals (' + prover.getProvedGoalsCount() + ' of ' + prover.getTotalGoalsCount() + ' proved):');
const proverState = prover.getState();
let success = 'PROVED', failure = ' OPEN';
if (prover.getProvedGoalsCount() < prover.getTotalGoalsCount() && proverState === 'completed')
{
success = ' PROVED';
failure = 'DISPROVED';
}
prover.getGoals().forEach(goalState => {
console.log(' ', goalState.proved ? success : failure, goalState.goal);
});
}
if (opt.statistics)
{
console.log('----- Statistics -----');
console.log(prover.getStatisticsAsString());
}
}
catch (error)
{
console.log('ERROR:', error.message);
//console.log(error.stack);
}
});
console.log('\nDONE.');
}