  We prove a general form of the regularity theorem for uniformity norms, and
deduce an inverse theorem for these norms which holds for a class of compact
nilspaces including all compact abelian groups, and also nilmanifolds; in
particular we thus obtain the first non-abelian versions of such theorems. We
derive these results from a general structure theorem for cubic couplings,
thereby unifying these results with the Host-Kra Ergodic Structure Theorem. A
unification of this kind had been propounded as a conceptual prospect by Host
and Kra. Our work also provides new results on nilspaces. In particular, we
obtain a new stability result for nilspace morphisms. We also strengthen a
result of Gutman, Manners and Varj\'u, by proving that a $k$-step compact
nilspace of finite rank is a toral nilspace (in particular, a connected
nilmanifold) if and only if its $k$-dimensional cube set is connected. We also
prove that if a morphism from a cyclic group of prime order into a compact
finite-rank nilspace is sufficiently balanced (i.e. equidistributed in a
certain quantitative and multidimensional sense), then the nilspace is toral.
As an application of this, we obtain a new proof of a refinement of the
Green-Tao-Ziegler inverse theorem.
