1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% 16 June 2003 Bart Demoen, Tom Schrijvers, K.U.Leuven
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 :- module(fibonacci,[fibonacci/0]).
8 :- use_module(library(chr)).
10 :- chr_constraint fibonacci/2, cleanup/1.
12 %% fibonacci(N,M) is true iff M is the Nth Fibonacci number.
14 %% Top-down Evaluation with effective Tabulation
15 %% Contrary to the version in the SICStus manual, this one does "true"
18 fibonacci(N,M1) # ID \ fibonacci(N,M2) <=> var(M2) | M1 = M2 pragma passive(ID).
20 fibonacci(0,M) ==> M = 1.
22 fibonacci(1,M) ==> M = 1.
32 cleanup(L), fibonacci(N,F) <=> L = [N-F|T], cleanup(T).
33 cleanup(L) <=> L = [].
40 SL == [0 - 1,1 - 1,2 - 2,3 - 3,4 - 5,5 - 8,6 - 13,7 - 21,8 - 34,9 - 55,10 - 89,11 - 144,12 - 233,13 - 377,14 - 610,15 - 987].