Skip to content
Snippets Groups Projects
Commit 6b8a9d9d authored by chrg's avatar chrg
Browse files

Small cleanup

parent 4dab88ec
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ library ...@@ -12,6 +12,7 @@ library
exposed-modules: exposed-modules:
Control.Monad.Reduce Control.Monad.Reduce
Control.RTree Control.RTree
Data.Valuation
other-modules: other-modules:
Paths_rtree Paths_rtree
hs-source-dirs: hs-source-dirs:
......
...@@ -40,10 +40,11 @@ module Control.Monad.Reduce ( ...@@ -40,10 +40,11 @@ module Control.Monad.Reduce (
-- * Helpers -- * Helpers
onBoth, onBoth,
liftMaybe,
) where ) where
import Control.Applicative
import Control.Monad import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
import Data.Maybe import Data.Maybe
...@@ -145,15 +146,14 @@ collectNonEmpty fn as = do ...@@ -145,15 +146,14 @@ collectNonEmpty fn as = do
instance (MonadReduce l m) => MonadReduce l (MaybeT m) where instance (MonadReduce l m) => MonadReduce l (MaybeT m) where
splitWith m (MaybeT lhs) (MaybeT rhs) = MaybeT (splitWith m lhs rhs) splitWith m (MaybeT lhs) (MaybeT rhs) = MaybeT (splitWith m lhs rhs)
-- | Helper that lifts a maybe into MonadPlus (or MaybeT)
liftMaybe :: (MonadPlus m) => Maybe a -> m a
liftMaybe = maybe mzero pure
-- | Returns either of the maybes or combines them if both have values. -- | Returns either of the maybes or combines them if both have values.
onBoth :: (Monad m) => MaybeT m a -> MaybeT m a -> (a -> a -> MaybeT m a) -> MaybeT m a onBoth :: (MonadPlus m) => m a -> m a -> (a -> a -> m a) -> m a
onBoth mlhs mrhs fn = MaybeT do onBoth mlhs mrhs fn =
runMaybeT mlhs >>= \case join $ (fn <$> mlhs <*> mrhs) <|> fmap pure mrhs <|> fmap pure mlhs
Nothing -> runMaybeT mrhs
Just l ->
runMaybeT mrhs >>= \case
Nothing -> pure (Just l)
Just r -> runMaybeT (fn l r)
{- | Given a list of ordered options, choose the first that statisfy the constraints, {- | Given a list of ordered options, choose the first that statisfy the constraints,
returning the last element if nothing else matches. returning the last element if nothing else matches.
...@@ -164,8 +164,10 @@ linearSearch = foldr1 (<|) . fmap pure ...@@ -164,8 +164,10 @@ linearSearch = foldr1 (<|) . fmap pure
{- | Given a list of ordered options, choose the first that statisfy the {- | Given a list of ordered options, choose the first that statisfy the
constraints, potentially returning nothing. constraints, potentially returning nothing.
-} -}
linearSearch' :: (MonadReduce l m) => [i] -> MaybeT m i linearSearch' :: (MonadReduce l m, MonadPlus m) => [i] -> m i
linearSearch' is = MaybeT $ linearSearch (NE.fromList (fmap Just is ++ [Nothing])) linearSearch' is = do
mp <- linearSearch (NE.fromList (fmap Just is ++ [Nothing]))
liftMaybe mp
-- | Given -- | Given
ddmin :: (MonadReduce l m) => [i] -> m [i] ddmin :: (MonadReduce l m) => [i] -> m [i]
......
...@@ -35,22 +35,22 @@ module Control.RTree ( ...@@ -35,22 +35,22 @@ module Control.RTree (
import Control.Applicative import Control.Applicative
import Control.Monad.Reader import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Functor.Identity import Data.Functor.Identity
import qualified Data.Map.Strict as Map
import Control.Monad.Reduce import Control.Monad.Reduce
import Control.Monad.State.Strict import qualified Data.Valuation as Val
type Valuation = Val.Valuation
data RTree l i data RTree l i
= SplitWith (Maybe l) (RTree l i) !(RTree l i) = SplitWith (Maybe l) (RTree l i) !(RTree l i)
| Done i | Done i
deriving (Functor) deriving (Functor)
type Valuation l = Map.Map l Bool
extract :: (Ord l) => Valuation l -> RTree l i -> i extract :: (Ord l) => Valuation l -> RTree l i -> i
extract v = \case extract v = \case
SplitWith ml lhs rhs -> case ml >>= (`Map.lookup` v) of SplitWith ml lhs rhs -> case ml >>= Val.truthValue v of
Just False -> extract v lhs Just False -> extract v lhs
_ -> extract v rhs _ -> extract v rhs
Done i -> i Done i -> i
...@@ -79,10 +79,10 @@ reduce p = checkgo ...@@ -79,10 +79,10 @@ reduce p = checkgo
checkgo v r = p (extract v r) *> go v r checkgo v r = p (extract v r) *> go v r
go v = \case go v = \case
Done i -> pure i Done i -> pure i
SplitWith (Just l) lhs rhs -> case Map.lookup l v of SplitWith (Just l) lhs rhs -> case Val.truthValue v l of
Just True -> checkgo v rhs Just True -> checkgo v rhs
Just False -> checkgo v lhs Just False -> checkgo v lhs
Nothing -> checkgo (Map.insert l False v) lhs <|> go (Map.insert l True v) rhs Nothing -> checkgo (Val.setTruthValue v l False) lhs <|> go (Val.setTruthValue v l True) rhs
SplitWith Nothing lhs rhs -> (checkgo v lhs <|> go v rhs) SplitWith Nothing lhs rhs -> (checkgo v lhs <|> go v rhs)
{-# INLINE reduce #-} {-# INLINE reduce #-}
...@@ -104,10 +104,10 @@ instance (Monad m, Ord l) => MonadReduce l (IRTreeT l m) where ...@@ -104,10 +104,10 @@ instance (Monad m, Ord l) => MonadReduce l (IRTreeT l m) where
ReState (uncons -> (a, as)) v -> ReState (uncons -> (a, as)) v ->
pure (a, ReState as v) pure (a, ReState as v)
Just l -> \case Just l -> \case
ReState as v@(Map.lookup l -> Just x) -> ReState as v@((`Val.truthValue` l) -> Just x) ->
pure (x, ReState as v) pure (x, ReState as v)
ReState (uncons -> (a, as)) v -> ReState (uncons -> (a, as)) v ->
pure (a, ReState as (Map.insert l a v)) pure (a, ReState as (Val.setTruthValue v l a))
where where
uncons [] = (True, []) uncons [] = (True, [])
uncons (a : as) = (a, as) uncons (a : as) = (a, as)
......
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
module Data.Valuation (
Valuation,
-- * Constructors
singleton,
fromMap,
fromPairs,
-- * Destructors
toMap,
toPairs,
-- * Access
setTruthValue,
truthValue,
condition,
-- * Helpers
viaMap,
viaMapF,
) where
import Data.Functor.Identity
import qualified Data.Map.Strict as Map
newtype Valuation l = Valuation {toMap :: Map.Map l Bool}
singleton :: (Ord l) => l -> Bool -> Valuation l
singleton l t = fromMap $ Map.singleton l t
viaMap :: (Map.Map l Bool -> Map.Map l Bool) -> Valuation l -> Valuation l
viaMap fn = runIdentity . viaMapF (Identity . fn)
viaMapF :: (Functor f) => (Map.Map l Bool -> f (Map.Map l Bool)) -> Valuation l -> f (Valuation l)
viaMapF fn = fmap fromMap . fn . toMap
fromMap :: Map.Map l Bool -> Valuation l
fromMap = Valuation
fromPairs :: (Ord l) => [(l, Bool)] -> Valuation l
fromPairs = Valuation . Map.fromList
toPairs :: Valuation l -> [(l, Bool)]
toPairs = Map.toList . toMap
truthValue :: (Ord l) => Valuation l -> l -> Maybe Bool
truthValue (Valuation m) = (`Map.lookup` m)
{- | Conditions a valuation with key value pair, if it conficts with the valuation,
it returns Nothing
-}
condition :: (Ord l) => Valuation l -> l -> Bool -> Maybe (Valuation l)
condition v l t =
viaMapF
( Map.alterF
\case
Just t' | t' /= t -> Nothing
_ -> Just (Just t)
l
)
v
setTruthValue :: (Ord l) => Valuation l -> l -> Bool -> Valuation l
setTruthValue v l t = viaMap (Map.insert l t) v
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment