creatur-5.6.8: Framework for artificial life experiments.

Copyright(c) Amy de Buitléir 2013-2014
Safe HaskellSafe-Inferred







class Diploid g where Source

A diploid agent has two complete sets of genetic instructions. Instances of this class can be thought of as paired genes or paired instructions for building an agent. When two instructions in a pair differ, dominance relationships determine how the genes will be expressed in the agent. Minimal complete definition: express.

Minimal complete definition



express :: g -> g -> g Source

Given two possible forms of a gene, express takes into account any dominance relationship, and returns a gene representing the result.

Deriving generic instances of Diploid

You can easily use the generic mechanism provided to automatically create implementations of Diploid for arbitrarily complex types. First, you need to import:

import GHC.Generics

Instances of Diploid have been defined for some base types. You will need to create instances for any additional base types that you use.

If the arrays are of different lengths, the result will be as long as the shorter array.

λ> express [1,2,3,4] [5,6,7,8,9] :: [Int]

You can automatically derive instances for more complex types:

data MyType = MyTypeA Bool | MyTypeB Int | MyTypeC Bool Int [MyType]
deriving (Show, Generic)
instance Diploid MyType
instance Diploid [MyType]
λ> express (MyTypeA True) (MyTypeA False)
MyTypeA True
λ> express (MyTypeB 2048) (MyTypeB 36)
MyTypeB 36

Even with complex values, the implementation should just "do the right thing".

λ> express (MyTypeC False 789 [MyTypeA True, MyTypeB 33, MyTypeC True 12 []]) (MyTypeC True 987 [MyTypeA False, MyTypeB 11, MyTypeC True 3 []])
MyTypeC True 789 [MyTypeA True,MyTypeB 11,MyTypeC True 3 []]

When a type has multiple constructors, the constructors that appear earlier in the definition are dominant over those that appear later. For example:

λ> express (MyTypeA True) (MyTypeB 7)
MyTypeA True
λ> express (MyTypeB 4) (MyTypeC True 66 [])
MyTypeB 4