*R functions

This commit is contained in:
crumbtoo
2024-02-01 10:37:51 -07:00
parent 1803a1e058
commit 46f0393a03
5 changed files with 52 additions and 34 deletions

View File

@@ -11,32 +11,29 @@ errors and the family of RLPC monads.
-- only used for mtl instances -- only used for mtl instances
{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DeriveGeneric, DerivingStrategies, DerivingVia #-} {-# LANGUAGE DeriveGeneric, DerivingStrategies, DerivingVia #-}
{-# LANGUAGE BlockArguments #-}
module Compiler.RLPC module Compiler.RLPC
( RLPC (
, RLPCT(..) -- * Rlpc Monad transformer
, RLPCIO RLPCT(RLPCT),
, RLPCOptions(RLPCOptions) -- ** Special cases
, IsRlpcError(..) RLPC, RLPCIO
, RlpcError(..) -- ** Running
, MsgEnvelope(..) , runRLPCT
, addFatal , evalRLPCT, evalRLPCIO, evalRLPC
, addWound -- * Rlpc options
, MonadErrorful , Language(..), Evaluator(..)
, Severity(..) , DebugFlag(..), CompilerFlag(..)
, Language(..) -- ** Lenses
, Evaluator(..) , rlpcLogFile, rlpcDFlags, rlpcEvaluator, rlpcInputFiles, rlpcLanguage
, evalRLPCT -- * Misc. MTL-style functions
, evalRLPCIO , liftErrorful, hoistRlpcT
, evalRLPC -- * Misc. Rlpc Monad -related types
, rlpcLogFile , RLPCOptions(RLPCOptions), IsRlpcError(..), RlpcError(..)
, rlpcDFlags , MsgEnvelope(..), Severity(..)
, rlpcEvaluator , whenDFlag, whenFFlag
, rlpcInputFiles -- * Convenient re-exports
, DebugFlag(..) , addFatal, addWound, def
, whenDFlag
, whenFFlag
, def
, liftErrorful
) )
where where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -71,6 +68,12 @@ newtype RLPCT m a = RLPCT {
} }
deriving (Functor, Applicative, Monad, MonadReader RLPCOptions) deriving (Functor, Applicative, Monad, MonadReader RLPCOptions)
rlpc :: (IsRlpcError e, Monad m)
=> (RLPCOptions -> (Maybe a, [MsgEnvelope e]))
-> RLPCT m a
rlpc f = RLPCT . ReaderT $ \opt ->
ErrorfulT . pure $ f opt & _2 . each . mapped %~ liftRlpcError
type RLPC = RLPCT Identity type RLPC = RLPCT Identity
type RLPCIO = RLPCT IO type RLPCIO = RLPCT IO
@@ -84,8 +87,7 @@ evalRLPC opt r = runRLPCT r
& flip runReaderT opt & flip runReaderT opt
& runErrorful & runErrorful
evalRLPCT :: (Monad m) evalRLPCT :: RLPCOptions
=> RLPCOptions
-> RLPCT m a -> RLPCT m a
-> m (Maybe a, [MsgEnvelope RlpcError]) -> m (Maybe a, [MsgEnvelope RlpcError])
evalRLPCT opt r = runRLPCT r evalRLPCT opt r = runRLPCT r
@@ -132,6 +134,11 @@ prettyRlpcErr msg = header
liftErrorful :: (Monad m, IsRlpcError e) => ErrorfulT (MsgEnvelope e) m a -> RLPCT m a liftErrorful :: (Monad m, IsRlpcError e) => ErrorfulT (MsgEnvelope e) m a -> RLPCT m a
liftErrorful e = RLPCT $ lift (fmap liftRlpcError `mapErrorful` e) liftErrorful e = RLPCT $ lift (fmap liftRlpcError `mapErrorful` e)
hoistRlpcT :: (forall a. m a -> n a)
-> RLPCT m a -> RLPCT n a
hoistRlpcT f rma = RLPCT $ ReaderT $ \opt ->
ErrorfulT $ f $ evalRLPCT opt rma
data RLPCOptions = RLPCOptions data RLPCOptions = RLPCOptions
{ _rlpcLogFile :: Maybe FilePath { _rlpcLogFile :: Maybe FilePath
, _rlpcDFlags :: HashSet DebugFlag , _rlpcDFlags :: HashSet DebugFlag

View File

@@ -1,11 +1,11 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TupleSections, PatternSynonyms #-} {-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Errorful module Control.Monad.Errorful
( ErrorfulT(..) ( ErrorfulT(..)
, Errorful , Errorful
, pattern Errorful , pattern Errorful
, errorful
, runErrorful , runErrorful
, mapErrorful , mapErrorful
, MonadErrorful(..) , MonadErrorful(..)
@@ -28,6 +28,9 @@ type Errorful e = ErrorfulT e Identity
pattern Errorful :: (Maybe a, [e]) -> Errorful e a pattern Errorful :: (Maybe a, [e]) -> Errorful e a
pattern Errorful a = ErrorfulT (Identity a) pattern Errorful a = ErrorfulT (Identity a)
errorful :: (Applicative m) => (Maybe a, [e]) -> ErrorfulT e m a
errorful = ErrorfulT . pure
runErrorful :: Errorful e a -> (Maybe a, [e]) runErrorful :: Errorful e a -> (Maybe a, [e])
runErrorful m = coerce (runErrorfulT m) runErrorful m = coerce (runErrorfulT m)

View File

@@ -105,7 +105,7 @@ checkCoreProg p = scDefs
where scname = sc ^. _lhs._1 where scname = sc ^. _lhs._1
-- | @checkCoreProgR p@ returns @p@ if @p@ successfully typechecks. -- | @checkCoreProgR p@ returns @p@ if @p@ successfully typechecks.
checkCoreProgR :: Program' -> RLPC Program' checkCoreProgR :: (Applicative m) => Program' -> RLPCT m Program'
checkCoreProgR p = undefined checkCoreProgR p = undefined
{-# WARNING checkCoreProgR "unimpl" #-} {-# WARNING checkCoreProgR "unimpl" #-}

View File

@@ -20,6 +20,7 @@ import Debug.Trace
import Data.Text (Text) import Data.Text (Text)
import Data.Text qualified as T import Data.Text qualified as T
import Data.String (IsString(..)) import Data.String (IsString(..))
import Data.Functor.Identity
import Core.Syntax import Core.Syntax
import Compiler.RLPC import Compiler.RLPC
-- TODO: unify Located definitions -- TODO: unify Located definitions
@@ -180,8 +181,11 @@ lexCore s = case m of
where where
m = runAlex s lexStream m = runAlex s lexStream
lexCoreR :: Text -> RLPC [Located CoreToken] lexCoreR :: forall m. (Applicative m) => Text -> RLPCT m [Located CoreToken]
lexCoreR = lexCore lexCoreR = hoistRlpcT generalise . lexCore
where
generalise :: forall a. Identity a -> m a
generalise (Identity a) = pure a
-- | @lexCore@, but the tokens are stripped of location info. Useful for -- | @lexCore@, but the tokens are stripped of location info. Useful for
-- debugging -- debugging

View File

@@ -17,6 +17,7 @@ module Core.Parse
import Control.Monad ((>=>)) import Control.Monad ((>=>))
import Data.Foldable (foldl') import Data.Foldable (foldl')
import Data.Functor.Identity
import Core.Syntax import Core.Syntax
import Core.Lex import Core.Lex
import Compiler.RLPC import Compiler.RLPC
@@ -224,8 +225,11 @@ insScDef sc = programScDefs %~ (sc:)
singletonScDef :: (Hashable b) => ScDef b -> Program b singletonScDef :: (Hashable b) => ScDef b -> Program b
singletonScDef sc = insScDef sc mempty singletonScDef sc = insScDef sc mempty
parseCoreProgR :: [Located CoreToken] -> RLPC Program' parseCoreProgR :: forall m. (Applicative m) => [Located CoreToken] -> RLPCT m Program'
parseCoreProgR = parseCoreProg parseCoreProgR = hoistRlpcT generalise . parseCoreProg
where
generalise :: forall a. Identity a -> m a
generalise (Identity a) = pure a
happyBind :: RLPC a -> (a -> RLPC b) -> RLPC b happyBind :: RLPC a -> (a -> RLPC b) -> RLPC b
happyBind m k = m >>= k happyBind m k = m >>= k