conways-game-of-life/GOL/Space.hs

28 lines
928 B
Haskell
Raw Normal View History

2021-12-20 15:25:54 -05:00
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
2021-12-20 14:24:16 -05:00
module GOL.Space where
import Data.Functor.Rep
2021-12-20 16:20:51 -05:00
-- | A space in which a Conway's Game of Life simulation
-- takes place, with a notion of "neighbors to a cell" defined.
--
-- More specifically, a space is a representable functor @f@ such
-- that @'Rep' f@ is a graph. 'neighbors' then takes a node of
-- that graph and returns all nodes that are adjacent.
--
-- Instances should satisfy:
--
-- * Symmetry: if @x '`elem`' 'neighbors' y@, then @y '`elem`' 'neighbors' x@.
--
-- * Irreflexivity: @x '`elem`' 'neighbors' x@ is always false.
2021-12-20 15:28:11 -05:00
class Representable f => Space f where
neighbors :: Rep f -> [Rep f]
2021-12-20 14:24:16 -05:00
2021-12-20 16:20:51 -05:00
-- | A space is _displayable_ if it is representable over a 2D
-- grid. The graphical system requires a displayable space.
2021-12-20 15:28:11 -05:00
class (Space f, Rep f ~ (Int, Int)) => DisplayableSpace f where
2021-12-20 16:20:51 -05:00
sizex :: Int
sizey :: Int