user1393214
user1393214

Reputation:

How to implement existential quantifiers in OO programming?

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

Upvotes: 2

Views: 1134

Answers (2)

m.ghoreshi
m.ghoreshi

Reputation: 852

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.

Upvotes: 0

Tomer
Tomer

Reputation: 1704

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;

Upvotes: 1

Related Questions