# Assertz in SWISH

**URL:** <https://swi-prolog.discourse.group/t/assertz-in-swish/4542>\
**Category:** Predicate\
**Created:** [October 11, 2021, 3:06am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542 "2021-10-11T03:06:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 3:06am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/1 "2021-10-11T03:06:20Z")

</div>

I am using swish

assertz/1 does not seem to work. Could you confirm?

```prolog
:- dynamic p/1.
?- assertz(p(a)).
true
?- p(a).
false

```

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 11, 2021, 3:45am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/2 "2021-10-11T03:45:46Z")

</div>

Works fine for me:  
 ![a](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/7/7be16e27b854a5b09f17927e9c6bfbda72a13e81.png)

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 4:27am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/3 "2021-10-11T04:27:03Z")

</div>

It works with swipl but not with swish.

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 11, 2021, 4:29am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/4 "2021-10-11T04:29:17Z")

</div>

The picture above is from swish, not swipl.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 4:32am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/5 "2021-10-11T04:32:40Z")

</div>

ok sorry. there is something I misunderstand then. The use of ?- in the prolog program. I ran assertz(p(a)) as a query.

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 11, 2021, 5:01am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/6 "2021-10-11T05:01:12Z")

</div>

usually you would use assert/1 within a predicate, something like:

```prolog
store_my_value(Value) :-
   assert(value(Value)).

do_something_with_values :-
   value(V), 
   format('got value ~w',[V]).

remove_values :-
   retractall(value(_)).

```

then, you could query like this:

```prolog
?- store_my_value(a), store_my_value(b), do_something_with_values.
got value a
true ;
got value b
true.

?- remove_values, do_something_with_values .
false.

```

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 5:05am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/7 "2021-10-11T05:05:17Z")

</div>

Thank you for your patience.

What am I doing wrong ?

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/b/baa71d175c2e06eb5e3b47ccf689cd1c12379082.png)

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 11, 2021, 5:12am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/8 "2021-10-11T05:12:33Z")

</div>

ohh I think you’re right, assert is limited within swish programs (but for some reason allowed as a query). I don’t use swish much, perhaps someone can help out.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 5:16am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/9 "2021-10-11T05:16:29Z")

</div>

Yes that was my conclusion as well 🙂

I hope that this limitation will be lifted soon.

In fact I want to use swish because It is really a great tool for teaching Prolog (I teach an introductory course on Prolog).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 11, 2021, 6:44am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/10 "2021-10-11T06:44:40Z")

</div>

The reason is that in SWISH, each query is isolated from all others. So, the assert and lookup query are executed in isolated environments. Using the dynamic database works fine, but it must all be a activity that results from the same query.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 11, 2021, 6:41pm UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/12 "2021-10-11T18:41:29Z")

</div>

Thank you Jan for your answer.  
Are there any plans to change this? It is difficult to show the value of assertz/1 to students with this behavior.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 12, 2021, 6:51am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/13 "2021-10-12T06:51:14Z")

</div>

> [@albangabillon](#):
>
> Are there any plans to change this? It is difficult to show the value of [assertz/1](https://www.swi-prolog.org/pldoc/doc_for?object=assertz/1) to students with this behavior.

Not really. There is also a great advantage in not having persistent state. Besides making resource management in the server a lot simpler it guarantees that computations repeat consistently.

As for students, just create examples where a single query both asserts and uses the clauses. Besides the fact that most Prolog programmers think you should not teach the dynamic database at all 🙂 I think there surely is value in the dynamic database, mostly when using Prolog as deductive database (often with tabling) or for ILP (Inductive logic programming) or other techniques where rules and/or facts are generated dynamically.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 13, 2021, 7:58am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/14 "2021-10-13T07:58:16Z")

</div>

My aim was to reproduce the behavior of a relational database with query and updates and also extend the database with some deductive capabilities.  
But I got your point. Thank you.

---

<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:** [October 13, 2021, 8:32am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/15 "2021-10-13T08:32:07Z")

</div>

> [@albangabillon](#):
>
> My aim was to reproduce the behavior of a relational database with query and updates and also extend the database with some deductive capabilities.

I have been droning on about this on this forum and here I go again 🙂

(SWI-)Prolog is not a replacement for a relational database without a lot of additional effort, and that’s a pity, but it is how it is. The opposite is also true, a relational database is a terrible replacement for a high-level general purpose language like SWI-Prolog. Of course experience with both is a great exercise.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 13, 2021, 10:16am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/16 "2021-10-13T10:16:26Z")

</div>

> [@Boris](#):
>
> (SWI-)Prolog is not a replacement for a relational database without a lot of additional effort, and that’s a pity, but it is how it is.

What makes you say so? Surely it is a different beast with very different properties. There are a lot of applications that could perfectly well use SWI-Prolog for storing and querying data though. The _lot of additional effort_ is typically setting up some way to access it and setting up some way to make the data persistent (if that is required).

Take a SWISH installation with a small bridge to library(persistency) and you’ve got a database with some cute properties such as being able to write and share programs that are close to the data and access through HTTP from several languages. I have used that setup a couple of times.

You can also take [TerminusDB](https://terminusdb.com/), which runs in SWI-Prolog 🙂 Yes, it does confirm your claim that it is a lot of work 🙂

---

<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:** [October 13, 2021, 10:32am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/17 "2021-10-13T10:32:57Z")

</div>

> [@jan](#):
>
> Take a [SWISH](https://swish.swi-prolog.org/) installation with a small bridge to library(persistency)

Is that possible with the online SWISH or are you implying a different instance?

I would like to use library(persistency) with SWISH online for demonstrating some examples. In particular the loading of an existing persistency file, updating the facts with assert, then saving the file. I think this would also dove tail in with the desires of @albangabillon

If it can’t be done because of the sandbox, perhaps a special predicate that is safe for the sandbox that loads the persistency file.

---

<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:** [October 13, 2021, 10:43am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/18 "2021-10-13T10:43:04Z")

</div>

> [@Boris](#):
>
> (SWI-)Prolog is not a replacement for a relational database without a lot of additional effort

I don’t see it that way. When I think of a relational database I also think of it bringing along SQL because that is how many people are introduced to relational databases and access the data in them.

If one removes the SQL baggage and replaces that with Prolog queries life is better. As you know I am a fan of [library(persistency)](https://www.swi-prolog.org/pldoc/man?section=persistency) and [data normalization](https://en.wikipedia.org/wiki/Database_normalization) and while I do know SQL, given the choice I would much rather use Prolog. Also when I use Prolog with normalized data like an SQL database I don’t pull the data from an SQL database but use library(persistency) instead. While I have not tried moving the persisted data into an SQL database, from my experience with using [Prolog with ODBC](https://swi-prolog.discourse.group/t/swi-prolog-connecting-to-postgresql-via-odbc/2404) it does not seem like a road block or even a lot of additional effort.

One point of note with library(persistency) that seems like a pit fall until you are aware of it is that Prolog terms like [blobs](https://swi-prolog.discourse.group/t/blob-types/4364) don’t persist until they are converted to atoms.

* * *

**EDIT**

For the sake of completeness: [Introductory Prolog tutorial for SQL programmers on Swish](https://swi-prolog.discourse.group/t/ive-put-an-introductory-prolog-tut-for-sql-programmers-on-swish/3636)

Note: I don’t do Prolog queries exactly the same as in the tutorial (think refactoring) but many of the underlying concepts from [Finding all Solutions to a Goal](https://www.swi-prolog.org/pldoc/man?section=allsolutions) are essential.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 13, 2021, 11:07am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/19 "2021-10-13T11:07:58Z")

</div>

The public SWISH instance is by design stateless. If you want to use it as a statefull database you need to write a library that provides the necessary persistency and declare the interface of that library as safe (possibly with checks and balances such as testing the user is authorized).

Public SWISH has a notion of a [data source](https://swish.swi-prolog.org/example/data_source.swinb)

---

<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:** [October 13, 2021, 11:14am UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/20 "2021-10-13T11:14:52Z")

</div>

> [@jan](#):
>
> The public [SWISH](https://swish.swi-prolog.org/) instance is by design stateless.

What are your thoughts on using something like prolog/0 or interactor/0 or a new predicate to open a new REPL in online SWISH that can hold state and accept assert/0 while it is running and then when the REPL ends the state is destroyed. I am thinking something like [Windows Sandbox](https://swi-prolog.discourse.group/t/windows-sandbox/3868) that creates an isolated sandbox that can be updated but when the sandbox is closed all in the sand box is destroyed.

---

<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:** [October 13, 2021, 12:21pm UTC](https://swi-prolog.discourse.group/t/assertz-in-swish/4542/21 "2021-10-13T12:21:35Z")

</div>

> [@jan](#):
>
> What makes you say so? Surely it is a different beast with very different properties.

Well you answered your question in the next sentence.

> [@jan](#):
>
> There are a lot of applications that could perfectly well use [SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual) for storing and querying data though.

Absolutely. Those applications probably don’t need a relational database (Oracle/Postgres/MariaDB) anyway, and probably don’t benefit from using SQL.

This is a very important point that I have also made before: based on my experience, Prolog (Datalog) is a far superior data query language than SQL.

I have my doubts about TerminusDB in terms of what problems it is supposed to solve. But I haven’t had the chance to evaluate it in practice so my opinion is useless.

The only reason I made my off-hand (and probably unnecessary) comment: in the long term, technology adoption is rather hampered by promising too much upfront and failing to deliver. This is my opinion on the matter.

PS: I definitely wasn’t attempting to troll or flame but it seems this is how it turned out, sorry. I will not delete stuff so that it doesn’t look like I am trying to hide something.

[Next page](https://swi-prolog.discourse.group/t/assertz-in-swish/4542.md?page=2)
