001// ***** This file is automatically generated from SeqIndexComparison.java.jpp
002
003package daikon.inv.unary.sequence;
004
005import daikon.*;
006import daikon.Quantify.QuantFlags;
007import daikon.inv.*;
008import daikon.inv.binary.twoSequence.*;
009import daikon.suppress.*;
010import java.util.Arrays;
011import java.util.Iterator;
012import java.util.logging.Logger;
013import org.checkerframework.checker.interning.qual.Interned;
014import org.checkerframework.checker.lock.qual.GuardSatisfied;
015import org.checkerframework.checker.nullness.qual.NonNull;
016import org.checkerframework.checker.nullness.qual.Nullable;
017import org.checkerframework.dataflow.qual.Pure;
018import org.checkerframework.dataflow.qual.SideEffectFree;
019import org.plumelib.util.Intern;
020import typequals.prototype.qual.NonPrototype;
021import typequals.prototype.qual.Prototype;
022
023/**
024 * Represents an invariant over sequences of long values between the index of an element of the
025 * sequence and the element itself. Prints as {@code x[i] > i}.
026 */
027public class SeqIndexIntGreaterThan extends SingleScalarSequence {
028  static final long serialVersionUID = 20040203L;
029
030  /** Debug tracer. */
031  public static final Logger debug =
032    Logger.getLogger("daikon.inv.unary.sequence.SeqIndexIntGreaterThan");
033
034  // Variables starting with dkconfig_ should only be set via the
035  // daikon.config.Configuration interface.
036  /** Boolean. True iff SeqIndexIntGreaterThan invariants should be considered. */
037  public static boolean dkconfig_enabled = false;
038
039  private @Prototype SeqIndexIntGreaterThan() {
040    super();
041  }
042
043  protected SeqIndexIntGreaterThan(PptSlice slice) {
044    super(slice);
045    assert slice != null;
046    assert var().rep_type == ProglangType.INT_ARRAY;
047  }
048
049  private static @Prototype SeqIndexIntGreaterThan proto = new @Prototype SeqIndexIntGreaterThan();
050
051  /** Returns the prototype invariant for SeqIndexIntGreaterThan */
052  public static @Prototype SeqIndexIntGreaterThan get_proto() {
053    return proto;
054  }
055
056  @Override
057  public boolean enabled() {
058    return dkconfig_enabled && !dkconfig_SeqIndexDisableAll;
059  }
060
061  @Override
062  public boolean instantiate_ok(VarInfo[] vis) {
063
064    if (!valid_types(vis)) {
065      return false;
066    }
067
068    // Don't compare indices to object addresses.
069    ProglangType elt_type = vis[0].file_rep_type.elementType();
070    if (!elt_type.baseIsIntegral()) {
071      return false;
072    }
073
074    // Make sure that the indices are comparable to the elements
075    VarInfo seqvar = vis[0];
076    assert seqvar.comparability != null;
077    VarComparability elt_compar = seqvar.comparability.elementType();
078    VarComparability index_compar = seqvar.comparability.indexType(0);
079    if (!VarComparability.comparable(elt_compar, index_compar)) {
080      return false;
081    }
082
083    return true;
084  }
085
086  @Override
087  public SeqIndexIntGreaterThan instantiate_dyn(@Prototype SeqIndexIntGreaterThan this, PptSlice slice) {
088    return new SeqIndexIntGreaterThan(slice);
089  }
090
091  /** returns the ni-suppressions for SeqIndexIntGreaterThan */
092  @Pure
093  @Override
094  public @Nullable NISuppressionSet get_ni_suppressions() {
095    return null;
096  }
097
098  protected Invariant resurrect_done_swapped() {
099
100      return new SeqIndexIntLessThan(ppt);
101  }
102
103  public String getComparator() {
104    return ">";
105  }
106
107  @SideEffectFree
108  @Override
109  public String format_using(@GuardSatisfied SeqIndexIntGreaterThan this, OutputFormat format) {
110    if (format.isJavaFamily()) {
111      return format_java_family(format);
112    }
113
114    // TODO: Eliminate the unnecessary format_xxx() below if the
115    // format_java_family() can handle all the Java family output.
116
117    if (format == OutputFormat.DAIKON) {
118      return format_daikon();
119    }
120    if (format == OutputFormat.ESCJAVA) {
121      return format_esc();
122    }
123    if (format == OutputFormat.CSHARPCONTRACT) {
124      return format_csharp_contract();
125    }
126    if (format == OutputFormat.SIMPLIFY) {
127      return format_simplify();
128    }
129
130    return format_unimplemented(format);
131  }
132
133  public String format_daikon(@GuardSatisfied SeqIndexIntGreaterThan this) {
134
135    // If this is an array/container and not a subsequence
136    if (var().isDerivedSubSequenceOf() == null) {
137      return var().apply_subscript("i") + " > i";
138    } else {
139      return var().name() + " > (index)";
140    }
141  }
142
143  // Bad code here: if the first index is changed from i this breaks
144  public String format_esc(@GuardSatisfied SeqIndexIntGreaterThan this) {
145    String[] form = VarInfo.esc_quantify(var());
146    return form[0] + "(" + form[1] + " > i)" + form[2];
147  }
148
149  public String format_csharp_contract(@GuardSatisfied SeqIndexIntGreaterThan this) {
150    String[] split = var().csharp_array_split();
151    return "Contract.ForAll(0, " + split[0] + ".Count(), i => " + split[0] + "[i]" + split[1] + " > i)";
152  }
153
154  public String format_java_family(@GuardSatisfied SeqIndexIntGreaterThan this, OutputFormat format) {
155    return "daikon.Quant.eltsGtIndex("
156      + var().name_using(format) + ")";
157  }
158
159  public String format_simplify(@GuardSatisfied SeqIndexIntGreaterThan this) {
160    String[] form = VarInfo.simplify_quantify(QuantFlags.include_index(),
161                                               var());
162    return form[0] + "(> " + form[1] + " " + form[2] + ")"
163      + form[3];
164  }
165
166  @Override
167  public InvariantStatus check_modified(long @Interned [] a, int count) {
168    for (int i = 0; i < a.length; i++) {
169      if (!(a[i] > i)) {
170        return InvariantStatus.FALSIFIED;
171      }
172    }
173    return InvariantStatus.NO_CHANGE;
174  }
175
176  @Override
177  public InvariantStatus add_modified(long @Interned [] a, int count) {
178
179    if (logDetail()) {
180      log("Entered add_modified: ppt.num_values()==%s, sample==%s",
181           ppt.num_values(), Arrays.toString(a));
182    }
183    InvariantStatus stat = check_modified(a, count);
184    if (logDetail()) {
185      log("Exiting add_modified status = %s", stat);
186    }
187
188    return stat;
189  }
190
191  @Override
192  @SuppressWarnings("UnnecessaryParentheses")  // generated code, parentheses are sometimes needed
193  protected double computeConfidence() {
194
195    // Make sure there have been some elements in the sequence
196    ValueSet.ValueSetScalarArray vs = (ValueSet.ValueSetScalarArray) ppt.var_infos[0].get_value_set();
197    if (vs.elem_cnt() == 0) {
198      return Invariant.CONFIDENCE_UNJUSTIFIED;
199    }
200
201    int num_values = ppt.num_values();
202    if (num_values == 0) {
203      return Invariant.CONFIDENCE_UNJUSTIFIED;
204    }
205
206      return 1 - Math.pow(.5, num_values);
207  }
208
209  @Pure
210  @Override
211  public boolean isSameFormula(Invariant other) {
212    return true;
213  }
214
215  @Pure
216  @Override
217  public boolean isExclusiveFormula(Invariant other) {
218    return false;
219  }
220
221  // Look up a previously instantiated invariant.
222  public static @Nullable SeqIndexIntGreaterThan find(PptSlice ppt) {
223    assert ppt.arity() == 1;
224    for (Invariant inv : ppt.invs) {
225      if (inv instanceof SeqIndexIntGreaterThan) {
226        return (SeqIndexIntGreaterThan) inv;
227      }
228    }
229    return null;
230  }
231
232  /**
233   * Checks to see if this is obvious over the specified variables Implements the following checks:
234   *
235   * <pre>
236   *    (A[] subsequence B[]) ^ (B[i] op i) &rArr; A[i] op i
237   * </pre>
238   *
239   * JHP: Its not obvious (to me) that this is true except when the subsequence starts at index
240   * 0. If B[] = {0, 1, 2, 3, 4} and A[] = {2, 3, 4}, A[] is a subsequence of B[] and B[i] == i, but
241   * A[i] = i is not true.
242   */
243  @Pure
244  @Override
245  public @Nullable DiscardInfo isObviousDynamically(VarInfo[] vis) {
246
247    DiscardInfo super_result = super.isObviousDynamically(vis);
248    if (super_result != null) {
249      return super_result;
250    }
251
252    VarInfo seqvar = vis[0];
253
254    // For each other sequence variable, if it is a supersequence of this
255    // one and it has the same invariant, then this one is obvious.
256    // We have to check for the same equality set here, because
257    // isObviousDynamically is called for each member of the equality set.
258    // We don't want other members of our own equality set to make this obvious
259    PptTopLevel pptt = ppt.parent;
260    for (int i = 0; i < pptt.var_infos.length; i++) {
261      VarInfo vi = pptt.var_infos[i];
262      if (vi.equalitySet == seqvar.equalitySet) {
263        continue;
264      }
265      if (SubSequence.isObviousSubSequenceDynamically(this, seqvar, vi)) {
266        PptSlice1 other_slice = pptt.findSlice(vi);
267        if (other_slice != null) {
268          SeqIndexIntGreaterThan other_sine = SeqIndexIntGreaterThan.find(other_slice);
269          if ((other_sine != null) && other_sine.enoughSamples()) {
270            return new DiscardInfo(this, DiscardCode.obvious,
271                        "The invariant " + format() + " over var "
272                       + seqvar.name() + " also holds over "
273                       +" the supersequence " + vi.name());
274          }
275        }
276      }
277    }
278
279    return null;
280  }
281}