Artificial Intelligence Languages
Programming languages, such as C, are often considered procedural or functional languages because they divide a large program into separate procedures or functions that tell the computer how to solve a problem step-by-step.
Although telling the computer what to do step-by-step might seem like the most logical way to program a computer, another way to program a computer is by using a declarative language. Instead of describing how to solve a problem, declarative programming languages describe
Facts: Information about the problem
Rules: Relationships between this information
By using facts and rules, programs written in declarative languages can literally figure out an answer on their own without being told explicitly how to do it.
The most popular declarative programming language is Prolog (short for Programming in Logic). A typical Prolog fact might look like this:
father("Sally", "Jesse").
The preceding fact tells the computer that Jesse is the father of Sally. Now if you want to know who the father of Sally might be, you could ask the following:
?- father("Sally", X).
Using the fact that earlier stated that the father of Sally was Jesse, the preceding Prolog command would simply return:
X = "Jesse".
At this point, Prolog simply uses a predefined fact to come up with an answer. Notice that even in this simple example, no instructions told the Prolog program how to use the fact that Jesse is the father of Sally.
A list of facts by themselves can be made more useful by including rules that define relationships between facts. Consider the following Prolog program that defines two facts and one rule:
father("Jesse", "Frank").father("Sally", "Jesse").grandFather(Person, GrandFather) :- father(Person, Father), father(Father, GrandFather).
The two facts tell the computer that Frank is the father of Jesse, and Jesse is the father of Sally. The grandfather rule tells the computer that someone is a grandfather if they’re the father of someone’s father.
Suppose you typed the following Prolog command:
?- grandFather("Sally", Y).
The Prolog program tells the computer to use its known facts and rules to deduce an answer, which is:
Y = "Frank".
In other words, Frank is the grandfather of Sally. (Frank is the father of Jesse, and Jesse is the father of Sally.)
Just from this simple example, you can see how different a Prolog program works compared to a program written in C or Java. Instead of telling the computer how to solve a problem, declarative programming languages let you state the facts and the rules for manipulating those facts so the computer can figure out how to solve the problem.
One of the most popular programming languages favored by the AI community is LISP (which stands for LISt Processing). The basic idea behind LISP is that everything is a list that can be manipulated by the computer. For example, a typical LISP command might look like this:
(print "Hello world")
This LISP command is a list that displays the following onscreen:
"Hello world"
The enclosing parentheses define the start and end of a list. A different way to print "Hello world"
onscreen would be to use this LISP command:
(list "Hello world")
The preceding command would print the following:
("Hello world")
In this case, the list command tells the computer to treat "Hello world"
as a list, so it encloses it in parentheses. Now consider what happens if you insert a command (list) inside another command (list):
(list (print "Hello world"))
This is how the preceding LISP command works:
1 The innermost command (list) runs first, which is the (print "Hello world") list.This displays the following onscreen:"Hello world"From the computer's point of view, the original LISP command now looks like this:(list "Hello world")
2 This command now displays the following onscreen: ("Hello world")
So, the command
(list (print "Hello world"))
prints the following:
"Hello world"("Hello world")
In the previous example, LISP treats the (print "Hello world")
list first as a command (to print "Hello world"
onscreen) and then as data to feed into the list command to display the list ("Hello world")
onscreen.
With traditional programming languages, like C or Java, commands and data are separate where data may change but commands never change. With LISP, a list can be both a command and data. That makes it possible for a program to change its lists (treated either as data or as a command), essentially allowing a program to modify itself while running, which can mimic the learning and thinking process of a human being.
As you can see, both LISP and Prolog offer radically different ways to program a computer compared to C or Java. Just as languages, like C and Java, free you from the tedium of manipulating registers and memory addresses to program a computer, so do LISP and Prolog free you from the tedium of writing explicit step-by-step instructions to program a computer.