"dynamic" predicate in prolog
Asked Answered
L

1

6

If I want to make a rule dynamic so i can use assert after the database file has been loaded, how do i do it? I'm using XSB Prolog at the moment, the file is something like this:

:- dynamic likes/2

likes(mary,tom)

when i try to consult the file with XSB i get an error:

? consult('D:\file.P).
not permitted to assert to static predicatelikes/2
forward continuation...blahblah

Any ideas?

Lifesaver answered 11/3, 2010 at 16:23 Comment(0)
D
8

The dynamic predicate works as you are expecting, so there is something else wrong if it is not working for you.

If test.P looks like this:

:- dynamic likes/2.

likes(mary,tom).

It can be consulted, and then more likes/2 facts can be asserted:

XSB Version 3.2 (Kopi Lewak) of March 15, 2009
[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; scheduling: local; word size: 32]

| ?- consult('test.P').
[Compiling ./test]
[test compiled, cpu time used: 0.0440 seconds]
[test loaded]

yes
| ?- assert(likes(mary, bob)).

yes
| ?- likes(X,Y).

X = mary
Y = tom;

X = mary
Y = bob;
Dhiman answered 12/3, 2010 at 0:16 Comment(3)
thats really rather odd - i do the same thing, and it throws this sort of weird error! I'm trying to understand that "not permitted blah blah..." means!Lifesaver
I just tested the same file with XSB version 3.2, it seems to work fine, but I must use 2.7.1 as something else im using seems to require it(interprolog)..Could you try the same file in 2.7.1 and let me know what you see? thanksLifesaver
I tried using XSB 2.7 and also got the error you did, so the behavior has changed since that version. However, I was able to load the file with load_dyn('test.P') which loads the entire file as dynamic instead of compiling it. (Remove the :- dynamic line from the file.) It is then possible to assert more likes/2 rules in addition to the one from test.P. See here for documentation on loading dynamic code: cs.sunysb.edu/~sbprolog/manual2/node5.htmlDhiman

© 2022 - 2024 — McMap. All rights reserved.