Tutorial: Repetitions


 

Repetitions are really not that interesting. They are just the concept in BETA which corresponds to arrays in other languages, defining several entities in one go in stead of just one.

Nevertheless, repetitions are one of the very few topics where gbeta has been designed to do something which is not backward compatible with BETA. This is because I thought the semantics was messy in BETA..

The basic idea is that a repetition implies "repeated." Whatever you can do with a single item of any kind should have a "repeated" parallel when applied to a repetition. Assignments can be described in the following way. Assume the following declarations:

(#
   rep1,rep2: [expr] @ptn;
   refrep: [expr] ^ptn
do 
   ...
#)

Firstly, the repetition being assigned to (on the right hand side of "->") gets adjusted to have the same length as the one being evaluated. Secondly, assignments distribute over the elements, i.e. assigning one repetition to another means assigning the first element of the repetition to the first element of the second, then the second etc. Thirdly, the object reference marker "[]" distributes over the elements. Summing up we get the following equivalences:

rep1->rep2

works like:

(* adjust size of 'rep2' *)
(for i:rep1.length repeat rep1[i]->rep2[i] for)

rep1[]->rep2

works like:

(* adjust size of 'rep2' *)
(for i:rep1.length repeat rep1[i][]->rep2[i] for)

rep1->rep2[]

works like:

(* adjust size of 'rep2' *)
(for i:rep1.length repeat rep1[i]->rep2[i][] for)

and, finally,

rep1[]->rep2[]

works like:

(* adjust size of 'rep2' *)
(for i:rep1.length repeat rep1[i][]->rep2[i][] for)

It is possible to extract a subrange of a repetition using a repetition slice,

<AttributeDenotation> "[" <Evaluation> ":" <Evaluation> "]"

For example "rep1[2:2*b]" evaluates to a repetition containing the elements from rep1 starting with rep1[2] and ending with rep1[2*b]. Note that there are some known bugs in repetition slice handling. Unfortunately it is rather easy to provoke internal errors (i.e. detection of internal inconsistencies) or outright run-time errors in the interpreter with repetition slices, but this will be fixed.

Example 11

As an example, the following defines a repetition of 10 character objects, puts 'ABCDEFGHIJ' into it, prints the middle four characters on standard output, then makes crefs refer to the same characters and (value) assigns to the first four of them:

(* FILE ex11.gb *)
-- betaenv:descriptor --
(#
   chars: [10] @char;
   crefs: [0] ^char
do
   (for i:chars.range repeat '@'+i -> chars[i] for);
   chars[4:7] -> stdio;
   chars[]->crefs[];
   '****'->crefs;
   chars->stdio
#)

Note that "'****'->crefs" truncates crefs such that its length becomes 4. This just means that we have references to the first 4 elements of chars, and chars is not itself affected by this length adjustment.

As usual, coercion ensures that the value assignment "'****'->crefs" implicitly accesses the character objects in chars by dereferencing the object references in crefs.

The next topic is even more trivial, it's about explicit object instantiation.

 


Signed by: eernst@cs.auc.dk. Last Modified: 3-Jul-01