$$ is defined in the source code of tex, it is not a latex-defined environment. Any two characters of catcode 3 start display math.
The source is written in web (pascal)
tex.web is available from ctan
https://ctan.org/tex-archive/systems/knuth/dist/tex
Here is the part that scans for $ or $$ to enter inline or display math.
@ We get into math mode from horizontal mode when a `\.\$' (i.e., a
|math_shift| character) is scanned. We must check to see whether this
`\.\$' is immediately followed by another, in case display math mode is
called for.
@<Cases of |main_control| that build...@>=
hmode+math_shift:init_math;
@ @<Declare act...@>=
procedure init_math;
label reswitch,found,not_found,done;
var w:scaled; {new or partial |pre_display_size|}
@!l:scaled; {new |display_width|}
@!s:scaled; {new |display_indent|}
@!p:pointer; {current node when calculating |pre_display_size|}
@!q:pointer; {glue specification when calculating |pre_display_size|}
@!f:internal_font_number; {font in current |char_node|}
@!n:integer; {scope of paragraph shape specification}
@!v:scaled; {|w| plus possible glue amount}
@!d:scaled; {increment to |v|}
begin get_token; {|get_x_token| would fail on .{\ifmmode}\thinspace!}
if (cur_cmd=math_shift)and(mode>0) then @<Go into display math mode@>
else begin back_input; @<Go into ordinary math mode@>;
end;
end;
@ @<Go into ordinary math mode@>=
begin push_math(math_shift_group); eq_word_define(int_base+cur_fam_code,-1);
if every_math<>null then begin_token_list(every_math,every_math_text);
end
@ We get into ordinary math mode from display math mode when \.{\\eqno}' or.{\leqno}' appears. In such cases |cur_chr| will be 0 or~1, respectively;
the value of |cur_chr| is placed onto |save_stack| for safe keeping.
@<Cases of |main_control| that build...@>=
mmode+eq_no: if privileged then
if cur_group=math_shift_group then start_eq_no
else off_save;
@ @<Put each...@>=
primitive("eqno",eq_no,0);
@!@:eq_no_}{.{\eqno} primitive@>
primitive("leqno",eq_no,1);
@!@:leq_no_}{.{\leqno} primitive@>
@ When \TeX\ is in display math mode, |cur_group=math_shift_group|,
so it is not necessary for the |start_eq_no| procedure to test for
this condition.
@<Declare act...@>=
procedure start_eq_no;
begin saved(0):=cur_chr; incr(save_ptr);
@<Go into ordinary math mode@>;
end;
@ @<Cases of |print_cmd_chr|...@>=
eq_no:if chr_code=1 then print_esc("leqno")@+else print_esc("eqno");
@ @<Forbidden...@>=non_math(eq_no),
@ When we enter display math mode, we need to call |line_break| to
process the partial paragraph that has just been interrupted by the
display. Then we can set the proper values of |display_width| and
|display_indent| and |pre_display_size|.
@<Go into display math mode@>=
begin if head=tail then {\.{\\noindent\$\$}' or.{$${ }$$}'}
begin pop_nest; w:=-max_dimen;
end
else begin line_break(display_widow_penalty);@/
@<Calculate the natural width, |w|, by which the characters of the
final line extend to the right of the reference point,
plus two ems; or set |w:=max_dimen| if the non-blank information
on that line is affected by stretching or shrinking@>;
end;
{now we are in vertical mode, working on the list that will contain the display}
@<Calculate the length, |l|, and the shift amount, |s|, of the display lines@>;
push_math(math_shift_group); mode:=mmode;
eq_word_define(int_base+cur_fam_code,-1);@/
eq_word_define(dimen_base+pre_display_size_code,w);
eq_word_define(dimen_base+display_width_code,l);
eq_word_define(dimen_base+display_indent_code,s);
if every_display<>null then begin_token_list(every_display,every_display_text);
if nest_ptr=1 then build_page;
end
$$is a TeX primitive. See Why is \[ ... \] preferable to $$ ... $$?.\[is defined in the LaTeX kernel, (texdoc source2eto see its definition) and redefined byamsmathif you are loading that package. – Alan Munn Sep 02 '23 at 17:05