# Are predicates (at the toplevel or even defined in files) by default dynamic?

**URL:** <https://swi-prolog.discourse.group/t/are-predicates-at-the-toplevel-or-even-defined-in-files-by-default-dynamic/2393>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [May 22, 2020, 9:51am UTC](https://swi-prolog.discourse.group/t/are-predicates-at-the-toplevel-or-even-defined-in-files-by-default-dynamic/2393 "2020-05-22T09:51:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [May 22, 2020, 9:51am UTC](https://swi-prolog.discourse.group/t/are-predicates-at-the-toplevel-or-even-defined-in-files-by-default-dynamic/2393/1 "2020-05-22T09:51:08Z")

</div>

Just running:

```
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.32-10-g1885b056f)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
 
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
 
?- assertz(tryit(alpha,beta)).
true.
 
?- tryit(A,B).
A = alpha,
B = beta.

```

I was not expecting this to work without a `dynamic/1` statement.

Similarly, loading a file with

```prolog
a(A,B) :- assertz(tryit(alpha,beta)),tryit(A,B).

```

No problem!

```prolog
?- [test].
true.

?- a(X,Y).
X = alpha,
Y = beta.

```

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [May 22, 2020, 10:06am UTC](https://swi-prolog.discourse.group/t/are-predicates-at-the-toplevel-or-even-defined-in-files-by-default-dynamic/2393/2 "2020-05-22T10:06:14Z")

</div>

In the [“Database” section in the manual](https://www.swi-prolog.org/pldoc/man?section=db), it says:

> Dynamic predicates are predicates for which the list of clauses is modified at runtime using [asserta/1](https://www.swi-prolog.org/pldoc/man?predicate=asserta/1), [assertz/1](https://www.swi-prolog.org/pldoc/man?predicate=assertz/1), [retract/1](https://www.swi-prolog.org/pldoc/man?predicate=retract/1) or [retractall/1](https://www.swi-prolog.org/pldoc/man?predicate=retractall/1).

The way that I understand this is that using any of these means that the predicate is, indeed, dynamic. In particular, for retractall/1, it says:

> If Head refers to a predicate that is not defined, it is implicitly created as a dynamic predicate.

If you hover on footnote 80 in the docs for `retractall/1` it also says,

> The ISO standard only allows using [dynamic/1](https://www.swi-prolog.org/pldoc/man?predicate=dynamic/1) as a _directive_ .

My understanding is that this implies that you can use `dynamic/1` as a goal in a predicate body in SWI-Prolog, in contrast to what the ISO standard allows (and if you try it out you will see that you can, indeed, use `dynamic/1` as a goal, not only as a directive).

I hope I am not misleading you on this.

---

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [May 22, 2020, 12:36pm UTC](https://swi-prolog.discourse.group/t/are-predicates-at-the-toplevel-or-even-defined-in-files-by-default-dynamic/2393/4 "2020-05-22T12:36:06Z")

</div>

Got it. Thank you for the responses.

I have added a comment to:

[https://eu.swi-prolog.org/pldoc/doc\_for?object=(dynamic)/1](https://eu.swi-prolog.org/pldoc/doc_for?object=(dynamic)/1)

Breadcrumbs for the next guy!

Prolog is like The House of Leaves: Bigger Inside!
