Swipl-win, 8.3.2, invoke goals at startup

Dear list,

this is my little prolog script:

init :-
    set_prolog_flag(float_overflow, infinity).

:- init.

example :-
    A is 1 + 1.0Inf,
    writeln(A).

Under Windows, Ctrl-x, ctrl-s for saving, then ctrl-c, ctrl-b for compiling/consulting. When I type example. I get

ERROR: Arithmetic: evaluation error: `float_overflow’
ERROR: In:
ERROR: [11] _13436 is 1+1.0Inf
ERROR: [10] example at c:/users/matth/documents/prolog/flag.pl:7
ERROR: [9]

I need to explicitly invoke init. and only then example. works as expected (i.e., prints an infinite number).

Seems like a bug (?)

Best wishes, Matthias

Yes and no. The IDE runs in its own thread and performs the compilation. Prolog flags are thread-specific, where a new thread copies the flags from its parent (actually, they share but implement copy-on-write such that subsequent changes are only visible in the calling thread.

So, this works perfectly fine if you load the program from the thread in which you will run the program. Typically you should load and init code that requires global changes to the flags in the main threads before creating any other threads. And of course it is dangerous in general as parts of your program may assume different settings :frowning:

Ah, of course. Got it. Victim of my old-fashioned mindset in which there’s only one thread (and even that one is lost quite often).

So this works: Double click on swipl, then

?- [flag].
true.

?- example.
1.0Inf
true.

On an unrelated note, the notation [flag]. for consulting a file appears even more old-fashioned :slight_smile:

Thanks for the clarification.

1 Like