triska / the-power-of-prolog

Introduction to modern Prolog
https://www.metalevel.at/prolog
1.21k stars 75 forks source link

Fibonacci example in Memoization capter does not work 'backwards' #10

Closed brodo closed 1 year ago

brodo commented 5 years ago

Hi, in the memoization chapter, the fibonacci rule runs 'backwars' just fine if you do not use tabling:

2 ?- fibonacci(X, 34).
X = 8 .

If tabling is enabled, it stops working:

4 ?- fibonacci(X, 34).
ERROR: Type error: `user:fibonacci(_2628{clpfd = ...},_2624)' contains attributed variables
ERROR: In:
ERROR:   [10] throw(error(type_error(free_of_attvar,...),context(...,_2870)))
ERROR:    [7] <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.

Additionally, I think it would make sense to mention that one has to load the tabling module before using it:

:- use_module(library(tabling)).
:- table fibonacci/2.

Cheers and thanks for writing the book, Julian

triska commented 5 years ago

Thank you very much for your feedback!

In recent versions of SWI-Prolog, library(tabling) is deprecated. For example, with 7.7.14, I get:

?- use_module(library(tabling)).
Warning: :- table/1 is built-in.  library(tabling) is deprecated.
true.

Please make sure to use recent versions of Prolog systems if you try examples from the book, since I am using many very recent features and generally also use very recent system versions.

The other issue you mentioned is a limitation of SWI-Prolog, please consider filing an issue in that project.

Thank you and all the best! Markus

brodo commented 5 years ago

Thanks! Which Prolog implementation would you recommend?

triska commented 5 years ago

In so far as the book is concerned, I feel it would be inappropriate for me to recommend one system over another: I try to cover topics that are either already commonly supported, or which I think would be interesting and useful (future) additions to all systems, or those on which I would like to encourage convergence and compatibility by increasing their usage in all available Prolog implementations.

It is my hope that the features that your example requires (constraints and tabling) will one day be supported in many systems, so that we all benefit from a set of great Prolog implementations.

I encourage you to file such requirements as issues in the systems you want to use, since that increases the likelihood of them being implemented and improved!