I think that's the case with Qu-Prolog.
Cheers,
Paulo
As Paulo says Qu-Prolog does have occurs check turned on for all
unifications (in principle).
Many years ago we added what seemed to be a simple optimization that
turns out to be very effective in practice. The data structure for
variables takes up two words - one is a pointer and the other is for a
collection of flags (as bits). One of those flags is set when the
variable first appears in a structure.
When a variable is unified with a term that flag is checked - if it is
not set then occurs checking is avoided.
The append example is one in which this trick does not work because the
T in the second clause gets flagged and when the bottom of the recursion
is hit then the unification is done with occurs check.
That makes the complexity of append (as joining) proportional to the
length of the first list + size of the second list.
(In your step 3 occurs checking is not needed because these are new
variables (that haven't been in a structure yet).)
Peter
Any Prolog which distinguishes local variables not occuring in structures
from heap variables can do your optimizations without any adaptation
of the intermediate code.
E.g., in SWI for 2', there is one occurs-check in the head of the fact,
but only if neither the 2nd nor 3rd argument is a local variable.
4'' will always be occurs-checked, because the heads of the
lists will always be allocated on the heap.
Since append([], X,s(X)) and append([X],[],[s(X)]) require the
occurs-check, an optimization in that place would need to take
goals like them into account.
>I have the feeling something could be done
>with a ref count annotation. ...
Possible. One argument against reference counting has been its
inability to detect cycles. But if the occurs-check is always on,
there are no cyclic structures. Not sure if this leads to anything
useful. There are many kinds of reference counting schemes like one bit,
weighted ...
Do you remember Geoff Sutcliffe and TPTP ... ? That was one of the
reasons why he used SWI.
The error mode is definitely the more helpful one. In particular, when
debugging programs.
Something else: I was thinking of a compile time
based occurs_check on/off thing, because it could be
applied selectively to code.
So instead of a flag that would affect the runtime,
if we had a flag that would affect the compile time,
we could for example compile certain modules without
occurs check and others with occurs check.
:- set_prolog_flag(clause_unify_checked, on).
/*
code where we want to have occurs check on
goes here.
*/
:- set_prolog_flag(clause_unify_checked, off).
That smells a lot as trying to mix type-checked and non-type-checked
code as Tom did in his demo type-checker. Complicated stuff and I'd
rather do something with typing :-)
> The problem is that when you have the occurs
> check off and no cyclic terms, your Prolog
> system is anyway not safe in the first place.
>
> I probably don't need to give you examples,
> but here is one:
>
> ?- X=f(X), Y=f(Y), X=Y.
>
> I guess it will hang in 75% of the Prolog
> systems, or is it 95%?
I may hope not. On this example, hanging or crashing systems are ISO
compliant, but I sincerely hope we left this era behind us.
Cheers --- Jan
Goody, goody. Made a little test, running Prolog systems
in default mode (so not explicitly fidling with the occurs
check flag):
?- X=f(X), Y=f(Y), X=Y.
SWI: Succeeds
SICStus: Succeeds
YAP: Succeeds
GNU: Hangs, reacts on ^C
B-Prolog: Hangs, does not react on ^C
Ciao: Memory allocation failed
50% in new era?
All 3 prologs that succeed show the resulting
cyclic terms in a different way...
Bye
Eclipse-Prolog did this. But they removed this recently,
since version 6.
It seems that managing such differences can be very tricky
in larger systems.
But in the meantime since 2011-04 or so SWI has a very nice and new
definition failing only for "new cycles". There are some cases still
awaiting perfection, but its seems really the best way to go.
...
>I probably don't need to give you examples,
>but here is one:
>
> ?- X=f(X), Y=f(Y), X=Y.
>
>I guess it will hang in 75% of the Prolog
>systems, or is it 95%?
Lets look at X=f(X), Y=f(Y), X\=Y. instead, to avoid
issues with printing the answer.
IF, SWI, YAP, SICStus, Ciao, Eclipse all fail finitely
B, GNU, XSB loop
> Lets look at X=f(X), Y=f(Y), X\=Y. instead, to avoid issues with
> printing the answer.
>
> IF, SWI, YAP, SICStus, Ciao, Eclipse all fail finitely
And so does hProlog (also for the original query), again lowering the
% of systems not dealing with it :-)
There is a difference (as Ulrich knows of course) between
unification supports cyclic terms
and
every builtin supports cyclic terms
Lots of systems (perhaps all) are somewhere in between those two.
Cheers
Bart Demoen
--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---