Related
I'm beginner in prolog and I need to resolve the following exercise:
Write a 3-predicate prereqchain(P,Q,L) that, given two courses P and Q, returns in L a representation of all the steps on a prerequisite path from P to Q.
For example prereqchain(comp085,itec450,L) should give the answer
L=prerequisite(comp085, comp101,
prerequisite(comp101, comp281,
prerequisite(comp281, itec450))).
Basically, the knowledge data base stand for the prerequisite relationship between the degree's courses. My main problem is, How can I capture the recursive calls trace and put it into variable ? On the other hand, is not really all the trace, because I just need the calls where the variables was replaced with the values.
Thanks a lot!
What's your base case? It's going to look like this:
prereqchain(Start, End, prerequisite(Start, End)) :-
prerequisite(Start, End).
This gives you the following situation:
?- prereqchain(comp281, itec450, L).
L = prerequisite(comp281, itec450).
So this is the first thing you need, now what happens when you ask for prereqchain(comp101, itec450)? You need an inductive case that follows the rest of the chain:
prereqchain(Start, End, prerequisite(Start, Middle, Tail)) :-
prerequisite(Start, Middle),
prereqchain(Middle, End, Tail).
Assume I have two arbitrary lists that represent the first two items of a 3-place predicate:
[anna,berta,charlotte],[charles,bob,andy]
I want to match every item in a third list (the third item of the 3-place predicate) as follows:
[[anna,andy],[berta,bob],[charlotte,charles]]
Basically the items get matched in a sequentially reverse fashion. To match the items in a sequential manner, I've devised the following code:
match([],[],[]).
match([A|At],[C|Ct],[[A,C]|Dt]):-match(At,Ct,Dt).
But this would give me the following:
match([anna,berta,charlotte],[charles,bob,andy],X).
X=[[anna,charles],[berta,bob],[charlotte,andy]]
So I need to reverse the second list somehow. So far, I've altered the code as follows:
match([],[],[]).
match([A|At],[C|Ct],[[A,B]|Dt]):-reverse([C|Ct],[B|Bt]),match(At,Bt,Dt).
But this would continually reverse the second list with each pass. The result would look as follows:
match([anna,berta,charlotte],[charles,bob,andy],X).
X=[[anna,andy],[berta,charles],[charlotte,bob]]
Question:
How do I reverse the second list only ONCE, so the actual results match the desired ones? Or is my approach fundamentally flawed? I'm new to prolog and am currently stymied by this. Any help would be appreciated.
Do exactly what you say: Reverse the list once, and then use the reversed list.
lists_pairs(Ps1, Ps2, Pairs) :-
reverse(Ps2, RPs2),
pairs_keys_values(Pairs, Ps1, RPs2).
You can check out the source code of reverse/2 and pairs_keys_values/3 in any decent Prolog library to see how it is defined.
Sample query and answer:
?- lists_pairs([anna,berta,charlotte], [charles,bob,andy], Ps).
Ps = [anna-andy, berta-bob, charlotte-charles].
I leave converting such pairs to the non-sensible "pair as list" representation as an exercise.
The trick to solving problems that require you to apply a rule only once is to build an auxiliary rule which performs extra steps before and/or after invoking the recursive rule:
match(A, B, R) :- reverse(B, RevB), match_impl(A, RevB, R).
match_impl([], [], []).
match_impl([A|At], [C|Ct], [[A,C]|Dt]) :- match_impl(At, Ct, Dt).
match_impl/3 is your match/3 rule renamed to avoid conflicting with the "top" match/3 rule that includes an auxiliary step.
This is a small followup to #mat's answer.
To aid termination in some cases you could add a redundant same_length_as/3 goal like so:
lists_pairs(Ps1, Ps2, Pairs) :-
same_length_as(Ps1, Ps2, Pairs),
reverse(Ps2, RPs2),
pairs_keys_values(Pairs, Ps1, RPs2).
The auxiliary predicate same_length_as/3 can be defined like this:
same_length_as([],[],[]).
same_length_as([_|As],[_|Bs],[_|Cs]) :-
same_length_as(As,Bs,Cs).
How can I build a predicate in prolog that receives a number and a list, I must insert the number in the list by the tail
I tried inserting the number in the list by the head: insert(H,[P|Q],[H,P|Q]). and it works, but how can I do it by the tail?
Simply use append/3 like this:
?- append([a,b,c,d],[x],List).
List = [a,b,c,d,x].
Inserting at the tail can be done with a two-part recursive rule:
When the list is empty, unify the result to a single-element list with the item being inserted
When the list is not empty, unify the result to a head followed by the result of inserting into the tail of a tail.
English description is much longer than its Prolog equivalent:
ins_tail([], N, [N]).
ins_tail([H|T], N, [H|R]) :- ins_tail(T, N, R).
Demo.
Nobody talked about difference lists yet.
Difference lists
Difference lists are denoted L-E, which is just a convenient notation for a couple made of a list L whose last cons-cell has E for its tail:
L = [ V1, ..., Vn | E]
The empty difference list is E-E, with E a variable. You unify E whenever you want to refine the list.
For example, if you want to add an element X, you can unify as follows:
E = [X|F]
And then, L-F is the new list. Likewise, you can append lists in constant time. If you unify F with a "normal" list, in particular [], you close your open-ended list. During all operations, you retain a reference to the whole list through L. Of course, you can still add elements in front of L with the ususal [W1, ..., Wm |L]-E notation.
Whether or not you need difference lists is another question. They are intereseting if adding an element at the end is effectively a common operation for you and if you are manipulating large lists.
Definite clause grammars
DCG are a convenient way of writing grammar rules in Prolog. They are typically implemented as reader macros, translating --> forms into actual predicates. Since the purpose of grammars is to build structures during parsing (a.k.a. productions), the translation to actual predicates involves difference lists. So, even though both concepts are theoretically unrelated, difference lists are generally the building material for DCGs.
The example on wikipedia starts with:
sentence --> noun_phrase, verb_phrase.
... which gets translated as:
sentence(S1,S3) :- noun_phrase(S1,S2), verb_phrase(S2,S3).
A lot of "plumbing" is provided by the syntax (a little like monads). The object being "built" by sentence/2 is S1, which is built from different parts joined together by the predicates. S1 is passed down to noun_phrase, which builds/extends it as necessary and "returns" S2, which can be seen as "whatever extends S1". This value is passed to verb_phrase which updates it and gives S3, a.k.a. whatever extends S2. S3 is an argument of sentence, because it is also "whatever extends S1", given the rule we have. But, this is Prolog, so S1, S2 and S3 are not necessarly inputs or outputs, they are unified during the whole process, during which backtracking takes place too (you can parse ambiguous grammars). They are eventually unified with lists.
Difference lists come to play when we encounter lists on the right-hand side of the arrow:
det --> [the].
The above rule is translated as:
det([the|X], X).
That means that det/2 unifies its first argument with an open-ended list which tail is X; other rules will unify X. Generally, you find epsilon rules which are associated with [].
All the above is done with macros, and a typical error is to try to call an auxiliary predicate on your data, which fails because the transformation add two arguments (a call to helper(X) is in fact a call to helper(X,V,W)). You must enclose actual bodies between braces { ... } to avoid treating prediates as rules.
Here is an another option.
insert(N,[],[N]).
insert(N,[H|T],[H|Q]) :- conc([H|T],[N],[H|Q]).
conc([],L,L).
conc([H|T],L,[H|Q]) :- conc(T,L,Q).
so I've been working on the following question:
Write a 3-place predicate scalarMult whose first argument is an
integer, whose second argument is a list of integers, and whose third
argument is the result of scalar multiplying the second argument by
the first. For example, the query
?- scalarMult(3,[2,7,4],Result).
should yield
Result = [6,21,12]
Do this with the help of an accumulator and a wrapper predicate.
This is what I have done:
scalarMult(I, List1, List2):- scalarMult1(I, List1, [], List2).
scalarMult1(I,[], A, A).
scalarMult1(I,[H|T], A, Result):- H1 is H*I, scalarMult1(I,T,[H1|A],Result).
The only trouble with this is that it's putting the new elements at the head of the accumulator so I kind of end up with a reversed list (so for the example above, I would get Result = [12,21,6]). Is there any way I could work around this? I tried using reverse in my code but all my attempts fails.
Thanks
using reverse/2 works, actually:
scalarMult(I, List1, List2):- scalarMult1(I, List1, [], T), reverse(T, List2).
but I think the requirement to use an accumulator (really useless here) could be on purpose to verify your level of lists handling.
Noting Carlo's remark about the use of accumulators being for didactical purposes, no accumulator is required for a straight-forward definition of the scalar_multiplication/3 predicate (renamed from scalarMult/3; camel case is not considered good programming style in Prolog):
% first exchange argument orders to take advantage of the first-argument
% indexing that is provided in most Prolog implementations
scalar_multiplication(Scalar, Numbers, ScaledNumbers) :-
scalar_multiplication_(Numbers, Scalar, ScaledNumbers).
% base case; either input list was empty or we finished traversing the list
scalar_multiplication_([], _, []).
% recursive case
scalar_multiplication_([Number| Numbers], Scalar, [ScaledNumber| ScaledNumbers]) :-
ScaledNumber is Number * Scalar,
scalar_multiplication_(Numbers, Scalar, ScaledNumbers).
This is an instance of a common pattern for processing lists. So common that several Prolog implementations provide a second-order predicate (or meta-predicate), usually named map/3 or maplist/3, to handle it.
i need to write a code that should access to list that created. but i don't know how, i read this question: How do I store and access a list within a variable in Prolog?
but it wasn't my problem I create the list with this code:
p([Head|Tail]):-
read(Head),
p([Tail|Taile]).
my_write([Head|Tail]):- write(Head),nl,my_write(Tail).
and i need created list. what should i do ? it's make me confuse.
try
p(L) :-
( read(Head),
Head \= end_of_file
-> p(Tail),
L = [Head|Tail]
; L = []
).
Prolog is designed more for symbolic processing, IO, being based on side effects, it's often error prone.
Please note that read/1 it's actually a very powerful 'parser', being able to process generalized Prolog terms (structures, expressions, list, with arbitrary nesting).
At EOF, SWI-Prolog return the atom 'end_of_file', hence the test you see n the code.