Differences between SWI-Prolog and GProlog results: Question about excercises in `Learn Prolog Now!`

156 views
Skip to first unread message

Donald Winiecki

unread,
Aug 8, 2015, 7:57:10 AM8/8/15
to SWI-Prolog
I have a question about exercise 2.2 in LPN!.

In particular, it appears to be the case that SWI-Prolog returns different results than does GProlog on question 5 in exercise 2.2.

I acknowledge this to be a very low-importance issue.

Text of the exercise is at the bottom of this message.

SWI-Prolog 7.2.2 returns:
?- magic(Hermoine).
Hermoine = dobby ;
Hermoine = hermoine ;
Hermoine = 'McGonagall' ;
Hermoine = rita_skeeter .

GProlog 1.4.4 returns
?- magic(Hermoine).
Hermoine = dobby ? ;
uncaught exception: error(existence_error(procedure,wizard/1),magic/1)

I understand SWI-Prolog's results, but can't understand why GProlog stops when it hits the rule "magic(X):- wizard(X)." Any pointers are terrifically appreciated!

Text of the exercise follows:

Exercise 2.2

We are working with the following knowledge base:

house_elf(dobby).
witch(hermoine).
witch('McGonagall').
witch(rita_skeeter).
magic(X):- house_elf(X).
magic(X):- wizard(X).
magic(X):- witch(X).

Which of the following queries are satisfied?...

1. magic(housde_elf).
2. wizard(harry).
3. magic(wizard).
4. magic('McGonagall').
5. magic(Hermoine).

Michael BEN YOSEF

unread,
Aug 9, 2015, 7:06:35 AM8/9/15
to swi-p...@googlegroups.com
There is a standard Prolog flag called unknown that controls this behaviour. See the documentation under unknown here.

In general, you shouldn't change that flag, though, because setting it to fail means a mistyped predicate call will fail silently. If you want a predicate to fail when it doesn't have any clauses, use a declaration

:- dynamic(P/N).

where P is the name of the predicate and N is the arity. This tells the system that the predicate should exist, even if it has no clauses.

Donald Winiecki

unread,
Aug 9, 2015, 9:43:36 AM8/9/15
to SWI-Prolog
Thank you for the quick reply!  It appears that SWI-Prolog has the `unknown` flag set to (non-default value) `fail`, while GProlog has it set to (default) `error`.

I built SWI-Prolog without flags and have not knowingly changed this flag in SWI-Prolog even though the doc entry you cite indicates that `unknown` should be set to `error` by default.  I do not understand the remainder of the doc listing for `unknown` that indicates how to set its value, in particular:

"...Using default setup, this implies that normal modules inherit the flag from user, which in turn inherit the value error from system. The user may change the flag for module user to change the default for all application modules or for a specific module. ..."

How do I return SWI-Prolog to default value for `unknown`?

Best wishes,

_don

On Sunday, August 9, 2015 at 5:06:35 AM UTC-6, Michael BEN YOSEF wrote:
There is a standard Prolog flag called unknown that controls this behaviour. See the documentation under unknown here - private.

Jan Wielemaker

unread,
Aug 9, 2015, 11:00:46 AM8/9/15
to Donald Winiecki, SWI-Prolog
Don,

Most definitely the default is 'error' and has always been that way. Maybe
you have a second version installed that has been modified? Where did
you get the source? Which version? There might be an old ~/.plrc
(~/.swiplrc for the latest version). Try

% swipl -F none -f none
?- current_prolog_flag(unknown, V).
V = error

If that is correct, drop "-f none". That will load the default init
file. Check the flag again. Repeat dropping "-F none" if it still
remains a mistery.

Cheers --- Jan

On 08/09/2015 03:43 PM, Donald Winiecki wrote:
> Thank you for the quick reply! It appears that SWI-Prolog has the
> `unknown` flag set to (non-default value) `fail`, while GProlog has it
> set to (default) `error`.
>
> I built SWI-Prolog without flags and have not knowingly changed this
> flag in SWI-Prolog even though the doc entry you cite indicates that
> `unknown` should be set to `error` by default. I do not understand the
> remainder of the doc listing for `unknown` that indicates how to set its
> value, in particular:
>
> "...Using default setup, this implies that normal modules inherit the
> flag from |user|, which in turn inherit the value |error| from |system|.
> The user may change the flag for module |user| to change the default for
> all application modules or for a specific module. ..."
>
> How do I return SWI-Prolog to default value for `unknown`?
>
> Best wishes,
>
> _don
>
> On Sunday, August 9, 2015 at 5:06:35 AM UTC-6, Michael BEN YOSEF wrote:
>
> There is a standard Prolog flag called unknownthat controls this
> behaviour. See the documentation under unknownhere
> <http://www.swi-prolog.org/pldoc/man?section=flags> - private
> <http://www.swi-prolog.org/pldoc/man?section=flags>.
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Donald Winiecki

unread,
Aug 9, 2015, 11:31:21 AM8/9/15
to SWI-Prolog, dwin...@boisestate.edu
Thank you for the speedy response!

In fact, with all that you suggest, the flag is set to `error` on both SWI-Prolog and GProlog.  The same behaviour still persists with the example I provided at the beginning of this thread.

However, I can say that there is no ~/.swiplrc file at root.

I imagine this could be a critical issue.  What should be the contents of a `typical` .swiplrc?

I will also note that I have the following in my ~/.emacs. The version of `prolog.el` is the one suggested in SWI-Prolog's webpages.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; These lines allow a particular Prolog to be run under Emacs.
;;
;; Insert the following lines in your init file--typically ~/.emacs
;; (GNU Emacs and XEmacs <21.4), or ~/.xemacs/init.el (XEmacs
;; 21.4)--to use this mode when editing Prolog files under Emacs:
;;
(setq load-path (cons "/usr/share/emacs24/site-lisp/" load-path)) ; points to prolog.el
(autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
(autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
(autoload 'mercury-mode "prolog" "Major mode for editing Mercury programs." t)
(setq prolog-system 'swi)  ; Specify the system you are using.
                           ; Recognized symbol values are:
                           ; eclipse - Eclipse Prolog
                           ; mercury - Mercury
                           ; sicstus - SICStus Prolog
                           ; swi     - SWI Prolog
                           ; xsb     - XSB <http://xsb.sourceforge.net>
                           ; gnu     - GNU Prolog
                           ; yap     - YAP Prolog
(setq auto-mode-alist (append '(("\\.pl$" . prolog-mode)
                                ("\\.m$" . mercury-mode))
                               auto-mode-alist))
;;
;;  o  insert in your Prolog files the following comment as the first line:
;;
;;       % -*- Mode: Prolog -*-
;;
;;     and then the file will be open in Prolog mode no matter its
;;     extension, or
;;
;;  o  manually switch to prolog mode after opening a Prolog file, by typing
;;     M-x prolog-mode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Best,

_don

Jan Wielemaker

unread,
Aug 9, 2015, 11:39:56 AM8/9/15
to Donald Winiecki, SWI-Prolog
On 08/09/2015 05:31 PM, Donald Winiecki wrote:
> Thank you for the speedy response!
>
> In fact, with all that you suggest, the flag is set to `error` on both
> SWI-Prolog and GProlog. The same behaviour still persists with the
> example I provided at the beginning of this thread.
>
> However, I can say that there is no ~/.swiplrc file at root.

Should be in your home, not root (unless you work as root of course).
There is no need to have one, but you can put directives in there to
make the system behave as you please.

> I imagine this could be a critical issue. What should be the contents
> of a `typical` .swiplrc?
>
> I will also note that I have the following in my ~/.emacs. The version
> of `prolog.el` is the one suggested in SWI-Prolog's webpages.

Maybe first try outside emacs? Anyway, putting this in 'x.pl' and
running swipl x.pl gives:

house_elf(dobby).
witch(hermoine).
witch('McGonagall').
witch(rita_skeeter).
magic(X):- house_elf(X).
magic(X):- wizard(X).
magic(X):- witch(X).

% swipl x.pl
?- magic(housde_elf).
ERROR: Undefined procedure: wizard/1
ERROR: In:
ERROR: [8] wizard(housde_elf)
ERROR: [7] magic(housde_elf) at /home/jan/x.pl:6
Exception: (8) wizard(housde_elf) ?

(the stack context is an example of personal configuration).

--- Jan

>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> <http://www.swi-prolog.org/pldoc/man?section=flags> - private
> <http://www.swi-prolog.org/pldoc/man?section=flags>> - private
> > <http://www.swi-prolog.org/pldoc/man?section=flags
> <http://www.swi-prolog.org/pldoc/man?section=flags> - private
> <http://www.swi-prolog.org/pldoc/man?section=flags>>.

Donald Winiecki

unread,
Aug 9, 2015, 2:02:44 PM8/9/15
to SWI-Prolog, dwin...@boisestate.edu
Thanks!

With your suggestions, I get mostly the same.  Since I have a box-stock configuration the stack is surely different:

With the same toy database offered by Jan (in copied message below) and using SWI-Prolog in the terminal.  The following occurs (essentially the same for magic(housde_elf).):

?- magic(house_elf).
ERROR: magic/1: Undefined procedure: wizard/1
   Exception: (8) wizard(house_elf) ? Unknown option (h for help)
   Exception: (8) wizard(house_elf) ? Unknown option (h for help)
   Exception: (8) wizard(house_elf) ? a


Same in emacs, albeit, with slightly different formatting of the reporting for exceptions/errors. All this is now consistent with GProlog output reported at the start of this thread (though GProlog doesn't ask for continuation and just returns to the Prolog prompt).

But... now I can't repeat the behaviour of SWI-Prolog that I was reporting at the start of this thread (!)...  Even though the systems appear to be performing properly and consistently, colour me confounded.

Best,

_don

 

On Sunday, August 9, 2015 at 9:39:56 AM UTC-6, Jan Wielemaker wrote:
On 08/09/2015 05:31 PM, Donald Winiecki wrote:
> Thank you for the speedy response!
>
> In fact, with all that you suggest, the flag is set to `error` on both
> SWI-Prolog and GProlog.  The same behaviour still persists with the
> example I provided at the beginning of this thread.
>
> However, I can say that there is no ~/.swiplrc file at root.

Should be in your home, not root (unless you work as root of course).
There is no need to have one, but you can put directives in there to
make the system behave as you please.

> I imagine this could be a critical issue.  What should be the contents
> of a `typical` .swiplrc?
>
> I will also note that I have the following in my ~/.emacs. The version
> of `prolog.el` is the one suggested in SWI-Prolog's webpages.

Maybe first try outside emacs?  Anyway, putting this in 'x.pl - private' and
running swipl x.pl - private gives:

house_elf(dobby).
witch(hermoine).
witch('McGonagall').
witch(rita_skeeter).
magic(X):- house_elf(X).
magic(X):- wizard(X).
magic(X):- witch(X).

% swipl x.pl - private
?- magic(housde_elf).
ERROR: Undefined procedure: wizard/1
ERROR: In:
ERROR:    [8] wizard(housde_elf)
ERROR:    [7] magic(housde_elf) at /home/jan/x.pl:6 - private
   Exception: (8) wizard(housde_elf) ?

(the stack context is an example of personal configuration).

        --- Jan

>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> ;; These lines allow a particular Prolog to be run under Emacs.
> ;;
> ;; Insert the following lines in your init file--typically ~/.emacs
> ;; (GNU Emacs and XEmacs <21.4), or ~/.xemacs/init.el (XEmacs
> ;; 21.4)--to use this mode when editing Prolog files under Emacs:
> ;;
> (setq load-path (cons "/usr/share/emacs24/site-lisp/" load-path)) ;
> points to prolog.el
> (autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
> (autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
> (autoload 'mercury-mode "prolog" "Major mode for editing Mercury
> programs." t)
> (setq prolog-system 'swi)  ; Specify the system you are using.
>                            ; Recognized symbol values are:
>                            ; eclipse - Eclipse Prolog
>                            ; mercury - Mercury
>                            ; sicstus - SICStus Prolog
>                            ; swi     - SWI Prolog
>                            ; xsb     - XSB <http://xsb.sourceforge.net - private>

Donald Winiecki

unread,
Aug 10, 2015, 7:21:02 AM8/10/15
to SWI-Prolog, dwin...@boisestate.edu
To follow up on Jan's questions (below) that I did not answer earlier.

I do not have another version of SWI-Prolog on the system on which these things occur (nor on any of my systems).  There is no existing ~/.plrc or ~/.swiplrc .

I retrieved the source from links on the SWI-Prolog website <http://www.swi-prolog.org/build/Debian.html> and built it following directions provided there.  The version on my systems is "SWI-Prolog (Multi-threaded, 64 bits, Version 7.2.2)".  I opted for `stable` rather than `development`.
 
All of the `tests` suggested by Jan (below) were `passed` -- the flag for `unknown` is properly set to `error` in all cases.

Mystery yet to be solved, but I'm turning to myself as the root cause...  If I come to an answer I will report it back here.

Best,

_don

On Sunday, August 9, 2015 at 9:00:46 AM UTC-6, Jan Wielemaker wrote:
Reply all
Reply to author
Forward
0 new messages