mymerge(L, [], L).
mymerge([], L, L).
mymerge([H1 | T1], [H2 | T2], [H1 | Out]) :-
H1 =< H2,
    mymerge(T1, [H2 | T2], Out).
mymerge([H1 | T1], [H2 | T2], [H2 | Out]) :-
    H1 > H2,
    mymerge([H1 | T1], T2, Out).

split([], [], []).
split([X], [X], []).
split([H1 | [H2 | T]], [H1 | L1], [H2 | L2]) :-
    split(T, L1, L2).
mysort([], []).
mysort([X], [X]).
mysort([X1 | [X2 | L]], O) :-
    split([X1 | [X2 | L]], Left, Right),
    mysort(Left, SLeft),
    mysort(Right, SRight),
    mymerge(SLeft, SRight, O).

profession(smith).
profession(baker).
profession(carpenter).
profession(tailor).
professions([(smith,Ps), (baker, Pb), (carpenter,Pc), (tailor, Pt)],
             [(son_of(smith),Pss), (son_of(baker),Pbs), (son_of(carpenter), Pcs), (son_of(tailo
  profession(Ps), profession(Pb), profession(Pc), profession(Pt),
  profession(Pss), profession(Pbs), profession(Pcs), profession(Pts),
  % Profession is different from name
  Ps \= smith,
  Pb \= baker,
  Pc \= carpenter,
2
Pt \= tailor,
% Son’s professions are different from name
Pss \= smith,
Pbs \= baker,
Pcs \= carpenter,
Pts \= tailor,
% No son has the same profession as his father
Ps \= Pss,
Pb \= Pbs,
Pc \= Pcs,
Pt \= Pts,
% Baker has the same profession as Carpenter’s son
Pb = Pcs,
% Smith’s son is a baker
Pss = baker,
% Professions don’t repeat
Ps \= Pb, Ps \= Pc, Ps \= Pt, Pb \= Pc, Pb \= Pt, Pc \= Pt,
Pss \= Pbs, Pss \= Pcs, Pss \= Pts, Pbs \= Pcs, Pbs \= Pts, Pcs \= Pts.