Skip to content

Warnings for Singleton Variables Modulo Lambdas #17

@kyledewey

Description

@kyledewey

Consider the following code:

test(lambda([], X = 2)) :-
  X = 7.

While X is used in two separate places, these two X's are distinct. The reason why is that for lambdas, we consider variables introduced in the lambda to be local to the lambda, starting from the clause head to the body. As such, the X in the lambda is encountered first, and it is marked local to the lambda. The body is then traversed, and the X in the body is considered local to the body.

Usually, this behavior reflects intended usage. However, this occasionally leads to some anti-patterns. For example, consider the following:

test :-
  X = _,
  Y = lambda([], X = 7),
  call(Y),
  writeln(X).

The seemingly useless X = _ in the above code is necessary to introduce X before the lambda is encountered. Without this, X would be considered local to the lambda, and we'd end up printing out an uninstantiated variable as opposed to 7. Moreover, if we neglected to include the X = _, this wouldn't get flagged as a singleton, as we piggyback off of SWI-PL's singleton checking, which doesn't understand that lambdas introduce a new scope (it sees them only as typical constructors). We should have a version of singleton checking which is aware of the scoping modifications lambdas introduce, to help alleviate this problem.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions