If you're not French and plan to never write in French, you can just do
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\mathchardef\normalcolon=\mathcode`:
\def\colon{%
\noexpand\nobreak
\mskip 2mu
\mathpunct{}
\nonscript\mkern -\thinmuskip
{\normalcolon}%
\mskip 6muplus1mu
\relax
}
\begingroup\lccode`\~=`\:\lowercase{\endgroup\let~}\colon
\mathcode`\:=\string"8000
\makeatother
\begin{document}
$f: A \to B$
$a\normalcolon b$
\end{document}
This would send TeX into infinite loop if babel-french is used. A safer patch:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{amsmath}
\makeatletter
\mathchardef\normalcolon=\mathcode`:
\def\colon{%
\noexpand\nobreak
\mskip 2mu
\mathpunct{}
\nonscript\mkern -\thinmuskip
{\normalcolon}%
\mskip 6muplus1mu
\relax
}
\@ifpackagewith{babel}{french}{% french babel makes : active
\declare@shorthand{french}{:}{%
\ifmmode % <--- added
\colon % <--- added
\else % <--- added
\ifFB@spacing
\ifhmode
\ifdim\lastskip>1sp
\unskip\penalty\@M\FBcolonspace
\else
\FDP@colonspace
\fi
\fi
\fi
\string:%
\fi % <--- added
}%
}{%
\begingroup\lccode`\~=`\:\lowercase{\endgroup\let~}\colon
\mathcode`\:=\string"8000
}
\makeatother
\begin{document}
$f: A \to B$
$a\normalcolon b$
\end{document}

You can still use \colon with the standard meaning. For the colon as relation symbol (what you get normally in TeX with :) I provided \normalcolon.
Disclaimer. This doesn't work with XeLaTeX or LuaLaTeX.
\usepackage[french]{babel}. Anyway,\numberis not needed after\mathcode. – egreg Feb 14 '20 at 20:31\number...don't know how to resolvebabelissue. – Steven B. Segletes Feb 14 '20 at 20:33\colonand your\newcolon, though? (: – balu Feb 14 '20 at 21:51\DeclareMathSymbol(see e.g. this question). Unfortunately, it seems the command requires specifying the font. Does anyone happen to know whether it's possible to use that command in a font-agnostic way to redefine the colon? (Maybe that'd be a solution for French speakers.) – balu Feb 14 '20 at 22:03{\text{:}}in my definition to{\text{\textup{:}}}will do it. However, the 1st version approach of egreg does not suffer this issue, in which you define\mathchardef\normalcolon=\mathcode\:and then replace in my definition{\text{:}}with{\normalcolon}`. – Steven B. Segletes Feb 14 '20 at 23:07Extended mathchar used as mathchar (6300214). [\mathchardef\normalcolon=\mathcode:]`. In any case, your first solution works! Thanks so much! – balu Feb 15 '20 at 13:52:in my output is different on the first line (using:) than it is on the 2nd and 3rd lines (using\colonand revised definition of:. – Steven B. Segletes Feb 15 '20 at 19:28\colon) and the third line (revised colon /\newcolon)? Basically, what I'm asking is why you defined a new command\newcolonwhen\colonis already available? – balu Feb 15 '20 at 19:48\-c-o-l-o-nand wanted to get the same thing by simply typing:in math mode. – Steven B. Segletes Feb 15 '20 at 20:57\global\let:\coloninstead of\global\let:\newcolonbut now I realize that the former causes an infinite loop, so I guess that must have been it. – balu Feb 15 '20 at 21:26\colonis defined in terms of math:, so defining:in terms of\coloncreates an infinite loop. Here,\newcolonis defined in terms of a non-math colon. – Steven B. Segletes Feb 15 '20 at 21:49