{-# LANGUAGE TypeFamilies #-}

{-
  This module is part of Antisplice.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Antisplice is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Antisplice is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Antisplice. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides a generalized way of running newtypes.
module Game.Antisplice.Utils.Run where

import Control.Monad
import Control.Monad.Trans.Class
import Game.Antisplice.Utils.Fail
import Game.Antisplice.Monad.Dungeon

-- | Typeclass for all runnable newtype 'MonadTrans'ers
class MonadTrans t => Run t where
  type Baretype t m a
  run :: Monad m => t m a -> Baretype t m a

instance Run (FailT e) where
  type Baretype (FailT e) m a = m (Either e a)
  run = runFailT

instance Run DungeonT where
  type Baretype DungeonT m a = DungeonState -> m (a,DungeonState)
  run = runDungeonT

instance Run RoomT where
  type Baretype RoomT m a = RoomState -> m (a,RoomState)
  run = runRoomT