Navigation
Synopsis Relation values.
Syntax { <Exp11, Exp12, ... > , <Exp21, Exp22, ... > , ... }
Types
Exp11 Exp12 ... { <Exp11, Exp12, ... > , ... }
T1 T2 ... rel[T1, T2, ... ]
Usage import Relation; (included in Prelude)
Description A relation is a set of elements with the following property:
  • All elements have the same static tuple type.
Relations are thus nothing more than sets of tuples, but since they are used so often we provide a shorthand notation for them. Relations are represented by the type rel[T1 L1, T2 L2, ... ], where T1, T2, ... are arbitrary types and L1, L2, ... are optional labels. It is a shorthand for set[tuple[T1 L1, T2 L2, ... ]].

An n-ary relations with m tuples is denoted by {<E11, E12, ..., E1n>,<E21, E22, ..., E2n>, ..., <Em1, Em2, ..., Emn>}, where the Eij are expressions that yield the desired element type Ti.

Since relations are a form of set all operations (see Set) and functions (see Prelude/Set) are also applicable to relations.

The following additional operators are provided for relations: The following additional functions are provided on relations:
  • carrier: Return the set of all elements in any tuple in a relation.
  • carrierR: A relation restricted to certain element values in tuples.
  • carrierX: A relation excluding tuples that contain certain element values.
  • complement: Complement of a relation.
  • domain: Domain of a relation: a set consisting of the first element of each tuple.
  • domainR: Relation restricted to certain domain elements.
  • domainX: Relation excluding certain domain values.
  • groupDomainByRange: Make sets of elements in the domain that relate to the same element in the range.
  • groupRangeByDomain: Make sets of elements in the range that relate to the same element in the domain.
  • ident: The identity relation.
  • index: Indexes a binary relation as a map
  • invert: Invert the tuples in a relation.
  • range: The range (i.e., all but the first element of each tuple) of a relation.
  • rangeR: Relation restricted to certain range values.
  • rangeX: Relation excluding certain range values.
Examples
rascal>{<1,10>, <2,20>, <3,30>}
rel[int,int]: {
  <3,30>,
  <2,20>,
  <1,10>
}
instead of rel[int,int] we can also give set[tuple[int,int]] as type of the above expression remember that these types are interchangeable.
rascal>{<"a",10>, <"b",20>, <"c",30>}
rel[str,int]: {
  <"c",30>,
  <"b",20>,
  <"a",10>
}
rascal>{<"a", 1, "b">, <"c", 2, "d">}
rel[str,int,str]: {
  <"a",1,"b">,
  <"c",2,"d">
}

Questions
Question [1]. A relation:






Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.