001// ***** This file is automatically generated from Bound.java.jpp 002 003package daikon.inv.unary.scalar; 004 005import daikon.*; 006import daikon.inv.*; 007 008 import daikon.inv.binary.sequenceScalar.*; 009 import daikon.inv.unary.sequence.*; 010 011import daikon.derive.unary.*; 012import daikon.inv.unary.*; 013import java.util.*; 014import org.checkerframework.checker.interning.qual.Interned; 015import org.checkerframework.checker.lock.qual.GuardSatisfied; 016import org.checkerframework.checker.nullness.qual.Nullable; 017import org.checkerframework.dataflow.qual.Pure; 018import org.checkerframework.dataflow.qual.SideEffectFree; 019import org.checkerframework.framework.qual.Unused; 020import org.plumelib.util.Intern; 021import typequals.prototype.qual.NonPrototype; 022import typequals.prototype.qual.Prototype; 023 024 /** 025 * Represents the invariant {@code x <= c}, where {@code c} is a constant and 026 * {@code x} is a long scalar. 027 */ 028 029// One reason not to combine LowerBound and UpperBound into a single range 030// invariant is that they have separate justifications: one may be 031// justified when the other is not. 032@SuppressWarnings("UnnecessaryParentheses") // code generated by macros 033public class UpperBound extends SingleScalar { 034 static final long serialVersionUID = 20030822L; 035 036 // Variables starting with dkconfig_ should only be set via the 037 // daikon.config.Configuration interface. 038 /** Boolean. True iff UpperBound invariants should be considered. */ 039 public static boolean dkconfig_enabled = Invariant.invariantEnabledDefault; 040 /** 041 * Long integer. Together with the corresponding {@code maximal_interesting} parameter, 042 * specifies the range of the computed constant that is ``interesting'' --- the range that should 043 * be reported. For instance, setting {@code minimal_interesting} to -1 and 044 * {@code maximal_interesting} to 2 would only permit output of UpperBound invariants whose 045 * cutoff was one of (-1,0,1,2). 046 */ 047 public static long dkconfig_minimal_interesting = -1; 048 /** 049 * Long integer. Together with the corresponding {@code minimal_interesting} parameter, 050 * specifies the range of the computed constant that is ``interesting'' --- the range that should 051 * be reported. For instance, setting {@code minimal_interesting} to -1 and 052 * {@code maximal_interesting} to 2 would only permit output of UpperBound invariants whose 053 * cutoff was one of (-1,0,1,2). 054 */ 055 public static long dkconfig_maximal_interesting = 2; 056 057 @Unused(when=Prototype.class) 058 private UpperBoundCore core; 059 060 @SuppressWarnings("nullness") // circular initialization 061 private UpperBound(PptSlice slice) { 062 super(slice); 063 assert slice != null; 064 core = new UpperBoundCore(this); 065 } 066 067 private @Prototype UpperBound() { 068 super(); 069 // do we need a core? 070 } 071 072 private static @Prototype UpperBound proto = new @Prototype UpperBound(); 073 074 /** Returns the prototype invariant for UpperBound */ 075 public static @Prototype UpperBound get_proto() { 076 return proto; 077 } 078 079 @Override 080 public boolean enabled() { 081 return dkconfig_enabled; 082 } 083 084 @Override 085 public boolean instantiate_ok(VarInfo[] vis) { 086 087 if (!valid_types(vis)) { 088 return false; 089 } 090 091 return vis[0].file_rep_type.baseIsIntegral(); 092 } 093 094 @Override 095 public UpperBound instantiate_dyn(@Prototype UpperBound this, PptSlice slice) { 096 return new UpperBound(slice); 097 } 098 099 @SideEffectFree 100 @Override 101 public UpperBound clone(@GuardSatisfied UpperBound this) { 102 UpperBound result = (UpperBound) super.clone(); 103 result.core = core.clone(); 104 result.core.wrapper = result; 105 return result; 106 } 107 108 public long max() { 109 return core.max(); // i.e., core.max1 110 } 111 112 @Override 113 public String repr(@GuardSatisfied UpperBound this) { 114 return "UpperBound" + varNames() + ": " 115 + core.repr(); 116 } 117 118 @SideEffectFree 119 @Override 120 public String format_using(@GuardSatisfied UpperBound this, OutputFormat format) { 121 String name = var().name_using(format); 122 PptTopLevel pptt = ppt.parent; 123 124 if ((format == OutputFormat.DAIKON) 125 || (format == OutputFormat.ESCJAVA) 126 || format.isJavaFamily() 127 || (format == OutputFormat.CSHARPCONTRACT)) { 128 129 if (PrintInvariants.dkconfig_static_const_infer) { 130 for (VarInfo vi : pptt.var_infos) { 131 // Check is static constant, and variables are comparable 132 if (vi.isStaticConstant() && VarComparability.comparable(vi, var())) { 133 // If variable is a double, then use fuzzy comparison 134 if (vi.rep_type == ProglangType.DOUBLE) { 135 Double constantVal = (Double)vi.constantValue(); 136 if (Global.fuzzy.eq(constantVal, (double)(core.max1)) || false) { 137 return name + " <= " + vi.name(); 138 } 139 } 140 // Otherwise just use the equals method 141 else { 142 Object constantVal = vi.constantValue(); 143 if (constantVal.equals(core.max1)) { 144 return name + " <= " + vi.name(); 145 } 146 } 147 } 148 } 149 } 150 151 return name + " <= " + core.max1; 152 } 153 154 if (format == OutputFormat.SIMPLIFY) { 155 156 return "(<= " + name + " " + simplify_format_long(core.max1) + ")"; 157 } 158 159 return format_unimplemented(format); 160 } 161 162 @Override 163 public InvariantStatus add_modified(long value, int count) { 164 // System.out.println("UpperBound" + varNames() + ": " 165 // + "add(" + value + ", " + modified + ", " + count + ")"); 166 167 return core.add_modified(value, count); 168 169 } 170 171 @Override 172 public InvariantStatus check_modified(long value, int count) { 173 174 return core.check(value); 175 176 } 177 178 @Override 179 public boolean enoughSamples(@GuardSatisfied UpperBound this) { 180 return core.enoughSamples(); 181 } 182 183 @Override 184 protected double computeConfidence() { 185 return core.computeConfidence(); 186 } 187 188 @Pure 189 @Override 190 public boolean isExact() { 191 return core.isExact(); 192 } 193 194 @Pure 195 @Override 196 public boolean isSameFormula(Invariant other) { 197 return core.isSameFormula(((UpperBound) other).core); 198 } 199 200 @Override 201 public boolean hasUninterestingConstant() { 202 // If the constant bound is not in some small range of interesting 203 // values (by default {-1, 0, 1, 2}), call it uninteresting. 204 if ((core.max1 < dkconfig_minimal_interesting) 205 || (core.max1 > dkconfig_maximal_interesting)) { 206 return true; 207 } 208 209 return false; 210 } 211 212 @Pure 213 @Override 214 public @Nullable DiscardInfo isObviousStatically(VarInfo[] vis) { 215 VarInfo var = vis[0]; 216 if ((var.derived instanceof SequenceLength) 217 && (((SequenceLength) var.derived).shift != 0)) { 218 return new DiscardInfo(this, DiscardCode.obvious, "Bounds are preferrable over sequence lengths with no shift"); 219 } 220 221 if (var.aux.hasValue(VarInfoAux.MAXIMUM_VALUE)) { 222 int minVal = var.aux.getInt(VarInfoAux.MAXIMUM_VALUE); 223 if (minVal == core.max1) { 224 return new DiscardInfo(this, DiscardCode.obvious, 225 var.name() + " GTE " + core.max1 + " is already known"); 226 } 227 } 228 229 return super.isObviousStatically(vis); 230 } 231 232 @Pure 233 @Override 234 public @Nullable DiscardInfo isObviousDynamically(VarInfo[] vis) { 235 DiscardInfo super_result = super.isObviousDynamically(vis); 236 if (super_result != null) { 237 return super_result; 238 } 239 240 PptTopLevel pptt = ppt.parent; 241 242 // This check always lets invariants pass through (even if it is not within 243 // the default range of (-1 to 2) if it matches a static constant 244 // As noted below, this check really doesn't belong here, but should be 245 // moved to hasUninterestingConstant() whenever that is implemented 246 if (PrintInvariants.dkconfig_static_const_infer) { 247 if (core.matchConstant()) { 248 return null; 249 } 250 } 251 252 // if the value is not in some range (like -1,0,1,2) then say that it is obvious 253 if ((core.max1 < dkconfig_minimal_interesting) 254 || (core.max1 > dkconfig_maximal_interesting)) { 255 // XXX This check doesn't really belong here. However It 256 // shouldn't get removed until hasUninterestingConstant() is 257 // suitable to be turned on everywhere by default. -SMcC 258 // if the value is not in some range (like -1,0,1,2) then say that 259 // it is obvious 260 String discardString = ""; 261 if (core.max1 < dkconfig_minimal_interesting) { 262 discardString = "MIN1="+core.max1+" is less than dkconfig_minimal_interesting==" 263 + dkconfig_minimal_interesting; 264 } else { 265 discardString = "MIN1="+core.max1+" is greater than dkconfig_maximal_interesting=="+ 266 dkconfig_maximal_interesting; 267 } 268 return new DiscardInfo(this, DiscardCode.obvious, discardString); 269 } 270 OneOfScalar oo = OneOfScalar.find(ppt); 271 if ((oo != null) && oo.enoughSamples() && oo.num_elts() > 0) { 272 assert oo.var().isCanonical(); 273 // We could also use core.max1 == oo.max_elt(), since the LowerBound 274 // will never have a core.max1 that does not appear in the OneOf. 275 if (core.max1 >= oo.max_elt()) { 276 String varName = vis[0].name(); 277 String discardString = varName + ">=" + core.max1 + " is implied by " + varName + ">=" + oo.max_elt(); 278 log("%s", discardString); 279 return new DiscardInfo(this, DiscardCode.obvious, discardString); 280 } 281 } 282 283 // NOT: "VarInfo v = var();" because we want to operate not on this 284 // object's own variables, but on the variables that were passed in. 285 VarInfo v = vis[0]; 286 287 // For each sequence variable, if this is an obvious member/subsequence, and 288 // it has the same invariant, then this one is obvious. 289 for (int i = 0; i < pptt.var_infos.length; i++) { 290 VarInfo vi = pptt.var_infos[i]; 291 292 if (Member.isObviousMember(v, vi)) 293 { 294 PptSlice1 other_slice = pptt.findSlice(vi); 295 if (other_slice != null) { 296 EltUpperBound eb = EltUpperBound.find(other_slice); 297 if ((eb != null) 298 && eb.enoughSamples() 299 && eb.max() == max()) { 300 String otherName = other_slice.var_infos[0].name(); 301 String varName = v.name(); 302 String discardString = varName+" is a subsequence of "+otherName+" for which the invariant holds."; 303 log("%s", discardString); 304 return new DiscardInfo(this, DiscardCode.obvious, discardString); 305 } 306 } 307 } 308 } 309 310 return null; 311 } 312 313 @Pure 314 @Override 315 public boolean isExclusiveFormula(Invariant other) { 316 317 if (other instanceof LowerBound) { 318 if (max() < ((LowerBound) other).min()) { 319 return true; 320 } 321 } 322 323 if (other instanceof OneOfScalar) { 324 return other.isExclusiveFormula(this); 325 } 326 return false; 327 } 328 329 // Look up a previously instantiated invariant. 330 public static @Nullable UpperBound find(PptSlice ppt) { 331 assert ppt.arity() == 1; 332 for (Invariant inv : ppt.invs) { 333 if (inv instanceof UpperBound) { 334 return (UpperBound) inv; 335 } 336 } 337 return null; 338 } 339 340 /** 341 * Bound can merge different formulas from lower points to create a single formula at an upper 342 * point. See merge() below. 343 */ 344 @Override 345 public boolean mergeFormulasOk() { 346 return true; 347 } 348 349 /** 350 * Merge the invariants in invs to form a new invariant. Each must be a UpperBound invariant. This 351 * code finds all of the min/max values in each invariant, applies them to a new parent invariant 352 * and returns the merged invariant (if any). 353 * 354 * @param invs list of invariants to merge. The invariants must all be of the same type and should 355 * come from the children of parent_ppt. They should also all be permuted to match the 356 * variable order in parent_ppt. 357 * @param parent_ppt slice that will contain the new invariant 358 */ 359 @Override 360 public @Nullable UpperBound merge(List<Invariant> invs, PptSlice parent_ppt) { 361 362 // Create the initial parent invariant from the first child 363 UpperBound first = (UpperBound) invs.get(0); 364 UpperBound result= first.clone(); 365 result.ppt = parent_ppt; 366 367 // Loop through the rest of the child invariants 368 for (int i = 1; i < invs.size(); i++ ) { 369 UpperBound lb = (UpperBound) invs.get(i); 370 result.core.add(lb.core); 371 } 372 373 result.log("Merged '%s' from %s child invariants", result.format(),invs.size()); 374 return result; 375 } 376}