public static IList compose(IList rel1, IList rel2) {

    Type otherTupleType = rel2.getType().getFieldTypes();

    if (rel1.getElementType() == voidType) return rel1;
    if (otherTupleType == voidType) return rel2;

    if (rel1.getElementType().getArity() != 2 || otherTupleType.getArity() != 2)
      throw new IllegalOperationException("compose", rel1.getElementType(), otherTupleType);

    // Relaxed type constraint:
    if (!rel1.getElementType().getFieldType(1).comparable(otherTupleType.getFieldType(0)))
      throw new IllegalOperationException("compose", rel1.getElementType(), otherTupleType);

    Type[] newTupleFieldTypes =
        new Type[] {rel1.getElementType().getFieldType(0), otherTupleType.getFieldType(1)};
    Type tupleType = typeFactory.tupleType(newTupleFieldTypes);

    IListWriter w = new ListWriter(tupleType);

    for (IValue v1 : rel1) {
      ITuple tuple1 = (ITuple) v1;
      for (IValue t2 : rel2) {
        ITuple tuple2 = (ITuple) t2;

        if (tuple1.get(1).isEqual(tuple2.get(0))) {
          w.append(Tuple.newTuple(tuple1.get(0), tuple2.get(1)));
        }
      }
    }
    return w.done();
  }
Example #2
0
 private static void argumentTypes(Type argTypes, StringBuilder b) {
   int i = 0;
   for (Type arg : argTypes) {
     if (i != 0) b.append(", ");
     b.append(arg);
     if (argTypes.hasFieldNames()) {
       b.append(' ');
       b.append(argTypes.getFieldName(i));
     }
     i++;
   }
 }
  public static IList closureStar(IList rel1) {
    Type resultType = rel1.getType().closure();
    // an exception will have been thrown if the type is not acceptable

    IListWriter reflex = List.createListWriter(resultType.getElementType());

    for (IValue e : carrier(rel1)) {
      reflex.insert(Tuple.newTuple(new IValue[] {e, e}));
    }

    return closure(rel1).concat(reflex.done());
  }
Example #4
0
  @Override
  public Type getKeywordArgumentTypes(Environment scope) {
    ArrayList<String> labels = new ArrayList<>();
    ArrayList<Type> types = new ArrayList<>();
    // TODO: I am not sure this is what we want. Double names will end up twice in the tuple type...

    for (AbstractFunction c : primaryCandidates) {
      Type args = c.getKeywordArgumentTypes(scope);

      if (args != null && args.hasFieldNames()) {
        for (String label : args.getFieldNames()) {
          labels.add(label);
          types.add(args.getFieldType(label));
        }
      }
    }

    for (AbstractFunction c : defaultCandidates) {
      Type args = c.getKeywordArgumentTypes(scope);

      if (args != null && args.hasFieldNames()) {
        for (String label : args.getFieldNames()) {
          labels.add(label);
          types.add(args.getFieldType(label));
        }
      }
    }

    return TF.tupleType(
        types.toArray(new Type[types.size()]), labels.toArray(new String[labels.size()]));
  }
  public static IList domain(IList rel1) {
    Type lrelType = rel1.getType();
    IListWriter w = List.createListWriter(lrelType.getFieldType(0));
    HashSet<IValue> cache = new HashSet<>();

    for (IValue elem : rel1) {
      ITuple tuple = (ITuple) elem;
      IValue e = tuple.get(0);
      if (!cache.contains(e)) {
        cache.add(e);
        w.append(e);
      }
    }
    return w.done();
  }
  public static IList carrier(IList rel1) {
    Type newType = rel1.getType().carrier();
    IListWriter w = List.createListWriter(newType.getElementType());
    HashSet<IValue> cache = new HashSet<>();

    for (IValue v : rel1) {
      ITuple t = (ITuple) v;
      for (IValue e : t) {
        if (!cache.contains(e)) {
          cache.add(e);
          w.append(e);
        }
      }
    }

    return w.done();
  }
Example #7
0
  private void printResult(IRascalResult result) throws IOException {
    if (result == null) {
      return;
    }
    PrintWriter out = getOutputWriter();
    IValue value = result.getValue();
    if (value == null) {
      out.println("ok");
      out.flush();
      return;
    }
    Type type = result.getType();

    if (type.isAbstractData() && type.isStrictSubtypeOf(RascalValueFactory.Tree)) {
      out.print(type.toString());
      out.print(": ");
      // we unparse the tree
      out.print("(" + type.toString() + ") `");
      TreeAdapter.yield((IConstructor) result.getValue(), true, out);
      out.print("`");
    } else {
      out.print(type.toString());
      out.print(": ");
      // limit both the lines and the characters
      try (Writer wrt = new LimitedWriter(new LimitedLineWriter(out, LINE_LIMIT), CHAR_LIMIT)) {
        indentedPrettyPrinter.write(value, wrt);
      } catch (IOLimitReachedException e) {
        // ignore since this is what we wanted
      }
    }
    out.println();
    out.flush();
  }
  public static IList closure(IList rel1) {
    Type resultType = rel1.getType().closure(); // will throw exception if not binary and reflexive
    IList tmp = rel1;

    int prevCount = 0;

    ShareableValuesHashSet addedTuples = new ShareableValuesHashSet();
    while (prevCount != tmp.length()) {
      prevCount = tmp.length();
      IList tcomp = compose(tmp, tmp);
      IListWriter w = List.createListWriter(resultType.getElementType());
      for (IValue t1 : tcomp) {
        if (!tmp.contains(t1)) {
          if (!addedTuples.contains(t1)) {
            addedTuples.add(t1);
            w.append(t1);
          }
        }
      }
      tmp = tmp.concat(w.done());
      addedTuples.clear();
    }
    return tmp;
  }
Example #9
0
 public TuplePattern(Type tupleType, Pattern<IValue, Type>[] children) {
   if (tupleType != null && !tupleType.isList()) {
     throw new IllegalArgumentException("Type should be tuple");
   }
   this.type = tupleType;
   if (children != null) {
     this.children = children.clone();
     boolean tmp = true;
     for (Pattern<IValue, Type> c : children) {
       tmp &= c.subTreeOnly();
     }
     subTreeOnly = tmp;
   } else {
     this.children = null;
     subTreeOnly = true;
   }
 }
Example #10
0
 public boolean isSubtypeOf(Type t1, Type t2) {
   return t1.isSubtypeOf(t2);
   //		Type[] key = new Type[] { t1, t2};
   //
   //		return subtypeCache.get(key, k -> t1.isSubtypeOf(t2));
 }
    private boolean indented(Type type) {
      return type.accept(
          new ITypeVisitor<Boolean, RuntimeException>() {
            @Override
            public Boolean visitReal(Type type) {
              return false;
            }

            @Override
            public Boolean visitInteger(Type type) {
              return false;
            }

            @Override
            public Boolean visitRational(Type type) {
              return false;
            }

            @Override
            public Boolean visitList(Type type) {
              return true;
            }

            @Override
            public Boolean visitMap(Type type) {
              return true;
            }

            @Override
            public Boolean visitNumber(Type type) {
              return false;
            }

            @Override
            public Boolean visitAlias(Type type) {
              return type.getAliased().accept(this);
            }

            @Override
            public Boolean visitSet(Type type) {
              return true;
            }

            @Override
            public Boolean visitSourceLocation(Type type) {
              return true;
            }

            @Override
            public Boolean visitString(Type type) {
              return false;
            }

            @Override
            public Boolean visitNode(Type type) {
              return true;
            }

            @Override
            public Boolean visitConstructor(Type type) {
              return true;
            }

            @Override
            public Boolean visitAbstractData(Type type) {
              return true;
            }

            @Override
            public Boolean visitTuple(Type type) {
              return true;
            }

            @Override
            public Boolean visitValue(Type type) {
              return false;
            }

            @Override
            public Boolean visitVoid(Type type) {
              return false;
            }

            @Override
            public Boolean visitBool(Type type) {
              return false;
            }

            @Override
            public Boolean visitParameter(Type type) {
              return type.getBound().accept(this);
            }

            @Override
            public Boolean visitExternal(Type type) {
              return false;
            }

            @Override
            public Boolean visitDateTime(Type type) {
              return false;
            }
          });
    }
Example #12
0
 @Override
 protected Type glbWithAbstractData(Type type) {
   return type.equivalent(RascalValueFactory.Tree) ? this : TF.voidType();
 }
Example #13
0
 @Override
 protected Type lubWithConstructor(Type type) {
   return type.getAbstractDataType().equivalent(RascalValueFactory.Tree)
       ? RascalValueFactory.Tree
       : TF.nodeType();
 }
Example #14
0
 @Override
 protected Type lubWithAbstractData(Type type) {
   return type.equivalent(RascalValueFactory.Tree) ? type : TF.nodeType();
 }
Example #15
0
 @Override
 protected boolean isSubtypeOfAbstractData(Type type) {
   return type.equivalent(RascalValueFactory.Tree);
 }