New to Prolog using SWI-Prolog: Questions about type-checking/assertions, findall and others.
Hello,
I am having trouble achieving a few things:
Question 1: Type-checking and assertions. I have the following:
is_cat(a).
is_cat(b).
...
And I find myself checking each time whether what is passed to the predicates really matches one of the cats I have defined, like so:
pred(X) :- is_cat(X), ...
Is there a better, more concise way to do this kind of type-checking on arguments passed to predicates? I find I'm excessively using things like is_cat/1 and is_list/1, etc... to make sure 1) the user doesn't input a wrong type of value at the prompt and 2) that my programming/typo mistakes are detected.
Also, is there a way to have swipl tell me why something returned false? A message like, "pred(foo) is false because is_cat(foo) is false"?
Furthermore, I replaced the implementation of is_cat/1 with the following:
is_cat(X) :- member(X, [a, b, ...]).
Is that a better or worse?
Question 2: Collecting all valid solutions and using findall. I have a predicate that gives me a single answer at a time and I'd like to use this predicate in the implementation of one that provides all possible answers in a list. I stumbled upon findall & co.. I am not sure what +Template here means. I am also not sure what +, -, :, @ and ? mean in SWI-Prolog documentation.
Question 3: Avoiding duplicate solutions. The first implementation doesn't make sense because it is already covered by backtracking if it were written as the second implementation, is that correct?
pred(A, Ba1, B, Ba2) :-
is_foo(A), is_foo(B), A \= B,
is_bar(Ba1), is_bar(Ba2), Ba1 \= Ba2, ...
((on_baz(X, Ba1), on_baz(Y, Ba2), X \= Y);
(on_baz(Y, Ba1), on_baz(X, Ba2), X \= Y)).
pred(A, Ba1, B, Ba2) :-
is_foo(A), is_foo(B), A \= B,
is_bar(Ba1), is_bar(Ba2), Ba1 \= Ba2, ...
on_baz(X, Ba1), on_baz(Y, Ba2), X \= Y.
Thank you very much!
EDIT: Formatting
EDIT: Thank you for the answers! I am struggling with a new issue, this piece of code is not terminating:
foo(L1, L2, L3) :-
member(X, L1), member(Y, L2), member(Z, L3), Z is X + Y, length(L3, 2).
What I'm trying to do is find the values of L3 (lists of 2 elements each) where each element in each answer is the sum of X and Y where X is each element in L1 and Y is each element in L2.
I should clarify that this is working when called this way:
?- foo([1,2,7], [3,4,5], [Y,X]).
Y = 4 ;
X = 4 ;
Y = 5 ;
X = 5 ;
Y = 6 ;
X = 6 ;
Y = 5 ;
...
but not that way, and I am not sure why:
?- foo([1,2,7], [3,4,5], X).
X = [4, _G18743771] ;
X = [_G18743765, 4] ;
^CAction (h for help) ? abort
% Execution Aborted
I would really appreciate help with this one! Thanks!
Posted Jun 25, 2015, 10:31 AM UTC · edited
You seem to be approaching logic programming in a procedural way. With logic programming, the goal is to declare your program logically. So it can be read logically.
cat(felix). cat(max). dog(bob).
meow(X):- cat(X). bark(X):- dog(X).
X can meow if X is a cat or X can bark if X is a dog.
cat(X):- member(X, [felix, max]). meow(X):- cat(X).
Same stuff. Your predicates are sort of like your types. But if you will like to just have meow(X):- stuff, without checking if X is a cat. That will get very ugly.
Thank you, I think I prefer to keep explicitly using predicates to check for everything. This also limits the amount of answers to only valid ones (valid types) that the prompt gives me back.