Can I use freeze/2 to detect a variable being instantiated, for debugging purposes?

I’m using: SWI-Prolog version 8.1.9

I saw this text on the docs page for freeze/2:

https://www.swi-prolog.org/pldoc/doc_for?object=freeze/2

Delay the execution of Goal until Var is bound (i.e. is not a variable or attributed variable). If Varis bound on entry freeze/2 is equivalent to call/1. The freeze/2 predicate is realised using an attributed variable associated with the module freeze . Use frozen(Var, Goal) to find out whether and which goals are delayed on Var.

For example, suppose I want to catch when a variable is first bound, could I do something like:

freeze(VarToWatch, do_something_interesting)

And do_something_interesting might write something out, or I could set a break point on it in the graphical debugger just to use it as an execution “trap”.

Or is the freeze/2 predicate only useful in the coroutining context, which is the topic group it is a member of? I don’t really understand coroutining yet, which is why I am asking.

If not freeze/2, would when/2 work?

https://www.swi-prolog.org/pldoc/doc_for?object=when/2

While I understand the concept of coroutine, I do not have any experience with using freeze/2 in SWI-Prolog. In such cases I search the source code for SWI-Prolog at GitHub and in this case there appears to many examples and test cases.

1 Like

freeze/2 delays a predicate call until the variable is instantiated. (The more general form is when/2)

Attributed variables allow you to do something interesting with unification.
https://www.swi-prolog.org/pldoc/man?section=attvar

2 Likes

2 posts were split to a new topic: How can a foreign language predicate also be a built-in predicate?