Newer
Older
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Applicative
import Control.Monad.Reader
import Control.Monad.Trans.Maybe
class (Monad m) => MonadReduce l m | m -> l where
split :: Maybe l -> m i -> m i -> m i
(<|) :: (MonadReduce l r) => r i -> r i -> r i
r1 <| r2 = split Nothing r1 r2
splitOn :: (MonadReduce l r) => l -> r i -> r i -> r i
splitOn l = split (Just l)
data RTree l i
= Split (RTree l i) !(RTree l i)
| SplitOn !l (RTree l i) !(RTree l i)
extract :: RTree l i -> i
extract = \case
Split _ rhs -> extract rhs
SplitOn _ _ rhs -> extract rhs
Split lhs rhs ->
Split (lhs >>= f) (rhs >>= f)
SplitOn l lhs rhs ->
SplitOn l (lhs >>= f) (rhs >>= f)
instance MonadReduce l (RTree l) where
split = \case
Just n -> SplitOn n
Nothing -> Split
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(Split lhs rhs) ->
(checkgo lhs Control.Applicative.<|> go rhs)
(SplitOn _ lhs rhs) ->
(checkgo lhs Control.Applicative.<|> go rhs)
checkgo rt = p (extract rt) *> go rt
{-# SPECIALIZE reduce :: (i -> MaybeT IO ()) -> RTree l i -> MaybeT IO i #-}
type Valuation l = Map.Map l Bool
extractL :: (Ord l) => Valuation l -> RTree l i -> i
extractL v = \case
Split _ rhs -> extractL v rhs
SplitOn l lhs rhs -> case Map.lookup l v of
Just False -> extractL v lhs
_ -> extractL v rhs
Done i -> i
reduceL
:: forall m l i
. (Alternative m, Ord l)
=> (Valuation l -> i -> m ())
-> Valuation l
-> RTree l i
-> m i
reduceL p = checkgo
where
checkgo v r = p v (extractL v r) *> go v r
go v = \case
Done i -> pure i
SplitOn l lhs rhs -> case Map.lookup l v of
Just True -> checkgo v rhs
Just False -> checkgo v lhs
Nothing -> checkgo (Map.insert l False v) lhs <|> go (Map.insert l True v) rhs
Split lhs rhs -> (checkgo v lhs <|> go v rhs)
{-# INLINE reduceL #-}
data ReState l = ReState ![Bool] !(Valuation l)
newtype ReReduce l i = ReReduce {runReReduce :: IORef (ReState l) -> IO i}
deriving (Functor, Applicative, Monad) via (ReaderT (IORef (ReState l)) IO)
instance (Ord l) => MonadReduce l (ReReduce l) where
split ml r1 r2 = ReReduce \ref -> do
test <- case ml of
Nothing -> do
atomicModifyIORef'
ref
( \case
ReState (a : as) v -> (ReState as v, a)
ReState [] v -> (ReState [] v, False)
)
Just l -> do
atomicModifyIORef'
ref
( \case
ReState as v@(Map.lookup l -> Just x) -> (ReState as v, not x)
ReState (a : as) v -> (ReState as (Map.insert l (not a) v), a)
ReState [] v -> (ReState [] (Map.insert l True v), False)
)
if test
then runReReduce r1 ref
else runReReduce r2 ref
reduceFast
:: forall m l i
. (MonadIO m, Ord l)
=> (Valuation l -> i -> MaybeT m ())
-> Valuation l
-> ReReduce l i
-> MaybeT m i
reduceFast p v (ReReduce ext) = MaybeT $ go []
where
go pth = do
ref <- liftIO $ newIORef (ReState pth v)
i <- liftIO $ ext ref
ReState r v' <- liftIO $ readIORef ref
case r of
[] -> do
t <- isJust <$> runMaybeT (p v' i)
if t
then go (pth <> [True])
else go (pth <> [False])
_
| null pth ->
pure Nothing
| otherwise ->
pure (Just i)
{-# INLINE reduceFast #-}
-- Combinators
type MRTree l = MaybeT (RTree l)
instance (MonadReduce l m) => MonadReduce l (MaybeT m) where
split m (MaybeT lhs) (MaybeT rhs) = MaybeT (split m lhs rhs)
-- | Given a list of item try to remove each of them the list.
collect :: (MonadReduce l m) => (a -> MaybeT m b) -> [a] -> m [b]
collect fn = fmap catMaybes . traverse (runMaybeT . fn)
{-# INLINE collect #-}
collectNonEmpty' :: (MonadReduce l m) => (a -> MaybeT m b) -> [a] -> MaybeT m [b]
collectNonEmpty' fn as =
NE.toList <$> collectNonEmpty fn as
{-# INLINE collectNonEmpty' #-}
collectNonEmpty :: (MonadReduce l m) => (a -> MaybeT m b) -> [a] -> MaybeT m (NE.NonEmpty b)
collectNonEmpty fn as = do
as' <- lift . fmap catMaybes . traverse (runMaybeT . fn) $ as
MaybeT . pure $ NE.nonEmpty as'
{-# INLINE collectNonEmpty #-}
-- newtype LTree l i = LTree {runLTree :: Valuation l -> Maybe (RTree l i)}
-- deriving (Functor)
--
-- instance Applicative (LTree l) where
-- pure i = LTree{runLTree = \_ -> Just $ Done i}
-- (<*>) = ap
--
-- instance Monad (LTree l) where
-- LTree ma >>= f = LTree \l ->
-- case ma l of
-- Done i -> f i
-- Split l lhs rhs ->
-- extract' :: RTree l i -> i
-- extract' = \case
-- RTree' (Split _ _ v) -> extract' v
-- Done v -> v
--
-- instance Functor (RTree l) where
-- fmap f (Done i) = Done (f i)
-- fmap f (RTree' r) = RTree' (fmap (fmap f) r)
--
--
-- instance MonadFree (RTreeF l) (RTree' l) where
-- wrap = RTree'
-- {-# INLINE wrap #-}
-- | Reduce an input using a monad.
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
-- newtype I l i = I ([(l, Bool)] -> RTreeI l i)
--
-- data RTreeI l i
-- = RTreeI (RTreeF l (I l i))
-- | DoneI !i
-- -- This is not a great defintions, as the i does not depend on
-- -- the current i, but instead on the final I.
-- data RTreeIO j i = RTreeIO ((j -> IO Bool) -> IO i) j
--
-- extractIO :: RTreeIO j i -> j
-- extractIO (RTreeIO _ i) = i
-- instance Functor (RTreeIO j) where
-- fmap f (RTreeIO mf i) = RTreeIO (\h -> f <$> mf (h . f)) (f i)
--
-- instance Applicative (RTreeIO j) where
-- pure i = RTreeIO (\_ -> pure i) i
-- (<*>) = ap
--
-- -- RTreeIO f fi <*> RTreeIO a ai = RTreeIO (f <*> a) (fi ai)
--
-- instance Monad (RTreeIO j) where
-- RTreeIO (ma :: ((a -> IO Bool) -> IO a)) a >>= (f :: (a -> RTreeIO b)) =
-- RTreeIO undefined (extractIO $ f a)
--
-- instance MonadFree (RTreeF Void) (RTreeIO j) where
-- wrap (Split Nothing (RTreeIO lhs le) (RTreeIO rhs re)) =
-- RTreeIO
-- ( \p ->
-- p le >>= \case
-- True -> lhs p
-- False -> rhs p
-- )
-- re
-- wrap (Split (Just x) _ _) = absurd x
-- reduceIO
-- :: forall i
-- . (i -> IO Bool)
-- -> RTreeIO j i
-- -> IO (Maybe i)
-- reduceIO p (RTreeIO rt i) = runMaybeT do
-- let (mi, i') = foldR go $ fmap (\i -> (pure i, i)) t
-- p i' *> mi
-- where
-- go :: RTreeF l (IO i, i) -> (IO i, i)
-- go (Split _ (lhs, le) (rhs, re)) =
-- ((p le *> lhs) <|> rhs, re)
-- | Split the world on a fact. False it does not happen, and True it does happen.
{- | A reducer should extract itself
@
extract . red = id
@
-}
{- | Binary reduction on the list assumming suffixes all contain eachother:
@[] < [c] < [b, c] < [a,b,c]@
-}
case res of
[] -> pure []
a : as' -> (a :) <$> rSuffixList as'
{- | Given a progression of inputs that are progressively larger, pick the smallest using
binary search.
-}
where
(NE.fromList -> f, NE.fromList -> l) = NE.splitAt (NE.length d `div` 2) d
{- | Given a progression of inputs that are progressively larger, pick the smallest using
binary search.
-}
exponentialSearch = go 1
where
go n = \case
d
| n >= NE.length d -> binarySearch d
| otherwise -> go (n * 2) l <| binarySearch f
where
(NE.fromList -> f, NE.fromList -> l) = NE.splitAt n d
nonEmptyOr :: String -> [a] -> NE.NonEmpty a
nonEmptyOr msg ls = case NE.nonEmpty ls of
Just a -> a
Nothing -> error msg
linearSearch' is = linearSearch (NE.fromList $ fmap Just is ++ [Nothing])
-- | Given
ddmin = \case
[] -> pure []
[a] -> pure [a]
as -> go 2 as
where
go n lst
| n' <= 0 = pure lst
| otherwise = do
r <- linearSearch' (partitions n' lst ++ composites n' lst)
case r of
Nothing -> go (n * 2) lst <| pure lst -- (for efficiency :D)
Just lst' -> ddmin lst'
where
n' = length lst `div` n
partitions n lst =
case lst of
[] -> []
_ -> let (h, r) = splitAt n lst in h : partitions n r
composites n lst =
case lst of
[] -> []
_ -> let (h, r) = splitAt n lst in r : fmap (h ++) (composites n r)