module Data.Tree.NTree.Edit
where
import Data.Maybe
import Data.Tree.NTree.TypeDefs
editNTreeBottomUp :: (NTree a -> Maybe [NTree a]) -> NTree a -> [NTree a]
editNTreeBottomUp :: (NTree a -> Maybe [NTree a]) -> NTree a -> [NTree a]
editNTreeBottomUp NTree a -> Maybe [NTree a]
f NTree a
t0 = [NTree a]
-> ([NTree a] -> [NTree a]) -> Maybe [NTree a] -> [NTree a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [NTree a
t0] [NTree a] -> [NTree a]
forall a. a -> a
id (Maybe [NTree a] -> [NTree a])
-> (NTree a -> Maybe [NTree a]) -> NTree a -> [NTree a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NTree a -> Maybe [NTree a]
editNTreeBU (NTree a -> [NTree a]) -> NTree a -> [NTree a]
forall a b. (a -> b) -> a -> b
$ NTree a
t0
where
editNTreeBU :: NTree a -> Maybe [NTree a]
editNTreeBU t :: NTree a
t@(NTree a
n [NTree a]
cs)
| Maybe [NTree a] -> Bool
forall a. Maybe a -> Bool
isNothing Maybe [NTree a]
r'
Bool -> Bool -> Bool
&&
Maybe [NTree a] -> Bool
forall a. Maybe a -> Bool
isJust Maybe [NTree a]
cl' = [NTree a] -> Maybe [NTree a]
forall a. a -> Maybe a
Just [NTree a
t']
| Bool
otherwise = Maybe [NTree a]
r'
where
cl' :: Maybe [NTree a]
cl' = [NTree a] -> Maybe [NTree a]
editNTreesBU [NTree a]
cs
t' :: NTree a
t' = case Maybe [NTree a]
cl' of
Maybe [NTree a]
Nothing -> NTree a
t
Just cs' -> a -> [NTree a] -> NTree a
forall a. a -> NTrees a -> NTree a
NTree a
n [NTree a]
cs'
r' :: Maybe [NTree a]
r' = NTree a -> Maybe [NTree a]
f NTree a
t'
editNTreesBU :: [NTree a] -> Maybe [NTree a]
editNTreesBU [] = Maybe [NTree a]
forall a. Maybe a
Nothing
editNTreesBU (NTree a
t : [NTree a]
ts) = Maybe [NTree a] -> Maybe [NTree a] -> Maybe [NTree a]
mergeRes
(NTree a -> Maybe [NTree a]
editNTreeBU NTree a
t )
([NTree a] -> Maybe [NTree a]
editNTreesBU [NTree a]
ts)
where
mergeRes :: Maybe [NTree a] -> Maybe [NTree a] -> Maybe [NTree a]
mergeRes Maybe [NTree a]
r' = case Maybe [NTree a]
r' of
Maybe [NTree a]
Nothing -> Maybe [NTree a]
-> ([NTree a] -> Maybe [NTree a])
-> Maybe [NTree a]
-> Maybe [NTree a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe [NTree a]
forall a. Maybe a
Nothing ([NTree a] -> Maybe [NTree a]
forall a. a -> Maybe a
Just ([NTree a] -> Maybe [NTree a])
-> ([NTree a] -> [NTree a]) -> [NTree a] -> Maybe [NTree a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NTree a
t NTree a -> [NTree a] -> [NTree a]
forall a. a -> [a] -> [a]
:))
Just [NTree a]
ts' -> [NTree a] -> Maybe [NTree a]
forall a. a -> Maybe a
Just ([NTree a] -> Maybe [NTree a])
-> (Maybe [NTree a] -> [NTree a])
-> Maybe [NTree a]
-> Maybe [NTree a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([NTree a]
ts' [NTree a] -> [NTree a] -> [NTree a]
forall a. [a] -> [a] -> [a]
++) ([NTree a] -> [NTree a])
-> (Maybe [NTree a] -> [NTree a]) -> Maybe [NTree a] -> [NTree a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NTree a] -> Maybe [NTree a] -> [NTree a]
forall a. a -> Maybe a -> a
fromMaybe [NTree a]
ts
mapNTree' :: (a -> Maybe a) -> NTree a -> NTree a
mapNTree' :: (a -> Maybe a) -> NTree a -> NTree a
mapNTree' a -> Maybe a
f NTree a
t0 = NTree a -> (NTree a -> NTree a) -> Maybe (NTree a) -> NTree a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe NTree a
t0 NTree a -> NTree a
forall a. a -> a
id (Maybe (NTree a) -> NTree a)
-> (NTree a -> Maybe (NTree a)) -> NTree a -> NTree a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NTree a -> Maybe (NTree a)
map' (NTree a -> NTree a) -> NTree a -> NTree a
forall a b. (a -> b) -> a -> b
$ NTree a
t0
where
map' :: NTree a -> Maybe (NTree a)
map' (NTree a
n NTrees a
cs) = Maybe a -> Maybe (NTrees a) -> Maybe (NTree a)
mergeRes (a -> Maybe a
f a
n) (NTrees a -> Maybe (NTrees a)
maps' NTrees a
cs)
where
mergeRes :: Maybe a -> Maybe (NTrees a) -> Maybe (NTree a)
mergeRes Maybe a
Nothing Maybe (NTrees a)
Nothing = Maybe (NTree a)
forall a. Maybe a
Nothing
mergeRes Maybe a
Nothing (Just NTrees a
cs') = NTree a -> Maybe (NTree a)
forall a. a -> Maybe a
Just (a -> NTrees a -> NTree a
forall a. a -> NTrees a -> NTree a
NTree a
n NTrees a
cs')
mergeRes (Just a
n') Maybe (NTrees a)
cl = NTree a -> Maybe (NTree a)
forall a. a -> Maybe a
Just (a -> NTrees a -> NTree a
forall a. a -> NTrees a -> NTree a
NTree a
n' (NTrees a -> Maybe (NTrees a) -> NTrees a
forall a. a -> Maybe a -> a
fromMaybe NTrees a
cs Maybe (NTrees a)
cl))
maps' :: NTrees a -> Maybe (NTrees a)
maps' [] = Maybe (NTrees a)
forall a. Maybe a
Nothing
maps' (NTree a
t : NTrees a
ts) = Maybe (NTree a) -> Maybe (NTrees a) -> Maybe (NTrees a)
mergeRes
(NTree a -> Maybe (NTree a)
map' NTree a
t )
(NTrees a -> Maybe (NTrees a)
maps' NTrees a
ts)
where
mergeRes :: Maybe (NTree a) -> Maybe (NTrees a) -> Maybe (NTrees a)
mergeRes Maybe (NTree a)
r' = case Maybe (NTree a)
r' of
Maybe (NTree a)
Nothing -> Maybe (NTrees a)
-> (NTrees a -> Maybe (NTrees a))
-> Maybe (NTrees a)
-> Maybe (NTrees a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe (NTrees a)
forall a. Maybe a
Nothing (NTrees a -> Maybe (NTrees a)
forall a. a -> Maybe a
Just (NTrees a -> Maybe (NTrees a))
-> (NTrees a -> NTrees a) -> NTrees a -> Maybe (NTrees a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NTree a
t NTree a -> NTrees a -> NTrees a
forall a. a -> [a] -> [a]
:))
Just NTree a
t' -> NTrees a -> Maybe (NTrees a)
forall a. a -> Maybe a
Just (NTrees a -> Maybe (NTrees a))
-> (Maybe (NTrees a) -> NTrees a)
-> Maybe (NTrees a)
-> Maybe (NTrees a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NTree a
t' NTree a -> NTrees a -> NTrees a
forall a. a -> [a] -> [a]
:) (NTrees a -> NTrees a)
-> (Maybe (NTrees a) -> NTrees a) -> Maybe (NTrees a) -> NTrees a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NTrees a -> Maybe (NTrees a) -> NTrees a
forall a. a -> Maybe a -> a
fromMaybe NTrees a
ts