Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list_to_set/2 is not required to order #150

Closed
Jean-Luc-Picard-2021 opened this issue Aug 24, 2022 · 5 comments
Closed

list_to_set/2 is not required to order #150

Jean-Luc-Picard-2021 opened this issue Aug 24, 2022 · 5 comments
Labels

Comments

@Jean-Luc-Picard-2021
Copy link

Jean-Luc-Picard-2021 commented Aug 24, 2022

I find a commit comment:

diff --git a/ports/fcube/fcube.lgt b/ports/fcube/fcube.lgt
index acdc0ebbf5..d1d46b5951 100644
--- a/ports/fcube/fcube.lgt
+++ b/ports/fcube/fcube.lgt
+ % Paulo Moura: added the next list_to_set/2 to allow using an ordered set representation
list_to_set(NewSet0, NewSet).

list_to_set/2 does not order, only deduplicate.
You can try yourself in SWI-Prolog:

/* SWI-Prolog 8.5.14 */
?- list_to_set([c,a,b,a], X).
X = [c, a, b].
/* Its not ordered, if it were ordered it were [a,b,c] */

In some Prolog systems it does order, but this
is not a guarantee neither a requirement.

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Aug 24, 2022

If you would like to use ordered sets, you need
to use the ISO core standard sort/2.
But its not guaranteed to run faster in any way.

It can also run slower. It would only run
faster if you could eliminate the sort/2. Which
is sometimes possible, like the result of

a ord_XXX operation, doesn't need a sort/2.

P.S.: You see here, its not list_to_set/2, but it would be list_to_ord_set/2,

list_to_ord_set(+List, -OrdSet)
Transform a list into an ordered set. This is the same as sorting the list.
https://www.swi-prolog.org/pldoc/man?predicate=list_to_ord_set/2

It has this implementation:

list_to_ord_set(List, Set) :-
 sort(List, Set). 

https://www.swi-prolog.org/pldoc/doc/_SWI_/library/ordsets.pl?show=raw

@pmoura pmoura added the invalid label Aug 24, 2022
@pmoura
Copy link
Member

pmoura commented Aug 24, 2022

In the same fcube.lgt file:

	:- uses(set, [
		as_set/2 as list_to_set/2, intersection/3, subtract/3, union/3
	]).

The Logtalk sets library is an ordered sets library.

@pmoura pmoura closed this as completed Aug 24, 2022
@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Aug 24, 2022

Ok, you use the same protocol for ord_XXX.
And it is implemented as follows:

as_set(List, Set) :-
     sort(List, Set).

Is it more performant? Or is it slower than with lists?
Does your lists have a list_to_set/2? Oh you cannot test it,

no union, intersection, etc.. here:
https://github.com/LogtalkDotOrg/logtalk3/blob/master/library/types/list.lgt

Edit 24.08.2022
But your lists have an interesting outlier:

	subtract([], _, []).
	subtract([Head| Tail], List, Rest) :-
		(	memberchk(Head, List) ->
			subtract(Tail, List, Rest)
		;	Rest = [Head| Tail2],
			subtract(Tail, List, Tail2)
		).

@pmoura
Copy link
Member

pmoura commented Aug 24, 2022

Union, intersection, ... are set operations. Subtract is not a set only operation.

@Jean-Luc-Picard-2021
Copy link
Author

Jean-Luc-Picard-2021 commented Aug 24, 2022

What do you mean are subtract is no set operation?
Do you mean no ordered list-set operation or no list-set operation.
Its actually both. I find it also in your set.lgt:

subtract(Set, [], Set) :- !.
subtract([], _, []) :- !.
subtract([Head1| Tail1], [Head2| Tail2], Difference) :-
		compare(Order, Head1, Head2),
		subtract(Order, Head1, Tail1, Head2, Tail2, Difference).

subtract(=, _, Tail1, _, Tail2, Difference) :-
		subtract(Tail1, Tail2, Difference).
subtract(<, Head1, Tail1, Head2, Tail2, [Head1| Difference]) :-
		subtract(Tail1, [Head2| Tail2], Difference).
subtract(>, Head1, Tail1, _, Tail2, Difference) :-
                subtract([Head1| Tail1], Tail2, Difference).

In SWI-Prolog its clearer since there is no name overloading:

https://www.swi-prolog.org/pldoc/doc_for?object=ord_subtract/3

P.S.: Mathematically we would write P \ Q for the set difference.

@LogtalkDotOrg LogtalkDotOrg locked as resolved and limited conversation to collaborators Aug 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants