mirror of
https://github.com/idris-lang/Idris2.git
synced 2024-12-21 02:31:50 +03:00
21c6f4fb79
* [ breaking ] remove parsing of dangling binders It used to be the case that ``` ID : Type -> Type ID a = a test : ID (a : Type) -> a -> a test = \ a, x => x ``` and ``` head : List $ a -> Maybe a head [] = Nothing head (x :: _) = Just x ``` were accepted but these are now rejected because: * `ID (a : Type) -> a -> a` is parsed as `(ID (a : Type)) -> a -> a` * `List $ a -> Maybe a` is parsed as `List (a -> Maybe a)` Similarly if you want to use a lambda / rewrite / let expression as part of the last argument of an application, the use of `$` or parens is now mandatory. This should hopefully allow us to make progress on #1703
68 lines
2.0 KiB
Idris
68 lines
2.0 KiB
Idris
module Control.Monad.Reader.Interface
|
|
|
|
import Control.Monad.Maybe
|
|
import Control.Monad.Error.Either
|
|
import Control.Monad.Reader.Reader
|
|
import Control.Monad.State.State
|
|
import Control.Monad.RWS.CPS
|
|
import Control.Monad.Trans
|
|
import Control.Monad.Writer.CPS
|
|
|
|
%default total
|
|
|
|
||| A computation which runs in a static context and produces an output
|
|
public export
|
|
interface Monad m => MonadReader stateType m | m where
|
|
||| Get the context
|
|
ask : m stateType
|
|
|
|
||| `local f c` runs the computation `c` in an environment modified by `f`.
|
|
local : (stateType -> stateType) -> m a -> m a
|
|
|
|
|
|
||| Evaluate a function in the context held by this computation
|
|
public export
|
|
asks : MonadReader stateType m => (stateType -> a) -> m a
|
|
asks f = map f ask
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Implementations
|
|
--------------------------------------------------------------------------------
|
|
|
|
public export %inline
|
|
Monad m => MonadReader stateType (ReaderT stateType m) where
|
|
ask = MkReaderT (\st => pure st)
|
|
|
|
local f (MkReaderT action) = MkReaderT (action . f)
|
|
|
|
public export %inline
|
|
Monad m => MonadReader r (RWST r w s m) where
|
|
ask = MkRWST $ \r,s,w => pure (r,s,w)
|
|
local f m = MkRWST $ \r,s,w => unRWST m (f r) s w
|
|
|
|
public export %inline
|
|
MonadReader r m => MonadReader r (EitherT e m) where
|
|
ask = lift ask
|
|
local = mapEitherT . local
|
|
|
|
public export %inline
|
|
MonadReader r m => MonadReader r (MaybeT m) where
|
|
ask = lift ask
|
|
local = mapMaybeT . local
|
|
|
|
public export %inline
|
|
MonadReader r m => MonadReader r (StateT s m) where
|
|
ask = lift ask
|
|
local = mapStateT . local
|
|
|
|
public export %inline
|
|
MonadReader r m => MonadReader r (WriterT w m) where
|
|
ask = lift ask
|
|
|
|
-- this differs from the implementation in the mtl package
|
|
-- which uses mapWriterT. However, it seems strange that
|
|
-- this should require a Monoid instance to further
|
|
-- accumulate values, while the implementation of
|
|
-- MonadReader for RWST does no such thing.
|
|
local f (MkWriterT m) = MkWriterT $ \w => local f (m w)
|