I'm having some troubles in understanding the following prolog predicate ,
I can understand that it concatenate some chars , also produces the possible lists (first & second goal ) , but I can't understand how it do this ? how it executes ?
domains
i=integer
l=i*
slist=string*
clist=char*
predicates
nondeterm conc(clist,clist,clist).
clauses
conc([],L,L).
conc([H|L1],L2,[H|L3]):-
conc(L1,L2,L3).
The first goal
goal
conc(['a','b'],['c','d'],L).
result
L=['a','b','c','d']
the second goal
goal
conc(L1,L2,['a','b','c']).
result
L1=[], L2=['a','b','c']
L1=['a'], L2=['b','c']
L1=['a','b'], L2=['c']
L1=['a','b','c'], L2=[]
4 Solutions
I'm neither an expert in prolog, nor in logic, but I'll try to explain what I think how it works.
After the call to:
<- conc(['a','b'],['c','d'],L).
Prolog will look for a predicate that matches the signature. In this case that would be:
conc([H|L1],L2,[H|L3])
It tries to resolve the variables with the given data.
|1: H:=['a'], L1:=['b'], L2:=['c','d']
Now it steps into the recursion with these data calling:
<- conc(['b'], ['c','d'], L3).
|2: H:=['b'], L1:=[], L2:=['c','d']
<- conc([], ['c','d'], L3).
The last line causes prolog to use the predicate with the signature:
conc([],L,L).
resolving:
|3: L:=['c','d']
Now Prolog is able to construct the concatinated List handing over L up the recursion stack.
|2: [H|L3]:=['b','c','d']
|1: [H|L3]:=['a','b','c','d']
I hope that is a hint in the right direction. Maybe you should read this article for clarification
Related
I'm studying for an exam and got stuck on one of the prep questions:
Question:
The following Prolog-program is a meta-program. Explain why this
program is a meta-program and give the output to the three questions
to the program:
?- prove(moving_method(bird,M),N).
?- prove(moving_method(ross,M),N).
?- prove(moving_method(kim,M),N).
I'm trying to run the code(on swish.swi-prolog.org) but it only gives me this error message:
Sandbox restriction!
Could not derive which predicate may be called from
call(C)
prove(A,B)
prove(moving_method(bird,A),B)
The code we are given:
:- dynamic moving_method/2, is_a/2.
is_a(bird,animal).
is_a(ross,albatross).
is_a(kim,kiwi).
is_a(albatross,bird).
moving_method(bird,fly).
moving_method(kiwi,walk).
prove(Fact,l):-
Fact,!.
prove(Fact,X):-
Fact=..[Rel,A1,A2],
is_a(A1,SA),
NewFact=..[Rel,SA,A2],
prove(NewFact,X1),
X is X1 + 1.
The error message might be fairly straight forward but how do I fix it? And why is this a meta-program?
Thank you!
why is this a meta-program?
See: SWI-Prolog Meta-Call Predicates
Meta-call predicates are used to call terms constructed at run time.
In this case passing in the predicate to call, Fact, then running it as a goal.
I've got the following knowledge base, which is supposed to add two arguments and give the results:
add(0,X,X).
add(succ(X),Y,succ(R)):- add(X,Y,R).
Now this is my query:
?- add(succ(succ(succ(0))), succ(succ(0)), Result).
The 0 does not unify with the first argument so it goes to the second add/3 clause. Now here is the thing i can't figure out. The book (LPN) tells me that the outermost succ factor is stipped off the first argument, but I can't figure out why? In my mind it adds a succ functor. Could someone please explain why it is stripping it off?
Thanks in advance!
Luuk
Try this at your top level prompt:
?- succ(succ(0)) = succ(X).
Before you type <Enter>, what do you think the solution will be?
The "stripping" that the book talks about happens between the head of the second clause, add(succ(X), ...) and the recursive call add(X, ...).
I don't really see the added value of talking about "stripping" here. What actually happens is that if the first argument to add/3 is a term with the functor succ/1 (so anything at all that looks like succ(<Whatever>), then, X will be unified with this <Whatever>. In the case of the query:
?- add(succ(succ(succ(0))), succ(succ(0)), Result).
<Whatever> is succ(succ(0)), so the X is unified with succ(succ(0)), and this is the first argument to the recursive call to add/3.
I have a predicate "lookupOptions" which returns one by one some lists (Menus).
I'm trying to get it to satisfy the case of multiple inputs. I can return a single set of options as follows, by reading the head of the "list_places" list.
find_options(Restaurant,Town,Menu) :- lookupOptions(Restaurant,H,Menu), list_places(Town,[H|T])
But, I'm not able to get it to iterate.
I have tried a lot of things, these were my best efforts so far.
a) standard enough iteration, but it wont resolve ...
doStuff(X,[],_).
doStuff(Restaurant,[H|T],_):- lookupOptions(Resturant,H,_), doStuff(Restaurant,T,_).
find_options(Restaurant,Town,Menu) :- doStuff(Restaurant,[H|T],Menu), list_places(Town,[H|T]).
b) expanding the goal predicate ...
find_options(_,Town,[H|T],_)
find_options(Restaurant,Town,Menu) :- find_options(Restaurant,Town,[],Menu).
find_options(Restaurant,Town,X,Menu) :- list_places(Town,X).
find_options(Restaurant,Town,[H|T],Menu) :- lookupOptions(Restaurant,[H],Menu), find_options(Restaurant,Town,T,Menu).
Would either of these work ? if the pattern was written correctly. Or if there was an appropriate cut put in place?
Any help most appreciated ...
It's no clear on what you want iterate. Prolog uses backtracking to examine all alternatives, then you should start backtracking if you are after some alternative, or use the all solutions family.
Now I think you want simply declare there could be more find_options(Restaurant,Town,Menu). Then try replacing the head match [H|T] with this:
find_options(Restaurant,Town,Menu) :-
lookupOptions(Restaurant,H,Menu),
list_places(Town, Places),
member(H, Places).
BTW T is a singleton in your original rule. This could be a hint for the need of generalize it.
I was wondering how to do the answer (or first function) to this question in Prolog only using one predicate? The link I'm referring to is here.
Here's an example of what I mean by only calling one predicate:
reverse([X|Y],Z,W) :- reverse(Y,[X|Z],W).
reverse([],X,X).
What are you trying to do and why do you want just one clause for the predicate?
personally I believe that having the ability to write many clauses is one of the great things of prolog and the code is more elegant and readable that way
Anyway, you will probably want to use or. assuming that you have the following clauses:
foo(Arg11,Arg12,Arg13):-
(Body1).
foo(Arg21,Arg22,Arg23):-
(Body2).
foo(Arg31,Arg32,Arg33):-
(Body3).
you should first make sure that every clause has the same arguments (bye bye pattern matching there!):
foo(Arg1,Arg2,Arg3):-
(Body1b).
foo(Arg1,Arg2,Arg3):-
(Body2b).
foo(Arg1,Arg2,Arg3):-
(Body3b).
and then you will use or (;):
foo(Arg1,Arg2,Arg3):-
(Body1b)
; (Body2b)
; (Body3b).
for example,
reverse([X|Y],Z,W):-
reverse(Y,[X|Z],W).
reverse([],X,X).
will become:
reverse(X,Y,Z):-
X = [H|T],
reverse(T,[H|Y],X).
reverse(X,Y,Z):-
X = [],
Z = Y.
and then:
reverse(X,Y,Z):-
(X = [H|T],
reverse(T,[H|Y],X) )
; (X = [],
Z = Y). *%ew ew ugly!*
regarding the example on your first post, there are two different predicates, each having just one clause. the second predicate has 2 arguments while the first one has 1 therefore they are different. The only way to "merge" them would be by simply calling the second one as has_equal_sums(List, _) in the place of the first.
To be honest, I dont see the point of doing this; I doubt you will not get any significant speedup and the code becomes way messier.
Of course, it's your code and there can be restrictions we dont know (that's why I asked what you want to accomplish)
I got stuck to implement a logic. At some instance in my program I have a list say named as List.
The length of this List is variable and I don't know in advance. Now I have to pass this list in a functor to create a fact and I am unable to implement it. For eg:
if List is [first] then it should add the fact functor(first).
if List is [first,second] then it should add the fact functor(first,second).
if List is [first,second,third] then it should add the fact functor(first,second,third).
and so on...
I was trying by =.. but here I am unable to map that variable length constraint. For fixed length I am able to perform but I don't know in advance that how many elements will be there in list.
Any suggestions to implement this logic. Thanks.
I don't quite understand your problem with =.. but this worked for me:
assert_list(List) :-
Term =.. [my_functor|List],
assert(Term).
Note that I use my_functor instead of simply functor because functor/3 is a built-in predicate so you cannot assert ternary functor facts (functor(first, second, third)).
Calling it:
?- assert_list([first,second,third]).
true.
Checking that it works:
?- listing(my_functor).
:- dynamic user:my_functor/3.
user:my_functor(first, second, third).
true.
Note that technically, the different n-ary my_functor/n predicates are not the same predicates. You must use different queries in your program for each n. To circumvent this, you could simply assert the list as one and only argument of my_functor:
?- List = [first, second, third],
assert(my_functor(List)).
true.
?- listing(my_functor).
:- dynamic user:my_functor/3.
user:my_functor([first, second, third]).
true.
My SWI-Prolog version is 5.7.5.