ground(T) :-
    nonvar(T), atomic(T),
    !.
ground(T) :-
    nonvar(T), compound(T),
    functor(T, _, N),
    ground(T, N).

ground(T, N) :-
    N \== 0,
    arg(N, T, A),
    ground(A),
    M is N - 1,
    ground(T, M).
ground(T, 0).
