Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union of two variable sets

Given two lists of variables, what is the most compact and canonical way in ISO Prolog to determine the union of both? That is, we want a definition for the (meta-logical) predicates

varset_union(VarSet1, VarSet2, Union)

and for a list of lists

varset_union(VarSets, Union)

where Union is a list of unique variables of the given VarSets.

Here is an overview of the built-ins in ISO/IEC 13211-1:1995 including Cor.2:2012.

like image 442
false Avatar asked Dec 10 '14 01:12

false


People also ask

What is the formula for union of two sets?

ii) Union of two sets:= n(A) + n(B) – n(A ∩ B) Simply, the number of elements in the union of set A and B is equal to the sum of cardinal numbers of the sets A and B, minus that of their intersection.

What is the union of Set A and set B?

The symbol ∪ is employed to denote the union of two sets. Thus, the set A ∪ B—read “A union B” or “the union of A and B”—is defined as the set that consists of all elements belonging to either set A or set B (or both).

Is a ∩ b equal to B ∩ A?

Is A ∩ B Equal to B ∩ A? As per the commutative property of the intersection of sets, the order of the operating sets does not affect the resultant set and thus A ∩ B equals B ∩ A.


2 Answers

Solution using term_variables/2:

varset_union(VarSet1, VarSet2, Union):-
    term_variables([VarSet1|VarSet2], Union).

varset_union(VarSets, Union):-
    term_variables(VarSets, Union).

Solution using setof/3:

varset_union(VarSet1, Varset2, Union):-
    varset_union([VarSet1, VarSet2], Union).

varset_union([], []).
varset_union(VarSets, Union):-
    setof(Var, VarSet^(member(VarSet, VarSets), member(Var, VarSet)), Union).
like image 54
Tudor Berariu Avatar answered Oct 23 '22 15:10

Tudor Berariu


Based on Tudor's great answer, I have devised a definition of varset_union/3 that is more compact by 2 characters:

varset_union(VarSet1, VarSet2, Union):-
        term_variables(VarSet1+VarSet2, Union).

;-)

like image 39
mat Avatar answered Oct 23 '22 15:10

mat