DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Flask Web Application for Smart Honeypot Deployment Using Reinforcement Learning
  • Outsmarting Cyber Threats: How Large Language Models Can Revolutionize Email Security
  • Query SQL and NoSQL Databases Using Artificial Intelligence
  • The Best C# .NET Reporting Tools

Trending

  • GBase 8a Implementation Guide: Performance Optimization
  • 7 Linux Commands and Tips to Improve Productivity
  • Machine Learning With Python: Data Preprocessing Techniques
  • Setting up CI/CD Pipelines: A Step-By-Step Guide
  1. DZone
  2. Coding
  3. Languages
  4. Introduction To Artificial Intelligence With Code: Part 1

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.

By 
Trần Ngọc Minh user avatar
Trần Ngọc Minh
·
May. 07, 24 · Tutorial
Like (3)
Save
Tweet
Share
1.2K Views

Join the DZone community and get the full member experience.

Join For Free

When 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:

C#
 
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:

C#
 
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:

C#
 
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:

C#
 
//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:

C#
 
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:

C#
 
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:

Microsoft Visual Studio Debug Console

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.

Computer science Proposition Operator (extension) artificial intelligence C# (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Flask Web Application for Smart Honeypot Deployment Using Reinforcement Learning
  • Outsmarting Cyber Threats: How Large Language Models Can Revolutionize Email Security
  • Query SQL and NoSQL Databases Using Artificial Intelligence
  • The Best C# .NET Reporting Tools

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: