Beispiel #1
0
  public Expression substitute(String var, Expression replacement) {
    Vector<Expression> new_operands = new Vector<Expression>();
    for (Expression o : operands) {
      new_operands.add(o.substitute(var, replacement));
    }

    return new Application(operator.substitute(var, replacement), new_operands);
  }
 @Override
 public void substitute(Expression oldExp, Expression newExp) {
   if (exp.equals(oldExp)) {
     exp = newExp;
   } else {
     exp.substitute(oldExp, newExp);
   }
 }
Beispiel #3
0
  public Expression oneStep() {
    if (operator.reducible()) {
      return new Application(operator.oneStep(), operands);
    } else if (isOperandsReducible()) {
      return new Application(operator, reduceOperandsByOneStep());
    } else if (allOperandsAreValues()) {
      if (operator instanceof RecFun) {
        RecFun operator = ((RecFun) this.operator);
        Expression body = operator.body;

        // Substitute occurrences of function name with the function
        body = body.substitute(operator.funVar, operator);

        // When we are here, we are sure that the form is
        // (fun ... end Op1 ... OpN)
        Vector<String> formals = operator.formals;

        for (int i = 0; i < formals.size(); i++) {
          body = body.substitute(formals.get(i), operands.get(i));
        }

        return body;
      } else if (operator instanceof Fun) {
        Fun operator = ((Fun) this.operator);
        Expression body = operator.body;

        // When we are here, we are sure that the form is
        // (fun ... end Op1 ... OpN)
        Vector<String> formals = operator.formals;

        for (int i = 0; i < formals.size(); i++) {
          body = body.substitute(formals.get(i), operands.get(i));
        }

        return body;
      } else {
        return this;
      }
    }
    return this;
  }
  /**
   * Performs the substitution for <b>(LAMBDA)</b> expressions.
   *
   * @param id the identifier for the substitution.
   * @param e the expression to substitute.
   * @return the new expression.
   */
  @Override
  public Expression substitute(String id, Expression e) {
    if (this.id.equals(id)) {
      return this;
    } else {
      // determine the free identifiers for e
      Set<String> free = e.free();

      // generate a new unique identifier
      String newId = this.id;
      while (free.contains(newId)) newId = newId + "'";

      // perform the bound renaming
      Expression newE = this.e.substitute(this.id, new Identifier(newId));

      // perform the substitution
      return new Lambda(newId, this.tau, newE.substitute(id, e));
    }
  }
  @Override
  public void substitute(Expression oldExp, Expression newExp) {
    if (target != null && target.equals(oldExp)) {
      target = newExp;
    }

    if (target != null) {
      target.substitute(oldExp, newExp);
    }

    for (int i = 0; i < arguments.size(); i++) {
      Expression e = arguments.get(i);

      if (e.equals(oldExp)) {
        arguments.set(i, newExp);
      }

      arguments.get(i).substitute(oldExp, newExp);
    }
  }