1% ----------------------------------------------------------------------
2% BEGIN LICENSE BLOCK
3% Version: CMPL 1.1
4%
5% The contents of this file are subject to the Cisco-style Mozilla Public
6% License Version 1.1 (the "License"); you may not use this file except
7% in compliance with the License.  You may obtain a copy of the License
8% at www.eclipse-clp.org/license.
9%
10% Software distributed under the License is distributed on an "AS IS"
11% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
12% the License for the specific language governing rights and limitations
13% under the License.
14%
15% The Original Code is  The ECLiPSe Constraint Logic Programming System.
16% The Initial Developer of the Original Code is  Cisco Systems, Inc.
17% Portions created by the Initial Developer are
18% Copyright (C) 1989-2006 Cisco Systems, Inc.  All Rights Reserved.
19%
20% Contributor(s): ECRC GmbH
21% Contributor(s): IC-Parc, Imperal College London
22%
23% END LICENSE BLOCK
24%
25% System:	ECLiPSe Constraint Logic Programming System
26% Version:	$Id: fd_util.pl,v 1.1 2008/06/30 17:43:45 jschimpf Exp $
27% ----------------------------------------------------------------------
28
29/*
30 * SEPIA PROLOG SOURCE MODULE
31 */
32
33/*
34 * FINITE DOMAINS
35 *
36 * IDENTIFICATION:      fd_util.pl
37 *
38 * AUTHOR:		Micha Meier
39 *
40 * DESCRIPTION:         Various utility predicates for the finite
41			domains library. They usually put together
42			several calls of built-in predicates that
43			occur frequently enough.
44
45   CONTENTS:
46			dvar_domain_list(Var, List)
47			dvar_range(Var, Min, Max)
48			dvar_size(Var, Size)
49
50			labeling(List)
51 *
52 */
53
54
55:- module(fd_util).
56
57:- import fd_arith.
58
59
60:- export
61    # /3,
62    dvar_domain_list/2,
63    dvar_range/3,
64    dvar_size/2,
65    labeling/1,
66    outof/2.
67
68
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% 	Domain processing
72%
73%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74
75%
76% return the list of elements in the domain of Var
77%
78dvar_domain_list(_{fd:(fd with domain:D)}, List) :-
79    -?->
80    !,
81    dom_to_list(D, List).
82dvar_domain_list(X, List) :-
83    nonground(X),
84    !,
85    error(4, dvar_domain_list(X, List)).
86dvar_domain_list(A, [A]).
87
88%
89% return the size of a finite term
90%
91dvar_size(Var, Size) :-
92    dvar_domain(Var, Dom),
93    dom_size(Dom, Size).
94
95dvar_range(Var, Min, Max) :-
96    dvar_domain(Var, Dom),
97    dom_range(Dom, Min, Max).
98
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100%
101% 	Composed Predicates
102%
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104
105outof(_, []).
106outof(X, [H|T]) :-
107    X ## H,
108    outof(X, T).
109
110%
111% Cardinality operator
112%
113#(Min, ConstrList, Max) :-
114    Min #<= Max,
115    bool_sum(ConstrList, Sum),
116    length(ConstrList, Length),
117    Sum #>= Min,
118    Sum #<= Max,
119    Min #>= 0,
120    Max #<= Length.
121
122bool_sum([], 0).
123bool_sum([C|L], B+S) :-
124    B isd C,
125    bool_sum(L, S).
126
127
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129%
130% 	Labeling
131%
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134labeling([]) ?- !.
135labeling([X|L]) ?- !,
136    indomain(X),
137    labeling(L).
138labeling(Junk) :-
139    error(5, labeling(Junk)).
140