HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleapecs-0.9.6Haskell2010

Apecs

This module forms the apecs Prelude. It selectively re-exports the user-facing functions from the submodules.

  • 14 types
  • 2 classes
  • 24 values
  • Packageapecs-0.9.6
  • Exports44
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceApecs.hs

Core types

10 declarations
newtypenewtype SystemT w (m :: Type -> Type) a
#

A SystemT is a newtype around `ReaderT w m a`, where w is the game world variable. Systems serve to

  • Allow type-based lookup of a component's store through getStore.

  • Lift side effects into their host Monad.

Constructors

Instances10MonadReader, MonadTrans, Monad, Functor, Applicative, MonadIO, …
classclass Elem (Storage c) ~ c => Component c where
#

A component is defined by specifying how it is stored. The constraint ensures that stores and components are mapped one-to-one.

Associated types

Instances21Component, …
newtypenewtype Entity
#

An Entity is just an integer, used to index into a component store. In general, use newEntity, cmap, and component tags instead of manipulating these directly.

For performance reasons, negative values like (-1) are reserved for stores to represent special values, so avoid using these.

Constructors

Instances8Enum, Eq, Num, Ord, Show, Component, …
classclass (Monad m, Component c) => Has w (m :: Type -> Type) c where
#

Has w m c means that world w can produce a Storage c. It is parameterized over m to allow stores to be foreign.

Methods

Instances19Has, …
  • Monad m => Has w m EntityDefined in apecs-0.9.6 · Apecs.Components · orphan
  • Monad m => Has w m ()Defined in apecs-0.9.6 · Apecs.Components · orphan
  • Has w m c => Has w m (Filter c)Defined in apecs-0.9.6 · Apecs.Components
  • Has w m c => Has w m (Not c)Defined in apecs-0.9.6 · Apecs.Components
  • Has w m c => Has w m (Head c)Defined in apecs-0.9.6 · Apecs.Experimental.Components
  • Has w m c => Has w m (Redirect c)Defined in apecs-0.9.6 · Apecs.Experimental.Components
  • Has w m c => Has w m (Identity c)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • Has w m c => Has w m (Maybe c)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Storage c ~ Pushdown s c, Has w m c) => Has w m (Stack c)Defined in apecs-0.9.6 · Apecs.Experimental.Stores
  • (MonadIO m, Component c, Has w m (Child c)) => Has w m (ChildList c)Defined in apecs-0.9.6 · Apecs.Experimental.Children
  • (MonadIO m, Component c, Has w m (Child c)) => Has w m (ChildValue c)Defined in apecs-0.9.6 · Apecs.Experimental.Children
  • (Has w m ca, Has w m cb) => Has w m (Either ca cb)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1) => Has w m (t_0, t_1)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2) => Has w m (t_0, t_1, t_2)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2, Has w m t_3) => Has w m (t_0, t_1, t_2, t_3)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2, Has w m t_3, Has w m t_4) => Has w m (t_0, t_1, t_2, t_3, t_4)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2, Has w m t_3, Has w m t_4, Has w m t_5) => Has w m (t_0, t_1, t_2, t_3, t_4, t_5)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2, Has w m t_3, Has w m t_4, Has w m t_5, Has w m t_6) => Has w m (t_0, t_1, t_2, t_3, t_4, t_5, t_6)Defined in apecs-0.9.6 · Apecs.Components · orphan
  • (Has w m t_0, Has w m t_1, Has w m t_2, Has w m t_3, Has w m t_4, Has w m t_5, Has w m t_6, Has w m t_7) => Has w m (t_0, t_1, t_2, t_3, t_4, t_5, t_6, t_7)Defined in apecs-0.9.6 · Apecs.Components · orphan
datadata Not a
#

Pseudocomponent indicating the absence of a. Mainly used as e.g. cmap $ (a, Not b) -> c to iterate over entities with an a but no b. Can also be used to delete components, like cmap $ a -> (Not :: Not a) to delete every a component.

Instances3Has, Component, Storage

Stores

5 declarations
newtypenewtype Map c
#

A map based on Data.IntMap.Strict. O(log(n)) for most operations.

Instances7ExplInit, ExplDestroy, ExplGet, ExplMembers, ExplSet, Cachable, …
newtypenewtype Unique c
#

A Unique contains zero or one component. Writing to it overwrites both the previous component and its owner. Its main purpose is to be a Map optimized for when only ever one component inhabits it.

Instances6ExplInit, ExplDestroy, ExplGet, ExplMembers, ExplSet, Elem
newtypenewtype Global c
#

A Global contains exactly one component. The initial value is mempty from the component's Monoid instance. Querying a Global at any Entity yields this one component, effectively sharing the component between all entities.

A Global component can be read with get 0 or get 1 or even get undefined. The convenience entity global is defined as -1, and can be used to make operations on a global more explicit, i.e. 'Time t <- get global'.

You also can read and write Globals during a cmap over other components.

Instances4ExplInit, ExplGet, ExplSet, Elem
datadata Cache (n :: Nat) s
#

A cache around another store. Caches store their members in a fixed-size vector, so read/write operations become O(1). Caches can provide huge performance boosts, especially when working with large numbers of components.

The cache size is given as a type-level argument.

Note that iterating over a cache is linear in cache size, so sparsely populated caches might decrease performance. In general, the exact size of the cache does not matter as long as it reasonably approximates the number of components present.

The cache uses entity (-2) internally to represent missing entities. If you manually manipulate Entity values, be careful that you do not use (-2)

The actual cache is not necessarily the given argument, but the next biggest power of two. This is allows most operations to be expressed as bit masks, for a large potential performance boost.

Instances7ExplInit, ExplDestroy, ExplGet, ExplMembers, ExplSet, Cachable, …
methodexplInit :: m s
#

Initialize a new empty store.

Systems

15 declarations
valueset :: Set w m c => Entity -> c -> SystemT w m ()
#

Writes a Component to a given Entity. Will overwrite existing Components.

value($=) :: Set w m c => Entity -> c -> SystemT w m ()
#

Writes a Component to a given Entity. Will overwrite existing Components.

set operator

value($~) :: (Get w m cx, Set w m cy) => Entity -> (cx -> cy) -> SystemT w m ()
#

Applies a function, if possible.

modify operator

valuecmap
  1. :: (Get w m cx, Members w m cx, Set w m cy)
  2. => cx -> cy
  3. -> SystemT w m ()
#

Maps a function over all entities with a cx, and writes their cy.

valuecmapIf
  1. :: (Get w m cx, Get w m cp, Members w m cx, Set w m cy)
  2. => cp -> Bool
  3. -> cx -> cy
  4. -> SystemT w m ()
#

Conditional cmap, that first tests whether the argument satisfies some property. The entity needs to have both a cx and cp component.

valuecmapM
  1. :: (Get w m cx, Set w m cy, Members w m cx)
  2. => cx -> SystemT w m cy
  3. -> SystemT w m ()
#

Monadically iterates over all entites with a cx, and writes their cy.

valuecfold :: (Members w m c, Get w m c) => (a -> c -> a) -> a -> SystemT w m a
#

Fold over the game world; for example, cfold max (minBound :: Foo) will find the maximum value of Foo. Strict in the accumulator.

valuecfoldM
  1. :: (Members w m c, Get w m c)
  2. => a -> c -> SystemT w m a
  3. -> a
  4. -> SystemT w m a
#

Monadically fold over the game world. Strict in the accumulator.

valuecollect
  1. :: (Get w m components, Members w m components)
  2. => components -> Maybe a
  3. -> SystemT w m [a]
#

Collect matching components into a list by using the specified test/process function. You can use this to preprocess data before returning. And you can do a test here that depends on data from multiple components. Pass Just to simply collect all the items.

Performance

When using cmap or cfold over a tuple of components, keep in mind the ordering of the tuple can have performance implications!

For tuples, the way the cmap and cfold work under the hood is by iterating over the component in the first position, and then for each entity that has that component, checking whether the entity also has the components in the remaining positions. Therefore, the first component will typically be the most determining factor for performance, and a good rule of thumb is to, when iterating over a tuple, put the rarest component in first position.

Let's take a look at an example. Consider a simple 2D rendering system built on top of cmapM_:

cmapM_ $ \(Sprite sprite, Visible) -> do
  renderSprite sprite

While this rendering system works, it could be made more efficient by leveraging knowledge of how the library handles reading of tupled components. The usage of cmapM_ here (or any of the other map/fold functions) will iterate over all entities with a Sprite component and filter out any of these entities that do not have a Visible component. Depending on the game, it is reasonable to assume that there are more sprites active in the game's world than sprites that are visible to the game's camera.

Swapping the component ordering in the tuple is likely to be more efficient:

cmapM_ $ \(Visible, Sprite sprite) -> do
  renderSprite sprite

Now the system iterates over just those entities that are visible to the game's camera and filters out any that do not have a Sprite component.

While putting the rarest component first is an excellent rule of thumb, to get the best possible performance, always consider how maps and folds are executed under the hood, and how you can order your components to optimize that process.

Other

9 declarations
valuerunWith :: w -> SystemT w m a -> m a
#

Run a system in a game world

newtypenewtype EntityCounter
#

Component used by newEntity to track the number of issued entities. Automatically added to any world created with makeWorld

Instances6Eq, Show, Semigroup, Monoid, Component, Storage
valueglobal :: Entity
#

Convenience entity, for use in places where the entity value does not matter, i.e. a global store.

valuemakeWorld :: String -> [Name] -> Q [Dec]
#

The typical way to create a world record, associated Has instances, and initialization function.

makeWorld "MyWorld" [''Component1, ''Component2, ...]

turns into

data MyWorld = MyWorld Component1 Component2 ... EntityCounter
instance MyWorld `Has` Component1 where ...
instance MyWorld `Has` Component2 where ...
...
instance MyWorld `Has` EntityCounter where ...

initMyWorld :: IO MyWorld
initMyWorld = MyWorld <$> initStore <*> initStore <*> ... <*> initStore

Re-exports

5 declarations
valueasks
  1. :: MonadReader r m
  2. => (r -> a)

    The selector function to apply to the environment.

  3. -> m a
#

Retrieves a function of the current environment.

methodask :: m r
#

Retrieves the monad environment.

methodliftIO :: IO a -> m a
#

Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

Example
import Control.Monad.Trans.State -- from the "transformers" library

printState :: Show s => StateT s IO ()
printState = do
  state <- get
  liftIO $ print state

Had we omitted liftIO, we would have ended up with this error:

• Couldn't match type ‘IO’ with ‘StateT s IO’
 Expected type: StateT s IO ()
   Actual type: IO ()

The important part here is the mismatch between StateT s IO () and IO ().

Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

> evalStateT printState "hello"
"hello"

> evalStateT printState 3
3
methodlift :: Monad m => m a -> t m a
#

Lift a computation from the argument monad to the constructed monad.

datadata Proxy (t :: k)
#

Proxy is a type that holds no data, but has a phantom parameter of arbitrary type (or even kind). Its use is to provide type information, even though there is no value available of that type (or it may be too costly to create one).

Historically, Proxy :: Proxy a is a safer alternative to the undefined :: a idiom.

Example1 expression
Proxy :: Proxy (Void, Int -> Int)Proxy

Proxy can even hold types of higher kinds,

Example1 expression
Proxy :: Proxy EitherProxy
Example1 expression
Proxy :: Proxy FunctorProxy
Example1 expression
Proxy :: Proxy complicatedStructureProxy
Instances29Generic1, Monad, Functor, Applicative, Foldable, Traversable, …
  • Generic1 ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Monad ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Functor ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Applicative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Foldable ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • Alternative ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • MonadPlus ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • MonadZip ProxyDefined in base-4.20.2.0 · Control.Monad.Zip
  • Eq1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Ord1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Read1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Show1 ProxyDefined in base-4.20.2.0 · Data.Functor.Classes
  • Contravariant ProxyDefined in base-4.20.2.0 · Data.Functor.Contravariant
  • NFData1 ProxyDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Bounded (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Enum (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Eq (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Data t => Data (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Read (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Show (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Ix (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Generic (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • Semigroup (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • Monoid (Proxy s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Proxy
  • NFData (Proxy a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • type Rep (Proxy t) = D1 ('MetaData "Proxy" "GHC.Internal.Data.Proxy" "ghc-internal" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) U1)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
  • type Rep1 Proxy = D1 ('MetaData "Proxy" "GHC.Internal.Data.Proxy" "ghc-internal" 'False) (C1 ('MetaCons "Proxy" 'PrefixI 'False) U1)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics