From 938721877d1a4d8d0c304ef43572856b1a39970a Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 17 Feb 2016 05:33:34 +0100 Subject: Monad transformers. --- src/Lib.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Lib.hs (limited to 'src') diff --git a/src/Lib.hs b/src/Lib.hs new file mode 100644 index 0000000..9748aed --- /dev/null +++ b/src/Lib.hs @@ -0,0 +1,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) -- cgit v1.2.3