def _constraintlist2conjunction(constraintlist): if len(constraintlist) == 0: return Expr(True) constraint = constraintlist[0] for c in constraintlist[1:]: constraint = Expr('and', constraint, c) return constraint
def assemblyCNF(exprlist): if len(exprlist) == 0: return Expr(True) expr = exprlist[0] for i in range(1, len(exprlist)): expr = Expr('and', expr, exprlist[i]) return expr
def resetHeap(): global heap, used, boolheap, boolused heap.clear() used.clear() boolheap.clear() boolused.clear() heapq.heappush(heap, Expr('My-Start-Symbol')) heapq.heappush(boolheap, Expr('My-Bool-Start-Symbol'))
def _assembly_clauses_with(op, clauses): if len(clauses) == 0: if op == 'or': return Expr(True) elif op == 'and': return Expr(False) else: assert False if len(clauses) == 1: return clauses[0] return Expr(op, _assembly_clauses_with(op, clauses[1:]), clauses[0])
def _subst_eqclass(expr, overallmap): if expr is None: return None op = expr.op if isinstance(op, int): return expr if isinstance(op, bool): return expr if isinstance(op, Func): return Expr( op, list(map(lambda x: _subst_eqclass(x, overallmap), expr.arg1))) if expr.arg1 is None: if expr.op in overallmap: return Expr(overallmap[expr.op]) return expr return Expr(op, _subst_eqclass(expr.arg1, overallmap), _subst_eqclass(expr.arg2, overallmap), _subst_eqclass(expr.arg3, overallmap))
def _instrument_add_suffix(constraint, inputmap): if constraint is None: return None if isinstance(constraint.op, Func): arg1 = list( map(lambda x: _instrument_add_suffix(x, inputmap), constraint.arg1)) return Expr(constraint.op, arg1) if isinstance(constraint.op, int): return constraint if isinstance(constraint.op, bool): return constraint if constraint.op not in [ '+', '-', '*', 'div', 'mod', 'ite', 'and', 'or', '=>', 'xor', 'xnor', 'nand', 'nor', 'iff', 'not', '=', '>=', '>', '<=', '<' ]: return Expr(constraint.op + '__' + inputmap[constraint.op]) return Expr(constraint.op, _instrument_add_suffix(constraint.arg1, inputmap), _instrument_add_suffix(constraint.arg2, inputmap), _instrument_add_suffix(constraint.arg3, inputmap))
def putInNot(expr): if expr.op == 'not': if expr.arg1.op == 'not': return putInNot(expr.arg1.arg1) if expr.arg1.op == 'and': return Expr('or', putInNot(Expr('not', expr.arg1.arg1)), putInNot(Expr('not', expr.arg1.arg2))) if expr.arg1.op == 'or': return Expr('and', putInNot(Expr('not', expr.arg1.arg1)), putInNot(Expr('not', expr.arg1.arg2))) if expr.op == 'and': return Expr('and', putInNot(expr.arg1), putInNot(expr.arg2)) if expr.op == 'or': return Expr('or', putInNot(expr.arg1), putInNot(expr.arg2)) return expr
def distribution(expr): # not in inner so no need to consider it if expr.op == 'and': return Expr('and', distribution(expr.arg1), distribution(expr.arg2)) if expr.op == 'or': # distribution left if expr.arg1.op == 'and': rd = distribution(expr.arg2) return Expr('and', distribution(Expr('or', expr.arg1.arg1, rd)), distribution(Expr('or', expr.arg1.arg2, rd))) if expr.arg2.op == 'and': ld = distribution(expr.arg1) return Expr('and', distribution(Expr('or', ld, expr.arg2.arg1)), distribution(Expr('or', ld, expr.arg2.arg2))) return expr
def main(): Expr.productions = [] expr = Expr('iff', Expr('=>', Expr('p'), Expr('q')), Expr('r')) cnfexpr = toRawCNF(expr) print(cnfexpr) # print(toCNF(expr)) c = getCNFclause(cnfexpr) vartab = {'p': Bool('p'), 'q': Bool('q'), 'r': Bool('r')} func = None print( clauseeq(Expr('q'), Expr('p'), { 'p': Bool('p'), 'q': Bool('q'), 'r': Bool('r') }, None)) print( clauseeq(Expr('q'), Expr('p'), { 'p': Bool('p'), 'q': Bool('q'), 'r': Bool('r') }, None)) for s in filterclause(c, vartab, func): print(s) print(buildCNF(expr, vartab, func))
def nextDistinctTerm(self, pts): ''' p = [Expr('x1'), Expr('x2'), Expr('x3'), Expr('x4'), Expr('x5'), Expr('x6')] p = [Expr('x'), Expr('y'), Expr('z'), Expr('u'), Expr('w')] p = [] for i in range(8): p.append(Expr(i)) ''' # p = [Expr(-1),Expr(1),Expr('+', Expr('x'), Expr('y'))] p = [ Expr(0), Expr(10), Expr(20), Expr(30), Expr(40), Expr(50), Expr('x') ] if len(self.terms) < len(p): t = p[len(self.terms)] ct = self.gencover(pts, t) # if self.covers.count(ct) == 0: for i in ct: self.labels[i].add(len(self.terms)) self.terms.append(t) self.covers.append(ct) return '''
def _do_instrument_constraint(clause, funcproto): if clause == None: return None, [] assert isinstance(funcproto, Func) guards = [] if clause.op == 'not': return Expr('not', _do_instrument_constraint(clause.op, funcproto)) if isinstance(clause.op, Func): assert clause.op.name == funcproto.name arglist, guardlist = zip(*list( map(lambda x: _do_instrument_constraint(x, funcproto), clause.arg1))) for g in guardlist: guards.extend(g) for x, a in zip(funcproto.arglist, arglist): guards.append(Expr('not', Expr('=', Expr(x), a))) return Expr(clause.op, list(map(Expr, funcproto.arglist))), guards a1, guard1 = _do_instrument_constraint(clause.arg1, funcproto) a2, guard2 = _do_instrument_constraint(clause.arg2, funcproto) a3, guard3 = _do_instrument_constraint(clause.arg3, funcproto) return Expr(clause.op, a1, a2, a3), guard1 + guard2 + guard3
def _cleanup_eq(expr): if expr is None: return None op = expr.op if isinstance(op, int): return expr if isinstance(op, bool): return expr if isinstance(op, Func): return Expr(op, list(map(_cleanup_eq, expr.arg1))) if op == '=': if expr.arg1.arg1 is None and expr.arg1.op == expr.arg2.op: return Expr(True) if expr.arg1 is None: return expr arg1 = _cleanup_eq(expr.arg1) arg2 = _cleanup_eq(expr.arg2) arg3 = _cleanup_eq(expr.arg3) if op == 'not': if isinstance(arg1.op, bool): return Expr(not arg1.op) elif op == 'or': if isinstance(arg1.op, bool): if arg1.op: return Expr(True) else: return arg2 if isinstance(arg2.op, bool): if arg2.op: return Expr(True) else: return arg1 elif op == 'and': if isinstance(arg1.op, bool): if arg1.op: return arg2 else: return Expr(False) if isinstance(arg2.op, bool): if arg2.op: return arg1 else: return Expr(False) elif op == '=>': if isinstance(arg1.op, bool): if arg1.op: return arg2 else: return Expr(True) if isinstance(arg2.op, bool): if arg2.op: return Expr(True) else: return arg1 elif op == 'xor': if isinstance(arg1.op, bool): if arg1.op: if isinstance(arg2.op, bool): return Expr(not arg2.op) else: return Expr('not', arg2) else: return arg2 if isinstance(arg2.op, bool): if arg2.op: return Expr('not', arg1) else: return arg1 elif op in ['iff', '=', 'xnor']: if isinstance(arg1.op, bool): if arg1.op: return arg2 else: if isinstance(arg2.op, bool): return Expr(not arg2.op) else: return Expr('not', arg2) if isinstance(arg2.op, bool): if arg2.op: return arg1 else: return Expr('not', arg1) elif op == 'nand': if isinstance(arg1.op, bool): if arg1.op: if isinstance(arg2.op, bool): return Expr(not arg2.op) else: return Expr('not', arg2) else: return Expr(True) if isinstance(arg2.op, bool): if arg2.op: return Expr('not', arg1) else: return Expr(True) elif op == 'nor': if isinstance(arg1.op, bool): if arg1.op: return Expr(False) else: if isinstance(arg2.op, bool): return Expr(not arg2.op) else: return Expr('not', arg2) if isinstance(arg2.op, bool): if arg2.op: return Expr(False) else: return Expr('not', arg1) return Expr(op, arg1, arg2, arg3)
def eliminateOp(expr): if expr.op == '=>': return Expr('or', Expr('not', eliminateOp(expr.arg1)), eliminateOp(expr.arg2)) if expr.op == 'xor': return Expr( 'or', Expr('and', Expr('not', eliminateOp(expr.arg1)), eliminateOp(expr.arg2)), Expr('and', eliminateOp(expr.arg1), Expr('not', eliminateOp(expr.arg2)))) if expr.op in ['xnor', 'iff']: return Expr( 'or', Expr('and', eliminateOp(expr.arg1), eliminateOp(expr.arg2)), Expr('and', Expr('not', eliminateOp(expr.arg1)), Expr('not', eliminateOp(expr.arg2)))) if expr.op == 'nand': return Expr( 'not', Expr('and', eliminateOp(expr.arg1), eliminateOp(expr.arg2))) if expr.op == 'nor': return Expr('not', Expr('or', eliminateOp(expr.arg1), eliminateOp(expr.arg2))) if expr.op == 'ite': return Expr( 'or', Expr('and', eliminateOp(expr.arg1), eliminateOp(expr.arg2)), Expr('and', Expr('not', eliminateOp(expr.arg1)), eliminateOp(expr.arg3))) return expr
def getExpr(self): return Expr('ite', self.pred, self.left.getExpr(), self.right.getExpr())