Your program combines many different problems into one predicate. Try to separate them a little. In addition, I assume that you are looking for the maximum list of two or more elements containing identical elements.
Let's start by approximating what you want: Identifying Subscriptions. Do not worry that this is too wide, and we will clarify it later. For this purpose I will use DCG. The nonterminal seq//1 describes an arbitrary sequence.
seq([]) --> []. seq([E|Es]) --> [E], seq(Es).
This is extremely useful not a terminal!
? - phrase ((seq (Prefix), seq (Sublist), seq (Postfix)),
[a, a, b, 2,2,2, a + 1, a + 1, s (1,2)]).
Prefix = Sublist, Sublist = [] ,
Postfix = [a, a, b, 2,2,2, a + 1, a + 1, s (1,2)];
Prefix = [],
Sublist = "a" ,
Postfix = [a, b, 2,2,2, a + 1, a + 1, s (1,2)] ...
Both responses are not expected, we only need 2 or more sublists, so we need to limit this definition a bit. Say, by requiring that a Sublist must contain at least two elements. This is Sublist = [_,_|_] .
? - Sublist = [_, _ | _],
phrase ((seq (Prefix), seq (Sublist), seq (Postfix)),
[a, a, b, 2,2,2, a + 1, a + 1, s (1,2)]).
Sublist = "aa",
Prefix = [],
Postfix = [b, 2,2,2, a + 1, a + 1, s (1,2)];
Sublist = "aab" ,
Prefix = [],
Postfix = [2,2,2, a + 1, a + 1, s (1,2)] ...
The first answer shows the sublist we are looking for. But the second is still incorrect: all elements of the sublist must be equal. The easiest way is to use maplist/2 :
? - maplist (= (_), Es).
Es = [];
Es = [_G221];
Es = [_G221, _G221];
Es = [_G221, _G221, _G221]
There are several places where we could indicate this requirement. I put it as soon as possible:
? - Sublist = [_, _ | _],
phrase ((seq (Prefix),
seq (Sublist), {maplist (= (_), Sublist)},
seq (Postfix)),
[a, a, b, 2,2,2, a + 1, a + 1, s (1,2)]).
Sublist = "aa",
Prefix = [],
Postfix = [b, 2,2,2, a + 1, a + 1, s (1,2)];
Sublist = [2.2] ,
Prefix = "aab",
Postfix = [2, a + 1, a + 1, s (1,2)];
Sublist = [2,2,2],
Prefix = "aab",
Postfix = [a + 1, a + 1, s (1,2)];
Sublist = [2.2] ,
Prefix = [a, a, b, 2],
Postfix = [a + 1, a + 1, s (1,2)];
Sublist = [a + 1, a + 1],
Prefix = [a, a, b, 2,2,2],
Postfix = [s (1,2)];
false
So now all sublists contain the same elements. Alas, we get both [2,2] and [2,2,2] as a sublist. We need only the maximum sublist. So how can we describe what a maximum subselection is?
One way is to look in front of our sublist: there should not be one and the same element of our sublist. Thus, either there is no (epsilon) in front, or a sequence that ends with an element other than ours.
difel(_E,[]). difel(E, Es) :- dif(E,F), phrase((seq(_), [F]), Es).
? - Sublist = [_, _ | _],
phrase ((seq (Prefix), {difel (E, Prefix)},
seq (Sublist), {maplist (= (E), Sublist)},
seq (Postfix)),
[a, a, b, 2,2,2, a + 1, a + 1, s (1,2)]).
Sublist = "aa",
Prefix = [],
E = a
Postfix = [b, 2,2,2, a + 1, a + 1, s (1,2)];
Sublist = [2.2],
Prefix = "aab",
E = 2,
Postfix = [2, a + 1, a + 1, s (1,2)];
Sublist = [2,2,2],
Prefix = "aab",
E = 2,
Postfix = [a + 1, a + 1, s (1,2)];
Sublist = [a + 1, a + 1],
Prefix = [a, a, b, 2,2,2],
E = a + 1,
Postfix = [s (1,2)];
false One wrong answer is less. In the end, still one is hiding.
? - Sublist = [_, _ | _],
phrase ((seq (Prefix), {difel (E, Prefix)},
seq (Sublist), {maplist (= (E), Sublist)},
([] | [F], {dif (E, F)}, seq (_))),
[a, a, b, 2,2,2, a + 1, a + 1, s (1,2)]).
Sublist = "aa",
Prefix = [],
E = a
F = b;
Sublist = [2,2,2],
Prefix = "aab",
E = 2,
F = a + 1;
Sublist = [a + 1, a + 1],
Prefix = [a, a, b, 2,2,2],
E = a + 1,
F = s (1,2);
false
This is not exactly what you wanted: you just need lengths. To do this, add length([_|Prefix],I), length(Sublist,Len) .
So here is the final definition:
plateau (Xs, I, Len): -
Sublist = [_, _ | _],
phrase ((seq (Prefix), {difel (E, Prefix)},
seq (Sublist), {maplist (= (E), Sublist)},
([] | [F], {dif (E, F)}, seq (_))),
Xs)
length ([_ | Prefix], I),
length (Sublist, Len).