Search code examples
rascal

Which alternative is that instance of a ADT?


I have defined an AlgebraicDataType and an instance of it:

data Expr = const(int n)
          | var(str name)
          | add(Expr lhs, Expr rhs)
          | mult(Expr lhs, Expr rhs)
          ;

I need a boolean indicating if a given instance of it is of type add:

isAdd(add(var("x"), const(3)));  // true
isAdd(var("x"));                 // false
isAdd(const(3));                 // false
isAdd(mult(var("x"), const(3))); //false

Maybe it exists a built-in function or operator, which would be a more generic solution than :

bool isAdd(add(Expr lhs, Expr rhs)) = true;
bool isAdd(Expr e) = false;

Solution

  • There is a built-in operator is indeed: isAdd(expr) = expr is add.