genprog- Genetic programming library

MaintainerJan Snajder <>
Safe HaskellNone




Implementation of the GenProg.GenExpr interface for members of the Data typeclass. The implementation is based on SYB and SYZ generic programming frameworks (see and for details).

NB: Subexpressions that are candidates for crossover points or mutation must be of the same type as the expression itself, and must be reachable from the root node by type-preserving traversal. See below for an example.



This module re-exports GenExpr typeclass.

class GenExpr e whereSource

This typeclass defines an interface to expressions that can be genetically programmed. The operations that must be provided by instances of this class are used for the generation of random individuals as well as crossover and mutation operations. (An instance for members of the Data typeclass is provided in GenProg.GenExpr.Data.)

Minimal complete definition: exchange, nodeMapM, nodeMapQ, and nodeIndices.


exchange :: e -> Int -> e -> Int -> (e, e)Source

Exchanges subtrees of two expressions: exchange e1 n1 e2 n2 replaces the subexpression of e1 rooted in node n1 with the subexpression of e2 rooted in n2, and vice versa.

nodeMapM :: Monad m => (e -> m e) -> e -> m eSource

Maps a monadic transformation function over the immediate children of the given node.

nodeMapQ :: (e -> a) -> e -> [a]Source

Maps a query function over the immediate children of the given node and returns a list of results.

nodeIndices :: e -> ([Int], [Int])Source

A list of indices of internal (functional) and external (terminal) nodes of an expression.

adjustM :: Monad m => (e -> m e) -> e -> Int -> m eSource

Adjusts a subexpression rooted at the given node by applying a monadic transformation function.

nodes :: e -> IntSource

Number of nodes an expression has.

depth :: e -> IntSource

The depth of an expression. Equals 1 for single-node expressions.


Data a => GenExpr a 


Suppose you have a datatype defined as

data E = A E E
       | B String [E]
       | C
 deriving (Eq,Show,Typeable,Data)

and an expression defined as

e = A (A C C) (B "abc" [C,C])

The subexpressions of a e are considered to be only the subvalues of e that are of the same type as e. Thus, the number of nodes of expression e is

>>> nodes e

because subvalues of node B are of different type than expression e and therefore not considered as subexpressions.

Consequently, during a genetic programming run, subexpressions that are of a different type than the expression itself, or subexpression that cannot be reached from the root node by a type-preserving traversal, cannot be chosen as crossover points nor can they be mutated.