We show that S-duality in four dimensional non-supersymmetric abelian gauge
theories can be formulated as a canonical transformation in the phase space of
the theory. This transformation is the usual interchange between electric and
magnetic degrees of freedom. It is shown that in phase space the modular
anomaly emerges as the result of integrating out the momenta degrees of
freedom. The generalization to d dimensional abelian gauge theories of p forms
is also considered. In the case of non-abelian gauge theories a careful
analysis of the constraints implied by the canonical transformation shows that
it does not relate Yang-Mills theories with inverted couplings. In fact the
dual theory is shown to be of Freedman- Townsend's type, also with τ~=−1/τ, τ=2πθ+g24πi.Comment: 13 pages, latex file, important changes in the non-abelian case. Some
references adde