0

As simple as it seems, how do I initialize an empty relation (of type say, [str,int]), then add new elements to it? An approximate Rascal code:

rel[str,int] myReln={};

myReln.add(<"a",1>);

Samples for remove/modify operations would also be appreciated.

apil.tamang
  • 2,545
  • 7
  • 29
  • 40
  • It looks as though you are assuming data is mutable, like in OO programming. This is not the case in Rascal. `myReln` points to the empty set `{}` which you can not change. What you _can_ do is make a new set and let `myReln' point to that, as Mark explains in his answer. Hope this helps! Thanks for asking. – Jurgen Vinju Nov 05 '14 at 20:10

2 Answers2

1

Relations are just a special form of sets, so all the operations available on sets are also available on relations. Your add operation would actually be

myReln += < "a", 1 >;

or

myReln = myReln + < "a", 1 >;

Removing items from a relation it similar to adding items to a relation. If you wanted to remove the tuple < "a", 1> from myReln, you would just write

myReln = myReln - < "a", 1 >;

or, as a shorthand

myReln -= < "a", 1 >;

If you don't know the entire tuple, but know you want to remove any tuple that starts with "a", you have (at least) two choices. The easiest is to use the domainX function in the Relation library:

import Relation;
myReln = domainX(myReln, {"a"});

This will remove any tuples that have "a" as their first element. You could also have more than one item in this set, so if you want to remove any tuples starting with either "a" or "b" you could say:

myReln = domainX(myReln, {"a","b"});

The other option is to use pattern matching and comprehensions -- basically, to rebuild the relation by looking at each item in the relation and deciding if you should keep it. That would look like:

myReln = { <a,b> | <a,b> <- myReln, a != "a" };

Modification is then just some series of additions and deletions. Since the relations are immutable, we don't have a concept of in-place modification.

You can find documentation on Rascal sets here: http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Expressions/Values/Set/Set.html. Special operations defined just for relations (again, sets of tuples) is here: http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Expressions/Values/Relation/Relation.html

Mark Hills
  • 1,028
  • 5
  • 4
1

It is important to observe two principles:

  • values are always immutable, i.e. once created they cannot be changed.
  • to variables you can assign different values over time.

In your example:

  • rel[str,int] myReln={};, fine variable myRel now has value {}.
  • Adding a new value to a set or relation is done with the + operator. In your case, {} + <"a", 1>`.
  • To assign this new value to myRel, you can write: myRel = myReln + <"a", 1>.
  • A shorthand for this is myReln += <"a", 1>.

Deletion can be done using (in this example) set difference (-) or by decomposing the set value in parts using pattern matching and then constructing a new set.

Paul Klint
  • 1,418
  • 7
  • 12
  • Just to make the comment about immutability more concrete, this means that, if you assign `myReln = {}`, then assign `myReln2 = myReln`, changes to `myReln` will not be reflected in `myReln2`; adding a new tuple to a relation essentially creates a new relation, versus altering the existing relation in place. – Mark Hills Nov 05 '14 at 14:17