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

**URL:** <https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958>\
**Category:** Help!\
**Created:** [July 15, 2019, 10:12pm UTC](https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958 "2019-07-15T22:12:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [July 15, 2019, 10:12pm UTC](https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958/1 "2019-07-15T22:12:22Z")

</div>

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](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](https://www.swi-prolog.org/pldoc/man?predicate=freeze/2) is equivalent to [call/1](https://www.swi-prolog.org/pldoc/man?predicate=call/1). The [freeze/2](https://www.swi-prolog.org/pldoc/man?predicate=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:

```prolog
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](https://www.swi-prolog.org/pldoc/doc_for?object=when/2)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 15, 2019, 10:26pm UTC](https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958/2 "2019-07-15T22:26:37Z")

</div>

While I understand the concept of [coroutine](https://en.wikipedia.org/wiki/Coroutine), I do not have any experience with using [freeze/2](https://www.swi-prolog.org/pldoc/doc_for?object=freeze/2) in SWI-Prolog. In such cases I [search](https://github.com/SWI-Prolog/swipl-devel/search?q=freeze&unscoped_q=freeze) the source code for SWI-Prolog at GitHub and in this case there appears to many examples and test cases.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 16, 2019, 6:39am UTC](https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958/3 "2019-07-16T06:39:09Z")

</div>

`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](https://www.swi-prolog.org/pldoc/man?section=attvar)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 16, 2019, 11:24am UTC](https://swi-prolog.discourse.group/t/can-i-use-freeze-2-to-detect-a-variable-being-instantiated-for-debugging-purposes/958/4 "2019-07-16T11:24:17Z")

</div>

2 posts were split to a new topic: [How can a foreign language predicate also be a built-in predicate?](https://swi-prolog.discourse.group/t/how-can-a-foreign-language-predicate-also-be-a-built-in-predicate/961)
