Skip to content

Commit c2310a3

Browse files
author
Alexander Steen
committed
added unit propagation for formula simplification
1 parent 3038044 commit c2310a3

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

js/inputoutput.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,36 @@ iol = new function() { const lib = this;
244244
return result
245245
}
246246

247-
/* TODO: unit propagation etc. for simplication */
247+
/* tries to reduce the complexity of the set of formulas, by:
248+
- applying intra-formula simplification
249+
- unit propagation
250+
- subsumption
251+
*/
248252
const semanticalInterreduce = function(A) {
253+
/* simplification: */
249254
const Asimp = pltk.simpset(A)
250255
const Asimpcnf = pltk.conjs(pltk.cnfsimp(pltk.cnf(pltk.mkConjs(Asimp))))
256+
/* do unit propagation */
257+
let units = _.filter(Asimpcnf, pltk.isUnitFormula)
258+
let nonUnits = _.without(Asimpcnf, ...units)
259+
let newUnits = units
260+
while (!_.isEmpty(newUnits)) {
261+
let replacements = _.map(units, u => [pltk.getUnitFormulaBody(u), pltk.getUnitFormulaPolarity(u)])
262+
let rewrittenNonUnits = _.map(nonUnits, f => pltk.simp(pltk.deepReplaceBys1(f, replacements)))
263+
newUnits = _.filter(rewrittenNonUnits, pltk.isUnitFormula)
264+
units = units.concat(newUnits)
265+
nonUnits = _.without(rewrittenNonUnits, ...newUnits)
266+
}
267+
const formulas = units.concat(nonUnits)
268+
/* do subsumption*/
251269
let result = []
252-
_.forEach(Asimpcnf, function(a) {
253-
if (!pltk.consequence(result,a)) {
254-
result = _.reject(result, r => pltk.consequence([a],r))
270+
_.forEach(formulas, function(a) {
271+
if (!pltk.consequence(result,a)) { /* <- forward subsumption */
272+
result = _.reject(result, r => pltk.consequence([a],r)) /* backward subsumption */
255273
result.push(a)
256274
}
257275
})
258-
//console.log("Result: ", pltk.plprintset(result))
276+
/* done */
259277
return result
260278
}
261279

js/pltk.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ pltk = new function() { const lib = this;
346346
} else return it
347347
}
348348

349+
lib.deepReplaceBy = function(it, what, by) {
350+
if (_.isEqual(it, what)) {
351+
return by
352+
} else {
353+
if (lib.isAtom(it)) {
354+
return it
355+
} else return { op: it.op, args: _.map(it.args, arg => lib.deepReplaceBy(arg, what, by)) }
356+
}
357+
}
358+
359+
/* deep replacement successively (it's NOT simultaneously)*/
360+
lib.deepReplaceBys = function(it, whats, bys) {
361+
const replacements = _.zip(whats, bys)
362+
return _.reduce(replacements, function (acc, replacement) { return lib.deepReplaceBy(acc, replacement[0], replacement[1]) }, it)
363+
}
364+
365+
/* deep replacement successively (it's NOT simultaneously)*/
366+
lib.deepReplaceBys1 = function(it, replacements) {
367+
return _.reduce(replacements, function (acc, replacement) { return lib.deepReplaceBy(acc, replacement[0], replacement[1]) }, it)
368+
}
349369

350370
/* true if a subsumes b */
351371
lib.disjClauseSubsumes = function(a,b) {
@@ -365,6 +385,24 @@ pltk = new function() { const lib = this;
365385
}
366386
return lib.isVariable(f)
367387
}
388+
389+
/* pre: isUnitFormula */
390+
lib.getUnitFormulaPolarity = function(f) {
391+
if (lib.isNeg(f)) {
392+
return { op: 'LF', args: []}
393+
} else {
394+
return { op: 'LF', args: []}
395+
}
396+
}
397+
398+
/* pre: isUnitFormula */
399+
lib.getUnitFormulaBody = function(f) {
400+
if (lib.isNeg(f)) {
401+
return f.args[0]
402+
} else {
403+
return f
404+
}
405+
}
368406

369407
lib.isVariable = function(f) {
370408
return _.get(f, 'op', '') == 'Var'
@@ -397,6 +435,10 @@ pltk = new function() { const lib = this;
397435
return _.has(f,'op') && f.op == 'LF'
398436
}
399437

438+
lib.isAtom = function(f) {
439+
return lib.isVariable(f) || lib.isT(f) || lib.isF(f)
440+
}
441+
400442
// ###############################
401443
/* Export to dimacs */
402444
// ###############################

0 commit comments

Comments
 (0)