prolog
play

Prolog Declarative/logic paradigm Functional paradigm No - PowerPoint PPT Presentation

Prolog Declarative/logic paradigm Functional paradigm No assignment statement Declarative paradigm No program! Specification without implementation. Prolog Declarative/logic paradigm Functional paradigm No


  1. Prolog • Declarative/logic paradigm • Functional paradigm – No assignment statement • Declarative paradigm – No program! Specification without implementation.

  2. Prolog • Declarative/logic paradigm • Functional paradigm – No assignment statement • Declarative paradigm – No program! Specification without implementation.

  3. Prolog • Declarative/logic paradigm • Functional paradigm – No assignment statement • Declarative paradigm – No program! Specification without implementation.

  4. Using Prolog • Two shells • vi to edit and save the database, or more to view it • Prolog to query the database

  5. 4 Chapter 1 lntroduction to Prolog The question ?- parent( tr, This time thel ,,t solution: ...: ,l ii X=ann 5: ã * We may now .â * find: ç 4 X=pat # ä â If we request f .æ ânn ã s the solutions . ã s Our progra ã ê * whom? That i .ë ,.Ê Find X and '9. * æ This is express e d ,* ?- parent( X, I Figure 1.1 A family tree. :t tí ç Prolog now flr t: t: displayed one This program consists of six clauses. Each of these clauses declares one fact about the solutions I the parent relation. For example, parent( tom, bob) is a particular instqnce of the x=pam parent relation. In general, a relation is defined as the set of all its instances. Y = bob; When this program has been communicated to the Prolog system, Prolog can X=tom be posed some questions about the parent relation. For example: Is Bob a parent Y = bob; of Pat? This question can be communicated to the Prolog system by typing: X=tom Y =liz; ?- parent( bob, pat). Having found this as an asserted fact in the program, Prolog will answer: We can alwa¡ semicolon. yes Our exampJ A further query can be: is a grandparr ?- parent( liz, pat), illustrated by ì t'îi (1) Who is a 1 answers: (2) Who is a I because the program does not mention anything aboutLiz being a parent of Pat. It also answers 'no' to the question: parent ?- parent( tom, ben). ì t More interesting questions can also be asked. For example: Who is Liz's parent? I I gr ?- parent( X, liz). parent Prolog will now tell us what is the value of X such that the above statement is true. So the answer is: X=tom Figure 1.2 The ¡

  6. 4 Chapter 1 lntroduction to Prolog The question Defining relations by facts ?- parent( tr, This time thel parent( pam, bob). ,,t solution: ...: ,l parent( tom, bob). ii X=ann parent( tom, liz). 5: ã * parent( bob, ann). We may now .â * find: parent( bob, pat). ç 4 X=pat parent( pat, jim). # ä â If we request f .æ ânn ã s the solutions . ã s Our progra ã ê * whom? That i .ë ,.Ê Find X and '9. * æ This is express e d ,* ?- parent( X, I Figure 1.1 A family tree. :t tí ç Prolog now flr t: t: displayed one This program consists of six clauses. Each of these clauses declares one fact about the solutions I the parent relation. For example, parent( tom, bob) is a particular instqnce of the x=pam parent relation. In general, a relation is defined as the set of all its instances. Y = bob; When this program has been communicated to the Prolog system, Prolog can X=tom be posed some questions about the parent relation. For example: Is Bob a parent Y = bob; of Pat? This question can be communicated to the Prolog system by typing: X=tom Y =liz; ?- parent( bob, pat). Having found this as an asserted fact in the program, Prolog will answer: We can alwa¡ semicolon. yes Our exampJ A further query can be: is a grandparr ?- parent( liz, pat), illustrated by ì t'îi (1) Who is a 1 answers: (2) Who is a I because the program does not mention anything aboutLiz being a parent of Pat. It also answers 'no' to the question: parent ?- parent( tom, ben). ì t More interesting questions can also be asked. For example: Who is Liz's parent? I I gr ?- parent( X, liz). parent Prolog will now tell us what is the value of X such that the above statement is true. So the answer is: X=tom Figure 1.2 The ¡

  7. Demo • ?- consult( ‘ch1.pl’). • ?- halt. % to quit • ; % next solution • a % all solutions • <ret> % stop

  8. 1.1 Defining relations by facts 5 The question Who are Bob's children? can be communicated to Prolog as: ?- parent( bob, X). This time there is more than iust one possible answer. Prolog frrst answers with one solution: X=ann We may now request another solution (by Wping a semicolon), and Prolog will ñnd: X=pat If we request further solutions (semicolon again), Prolog will answer'no'because all the solutions have been exhausted. Our program can be asked an even broader question: Who is a parent of whom? That is: Find X and Y such that X is a parent of Y. This is expressed in Prolog by: ?- parent( X, Y). Prolog now finds all the parent-child pairs one after another. The solutions will be displayed one at a time as long as we tell Prolog we want more solutions, until all the solutions have been found. The answers are output as: auses declares one fact about is a particular instønce of the x=pam : set of all its instances. Y = bob; he Prolog system, Prolog can X=tom For example: Is Bob a parent Y = bob; rolog system by Çping: X=tom Y = liz; Prolog will answer: We can always stop the stream of solutions by typing a return instead of a semicolon. Our example program can be asked still more complicated questions like: Who is a grandparent of Jim? This query has to be broken down into two steps, as illustrated by Figure 1.2. (1) \Âfho is a parent of Jim? Assume that this is some Y. (2) Who is a parent of Y? Assume that this is some X. ut Liz being a parent of Pat. It parent I mple: Who is Liz's parent? I grandparent I I parent tt the above statement is true. Figure 1.2 The grandparent relation expressed as a composition of two parent relations. Who is a grandparent of jim? 1. Who is a parent of jim? Y 2. Who is a parent of Y ? X Query: ?- parent( Y, jim), parent( X, Y).

  9. Who are tom’s grandchildren? ?- parent( tom, X), parent( X, Y).

  10. Who are tom’s grandchildren? ?- parent( tom, X), parent( X, Y).

  11. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? X = bob Y = ann ? ; 2 1 Redo: parent(bob,ann) ? 2 1 Exit: parent(bob,pat) ? X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  12. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? Exits one goal, and calls the next goal. X = bob Y = ann ? ; Exit means “success”. 2 1 Redo: parent(bob,ann) ? 2 1 Exit: parent(bob,pat) ? X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  13. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? The invocation number. X = bob Y = ann ? ; Unique for every invocation. 2 1 Redo: parent(bob,ann) ? 2 1 Exit: parent(bob,pat) ? X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  14. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? The index number. X = bob Y = ann ? ; The number of direct ancestors of the goal, 2 1 Redo: parent(bob,ann) ? 2 1 Exit: parent(bob,pat) ? i.e., the current depth of the goal. X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  15. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? X = bob The invocation number increases. Y = ann ? ; 2 1 Redo: parent(bob,ann) ? Now working off of invocation 1 . 2 1 Exit: parent(bob,pat) ? X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  16. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? X = bob The index number remains 1 . Y = ann ? ; 2 1 Redo: parent(bob,ann) ? No direct ancestors of the goal, 2 1 Exit: parent(bob,pat) ? i.e., the current depth of the goal is 1 . X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

  17. Demo trace | ?- parent( tom, X), parent( X, Y). 1 1 Call: parent(tom,_273) ? 1 1 Exit: parent(tom,bob) ? 2 1 Call: parent(bob,_277) ? 2 1 Exit: parent(bob,ann) ? X = bob Y = ann ? ; 2 1 Redo: parent(bob,ann) ? 2 1 Exit: parent(bob,pat) ? X = bob Y = pat ? ; 1 1 Redo: parent(tom,bob) ? 1 1 Exit: parent(tom,liz) ? 2 1 Call: parent(liz,_277) ? 2 1 Fail: parent(liz,_277) ? (1 ms) no

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend