1

I am trying to use jpl to load the same swipl file into different modules. The reason I had to do this is because I want to have a module that I can assert new predicates to, while leave the other untouched. Problem is swipl seems forbidding this,

jpl.PrologException: PrologException: error(permission_error(load, source, 'load.pro'), context(/(load_files, 2), 'Non-module file already loaded into module stable; trying to load into to_mess'))
    at jpl.Query.get1(Query.java:336)
    at jpl.Query.hasMoreSolutions(Query.java:258)
    at jpl.Query.oneSolution(Query.java:688)
    at jpl.Query.hasSolution(Query.java:759)

I have tried to set redefine_module(true) for load_files, but still no go

val query = new Query(s"load_files(${m}:'${loader}', [redefine_module(true)])")
query.allSolutions()

I have been blocked by this for hours, but cannot find a solution online. Can anybody please help??

Sheng
  • 1,697
  • 4
  • 19
  • 33

3 Answers3

0

The module name is arbitrary, I think you could append an increasing integer to the name. Just be sure to keep track of it, to be able to reference the asserted predicates.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Could you be more specific? Do you mean there is a way to load the same file into two different modules? If so, how? – Sheng Aug 14 '13 at 04:48
0

You can use Logtalk running on SWI-Prolog + JPL to easily accomplish having two encapsulation units (objects instead of modules in this case) sharing a common initial definition (the contents of the file you're trying to load in two or more different modules). For the details of using Logtalk + SWI-Prolog + JPL see for example:

https://github.com/LogtalkDotOrg/logtalk3/wiki/Using-Logtalk-with-JPL

For the code sharing implied in your question, one solution is to put the contents of the file in an object and then derive from it (using inheritance) as many object as needed. For a more specific advice I would need more details on what you're trying to accomplish.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • Is logtalk able to load the source files in pure prolog (swipl)? What I want is I have a load.pl file which contains something like :- ['apple.pl', 'orange.pl', 'banana.pl'] . And I want to "consult" load.pl into 2 modules, by writing something like this, consult(stable:'load.pl') . and consult(mess_around:'load.pl') . Apparently jpl does not allow me to do this since the same file cannot be loaded for 2 different modules. – Sheng Aug 14 '13 at 14:34
  • You would need some changes such as encapsulating the contents of your `apple.pl`, `orange.pl`, and `banana.pl` files in one or more objects. That is usually simple to do (one open object directive at the beginning, one close object directive at the end, and some predicate scope directives as object predicates are private by default). – Paulo Moura Aug 14 '13 at 16:25
  • Could you provide a simple example in your code about how to properly load a prolog file. I surrounded my prolog files with object definition, and claimed the public methods, and used logtalk_load to load it through jpl. I then tried to run a query new Query(logtalk::my_data_structure(foo(X))).allSolutions().foreach(println), but failed with a message saying the object "my_data_structure" does not exist. – Sheng Aug 14 '13 at 18:45
  • Never mind. I just realized only logtalk 3 can do full path load. – Sheng Aug 14 '13 at 19:15
  • Correct. Using Logtalk 3 over Logtalk 2 is strongly recommended. Despite the current alpha tag, is better in every way and quite stable. I assume you also renamed the source file to use a `.lgt` or `.logtalk` extension. – Paulo Moura Aug 14 '13 at 22:43
0

From what I can tell, jpl seems not to be always consistent with what you'd achieved in swipl console, even though they are coming from the exact same build.

Reading documentation of load_files/2 again, I ended up with this solution to make things work...

load_files(stable:'load.pl', [register(false)]) .
load_files('load.pl', [register(false)]) .

Note I cannot assert new predicates into module (this works in swipl console, but NOT through jpl), so I just loaded files into a module which is supposed to be stable, and loaded the same set of files again into the prolog vm (no module) directly where I can assert new predicates.

Update: if I assert the new predicate in prolog directly through jpl (no module), the predicate will transcend into modules. This behaves differently from swipl console.

Update: This is wrong - although jpl does not complain or throw exception, the file is actually loaded for just once.

Sheng
  • 1,697
  • 4
  • 19
  • 33