:- style_check(-singleton), making it selective for specific predicates

74 views
Skip to first unread message

Dan

unread,
Aug 10, 2018, 12:43:12 AM8/10/18
to SWI-Prolog
Hi, 

In my program I have some test code where one branch is always true (success) and other branches report errors. Due to the structure of the code, the true branch lacks certain variables, and hence I am getting a singleton error in branch. 

I found the style_check(-singleton) directive to suppress the message, but this suppresses it across the whole module. 

Better, if i could suppress it for a specific predicate only -- say, something like:

:-style_check(-singleton, [required_condition/2]).

So, that other singleton warnings (which are very useful), show up.

just some thought :-)

thanks,

Dan

Anne Ogborn

unread,
Aug 10, 2018, 1:06:04 AM8/10/18
to Dan, SWI-Prolog
turn it off, define that predicate, turn it back on.


Dan

unread,
Aug 10, 2018, 1:18:16 AM8/10/18
to SWI-Prolog
thanks :-)

Dan

Michael BEN YOSEF

unread,
Aug 10, 2018, 2:30:01 AM8/10/18
to SWI-Prolog
The "Singleton variable in branch" often happens because a variable doesn't look like a singleton because it's being "shared" across branches, but it actually is a singleton, because it only occurs once within one of the branches. You can probably fix this by adding an underscore. Here's a silly example:

female(alice).
male(bob).

person_exists :-
        (       female(X)
        ;       male(X)
        ).

This triggers the warning "Singleton variable in branch: X", which you can fix by renaming both occurrences of X, to _.

Dan

unread,
Aug 10, 2018, 2:52:59 AM8/10/18
to SWI-Prolog
Hi Michael,

Thank you.

In my case, the variables are actually bound in the main "body", just prior to the branch, and i am interested in printing out the bound variables. Something like this:

pred(X, Y) :-
   (
      test1(X),
      test2(Y)
   } -> true;
   message([X, Y]).

Trouble here is that X and Y are not bound in the true "branch", which triggers the warning. 

Dan

Boris Vassilev

unread,
Aug 10, 2018, 3:08:18 AM8/10/18
to SWI-Prolog
This looks a bit strange. You should maybe provide a minimal example that we can run.

Here is how I'd write this:

integers(X, Y) :-
    (   integer(X),
        integer(Y)
    ->  true
    ;   format("~w~n", [not_integers(X, Y)])
    ).

This compiles without warnings and does something; I don't know if it does what it should, though.

?- integers(2, 3).
true.

?- integers(2, foo).
not_integers(2,foo)
true.

So without more detail difficult to say.

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Michael BEN YOSEF

unread,
Aug 10, 2018, 3:19:30 AM8/10/18
to SWI-Prolog


On Friday, 10 August 2018 08:52:59 UTC+2, Dan wrote:
In my case, the variables are actually bound in the main "body", just prior to the branch, and i am interested in printing out the bound variables. Something like this:

pred(X, Y) :-
   (
      test1(X),
      test2(Y)
   } -> true;
   message([X, Y]).

I agree with Boris, this doesn't give a singleton warning and should be formatted something like this for clarity:

pred(X, Y) :-
    (
        test1(X),
        test2(Y) ->
        true
    ;   message([X,Y])
    ).

 
Trouble here is that X and Y are not bound in the true "branch", which triggers the warning.

I don't think so. Look for a variable that occurs only once between ( and ; or only once between ; and ).

Reply all
Reply to author
Forward
0 new messages