10-prolog-part2 - CSE 452: Programming Languages Logical...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 452: Programming Languages Logical Programming Languages Part 2 2 Organization of Programming Languages-Cheng Prolog Statements ? Prolog statements consist of facts, rules, and queries. ? Example of facts (written as headless Horn clauses) male(tony). male(greg). female(nikki). female(karen). ? Example of rules (written as headed Horn clauses) sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X \= Y. ? Facts and Rules are stored in a file with extension .pl 3 Organization of Programming Languages-Cheng Loading the Knowledge Base ? Suppose the facts and rules are stored in a file called c:\classes\prolog\gender.pl ? To load the file: | ?- ['C:/classes/prolog/gender']. or | ?- consult('C:/classes/prolog/gender').
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 4 Organization of Programming Languages-Cheng Prolog Statements ? Goal/Query statements ? Also in the form of headless Horn clauses | ?- male(tony). yes | ?- female(X). X = nikki ? ; X = karen yes 5 Organization of Programming Languages-Cheng Query/Goal Statement ? A query can just be an assertion of a fact ? Prolog will try to establish whether that fact can be shown to be true – 1 ? Alternatively, a query can contain variables ? Prolog will try to find values for the variables that make the query true ? Variables are written with initial uppercase letters. ? Prolog will print the variable values one by one. After each one, type ? “;” to see the next value (solution), ? “RETURN” to accept the last value (solution), or ? “a” to print all values 6 Organization of Programming Languages-Cheng Inference Process ? Given a goal/query, how does Prolog match the goal against the propositions in the database? ? In general, there are two approaches: ? Forward chaining (bottom-up resolution) ? System begin with the facts and rules of the database and attempt to find a sequence of matches that lead to the goal ? Backward chaining (top-down resolution) ? System begin with the goal and attempts to find a sequence of matching propositions that lead to some set of original facts inthe database ? Prolog implementation uses backward chaining
Background image of page 2
3 7 Organization of Programming Languages-Cheng Inference Process (Example) ? Example: ? Given the following facts and rules: father(bob). man(X) :- father(X). ? Given the following goal/query: man(bob). ? Forward chaining: ? Start with the first proposition: father(bob). ? Goal is inferred by matching father(bob) to right side of the second rule ? This leads to the goal man(bob). 8 Organization of Programming Languages-Cheng Inference Process (Example) ? Example: ? Given the following facts and rules: father(bob). man(X) :- father(X). ? Given the following goal/query: man(bob). ? Backward chaining: ? Start with the goal: man(bob). ? Match the goal to the left-hand side of second proposition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 452 taught by Professor Cheng during the Fall '05 term at Michigan State University.

Page1 / 14

10-prolog-part2 - CSE 452: Programming Languages Logical...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online