summaryrefslogtreecommitdiff
path: root/src/Lib.hs
blob: 695f0de48afbbcc6c1eb855d63a729cd6f0f30c9 (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
49
50
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Lib where

import Control.Lens (makeLenses)
import Control.Monad.Identity (Identity(..))
import Control.Monad.IO.Class (MonadIO(..))
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 IO) a}
    deriving ( Applicative
             , Functor
             , Monad
             , MonadState World
             , MonadReader Config
             , MonadIO
             )

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