# An Invitation to SETL

Mathematicians, rejoice. The SETL programming language, almost as old as C, is closely modeled on set theory and looks like a version of Perl with a mathematical flavour.
Quantifiers

The implementation of sets as elementary SETL data types would not be so effective without the quantifiers exists and forall that, for example, allow subsets to be built in a compact way because each of them implicitly involves a loop. Needless to say, quantifiers syntax is the one usually used in mathematics:

```A:={3,5,7};
print( exists x in A | x mod 2 = 0 );
print( forall x in A | x >= 2 );

#F
#T
```

For example, quantifiers can be used to define sets in a compact way:

```N:={1..50};
P:={x: x in N | forall y in {2..x-1} | x mod y /=0};
print(P);

{1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47}
```

The P set defined above in one line contains all integers smaller than 50 that cannot be divided exactly by any other integer number except for 1 and itself, namely all prime numbers smaller than 50.

Tuples

The Cartesian Product of sets, of course, could not be left out from SETL. Given two or more sets, their Cartesian Product is the collection of all ordered couples (or n-tuples) of elements from the two or more sets. In formulas this is expressed as:

In SETL, the notation for the elements of the Cartesian Product—implemented through the data type array in other languages, such as C or Perl—uses squared rather than the round parentheses usually used in mathematics. SETL uses the round parenthesis to access single elements of a tuple, so here it is exactly the opposite of Perl:

```t:=[2,8,3.4,'hello world',[2,3],{4,5}];
print(t(4));

hello world
```

As can be seen in the example above, the first index is 1 rather than 0. Tuples can contain not only scalar data types but also sets or other tuples, making it an extremely flexible tool.

New elements are added simply by defining the right value of the right array element or by using the with instruction to append a value as the last element of the array. Operators fromb and frome are used to get rid of a single element, from the beginning and the end of an array, respectively. Finally, the operator + is used to append the second tuple to the first, and the operator # returns the index of the last element of the tuple.

```a:=[3,9,4.2];
a:= a with 'abc';
print(a,#a);
a(8):=1;
print(a,#a);
b frome a;
print(a,#a);

[3 9 4.2 abc]
[3 9 4.2 abc * * * 1]
[3 9 4.2 abc]
```

The simple fact of being ordered makes tuples and sets quite different objects. For example, the sum (union) of two equal sets produces the same set, while the sum (concatenation) of two equal tuples produces a longer tuple, that is a tuple belonging to a different Cartesian Product:

```print({1}+{1});
print([1]+[1]);

{1}
[1 1]
```

The whole Cartesian Product of two sets can be generated easily with a simple Set Former expression:

```A:={1,2};
B:={'a','b','c'};
C:={[x,y]:x in A, y in B};
print(C);

{[1 a] [1 b] [1 c] [2 a] [2 b] [2 c]}
```

Maps

Last but not least, SETL comes with natural support for maps.

In mathematics it is called map when between two sets (A and B), any rule f that associates to each element a of A and element b of B. The element b also is called value of the map f at the point a; in short: b = f(a). Any such map f can be described unequivocally by a subset F of the Cartesian product A×B if we agree to put in F all pairs (a,b) where b=f(a).

SETL automatically enables special calls for every subset of a Cartesian Product of two sets, which are sets containing only pairs. It allows you to access such a set with the same notation used in mathematics as in the following example:

```sqroot:={[1,1],[4,2],[9,3],[16,4]};
print(sqroot(9));

3
```

Each map f from A to B has a domain, namely the set of all elements of A on which it is defined (like the keys of a Perl hash), and a co-domain, namely the set of all of the elements of B it is able to reach (the values of a Perl hash). Because these sets are crucial for working with maps, SETL implements two calls, domain and range, to return those two sets.

```sqroot:={[1,1],[4,2],[9,3],[16,4]};
print(domain(sqroot));
print(range(sqroot));

{1 4 9 16}
{1 2 3 4}
```

Finally, SETL is aware of the concept of multivalued map, those maps f from A to B that associate to some element a more than one element of B. In this case the function must be called using the set brackets:

```f:={[1,1],[1,2],[1,3],[16,4]}; print(f{1}); print(f(1));

{1 2 3} *
```

______________________

## Comment viewing options

### SETL-like sources

Of course, if someone has an URL to the original SETL's sources ... please, share.

### Roberto De Leo wrotes: | ...

Roberto De Leo wrotes:
| ...
| open-source programming language that implements as closely as
~~~~~~~~~~~
| possible the Set Theory formal language. The language is called SETL
| (Set Language).
| ...
Is it only available via email according to the comments from ftp://cs.nyu.edu/pub/languages/setl2/README.SOURCES ?