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
7fb3f667
Commit
7fb3f667
authored
1 year ago
by
chrg
Browse files
Options
Downloads
Patches
Plain Diff
Add ddmin
parent
5b0eed9d
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
package.yaml
+2
-1
2 additions, 1 deletion
package.yaml
rtree.cabal
+2
-1
2 additions, 1 deletion
rtree.cabal
src/Control/RTree.hs
+72
-19
72 additions, 19 deletions
src/Control/RTree.hs
with
76 additions
and
21 deletions
package.yaml
+
2
−
1
View file @
7fb3f667
...
@@ -9,7 +9,8 @@ ghc-options: -Wall -fno-warn-incomplete-uni-patterns -Werror
...
@@ -9,7 +9,8 @@ ghc-options: -Wall -fno-warn-incomplete-uni-patterns -Werror
dependencies
:
dependencies
:
-
base >= 4.9 && <
5
-
base >= 4.9 && <
5
-
containers
-
transformers
-
mtl
-
language-c
-
language-c
library
:
library
:
...
...
This diff is collapsed.
Click to expand it.
rtree.cabal
+
2
−
1
View file @
7fb3f667
...
@@ -18,6 +18,7 @@ library
...
@@ -18,6 +18,7 @@ library
ghc-options: -Wall -fno-warn-incomplete-uni-patterns -Werror
ghc-options: -Wall -fno-warn-incomplete-uni-patterns -Werror
build-depends:
build-depends:
base >=4.9 && <5
base >=4.9 && <5
, containers
, language-c
, language-c
, mtl
, transformers
default-language: Haskell2010
default-language: Haskell2010
This diff is collapsed.
Click to expand it.
src/Control/RTree.hs
+
72
−
19
View file @
7fb3f667
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
-- |
{- |
--
-- Module: Control.RTree
Module: Control.RTree
-}
module
Control.RTree
where
module
Control.RTree
where
import
Control.Monad
(
MonadPlus
(
..
),
ap
,
liftM2
)
import
Control.Monad
import
Data.Functor
import
Data.Functor
import
Data.Functor.Identity
import
qualified
Data.List.NonEmpty
as
NE
-- | The reduction tree, parameterized by a genrative functor 'f'.
-- | The reduction tree, parameterized by a genrative functor 'f'.
data
ReduceT
f
i
data
ReduceT
f
i
=
Done
i
=
Done
i
|
f
(
ReduceT
f
i
)
:<|
ReduceT
f
i
|
f
(
ReduceT
f
i
)
:<|
ReduceT
f
i
type
RTree
i
=
ReduceT
Identity
i
{- | The reduction tree is a functor, but only over order-embeddings,
this means that i@f a <= f b@ iff @a <= b@.
-}
instance
(
Functor
f
)
=>
Functor
(
ReduceT
f
)
where
instance
(
Functor
f
)
=>
Functor
(
ReduceT
f
)
where
fmap
f
=
\
case
fmap
f
=
\
case
Done
i
->
Done
(
f
i
)
Done
i
->
Done
(
f
i
)
...
@@ -60,26 +70,69 @@ given = pure False <| pure True
...
@@ -60,26 +70,69 @@ given = pure False <| pure True
-- | A reducer is something that takes an inputs and returns a reduction tree.
-- | A reducer is something that takes an inputs and returns a reduction tree.
type
Reducer
m
i
=
i
->
ReduceT
m
i
type
Reducer
m
i
=
i
->
ReduceT
m
i
-- | A reducer should extract itself
{- | A reducer should extract itself
-- @
@
-- extract . red = id
extract . red = id
-- @
@
-}
lawReduceId
::
(
Eq
i
)
=>
Reducer
m
i
->
i
->
Bool
lawReduceId
::
(
Eq
i
)
=>
Reducer
m
i
->
i
->
Bool
lawReduceId
red
i
=
extract
(
red
i
)
==
i
lawReduceId
red
i
=
extract
(
red
i
)
==
i
-- | Reducing a list one element at a time.
rList
::
(
Applicative
m
)
=>
Reducer
m
[
a
]
rList
::
(
Applicative
m
)
=>
Reducer
m
[
a
]
rList
=
\
case
rList
=
\
case
[]
->
Done
[]
[]
->
Done
[]
a
:
as
->
rList
as
<|
(
a
:
)
<$>
rList
as
a
:
as
->
rList
as
<|
(
a
:
)
<$>
rList
as
rBinaryList
::
(
Applicative
m
)
=>
Reducer
m
[
a
]
{- | Binary reduction on the list assumming suffixes all contain eachother:
rBinaryList
=
\
case
@[] < [c] < [b, c] < [a,b,c]@
[]
->
Done
[]
-}
as
->
Done
[]
<|
go
as
rSuffixList
::
(
Applicative
m
)
=>
Reducer
m
[
a
]
where
rSuffixList
as
=
do
go
=
\
case
res
<-
binarySearch
(
NE
.
reverse
(
NE
.
tails
as
))
[]
->
error
"unexpected"
case
res
of
[
a
]
->
Done
[
a
]
[]
->
pure
[]
as
->
go
l
<|
liftM2
(
<>
)
(
go
f
)
(
Done
[]
<|
go
l
)
a
:
as'
->
(
a
:
)
<$>
rSuffixList
as'
where
(
f
,
l
)
=
splitAt
(
length
as
`
div
`
2
)
as
{- | Given a progression of inputs that are progressively larger, pick the smallest using
binary search.
-}
binarySearch
::
(
Applicative
m
)
=>
NE
.
NonEmpty
i
->
ReduceT
m
i
binarySearch
=
\
case
a
NE
.:|
[]
->
Done
a
d
->
binarySearch
f
<|
binarySearch
l
where
(
NE
.
fromList
->
f
,
NE
.
fromList
->
l
)
=
NE
.
splitAt
(
NE
.
length
d
`
div
`
2
)
d
-- | Given a list of orderd options, the
linearSearch
::
(
Applicative
m
)
=>
NE
.
NonEmpty
i
->
ReduceT
m
i
linearSearch
=
foldr1
(
<|
)
.
fmap
Done
-- | Given a list of orderd options, the
linearSearch'
::
(
Applicative
m
)
=>
[
i
]
->
ReduceT
m
(
Maybe
i
)
linearSearch'
is
=
linearSearch
(
NE
.
fromList
$
fmap
Just
is
++
[
Nothing
])
-- | Given
ddmin
::
(
Applicative
m
)
=>
[
i
]
->
ReduceT
m
[
i
]
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
)
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