module Control.Arrow.ArrowTree
( ArrowTree(..)
, Tree
)
where
import Data.Tree.Class (Tree)
import qualified Data.Tree.Class as T hiding (Tree)
import Control.Arrow
import Control.Arrow.ArrowList
import Control.Arrow.ArrowIf
infixl 5 />, //>, </
class (ArrowPlus a, ArrowIf a) => ArrowTree a where
mkLeaf :: Tree t => b -> a c (t b)
mkLeaf = t b -> a c (t b)
forall (a :: * -> * -> *) c b. ArrowList a => c -> a b c
constA (t b -> a c (t b)) -> (b -> t b) -> b -> a c (t b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> t b
forall (t :: * -> *) a. Tree t => a -> t a
T.mkLeaf
{-# INLINE mkLeaf #-}
mkTree :: Tree t => b -> [t b] -> a c (t b)
mkTree b
n = t b -> a c (t b)
forall (a :: * -> * -> *) c b. ArrowList a => c -> a b c
constA (t b -> a c (t b)) -> ([t b] -> t b) -> [t b] -> a c (t b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> [t b] -> t b
forall (t :: * -> *) a. Tree t => a -> [t a] -> t a
T.mkTree b
n
{-# INLINE mkTree #-}
getChildren :: Tree t => a (t b) (t b)
getChildren = (t b -> [t b]) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. ArrowList a => (b -> [c]) -> a b c
arrL t b -> [t b]
forall (t :: * -> *) a. Tree t => t a -> [t a]
T.getChildren
{-# INLINE getChildren #-}
getNode :: Tree t => a (t b) b
getNode = (t b -> b) -> a (t b) b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr t b -> b
forall (t :: * -> *) a. Tree t => t a -> a
T.getNode
{-# INLINE getNode #-}
hasNode :: Tree t => (b -> Bool) -> a (t b) (t b)
hasNode b -> Bool
p = (a (t b) b
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) b
getNode a (t b) b -> a b b -> a (t b) b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (b -> Bool) -> a b b
forall (a :: * -> * -> *) b. ArrowList a => (b -> Bool) -> a b b
isA b -> Bool
p) a (t b) b -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a b d -> a b d
`guards` a (t b) (t b)
forall (a :: * -> * -> *) b. ArrowList a => a b b
this
{-# INLINE hasNode #-}
setChildren :: Tree t => [t b] -> a (t b) (t b)
setChildren [t b]
cs = (t b -> t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ([t b] -> t b -> t b
forall (t :: * -> *) a. Tree t => [t a] -> t a -> t a
T.setChildren [t b]
cs)
{-# INLINE setChildren #-}
setNode :: Tree t => b -> a (t b) (t b)
setNode b
n = (t b -> t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (b -> t b -> t b
forall (t :: * -> *) a. Tree t => a -> t a -> t a
T.setNode b
n)
{-# INLINE setNode #-}
changeChildren :: Tree t => ([t b] -> [t b]) -> a (t b) (t b)
changeChildren [t b] -> [t b]
csf = (t b -> t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (([t b] -> [t b]) -> t b -> t b
forall (t :: * -> *) a. Tree t => ([t a] -> [t a]) -> t a -> t a
T.changeChildren [t b] -> [t b]
csf)
{-# INLINE changeChildren #-}
changeNode :: Tree t => (b -> b) -> a (t b) (t b)
changeNode b -> b
nf = (t b -> t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((b -> b) -> t b -> t b
forall (t :: * -> *) a. Tree t => (a -> a) -> t a -> t a
T.changeNode b -> b
nf)
{-# INLINE changeNode #-}
processChildren :: Tree t => a (t b) (t b) -> a (t b) (t b)
processChildren a (t b) (t b)
f = (t b -> b) -> a (t b) b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr t b -> b
forall (t :: * -> *) a. Tree t => t a -> a
T.getNode
a (t b) b -> a (t b) [t b] -> a (t b) (b, [t b])
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&&
a (t b) (t b) -> a (t b) [t b]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA ((t b -> [t b]) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. ArrowList a => (b -> [c]) -> a b c
arrL t b -> [t b]
forall (t :: * -> *) a. Tree t => t a -> [t a]
T.getChildren a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t b) (t b)
f)
a (t b) (b, [t b]) -> a (b, [t b]) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
(b -> [t b] -> t b) -> a (b, [t b]) (t b)
forall (a :: * -> * -> *) b1 b2 c.
ArrowList a =>
(b1 -> b2 -> c) -> a (b1, b2) c
arr2 b -> [t b] -> t b
forall (t :: * -> *) a. Tree t => a -> [t a] -> t a
T.mkTree
replaceChildren :: Tree t => a (t b) (t b) -> a (t b) (t b)
replaceChildren a (t b) (t b)
f = (t b -> b) -> a (t b) b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr t b -> b
forall (t :: * -> *) a. Tree t => t a -> a
T.getNode
a (t b) b -> a (t b) [t b] -> a (t b) (b, [t b])
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&&
a (t b) (t b) -> a (t b) [t b]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA a (t b) (t b)
f
a (t b) (b, [t b]) -> a (b, [t b]) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
(b -> [t b] -> t b) -> a (b, [t b]) (t b)
forall (a :: * -> * -> *) b1 b2 c.
ArrowList a =>
(b1 -> b2 -> c) -> a (b1, b2) c
arr2 b -> [t b] -> t b
forall (t :: * -> *) a. Tree t => a -> [t a] -> t a
T.mkTree
(/>) :: Tree t => a b (t c) -> a (t c) d -> a b d
a b (t c)
f /> a (t c) d
g = a b (t c)
f a b (t c) -> a (t c) d -> a b d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t c) (t c)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t c) (t c) -> a (t c) d -> a (t c) d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t c) d
g
{-# INLINE (/>) #-}
(//>) :: Tree t => a b (t c) -> a (t c) d -> a b d
a b (t c)
f //> a (t c) d
g = a b (t c)
f a b (t c) -> a (t c) d -> a b d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t c) (t c)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t c) (t c) -> a (t c) d -> a (t c) d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t c) d -> a (t c) d
forall (a :: * -> * -> *) (t :: * -> *) b c.
(ArrowTree a, Tree t) =>
a (t b) c -> a (t b) c
deep a (t c) d
g
{-# INLINE (//>) #-}
(</) :: Tree t => a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
a (t b) (t b)
f </ a (t b) (t b)
g = a (t b) (t b)
f a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c d.
ArrowIf a =>
a b c -> a c d -> a b c
`containing` (a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t b) (t b)
g)
{-# INLINE (</) #-}
deep :: Tree t => a (t b) c -> a (t b) c
deep a (t b) c
f = a (t b) c
f
a (t b) c -> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) b c. ArrowIf a => a b c -> a b c -> a b c
`orElse`
(a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t b) (t b) -> a (t b) c -> a (t b) c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) (t :: * -> *) b c.
(ArrowTree a, Tree t) =>
a (t b) c -> a (t b) c
deep a (t b) c
f)
deepest :: Tree t => a (t b) c -> a (t b) c
deepest a (t b) c
f = (a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t b) (t b) -> a (t b) c -> a (t b) c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) (t :: * -> *) b c.
(ArrowTree a, Tree t) =>
a (t b) c -> a (t b) c
deepest a (t b) c
f)
a (t b) c -> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) b c. ArrowIf a => a b c -> a b c -> a b c
`orElse`
a (t b) c
f
multi :: Tree t => a (t b) c -> a (t b) c
multi a (t b) c
f = a (t b) c
f
a (t b) c -> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+>
(a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren a (t b) (t b) -> a (t b) c -> a (t b) c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (t b) c -> a (t b) c
forall (a :: * -> * -> *) (t :: * -> *) b c.
(ArrowTree a, Tree t) =>
a (t b) c -> a (t b) c
multi a (t b) c
f)
processBottomUp :: Tree t => a (t b) (t b) -> a (t b) (t b)
processBottomUp a (t b) (t b)
f = a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processChildren (a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processBottomUp a (t b) (t b)
f)
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a (t b) (t b)
f
processTopDown :: Tree t => a (t b) (t b) -> a (t b) (t b)
processTopDown a (t b) (t b)
f = a (t b) (t b)
f
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processChildren (a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processTopDown a (t b) (t b)
f)
processBottomUpWhenNot
:: Tree t => a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
processBottomUpWhenNot a (t b) (t b)
f a (t b) (t b)
p
= ( a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processChildren (a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
processBottomUpWhenNot a (t b) (t b)
f a (t b) (t b)
p)
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a (t b) (t b)
f
) a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. ArrowIf a => a b b -> a b c -> a b b
`whenNot` a (t b) (t b)
p
processTopDownUntil :: Tree t => a (t b) (t b) -> a (t b) (t b)
processTopDownUntil a (t b) (t b)
f
= a (t b) (t b)
f
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. ArrowIf a => a b c -> a b c -> a b c
`orElse`
a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processChildren (a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processTopDownUntil a (t b) (t b)
f)
insertChildrenAt :: Tree t => Int -> a (t b) (t b) -> a (t b) (t b)
insertChildrenAt Int
i a (t b) (t b)
f
= a (t b) (t b) -> a (t b) [t b]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA a (t b) (t b)
f a (t b) [t b] -> a (t b) (t b) -> a (t b) ([t b], t b)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a (t b) (t b)
forall (a :: * -> * -> *) b. ArrowList a => a b b
this a (t b) ([t b], t b) -> a ([t b], t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ([t b] -> t b -> t b) -> a ([t b], t b) (t b)
forall (a :: * -> * -> *) b1 b2 c.
ArrowList a =>
(b1 -> b2 -> c) -> a (b1, b2) c
arr2 [t b] -> t b -> t b
forall (t :: * -> *) a. Tree t => [t a] -> t a -> t a
insertAt
where
insertAt :: [t a] -> t a -> t a
insertAt [t a]
newcs
= ([t a] -> [t a]) -> t a -> t a
forall (t :: * -> *) a. Tree t => ([t a] -> [t a]) -> t a -> t a
T.changeChildren (\ [t a]
cs -> let
([t a]
cs1, [t a]
cs2) = Int -> [t a] -> ([t a], [t a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
i [t a]
cs
in
[t a]
cs1 [t a] -> [t a] -> [t a]
forall a. [a] -> [a] -> [a]
++ [t a]
newcs [t a] -> [t a] -> [t a]
forall a. [a] -> [a] -> [a]
++ [t a]
cs2
)
insertChildrenAfter :: Tree t => a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
insertChildrenAfter a (t b) (t b)
p a (t b) (t b)
f
= a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
replaceChildren
( ( ( a (t b) (t b) -> a (t b) [t b]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b)
getChildren
a (t b) [t b] -> a [t b] ([t b], [t b]) -> a (t b) ([t b], [t b])
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a (t b) (t b) -> a [t b] ([t b], [t b])
forall (a :: * -> * -> *) b. ArrowIf a => a b b -> a [b] ([b], [b])
spanA a (t b) (t b)
p
)
a (t b) ([t b], [t b])
-> a (t b) [t b] -> a (t b) (([t b], [t b]), [t b])
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&&
a (t b) (t b) -> a (t b) [t b]
forall (a :: * -> * -> *) b c. ArrowList a => a b c -> a b [c]
listA a (t b) (t b)
f
)
a (t b) (([t b], [t b]), [t b])
-> a (([t b], [t b]), [t b]) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (([t b], [t b]) -> [t b] -> [t b])
-> a (([t b], [t b]), [t b]) (t b)
forall (a :: * -> * -> *) b c d.
ArrowList a =>
(b -> c -> [d]) -> a (b, c) d
arr2L (\ ([t b]
xs1, [t b]
xs2) [t b]
xs -> [t b]
xs1 [t b] -> [t b] -> [t b]
forall a. [a] -> [a] -> [a]
++ [t b]
xs [t b] -> [t b] -> [t b]
forall a. [a] -> [a] -> [a]
++ [t b]
xs2)
)
insertTreeTemplate :: Tree t =>
a (t b) (t b) ->
[IfThen (a (t b) c) (a (t b) (t b))] ->
a (t b) (t b)
insertTreeTemplate a (t b) (t b)
template [IfThen (a (t b) c) (a (t b) (t b))]
choices
= t b -> a (t b) (t b)
insertTree (t b -> a (t b) (t b)) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) c b d.
ArrowList a =>
(c -> a b d) -> a b c -> a b d
$< a (t b) (t b)
forall (a :: * -> * -> *) b. ArrowList a => a b b
this
where
insertTree :: t b -> a (t b) (t b)
insertTree t b
t
= a (t b) (t b)
template
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>
a (t b) (t b)
processTemplate
where
processTemplate :: a (t b) (t b)
processTemplate
= [IfThen (a (t b) c) (a (t b) (t b))] -> a (t b) (t b)
forall (a :: * -> * -> *) b c d.
ArrowIf a =>
[IfThen (a b c) (a b d)] -> a b d
choiceA [IfThen (a (t b) c) (a (t b) (t b))]
forall a. [IfThen (a (t b) c) (a a (t b))]
choices'
a (t b) (t b) -> a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) b c. ArrowIf a => a b c -> a b c -> a b c
`orElse`
a (t b) (t b) -> a (t b) (t b)
forall (a :: * -> * -> *) (t :: * -> *) b.
(ArrowTree a, Tree t) =>
a (t b) (t b) -> a (t b) (t b)
processChildren a (t b) (t b)
processTemplate
choices' :: [IfThen (a (t b) c) (a a (t b))]
choices'
= (IfThen (a (t b) c) (a (t b) (t b))
-> IfThen (a (t b) c) (a a (t b)))
-> [IfThen (a (t b) c) (a (t b) (t b))]
-> [IfThen (a (t b) c) (a a (t b))]
forall a b. (a -> b) -> [a] -> [b]
map IfThen (a (t b) c) (a (t b) (t b))
-> IfThen (a (t b) c) (a a (t b))
forall (cat :: * -> * -> *) a c a.
ArrowList cat =>
IfThen a (cat (t b) c) -> IfThen a (cat a c)
feedTree [IfThen (a (t b) c) (a (t b) (t b))]
choices
feedTree :: IfThen a (cat (t b) c) -> IfThen a (cat a c)
feedTree (a
cond :-> cat (t b) c
action)
= a
cond a -> cat a c -> IfThen a (cat a c)
forall a b. a -> b -> IfThen a b
:-> (t b -> cat a (t b)
forall (a :: * -> * -> *) c b. ArrowList a => c -> a b c
constA t b
t cat a (t b) -> cat (t b) c -> cat a c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> cat (t b) c
action)