Re: [eclipse-clp-users] Dealing with ambiguity between empty arrays and empty lists

From: Joachim Schimpf <jschimpf_at_coninfer.com>
Date: Thu, 05 Dec 2013 01:21:41 +0000
On 04/12/2013 19:50, Alex Mahrer wrote:
> Lists are defined in ECLiPSe as recursive data structures with root term
> '[]' so that an empty list is represented by the atom '[]'. Arrays are
> simply compound terms with functor '[]', thus empty arrays are also
> represented by the atom '[]'.
> 
> This ambiguity leads to inconsistencies in instances where empty arrays
> must be distinguished from empty lists. As an example, consider the
> following query that uses dim/2 to both create a two-dimensional array
> and retrieve the dimensions of that array:
> 
> ?- dim(Array2D, [3, 0]), dim(Array2D, Dims).
> 
> Array2D = []([], [], [])
> Dims = [3]
> 
> In this example, the dimensions provided to dim/2 in the first call do
> not match the dimensions that it derives from the array in the second call.
> 
> Two approaches to dealing with this problem come to mind:
> 1) Avoid the use of empty arrays.
> 2) Represent arrays using a different functor, and rewrite all
> predicates that handle arrays to accommodate.
> 
> Neither of these approaches is ideal. Are there any alternatives that
> I'm missing?


You are quite right about the design, and the resulting issues.
I had agonized over the functor when introducing the arrays, but
settled on this solution eventually...

Some of the new array builtins in release 6.1 were indeed introduced
to address related issues, in particular connected to the now
deprecated flatten_array/2 primitive:

http://www.eclipseclp.org/doc/bips/kernel/termmanip/array_concat-3.html
http://www.eclipseclp.org/doc/bips/kernel/termmanip/array_flat-3.html
http://www.eclipseclp.org/doc/bips/kernel/termmanip/array_list-2.html

As for the remaining problems, I believe (correct me if I'm wrong) they
only occur when you do more than one of these things at the same time:

1. the code is unaware of the dimensions of the arrays it works with,
   or not all rows have the same length

2. the arrays can contain lists or other arrays as elements (the common
   case of numeric arrays should be fine)

To address (1), you could explicitly carry around the array dimensions
in addition to the array itself.  To address (2), you could use type
wrappers around the elements.

That's as much as I can think of in general terms -- do you have a specific
problem that you need to solve?


-- Joachim
Received on Thu Dec 05 2013 - 01:21:53 CET

This archive was generated by hypermail 2.2.0 : Tue Dec 10 2013 - 06:13:25 CET