Question: Okay, a before;

```
Array (
    'home' => array('order' => 1),
    'about' => array(),
    'folio' => array('order' => 2),
    'folio/web' => array('order' => 2),
    'folio/print' => array('order' => 1)
    'contact' => array('order' => 2)
)

```

And a desired after;

```
Array (
    'home' => array('order' => 1),
    'contact' => array('order' => 2),
    'folio' => array('order' => 2),
    'folio/print' => array('order' => 1),
    'folio/web' => array('order' => 2),
    'about' => array()
)

```

I know, horrific (don't ask!)

See how the slash in the key indicates children, and how the order is nested accordingly? And items without orders are simply shifted to the bottom.

But also how multiple 'same level' items with the same order are merely sorted by key?

Answer: 
I believe I figured it out...

```
s --> x.
s --> a,d.
s --> a,s,d.

x --> [].
x --> b,b,x,c,c.

a --> [a].
b --> [b].
c --> [c].
d --> [d].

?- s([],[]).
Yes

?- s([a,b,c,c,d],[]).
No

?- s([a,a,a,b,b,c,c,d,d,d],[]).
Yes

```

It's amusing to look at the solution and think, "I racked my brain over *that*?" But I guess that's half the fun of learning something new, especially when it's something like logic programing coming from an imperative programming background.
How about something like:

```
n(L,N) --> n(L,N,0).

n(_,N,N) --> [], !.
n(L,N,K) --> L, {K1 is K + 1}, n(L, N, K1).

abbccd(N,M) -->
    {M1 is 2*M},
    n("a",N),
    n("b",M1),
    n("c",M1),
    n("d",N).

gen :-
    forall((
           between(1,4,N),
        between(1,4,M),
        phrase(abbccd(N,M),S),
        string_to_atom(S,A)
           ),
           writeln(A)).

```

executing:

```
 ?- gen.
abbccd
abbbbccccd
abbbbbbccccccd
abbbbbbbbccccccccd
aabbccdd
aabbbbccccdd
aabbbbbbccccccdd
aabbbbbbbbccccccccdd
aaabbccddd
aaabbbbccccddd
aaabbbbbbccccccddd
aaabbbbbbbbccccccccddd
aaaabbccdddd
aaaabbbbccccdddd
aaaabbbbbbccccccdddd
aaaabbbbbbbbccccccccdddd
true.

```