Skip to main content

Logic Programming and Prolog

  • Chapter
  • First Online:
Fundamentals of Artificial Intelligence
  • 12k Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 64.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Hardcover Book
USD 84.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    A Prolog variable starts with uppercase letter.

References

  1. Clocksin WF, Mellish CS (2009) Programming in polog, 3rd edn. Narosa, New Delhi

    Google Scholar 

  2. http://www.dtic.upf.edu/~rramirez/lc/Prolog.pdf. Cited 19 Dec 2017

  3. http://www.gprolog.org/. Cited 19 Dec 2017

  4. Ivan B (2007) PROLOG programming for artificial intelligence, 3rd edn. Pearson Education

    Google Scholar 

  5. Strong John S (1989) The Legend of King Asoka—a study and translation of Asokavadana. Princeton University Press, Princeton, N.J.

    Book  Google Scholar 

  6. Kowalski RA (1979) Algorithm = Logic + Control. Commun ACM 22(7):424–436

    Article  Google Scholar 

  7. Van Emden MH, Kowalski RA (1976) The semantics of predicate logic as a programming language. J ACM 23(4):733–742

    Google Scholar 

  8. http://www.swi-prolog.org/. Cited 19 Dec 2017

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to K. R. Chowdhary .

Exercises

Exercises

  1. 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. 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. 3.

    Given the following knowledge base for prolog, find a female descendant of ‘george’, by manually running the program.

    figure ao
  4. 4.

    What is response of Prolog interpreter for following queries?

    figure ap
  5. 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. 6.

    Write a predicate second(SList), that checks whether S is the second element of List.

  7. 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(HE) that translates a list of Hindi number words to the corresponding list of English number words. For example, for a list X, listtran([ekteenchaar], X), should give response:

    \(X = [one,three,four].\)

  8. 8.

    Draw the search trees for the following prolog queries:

    1. a.

      ?- member(x, [abc]).

    2. b.

      ?- member(a, [cbay]).

    3. c.

      ?- member(X, [abc]).

  9. 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. 10.

    Run the following programs in trace mode with single step, and describe the observed behavior, as why it is so?

    1. a.

      Factorial Program (Example 5.5).

    2. b.

      GCD Program (Example 5.6).

    3. c.

      Mortal men program (Example 5.2).

  11. 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

Reprints and permissions

Copyright information

© 2020 Springer Nature India Private Limited

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

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)

Publish with us

Policies and ethics