0

Is it possible to model sets in Logicblox. Specifically, I want to have a set of Integers, say {1,4,9}, and would like to check if the set is an empty-set, and if some element belongs to the set.

Shambo
  • 898
  • 1
  • 10
  • 17

1 Answers1

1

Predicates actually naturally model set so if you want to model one set you can create a predicate for it as in this example:

create --unique
addblock <doc>
  my_set(x) -> int(x).
  my_set_not_empty() <- my_set(_).
  my_set_empty() <- !my_set_not_empty().
</doc>
echo 'my_set'
print my_set
echo 'my_set_empty'
print my_set_empty
echo 'my_set_not_empty'
print my_set_not_empty
echo 'adding an element'
exec '+my_set(1).'
echo 'my_set'
print my_set
echo 'my_set_empty'
print my_set_empty
echo 'my_set_not_empty'
print my_set_not_empty
close --destroy

If you want to model sets as entities all it takes is an extra key:

create --unique
addblock <doc>
  integer_set(set),integer_set:name(set:name) -> string(name).
  contains(set,integer) -> integer_set(set),int(integer).
  not_empty(set) <- contains(set,_).
  empty(set) <- !not_empty(set), integer_set(set).
</doc>
exec <doc>
  +integer_set(my_set),+integer_set:name(my_set:"my_set").
</doc>
echo "empty"
print empty
echo "not empty"
print not_empty
echo "adding elements and creating another set"
exec <doc>
  +integer_set(my_set),+integer_set:name(my_set:"other_set").
  +contains(my_set,1) <- integer_set:name(my_set:"my_set").
  +contains(my_set,4) <- integer_set:name(my_set:"my_set").
  +contains(my_set,9) <- integer_set:name(my_set:"my_set").
</doc>
echo "contains"
print contains
echo "empty"
print empty
echo "not empty"
print not_empty

close --destroy
  • Thanks a lot for your solution. I was wondering if second-order operations like `addElement[set, int] -> set` can also be defined, which takes in a set and an element, and returns another set. – Shambo Jan 11 '17 at 02:53
  • You can define that sort of things but you are entering dangerous waters there. The most obvious interpretation of 'returns another set' would involve writing a recursive rule creating an entity, which by default will be rejected by the compiler, as it could lead to non-terminating computations. There is a pragma to force the compiler to accept that sort of things if you feel adventurous. – Laurent Oget - LogicBlox Jan 19 '17 at 21:53
  • Thanks a lot for the clarification. It indeed is quite tricky. I would definitely love to try it out. I was wondering if you can give me some pointers to the pragma you mentioned. – Shambo Jan 20 '17 at 05:03