/* Global constraints value_precede and value_precede_chain in Picat. The global constraint value_precede(S,T, X) ensures that the value S precedes the value T in array X if both S and T are in X. The global constraint value_precede_chain(C, X) ensures that the value C[I-1] precedes the value C[I] is the array X if both C[I-1] and C[I] are in X. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 5, X = new_list(N), X :: 1..N, % all_different(X), value_precede(4,3,X), solve(X), println(x=X), fail, nl. go => true. go2 => N = 5, X = new_list(N), X :: 1..N, % value_precede_chain([3,4,1,2,5],X), value_precede_chain(1..N,X), solve(X), println(x=X), fail, nl. value_precede_chain(C, X) => foreach(I in 2..C.length) value_precede(C[I-1], C[I], X) end. % This definition is inspired by % MiniZinc definition value_precede_int.mzn value_precede(S,T,X) => XLen = X.length, B = new_list(XLen+1), B :: 0..1, foreach(I in 1..XLen) % Xis :: 0..1, Xis #= (X[I] #= S), (Xis #=> (B[I+1] #= 1)) #/\ ((#~ Xis #= 1) #=> (B[I] #= B[I+1])) #/\ ((#~ B[I] #= 1) #=> (X[I] #!= T)) end, B[1] #= 0.