I'm trying to call the following at the top of my prolog file.
:- dynamic openList/1, dynamic closedList/1.
But this results in the following syntax error.
syntax error: . or operator expected after expression
I can't figure out what I'm doing wrong?
Thanks in advance.
In ISO Prolog only the following forms are legal:
:- dynamic(openList/1).
:- dynamic(closedList/1).
or
:- dynamic([openList/1,closedList/1]).
or (strangely, and not recommended)
:- dynamic((openList/1,closedList/1)).
Some Prologs will also allow (not portable)
:- dynamic openList/1, closedList/1.
Related
I've never written in Prolog before. I have to provide facts so that when it runs it displays:
?- subset([a,b],[a,c,d,b]).
true.
?-include([],[a,b]).
true.
So far, I've written this:
subset([],_Y).
subset([X|T],Y):- member(X,Y),subset(T,Y).
But include doesn't work when I write include([],[a,b]). . It shows this:
ERROR: toplevel: Undefined procedure: include/2 (DWIM could not correct goal)
Any help would be appreciated. Thanks
You get the error because you didn't define the predicate include/2. Your given example looks like include/2 should be describing the same relation as subset/2. So you can either rename your definition from subset/2 to include/2 and then run the query or you can use subset/2 to define include/2:
include(X,Y) :-
subset(X,Y).
Note that in order to use member/2 you have to use library(lists). However, in some Prolog systems (e.g. SWI) this library includes a predicate subset/2 thus leading to a warning when you consult your source file:
Warning: ...
Local definition of user:subset/2 overrides weak import from lists
If you want to implement your own version of subset/2 anyway and not get this warning, you can rename your predicate or not use library(lists) and implement your version of member/2, for example:
subset([],_Y).
subset([X|T],Y) :-
element_in(X,Y),
subset(T,Y).
element_in(X,[X|_]).
element_in(X,[Y|Ys]) :-
dif(X,Y),
element_in(X,Ys).
I'm having a lot of fun using Logtalk, but ran into an issue using phrase_from_file. Specifically, my case looks something like this:
:- object(scan_parser).
:- public(scanlist//1).
scanlist([Scan|Scans]) --> scan(Scan), dcg_basics:blanks, scanlist(Scans).
scanlist([]) --> [].
:- public(scan_file/2).
:- mode(scan_file(+filename, -scans), one).
scan_file(Filename, Scans) :- pio:phrase_from_file(scanlist(Scans), Filename).
...
:- end_object.
The trouble is all in that call to phrase_from_file. It's unable to find scanlist, presumably because it is local to this object, so I get this error:
?- scan_parser::scan_file('input.txt', Scans).
ERROR: phrase/3: Undefined procedure: pio:scanlist/3
But, if I try to aggrandize it with a module reference like so:
scan_file(Filename, Scans) :- pio:phrase_from_file(::scanlist(Scans), Filename).
I get this error:
?- scan_parser::scan_file('input.txt', Scans).
ERROR: phrase/3: Undefined procedure: pio: (::)/3
Same if I use pio:phrase_from_file(this::scanlist(Scans), Filename) or pio:phrase_from_file(scan_parser::scanlist(Scans), Filename). If I use a single colon instead in emulation of SWI's module facility, I get messages like ERROR: phrase/3: Undefined procedure: scan_parser:scanlist/3.
I assume that the problem here is that SWI's PIO library is trying to construct something to hand to phrase and it's just not intelligent enough. But this is something that comes up for me a lot, using phrase_from_file/2, and I'm sure there will be other times I want to excavate something from SWI's library and borrow it. What's the right way forward? I'd like to preserve the encapsulation of Logtalk as much as possible.
Thanks!
I'm designing a general solution for Logtalk 3.x to support Prolog module meta-predicates that take closures as meta-arguments. Meanwhile, can you try the following (ugly) workaround:
% ensure the module is loaded
:- use_module(library(pio)).
:- object(scan_parser).
% override the non-standard meta-arguments declarations
:- meta_predicate(pio:phrase_from_file(2,*)).
:- public(scanlist//1).
scanlist([Scan|Scans]) --> scan(Scan), dcg_basics:blanks, scanlist(Scans).
scanlist([]) --> [].
:- public(scan_file/2).
:- mode(scan_file(+filename, -scans), one).
scan_file(Filename, Scans) :- pio:phrase_from_file(user:scan_parser_scanlist(Scans), Filename).
{scan_parser_scanlist(Scans, A, B)} :-
phrase(scanlist(Scans), A, B).
...
:- end_object.
I cannot test as you only posted part of the object code.
I'm trying to define an infix operator but I keep getting errors. I'm using GNU Prolog 1.4.
I tried this:
[user].
op(35, xfx, =>).
ctrl-D
But got the error "native code procedure op/3 cannot be redefined (ignored)"
I also tried op(35, xfx, '=>'). and got the same error, and tried doing it without typing in [user]. first, but when I tried to actually use the operator I got an existence_error.
yup you need to run the predicate instead of defining it. To do that just insert :- before your op/3 call.
I'm attempting to parse a "function call" for a language that I am creating, but I am getting:
:30:0 Syntax error: Operator expected
The first line below is where I am getting my error:
Fun(FXs) --> name(F),
ws,
[0'(],
ws,
args(Xs),
ws,
[0')],
{FXs =.. [F, Xs]}.
name(N) --> id(Cs),
{atom_chars(Cs, N)}.
I have exhaustively searched Google for help with errors in DCG parsing, and I have not found anything. An explanation on what I am doing wrong will be helpful, or any resources that would allow me to understand DCG more!
I guess it's Fun: you should write fun instead. SWI-Prolog has an extension to allow CamelCase functors, but must be enabled.
I have this code in Prolog:
dynamic(player_at/1).
player_at(house).
goto(X) :- retract(player_at(house)), assert(player_at(X)).
But I still get this error:
uncaught exception: error(permission_error(modify,static_procedure,player_at/1),retract/1)
when I execute goto(foo).
I've read the dynamic documentation, but I can't figure out how to use it, at least in gprolog. Am I missing something?
Fix the first line by prepending :-:
:- dynamic(player_at/1).
Without :- the line would dreefine predicate dynamic/1, instead of executing the existing dynamic predicate.
Other prolog implementations (but not gprolog) support this as well:
:- dynamic player_at/1.