Hi Jan,
> On 25 Jun 2018, at 12:27, Jan Wielemaker <
j...@swi-prolog.org> wrote:
>
> Hi Paulo,
>
> On 06/25/2018 12:41 PM, Paulo Moura wrote:
>> Hi,
>> I'm wondering if there's some way of getting the read_term/3
>> predicate to report as singleton variables all variables that start
>> with an underscore (minus, of course, the anonymous variable). For
>> example:
>> ?- read_term(user_input, T, [singletons(L)]). |: a(_X, _Y, _x, _y).
>> T = a(_3612, _3614, _3616, _3618), L = ['_x'=_3616, '_y'=_3618].
>> In my particular case, I ideally would get:
>> | ?- read_term(user_input, T, [singletons(L)]). |: a(_X, _Y, _x,
>> _y). T = a(_A,_B,_C,_D), L = ['_X'=_A,'_Y'=_B,'_x'=_C,'_y'=_D] ? yes
>> The idea of using variables such as _X is a common practice and helps
>> to understand code compared with the alternative of just using
>> anonymous variables. But unfortunately the interpretation of
>> variables that start with an underscore followed by an upper case
>> letter depends on the Prolog system. Is there any acceptable hack or
>> hidden setting to get the behavior illustrated by the second query in
>> SWI-Prolog?
Thanks for the quick reply.
This is a recent issue due to the introducing of parameter variables in Logtalk 2.14.0, which use the syntax _VariableName_. In the case of SWI-Prolog and others that don't interpret _Var as a singleton, typos in parameter variables (that turn them singletons) are currently not detected (unless the programmer uses _varName_).
> Get all variable names and do your own logic?
I did notice the term_singletons/2 built-in predicate in the documentation, which could be used to filter the list returned by the variable_names/2 option. But that requires costly list operations and that would result in a too big overhead when compiling files :(
> ?- read_term(X, [variable_names(L)]).
> |: a(_X, _Y, _x, _y).
>
> X = a(_9354, _9356, _9358, _9360),
> L = ['_X'=_9354, '_Y'=_9356, '_x'=_9358, '_y'=_9360].
>
> As the variable_names option is fairly portable (I hope; you'll know
> better), this should be more portable anyway.
My question did indeed resulted from a nasty portability issue. CxProlog, ECLiPSe, GNU Prolog, JIProlog, and SICStus Prolog (and a few more) report variables such as _X as a singleton while others like SWI-Prolog consider it an anonymous variable (sometimes I call it a "named anonymous variable" :-), which is quite useful for self-documenting code. In the former case, the overhead is smaller as I just need to filter any singletons (if any) that start with an underscore followed by an upper case letter for consistent semantics.
> Note that the current logic is the result of some debates supporting
> languages that do not have the notion of upper and lowercase. The
> current scheme was suggested by Richard O'Keefe AFAIK.
The idea is quite old indeed but I don't recall the historic bits around it.
Cheers,
Paulo