{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.BTree (
BTree
, empty
, singleton
, fromList
, insert
, delete
, search
, height
, levels
, preorder
, inorder
, postorder
, draw
) where
import Control.Applicative ()
import Data.Foldable (find)
import Data.Function (on)
import qualified Data.List as L
import Data.Maybe ()
import Data.Monoid ()
import Data.Traversable ()
data BTree a where
BTree :: Tree n a -> BTree a
data Natural
= Z
| Succ Natural
data Node n a
= Subtree (Tree n a) a (Tree n a)
| Subtree' (Tree n a) a (Tree n a) a (Tree n a)
data Tree n a where
Branch :: Node n a -> Tree ('Succ n) a
Leaf :: Tree 'Z a
type Keep t n a = Tree n a -> t
type Push t n a = Tree n a -> a -> Tree n a -> t
insert :: forall a. Ord a => a -> BTree a -> BTree a
insert :: forall a. Ord a => a -> BTree a -> BTree a
insert a
x (BTree Tree n a
tree) = Tree n a -> Keep (BTree a) n a -> Push (BTree a) n a -> BTree a
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree n a
tree Keep (BTree a) n a
forall (n :: Natural) a. Tree n a -> BTree a
BTree (Push (BTree a) n a -> BTree a) -> Push (BTree a) n a -> BTree a
forall a b. (a -> b) -> a -> b
$ \Tree n a
a a
b Tree n a
c -> Tree ('Succ n) a -> BTree a
forall (n :: Natural) a. Tree n a -> BTree a
BTree (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b Tree n a
c)
where
insert' :: forall n t. Tree n a -> Keep t n a -> Push t n a -> t
insert' :: forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree n a
Leaf = \Keep t n a
_ Push t n a
push -> Push t n a
push Tree n a
forall a. Tree 'Z a
Leaf a
x Tree n a
forall a. Tree 'Z a
Leaf
insert' (Branch Node n a
n) = Node n a -> Keep t n a -> Push t n a -> t
forall (p :: Natural) (m :: Natural).
('Succ p ~ m) =>
Node p a -> Keep t m a -> Push t m a -> t
i Node n a
n
where
i :: forall p m. ('Succ p ~ m) => Node p a -> Keep t m a -> Push t m a -> t
i :: forall (p :: Natural) (m :: Natural).
('Succ p ~ m) =>
Node p a -> Keep t m a -> Push t m a -> t
i (Subtree' Tree p a
a a
b Tree p a
c a
d Tree p a
e) Keep t m a
keep Push t m a
push = a -> a -> a -> t -> t -> t -> t -> t -> t
forall a p. Ord a => a -> a -> a -> p -> p -> p -> p -> p -> p
select' a
x a
b a
d t
xltb t
xeqb t
xbtw t
xeqd t
xgtd
where
Keep t m a
_ = Keep t m a
keep :: Tree m a -> t
Push t m a
_ = Push t m a
push :: Tree m a -> a -> Tree m a -> t
xltb :: t
xltb = Tree p a -> Keep t p a -> Push t p a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree p a
a (\Tree p a
k -> Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
k a
b Tree p a
c a
d Tree p a
e)) (\Tree p a
p a
q Tree p a
r -> Push t m a
push (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
p a
q Tree p a
r) a
b (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
c a
d Tree p a
e))
xeqb :: t
xeqb = Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
x Tree p a
c a
d Tree p a
e)
xbtw :: t
xbtw = Tree p a -> Keep t p a -> Push t p a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree p a
c (\Tree p a
k -> Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
b Tree p a
k a
d Tree p a
e)) (\Tree p a
p a
q Tree p a
r -> Push t m a
push (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
a a
b Tree p a
p) a
q (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
r a
d Tree p a
e))
xeqd :: t
xeqd = Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
b Tree p a
c a
x Tree p a
e)
xgtd :: t
xgtd = Tree p a -> Keep t p a -> Push t p a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree p a
e (Keep t m a
keep Keep t m a -> (Tree p a -> Tree m a) -> Keep t p a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
b Tree p a
c a
d) (\Tree p a
p a
q Tree p a
r -> Push t m a
push (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
a a
b Tree p a
c) a
d (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
p a
q Tree p a
r))
i (Subtree Tree p a
a a
b Tree p a
c) Keep t m a
keep Push t m a
_ = a -> a -> t -> t -> t -> t
forall a p. Ord a => a -> a -> p -> p -> p -> p
select a
x a
b t
xltb t
xeqb t
xgtb
where
xltb :: t
xltb = Tree p a -> Keep t p a -> Push t p a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree p a
a (\Tree p a
k -> Keep t m a
keep (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
k a
b Tree p a
c)) (\Tree p a
p a
q Tree p a
r -> Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
p a
q Tree p a
r a
b Tree p a
c))
xgtb :: t
xgtb = Tree p a -> Keep t p a -> Push t p a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Push t n a -> t
insert' Tree p a
c (Keep t m a
keep Keep t m a -> (Tree p a -> Tree m a) -> Keep t p a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
a a
b) (\Tree p a
p a
q Tree p a
r -> Keep t m a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
b Tree p a
p a
q Tree p a
r))
xeqb :: t
xeqb = Keep t m a
keep (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
a a
x Tree p a
c)
type Pull t n a = Shrunk n a -> t
data Shrunk (n :: Natural) a where
H :: Tree n a -> Shrunk ('Succ n) a
delete :: forall a. Ord a => a -> BTree a -> BTree a
delete :: forall a. Ord a => a -> BTree a -> BTree a
delete a
x (BTree Tree n a
tree) = Tree n a -> Keep (BTree a) n a -> Pull (BTree a) n a -> BTree a
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
tree Keep (BTree a) n a
forall (n :: Natural) a. Tree n a -> BTree a
BTree Pull (BTree a) n a
forall (n :: Natural). Shrunk n a -> BTree a
shrink
where
shrink :: forall n. Shrunk n a -> BTree a
shrink :: forall (n :: Natural). Shrunk n a -> BTree a
shrink (H Tree n a
t) = Tree n a -> BTree a
forall (n :: Natural) a. Tree n a -> BTree a
BTree Tree n a
t
find' :: forall n t. Tree n a -> Keep t n a -> Pull t n a -> t
find' :: forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
Leaf Keep t n a
keep Pull t n a
_ = Keep t n a
keep Tree n a
forall a. Tree 'Z a
Leaf
find' (Branch (Subtree Tree n a
a a
b Tree n a
c)) Keep t n a
keep Pull t n a
pull = a -> a -> t -> t -> t -> t
forall a p. Ord a => a -> a -> p -> p -> p -> p
select a
x a
b t
xltb t
xeqb t
xgtb
where
xltb, xeqb, xgtb :: t
xltb :: t
xltb = Tree n a -> Keep t n a -> Pull t n a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
a (\Tree n a
k -> Keep t n a
keep (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
k a
b Tree n a
c)) (\Shrunk n a
p -> Shrunk n a -> a -> Keep t n a
forall (p :: Natural).
('Succ p ~ n) =>
Shrunk p a -> a -> Tree p a -> t
mrgl Shrunk n a
p a
b Tree n a
c)
xgtb :: t
xgtb = Tree n a -> Keep t n a -> Pull t n a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
c (Keep t n a
keep Keep t n a -> (Tree n a -> Tree n a) -> Keep t n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b) (Keep t ('Succ n) a
-> Pull t ('Succ n) a -> Tree n a -> a -> Pull t n a
forall (p :: Natural) t.
Keep t ('Succ p) a
-> Pull t ('Succ p) a -> Tree p a -> a -> Shrunk p a -> t
mrg2r Keep t n a
Keep t ('Succ n) a
keep Pull t n a
Pull t ('Succ n) a
pull Tree n a
a a
b)
xeqb :: t
xeqb = Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
a (\Tree n a
k a
r -> Keep t n a
keep (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
k a
r Tree n a
c)) (\Shrunk n a
p a
r -> Shrunk n a -> a -> Keep t n a
forall (p :: Natural).
('Succ p ~ n) =>
Shrunk p a -> a -> Tree p a -> t
mrgl Shrunk n a
p a
r Tree n a
c) (Pull t n a
pull (Tree n a -> Shrunk ('Succ n) a
forall (n :: Natural) a. Tree n a -> Shrunk ('Succ n) a
H Tree n a
a))
mrgl :: forall p. ('Succ p ~ n) => Shrunk p a -> a -> Tree p a -> t
mrgl :: forall (p :: Natural).
('Succ p ~ n) =>
Shrunk p a -> a -> Tree p a -> t
mrgl (H Tree n a
a') a
b' (Branch (Subtree Tree n a
c' a
d Tree n a
e)) = Pull t n a
pull (Tree ('Succ n) a -> Shrunk ('Succ ('Succ n)) a
forall (n :: Natural) a. Tree n a -> Shrunk ('Succ n) a
H (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a' a
b' Tree n a
Tree n a
c' a
d Tree n a
Tree n a
e))
mrgl (H Tree n a
a') a
b' (Branch (Subtree' Tree n a
c' a
d Tree n a
e a
f Tree n a
g)) = Keep t n a
keep (Tree ('Succ n) a
-> a -> Tree ('Succ n) a -> Tree ('Succ ('Succ n)) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a' a
b' Tree n a
Tree n a
c') a
d (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
Tree n a
e a
f Tree n a
Tree n a
g))
find' (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) Keep t n a
keep Pull t n a
_ = a -> a -> a -> t -> t -> t -> t -> t -> t
forall a p. Ord a => a -> a -> a -> p -> p -> p -> p -> p -> p
select' a
x a
b a
d t
xltb t
xeqb t
xbtw t
xeqd t
xgtd
where
xltb :: t
xltb = Tree n a -> Keep t n a -> Pull t n a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
a (\Tree n a
k -> Keep t n a
keep (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
k a
b Tree n a
c a
d Tree n a
e)) (\Shrunk n a
p -> Shrunk n a -> a -> Tree n a -> a -> Keep t n a
mrgl Shrunk n a
p a
b Tree n a
c a
d Tree n a
e)
xbtw :: t
xbtw = Tree n a -> Keep t n a -> Pull t n a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
c (\Tree n a
k -> Keep t n a
keep (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
k a
d Tree n a
e)) (\Shrunk n a
p -> Tree n a -> a -> Shrunk n a -> a -> Keep t n a
mrgm Tree n a
a a
b Shrunk n a
p a
d Tree n a
e)
xgtd :: t
xgtd = Tree n a -> Keep t n a -> Pull t n a -> t
forall (n :: Natural) t. Tree n a -> Keep t n a -> Pull t n a -> t
find' Tree n a
e (Keep t n a
keep Keep t n a -> (Tree n a -> Tree n a) -> Keep t n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
c a
d) (Keep t ('Succ n) a -> Tree n a -> a -> Tree n a -> a -> Pull t n a
forall (p :: Natural) t.
Keep t ('Succ p) a
-> Tree p a -> a -> Tree p a -> a -> Shrunk p a -> t
mrg3r Keep t n a
Keep t ('Succ n) a
keep Tree n a
a a
b Tree n a
c a
d)
xeqb :: t
xeqb = Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
a (\Tree n a
k a
r -> Keep t n a
keep (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
k a
r Tree n a
c a
d Tree n a
e)) (\Shrunk n a
p a
r -> Shrunk n a -> a -> Tree n a -> a -> Keep t n a
mrgl Shrunk n a
p a
r Tree n a
c a
d Tree n a
e) (Keep t n a
keep (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
c a
d Tree n a
e))
xeqd :: t
xeqd = Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
c (\Tree n a
k a
r -> Keep t n a
keep (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
k a
r Tree n a
e)) (\Shrunk n a
p a
r -> Tree n a -> a -> Shrunk n a -> a -> Keep t n a
mrgm Tree n a
a a
b Shrunk n a
p a
r Tree n a
e) (Keep t n a
keep (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b Tree n a
c))
mrgl :: Shrunk n a -> a -> Tree n a -> a -> Keep t n a
mrgl (H Tree n a
a') a
b' (Branch (Subtree' Tree n a
c' a
d' Tree n a
e' a
f Tree n a
g)) a
h Tree n a
i = Keep t n a
keep (Tree ('Succ n) a
-> a
-> Tree ('Succ n) a
-> a
-> Tree ('Succ n) a
-> Tree ('Succ ('Succ n)) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a' a
b' Tree n a
Tree n a
c') a
d' (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
Tree n a
e' a
f Tree n a
Tree n a
g) a
h Tree n a
Tree ('Succ n) a
i)
mrgl (H Tree n a
a') a
b' (Branch (Subtree Tree n a
c' a
d' Tree n a
e')) a
f Tree n a
g = Keep t n a
keep (Tree ('Succ n) a
-> a -> Tree ('Succ n) a -> Tree ('Succ ('Succ n)) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a' a
b' Tree n a
Tree n a
c' a
d' Tree n a
Tree n a
e') a
f Tree n a
Tree ('Succ n) a
g)
mrgm :: Tree n a -> a -> Shrunk n a -> a -> Keep t n a
mrgm Tree n a
a' a
b' (H Tree n a
c') a
d' (Branch (Subtree' Tree n a
e' a
f Tree n a
g a
h Tree n a
i)) = Keep t n a
keep (Tree ('Succ n) a
-> a
-> Tree ('Succ n) a
-> a
-> Tree ('Succ n) a
-> Tree ('Succ ('Succ n)) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
Tree ('Succ n) a
a' a
b' (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
c' a
d' Tree n a
Tree n a
e') a
f (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
Tree n a
g a
h Tree n a
Tree n a
i))
mrgm Tree n a
a' a
b' (H Tree n a
c') a
d' (Branch (Subtree Tree n a
e' a
f Tree n a
g)) = Keep t n a
keep (Tree ('Succ n) a
-> a -> Tree ('Succ n) a -> Tree ('Succ ('Succ n)) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
Tree ('Succ n) a
a' a
b' (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
c' a
d' Tree n a
Tree n a
e' a
f Tree n a
Tree n a
g))
replace :: forall n t. Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace :: forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
Leaf Keep (a -> t) n a
_ Pull (a -> t) n a
_ t
leaf = t
leaf
replace (Branch (Subtree Tree n a
a a
b Tree n a
c)) Keep (a -> t) n a
keep Pull (a -> t) n a
pull t
_
= Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
c (Keep (a -> t) n a
keep Keep (a -> t) n a -> (Tree n a -> Tree n a) -> Keep (a -> t) n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b) (Keep (a -> t) ('Succ n) a
-> Pull (a -> t) ('Succ n) a -> Tree n a -> a -> Pull (a -> t) n a
forall (p :: Natural) t.
Keep t ('Succ p) a
-> Pull t ('Succ p) a -> Tree p a -> a -> Shrunk p a -> t
mrg2r Keep (a -> t) n a
Keep (a -> t) ('Succ n) a
keep Pull (a -> t) n a
Pull (a -> t) ('Succ n) a
pull Tree n a
a a
b) (Pull (a -> t) n a
pull (Tree n a -> Shrunk ('Succ n) a
forall (n :: Natural) a. Tree n a -> Shrunk ('Succ n) a
H Tree n a
a) a
b)
replace (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) Keep (a -> t) n a
keep Pull (a -> t) n a
_ t
_ =
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
forall (n :: Natural) t.
Tree n a -> Keep (a -> t) n a -> Pull (a -> t) n a -> t -> t
replace Tree n a
e (Keep (a -> t) n a
keep Keep (a -> t) n a -> (Tree n a -> Tree n a) -> Keep (a -> t) n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
c a
d) (Keep (a -> t) ('Succ n) a
-> Tree n a -> a -> Tree n a -> a -> Pull (a -> t) n a
forall (p :: Natural) t.
Keep t ('Succ p) a
-> Tree p a -> a -> Tree p a -> a -> Shrunk p a -> t
mrg3r Keep (a -> t) n a
Keep (a -> t) ('Succ n) a
keep Tree n a
a a
b Tree n a
c a
d) (Keep (a -> t) n a
keep (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b Tree n a
c) a
d)
mrg2r :: forall p t. Keep t ('Succ p) a -> Pull t ('Succ p) a -> Tree p a -> a -> Shrunk p a -> t
mrg2r :: forall (p :: Natural) t.
Keep t ('Succ p) a
-> Pull t ('Succ p) a -> Tree p a -> a -> Shrunk p a -> t
mrg2r Keep t ('Succ p) a
_ Pull t ('Succ p) a
pull (Branch (Subtree Tree n a
a a
b Tree n a
c)) a
d (H Tree n a
e) = Pull t ('Succ p) a
pull (Tree p a -> Shrunk ('Succ p) a
forall (n :: Natural) a. Tree n a -> Shrunk ('Succ n) a
H (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
c a
d Tree n a
Tree n a
e))
mrg2r Keep t ('Succ p) a
keep Pull t ('Succ p) a
_ (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) a
f (H Tree n a
g) = Keep t ('Succ p) a
keep (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b Tree n a
c) a
d (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
e a
f Tree n a
Tree n a
g))
mrg3r :: forall p t. Keep t ('Succ p) a -> Tree p a -> a -> Tree p a -> a -> Shrunk p a -> t
mrg3r :: forall (p :: Natural) t.
Keep t ('Succ p) a
-> Tree p a -> a -> Tree p a -> a -> Shrunk p a -> t
mrg3r Keep t ('Succ p) a
keep Tree p a
a a
b (Branch (Subtree' Tree n a
c a
d Tree n a
e a
f Tree n a
g)) a
h (H Tree n a
i) = Keep t ('Succ p) a
keep (Tree p a -> a -> Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree p a
a a
b (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
c a
d Tree n a
e) a
f (Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
g a
h Tree n a
Tree n a
i))
mrg3r Keep t ('Succ p) a
keep Tree p a
a a
b (Branch (Subtree Tree n a
c a
d Tree n a
e)) a
f (H Tree n a
g) = Keep t ('Succ p) a
keep (Tree p a -> a -> Tree p a -> Tree ('Succ p) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree p a
a a
b (Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
c a
d Tree n a
e a
f Tree n a
Tree n a
g))
search :: forall a. Ord a => a -> BTree a -> Maybe a
search :: forall a. Ord a => a -> BTree a -> Maybe a
search a
x = (a -> Bool) -> BTree a -> Maybe a
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x)
empty :: BTree a
empty :: forall a. BTree a
empty = Tree 'Z a -> BTree a
forall (n :: Natural) a. Tree n a -> BTree a
BTree Tree 'Z a
forall a. Tree 'Z a
Leaf
singleton :: Ord a => a -> BTree a
singleton :: forall a. Ord a => a -> BTree a
singleton a
x = a -> BTree a -> BTree a
forall a. Ord a => a -> BTree a -> BTree a
insert a
x BTree a
forall a. BTree a
empty
fromList :: Ord a => [a] -> BTree a
fromList :: forall a. Ord a => [a] -> BTree a
fromList = (BTree a -> a -> BTree a) -> BTree a -> [a] -> BTree a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' ((a -> BTree a -> BTree a) -> BTree a -> a -> BTree a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> BTree a -> BTree a
forall a. Ord a => a -> BTree a -> BTree a
insert) BTree a
forall a. BTree a
empty
inorder :: forall a. BTree a -> [a]
inorder :: forall a. BTree a -> [a]
inorder (BTree Tree n a
tree) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
tree
where
pre :: Tree n a -> [a]
pre :: forall (n :: Natural). Tree n a -> [a]
pre (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
b] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
c
pre (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
b] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
c [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
d] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pre Tree n a
e
pre Tree n a
Leaf = []
postorder :: forall a. BTree a -> [a]
postorder :: forall a. BTree a -> [a]
postorder (BTree Tree n a
tree) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
tree
where
pos :: Tree n a -> [a]
pos :: forall (n :: Natural). Tree n a -> [a]
pos (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
c [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
b]
pos (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
c [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
pos Tree n a
e [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
b] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
d]
pos Tree n a
Leaf = []
preorder :: forall a. BTree a -> [a]
preorder :: forall a. BTree a -> [a]
preorder (BTree Tree n a
tree) = Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
tree
where
ino :: Tree n a -> [a]
ino :: forall (n :: Natural). Tree n a -> [a]
ino (Branch (Subtree Tree n a
a a
b Tree n a
c)) = [a
b] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
c
ino (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = [a
b] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
d] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
c [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Tree n a -> [a]
forall (n :: Natural). Tree n a -> [a]
ino Tree n a
e
ino Tree n a
Leaf = []
height :: forall a. BTree a -> Int
height :: forall a. BTree a -> Int
height (BTree Tree n a
tree) = Tree n a -> Int
forall (n :: Natural). Tree n a -> Int
height' Tree n a
tree
where
height' :: Tree n a -> Int
height' :: forall (n :: Natural). Tree n a -> Int
height' (Branch (Subtree Tree n a
a a
_ Tree n a
_)) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Tree n a -> Int
forall (n :: Natural). Tree n a -> Int
height' Tree n a
a
height' (Branch (Subtree' Tree n a
a a
_ Tree n a
_ a
_ Tree n a
_)) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Tree n a -> Int
forall (n :: Natural). Tree n a -> Int
height' Tree n a
a
height' Tree n a
Leaf = Int
0
draw :: forall a. Show a => BTree a -> String
draw :: forall a. Show a => BTree a -> String
draw (BTree Tree n a
tree) = Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
tree
where
draw' :: Tree n a -> String
draw' :: forall (n :: Natural). Tree n a -> String
draw' (Branch (Subtree Tree n a
a a
b Tree n a
c)) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
b String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
draw' (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
b String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
d String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Tree n a -> String
forall (n :: Natural). Tree n a -> String
draw' Tree n a
e String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
draw' Tree n a
Leaf = String
"."
levels :: forall a. Ord a => BTree a -> [[a]]
levels :: forall a. Ord a => BTree a -> [[a]]
levels (BTree Tree n a
tree) = ([(Int, a)] -> [a]) -> [[(Int, a)]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map (((Int, a) -> a) -> [(Int, a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Int, a) -> a
forall a b. (a, b) -> b
snd) ([[(Int, a)]] -> [[a]]) -> [[(Int, a)]] -> [[a]]
forall a b. (a -> b) -> a -> b
$ ((Int, a) -> (Int, a) -> Bool) -> [(Int, a)] -> [[(Int, a)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
L.groupBy (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Int -> Int -> Bool)
-> ((Int, a) -> Int) -> (Int, a) -> (Int, a) -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Int, a) -> Int
forall a b. (a, b) -> a
fst) ([(Int, a)] -> [[(Int, a)]]) -> [(Int, a)] -> [[(Int, a)]]
forall a b. (a -> b) -> a -> b
$ [(Int, a)] -> [(Int, a)]
forall a. Ord a => [a] -> [a]
L.sort ([(Int, a)] -> [(Int, a)]) -> [(Int, a)] -> [(Int, a)]
forall a b. (a -> b) -> a -> b
$ Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' Int
0 Tree n a
tree
where
levels' :: Int -> Tree n a -> [(Int, a)]
levels' :: forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' Int
_ Tree n a
Leaf = []
levels' Int
n (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Tree n a
a [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ [(Int
n, a
b)] [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Tree n a
c
levels' Int
n (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Tree n a
a [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ [(Int
n, a
b)] [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Tree n a
c [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ [(Int
n, a
d)] [(Int, a)] -> [(Int, a)] -> [(Int, a)]
forall a. [a] -> [a] -> [a]
++ Int -> Tree n a -> [(Int, a)]
forall (n :: Natural). Int -> Tree n a -> [(Int, a)]
levels' (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Tree n a
e
fmapT :: (a -> b) -> BTree a -> BTree b
fmapT :: forall a b. (a -> b) -> BTree a -> BTree b
fmapT a -> b
f (BTree Tree n a
tree) = Tree n b -> BTree b
forall (n :: Natural) a. Tree n a -> BTree a
BTree (Tree n b -> BTree b) -> Tree n b -> BTree b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f Tree n a
tree
where
fmap' :: (a -> b) -> Tree n a -> Tree n b
fmap' :: forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Node n b -> Tree ('Succ n) b
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Tree n b -> b -> Tree n b -> Node n b
forall (n :: Natural) a. Tree n a -> a -> Tree n a -> Node n a
Subtree ((a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' Tree n a
a) (a -> b
f' a
b) ((a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' Tree n a
c))
fmap' a -> b
f' (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Node n b -> Tree ('Succ n) b
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Tree n b -> b -> Tree n b -> b -> Tree n b -> Node n b
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Node n a
Subtree' ((a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' Tree n a
a) (a -> b
f' a
b) ((a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' Tree n a
c) (a -> b
f' a
d) ((a -> b) -> Tree n a -> Tree n b
forall a b (n :: Natural). (a -> b) -> Tree n a -> Tree n b
fmap' a -> b
f' Tree n a
e))
fmap' a -> b
_ Tree n a
Leaf = Tree n b
forall a. Tree 'Z a
Leaf
foldMapT :: forall m a. Monoid m => (a -> m) -> BTree a -> m
foldMapT :: forall m a. Monoid m => (a -> m) -> BTree a -> m
foldMapT a -> m
f (BTree Tree n a
t) = Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
t
where
fm :: forall n. Tree n a -> m
fm :: forall (n :: Natural). Tree n a -> m
fm (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
b m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
c
fm (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
b m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
c m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
d m -> m -> m
forall a. Semigroup a => a -> a -> a
<> Tree n a -> m
forall (n :: Natural). Tree n a -> m
fm Tree n a
e
fm Tree n a
Leaf = m
forall a. Monoid a => a
mempty
instance Functor BTree where fmap :: forall a b. (a -> b) -> BTree a -> BTree b
fmap = (a -> b) -> BTree a -> BTree b
forall a b. (a -> b) -> BTree a -> BTree b
fmapT
instance Foldable BTree where foldMap :: forall m a. Monoid m => (a -> m) -> BTree a -> m
foldMap = (a -> m) -> BTree a -> m
forall m a. Monoid m => (a -> m) -> BTree a -> m
foldMapT
instance Traversable BTree where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> BTree a -> f (BTree b)
traverse a -> f b
f (BTree Tree n a
tree) = Tree n b -> BTree b
forall (n :: Natural) a. Tree n a -> BTree a
BTree (Tree n b -> BTree b) -> f (Tree n b) -> f (BTree b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f Tree n a
tree
where
traverse' :: forall n a b f. Applicative f => (a -> f b) -> Tree n a -> f (Tree n b)
traverse' :: forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' (Branch (Subtree Tree n a
a a
b Tree n a
c)) = Node n b -> Tree n b
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Node n b -> Tree n b) -> f (Node n b) -> f (Tree n b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Tree n b -> b -> Tree n b -> Node n b
forall (n :: Natural) a. Tree n a -> a -> Tree n a -> Node n a
Subtree (Tree n b -> b -> Tree n b -> Node n b)
-> f (Tree n b) -> f (b -> Tree n b -> Node n b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' Tree n a
a f (b -> Tree n b -> Node n b) -> f b -> f (Tree n b -> Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f' a
b f (Tree n b -> Node n b) -> f (Tree n b) -> f (Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' Tree n a
c)
traverse' a -> f b
f' (Branch (Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)) = Node n b -> Tree n b
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Node n b -> Tree n b) -> f (Node n b) -> f (Tree n b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Tree n b -> b -> Tree n b -> b -> Tree n b -> Node n b
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Node n a
Subtree' (Tree n b -> b -> Tree n b -> b -> Tree n b -> Node n b)
-> f (Tree n b) -> f (b -> Tree n b -> b -> Tree n b -> Node n b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' Tree n a
a f (b -> Tree n b -> b -> Tree n b -> Node n b)
-> f b -> f (Tree n b -> b -> Tree n b -> Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f' a
b f (Tree n b -> b -> Tree n b -> Node n b)
-> f (Tree n b) -> f (b -> Tree n b -> Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' Tree n a
c f (b -> Tree n b -> Node n b) -> f b -> f (Tree n b -> Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f' a
d f (Tree n b -> Node n b) -> f (Tree n b) -> f (Node n b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (a -> f b) -> Tree n a -> f (Tree n b)
forall (n :: Natural) a b (f :: * -> *).
Applicative f =>
(a -> f b) -> Tree n a -> f (Tree n b)
traverse' a -> f b
f' Tree n a
e)
traverse' a -> f b
_ Tree n a
Leaf = Tree n b -> f (Tree n b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Tree n b
forall a. Tree 'Z a
Leaf
instance Show a => Show (BTree a) where
showsPrec :: Int -> BTree a -> String -> String
showsPrec Int
n BTree a
t = Bool -> (String -> String) -> String -> String
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) ((String -> String) -> String -> String)
-> (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String -> String
forall a. Show a => a -> String -> String
shows (BTree a -> String
forall a. Show a => BTree a -> String
draw BTree a
t)
select :: Ord a => a -> a -> p -> p -> p -> p
select :: forall a p. Ord a => a -> a -> p -> p -> p -> p
select a
x a
y p
lt p
eq p
gt = case a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y of { Ordering
LT -> p
lt; Ordering
EQ -> p
eq; Ordering
GT -> p
gt }
select' :: Ord a => a -> a -> a -> p -> p -> p -> p -> p -> p
select' :: forall a p. Ord a => a -> a -> a -> p -> p -> p -> p -> p -> p
select' a
x a
y a
z p
xlty p
xeqy p
xbtw p
xeqz p
xgtz = a -> a -> p -> p -> p -> p
forall a p. Ord a => a -> a -> p -> p -> p -> p
select a
x a
y p
xlty p
xeqy (a -> a -> p -> p -> p -> p
forall a p. Ord a => a -> a -> p -> p -> p -> p
select a
x a
z p
xbtw p
xeqz p
xgtz)
branch :: Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch :: forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch Tree n a
a a
b Tree n a
c = Node n a -> Tree ('Succ n) a
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Tree n a -> a -> Tree n a -> Node n a
forall (n :: Natural) a. Tree n a -> a -> Tree n a -> Node n a
Subtree Tree n a
a a
b Tree n a
c)
branch' :: Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' :: forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Tree ('Succ n) a
branch' Tree n a
a a
b Tree n a
c a
d Tree n a
e = Node n a -> Tree ('Succ n) a
forall (n :: Natural) a. Node n a -> Tree ('Succ n) a
Branch (Tree n a -> a -> Tree n a -> a -> Tree n a -> Node n a
forall (n :: Natural) a.
Tree n a -> a -> Tree n a -> a -> Tree n a -> Node n a
Subtree' Tree n a
a a
b Tree n a
c a
d Tree n a
e)