# Wiki Discussion: Prolog Many Worlds

**URL:** <https://swi-prolog.discourse.group/t/wiki-discussion-prolog-many-worlds/2747>\
**Category:** Wiki Discussion\
**Created:** [August 4, 2020, 4:30pm UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-many-worlds/2747 "2020-08-04T16:30:00Z")\
**Posts on this page:** 1\
**Showing post:** 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:** [August 4, 2020, 5:25pm UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-many-worlds/2747/2 "2020-08-04T17:25:58Z")

</div>

Before writing the Wiki, think about doing many examples of all of the module predicates and then alternatives such as the way Paulo does Many Worlds in Logtalk, using Tabling, rewriting the module property of a predicate (if that is even possible), etc. Then also lay the foundation for how a theoretical Many Worlds would work and then show how to implement it in SWI-Prolog, hopefully with a clean solution.

Other ideas: ([ref 1](https://swi-prolog.discourse.group/t/threaded-queries-rulebase-independence/1236/23)) ([ref 2](https://swi-prolog.discourse.group/t/threaded-queries-rulebase-independence/1236/24))

> Yes. a clean solution requires some _self_ notion. Logtalk has that by design. In plain (SWI-)Prolog you have some options, roughly:
> 
> - (Mis)use meta predicates. Problem is that this indeed requires a lot of book keeping, although it isn’t that hard to automate that.
> - Use the [SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual) stack introspection to get some goal on the stack from which we can derive _self_ . This is fairly simple to program and “not so bad”. Can be a little slow if the stacks are deep. It is still a design pattern for which I at some plan want better (means faster and cheaper) support.
> - Notably for this case, where the combination of a thread and a module are used, put the _self_ in a global variable, so you get
> 
> ```prolog
> nb_getval(self, Self),
> Self:fact(X).
> 
> ```

Of note: predicates, modules and functors (name/arity pairs) are not garbage collected.  
Includes example code demonstrating issue. ([ref](https://swi-prolog.discourse.group/t/threaded-queries-rulebase-independence/1236/22))

**EDIT**

The idea of [this,self or Me](https://en.wikipedia.org/wiki/This_(computer_programming)) is something that might be needed with one possible way to implement `Many Worlds`.

In thinking about `Many Worlds`, one of the stumbling points I ran into was that I could not find a formal definition of `World`.

The commonality that I am seeing is

1. The use of the word `World` relates back to the Prolog database being a `Closed World`
2. The Worlds are based on the Prolog database of facts and predicates.
3. There is a way to distinguish Worlds.
4. If a comparison is made of two Worlds, there is a difference.

Questions:

1. Should a `World` allow for dynamic predicates? At present that seems reasonable, but in practice may be hard to implement.
2. How should the Worlds be created?
3. Can worlds communicate with other worlds? This can be tricky if worlds are composed from other worlds.
4. Will the garbage collector have to be considered?
5. If a `World` is copied, and worlds have references, is it a copy of the reference or the values in the `World`. This is tricky if the worlds are composed from other worlds. This reminds me of [Retroactive data structure](https://en.wikipedia.org/wiki/Retroactive_data_structure). It also reminds me of [TerminusDB](https://terminusdb.com/).
6. Are there different types of Many worlds? Are the some worlds composed of other worlds? Are all of the worlds flat, not composed of other worlds? Are there worlds that change and become identical to other worlds? Can worlds span new worlds? Can worlds have Sub-worlds? When trying to define the operators of `Many Worlds`, this is reminding me of [Category Theory](https://en.wikipedia.org/wiki/Category_theory) and in trying to identify ways to classify `Many Worlds` this reminds me of [Multiverse](https://en.wikipedia.org/wiki/Multiverse).
7. Do worlds survive backtracking?
8. Do worlds have to be ground?

**EDIT**

[Asked](https://discuss.terminusdb.com/t/can-terminusdb-be-used-to-model-prolog-many-worlds/143?u=ericgt) about Many Worlds at the the [TeminusDB forum](https://discuss.terminusdb.com/). Here is the reply from @gavin.mendel.gleason

> TerminusDB was designed with the idea of many worlds in mind conceptually. Queries occur at a given state of the world, and updates are actually transitions to new worlds. You can also create branches, which allows you to have a tree of worlds. It is even possible to do meta-reasoning about the multiverse by looking at multiple worlds simultaneously in a query.
> 
> Currently our only merge strategy is rebasing which means that our worlds are always only accessible by a tree like branching. However in the future we will introduce proper merges, in which case the associated Kripke structure will be a DAG (directed acyclic graph).

Kripke structure ([Wikipedia](https://en.wikipedia.org/wiki/Kripke_structure_(model_checking)))

> **Click to expand**
>
> [Dynamic Dispatch](https://en.wikipedia.org/wiki/Dynamic_dispatch).
> 
> How do you discover worlds? current\_module/1  
> How do you know about a world once you discover it? predicate\_property/2 e.g. [predicate\_property](https://www.swi-prolog.org/pldoc/man?predicate=predicate_property/2)(\<world\>:Predicates).  
> With tabling the query results to the worlds could be tabled, but tabling AFIAK is a tree structure and not a graph. But maybe it does not matter as the execution of code is a tree and not a graph. Could the execution be a DAG with tabling?
> 
> [Software build](https://en.wikipedia.org/wiki/Software_build), [List of build automation software](https://en.wikipedia.org/wiki/List_of_build_automation_software), [Artifact (software development)](https://en.wikipedia.org/wiki/Artifact_(software_development))  
> JavaScript module systems - See [What are CJS, AMD, UMD, and ESM in Javascript?](https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm)
> 
> Starting to think that the the code that is run is an artifacts of a build process, and that the build process is based on a query with operations specific to creating the artifact. In so thinking there has to be an atom, think the smallest part that can not be divided, which would be either the [head or body](https://www.metalevel.at/prolog/concepts), but not predicate. For the body it would have to be able to select out individual clauses of a predicate. Also this may require the redesign of predicates so that they have no internal use of `;` but instead rely on multiple clauses. The code may even have to be pure for this to work.
> 
> Some of the kinds of operations would be, `select`, `order`, `group` (thinking something like reexport for modules), `where` (thinking predicate indicator or [Type, mode and determinism declaration headers](https://www.swi-prolog.org/pldoc/man?section=modes) ), selection by identity (IIRC clauses have an identity internally). This is looking more like SQL, but I should check out the TerminusDB query language. This does not take into account a temporal variable.
> 
> [Modularity in Mathematics](https://www.andrew.cmu.edu/user/avigad/Talks/toulouse5.pdf)  
> [Locales: a Module System for Mathematical Theories](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.297.1815&rep=rep1&type=pdf)
> 
> Starting to bump into the notion that predicates and facts might need to be handled differently, e.g. [Loading fact files, is this still the preferred means?](https://swi-prolog.discourse.group/t/loading-fact-files-is-this-still-the-preferred-means/2946)
> 
> [Child Modules for Erlang and Prolog](http://www.cs.otago.ac.nz/staffpriv/ok/childmod.htm)

---

_[View the full topic](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-many-worlds/2747)._
