Learn Prolog now Translations-chapter II-Unity and Proof Search-section III, exercises and answers

Source: Internet
Author: User
Tags knowledge base

Exercise 2.1 which groups in the following groups of statements can be unified? If you can combine, give information about the initialization of variables.

1. Bread = bread.

2. ' Bread ' = Bread.

3. ' Bread ' = bread.

4. Bread = Bread.

5. Bread = sausage.

6. Food (bread) = bread.

7. Food (bread) = X.

8. Food (X) = food (bread).

9. Food (bread, X) = food (Y, sausage).

Food (bread, x, beer) = food (Y, sausage, x).

Food (bread, X, beer) = food (Y, Kahuna_burger).

Food (x) = x.

Meal (Food (bread), drink (beer)) = Meal (X, Y).

Meal (Food (bread), x) = Meal (x, drink (beer)).

My answer and the explanation:

1. Bread = bread. Can be oneness, the same atom;

2. ' Bread ' = Bread. Cannot unite because it is a different atom;

3. ' Bread ' = bread. Can be oneness, the same atom;

4. Bread = Bread. can be unity, variable bread initialized to bread;

5. Bread = sausage. Cannot unite because it is a different atom;

6. Food (bread) = bread. Cannot unite, one is a complex statement, one is an atom;

7. Food (bread) = x. can be unity, variable X is initialized to food (bread);

8. Food (X) = food (bread). can be unity, variable x is initialized to bread;

9. Food (bread, X) = food (Y, sausage). Can be unity, the variable x is initialized to sausage, and the variable y is initialized to bread;

Food (bread, x, beer) = food (Y, sausage, x). Cannot be a unity, cannot initialize X;

Food (bread, X, beer) = food (Y, Kahuna_burger). Not one, two complex statements of different meta-numbers;

Food (x) = X. Can be a unity, according to the realization of my machine, the variable X is initialized to food (x);

Meal (Food (bread), drink (beer)) = Meal (X, Y). Can be unity, the variable x is initialized to food (bread), the variable y is initialized to drink (beer);

Meal (Food (bread), x) = Meal (x, drink (beer)). Cannot be a unity, cannot initialize X.

Exercise 2.2 Give a knowledge base, and its query based on this Knowledge base, please select those queries can be satisfied, if it can be satisfied, please give the relevant variable initialization information.

The knowledge base is as follows:

house_elf (Dobby). Witch (Hermione). Witch ('mcgonagall'). Witch (Rita_ Skeeter). Magic (x):- house_elf (x). Magic (x):- Wizard (x). Magic (x):-Witch (x).

The query is as follows:

?-Magic (house_elf).

?-wizard (Harry).

?-Magic (Wizard).

?-Magic (' McGonagall ').

?-Magic (Hermione).

and draw a search tree of Magic (Hermione).

My answers and explanations:

1.?-Magic (house_elf). My Prolog implementation will make an error on this issue, because the wizard does not implement this predicate, but the query as a whole is invalid because it is neither a fact nor a push to export;

2.?-wizard (Harry). My Prolog implementation will error because the wizard predicate is not defined in the Knowledge base;

3.?-Magic (Wizard). My Prolog implementation will error because the wizard predicate is not defined;

4.?-Magic (' McGonagall '). Prolog answer True, because magic (' McGonagall '), witch (' McGonagall '), and Witch (' McGonagall ') are the facts defined in the Knowledge base;

5.?-Magic (Hermione). Note that the Hermione is capitalized, which is a variable, so here's the answer:

Hermione = Dobby;

Hermione = Hermione;

Hermione = ' McGonagall '.

The Search tree for Magic (Hermione) is as follows:

Exercise 2.3 is like a mini-thesaurus (that is, information about individual words), and consists of a syntactic rule (which defines a sentence in the following order: a quantifier, a noun, a verb, a quantifier,

a noun) composition of the micro-grammar. If you want to find a reasonable sentence in this knowledge base, how do you make a query? Lists all sentences that conform to the grammar rules.

The knowledge base is as follows:

' Big Kahuna Burger ' ). Word (verb, eats). Word (verb, likes). Sentence (Word1, Word2, Word3, Word4, WORD5):-    Word ( Determiner, Word1),    Word (noun, Word2), Word (    verb, Word3), Word (determiner    , Word4), Word (    noun, WORD5).

My answers and explanations:

You can simply make the following query, and then use the semicolon ";" Query out each possible combination of values:

?-sentence (A, B, C, D, E).

Combine the values of a,b,c,d,e each time (easy to read), and you can draw all the answers as follows: (confined to space, not listed)

A criminal eats a criminal

A criminal eats a ' big Kahuna burger '

A criminal eats every crinimal

A criminal eats every ' big Kahuna burger '

A crinimal likes a crinimal

A crinimal likes a ' Big Kahuna burger '

A crinimal likes every crinimal

A crinimal likes every ' Big Kahuna burger '

...

Exercises 2.4 Complete the following crossword puzzles

There are 6 Latin words below:

Astente, Astoria, Baratto, Cobalto, Pistola, Statale.

They are assigned to the following puzzle Gangzhong:

The following Knowledge base is a simple thesaurus of these words:

Word (Astante, a,s,t,a,n,t,e). Word (Astoria, a,s,t,o,r,i,a). Word (Baratto, b,a,r,a,t,t,o). Word (Cobalto, c,o,b,a,l,t,o ). Word (Pistola, p,i,s,t,o,l,a). Word (Statale, s,t,a,t,a,l,e).

please construct a predicate named CROSSWORD/6 to complete the puzzle Gongge. The first three parameters represent the words on the left-to-right column, and the last three parameters represent the words from top to bottom lines.

My answers and explanations:

The definition of the CROSSWORD/6 predicate is such that the first 6 words must conform to Word defined in the knowledge Base, followed by the principle of the same letter according to the position of the intersection of the lines;

Cannot be equal:

Crossword (V1, V2, V3, H1, H2, H3):-    Word (V1,  _,v1h1,_,v1h2,_,v1h3,_),    Word (V2, _,v2h1,_,v2h2,_,v2h3, _), Word (    V3, _,v3h1,_,v3h2,_,v3h3,_), Word (    H1, _,v1h1,_,v2h1,_,v3h1,_), Word    (H2, _,v1h2,_,v2h2,_, v3h2,_),    Word (H3, _,v1h3,_,v2h3,_,v3h3,_),    V1 \= H1,    V2 \= H2,    V3 \= H3.

The results of running the query are as follows:

?-Crossword (V1, V2, V3, H1, H2, H3).

V1 = Astanto, V2 = cobalto, V3 = pistola, H1 = Astoria, H2 = Baratto, H3 = Statale.

The puzzle Gongru after filling in the results is as follows:

This problem has a little intuitive sense of Prolog programming: I'm just describing and building facts , including the fact that the rules are based on what I've observed, and not telling the computer if the words are specifically found ;

-The way of redemption is in it.

Learn Prolog now Translations-chapter II-Unity and Proof Search-section III, exercises and answers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.