Skip to content

how to launch a subset query? #165

Description

@sunriseXu

hi, do logica have subset function? I want to compare one set to another, and return a boolean value showing whether the two sets are equal or subset?
eg:
SUBSET({ }, { }) = TRUE
SUBSET({ 1, 2, 3 }, { }) = TRUE
SUBSET({ 1, 2 }, { 1, 2 }) = TRUE
SUBSET({ 1, 2, 3 }, { 1, 2 }) = TRUE
SUBSET({ 1, 3, 5 }, { 1, 2 }) = FALSE
many thanks!

Activity

  1. EvgSkv commented on Sep 3, 2021

    @EvgSkv
    Owner

    Hello!

    We need to check membership in the second list for each element in the first list.
    Here is the implementation and a test:

    @Engine("sqlite");
    
    # Returns 1 if a is subset of b and 0 otherwise.
    # To call as a predicate call Constraint(Subset(a, b)). 
    Subset(a, b) = result :-
      result_or_null Min= (e in b :- e in a),
      result == Coalesce(result_or_null, 1);
    
    # Testing Subset function.
    Test(a, b, Subset(a, b)) :-
      (a == [], b == [1,2]) |
      (a == [1,2], b == [1,2,3]) |
      (a == [1,2,3], b == [1,2]);
    

    Let me know if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions