I’ve a small code snippet below. The findit/4 predicate maps a seed ID with a location ID by zig-zagging through some connected categories. Seeds range from 55-67 and from 78-92. You can call it for any seed and it will give you its location: findit(59,Loc)
. You can call it for any location and it will give you its seed: findit(Seed,61).
THE PROBLEM: I want to enumerate all seeds and locations, I expect to be able to do this: findit(Seed,Loc),label([Seed,Loc])
, but for some reason it skips seeds in the range 59-67. Another way to see that is to note this output:
?- findit(Seed,Loc).
Seed in 79..92, <-- OK
2+Seed#=Loc,
Loc in 81..94 ;
Seed in 55..58, <-- WHY NO 59-67?
2+Seed#=_A,
_A in 57..60,
-4+_A#=Loc,
Loc in 53..56.
?- ?- Seed in 59..70, findit(Seed,Loc).
Seed in 59..67, <-- APPEARS WHEN I FORCE DOMAIN OF Seed.
2+Seed#=Loc,
Loc in 61..69.
Here is the code. Any help would be appreciated.
:- use_module(library(clpfd)).
% Data.
c(79-79-14,none,seed).
c(55-55-13,none,seed).
c(50-98-2,seed,soil).
c(52-50-48,seed,soil).
c(0-15-37,soil,fertilizer).
c(37-52-2,soil,fertilizer).
c(39-0-15,soil,fertilizer).
c(49-53-8,fertilizer,location).
c(0-11-42,fertilizer,location).
c(42-0-7,fertilizer,location).
c(57-7-4,fertilizer,location).
findit(location,Src,Src,_) :- !.
findit(Fr,Src,Loc,Seed) :-
c(D-S-L,Fr,To),
Src#>=S,S+L-1#>=Src,
SrcNew #= Src+D-S,
findit(To,SrcNew,Loc,Seed),!.
findit(Fr,Src,Loc,Seed) :-
c(_,Fr,To),
findit(To,Src,Loc,Seed).
findit(Seed,Loc) :-
c(_-S-L,none,To),
Seed #>= S, S+L-1 #>= Seed,
findit(To,Seed,Loc,Seed).