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. In order 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 also requires a chain of calls to access an element of a multi-dimensional array. In order to facilitate the access of array elements, B-Prolog supports the array subscript notation X[I1,...,In], where X is a structure, and each Ii is an integer expression. However, this common notation for accessing arrays is not part of the standard Prolog syntax. In order to accommodate this notation, the parser is modified to insert a token, $^\wedge$, between a variable token and [. So, the notation X[I1,...,In] is just a shorthand for X$^\wedge$[I1,...,In]. 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 indicates 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 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 by using foreach and list comprehension (see Chapter 4).

Neng-Fa Zhou 2013-01-25