{-# LANGUAGE BangPatterns #-}
module ELynx.Tree.Simulate.PointProcess
( PointProcess (..),
TimeSpec (..),
simulate,
toReconstructedTree,
simulateReconstructedTree,
simulateNReconstructedTrees,
)
where
import Control.Monad
import Data.Function
import Data.List
import Data.Sequence (Seq)
import qualified Data.Sequence as S
import ELynx.Tree.Distribution.BirthDeath
import ELynx.Tree.Distribution.BirthDeathCritical
import ELynx.Tree.Distribution.BirthDeathCriticalNoTime
import ELynx.Tree.Distribution.BirthDeathNearlyCritical
import ELynx.Tree.Distribution.TimeOfOrigin
import ELynx.Tree.Distribution.TimeOfOriginNearCritical
import ELynx.Tree.Distribution.Types
import ELynx.Tree.Length
import ELynx.Tree.Rooted
import qualified Statistics.Distribution as D
import System.Random.Stateful
epsNearCriticalPointProcess :: Double
epsNearCriticalPointProcess :: Double
epsNearCriticalPointProcess = Double
1e-5
epsNearCriticalTimeOfOrigin :: Double
epsNearCriticalTimeOfOrigin :: Double
epsNearCriticalTimeOfOrigin = Double
1e-8
eps :: Double
eps :: Double
eps = Double
1e-12
(=~=) :: Double -> Double -> Bool
Double
x =~= :: Double -> Double -> Bool
=~= Double
y = Double
eps forall a. Ord a => a -> a -> Bool
> forall a. Num a => a -> a
abs (Double
x forall a. Num a => a -> a -> a
- Double
y)
sortListWithIndices :: Ord a => [a] -> [(a, Int)]
sortListWithIndices :: forall a. Ord a => [a] -> [(a, Int)]
sortListWithIndices [a]
xs = forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs ([Int
0 ..] :: [Int])
randomInsertList :: StatefulGen g m => a -> [a] -> g -> m [a]
randomInsertList :: forall g (m :: * -> *) a. StatefulGen g m => a -> [a] -> g -> m [a]
randomInsertList a
e [a]
v g
g = do
let l :: Int
l = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
v
Int
i <- forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (Int
0, Int
l) g
g
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
i [a]
v forall a. [a] -> [a] -> [a]
++ [a
e] forall a. [a] -> [a] -> [a]
++ forall a. Int -> [a] -> [a]
drop Int
i [a]
v
data PointProcess a b = PointProcess
{ forall a b. PointProcess a b -> [a]
points :: ![a],
forall a b. PointProcess a b -> [b]
values :: ![b],
forall a b. PointProcess a b -> b
origin :: !b
}
deriving (ReadPrec [PointProcess a b]
ReadPrec (PointProcess a b)
ReadS [PointProcess a b]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall a b. (Read a, Read b) => ReadPrec [PointProcess a b]
forall a b. (Read a, Read b) => ReadPrec (PointProcess a b)
forall a b. (Read a, Read b) => Int -> ReadS (PointProcess a b)
forall a b. (Read a, Read b) => ReadS [PointProcess a b]
readListPrec :: ReadPrec [PointProcess a b]
$creadListPrec :: forall a b. (Read a, Read b) => ReadPrec [PointProcess a b]
readPrec :: ReadPrec (PointProcess a b)
$creadPrec :: forall a b. (Read a, Read b) => ReadPrec (PointProcess a b)
readList :: ReadS [PointProcess a b]
$creadList :: forall a b. (Read a, Read b) => ReadS [PointProcess a b]
readsPrec :: Int -> ReadS (PointProcess a b)
$creadsPrec :: forall a b. (Read a, Read b) => Int -> ReadS (PointProcess a b)
Read, Int -> PointProcess a b -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> PointProcess a b -> ShowS
forall a b. (Show a, Show b) => [PointProcess a b] -> ShowS
forall a b. (Show a, Show b) => PointProcess a b -> String
showList :: [PointProcess a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [PointProcess a b] -> ShowS
show :: PointProcess a b -> String
$cshow :: forall a b. (Show a, Show b) => PointProcess a b -> String
showsPrec :: Int -> PointProcess a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> PointProcess a b -> ShowS
Show, PointProcess a b -> PointProcess a b -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b.
(Eq a, Eq b) =>
PointProcess a b -> PointProcess a b -> Bool
/= :: PointProcess a b -> PointProcess a b -> Bool
$c/= :: forall a b.
(Eq a, Eq b) =>
PointProcess a b -> PointProcess a b -> Bool
== :: PointProcess a b -> PointProcess a b -> Bool
$c== :: forall a b.
(Eq a, Eq b) =>
PointProcess a b -> PointProcess a b -> Bool
Eq)
data TimeSpec
=
Random
|
Origin Time
|
Mrca Time
simulate ::
StatefulGen g m =>
Int ->
TimeSpec ->
Rate ->
Rate ->
g ->
m (PointProcess Int Double)
simulate :: forall g (m :: * -> *).
StatefulGen g m =>
Int
-> TimeSpec -> Double -> Double -> g -> m (PointProcess Int Double)
simulate Int
n TimeSpec
ts Double
l Double
m g
g
| Int
n forall a. Ord a => a -> a -> Bool
< Int
1 = forall a. HasCallStack => String -> a
error String
"Number of samples needs to be one or larger."
| Double
l forall a. Ord a => a -> a -> Bool
< Double
0.0 = forall a. HasCallStack => String -> a
error String
"Birth rate needs to be positive."
| Bool
otherwise = case TimeSpec
ts of
TimeSpec
Random -> forall g (m :: * -> *).
StatefulGen g m =>
Int -> Double -> Double -> g -> m (PointProcess Int Double)
simulateRandom Int
n Double
l Double
m g
g
Origin Double
t -> forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateOrigin Int
n Double
t Double
l Double
m g
g
Mrca Double
t -> forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateMrca Int
n Double
t Double
l Double
m g
g
simulateRandom ::
StatefulGen g m =>
Int ->
Double ->
Double ->
g ->
m (PointProcess Int Double)
simulateRandom :: forall g (m :: * -> *).
StatefulGen g m =>
Int -> Double -> Double -> g -> m (PointProcess Int Double)
simulateRandom Int
n Double
l Double
m g
g
|
Double
m forall a. Ord a => a -> a -> Bool
> Double
l =
forall a. HasCallStack => String -> a
error
String
"simulateRandom: Please specify height if mu > lambda."
|
Double
m Double -> Double -> Bool
=~= Double
l =
do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
1) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> BirthDeathCriticalNoTimeDistribution
BDCNTD Double
l) g
g)
let t :: Double
t = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Double]
vs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs Double
t
|
forall a. Num a => a -> a
abs (Double
m forall a. Num a => a -> a -> a
- Double
l) forall a. Ord a => a -> a -> Bool
<= Double
epsNearCriticalTimeOfOrigin =
do
Double
t <- forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Int -> Double -> Double -> TimeOfOriginNearCriticalDistribution
TONCD Int
n Double
l Double
m) g
g
forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateOrigin Int
n Double
t Double
l Double
m g
g
|
Bool
otherwise =
do
Double
t <- forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Int -> Double -> Double -> TimeOfOriginDistribution
TOD Int
n Double
l Double
m) g
g
forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateOrigin Int
n Double
t Double
l Double
m g
g
simulateOrigin ::
StatefulGen g m =>
Int ->
Time ->
Double ->
Double ->
g ->
m (PointProcess Int Double)
simulateOrigin :: forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateOrigin Int
n Double
t Double
l Double
m g
g
| Double
t forall a. Ord a => a -> a -> Bool
< Double
0.0 = forall a. HasCallStack => String -> a
error String
"simulateOrigin: Time of origin needs to be positive."
|
Double
m Double -> Double -> Bool
=~= Double
l = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
1) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> BirthDeathCriticalDistribution
BDCD Double
t Double
l) g
g)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs Double
t
| forall a. Num a => a -> a
abs (Double
m forall a. Num a => a -> a -> a
- Double
l) forall a. Ord a => a -> a -> Bool
<= Double
epsNearCriticalPointProcess = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
1) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> Double -> BirthDeathNearlyCriticalDistribution
BDNCD Double
t Double
l Double
m) g
g)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs Double
t
| Bool
otherwise = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
1) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> Double -> BirthDeathDistribution
BDD Double
t Double
l Double
m) g
g)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs Double
t
simulateMrca ::
StatefulGen g m =>
Int ->
Time ->
Double ->
Double ->
g ->
m (PointProcess Int Double)
simulateMrca :: forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Double -> Double -> Double -> g -> m (PointProcess Int Double)
simulateMrca Int
n Double
t Double
l Double
m g
g
| Double
t forall a. Ord a => a -> a -> Bool
< Double
0.0 = forall a. HasCallStack => String -> a
error String
"simulateMrca: Time of MRCA needs to be positive."
| Double
m Double -> Double -> Bool
=~= Double
l = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
2) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> BirthDeathCriticalDistribution
BDCD Double
t Double
l) g
g)
[Double]
vs' <- forall g (m :: * -> *) a. StatefulGen g m => a -> [a] -> g -> m [a]
randomInsertList Double
t [Double]
vs g
g
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs' Double
t
| forall a. Num a => a -> a
abs (Double
m forall a. Num a => a -> a -> a
- Double
l) forall a. Ord a => a -> a -> Bool
<= Double
epsNearCriticalPointProcess = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
2) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> Double -> BirthDeathNearlyCriticalDistribution
BDNCD Double
t Double
l Double
m) g
g)
[Double]
vs' <- forall g (m :: * -> *) a. StatefulGen g m => a -> [a] -> g -> m [a]
randomInsertList Double
t [Double]
vs g
g
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs' Double
t
| Bool
otherwise = do
![Double]
vs <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Int
n forall a. Num a => a -> a -> a
- Int
2) (forall d g (m :: * -> *).
(ContGen d, StatefulGen g m) =>
d -> g -> m Double
D.genContVar (Double -> Double -> Double -> BirthDeathDistribution
BDD Double
t Double
l Double
m) g
g)
[Double]
vs' <- forall g (m :: * -> *) a. StatefulGen g m => a -> [a] -> g -> m [a]
randomInsertList Double
t [Double]
vs g
g
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int
0 .. (Int
n forall a. Num a => a -> a -> a
- Int
1)] [Double]
vs' Double
t
sortPP :: (Ord b) => PointProcess a b -> ([b], [Int])
sortPP :: forall b a. Ord b => PointProcess a b -> ([b], [Int])
sortPP (PointProcess [a]
_ [b]
vs b
_) = ([b]
vsSorted, [Int]
isSorted)
where
vsIsSorted :: [(b, Int)]
vsIsSorted = forall a. Ord a => [a] -> [(a, Int)]
sortListWithIndices [b]
vs
vsSorted :: [b]
vsSorted = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(b, Int)]
vsIsSorted
isSorted :: [Int]
isSorted = [Int] -> [Int]
flattenIndices forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(b, Int)]
vsIsSorted
flattenIndices :: [Int] -> [Int]
flattenIndices :: [Int] -> [Int]
flattenIndices [Int]
is = forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL [Int] -> Int -> ([Int], Int)
fAcc [] [Int]
is
fAcc :: [Int] -> Int -> ([Int], Int)
fAcc :: [Int] -> Int -> ([Int], Int)
fAcc [Int]
is Int
i = (Int
i forall a. a -> [a] -> [a]
: [Int]
is, Int
i') where i' :: Int
i' = Int
i forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Ord a => a -> a -> Bool
< Int
i) [Int]
is)
simulateNReconstructedTrees ::
(StatefulGen g m) =>
Int ->
Int ->
TimeSpec ->
Rate ->
Rate ->
g ->
m (Forest Length Int)
simulateNReconstructedTrees :: forall g (m :: * -> *).
StatefulGen g m =>
Int
-> Int
-> TimeSpec
-> Double
-> Double
-> g
-> m (Forest Length Int)
simulateNReconstructedTrees Int
nT Int
nP TimeSpec
t Double
l Double
m g
g
| Int
nT forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise = forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
nT forall a b. (a -> b) -> a -> b
$ forall g (m :: * -> *).
StatefulGen g m =>
Int -> TimeSpec -> Double -> Double -> g -> m (Tree Length Int)
simulateReconstructedTree Int
nP TimeSpec
t Double
l Double
m g
g
simulateReconstructedTree ::
StatefulGen g m =>
Int ->
TimeSpec ->
Rate ->
Rate ->
g ->
m (Tree Length Int)
simulateReconstructedTree :: forall g (m :: * -> *).
StatefulGen g m =>
Int -> TimeSpec -> Double -> Double -> g -> m (Tree Length Int)
simulateReconstructedTree Int
n TimeSpec
t Double
l Double
m g
g = do
PointProcess [Int]
ns [Double]
vs Double
o <- forall g (m :: * -> *).
StatefulGen g m =>
Int
-> TimeSpec -> Double -> Double -> g -> m (PointProcess Int Double)
simulate Int
n TimeSpec
t Double
l Double
m g
g
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> PointProcess a Length -> Tree Length a
toReconstructedTree Int
0 forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> b -> PointProcess a b
PointProcess [Int]
ns (forall a b. (a -> b) -> [a] -> [b]
map Double -> Length
toLengthUnsafe [Double]
vs) (Double -> Length
toLengthUnsafe Double
o)
toReconstructedTree ::
a ->
PointProcess a Length ->
Tree Length a
toReconstructedTree :: forall a. a -> PointProcess a Length -> Tree Length a
toReconstructedTree a
l pp :: PointProcess a Length
pp@(PointProcess [a]
ps [Length]
vs Length
o)
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ps forall a. Eq a => a -> a -> Bool
/= forall (t :: * -> *) a. Foldable t => t a -> Int
length [Length]
vs forall a. Num a => a -> a -> a
+ Int
1 = forall a. HasCallStack => String -> a
error String
"Too few or too many points."
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [Length]
vs forall a. Ord a => a -> a -> Bool
<= Int
1 = forall a. HasCallStack => String -> a
error String
"Too few values."
|
Bool
otherwise =
Tree Length a
treeOrigin
where
([Length]
vsSorted, [Int]
isSorted) = forall b a. Ord b => PointProcess a b -> ([b], [Int])
sortPP PointProcess a Length
pp
!lvs :: Seq (Tree Length a)
lvs = forall a. [a] -> Seq a
S.fromList [forall e a. e -> a -> Forest e a -> Tree e a
Node Length
0 a
p [] | a
p <- [a]
ps]
!heights :: Seq Length
heights = forall a. Int -> a -> Seq a
S.replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ps) Length
0
!treeRoot :: Tree Length a
treeRoot = forall a.
[Int]
-> [Length]
-> a
-> Seq (Tree Length a)
-> Seq Length
-> Tree Length a
toReconstructedTree' [Int]
isSorted [Length]
vsSorted a
l Seq (Tree Length a)
lvs Seq Length
heights
!h :: Length
h = forall a. [a] -> a
last [Length]
vsSorted
!treeOrigin :: Tree Length a
treeOrigin = forall e a. (e -> e) -> Tree e a -> Tree e a
modifyStem (forall a. Num a => a -> a -> a
+ (Length
o forall a. Num a => a -> a -> a
- Length
h)) Tree Length a
treeRoot
toReconstructedTree' ::
[Int] ->
[Length] ->
a ->
Seq (Tree Length a) ->
Seq Length ->
Tree Length a
toReconstructedTree' :: forall a.
[Int]
-> [Length]
-> a
-> Seq (Tree Length a)
-> Seq Length
-> Tree Length a
toReconstructedTree' [] [] a
_ Seq (Tree Length a)
trs Seq Length
_ = Seq (Tree Length a)
trs forall a. Seq a -> Int -> a
`S.index` Int
0
toReconstructedTree' [Int]
is [Length]
vs a
l Seq (Tree Length a)
trs Seq Length
hs = forall a.
[Int]
-> [Length]
-> a
-> Seq (Tree Length a)
-> Seq Length
-> Tree Length a
toReconstructedTree' [Int]
is' [Length]
vs' a
l Seq (Tree Length a)
trs'' Seq Length
hs'
where
!i :: Int
i = forall a. [a] -> a
head [Int]
is
!is' :: [Int]
is' = forall a. [a] -> [a]
tail [Int]
is
!v :: Length
v = forall a. [a] -> a
head [Length]
vs
!vs' :: [Length]
vs' = forall a. [a] -> [a]
tail [Length]
vs
!hl :: Length
hl = Seq Length
hs forall a. Seq a -> Int -> a
`S.index` Int
i
!hr :: Length
hr = Seq Length
hs forall a. Seq a -> Int -> a
`S.index` (Int
i forall a. Num a => a -> a -> a
+ Int
1)
!dvl :: Length
dvl = Length
v forall a. Num a => a -> a -> a
- Length
hl
!dvr :: Length
dvr = Length
v forall a. Num a => a -> a -> a
- Length
hr
!tl :: Tree Length a
tl = forall e a. (e -> e) -> Tree e a -> Tree e a
modifyStem (forall a. Num a => a -> a -> a
+ Length
dvl) forall a b. (a -> b) -> a -> b
$ Seq (Tree Length a)
trs forall a. Seq a -> Int -> a
`S.index` Int
i
!tr :: Tree Length a
tr = forall e a. (e -> e) -> Tree e a -> Tree e a
modifyStem (forall a. Num a => a -> a -> a
+ Length
dvr) forall a b. (a -> b) -> a -> b
$ Seq (Tree Length a)
trs forall a. Seq a -> Int -> a
`S.index` (Int
i forall a. Num a => a -> a -> a
+ Int
1)
!h' :: Length
h' = Length
hl forall a. Num a => a -> a -> a
+ Length
dvl
!tm :: Tree Length a
tm = forall e a. e -> a -> Forest e a -> Tree e a
Node Length
0 a
l [Tree Length a
tl, Tree Length a
tr]
!trs'' :: Seq (Tree Length a)
trs'' = (forall a. Int -> Seq a -> Seq a
S.take Int
i Seq (Tree Length a)
trs forall a. Seq a -> a -> Seq a
S.|> Tree Length a
tm) forall a. Seq a -> Seq a -> Seq a
S.>< forall a. Int -> Seq a -> Seq a
S.drop (Int
i forall a. Num a => a -> a -> a
+ Int
2) Seq (Tree Length a)
trs
!hs' :: Seq Length
hs' = (forall a. Int -> Seq a -> Seq a
S.take Int
i Seq Length
hs forall a. Seq a -> a -> Seq a
S.|> Length
h') forall a. Seq a -> Seq a -> Seq a
S.>< forall a. Int -> Seq a -> Seq a
S.drop (Int
i forall a. Num a => a -> a -> a
+ Int
2) Seq Length
hs