Question: In prolog please As stated previously, you must write your code in a source file and then execute it later while running gprolog. Prolog code
In prolog please
As stated previously, you must write your code in a source file and then execute it later while running gprolog. Prolog code contains facts and rules. The following syntax is used to enter facts, also known as axioms and assertions (begin copying the code in the text file at this point): color(red). This statement enters the fact color is red. The following fact has multiple clauses: ate(albert, hamburger). This statement would be read as albert ate hamburger. By convention, we will place the operator after the first clause. Enter two more facts: ate(joe, salad). ate(jill, hamburger). Rules Rules are encoded with the consequent first, followed by the operator :-, then the clauses of the antecedent separated by commas. For example: fruit(apple) :- color(red). full(X) :- ate(X, hamburger). The first rule is interpreted as: IF color is red THEN apple is fruit. The second rule is interpreted as: IF X ate hamburger THEN X is full. Unlike the first rule, the second rule uses a variable. Variables in Prolog begin with an upper case letter. Invoking full(X) during runtime will check for all objects in the database that ate a hamburger. If you invoke full(jill) it will check if jill ate a hamburger. This is how you will implement functions in Prolog. Queries Enter code into a text editor, save it, then run gprolog and consult your source code (see above for the command to do this). Here is the code repeated for your convenience: color(red). ate(albert, hamburger). ate(joe, salad). ate(jill, hamburger). fruit(apple) :- color(red). full(X) :- ate(X, hamburger). To check if a rule has been proven (e.g. it has fired/it is true/it is in the database) invoke the following command: | ?- fruit(apple). true. which attempts to prove that fruit is apple. Since color is red is fact, the rule fruit(apple) :- color(red). will always be true. This is an example of a ground query. To check if an object is full: | ?- full(joe). false.
We know for a fact that joe ate salad, so he cant be full according to our rule. A non-ground query is a query invocation with a variable in it. Recall that variables start with an uppercase letter. | ?- full(Y). gprolog should respond with the following result: Y = albert It may seem like gprolog has hung at this point, but it is actually stepping through all objects which cause full(Y). to evaluate to true. To step through to the next object you must enter a semicolon, your screen output will look like this: Y = albert; Y = jill This brief introduction should be all that is needed to complete the lab. For additional information, view this link for a Prolog Tutorial. Technical Approach Part 1 Inference Example from Textbook To write a program in Prolog you will write your code in vi or emacs and then use gprolog or swipl to execute your code. Prolog code usually has the extension .pro or .pl. The following rules were discussed in Section 2.6.1 of the text: Rule 1: & Rule 2: & & Rule 3: Rule 4: Rule 5: & Enter these rules into your Prolog source code. Following the books example enter A, B, C, D and E as fact. Verify that all objects are fact except for N. Submit your *.pro source code to the instructor for credit.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
