Abstract
Prolog is logic programming languages for AI, based on predicate logic. This chapter discusses the structure, syntax, and semantics of Prolog language, provides comparison with procedural language like C, interpretation of predicate logic and that of Prolog, both formally as well through worked out examples, and explain how the recursion is definition as well solution of a problem, and explains with simple examples as how the control sequencing takes place in Prolog. Use of two open source compilers of prolog using simple worked out examples is demonstrated. Each concept of Prolog and logic programming is explained with the help of worked out examples. At the end, a brief summary gives glimpse of the chapter, followed with number of exercises to strengthen the learning.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
A Prolog variable starts with uppercase letter.
References
Clocksin WF, Mellish CS (2009) Programming in polog, 3rd edn. Narosa, New Delhi
http://www.dtic.upf.edu/~rramirez/lc/Prolog.pdf. Cited 19 Dec 2017
http://www.gprolog.org/. Cited 19 Dec 2017
Ivan B (2007) PROLOG programming for artificial intelligence, 3rd edn. Pearson Education
Strong John S (1989) The Legend of King Asoka—a study and translation of Asokavadana. Princeton University Press, Princeton, N.J.
Kowalski RA (1979) Algorithm = Logic + Control. Commun ACM 22(7):424–436
Van Emden MH, Kowalski RA (1976) The semantics of predicate logic as a programming language. J ACM 23(4):733–742
http://www.swi-prolog.org/. Cited 19 Dec 2017
Author information
Authors and Affiliations
Corresponding author
Exercises
Exercises
-
1.
Determine, in which of the following lists cases the unification succeeds and where it fails? If it succeeds, write down the unifier. (Note: Uppercase are variables.)
\(\begin{array}{l} \left[ a,d,z,c\right] \text { and }\left[ H|T\right] \\ \left[ apple,pear,grape\right] \text { and } \left[ A,pear|Rest\right] \\ \left[ a|Rest\right] \text { and } \left[ a,b,c\right] \\ \left[ a,\left[ \right] \right] \text { and }\left[ A,B|Rest\right] \\ \left[ One\right] \text { and }\left[ two|\left[ \right] \right] \\ \left[ one\right] \text { and } \left[ Two\right] \\ \left[ a,b,X\right] \text { and } \left[ a,b,c,d\right] \end{array}\)
-
2.
Give Prolog predicates for natural-number and plus that implement the Peano axioms. Using plus give a Prolog program times for multiplication. Further, using times give a Prolog program exp for exponentiation.
-
3.
Given the following knowledge base for prolog, find a female descendant of ‘george’, by manually running the program.
-
4.
What is response of Prolog interpreter for following queries?
-
5.
Which of the following lists are syntactically correct for Prolog language? Find out the number of elements in the lists that are correct.
\(\begin{array}{l} \left[ 1,2,3,4|\left[ \right] \right] \\ \left[ 1|\left[ 2|\left[ 3|\left[ 4\right] \right] \right] \right] \\ \left[ 1,2,3|\left[ \right] \right] \\ \left[ \left[ \right] |\left[ \right] \right] \\ \left[ \left[ 1,2\right] |4\right] \\ \left[ 1|\left[ 2,3,4\right] \right] \\ \left[ \left[ 1,2\right] ,\left[ 3,4\right] |\left[ 5,6,7\right] \right] \left[ 1|2,3,4\right] \\ \end{array}\)
-
6.
Write a predicate second(S, List), that checks whether S is the second element of List.
-
7.
Consider the knowledge base comprising the the following facts:
\(\begin{array}{l} tran(ek,one).\\ tran(do,two).\\ tran(teen,three).\\ tran(char,four).\\ tran(panch,five).\\ tran(cha,six).\\ tran(saat,seven).\\ tran(aat,eight).\\ tran(no,nine).\\ \end{array}\)
Write a predicate listtran(H, E) that translates a list of Hindi number words to the corresponding list of English number words. For example, for a list X, listtran([ek, teen, chaar], X), should give response:
\(X = [one,three,four].\)
-
8.
Draw the search trees for the following prolog queries:
-
a.
?- member(x, [a, b, c]).
-
b.
?- member(a, [c, b, a, y]).
-
c.
?- member(X, [a, b, c]).
-
a.
-
9.
Write a program that takes a grammar represented as a list of rules and given a query of as sentence, and returns whether the sentence is grammatically correct.
-
10.
Run the following programs in trace mode with single step, and describe the observed behavior, as why it is so?
-
11.
Given the following facts and rules about a blocks world, represent them in rules forms, then translate the rules into prolog, and find out “what block is on black block?”
-
Facts:
-
A is on table.
-
B is on table.
-
E is on B.
-
C is on A.
-
C is heavy.
-
D has top clear.
-
E has top clear.
-
E is heavy.
-
C is iron made.
-
D is on C.
-
Rules:
-
Every big, black block is on a red block.
-
Every heavy, iron block is big.
-
All blocks with top clear are black.
-
All iron made blocks are black.
-
Rights and permissions
Copyright information
© 2020 Springer Nature India Private Limited
About this chapter
Cite this chapter
Chowdhary, K.R. (2020). Logic Programming and Prolog. In: Fundamentals of Artificial Intelligence. Springer, New Delhi. https://doi.org/10.1007/978-81-322-3972-7_5
Download citation
DOI: https://doi.org/10.1007/978-81-322-3972-7_5
Published:
Publisher Name: Springer, New Delhi
Print ISBN: 978-81-322-3970-3
Online ISBN: 978-81-322-3972-7
eBook Packages: Computer ScienceComputer Science (R0)