summaryrefslogtreecommitdiff
path: root/src/Lib.hs
blob: 9748aed294cd663588baecdb887751dc3ec178cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Lib where

import Control.Lens (makeLenses)
import Control.Monad.Identity (Identity(..))
import Control.Monad.Reader (ReaderT(..), runReaderT)
import Control.Monad.Reader.Class (MonadReader(..))
import Control.Monad.State.Class (MonadState(..))
import Control.Monad.State.Strict (StateT(..), evalStateT)
import Data.Default (Default(..))

data Position = Position { _x :: Int, _y :: Int }
                deriving (Show, Read)
instance Default Position where
    def = Position 0 0
makeLenses ''Position

data Player = Player {
      position :: Position}
              deriving (Show, Read)
instance Default Player where
    def = Player { position = def }
makeLenses ''Player

newtype Config = Config String
    deriving (Show)

data World = World {
      player :: Player}
             deriving (Show, Read)
instance Default World where
    def = World { player = def }
makeLenses ''World

newtype App a = App {
      runApp :: StateT World (ReaderT Config Identity) a}
    deriving ( Applicative
             , Functor
             , Monad
             , MonadState World
             , MonadReader Config
             )

evalApp :: App a -> World -> Config -> a
evalApp f world config =
    runIdentity (runReaderT (evalStateT (runApp f) world) config)