Introduction To Artificial Intelligence With Code: Part 1
Discover the most fundamental aspects of AI for beginners presented with a combination of theory and code, starting with an introduction to propositional logic.
Join the DZone community and get the full member experience.
Join For FreeWhen ChatGPT became a global phenomenon, books, papers, or articles about AI (Artificial Intelligence) appeared in countless numbers, but most of them were heavy on theory and mathematics.
The series of articles "Introduction to Artificial Intelligence with Code" is a compilation of the most fundamental aspects of AI for beginners, presented with a combination of theory and code (C#) to help readers gain a better understanding of the concepts and ideas discussed in these articles. In the first article of the series, we will introduce propositional logic.
Theory: An Introduction to Propositional Logic
The rules of logic provide precise meanings for propositions. These rules are used to distinguish between valid and invalid mathematical arguments. Alongside its significance in understanding mathematical reasoning, logic also has many applications in computer science, such as designing computer networks, programming, checking program correctness, and so on.
Propositions are the building blocks of the logical edifice of propositional logic. A proposition is a statement that is either true or false but cannot be both true and false simultaneously. The truth value of a proposition (in propositional logic) is referred to as its logical value (true or false).
Letters are used to symbolize propositions, as well as to represent variables in programming. The commonly used conventions for these letters are p, q, r, s, and so on.
Many mathematical propositions are created by combining one or more propositions we already have. These new propositions are called compound propositions (denoted temporarily as F), and they are formed from existing propositions using logical operators. Some basic logical operators are AND, OR, and NOT. A classical application of logical operators in computer science is to design logic gates.
To check the truth value of a compound proposition, we need to apply the rules of logic and consider the truth values of the individual propositions along with the logical operators used.
Coding: Checking the Truth Value of a Compound Proposition (F)
We’ll create a set of classes, all related by inheritance, that will allow us to obtain the output of any F
from inputs defined a priori. Here is the first class:
public abstract class F
{
public abstract bool Check();
public abstract IEnumerable<Prop> Props();
}
The abstract F
class states that all its descendants must implement a Boolean
method Check()
and an IEnumerable<Prop>
method Props()
. The first will return the evaluation of the compound proposition and the latter the propositions contained within it. Because logical operators share some features, we’ll create an abstract
class to group these features and create a more concise, logical inheritance design. The Op
class, which can be seen in the code below, will contain the similarities that every logical operator shares:
public abstract class Op : F
{
public F P { get; set; }
public F Q { get; set; }
public Op(F p, F q)
{
P = p;
Q = q;
}
public override IEnumerable<Prop> Props()
{
return P.Props().Concat(Q.Props());
}
}
The first logical operator, the AND
, is illustrated:
public class AND: Op
{
public AND(F p, F q): base(p, q)
{ }
public override bool Check()
{
return P.Check() &&Q.Check();
}
}
The implementation of the AND
class is pretty simple. It receives two arguments that it passes to its parent constructor, and the Check
method merely returns the logic AND
that is built into C#. Very similar are the OR
, NOT
, and Prop
classes, which are shown below:
//OR class
public class OR : Op
{
public OR(F p, F q): base(p, q)
{ }
public override bool Check()
{
return P.Check() || Q.Check();
}
}
//NOT class
public class NOT : F
{
public F P { get; set; }
public NOT(F p)
{
P = p;
}
public override bool Check()
{
return !P.Check();
}
public override IEnumerable<Prop> Props()
{
return new List<Prop>(P.Props());
}
}
The Prop
class is the one we use for representing propositions in compound propositions. It includes a truthValue
field, which is the truth value given to the proposition (true, false), and when the Props()
method is called it returns a List<Prop>
whose single element is itself:
public class Prop : F
{
public bool truthValue { get; set; }
public Prop(bool truthvalue)
{
truthValue = truthvalue;
}
public override bool Check()
{
return truthValue;
}
public override IEnumerable<Prop> Props()
{
return new List<Prop>() { this };
}
}
Creating and checking F = NOT(p) OR q:
var p = new Prop(false);
var q = new Prop(false);
var f = new OR(new NOT(p), q);
Console.WriteLine(f.Check());
p.trueValue = true;
Console.WriteLine(f.Check());
The result looks like this:
Summary
In this article, we introduced a basic logic — propositional logic — and we also described C# code for representing compound propositions (propositions, logical operators, and so on). In the next article, we’ll introduce a very important logic that extends propositional logic: first-order logic.
Opinions expressed by DZone contributors are their own.
Comments