Functional Code Building Genetic Programming

Edward R. Pantridge, Thomas Helmuth, Lee SpectorGECCO 2022

Abstract

Functional Code Building Genetic Programming (FCBGP) bridges the gap between flexible stack-based genetic programming and rigorous grammar-based approaches by incorporating the Hindley-Milner type system. By enforcing type safety through unification and inference during the compilation of linear genomes into abstract syntax trees (ASTs), FCBGP produces concise, human-readable, and highly generalizable programs. Evaluation on a subset of the PSB1 benchmark demonstrates that FCBGP achieves near-perfect generalization and outperforms traditional methods on several complex tasks, though challenges remain in evolving higher-order logic.

Introduction

The goal of general program synthesis is to automatically generate code that solves specific tasks. Historically, two dominant approaches have emerged in the Genetic Programming (GP) community, each with distinct trade-offs. Stack-based GP, such as PushGP, offers high flexibility and robustness to mutation but frequently produces "spaghetti code" that is difficult for humans to interpret and struggles with complex polymorphic types. Conversely, grammar-based GP (e.g., GE or G3P) produces readable code in conventional languages, yet often fails to capture the full complexity of type information, making polymorphism and higher-order functions difficult to implement.

This paper formalizes Functional Code Building Genetic Programming (FCBGP), a method designed to handle arbitrary data types and polymorphism while ensuring every evolved program is strictly type-safe. By leveraging formal type theory, FCBGP aims to combine the search efficiency of stack-based representations with the structural integrity of typed functional programming.

Methodology

FCBGP introduces a functional variant of Code Building Genetic Programming (CBGP) that utilizes several key components from formal type theory and compiler design:

  • Hindley-Milner (HM) Type System: FCBGP employs the HM system—the foundation for languages like Haskell and ML—to perform type inference and unification. This allows the system to determine the types of expressions automatically and ensure that function applications are valid.
  • Linear "Plushy" Genomes: Programs are represented as linear lists of instructions, similar to the representation used in PushGP. This maintains the flexibility and ease of mutation characteristic of stack-based GP.
  • Stack-Based AST Compilation: A compiler "folds" the linear genome into an Abstract Syntax Tree (AST). During this process, the HM system ensures that when a function (e.g., map or +) is applied to arguments, the types match perfectly. If a type mismatch occurs, the instruction is typically skipped or handled gracefully, ensuring the final program is always executable.
  • Polymorphism via Type Schemes: The system handles functions like map, which takes a function from type $\alpha$ to $\beta$ and a sequence of $\alpha$. The unification algorithm binds these type variables to concrete types (e.g., Int or String) during the compilation phase.
  • Clojure Integration: The system evolves code in Clojure, a LISP-dialect. Its S-expression syntax allows for trivial conversion between evolved ASTs and executable source code, facilitating rapid evaluation.

Experimental Setup

The researchers evaluated FCBGP using the PSB1 (Program Synthesis Benchmark suite), focusing on a subset of 14 problems representing diverse requirements:

  • Arithmetic and Logic: smallest (minimum of four integers), median (median of three integers).
  • Data Transformation: negative-to-zero (replacing negatives in a list), vectors-summed (summing corresponding elements of two lists).
  • String Manipulation: mirror-image (checking if two lists are mirrors of each other).

Each problem was tested over 100 independent runs, using 100 training cases and 300 unseen test cases to measure generalization performance.

Results and Discussion

High Success and Generalization Rates

FCBGP achieved near 100% success on 7 of the 14 problems. A standout result was the system's performance on the vectors-summed problem, where FCBGP achieved a 100% success rate. In comparison, baseline methods like PushGP (11%), G3P (85%), and GE (1%) lagged significantly behind.

Perhaps most importantly, the generalization rate was nearly 1.0 across all solved problems. In many GP systems, programs often overfit the training data; however, the strict type constraints in FCBGP appear to force the evolution of robust, logical structures rather than "memorization" hacks.

Code Conciseness and "Anti-Bloat"

Evolved FCBGP solutions were remarkably concise, often reaching the theoretical minimum size. For example, in the mirror-image problem, where the minimum possible size is 4 nodes, the average FCBGP solution was 4.06 nodes. The authors hypothesize that the stack-based AST construction makes it difficult for the GP to accumulate "bloat" (useless code that does not affect output), as every instruction must satisfy strict type requirements to be included in the final tree.

Type Safety as a Search Constraint

By enforcing strict type safety during compilation, the system effectively prunes the search space. The GP cannot generate programs that attempt invalid operations, such as adding a string to an integer. This constraint directs the evolutionary search toward the subset of the search space containing valid, well-typed programs.

Limitations and Future Work

Despite its strengths, FCBGP failed to find solutions for 6 of the 14 problems, such as replace-space-with-newline and digits. These failures highlight several areas for future research:

  • Higher-Order Logic: While the system supports anonymous functions (lambdas), it rarely evolved them effectively. Improving the evolution of higher-order logic remains a significant challenge.
  • Type System Extensions: Future iterations could incorporate sub-typing, type classes, or variadic functions to handle more complex programming tasks.
  • Search Dynamics: Further investigation is needed to determine if the failures on specific problems are due to the representation itself or limitations in the search algorithm's ability to navigate certain fitness landscapes.

Conclusion

FCBGP demonstrates that integrating formal type theory into genetic programming provides a powerful mechanism for synthesizing readable, concise, and highly generalizable code. By treating type safety as a fundamental search constraint, FCBGP moves program synthesis closer to the standards of professional software development.

Summary updated

BibTeX
@inproceedings{pantridge2022functional,
  author = {Edward R. Pantridge and Thomas Helmuth and Lee Spector},
  title = {{Functional Code Building Genetic Programming}},
  booktitle = {GECCO},
  year = {2022},
}
Figure