def flattenInstances(instances): ''' sets the new polarities to corresponding ternary values ''' for (sort, index) in instances.keys(): final_expr = mOr(SMTLib.SMT_BoolConst(False)) (l, h, _) = sort.instanceRanges[index] (exprs, pols) = instances[(sort, index)] final_polarity = Common.DEFINITELY_OFF all_on = True for i in range(l, h + 1): currExpr = exprs.get(i) currPol = pols.get(i) if not (i in pols.keys()) or currPol == Common.DEFINITELY_OFF: all_on = False continue elif currPol == Common.UNKNOWN: all_on = False final_polarity = Common.UNKNOWN final_expr = mOr(final_expr, currExpr) else: final_polarity = Common.UNKNOWN final_expr = mOr(final_expr, currExpr) if all_on: final_polarity = Common.DEFINITELY_ON final_expr = SMTLib.SMT_BoolConst(True) instances[(sort, index)] = (final_expr, final_polarity) return instances
def op_union(left,right): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~ExprArg` Computes the set union (left ++ right) ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) if left.getInts() or right.getInts(): sys.exit("FIXME ints union") matches = getSetInstancePairs(left,right) newInstances = {} for (sort,index) in matches.keys(): key = (sort,index) ((lexpr,lpol),(rexpr,rpol)) = matches[(sort,index)] if rpol == Common.DEFINITELY_OFF and lpol == Common.DEFINITELY_OFF: continue else: new_expr = mOr(lexpr,rexpr) newInstances[key] = (new_expr, Common.aggregate_polarity(lpol, rpol)) return ExprArg(newInstances)
def joinWithParent(arg): instances = arg.getInstances(nonsupered=True) newInstances = {} for (sort, index) in instances.keys(): (expr, pol) = instances[(sort, index)] if pol == Common.DEFINITELY_OFF: continue (lower, upper, _) = sort.instanceRanges[index] for i in range(lower, min(sort.parentInstances, upper + 1)): (old_expr, old_pol) = newInstances.get( (sort.parent, i), (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) if pol == Common.DEFINITELY_ON and lower == upper: new_pol = Common.DEFINITELY_ON new_expr = SMTLib.SMT_BoolConst(True) else: new_pol = Common.aggregate_polarity(old_pol, Common.UNKNOWN) new_expr = mOr( old_expr, mAnd( expr, SMTLib.SMT_EQ(sort.instances[index], SMTLib.SMT_IntConst(i)))) newInstances[(sort.parent, i)] = (new_expr, new_pol) return ExprArg(newInstances, nonsupered=True)
def op_union(left, right): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~ExprArg` Computes the set union (left ++ right) ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) if left.getInts() or right.getInts(): sys.exit("FIXME ints union") matches = getSetInstancePairs(left, right) newInstances = {} for (sort, index) in matches.keys(): key = (sort, index) ((lexpr, lpol), (rexpr, rpol)) = matches[(sort, index)] if rpol == Common.DEFINITELY_OFF and lpol == Common.DEFINITELY_OFF: continue else: new_expr = mOr(lexpr, rexpr) newInstances[key] = (new_expr, Common.aggregate_polarity(lpol, rpol)) return ExprArg(newInstances)
def compute_int_set(instances): cons = [] for index in range(len(instances)): (i, c) = instances[index] cons.append( mAnd( c, *[ mOr(SMTLib.createNot(jc), SMTLib.SMT_NE(j, i)) for (j, jc) in instances[0:index] ])) return cons
def addMatchValues(matches, instances, left=True): ''' Ignores PrimitiveSorts ''' for (sort, index) in instances.keys(): (expr,polarity) = instances[(sort,index)] #!!! default = (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF) (prev_left, prev_right) = matches.get((sort,index), (default,default)) if left: (prev_expr, prev_pol) = prev_left new_left = (mOr(expr, prev_expr), Common.aggregate_polarity(polarity, prev_pol)) new_right = prev_right else: (prev_expr, prev_pol) = prev_right new_left = prev_left new_right = (mOr(expr, prev_expr), Common.aggregate_polarity(polarity, prev_pol)) matches[(sort,index)] = (new_left,new_right) return matches
def addMatchValues(matches, instances, left=True): ''' Ignores PrimitiveSorts ''' for (sort, index) in instances.keys(): (expr, polarity) = instances[(sort, index)] #!!! default = (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF) (prev_left, prev_right) = matches.get((sort, index), (default, default)) if left: (prev_expr, prev_pol) = prev_left new_left = (mOr(expr, prev_expr), Common.aggregate_polarity(polarity, prev_pol)) new_right = prev_right else: (prev_expr, prev_pol) = prev_right new_left = prev_left new_right = (mOr(expr, prev_expr), Common.aggregate_polarity(polarity, prev_pol)) matches[(sort, index)] = (new_left, new_right) return matches
def op_or(left,right): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~BoolArg` Computes the boolean disjunction of the left and right instances. ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) lval = left.getBool() rval = right.getBool() return BoolArg(mOr(lval, rval))
def getInstances(self, nonsupered=False): if nonsupered or self.hasBeenSupered: return self.clafers else: newClafers = {} for (sort, index) in self.clafers.keys(): (expr, polarity) = self.clafers[(sort,index)] if polarity == Common.DEFINITELY_OFF: continue key = (sort.highestSuperSort, sort.indexInHighestSuper + index) if polarity == Common.DEFINITELY_ON: newClafers[key] = (SMTLib.SMT_BoolConst(True), Common.DEFINITELY_ON) continue (currEntry, currPolarity) = newClafers.get(key, (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) currEntry = mOr(currEntry, expr) newClafers[key] = (currEntry, Common.aggregate_polarity(currPolarity, polarity)) self.clafers = newClafers self.hasBeenSupered = True return self.clafers
def getQuantifierConditionList(exprs): ''' Iterates over all values in all masks, and returns a list of Booleans, indicating whether each instance is on (or true), or not. ''' finalList = [] for i in exprs: for expr in i: if isinstance(expr, BoolArg): finalList.append(expr.getBool()) continue condList = [] for k in expr.getInstances().values(): (e, pol) = k if pol != Common.DEFINITELY_ON: condList.append(e) else: condList.append(SMTLib.SMT_BoolConst(True)) break finalList.append(mOr(*condList)) return finalList
def getInstances(self, nonsupered=False): if nonsupered or self.hasBeenSupered: return self.clafers else: newClafers = {} for (sort, index) in self.clafers.keys(): (expr, polarity) = self.clafers[(sort, index)] if polarity == Common.DEFINITELY_OFF: continue key = (sort.highestSuperSort, sort.indexInHighestSuper + index) if polarity == Common.DEFINITELY_ON: newClafers[key] = (SMTLib.SMT_BoolConst(True), Common.DEFINITELY_ON) continue (currEntry, currPolarity) = newClafers.get( key, (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) currEntry = mOr(currEntry, expr) newClafers[key] = (currEntry, Common.aggregate_polarity( currPolarity, polarity)) self.clafers = newClafers self.hasBeenSupered = True return self.clafers
def joinWithClaferRef(arg): instances = arg.getInstances(nonsupered=True) newArg = ExprArg(nonsupered=True) for (sort, index) in instances.keys(): (expr, pol) = instances[(sort, index)] while not sort.refSort: (sort, index) = joinWithSuper(sort, index) if isinstance(sort.refSort, PrimitiveType): joinWithPrimitive(newArg, sort, index, expr, pol) else: for i in range(sort.refSort.numInstances): (prev_expr, _) = newArg.getInstances(nonsupered=True).get( (sort.refSort, i), (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) newArg.getInstances(nonsupered=True)[(sort.refSort, i)] = (mOr( prev_expr, mAnd( expr, SMTLib.SMT_EQ( sort.refs[index], SMTLib.SMT_IntConst(i)))), Common.UNKNOWN) return newArg
def op_eq(left,right, cacheJoins=False, bc = None): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~BoolArg` Ensures that the left = right. ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) if cacheJoins and bc: #TODO CLEAN left_key = None right_key = None keys = [] #asil allocation speedup, if both sides are sets, we can perform expression substitution in other constraints #bc is the bracketed constraint to put the cache for i in [left,right]: if isinstance(i, JoinArg): newkeys = Common.computeCacheKeys(i.flattenJoin()) #print(tuple(key)) keys = keys + newkeys #need to return all keys during the progress of join, add flag? #get the all keys all_keys = i.checkIfJoinIsComputed(nonsupered=True, getAllKeys = True) #print(keys) #print(all_keys) keys = keys+all_keys #sys.exit() #print() #print("GGGG right" + str(right.__class__)) #print(right.clafers) if len(left.clafers) != len(right.clafers): minJoinVal = left.clafers if len(left.clafers) < len(right.clafers) else right.clafers for i in keys: #TODO make more robust (e.g. if multiple equalities exist for the same join key, aggregate expressions bc.cache[i] = ExprArg(minJoinVal) #print(i) #print(minJoinVal) #print(str(len(minJoinVal)) + " " + str(len(left.clafers)) + " " + str(len(right.clafers))) #print(str(len(left.clafers)) + " " + str(len(right.clafers))) cond = [] #int equality case lints = [(e,c) for (e,c) in left.getInts() if str(c) != "False"] rints = [(e,c) for (e,c) in right.getInts() if str(c) != "False"] if lints or rints: for (e,c) in lints: #exists r in R s.t. e == r expr = mOr(*[mAnd(rc, SMTLib.SMT_EQ(e,r)) for (r,rc) in rints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) for (e,c) in rints: #exists l in L s.t. e == l expr = mOr(*[mAnd(lc, SMTLib.SMT_EQ(e,l)) for (l,lc) in lints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) #clafer-set equality case matches = getSetInstancePairs(left,right) for ((lexpr, lpol),(rexpr, rpol)) in matches.values(): if lpol == Common.DEFINITELY_OFF and rpol == Common.DEFINITELY_OFF: continue elif lpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(rexpr)) elif rpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(lexpr)) else: cond.append(SMTLib.SMT_Implies(lexpr, rexpr)) cond.append(SMTLib.SMT_Implies(rexpr, lexpr)) return BoolArg(mAnd(*cond))
def quant_no(exprs, ifConstraints): condList = getQuantifierConditionList(exprs) if ifConstraints: condList = [mAnd(i, j) for i,j in zip(ifConstraints, condList)] return SMTLib.createNot(mOr(*condList))
def quant_some(exprs, ifConstraints): condList = getQuantifierConditionList(exprs) if ifConstraints: condList = [mAnd(i, j) for i, j in zip(ifConstraints, condList)] return mOr(*condList)
def quant_no(exprs, ifConstraints): condList = getQuantifierConditionList(exprs) if ifConstraints: condList = [mAnd(i, j) for i, j in zip(ifConstraints, condList)] return SMTLib.createNot(mOr(*condList))
def compute_int_set(instances): cons = [] for index in range(len(instances)): (i,c) = instances[index] cons.append(mAnd(c, *[mOr(SMTLib.createNot(jc), SMTLib.SMT_NE(j,i)) for (j, jc) in instances[0:index]])) return cons
def addRefConstraints(self): if not self.refSort: return elif isinstance(self.refSort, PrimitiveType) and self.refSort.type == "real": self.refs = SMTLib.SMT_RealVector(self.element.uid + "_ref",self.numInstances) elif isinstance(self.refSort, PrimitiveType): self.refs = SMTLib.SMT_IntVector(self.element.uid + "_ref",self.numInstances) else: self.refs = SMTLib.SMT_IntVector(self.element.uid + "_ref",self.numInstances, bits=self.getBits(self.refSort.parentInstances+1)) if not isinstance(self.refSort, PrimitiveType): for i in range(self.numInstances): #refs pointer is >= 0 self.constraints.addRefConstraint(SMTLib.SMT_GE(self.refs[i], SMTLib.SMT_IntConst(0))) #ref pointer is <= upper card of ref parent self.constraints.addRefConstraint(SMTLib.SMT_LE(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if integer refs, zero out refs that do not have live parents, #if clafer refs, set equal to ref.parentInstances if not live #reference symmetry breaking if not self.element.isAbstract: for i in range(self.numInstances - 1): for j in range(i+1, self.numInstances): if isinstance(self.refSort, PrimitiveType): self.constraints.addRefConstraint(SMTLib.SMT_Implies(SMTLib.SMT_EQ(self.instances[i],self.instances[j]), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) else: self.constraints.addRefConstraint(SMTLib.SMT_Implies(mAnd(SMTLib.SMT_NE(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)), SMTLib.SMT_EQ(self.instances[i], self.instances[j])), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) for i in range(self.numInstances): if isinstance(self.refSort, PrimitiveType): if self.refSort == "integer": self.constraints.addRefConstraint(SMTLib.SMT_Implies(self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) elif self.refSort == "string": if Options.STRING_CONSTRAINTS: self.constraints.addRefConstraint(SMTLib.SMT_Implies(self.isOff(i), SMTLib.SMT_EQ(self.refs[i], self.cfr.EMPTYSTRING)), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint(SMTLib.SMT_Implies(self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint(SMTLib.SMT_Implies(self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: if self.known_polarity(i, local=True) != Common.DEFINITELY_ON: self.constraints.addRefConstraint(SMTLib.SMT_If(self.isOff(i) , SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)) , SMTLib.SMT_NE(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)))) else: self.constraints.addRefConstraint(SMTLib.SMT_NE(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if refsort.full does not exist, create it if not self.refSort.full: self.refSort.full = lambda x:mOr(*[SMTLib.SMT_And(SMTLib.SMT_EQ(x, SMTLib.SMT_IntConst(i)), self.refSort.isOn(i)) for i in range(self.refSort.numInstances)]) #the clafer that the reference points to must be "on" self.constraints.addRefConstraint(SMTLib.SMT_Implies(SMTLib.SMT_NE(self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)), self.refSort.full(self.refs[i])))
def quant_some(exprs, ifConstraints): condList = getQuantifierConditionList(exprs) if ifConstraints: condList = [mAnd(i, j) for i,j in zip(ifConstraints, condList)] return mOr(*condList)
def addRefConstraints(self): if not self.refSort: return elif isinstance(self.refSort, PrimitiveType) and self.refSort.type == "real": self.refs = SMTLib.SMT_RealVector(self.element.uid + "_ref", self.numInstances) elif isinstance(self.refSort, PrimitiveType): self.refs = SMTLib.SMT_IntVector(self.element.uid + "_ref", self.numInstances) else: self.refs = SMTLib.SMT_IntVector( self.element.uid + "_ref", self.numInstances, bits=self.getBits(self.refSort.parentInstances + 1)) if not isinstance(self.refSort, PrimitiveType): for i in range(self.numInstances): #refs pointer is >= 0 self.constraints.addRefConstraint( SMTLib.SMT_GE(self.refs[i], SMTLib.SMT_IntConst(0))) #ref pointer is <= upper card of ref parent self.constraints.addRefConstraint( SMTLib.SMT_LE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if integer refs, zero out refs that do not have live parents, #if clafer refs, set equal to ref.parentInstances if not live #reference symmetry breaking if not self.element.isAbstract: for i in range(self.numInstances - 1): for j in range(i + 1, self.numInstances): if isinstance(self.refSort, PrimitiveType): self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_EQ(self.instances[i], self.instances[j]), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( mAnd( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_EQ(self.instances[i], self.instances[j])), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) for i in range(self.numInstances): if isinstance(self.refSort, PrimitiveType): if self.refSort == "integer": self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) elif self.refSort == "string": if Options.STRING_CONSTRAINTS: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], self.cfr.EMPTYSTRING)), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: if self.known_polarity(i, local=True) != Common.DEFINITELY_ON: self.constraints.addRefConstraint( SMTLib.SMT_If( self.isOff(i), SMTLib.SMT_EQ( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)))) else: self.constraints.addRefConstraint( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if refsort.full does not exist, create it if not self.refSort.full: self.refSort.full = lambda x: mOr(*[ SMTLib.SMT_And( SMTLib.SMT_EQ(x, SMTLib.SMT_IntConst(i)), self.refSort.isOn(i)) for i in range(self.refSort.numInstances) ]) #the clafer that the reference points to must be "on" self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)), self.refSort.full(self.refs[i])))
def op_eq(left, right, cacheJoins=False, bc=None): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~BoolArg` Ensures that the left = right. ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) if cacheJoins and bc: #TODO CLEAN left_key = None right_key = None keys = [] #asil allocation speedup, if both sides are sets, we can perform expression substitution in other constraints #bc is the bracketed constraint to put the cache for i in [left, right]: if isinstance(i, JoinArg): newkeys = Common.computeCacheKeys(i.flattenJoin()) #print(tuple(key)) keys = keys + newkeys #need to return all keys during the progress of join, add flag? #get the all keys all_keys = i.checkIfJoinIsComputed(nonsupered=True, getAllKeys=True) #print(keys) #print(all_keys) keys = keys + all_keys #sys.exit() #print() #print("GGGG right" + str(right.__class__)) #print(right.clafers) if len(left.clafers) != len(right.clafers): minJoinVal = left.clafers if len(left.clafers) < len( right.clafers) else right.clafers for i in keys: #TODO make more robust (e.g. if multiple equalities exist for the same join key, aggregate expressions bc.cache[i] = ExprArg(minJoinVal) #print(i) #print(minJoinVal) #print(str(len(minJoinVal)) + " " + str(len(left.clafers)) + " " + str(len(right.clafers))) #print(str(len(left.clafers)) + " " + str(len(right.clafers))) cond = [] #int equality case lints = [(e, c) for (e, c) in left.getInts() if str(c) != "False"] rints = [(e, c) for (e, c) in right.getInts() if str(c) != "False"] if lints or rints: for (e, c) in lints: #exists r in R s.t. e == r expr = mOr(*[mAnd(rc, SMTLib.SMT_EQ(e, r)) for (r, rc) in rints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) for (e, c) in rints: #exists l in L s.t. e == l expr = mOr(*[mAnd(lc, SMTLib.SMT_EQ(e, l)) for (l, lc) in lints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) #clafer-set equality case matches = getSetInstancePairs(left, right) for ((lexpr, lpol), (rexpr, rpol)) in matches.values(): if lpol == Common.DEFINITELY_OFF and rpol == Common.DEFINITELY_OFF: continue elif lpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(rexpr)) elif rpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(lexpr)) else: cond.append(SMTLib.SMT_Implies(lexpr, rexpr)) cond.append(SMTLib.SMT_Implies(rexpr, lexpr)) return BoolArg(mAnd(*cond))