The question I posted makes no sense.
The type signatures were provided by ghci.
I misread it: not (MonadReader (Map k (Set a -> Set a)) m ;
but rather: MonadReader (Map k (Set a -> Set a) m) .
It was just an oversight. Sorry for wasting your time.
========================================
Is FlexibleContexts really needed in this context?
Which monad does "m" stand for?
I tried replacing "m" with [ ], but it didn't work.
{-# LANGUAGE FlexibleContexts #-}
module LeftCensorsList where
import Data.Set as S
import Data.Map as M
import Data.List as L
import Control.Monad.Reader
-- ============================== FUNCTIONS ==============================
censorsList :: Ord a => Map a (Set a -> Set a) -> [Set a] -> ([Set a], [Set a])
censorsList _ [] = ([],[])
censorsList cnsrmap (xs:xss) = runReader (go xs xss [xs]) cnsrmap
where
go _ [] invacc = pure (reverse invacc,[])
go cnsrs (ys:yss) invacc = do
ys' <- applyCensors cnsrs ys
if S.null ys'
then pure (reverse invacc,ys:yss)
else go ys' yss (ys': invacc)
applyCensors :: (MonadReader (Map k (Set a -> Set a)) m, Ord k) => Set k -> Set a -> m (Set a) -- <<<<<
applyCensors cnsrs xs = go (S.toList cnsrs) xs
where
go [] ys = pure ys
go (cnsr:rest) ys = do
ys' <- applyCensor cnsr ys
if S.null ys'
then pure S.empty
else go rest ys'
applyCensor :: (MonadReader (Map k (t -> t)) m, Ord k) => k -> t -> m t -- <<<<<
applyCensor cnsr xs = do
cnsrmap <- ask
case M.lookup cnsr cnsrmap of
Nothing -> pure xs
Just f -> pure $ f xs
-- ============================== TEST ==============================
t1,t2,t3, t4 :: ([Set Int],[Set Int])
t1 = censorsList M.empty $ L.map S.fromList [[1,3,13],[2,4,6]] -- ok
t2 = censorsList exCensorsMap $ L.map S.fromList [[1,3,13],[2,4,6]] -- ok
t3 = censorsList exCensorsMap [] -- ok
t4 = censorsList exCensorsMap $ L.map S.fromList [[1,4,13],[2,4,5]] -- ok
-- ============================== EXAMPLES ==============================
-- -------------------- MAPS --------------------
exCensorsMap :: Map Int (Set Int -> Set Int)
exCensorsMap = M.fromList $
[(1,censor1)
,(2,censor2)
,(3,censor3)
,(4,censor4)
]
-- -------------------- CENSORS --------------------
censor1, censor2, censor3, censor4 :: Set Int -> Set Int
censor1 = \xs -> xs S.\\ (S.fromList [2,4])
censor2 = \xs -> xs S.\\ (S.fromList [3])
censor3 = \xs -> xs S.\\ (S.fromList [1,2])
censor4 = \xs -> xs S.\\ (S.fromList [2,5])
The monad you are instantiating is the reader monad:
applyCensors :: (Ord k) => Set k -> Set a -> Reader (Map k (Set a -> Set a)) (Set a)
You can infer this yourself by noting the type of runReader
and unifying from there.
The type signatures were provided by ghci. I misread it: not (MonadReader (Map k (Set a -> Set a)) m; but rather: MonadReader (Map k (Set a -> Set a) m). It was just an oversight and therefore the question makes no sense.
Sorry for wasting your time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.