module DDC.Core.Flow.Convert.Base
( ConvertM
, Error (..)
, withRateXLAM, isRateXLAM
, withSuspFns, isSuspFn)
where
import DDC.Base.Pretty
import DDC.Core.Exp.Annot.Compounds
import DDC.Type.Exp
import DDC.Core.Flow.Prim as F
import qualified DDC.Control.Monad.Check as G
import qualified Data.Set as S
import Data.Maybe
type ConvertM x = G.CheckM (S.Set F.Name, S.Set F.Name) Error x
withRateXLAM :: Bind F.Name -> ConvertM a -> ConvertM a
withRateXLAM r c
| Just r' <- takeNameOfBind r
= do (fs,rs) <- G.get
G.put (fs, S.insert r' rs)
val <- c
G.put (fs, rs)
return $ val
| otherwise
= c
isRateXLAM :: F.Name -> ConvertM Bool
isRateXLAM r
= do (_,rs) <- G.get
return $ S.member r rs
withSuspFns :: [Bind F.Name] -> ConvertM a -> ConvertM a
withSuspFns bs c
= do (fs,rs) <- G.get
let ns = catMaybes $ map takeNameOfBind bs
G.put (S.union (S.fromList ns) fs, rs)
val <- c
G.put (fs, rs)
return $ val
isSuspFn :: F.Name -> ConvertM Bool
isSuspFn f
= do (fs,_) <- G.get
return $ S.member f fs
data Error
= ErrorInvalidBinder F.Name
| ErrorPartialPrimitive F.Name
| ErrorNotSupported F.Name
| ErrorUnexpectedSum
instance Pretty Error where
ppr err
= case err of
ErrorInvalidBinder n
-> vcat [ text "Invalid name used in binder '" <> ppr n <> text "'."]
ErrorPartialPrimitive n
-> vcat [ text "Cannot convert primitive " <> ppr n <> text "." ]
ErrorNotSupported n
-> vcat [ text "Cannot convert " <> ppr n <> text ", as it shouldn't be generated by flow transforms." ]
ErrorUnexpectedSum
-> vcat [ text "Unexpected type sum."]