Syntax

Kind

Constructing

Set Literals

Set Comprehensions

From Matrices

Accessing Elements

Membership

Assigning Elements

Operations

Union

Intersection

Difference

Subset and Superset

Proper Subset and Superset

set

A set is an unordered collection of unique values. Sets are useful for representing groups of items where duplicates are not allowed, and for performing mathematical set operations like union, intersection, and difference.


Operator

Function

Description

union

Computes the union of two sets.

intersection

Computes the intersection of two sets.

difference

Computes the difference between two sets.

subset

Determines if one set is a subset of another.

proper-subset

Determines if one set is a proper subset of another.

superset

Determines if one set is a superset of another.

proper-superset

Determines if one set is a proper superset of another.

element-of

Checks if an element is a member of a set.

not-element-of

Checks if an element is not a member of a set.

symmetric-difference

Computes the symdifference between two sets.

Syntax

A set is declared using curly braces {}.

Elements are separated by commas ,. Duplicate elements are automatically removed.

{}                 -- An empty set
{1, 2, 3}          -- A set of numbers
{"a", "b", "c"}    -- A set of strings
{1, 1, 2, 3}       -- Duplicates are removed

When parsed and rendered, this code will appear as:

{}--

An empty set

{1, 2, 3}--

A set of numbers

{"a", "b", "c"}--

A set of strings

{1, 1, 2, 3}--

Duplicates are removed

Sets are unordered. The order in which elements appear does not affect equality or behavior.

Kind

In general, a set has the kind:

<{T}:N>

Where T is the kind of the elements, and N is the number of elements. The number of elements can be omitted to compare sets of different sizes, but strictly speaking, two sets of different sizes have different kinds.

For example:

A:={1, 2, 3}--

kind: <{f64}:3>

B:={4, 5}--

kind: <{f64}:2>

Other examples include:

{1, 2, 3}--

kind: <{f64}:3>

{"a", "b"}--

kind: <{string}:2>

{(1,2), (3,4)}--

kind: <{(f64,f64)}:2>

Constructing

There are three ways to create sets:

  • Set Literals

  • Set Comprehensions

  • From Matrices

Set Literals

An empty set is written as {}, and has kind {<{_}>}:

Non-empty sets are written with elements inside the braces, separated by commas:

{1, 2, 3}

Set Comprehensions

Sets can be constructed using set comprehensions, which generate elements based on patterns and predicates.

Comprehensions may contain:

  • One or more generators (<-)

  • Boolean conditions

  • Let bindings

  • Arbitrary expressions for the yielded element

For example, the following comprehension generates the set of friends-of-friends for a given user, excluding those with an ID of 9 or higher:

pairs:={(1,2), (1,3), (2,8), (3,5), (3,9)}user:=1{fof | ( u, f ) <- pairs, ( f, fof ) <- pairs, uuser, fof<9}

For more, see set/comprehensions

From Matrices

You can create a set from a matrix using a kind annotation, as long as the kinds are compatible. For example, if you have a matrix of f64 values, you can create a set of kind f64:

x := [1.1 2.2; 3.3 4.4]
s<{f64}> := x

Accessing Elements

Because sets are unordered collections, you cannot access elements by index or position like you would with lists or arrays.

Instead, elements are accessed through:

  • Membership tests -- checking if an element is in the set or not

  • Set operations -- union, intersection, difference, etc.

  • Iteration (via comprehensions) -- to process or transform elements one by one, but not in any specific order

Membership

Use to test membership:

A:={1, 2, 3}2A

For more, see set/element-of

4A

Use for non-membership:

4A

For more, see set/not-element-of

Assigning Elements

Sets are immutable, meaning that once they are created you cannot add or remove elements from them.

Instead, new sets are created using set operations. To add elements to a set, use the union operator to combine the existing set with a new element:

A:={1, 2, 3}A2:=A{4}--

Creates a new set

To remove elements from a set, use the difference operator :

A:={1, 2, 3}A2:=A{2}--

Creates a new set without 2

Operations

Mech supports standard mathematical set operations.

Union

The union of two sets contains all elements that appear in either set.

A:={1, 2, 3}B:={2, 3, 4}AB

For more, see set/union

Intersection

The intersection contains elements common to both sets.

A:={1, 2, 3}B:={2, 3, 4}AB

For more, see set/intersection

Difference

The difference A ∖ B contains elements in A that are not in B.

A:={"a", "b", "c"}B:={"b", "c", "d"}AB

For more, see set/difference

Subset and Superset

The subset operator tests if all elements of one set are contained in another:

A:={"b", "c"}B:={"b", "c", "d"}AB

For more, see set/subset

Similarly, the superset operator tests if a set contains all elements of another:

BA

For more, see set/superset

Proper Subset and Superset

A:={1, 2}B:={1, 2, 3}AB

For more, see set/proper-subset

Likewise, the proper superset operator tests if a set strictly contains another:

BA

For more, see set/proper-superset

If the sets are equal, strict relations are false:

A:={1, 2}B:={1, 2}AB

Also:

AB