Prolog Homework Solution

Prolog Homework Solution:

If you want to pay someone to do my Prolog assignment, then we are able to help, the first background about Prolog.

 

And Prolog is an old programming language and was invented in the early 1970s and stands for Programmation en logique (it was invented in France by Philippe Roussel) or programming in logic if you want it in English.

Likewise, It is more like SQL than a traditional programming language as it is declarative rather than imperative, you give a series of facts and clauses and then can make queries.

 

Similarly, Prolog consists of terms, which can be atoms, numbers, variables, or a compound term. An atom is a general name with no defined value, for example, x, red, “United States”. Numbers are either integers or floating-point (and integers are normally extended precision so can be arbitrary length).

 

A variable begins with an uppercase letter or an underscore, for example, X, Color. A compound term may be a functor (similar to a function in a regular programming language) or a list of items inside [].
A prolog program consists of facts and rules and is started with a query.

 

And A rule is something that is always true if the conditions are matched.
If you are looking for someone to do my Prolog homework then we can help, but if you are just looking for help perhaps the following will be useful.

Do You need Prolog Assignment help?

We can provide the best help. Below is the sample work.

So a fact is a rule with an implied condition of true, so you can either write
cat(tom) :- true.
Or
cat(tom).
To query the program you use
?- cat(tom).
Which returns yes
Or you can ask what cats are there.
?- cat(X).
x = tom
As You can also chain rules, so
animal(X) :- cat(X).

 

Likewise, the program operates in a recursive fashion and can handle more complex rules with multiple rules as clauses.
mother_child(diana, william).
father_child(Charles, William).
mother_child(diana, harry).
father_child(Charles, harry).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).

?- sibling(William, harry)
Yes

 

And, Unfortunately, this would also return Yes if you asked if William was a sibling of William.
You can also add negative rules, so the condition is inverted, so can say
dog(X) :- \+ cat(X).
Which means a dog is not a cat.

Tutors for Help:

Likewise, We can provide a Prolog online tutor if you are having problems with this programming language.
In the AI community Prolog is used more in Europe than America where Lisp is more common. And Some Prolog code was used in the IBM Watson program (the program that won the Jeopardy game on TV).

 

Similarly, Here is an example of the help with Prolog homework we can provide as a Prolog homework solution.
This assignment should be completed using a swi-prolog (use swipe at the command line to execute it). And You need to solve the following puzzle using Prolog.

 

And, there are 3 people A, B, C, and they live in houses numbered 1, 2, 3 (going left to right). We know the following facts:
a) A’s house is to the right of the house painted yellow.
b) B lives in the red house.
c) The greenhouse is not number 3.
Who lives in which house and what color is each house?

 

This type of problem can easily be solved in your head but the task is to write a program to solve it. Thus, You need to solve the problem using generate and test (this is the least sophisticated technique as it uses brute force to try all the possible combinations but it is sufficient to solve this program in a reasonable time).

The skeletal Prolog assignment help should start with the following.
%—— DIRECTIVES …
%
:- set_prolog_flag(toplevel_print_anon, false).

:- set_prolog_flag(toplevel_print_options,[max_depth(100)]).

%—— PEOPLE AND COLOURS …
%
people([a, b, c]).
cols([green, red, yellow]).

%—— THE PREDICATE is_element/2 …

is_element(E,[E|_]).
is_element(E,[_|T]) :- is_element(E,T).

The is_element allows you to check if the first value is contained in the second value.
I?- is element(9,[1,9,8,4]).
true .
?- is element(9,[2,0,1,8]).
false.
?- is element(E,[1,9,8,4]).
E = 1 ;
and E = 9 ;
E = 8 ;
and E = 4 ;

 

Prolog Task 1:

Write a predicate that generates all the pairs of p(Person, Color) called allhousepairs.
?- allhousepairs(Ps)
Ps = [p(a,green),p(a,red),p(a,yellow),p(b,green),p(b,red),p(b,yellow),p(c,green),p(c,red),p(c,yellow)]

 

Task 2:

Write a predicate that generates all the triplets (p(Person1, Color1), p(Person2, Color2), p(Person3, Color3)) called all_triplets.
?- all_triplets(Ts)
Ts = [ (p(a,green),p(b,red),p(c,yellow)), (p(a,green),p(b,yellow),p(c,red)),
…..]

 

Task 3:

Define a predicate that examines a triplet to find which person lives in which colored house, or which color house is occupied by a person.
?- lives pattern((p(b,yellow),p(c,red),p(a,green)), yellow, Person).
Person = b .
?- lives pattern((p(b,yellow),p(c,red),p(a,green)), red, Person).
Person = c .
?- lives pattern((p(b,yellow),p(c,red),p(a,green)), Colour, b).
Colour = yellow .

 

Task 4:

Define a predicate house_info1 to check the first condition a) A’s house is to the right of the house painted yellow.
?- house info1((p(c,red),p(b,yellow),p(a,green))).
true .
?- house info1((p(a,red),p(c,yellow),p(b,green))).
False

 

Task 5:

 

Define a predicate house_info2 to check the second condition b) B lives in the red house.

?- house_info2((p(b,red),p(c,yellow),p(a,green))).
true .
?- house_info2((p(a,red),p(c,yellow),p(b,green))).
False.

 

Task 6:

Define a predicate house_info3 to check the final condition c) The green house is not number 3.

?- house info3((p(a,red),p(c,green),p(b,yellow))).
true.
?- house info3((p(a,red),p(c,yellow),p(b,green))).
False.

 

Task 7:

Define a predicate house_info that checks each of the previous 3 predicates.

?- house info((p(c,yellow),p(a,green),p(b,red))).
true .
?- house info((p(c,red),p(b,yellow),p(a,green))).
False.

 

Task 8:

Now use house_info to verify there is only 1 possible solution.

The finished solution is the type of Prolog homework help we can offer,

%—— DIRECTIVES …

:- set_prolog_flag(toplevel_print_anon, false).

:- set_prolog_flag(toplevel_print_options,[max_depth(100)]).

%—— PEOPLE AND COLOURS …

people([a, b, c]).
cols([green, red, yellow]).

%—— THE PREDICATE is_element/2 …

is_element(E,[E|_]).
is_element(E,[_|T]) :- is_element(E,T).
%

% Task 1

The goal succeeds if PS is a list of all p(P, C) where P is element of People and C is element of Colors,
% where People and Colors are the names of people and colors.
allhousepairs(Ps):- people(People), cols(Colors),
bagof(p(Person, Color), (is_element(Person, People), is_element(Color, Colors)), Ps).

% Task 2

The goal succeeds if Ts is a list of all triplets A, B, C where
% A, B, and C are pairs of people and colors, and no color and person repeats.
all_triplets(Ts):- allhousepairs(AllPairs),
bagof((p(P1, C1), p(P2, C2), p(P3, C3)),
(
is_element(p(P1, C1), AllPairs), is_element(p(P2, C2), AllPairs), is_element(p(P3, C3), AllPairs),
not(P1 = P2), not(P1 = P3), not(P2 = P3),
not(C1 = C2), not(C1 = C3), not(C2 = C3)
),
Ts
).

Task 3

% The goal succeeds id the patter contains p(Person, Colour) at any position.
lives_pattern(((p(Person,Colour), _, _)), Colour, Person).
lives_pattern((_, (p(Person,Colour), _)), Colour, Person).
lives_pattern((_, _, (p(Person,Colour))), Colour, Person).

Task 4

% The goal succeeds id p(a, _) is to the right to p(_, yellow),
% i.e. if a’s house is to the right of the yellow house.
house_info1((_, p(_, yellow), p(a, _))).
house_info1((p(_, yellow), p(a, _), _)).
house_info1((p(_, yellow), _, p(a, _))).

Task 5

The goal succeeds id b’s house is red in the pattern.
house_info2(Pattern):- lives_pattern(Pattern, red, b).

% Task 6

The goal succeeds if the third house is not green.
house_info3((_, _, p(_, Color3))):- Color3 \= green.

% Task 7

The pattern satisfies all the conditions if it satisfies each
% rule house_info1, house_info2, house_info3.
house_info(Pattern):- house_info1(Pattern), house_info2(Pattern), house_info3(Pattern).

Task 8
% Run this:

(here we find all the patterns that assign persons and colors to houses
% and satisfy all the conditions)
% bagof(Pattern, (all_triplets(Ts), is_element(Pattern, Ts), house_info(Pattern)), Patterns).

So contact us if you need Prolog programming homework help, or more likely other programming languages as we provide a full range of services.