Logic Evaluation Using Powershell

Murat Yıldırımoğlu
3 min readDec 5, 2020

--

Logic is defined as “Logic is the systematic study of valid rules of inference, i.e. the relations that lead to the acceptance of one proposition (the conclusion) on the basis of a set of other propositions (premises). More broadly, logic is the analysis and appraisal of arguments.” in Wikipedia.

That is, there are statements and Logic analysis the relations, if any, between the statements and concludes that a result statement is true or false.

There are good examples of statements in the article “Aristotle’s Demonstrative Logic” (by JOHN CORCORAN, Department of Philosophy, University at Buffalo” like:

Every quadrangle is a polygon.

Every rectangle is a quadrangle.

Every square is a rectangle.

From the above statements, we can deduce some conclusions:

Every square is a quadrangle.

Every square is a polygon.

Not every rectangle is a square

We deduce those conclusions but it is not clear or objective why and how do we deduce them. They seem correct, they are based on common sense but they do not seem objective, free from the eye of the beholder.

One way to avoid subjectivity in deductions is using object-oriented software concepts like classes and inheritance. It is almost unavoidable to arrange first three statements as definitions of objects (classes).

To demonstrate it, I will use Powershell, Microsoft’s new command shell solution, because it is relatively simple to use it compared to other object-oriented programming solutions and it is in every Windows version, starting with Windows Vista.

Starting with version 5.0, Powershell has object oriented skills. We can create objects, crate define inheritance among object, we can create instances of objects, etc. And we can use “-is” and “-isnot” operators to check the class of an object too.

We will create classes based on the first three statements first. And also, we will add other classes, “circle” and “triangle” to demontrate it more clearly.

Class definitions will be like the following:

Class circle {}

Class polygon {}

Class quadrangle : polygon{}

Class triangle : polygon{}

Class rectangle : quadrangle {}

Class square : rectangle {}

Look at the figure for these definitions in a Powershell console:

Figure 1: Creating the classes

After defining the classes, we can create instances out of these class definitions. The command for the creation of a new object using a class definition is “new-object” command.

The commands to create instances are as follows:

$instanceCircle=new-object circle

$instancePolygon=new-object polygon

$instanceQuadrangle=new-object quadrangle

$instanceTriangle=new-object triangle

$instanceRectangle=new-object rectangle

$instanceSquare=new-object square

Figure 2: Creating instances of classes

Now, just using the above class definitions, we can decide if a statement is True or False, using just ordinary-looking statements, and results will be provided by the object-oriented engine, not by common sense:

$instanceSquare -is [quadrangle] TRUE

$instanceSquare -is [polygon] TRUE

$instanceSquare -is [circle] FALSE

$instanceSquare -isnot [circle] TRUE

$instanceRectangle -is [square] FALSE

$instanceSquare -isnot [rectangle] FALSE

$instanceSquare -is [rectangle] TRUE

Conclusion: Object-oriented programming presents interesting opportunities to evaluate logic statements objectively. And ubiquitous Powershell provides us with a simple tool to do logic exercises.

https://buymeacoffee.com/muratyildirimoglu

--

--

Murat Yıldırımoğlu

Electronics Engineer, Microsoft Certified Systems Engineer, Microsoft Certified Trainer