UNPKG

stsys

Version:

String rewriting system (semi-Thue system)

101 lines (90 loc) 2.02 kB
const strpair = require('./strpair'); class GoalManager { constructor(prover) { this.prover = prover; this.goalsCount = 0; this.goals = []; this.unprovedGoalsCount = 0; } clearGoals() { this.goals.length = this.goalsCount = this.unprovedGoalsCount = 0; } addGoals(spec) { let goal; if (typeof spec === 'string') { this.goals.push(goal = new Goal(spec)); goal.id = ++this.goalsCount; if (!goal.proved) this.unprovedGoalsCount++; } else if (spec instanceof Array) spec.forEach(gs => { this.goals.push(goal = new Goal(gs)); goal.id = ++this.goalsCount; if (!goal.proved) this.unprovedGoalsCount++; }); else if (spec) throw new Error('invalid specification of goals (illegal type: ' + typeof spec + ')'); } getGoalsCount() { return this.goals.length; } getGoals() { return this.goals.map(g => { const r = { id: g.id, goal: g.original, proved: g.proved }; if (g.proved) r.provedBy = g.modifiable.toString(); return r; }); } reduceAll(reducer, rule) { this.goals.forEach(goal => { if (!goal.proved) { goal.reduce(reducer, rule); if (goal.proved) { --this.unprovedGoalsCount; if (this.prover.isLoggable(2)) this.prover.log('Goal proved: ' + goal.original); } } }); } allGoalsProved() { return this.goals.length > 0 && this.unprovedGoalsCount === 0; } } class Goal { constructor(spec) { this.modifiable = strpair.parseStrPair(spec); this.original = this.modifiable.toString(); this.proved = this.modifiable.isTrivial(); this.id = 0; } reduce(reducer, rule) { if (this.modifiable.reduce(reducer, rule) && !this.proved) this.proved = this.modifiable.isTrivial(); } } module.exports.createGoalManager = function(prover) { return new GoalManager(prover); };