Arrays and the array subscript notation (not in ISO)

In B-Prolog, the maximum arity of a structure is 65535. This entails that a structure can be used as a one-dimensional array and a multi-dimensional array can be represented as a structure of structures. To facilitate creating and manipulating arrays, B-Prolog provides the following built-ins.

The built-in predicate arg/3 can be used to access array elements, but it requires a temporary variable to store the result, and a chain of calls to access an element of a multi-dimensional array. To facilitate accessing array elements, B-Prolog supports the array subscript notation $X$[$I_1$,...,$I_n$], where $X$ is a structure and each $I_i$ is an integer expression. This common notation for accessing arrays is, however, not part of the standard Prolog syntax. To accommodate this notation, the parser is modified to insert a token $^\wedge$ between a variable token and [. So, the notation $X$[$I_1$,...,$I_n$] is just a shorthand for $X$$^\wedge$[$I_1$,...,$I_n$]. This notation is interpreted as an array access when it occurs in an arithmetic expression, a constraint, or as an argument of a call to @=/2. In any other context, it is treated as the term itself. The array subscript notation can also be used to access elements of lists. For example, the nth/3 and nth0/3 predicates can be defined as follows:

    nth(I,L,E) :- E @= L[I].
    nth0(I,L,E) :- E @= L[I+1].

In arithmetic expressions and arithmetic constraints, the term $X^\wedge$length means the size of the compound term $X$. Examples:

      ?-S=f(1,1), Size is S^length.
      Size=2

      ?-L=[1,1], L^length=:=1+1.
      yes
The term $X^\wedge$length is also interpreted as the size of $X$ when it occurs as one of the arguments of a call to @=/2. Examples:
      ?-S=f(1,1), Size @= S^length.
      Size=2
In any other context, the term $X^\wedge$length is interpreted as it is.

The operator @:= is provided for destructively updating an argument of a structure or an element of a list. For example:

      ?-S=f(1,1), S[1] @:= 2.
      S=f(2,1)
The update is undone upon backtracking.

The following built-ins on arrays have become obsolete since they can be easily implemented using foreach and list comprehension (see Chapter 4).

Neng-Fa Zhou 2012-01-03