Prolog() is does not get a new prolog -- possible to create multiple independent instances?

192 views
Skip to first unread message

Stuart Reynolds

unread,
Sep 11, 2019, 7:06:43 PM9/11/19
to pyswip
It seems the Prolog() constructor has side-effects:

from pyswip import Prolog

def test():
prolog = Prolog()
prolog.assertz('is_a(a, b)')
result = list(prolog.query("is_a(a,b)"))
print(result)
assert result == [{}]
test() # OK
test() # Fail

In the second call is_a(a, b) is in the rulebase twice. Is it possible for second call to instantiate an independent Prolog, or an object with an independent rule base and stack?

- Stuart

yuce

unread,
Sep 12, 2019, 1:23:09 AM9/12/19
to pyswip
Only one instance of SWI-Prolog can be created (limitation of SWI-Prolog) but you can create multiple PySwip modules to emulate the effect of multiple SWI-Prolog instances. There's an example here: https://github.com/yuce/pyswip/blob/master/examples/knowledgebase.py

Stuart Reynolds

unread,
Sep 12, 2019, 2:02:25 PM9/12/19
to pys...@googlegroups.com
Thanks!

--
You received this message because you are subscribed to the Google Groups "pyswip" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyswip+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyswip/bd9f00b3-ca57-48bf-8655-f968d7be9965%40googlegroups.com.

Luke Miller

unread,
Mar 1, 2020, 2:43:33 AM3/1/20
to pyswip
Hi, is it possible to use 'consult' with multiple modules somehow? I'd like to load the same .pl file multiple times in my test suite.

R Zhao

unread,
Aug 29, 2020, 11:48:12 PM8/29/20
to pyswip
Would it be possible to support closing the current prolog instance, and then get a clean restart?
I'm imagining a workflow like this:

```
p1 = Prolog()
p1.shutdown()
p2 = Prolog()
...
```

In my use case, each of the queries are independent from each other, so it's very natural to use a clean restart. Previous results and facts shall be discarded to free some resources (not significant, though).

Stuart Reynolds

unread,
Aug 29, 2020, 11:56:25 PM8/29/20
to pys...@googlegroups.com
I don’t think the C library allows it

--


You received this message because you are subscribed to the Google Groups "pyswip" group.


To unsubscribe from this group and stop receiving emails from it, send an email to pyswip+un...@googlegroups.com.


R Zhao

unread,
Aug 30, 2020, 6:14:57 AM8/30/20
to pyswip
Hmm, that's unfortunate... I'll try other workarounds.
Thanks for the reply.

yuce

unread,
Aug 30, 2020, 6:18:09 AM8/30/20
to pyswip
Have you tried to use multiple knowledge bases? There''s an example at: https://github.com/yuce/pyswip/blob/master/examples/knowledgebase.py

Stuart Reynolds

unread,
Sep 1, 2020, 12:35:29 PM9/1/20
to pys...@googlegroups.com
You can assert facts into distinct modules though.

That’s one way of implementing multiple worlds:

mod1:assert(f(1)).

Reply all
Reply to author
Forward
0 new messages