Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rtree
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
chrg
rtree
Commits
e0cc21e2
Commit
e0cc21e2
authored
1 year ago
by
chrg
Browse files
Options
Downloads
Patches
Plain Diff
Improve the code
parent
4bd2472b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bin/rtree-c/Main.hs
+1
-1
1 addition, 1 deletion
bin/rtree-c/Main.hs
src/Control/RTree.hs
+14
-92
14 additions, 92 deletions
src/Control/RTree.hs
with
15 additions
and
93 deletions
bin/rtree-c/Main.hs
+
1
−
1
View file @
e0cc21e2
...
...
@@ -114,7 +114,7 @@ run = do
unless
t
do
liftIO
exitFailure
l
<-
runMaybeT
(
reduce
Fast
(
check
file
)
(
Map
.
singleton
(
C
.
internalIdent
"main"
)
True
)
(
reduceC
c
))
l
<-
runMaybeT
(
reduce
IO
(
check
file
)
(
Map
.
singleton
(
C
.
internalIdent
"main"
)
True
)
(
reduceC
c
))
case
l
of
Just
l'
->
do
...
...
This diff is collapsed.
Click to expand it.
src/Control/RTree.hs
+
14
−
92
View file @
e0cc21e2
...
...
@@ -82,10 +82,8 @@ reduce p = checkgo
where
go
=
\
case
(
Done
i
)
->
pure
i
(
Split
lhs
rhs
)
->
(
checkgo
lhs
Control
.
Applicative
.<|>
go
rhs
)
(
SplitOn
_
lhs
rhs
)
->
(
checkgo
lhs
Control
.
Applicative
.<|>
go
rhs
)
(
Split
lhs
rhs
)
->
(
checkgo
lhs
<|>
go
rhs
)
(
SplitOn
_
lhs
rhs
)
->
(
checkgo
lhs
<|>
go
rhs
)
checkgo
rt
=
p
(
extract
rt
)
*>
go
rt
{-# SPECIALIZE reduce :: (i -> MaybeT IO ()) -> RTree l i -> MaybeT IO i #-}
...
...
@@ -120,11 +118,14 @@ reduceL p = checkgo
data
ReState
l
=
ReState
!
[
Bool
]
!
(
Valuation
l
)
newtype
ReReduce
l
i
=
ReReduce
{
runReReduce
::
IORef
(
ReState
l
)
->
IO
i
}
{- | A faster version of the RTree which simply reruns the reducer program instead
of building a tree.
-}
newtype
IORTree
l
i
=
IORTree
{
runIORTree
::
IORef
(
ReState
l
)
->
IO
i
}
deriving
(
Functor
,
Applicative
,
Monad
)
via
(
ReaderT
(
IORef
(
ReState
l
))
IO
)
instance
(
Ord
l
)
=>
MonadReduce
l
(
ReReduc
e
l
)
where
split
ml
r1
r2
=
ReReduc
e
\
ref
->
do
instance
(
Ord
l
)
=>
MonadReduce
l
(
IORTre
e
l
)
where
split
ml
r1
r2
=
IORTre
e
\
ref
->
do
test
<-
case
ml
of
Nothing
->
do
atomicModifyIORef'
...
...
@@ -142,17 +143,17 @@ instance (Ord l) => MonadReduce l (ReReduce l) where
ReState
[]
v
->
(
ReState
[]
(
Map
.
insert
l
True
v
),
False
)
)
if
test
then
run
ReReduc
e
r1
ref
else
run
ReReduc
e
r2
ref
then
run
IORTre
e
r1
ref
else
run
IORTre
e
r2
ref
reduce
Fast
reduce
IO
::
forall
m
l
i
.
(
MonadIO
m
,
Ord
l
)
=>
(
Valuation
l
->
i
->
MaybeT
m
()
)
->
Valuation
l
->
ReReduc
e
l
i
->
IORTre
e
l
i
->
MaybeT
m
i
reduce
Fast
p
v
(
ReReduc
e
ext
)
=
MaybeT
$
go
[]
reduce
IO
p
v
(
IORTre
e
ext
)
=
MaybeT
$
go
[]
where
go
pth
=
do
ref
<-
liftIO
$
newIORef
(
ReState
pth
v
)
...
...
@@ -169,7 +170,7 @@ reduceFast p v (ReReduce ext) = MaybeT $ go []
pure
Nothing
|
otherwise
->
pure
(
Just
i
)
{-# INLINE reduce
Fast
#-}
{-# INLINE reduce
IO
#-}
-- Combinators
...
...
@@ -194,85 +195,6 @@ collectNonEmpty fn as = do
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.
-- 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.
given
::
RTree
Void
Bool
given
=
pure
False
<|
pure
True
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment