Learning Functional Equivalence via Supervised Contrastive Code-Problem Alignment
We present a study on learning code representations that robustly capture functional equivalence by enforcing a supervised bimodal alignment between source code and the natural language problem statement it solves. The core challenge in code representation is achieving functional invariance: ensuring that embeddings of programs solving the same task are clustered closely, independent of high syntactic and lexical variance. Previous code-only methods, limited to discrete problem identifiers, often fail this invariance test. We propose a supervised contrastive dual-encoder framework. By leveraging the NL problem statement as a stable semantic anchor, the model is explicitly forced to map diverse, functionally equivalent code solutions into the same region of a shared embedding space. Using the POJ-104 dataset augmented with NL descriptions, we evaluate the pre-trained GraphCodeBERT on retrieval and problem-level generalization. Our results demonstrate that the proposed supervised contrastive dual-encoder framework, which jointly trains code and problem representations through supervised contrastive learning, markedly improves retrieval accuracy and generalization over methods relying solely on discrete labels. We further examine a hard-negative mining variant that stabilizes the embedding geometry by mitigating negative distribution collapse, providing an alternative formulation with comparable performance.