/* Decomposition of global contiguity using DCG in Picat. Cf global_contiguity_regular.pi which use (a decomposition of) regular constraint. There's duplicates so we use table to fix this. Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import dcg_utils. import cp. main => go. % global_cardinality/2: must use table + flatten/1 go ?=> N = 12, % length(Z,N), Z = new_list(N), % X is the proper list, Z is the flattened version All= findall(Z,global_contiguity(_X,Z,[])), println(All), println(len=All.len), nl. go => true. % global_cardinality2. Must use table go2 ?=> N = 12, X = new_list(N), All = findall(X,global_contiguity2(X,[])), println(All), println(len=All.len), nl. go2 => true. % global_cardinality3. % Don't work since many_of/2, any_except/2 are not generators, just parsers! go3 ?=> member(N,1..10), println(n=N), X = new_list(N), global_contiguity3(X,Z,[]), println(x=X), println(z=Z), fail, nl. go3 => true. table global_contiguity(GC) --> zeros(Zs1), ones(Ones), zeros(Zs2), {GC = [Zs1,Ones,Zs2].flatten}. zeros([]) --> []. zeros([0|Zs]) --> [0], zeros(Zs). ones([]) --> []. ones([1|Os]) --> [1], ones(Os). table global_contiguity2 --> zeros2, ones2, zeros2. zeros2 --> []. zeros2 --> [0], zeros2. ones2 --> []. ones2 --> [1], ones2. % table % Ah, many_of/2, any_except/2 are not generators, just parsers % so this doesn't work as a generator. global_contiguity3([Z1,Ones,Z2]) --> {println(start)}, many_of(Z1,"0"), many_of(Ones,"1"),many_of(Z2,"0"). global_contiguity3([]) --> [].