Skip to content
  • REL
  • REL CONCEPTS
  • Modules

Modules

This concept guide introduces the basic syntax for modules in Rel through simple and more advanced examples.

Introduction

Modules in Rel provide a way to organize and structure your data modeling, offering some distinct advantages:

  • Related definitions can be grouped together.
  • Models and database logic can be parameterized and reused.
  • Names can be local to each module, which prevents clashes and keeps the top-level namespace clean.
  • Relations can be renamed before they are used.

Simple Example

The basic module syntax groups relations under a common namespace. For example:

// install
 
module store
    def office = {"Boston"; "New York"; "Los Angeles"; "Chicago"}
    def product = {(1, "Laptops"); (2, "Desktops"); (3, "Phones")}
end

A module is a relation that can be thought of as containing nested relations.

// query
 
store

In the example above, office and product are nested inside store, and available as store[:office] and store[:product]:

// query
 
store[:office]
// query
 
store[:product][1]

Instead of writing store[:office], you can write store:office. Instead of store[:product][1], you can write store:product[1]. The shorter versions are preferred:

// query
 
def R = equal(store[:office], store:office)
def output = R
 

The syntax for module declarations is just a convenient notation. The example above could also be written as:

def store:office = {"Boston"; "New York"; "Los Angeles"; "Chicago"}
def store:product = {(1, "Laptops"); (2, "Desktops"); (3, "Phones")}

You could write it even more directly:

def store = :office, {"Boston"; "New York"; "Los Angeles"; "Chicago"}
def store = :product, {(1, "Laptops"); (2, "Desktops"); (3, "Phones")}

In the interest of readability, it is recommended that you use the module syntax, rather than any alternatives.

Importing Module Names: with

You can choose to import some of the definitions of a module into other namespaces and optionally rename them, using statements of the form with <module> use <relation> [as <newname>]. For example:

// query
 
with store use office
def output = count[office]

This example shows how you can rename relations when you import:

// query
 
with store use product as p
def output = p[1]

Using Modules Inside Other Modules

You can use existing module definitions when defining new modules. For example:

// query
 
module summary
    with store use office
    def office_count = count[office]
    def product = store:product
end
 
def output = summary

Note that while store:office was used to define office_count, it did not become part of summary. By contrast, store:product is part of summary, because of the definition def product = store:product.

Variable Scope

When variable names clash, the innermost scope takes precedence. For example:

// query
 
def a = 10
 
module m
    def a = 20
    def b = a + 5
end
 
def output = m

Imports can be shadowed by a new definition that follows them, which will take precedence. In this case, a warning is printed:

// query
 
module m1
     def a = 1
end
 
with m1 use a
 
def a = 5
 
def output = a

The warning states: “‘a’ is imported but not used because there is a declaration of the same name in the same scope.”

If the def a = ... comes before the with, the import takes precedence in the code that follows it.

// query
 
module m1
     def a = 1
end
def a = 5
 
with m1 use a
 
def output = a

Try to avoid name clashes in the same scope. It is preferred to put with statements earlier in the code, but later definitions can shadow them. There will be an error message when this happens.

Nested Modules

Modules can be nested. In other words, modules can be defined within modules. This is especially useful when data are fully normalized into Graph Normal Form:

// query
 
module person
    def ssn = 123-45-6789
    module name
        def first = "John"
        def middle = "Q"
        def last = "Public"
    end
    module birth
        def city = "Pittsburg"
        def state = "PA"
        def country = "USA"
        def date = parse_date["2000-01-01", "Y-m-d"]
    end
end
 
def output = person:birth:city

Multiple Definitions

Like all relations in Rel, modules can be made up of a number of separate definitions:

// query
 
module m2
    def a = 1
end
 
module m2
    def b = 1
end
 
def output = m2

With this feature, you can, for example:

  • Define large or complex features of a given module separately, even in different installed sources.
  • Extend an existing module for the purposes of a particular query, without persisting the change.

You can also take the union of two separate modules. For example:

// query
 
module A
    def a = 1
end
 
module B
    def a = 1
    def b = 1
end
 
def AB = A ; B
 
def output = AB

This lets you extend a given module with functionality defined in another.

Integrity Constraints

Currently, basic integrity constraints are supported in non-parameterized modules. A simple example is shown below:

// query
 
module mymodule
    def R = {1; 2}
    ic {count[R] = 2}
end
 
def output = mymodule

Parameterized Modules

Modules can be made more reusable by parameterizing them. For example, you can build a simple module that collects a few statistics for a relation R:

// install
 
@outline
module my_stats[R]
    def my_minmax = (min[R], max[R])
    def my_mean = mean[R]
    def my_median = median[R]
end
// query
 
def output = my_stats[{1; 2; 3; 5; 8; 13; 100}]

The module parameter R can be instantiated with any relation that has a numeric range. For example:

// query
 
def pop = {("a", 10); ("b", 100); ("c", 20); ("d", 23)}
def output = my_stats[pop]

In a paremeterized module each parameter represents a relation. The relation may be a singleton, in which case the argument used in instantiating the module may be a single data value or an individual variable. If a parameter is intended to represent a relation that is not a singleton, then its name must begin with an upper-case letter.

The definition of a parameterized module whose parameters are not singletons should be preceded by one of two annotations: @outline or @inline. The recommended annotation is @outline. Unlike @inline, it allows the module to contain recursive definitions. The recursive definitions need not be annotated with additional occurrences of @outline.

Just like in definitions of relations, the name of a parameter intended to represent a relation that is not a singleton must begin with a capital letter: R in the example above.

The following example shows a module that is intended to be parameterized with a singleton relation, i.e., a single data value or an individual variable. The name of the parameter may begin with a lower-case letter, and an annotation is not necessary:

// query
 
module mymodule[x]
    def multiply[y] = y * x
    def divide[y] = y / x
end
 
def m = mymodule[10]
 
with mymodule[20] use divide as divides_twenty
def output:first = m:divide[240]
def output:second = divides_twenty[120]
 

The following is a simple example of a module that features a recursive relation, transitive. Notice that there is no need to annotate the definition of transitive with @outline:

// query
 
// Define the common extensions of a binary relation
@outline
module extensions[R]
  def id = R
  def symmetric = R ; transpose[R]
 
  def reflexive = x, y : (first[R]; second[R])(x) and x = y
  def reflexive = R
 
  def transitive = R
  def transitive = R . transitive
 
  def closure = reflexive ; transitive
end
 
def extended = extensions[{("a", "A"); ("b", "B"); ("B", "b")}]
 
def output:id = extended:id
def output:trans = extended:transitive

Multiple Parameters

Modules can have any number of parameters representing singleton or general relations. A module definition with four parameters can be written as:

module M[A, B, x, y]
    // ....
end

The higher-order parameters A and B are for general relations. The first-order parameters x and y are for singleton relations.

🔎

The higher-order parameters don’t have to appear before the first-order parameters but this ordering is recommended. It also reduces the risk of arity ambiguities.

In the example below, csv_column_filter[Data, Column] is defined as a parameterized module that acts as a column filter operation over the CSV data Data.

// query
 
// define module that filters columns
@outline
module csv_column_filter[Data, Column]
    def result = Column <: Data
end
 
// toy data defined in a string
def config:data = """
name,country,city
Liam,USA,"Portland, ME"
Olivia,Canada,Toronto
Noah,Mexico,Yucatan
Emma,USA,"Washington, D.C."
"""
 
// load the string as csv data
def my_csv = load_csv[config]
 
// filter columns and display the result as a csv table
def output = table[
    csv_column_filter[my_csv, {:name; :city}][:result]
]

The module csv_column_filter is invoked for the CSV data my_csv, which has been created with load_csv, and is filtered on the columns “name” and “city” using the prefix join operator, <:. The filtered data are assigned to the relation output within the module csv_column_filter.

See the declaration of BipartiteGraph in section Example: Graph Modules for an example of parameterized modules in the context of graphs.

Defining Templates

Thanks to the fact that modules can have multiple parameters, they can also be used to define templates for tasks such as transformations. This means entire classes of transformations can be defined with parameterized modules. Custom names can then be assigned to specific transformations within this class, allowing users to build a complex transformation in a highly modularized way.

In the example below, the module csv_filter acts as a template for column and row filtering of data in GNF.

// query
 
// define a template for filtering data in the GNF format
@outline
module csv_filter[Data, Column, n_row]
    def top(c, r, v) = Data(c, r, v) and Column(c) and r <= n_row
    def bottom(c, r, v) = Data(c, r, v) and Column(c) and r > n_row
end
 
// custom filter with concrete filtering parameters
@outline
def my_filter[Data] = csv_filter[Data, {:name; :city}, 2]
 
// toy data
module my_csv
    def name {(1, "Liam"); (2, "Olivia"); (3, "Noah"); (4, "Emma")}
    def country {(1, "USA"); (2, "Canada"); (3, "Mexico"); (4, "USA")}
    def city {(1, "Portland, ME"); (2, "Toronto"); (3, "Yucatan"); (4, "Washignton, D.C.")}
end
 
// apply custom filter
def my_csv_filtered = my_filter[my_csv][:top]
 
// display result
def output = table[my_csv_filtered]

The relations top and bottom within csv_filter select (drop) the first n_row rows in the CSV data. They are designed — in contrast to top and bottom in the stdlib — to filter data in GNF format.

The relation my_filter is defined to be a specific custom filter based on the csv_filter template. It selects or drops two rows, and only from the columns “name” and “city”.

Once my_filter is defined — within the query or installed in the database — it can be applied to any relation, like my_filter[my_csv], without the need to specify the filter parameters again. The argument :top selects that the relation top within csv_filter should be used. Without it, both filters (top and bottom) would be applied to my_csv.

For the final output, only the first two rows are requested, because my_csv_filtered:top selects only the top filter within my_filter (and consequently within csv_filter).

Example: Graph Modules

Since modules are relations themselves, they can be used to parameterize other modules.

Consider a module that constructs a complete graph for a set of nodes N; that is, a graph where each node is connected to all other nodes:

// install
 
@outline
module CompleteGraph[N]
    def node = N
    def edge = N,N
end

Two graphs are defined in the example below. The first is a bipartite graph, with a set of nodes, M, each of which is connected to all nodes from the set N. The second is a cycle graph, with a set of nodes, N. Each node in N is connected to one “next” node, forming a loop:

// install
 
@outline
module BipartiteGraph[M, N]
    def node = M; N
    def edge = M, N
end
 
@outline
module CycleGraph[N]
    def node = N
    def edge(a in N, b in N) =
            sort[N](x, a)
        and sort[N](y, b)
        and y = x % count[N] + 1
        from x, y
end

The module GraphProperties defined below computes some basic properties of a graph module G:

// install
 
@outline
module GraphProperties[G]
    def outdegree[v in G:node] = count[v1 : G:edge(v, v1)] <++ 0
    def indegree[v in G:node] = count[v1 : G:edge(v1, v)] <++ 0
    def edge_count = count[G:edge] <++ 0
end

The following code instantiates the BipartiteGraph module and passes it as a parameter to the GraphProperties module to compute its properties:

// query
 
def graph = BipartiteGraph[{"l1"; "l2"}, {"r1"; "r2"; "r3"}]
 
def output = GraphProperties[graph]
 
 

Here are more examples showing how you can use these definitions:

// query
 
def cg = CompleteGraph[range[1, 5, 1]]
def cg_props = GraphProperties[cg]
 
def bg = BipartiteGraph[{1; 2}, {3; 4; 5}]
def bg_props = GraphProperties[bg]
 
def cycleg = CycleGraph[{"a"; "b"; "c"; "d" ; "e"}]
def cycleg_props = GraphProperties[cycleg]
 
module output
    def complete_edge_count = cg_props:edge_count
    def bipartite_edge_count = bg_props:edge_count
    def cycle_edge_count = cycleg_props:edge_count
end
 

Types Defined in Modules

A module can contain definitions of entity types and value types.

Here is a very simple example:

// query
 
module m
  entity type Country = String
end
 
def scandinavia(x, c) {
    c = m:^Country[x] and {"Sweden"; "Denmark"; "Norway"}(x)
}
def output = scandinavia

See Value Types in Modules for more information.

Imported CSV Files and JSON Documents Are Modules

The result of importing CSV and JSON data can be viewed as a module.

CSV Data as Modules

Loading a CSV file into a relation csv_data gives a module of the form:

module csv_data
   def column_one[pos] = ...
   def column_two[pos] = ...
   ...
end

The relation csv_data:column_one maps positions to values for the first column in the CSV file, the same for csv_data:column_two and the second column and so on. See the CSV Import Guide for further details. For example:

// query
 
def config:data = """
order_id,customer,price
1,92819,3.4
3,78271,1.6
"""
 
def csv_data = load_csv[config]
def output = csv_data

This relation is equivalent to the module:

module order_csv[pos]
    def order_id = ...
    def customer = ...
    def total_price = ...
end

This in turn is equivalent to:

def order_csv[pos, :order_id] = ...
def order_csv[pos, :customer] = ...
def order_csv[pos, :total_price] = ...

JSON Data as Modules

For JSON data, the input JSON structure is naturally reflected in the resulting Rel module structure, with nested JSON structures becoming nested modules, recursively. JSON names become Rel symbols, and values become scalars (single values) or nested modules.

For example:

// query
 
def config:data = """
{
  "name": "John",
  "address": {"state": "WA", "city": "Seattle"},
   "age": 21
}"""
 
def person = load_json[config]
def output = person

Summary

In summary, modules in Rel allow you to organize and model your data, and they also provide certain distinct advantages. Some of these advantages include grouping relations with common definitions or using existing definitions to define new modules. Moreover, modules can be parameterized and used to construct complete graphs. Lastly, imported CSV and JSON data can also be viewed as Rel modules.

Was this doc helpful?