Search code examples
prolog

Why do i get : uncaught exception: error(existence_error(procedure,foggy/0),top_level/0)?


Im going through a tutorial on prolog and I get an error.

In my file i wrote: sunny.

When i run GNU, i write sunny. and get "YES".

When I write foggy. I get an error
uncaught exception: error(existence_error(procedure,foggy/0),top_level/0) instead of "NO", like the tutorial shows.


Solution

  • The tutorial may be making an assumption about a particular Prolog implementation.

    In GNU Prolog, if you make a query and the predicate with that number of arguments does not exist, and you haven't issued any directives to indicate that it exists, then you will get an existence_error. What you need to include in GNU Prolog is information about predicates or facts you plan to query but may not have been asserted. It's thought of as a dynamic fact or predicate, and you would use the dynamic/1 directive.

    So issue the directive, dynamic(foggy/0).:

    $ gprolog
    GNU Prolog 1.4.4 (64 bits)
    Compiled Oct 16 2017, 09:23:33 with gcc
    By Daniel Diaz
    Copyright (C) 1999-2013 Daniel Diaz
    | ?- foggy.
    uncaught exception: error(existence_error(procedure,foggy/0),top_level/0)
    | ?- [user].
    compiling user for byte code...
    :- dynamic(foggy/0).
    
    user compiled, 2 lines read - 122 bytes written, 6200 ms
    
    yes
    | ?- foggy.
    
    no
    | ?-
    

    Note that after entering your last line of code directly using [user]. you must finish by typing ctrl-D to indicate end of input.


    As @false points out in the comment, you can also set the Prolog `unknown` flag. See the [`set_prolog_flag` documentation](http://www.gprolog.org/manual/html_node/gprolog046.html#set-prolog-flag%2F2) for GNU Prolog.
    $ gprolog
    GNU Prolog 1.4.4 (64 bits)
    Compiled Oct 16 2017, 09:23:33 with gcc
    By Daniel Diaz
    Copyright (C) 1999-2013 Daniel Diaz
    | ?- foggy.
    uncaught exception: error(existence_error(procedure,foggy/0),top_level/0)
    | ?- set_prolog_flag(unknown, fail).
    
    (1 ms) yes
    | ?- foggy.
    
    no
    | ?-