r/prolog•
fredmorcos

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

610
segmond•

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.

2
fredmorcosOP•

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.

1
MrQuimico•

At least in Ciao Prolog, you can use assertions to emulate "type-checking". Keep in mind that assertions or explicit calls to type-checking predicates happen at runtime so it's not proper type checking. But don't worry too much about this, because usually prolog predicates will fail if you don't use the correct types.

You should implement is_cat/1 enumerating all the clauses:

is_cat(a).
is_cat(b).

This is better because modern prolog systems implement clause indexing.

Here you can find the meaning of the predicate definition symbols (+,-,?,...). I hope this helps.

I think that the second predicate is better, but you need to explain what a duplicate is in the context of your problem. For example, does the order matter?

1
fredmorcosOP•

In my case a duplicate does not matter because ordering does not matter. I was trying to assert my own understanding with this question. Thanks!

1
segmond•
1
fredmorcosOP•

Thanks!

1
MrQuimico•

Well I was bored at work so... Can you use another predicate?

bar([], 0).
bar([H|T], N) :- bar(T, N1), N is H+N1.

foo(L1, L2, L3) :- bar(L1,N1), bar(L2,N2), L3 = [N1,N2].
1
fredmorcosOP•

Thank you. It is definitely OK to use another predicate, but this is not equivalent to the version I have.

What I was looking for was all the possibilities of sums using one operand from the first list and the second operand from the second list. As an example:

foo([1,2,3], [3,4,5], X).
X = [4, 4].
X = [4, 5].
X = [5, 5].
X = [5, 6].
X = [6, 6].
...

Also, ultimately I would have liked to change the list length from being exactly 2 to being anything less than some length N.

1
justtotellyouso•

findall/3 works like this:

is_cat(a).
is_cat(b).

findall(X,is_cat(X),Y).
Y=[a,b].

"Find all the X's that satisfy the goal and put them in list Y".

findall(+, :, -) tells what you can do with the parameters. Basically this means that findall(X,is_cat(X),[a,b]) will only return true of false because X must be instantiated and Y is an output variable only.

1
fredmorcosOP•

Thanks!

1