sbool ai_triplesToTripleRefs_lazyList_debug;

static L<TripleRef<Symbol>> ai_triplesToTripleRefs_lazyList(final Symbol searchTerm, final L<TripleWeb> l) {
  if (l == null) null;
  class TTTRList extends LazyList<TripleRef<Symbol>> {
    final int n = l(l);
    public int size() { ret n; }
    public TripleRef<Symbol> get(int i) {
      if (ai_triplesToTripleRefs_lazyList_debug) print("tttrlist.get " + i + "/" + l(l));
      TripleWeb w = syncGet(l, i);
      if (w == null) null;
      
      // run-length magic - TODO: concatenated lists
      int runLength = 0;
      while (i-runLength > 0 && l.get(i-runLength-1) == w)
        ++runLength;
        
      ret ai_tripleToTripleRef(l.get(i), searchTerm, runLength); // may be null
    }
  }
  ret new TTTRList;
}