Question

How to recognize A^n B^n language in Prolog without arithmetics and for any A, B where A != B?

With known A = a and B = b we could write

% For each 'a' save 'b' in a list, then check 
% whether constructed list is equal to the rest of input list

anbn(L) :- anbn(L, []). 

anbn(L, L). 

anbn([a|L],A) :- anbn(L, [b|A]).

For any A and B I was thinking of a solution starting with

anbn(L) :- anbn(L, []).

anbn([H|L],[]) :- anbn(L,[H]). % save an element

anbn([H|L], [H|A]) :- anbn(L, [H,H|A]). % make sure front elements are the same

so that the first elements are all the same, but than I don't see an elegant way of checking whether all elements in the rest of the list are the same and different than the elemenets in the front.

I could check whether the rest is as long as the stored list and then whether it only consists of the second type elements but I believe I'm overcomplicating the problem and there exists a short and simple solution.

Was it helpful?

Solution 2

Edit: back to the original solution, and sticking to it:

anbn(List) :- List = [] -> true; List = [A|Rest], a(Rest, A, 0).

a([A|Rest], A, N) :- !, a(Rest, A, s(N)).
a([B|Rest], _, N) :- b(Rest, B, N).

b([B|Rest], B, s(N)) :- b(Rest, B, N).
b([], _, 0).

It is iterative, it does not create choice-points, it is obvious, and it is correct, if all elements of the list are ground.

OTHER TIPS

Use a definite clause grammar.

s(_, _) --> [].
s(A, B) --> [A], s(A, B), [B].

Demo:

?- phrase(s(1, 2), X).
X = [] ;
X = [1, 2] ;
X = [1, 1, 2, 2] ;
X = [1, 1, 1, 2, 2, 2] .

It is common for DCGs that naked variables are rewrapped as phrase/3 during translation. So that one can implement A^n B^n not only for when A and B are terminals, but also when A and B are arbitrary DCGs goals.

Here is the code for that:

 s(_,_) --> [].
 s(A,B) --> A, s(A,B), B.

Here one sees the translation that is done by SWI-Prolog. As one can see the naked variables have been converted to phrase/3 goals:

 ?- listing.
 s(_, _, A, A).
 s(A, C, B, F) :-
    phrase(A, B, D),
    s(A, C, D, E),
    phrase(C, E, F).

Here is a sample run, for terminals A and B:

 ?- phrase(s("alfa","beta"),X), atom_codes(Y,X).
 X = [],
 Y = '' ;
 X = [97, 108, 102, 97, 98, 101, 116, 97],
 Y = alfabeta ;
 X = [97, 108, 102, 97, 97, 108, 102, 97, 98|...],
 Y = alfaalfabetabeta ;
 X = [97, 108, 102, 97, 97, 108, 102, 97, 97|...],
 Y = alfaalfaalfabetabetabeta .

Here is a sample run, for some DCG goals as A and B:

  bit --> "0".
  bit --> "1".

  ?- length(L,8), phrase(s(("(",bit),(bit,")")),L), atom_codes(R,L).
  L = [40, 48, 40, 48, 48, 41, 48, 41],
  R = '(0(00)0)' ;
  L = [40, 48, 40, 48, 48, 41, 49, 41],
  R = '(0(00)1)' ;
  L = [40, 48, 40, 48, 49, 41, 48, 41],
  R = '(0(01)0)' ;
  Etc..

Bye

I think it's simpler than that:

anbn(L) :- append(As, Bs, L), maplist(ab, As, Bs).
ab(a, b).

EDIT: This is easily generalized to arbitrary literals.

anbn(L) :- L = [A|_], append(As, Bs, L), maplist(ab(A), As, Bs).
ab(A, A, B) :- A \== B.

EDIT: after Will Ness noting that's bugged: what I meant was

anbn(L) :- append([A|As], [B|Bs], L), A \= B, maplist(ab(A, B), As, Bs).
ab(A, B, A, B).
anbn( [] ) :- !.
anbn( L ) :- L=[A|_], copy_half( L,L, Z,Z, A,_V). % Z is second half
copy_half( [A|B], [_,_|C], [V|D], Z, A,V) :- !, copy_half( B,C, D,Z, A,V).
copy_half( Z,     [],      [],    Z, A,V) :- A \== V.

Equivalent to

anbn_verboten(L):- L = [] ;
  length(L,N), N2 is N div 2, length(X,N2), length(Y,N2), append(X,Y,L), 
  L=[P|_], Y=[Q|_], P \= Q.`.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top