# CLP(FD) labeling/2 predicate

**URL:** <https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847>\
**Category:** Predicate\
**Tags:** clpfd\
**Created:** [September 15, 2023, 3:25am UTC](https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847 "2023-09-15T03:25:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [September 15, 2023, 3:25am UTC](https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847/1 "2023-09-15T03:25:28Z")

</div>

Hi,

I cannot figure out how the **ff** option works for the labeling/2 predicate

```prolog
X in 1..5, Y in 1..2, X + Y #= 6, labeling([ff],[X,Y]).
X = 4,
Y = 2
X = 5,
Y = 1

```

I would have expected solution X=5 and Y=1 to come up first. There is no difference if I use **lefmost** instead of **ff**

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [September 15, 2023, 8:47am UTC](https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847/2 "2023-09-15T08:47:44Z")

</div>

You could check the code: [https://github.com/SWI-Prolog/swipl-devel/blob/06aed1f8902aaba51ae6818e18999bd99da2ec46/library/clp/clpfd.pl#L1724](https://github.com/SWI-Prolog/swipl-devel/blob/06aed1f8902aaba51ae6818e18999bd99da2ec46/library/clp/clpfd.pl#L1724)

---

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [September 15, 2023, 8:49am UTC](https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847/3 "2023-09-15T08:49:01Z")

</div>

So one of the benefits of constraints, are that as you add new constraints, they can further constrain existing variables via constraint propagation. Prior to `X+Y #= 6`, X had 5 possible values and Y had 2 possible values. Labeling with `ff` at that point would try and restrict values for Y first as it has the smallest domain. However, once you added the constraint X+Y #= 6, clpfd is smart enough to reduce the domain of X to just 2 possible values. If you run your statement without labeling you will see:

```prolog
?- X in 1..5, Y in 1..2, X + Y #= 6.
Y in 1..2,
X+Y#=6,
X in 4..5

```

So now both variables have the same domain size of two possible values. X is 4 or 5, Y is 1 or 2.

Now if you look at the help for labeling options:

> _First fail_ . Label the leftmost variable with smallest domain next, in order to detect infeasibility early. This is often a good strategy

Then you should see why it try the values of X first rather than Y.

HTH.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [September 15, 2023, 11:27pm UTC](https://swi-prolog.discourse.group/t/clp-fd-labeling-2-predicate/6847/4 "2023-09-15T23:27:46Z")

</div>

Oh yes of course, labeling takes place after constraints propagation. It was a stupid question.

Thank you very much for your answer.
