Search code examples
prologdcg

Possible bug or omission


Possible bug OR I'm missing something obvious :

 test(struct(X,Y)) --> X,[isa],Y.

When I try to use this rule I get an error.. and it was working the other day !! The listing uses phrase() inside the rule, if memory serves me correctly it should be difference lists !!!

What does your listing() shows ?

What I'm missing ? seems stupid !!! The only thing that seems plausible is that i upgraded SWI recently !! hmm..

Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- consult(test_grammar).
true.

?- listing(test).
test(struct(X, Y), A, B) :-
    phrase(X, A, C),
    C=[isa|D],
    phrase(Y, D, B).

true.

?- phrase(test(T), [this,isa,test]).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [15] '$dcg':call_dcg(user:_9038,[this,isa|...],_9032)
ERROR:   [13] test(struct(_9082,_9084),[this,isa|...],[]) at /my/dev/python3/chatbot/lib/semantics/test_grammar.pl:2
ERROR:   [12] '$dcg':call_dcg(user:test(...),[this,isa|...],[]) at /usr/lib/swi-prolog/boot/dcg.pl:368
ERROR:    [9] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
?- 

Solution

  • You grammar rule should be:

    test(struct(X,Y)) --> [X,isa,Y].
    

    This will give you:

    ?- [user].
    |: test(struct(X,Y)) --> [X,isa,Y].
    |: ^D% user://1 compiled 0.01 sec, 1 clauses
    true.
    
    ?- phrase(test(T), [this,isa,test]).
    T = struct(this, test).