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
Please enter at least three characters to search
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Automatic Code Transformation With OpenRewrite
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • The Role of Artificial Intelligence in Climate Change Mitigation
  • A Comprehensive Guide to Protect Data, Models, and Users in the GenAI Era

Trending

  • The Modern Data Stack Is Overrated — Here’s What Works
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  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
Likes (3)
Comment
Save
Tweet
Share
1.7K 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

  • Automatic Code Transformation With OpenRewrite
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • The Role of Artificial Intelligence in Climate Change Mitigation
  • A Comprehensive Guide to Protect Data, Models, and Users in the GenAI Era

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!