SWI-Prolog / issues

Dummy repository for issue tracking
7 stars 3 forks source link

File works correctly when consulted, but compiling gives "Type error: `callable' expected" #89

Closed nick8325 closed 5 years ago

nick8325 commented 5 years ago

Here is my file, funny.pl:

p :- q([]).
q([]) :- !.
q(Pred) :- call(Pred).

The file can be consulted, and indeed p executes correctly:

?- [funny].
true.

?- p.
true.

However, running the compiler gives an error:

% swipl -c funny.pl
ERROR: /home/nick/funny.pl:1:7: Type error: `callable' expected, found `[]' (an empty_list)
ERROR: /home/nick/funny.pl:1:7: Type error: `callable' expected, found `[]' (an empty_list)

This is using version 8.0.2.

JanWielemaker commented 5 years ago

This was a nice puzzle. It results from the system trying to pull in or report possibly undefined predicates. The clause q(Pred) :- call(Pred). causes it to derive that q/1 is a meta predicate. This implies it wants to see what is being called from the q([]) call, but [] is not considered a callable term in SWI-Prolog.

As there is a clause q([]) this looks like an ok program, but after deriving the meta-predicate, q/1 is always called with a qualified (Module:Callable) term and the first clause thus never fires and calling p will indeed result in a type error.

Bottom story: it is arguable that this error is produced and the program is flawed.

nick8325 commented 5 years ago

Ah! Thanks for the explanation, that's all very clear now. I'd forgotten that modules interact with meta-predicates - I can see that q/1 isn't going to work inside a module. Feel free to close the bug report.

(I was originally hoping to define a meta-predicate that takes either a predicate or a list of predicates as an argument, hence the rather odd test case. I suppose that may be doable using module_transparent, but I think it's better for me to change my design instead.)

JanWielemaker commented 5 years ago

Yes, lists of goals is not really a good idea. It can be done, but simply using a conjunction (A,B) if typically better design.