{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE FlexibleContexts #-} module Module.Environment ( Environment(..) , HasEnvironment , empty , register , reorder ) where import Context (Path) import Control.Monad.State (MonadState, modify) import Data.Map (Map) import qualified Data.Map as Map (empty, insert) import Priority (Queue) import qualified Priority (empty) data Environment a = Environment { modules :: Map Path a , queue :: Queue Path } type HasEnvironment a = MonadState (Environment a) register :: HasEnvironment a m => Path -> a -> m () register path module_ = modify $ \moduleSpace -> moduleSpace { modules = Map.insert path module_ (modules moduleSpace) } reorder :: HasEnvironment a m => (Queue Path -> Queue Path) -> m () reorder f = modify $ \moduleSpace -> moduleSpace { queue = f (queue moduleSpace) } empty :: Environment a empty = Environment { modules = Map.empty , queue = Priority.empty }