2

It's easy to asses and implement logic formulas including universal quantifiers via (nested) foreach or for loops:

(\forall x \in X)(\forall y \in Y) (Z(x,y))

foreach (type x in X)
{
   foreach (type y in Y)
   {
      if(Z(x,y))
         return true;
      else
         return false;
   }
}

But how can one implement the existential quantifiers in OO programming languages, especially C# (not logic programming languages)?

(\forall x \in X)(\exists y \in Y) (Z(x,y))

For example, to assess a number x whether it is even or not, we must code the following formula:

(\forall x)(\exists y) (x = y + y) enter image description here

enter image description here

User
  • 952
  • 2
  • 21
  • 43

2 Answers2

2

There are a few problems with your question. The first is, the code snippet you provided doesn't perform what you intend it to perform:

foreach (type x in X)
{
   foreach (type y in Y)
   {
      if(Z(x,y))
         return true;
      else
         return false;
   }
}

This will not be evaluated for all values in X nor all values in Y. Instead, the test Z(x,y) will be executed only once, on the first element x0 in X and the first element y0 in Y. Then, according to this test, both loops will break (because return exits the method).

The conventional way to do a "for all" test using loops would be something like:

foreach (var x in X)
{
   foreach (var y in Y)
   {
      if(!Z(x,y))
         return false;
   }
}
return true;

Similarly, the conventional way to do "exists" test using loops would be:

foreach (var x in X)
{
   foreach (var y in Y)
   {
      if(Z(x,y))
         return true;
   }
}
return false;

In C#, however, you can eliminate the need for loops using LINQ. So if you want to check whether a set of numbers X contains an even number, you can write:

return X.Any(x => x % 2 == 0);

EDIT: to clarify, after your edit: if you want to code the "for all x in X exists y in Y such that y+y==x", you can write something like:

foreach (var x in X)
{
   if (!Y.Any(y => y+y == x))
      return false;
}
return true;
Tomer
  • 1,606
  • 12
  • 18
  • Let's consider `X = {2,4,6,8}` and `Y = {1,2,3,5}`. Now let `z(x,y): x = y+y`. The result must be false, but your code returns true. The formula actually says: "For all x, there is a y such that x = y+y". In other words, "x is even". – User Jun 05 '17 at 08:36
  • Please find the last uploaded image. – User Jun 05 '17 at 08:38
  • Thanks. Is your edited answer a general approach to cover any relation between sets? – User Jun 05 '17 at 08:44
  • Yes. LINQ includes a variety of methods acting on sets. – Tomer Jun 05 '17 at 08:46
  • Also, it has nothing to do with OO programming at all. – Tomer Jun 05 '17 at 08:46
  • My emphasis on OOP is due to the fact that there are much easier approaches in logic languages like Prolog to solve this predicates, which don't exist in OOP languages. Thanks in advance for your help. – User Jun 05 '17 at 08:50
0

You can also use "First-Order Logic Library" in particular, an open source library. You can use the source and related design documents of such a library as how to design OOP solution for concepts like "Propositional Logic", "First-Order Logic", "Conditional Logic", "Relational Conditional Logic" and "Probabilistic Conditional Logic". For example, http://tweetyproject.org/ is provide us such a library in Java.

m.ghoreshi
  • 782
  • 5
  • 12