- [Instructor] Welcome back.
In this our last week,
we are going to review logic programming.
In particular, we are going to review
programming with Prolog.
Prolog is the language that
we're going to be using
to apply logic programming.
Let's start.
Logic programming focus on what is needed,
instead of how to implement that.
In logic programming the
programmer describe problem
and basically let the
computer to find a solution.
We trust in environment and interpreter,
that we are going to be
using to find a solution,
that meet the requirement,
that we specify.
In the logic programming
paradigm we use a variation
of predicate logic syntax, that is similar
to natural language,
predicate logic basically
eliminates necessary wards from sentences
and then transforms the
sentence by placing first
the relationship, that we're
gonna call the predicate
of the sentence.
And then inside parenthesis
is going to group
the items, objects or
elements that I involve
in that relationship, let
me show you some examples.
The first column in this
table, it's showing you
some sentence in english.
In the second column you
can see the key valent
of that sentence but in predicate logic.
In the first line, a car is fast.
That is the way, that you write this idea
using natural language.
If you think about it, the two ideas,
that are important there,
the car, that is the object
and the predicate, fast.
So in predicate logic, you
can express this like fast,
the predicate and inside parenthesis car,
because that is the
object, that is involved
in this relationship.
In the second line, another
expression in english.
A rose is red, the two ideas,
that are important there
is rose, the object
and red, the predicate.
In predicate logic, you put
further the predicate, red,
and inside parenthesis, the object, rose.
Those two are simple
sentences, we are going to call
simple sentence, like
those two rose, facts.
Those two are facts, in the next two lines
we have a different type of
sentence, in the next two lines
we have what we call
rules, they are sentence
connected by a condition and if statement.
That if statement is
going to be represented
by column dash in the predicate logic.
For instance Bill likes the
car, if the car is fast.
We have two parts in this
sentence and those two parts
are connected by the if condition.
In the first part of the
statement, the predicate is lights
and the objects involved
in that relationship, likes
are Bill and the car.
There is a relationship
between Bill and the car
and that relationship is likes.
Bill likes the car.
However, that is conditioned
to a different fact,
the car is fast, in this
part, the predicate is fast
and the object, that is involved
in that sentence is car.
The representation of that
is fast and in parenthesis
the object, in this case,
only one object, car.
Those two sentence are
connected by the if statement
and therefore a rule, Bill like the car,
if the car is fast.
In a similar way, in the other
sentence, we have two parts,
the first one, humidity is
high, the predicate is high
and the object involved in
that predicate is humidity.
High parenthesis, the object, humidity.
In the second part, we only have it rains
and there we have a predicate,
rains, but we do not
have an object, so it's just
a predicate without objects.
As you notice in the sentence
we the have the bear,
but we do not have a
complement in the sentence.
So it makes sense, that we
only have the predicate,
but no objects involved
and those two sentence
humidity is high, it
rains, they are connected
by a conditioned statement
if, and you can notice
what happen with those two
if, that you can see in blue,
they are represented by this colon dash.
These two lines are called
rules in predicate logic
and you are going to identify them,
because they are gonna
have this colon dash,
that represents the ward
if in natural language.
In natural language you can
express the same relationship
between different objects, for instance,
you can state, that Jane
is the mother of Elaine
and also you can state, that
Jane is the mother of Mike.
You have an object, that
have the same relationship
with other two or you
can express, that David
is the father of Jessie and
Jessie the father of Obed,
so you have an object, that has two childs
and you have an object, that have a son,
which is also the father
of another object.
Those sentence in natural
language can be expressed
in predicate logic as
follows, similar idea,
that before you have two
relationships, we can call it
mother of and father of,
and then you express,
in parenthesis, the
objects, that are involved.
Jane is involved in two
lines as the first object
and the second object is
Elaine in the first line
and Mike in the second
one, regarding father of,
as you notice, Jessie, in the first line,
represent the son, but in the second line
represent the father.
You have this transition,
David father of Jessie
and then Jessie father of Obed.
Those four are four different lines
and each of those is a fact.
Finally, in the last line, we have a rule
and we know, that these are rules,
because we have this if condition
in the natural language
sentence and you can notice
the colon dash in the predicate logic.
In the first part, as usual,
we have the predicate,
grandmother of and the
objects involved are X and C.
Then in the second part, we
are using the relationship
mother of and father of,
that we defined before
to explain what is the
meaning of grandmother of.
As you notice, we have twice
in the natural language,
the expression mother
of and we have one time
the expression father of.
The four in the predicate
logic, we have the predicate
mother of twice and father of one.
Something, that is new,
as you can notice in red
are the comma and the semicolon.
The comma is going to mean
and and the semicolon is or.
In the predicate logic, comma is and
and semicolon is or.
X is grandmother of C,
if X is the mother of Y
and Y is the mother of C
or Y is the father of C.
The syntax is a little bit different,
but when you read the
natural language expression
or the predicate logic, they are the same.
An additional note in this example,
that you need to remember,
uppercase strings
represent variables, lower
case represent names.
When you use Jane,
Elaine, Mike, car, rose,
all of those, because they are lowercase,
they represent a particular
name, a particular object,
however, in this last example,
we are using X, C and Y
in the uppercase, that
represent variables.
It's in a space, that can be
used for more than one object
or for more, than one item and
the expression will be true.
We have facts representing
knowledge, that we have,
a key valent to sentences
in natural language
in which we put the predicate
first and the objects
or items inside parenthesis
separated by comma.
We have the comma, that represent and
and connect different facts.
We have the semicolon, that represent or
and also connect different facts.
Here we have Bill like
cars and Bill like bike.
Or in the second line,
Bill travels using a car
or Bill travels using a bike.
Notice, that at the same
time you can like two
different objects, like Bill like the car
and the bike at the same time.
However, when we apply
the relationship travel,
we travel by a car or
we travel using a bike,
but we cannot do both at the same time,
therefore we use or instead of and.
We use facts to create
rules, the rules help us
to infer information from the given facts.
You are going to identify the rules,
because the colon and
the dash, that represent
this if statement, so in
this example we are defining,
that there is a relationship
between Bill and Joe,
because Bill is father
of Joe and we establish
this rule, that the relationship son exist
between Joe and Bill, because we know,
that the relationship father
exist between Bill and Joe.
The programming language,
that we are going to use
to work with logic programming
is going to be Prolog.
The name or Prolog means programming logic
and this language uses an interpreter.
This interpreter implements
a deductive database.
What is going to happen is,
that we are going to provide
a set of statements as rules and facts
and the deduction system of the database
is going to allow us to make
queries in the database.
And those queries are
going to be called goals.
Tricky elements programming with Prolog
are going to be facts, rules and queries.
And remember, in Prolog,
variables are going to have
uppercase letters and constants or names
are going to be lowercase.
As usual, to start working
with the new language,
that we are going to
learn, we need a compiler
or an interpreter, in this
case we need an interpreter.
As I mentioned before,
Prolog uses an interpreter.
Your first assignment is to install
an interpreter for Prolog.
I could recommend you
this one, you can download
the interpreter from this
URL, this one work in Windows,
Mac and Linux.
I am sure, that you can
find another options,
there are several, this
is only one of them.
As usual, I want to share with
you, that there is an option
for an online interpreter,
so you do not need
to install anything in your
computer, you can just go
to this URL and in this
webpage you can have access
to an interpreter for Prolog,
that is exactly the same
that the one, that I am
recommending you to download.
You can identify the same
logo in the background.
Let me show you the environment
to work with Prolog.
I'm gonna use the online version.
Here in the online, the first
thing, that you need to do
is to establish, that you
want to create a new program.
An empty program, so I can
start with a click here.
Create a program.
And your environment is going
to be three different sections.
in the first section here
we are able to input data
to the deductive database,
so here you are going
to be able to define facts and rules.
Facts and rules go here.
After you provide the
data to the database,
then you can do queries.
Your queries are going to be here.
Finally, you can rule your queries.
When you rule your queries,
you click this bottom here run
and you are gonna saw the
result here in this space.
Step number one, input
data, facts and rules.
Step number two, queries, run the query,
step number three, you get results.
For instance, we can define a fact.
The exam is going to be easy.
We can define that just with the predicate
and if I do not want to add the objects,
that participate in that
predicate, in that relationship,
the only thing, that I need
is the name for the predicate.
Everything in lowercase and at
the end I need to put a dot.
Dot represent the end of this statement.
And as you notice, the text
is gonna change the color.
That red color indicate,
that that one is the name
of a predicate.
After that, my database
have one line, one fact.
Then I can go here and make a query.
My query could be as simple as asking
exam easy as you notice,
it's exactly the same,
that I put before in the database.
When I run this query,
my result is going to be true.
The meaning of this is, that
I give information to Prolog,
the information, that I provide was,
that the exam is going to be easy,
I provide that with one
predicate, exam easy.
Then, when I ask with this query,
if the exam is going to be
easy, my answer is yes, true.
Exam easy is true.
I can provide more
information, like Arizona hot,
when I run this query,
again, my result is true.
Now I can define facts, that have objects.
For instance, I can define
eats and that is going
to be a relationship
between Fred and orange.
Fred eats oranges, dot.
Also I can define age
and I can use again the object Fred
and I can use another object,
that is going to be a number
and I define this relationship
H between Fred and 32.
So Fred age 32, dot.
Now with that information in my database
I can ask new queries.
For instance, I can ask
exactly the same thing,
that I put in the line three
of the database, eat Fred and orange.
If I run this query, as expected,
the result is going to
be true, Fred eat orange.
Now, what about a small change here.
I'm gonna run the query, eat Fred,
but now with apples instead of orange.
And the result is going to be false.
Because that relationship do
not exist in the database.
In a similar way, I can
change Fred for Jane
and I can ask if Jane eats apples.
If I run the query,
I'm going to get false.
Because that relationship is not defined.
I can also use the relationship
age in a similar way,
so I can ask for Fred and I
can ask if the age of Fred
is 20 and the result is going to be false.
Because I defined before,
that the age of Fred is 32.
Everything, that you
define in the database,
every fact, that you provide
is going to return true,
when you use the same
sentence in the query section.
But anything, that is not
defined in the database
is going to return a false value.
Let's include more data in the database.
We can add new objects
for the relationship eats,
like Fred also eats apples,
like Mike eats oranges.
And maybe also Jane eat orange.
And we can add more data to
the relationship age also,
so age Jane, 32
or age Mike 20.
With this data we can ask
more complex questions.
We can write the query,
like the following.
We can ask about the relationship eats,
we can ask about Fred
and if we put a value here, the result,
that we're going to get is
true of false as before,
depending on whether or not
that fact is defined, right?
However, if we put here
a variable and variables
are in the uppercase, the
result is going to be different.
This X here is not an
object, I am not defining,
that Fred eats X, because
that X is in uppercase,
it is a variable, variables store data.
When I run this query,
my result is going to be,
that X can be equal to orange.
The value orange make
this expression true.
If I click here in next,
prologue is going to tell me,
that another value, that
make this expression true
is X equal apples.
Therefore my question about
Fred eats is something
like what does Fred eat.
And the answer are orange and apples.
Now I can also ask regarding eats
something like this.
In this case I want to
know who eats orange.
If I run this query, the result,
one of the results is Fred.
Prolog is telling me, that
if I put Fred in the place
of the variable, I'm going to
get a statement, that is true.
If I press next, another value,
that makes this expression true is Mike,
so Mike eat orange and
if I press next again,
another value, that could make
this expression true is Jane.
Basically the answer to
my question who eat orange
is Fred, Mike and Jane, those
three have a relationship
with orange and that relationship is eats.
In a similar way I can
use age and I can ask
with a variable,
who is 32
or who has the age 32.
And when I run this query,
the answer is going to be Fred
and also it's going to be
Jane, so both Fred and Jane
have as an age 32.
I can remove the variable
and put here Mike
and here my question is Mike is 32,
if I run it, my answer is false.
I can ask age Mike and instead of a number
I can put here a variable
and my answer is going to be
the value for that variable,
that make the statement
true and that is 20.
So the age of Mike is 20.
Moreover, I can use variables
for both objects, like here,
age and W comma X, both uppercases
and if I run this query,
the first value is,
I can use W equal Fred and
X equal 32 and that is true.
Or I can use W equal Jane and
X equal 32 and that is true
or W equal Mike and X
equal 20, and that is true.
Basically, I if I use
variables for both objects,
what I get is a list with
all the relationships,
that I defined in the database.
Those are the key ideas in Prolog,
at this point I recommend you
to install the interpreter
or go online and use this one.
Read the textbook section one
to section five in chapter
five and start practicing.
After that you can go
and sort the question
in the homework and then your quiz.
See you later.
