Best definition for delete/3 
Author Message
 Best definition for delete/3

How should delete/3 behave when queried to remove from a list a
term containing an isolated variable? I tested three Prologs
containing built-in delete-predicates, attempting to delete f(_)
from the list [f(1),g(2),f(3)]. The results are shown below.

LPA
        | ?- removeall(f(_),[f(1),g(2),f(3)],X).
        X = [g(2)]

SWI
        1 ?- delete([f(1),g(2),f(3)],f(_),X).
        X = [g(2), f(3)]

SICStus
        | ?- delete([f(1),g(2),f(3)],f(_),X).
        X = [f(1),g(2),f(3)] ?

LPA's removeall/3 deleted f(1) and f(3). SWI, on the other hand,
deleted only f(1); the first match instantiated the anonymous
variable to 1. SICStus found no matches and deleted nothing.

If f(1), g(2) and f(3) were clauses in the database, the goal
'retractall(f(_))' or 'retractall(f(X))' would remove f(1) and
f(3). This seems to me to be a reasonable model for how delete/3
should behave, i.e., an isolated variable should be treated as a
"don't-care" -- as LPA's removeall/3 does.

I would be grateful for comment on whether delete/3 should behave
as specified by the following procedure:

            delete2([],_,[]).
            delete2([Y|Xs],X,NewXs) :-
                %% Compare X and Y without instantiating.
                copy_term(X,Y), !,
                delete2(Xs,X,NewXs).
            delete2([Y|Xs],X,[Y|NewXs]) :-
                delete2(Xs,X,NewXs).

F.M. Brown



Tue, 03 Aug 2004 02:59:37 GMT  
 Best definition for delete/3

Quote:

> I would be grateful for comment on whether delete/3 should behave
> as specified by the following procedure:

I think you just discovered that there is more than one way of deleting
something from a list. Insisting that delete/3 implements a particular way,
seems like heading for disappointment.  It is a pity that implementations
use the same name for a different thing, but there is no unique right way,
except to agree and that would be arbitrarily or by intimidation.

Maybe you should ask the implementors of the respective system
why they choose a particular way - I assume they had a reason ...

Anyway, I would strongly defend that deleting from an empty list fails :-)

And finally,

The SWI help on delete/3 says:

Quote:
> delete(+List1, ?Elem, ?List2)
> Delete all members  of List1 that simultaneously unify with Elem and
>    unify the result with List2.

seems to me to allow also an answer like

    ?- delete([a,b,c],X,L).
    X = b
    L = [a,c]

but SWI does not return that one.

Cheers

Bart Demoen



Tue, 03 Aug 2004 16:21:45 GMT  
 Best definition for delete/3

Quote:


>> I would be grateful for comment on whether delete/3 should behave
>> as specified by the following procedure:

> I think you just discovered that there is more than one way of deleting
> something from a list. Insisting that delete/3 implements a particular way,
> seems like heading for disappointment.  It is a pity that implementations
> use the same name for a different thing, but there is no unique right way,
> except to agree and that would be arbitrarily or by intimidation.

> Maybe you should ask the implementors of the respective system
> why they choose a particular way - I assume they had a reason ...

> Anyway, I would strongly defend that deleting from an empty list fails :-)

> And finally,

> The SWI help on delete/3 says:

>> delete(+List1, ?Elem, ?List2)
>> Delete all members  of List1 that simultaneously unify with Elem and
>>    unify the result with List2.

> seems to me to allow also an answer like

>     ?- delete([a,b,c],X,L).
>     X = b
>     L = [a,c]

> but SWI does not return that one.

This is exactly *the* problem that should be resolved in the (hopefully)
near future: standardise.  As Bart and the results illustrate, there are
several sensible definitions for deleting elements from a list.   They
must be identified and a standard binding between this definition and
a sensible name must be defined.

        --- Jan

P.s.    I think SWI-Prolog's definition is wrong.  Defined as it is, it
        should allow for backtracking to delete other sets of elements.
        I think I won't touch these if there is any hope that a (de-facto)
        standard will tell me the correct definition.

P.s.s.  Long time ago I was optimistic that for most of the Prolog basic
        library there is a lot of variation _where_ and _whether_ predicates
        are defined in a specific implementation. Recently I also found
        various examples that the definition varies while the name is
        the same. Grrrr!



Wed, 04 Aug 2004 18:23:14 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Best way to document definitions?

2. Best way to document definitions?

3. Simple Librarian (a better definition?) for gnat

4. Duplicates in a STEM, best way to delete?

5. Query on macro definitions within macro definitions within...

6. Delete key on keyboard deleting records

7. Deleted procs not deleted???

8. Delete all files in Dir or delete Dir

9. Keyboard Delete button to delete row in array

10. bug(?): menu delete operation always deletes at least one entry

11. delete or not to delete

12. The better is the enemy of the good

 

 
Powered by phpBB® Forum Software