{-# LANGUAGE BangPatterns, FlexibleInstances, TypeSynonymInstances #-}
module Math.Combinat.LatticePaths where
import Data.List
import System.Random
import Math.Combinat.Classes
import Math.Combinat.Numbers
import Math.Combinat.Trees.Binary
import Math.Combinat.ASCII as ASCII
data Step
= UpStep
| DownStep
deriving (Step -> Step -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Step -> Step -> Bool
$c/= :: Step -> Step -> Bool
== :: Step -> Step -> Bool
$c== :: Step -> Step -> Bool
Eq,Eq Step
Step -> Step -> Bool
Step -> Step -> Ordering
Step -> Step -> Step
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Step -> Step -> Step
$cmin :: Step -> Step -> Step
max :: Step -> Step -> Step
$cmax :: Step -> Step -> Step
>= :: Step -> Step -> Bool
$c>= :: Step -> Step -> Bool
> :: Step -> Step -> Bool
$c> :: Step -> Step -> Bool
<= :: Step -> Step -> Bool
$c<= :: Step -> Step -> Bool
< :: Step -> Step -> Bool
$c< :: Step -> Step -> Bool
compare :: Step -> Step -> Ordering
$ccompare :: Step -> Step -> Ordering
Ord,Int -> Step -> ShowS
[Step] -> ShowS
Step -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Step] -> ShowS
$cshowList :: [Step] -> ShowS
show :: Step -> String
$cshow :: Step -> String
showsPrec :: Int -> Step -> ShowS
$cshowsPrec :: Int -> Step -> ShowS
Show)
type LatticePath = [Step]
asciiPath :: LatticePath -> ASCII
asciiPath :: [Step] -> ASCII
asciiPath [Step]
p = [String] -> ASCII
asciiFromLines forall a b. (a -> b) -> a -> b
$ forall a. [[a]] -> [[a]]
transpose (Int -> [Step] -> [String]
go Int
0 [Step]
p) where
go :: Int -> [Step] -> [String]
go !Int
h [] = []
go !Int
h (Step
x:[Step]
xs) = case Step
x of
Step
UpStep -> Int -> Step -> String
ee Int
h Step
x forall a. a -> [a] -> [a]
: Int -> [Step] -> [String]
go (Int
hforall a. Num a => a -> a -> a
+Int
1) [Step]
xs
Step
DownStep -> Int -> Step -> String
ee (Int
hforall a. Num a => a -> a -> a
-Int
1) Step
x forall a. a -> [a] -> [a]
: Int -> [Step] -> [String]
go (Int
hforall a. Num a => a -> a -> a
-Int
1) [Step]
xs
maxh :: Int
maxh = [Step] -> Int
pathHeight [Step]
p
ee :: Int -> Step -> String
ee Int
h Step
x = forall a. Int -> a -> [a]
replicate (Int
maxhforall a. Num a => a -> a -> a
-Int
hforall a. Num a => a -> a -> a
-Int
1) Char
' ' forall a. [a] -> [a] -> [a]
++ [Step -> Char
ch Step
x] forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
h Char
' '
ch :: Step -> Char
ch Step
x = case Step
x of
Step
UpStep -> Char
'/'
Step
DownStep -> Char
'\\'
instance DrawASCII LatticePath where
ascii :: [Step] -> ASCII
ascii = [Step] -> ASCII
asciiPath
isValidPath :: LatticePath -> Bool
isValidPath :: [Step] -> Bool
isValidPath = Int -> [Step] -> Bool
go Int
0 where
go :: Int -> LatticePath -> Bool
go :: Int -> [Step] -> Bool
go !Int
y [] = Int
yforall a. Ord a => a -> a -> Bool
>=Int
0
go !Int
y (Step
t:[Step]
ts) = let y' :: Int
y' = case Step
t of { Step
UpStep -> Int
yforall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yforall a. Num a => a -> a -> a
-Int
1 }
in if Int
y'forall a. Ord a => a -> a -> Bool
<Int
0 then Bool
False
else Int -> [Step] -> Bool
go Int
y' [Step]
ts
isDyckPath :: LatticePath -> Bool
isDyckPath :: [Step] -> Bool
isDyckPath = Int -> [Step] -> Bool
go Int
0 where
go :: Int -> LatticePath -> Bool
go :: Int -> [Step] -> Bool
go !Int
y [] = Int
yforall a. Eq a => a -> a -> Bool
==Int
0
go !Int
y (Step
t:[Step]
ts) = let y' :: Int
y' = case Step
t of { Step
UpStep -> Int
yforall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yforall a. Num a => a -> a -> a
-Int
1 }
in if Int
y'forall a. Ord a => a -> a -> Bool
<Int
0 then Bool
False
else Int -> [Step] -> Bool
go Int
y' [Step]
ts
pathHeight :: LatticePath -> Int
pathHeight :: [Step] -> Int
pathHeight = Int -> Int -> [Step] -> Int
go Int
0 Int
0 where
go :: Int -> Int -> LatticePath -> Int
go :: Int -> Int -> [Step] -> Int
go !Int
h !Int
y [] = Int
h
go !Int
h !Int
y (Step
t:[Step]
ts) = case Step
t of
Step
UpStep -> Int -> Int -> [Step] -> Int
go (forall a. Ord a => a -> a -> a
max Int
h (Int
yforall a. Num a => a -> a -> a
+Int
1)) (Int
yforall a. Num a => a -> a -> a
+Int
1) [Step]
ts
Step
DownStep -> Int -> Int -> [Step] -> Int
go Int
h (Int
yforall a. Num a => a -> a -> a
-Int
1) [Step]
ts
instance HasHeight LatticePath where
height :: [Step] -> Int
height = [Step] -> Int
pathHeight
instance HasWidth LatticePath where
width :: [Step] -> Int
width = forall (t :: * -> *) a. Foldable t => t a -> Int
length
pathEndpoint :: LatticePath -> (Int,Int)
pathEndpoint :: [Step] -> (Int, Int)
pathEndpoint = Int -> Int -> [Step] -> (Int, Int)
go Int
0 Int
0 where
go :: Int -> Int -> LatticePath -> (Int,Int)
go :: Int -> Int -> [Step] -> (Int, Int)
go !Int
x !Int
y [] = (Int
x,Int
y)
go !Int
x !Int
y (Step
t:[Step]
ts) = case Step
t of
Step
UpStep -> Int -> Int -> [Step] -> (Int, Int)
go (Int
xforall a. Num a => a -> a -> a
+Int
1) (Int
yforall a. Num a => a -> a -> a
+Int
1) [Step]
ts
Step
DownStep -> Int -> Int -> [Step] -> (Int, Int)
go (Int
xforall a. Num a => a -> a -> a
+Int
1) (Int
yforall a. Num a => a -> a -> a
-Int
1) [Step]
ts
pathCoordinates :: LatticePath -> [(Int,Int)]
pathCoordinates :: [Step] -> [(Int, Int)]
pathCoordinates = Int -> Int -> [Step] -> [(Int, Int)]
go Int
0 Int
0 where
go :: Int -> Int -> LatticePath -> [(Int,Int)]
go :: Int -> Int -> [Step] -> [(Int, Int)]
go Int
_ Int
_ [] = []
go !Int
x !Int
y (Step
t:[Step]
ts) = let x' :: Int
x' = Int
x forall a. Num a => a -> a -> a
+ Int
1
y' :: Int
y' = case Step
t of { Step
UpStep -> Int
yforall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yforall a. Num a => a -> a -> a
-Int
1 }
in (Int
x',Int
y') forall a. a -> [a] -> [a]
: Int -> Int -> [Step] -> [(Int, Int)]
go Int
x' Int
y' [Step]
ts
pathNumberOfUpSteps :: LatticePath -> Int
pathNumberOfUpSteps :: [Step] -> Int
pathNumberOfUpSteps = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Step] -> (Int, Int)
pathNumberOfUpDownSteps
pathNumberOfDownSteps :: LatticePath -> Int
pathNumberOfDownSteps :: [Step] -> Int
pathNumberOfDownSteps = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Step] -> (Int, Int)
pathNumberOfUpDownSteps
pathNumberOfUpDownSteps :: LatticePath -> (Int,Int)
pathNumberOfUpDownSteps :: [Step] -> (Int, Int)
pathNumberOfUpDownSteps = Int -> Int -> [Step] -> (Int, Int)
go Int
0 Int
0 where
go :: Int -> Int -> LatticePath -> (Int,Int)
go :: Int -> Int -> [Step] -> (Int, Int)
go !Int
u !Int
d (Step
p:[Step]
ps) = case Step
p of
Step
UpStep -> Int -> Int -> [Step] -> (Int, Int)
go (Int
uforall a. Num a => a -> a -> a
+Int
1) Int
d [Step]
ps
Step
DownStep -> Int -> Int -> [Step] -> (Int, Int)
go Int
u (Int
dforall a. Num a => a -> a -> a
+Int
1) [Step]
ps
go !Int
u !Int
d [] = (Int
u,Int
d)
pathNumberOfPeaks :: LatticePath -> Int
pathNumberOfPeaks :: [Step] -> Int
pathNumberOfPeaks = Int -> [Step] -> Int
go Int
0 where
go :: Int -> LatticePath -> Int
go :: Int -> [Step] -> Int
go !Int
k (Step
x:xs :: [Step]
xs@(Step
y:[Step]
_)) = Int -> [Step] -> Int
go (if Step
xforall a. Eq a => a -> a -> Bool
==Step
UpStep Bool -> Bool -> Bool
&& Step
yforall a. Eq a => a -> a -> Bool
==Step
DownStep then Int
kforall a. Num a => a -> a -> a
+Int
1 else Int
k) [Step]
xs
go !Int
k [Step
x] = Int
k
go !Int
k [ ] = Int
k
pathNumberOfZeroTouches :: LatticePath -> Int
pathNumberOfZeroTouches :: [Step] -> Int
pathNumberOfZeroTouches = Int -> [Step] -> Int
pathNumberOfTouches' Int
0
pathNumberOfTouches'
:: Int
-> LatticePath -> Int
pathNumberOfTouches' :: Int -> [Step] -> Int
pathNumberOfTouches' Int
h = Int -> Int -> Int -> [Step] -> Int
go Int
0 Int
0 Int
0 where
go :: Int -> Int -> Int -> LatticePath -> Int
go :: Int -> Int -> Int -> [Step] -> Int
go !Int
cnt Int
_ Int
_ [] = Int
cnt
go !Int
cnt !Int
x !Int
y (Step
t:[Step]
ts) = let y' :: Int
y' = case Step
t of { Step
UpStep -> Int
yforall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yforall a. Num a => a -> a -> a
-Int
1 }
cnt' :: Int
cnt' = if Int
y'forall a. Eq a => a -> a -> Bool
==Int
h then Int
cntforall a. Num a => a -> a -> a
+Int
1 else Int
cnt
in Int -> Int -> Int -> [Step] -> Int
go Int
cnt' (Int
xforall a. Num a => a -> a -> a
+Int
1) Int
y' [Step]
ts
dyckPaths :: Int -> [LatticePath]
dyckPaths :: Int -> [[Step]]
dyckPaths = forall a b. (a -> b) -> [a] -> [b]
map [Paren] -> [Step]
nestedParensToDyckPath forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Paren]]
nestedParentheses
dyckPathsNaive :: Int -> [LatticePath]
dyckPathsNaive :: Int -> [[Step]]
dyckPathsNaive = forall {a}. (Eq a, Enum a, Num a) => a -> [[Step]]
worker where
worker :: a -> [[Step]]
worker a
0 = [[]]
worker a
m = [[Step]]
as forall a. [a] -> [a] -> [a]
++ [[Step]]
bs where
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- a -> [[Step]]
worker (a
mforall a. Num a => a -> a -> a
-a
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p forall a. [a] -> [a] -> [a]
++ [Step]
q | a
k <- [a
1..a
mforall a. Num a => a -> a -> a
-a
1] , [Step]
p <- a -> [[Step]]
worker (a
kforall a. Num a => a -> a -> a
-a
1) , [Step]
q <- a -> [[Step]]
worker (a
mforall a. Num a => a -> a -> a
-a
k) ]
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countDyckPaths :: Int -> Integer
countDyckPaths :: Int -> Integer
countDyckPaths Int
m = forall a. Integral a => a -> Integer
catalan Int
m
nestedParensToDyckPath :: [Paren] -> LatticePath
nestedParensToDyckPath :: [Paren] -> [Step]
nestedParensToDyckPath = forall a b. (a -> b) -> [a] -> [b]
map Paren -> Step
f where
f :: Paren -> Step
f Paren
p = case Paren
p of { Paren
LeftParen -> Step
UpStep ; Paren
RightParen -> Step
DownStep }
dyckPathToNestedParens :: LatticePath -> [Paren]
dyckPathToNestedParens :: [Step] -> [Paren]
dyckPathToNestedParens = forall a b. (a -> b) -> [a] -> [b]
map Step -> Paren
g where
g :: Step -> Paren
g Step
s = case Step
s of { Step
UpStep -> Paren
LeftParen ; Step
DownStep -> Paren
RightParen }
boundedDyckPaths
:: Int
-> Int
-> [LatticePath]
boundedDyckPaths :: Int -> Int -> [[Step]]
boundedDyckPaths = Int -> Int -> [[Step]]
boundedDyckPathsNaive
boundedDyckPathsNaive
:: Int
-> Int
-> [LatticePath]
boundedDyckPathsNaive :: Int -> Int -> [[Step]]
boundedDyckPathsNaive = Int -> Int -> [[Step]]
worker where
worker :: Int -> Int -> [[Step]]
worker !Int
h !Int
m
| Int
hforall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
mforall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
mforall a. Eq a => a -> a -> Bool
==Int
0 = [[]]
| Int
hforall a. Ord a => a -> a -> Bool
<=Int
0 = []
| Bool
otherwise = [[Step]]
as forall a. [a] -> [a] -> [a]
++ [[Step]]
bs
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- Int -> Int -> [[Step]]
boundedDyckPaths (Int
hforall a. Num a => a -> a -> a
-Int
1) (Int
mforall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
k <- [Int
1..Int
mforall a. Num a => a -> a -> a
-Int
1] , [Step]
p <- Int -> Int -> [[Step]]
boundedDyckPaths (Int
hforall a. Num a => a -> a -> a
-Int
1) (Int
kforall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- Int -> Int -> [[Step]]
boundedDyckPaths Int
h (Int
mforall a. Num a => a -> a -> a
-Int
k) ]
latticePaths :: (Int,Int) -> [LatticePath]
latticePaths :: (Int, Int) -> [[Step]]
latticePaths = (Int, Int) -> [[Step]]
latticePathsNaive
latticePathsNaive :: (Int,Int) -> [LatticePath]
latticePathsNaive :: (Int, Int) -> [[Step]]
latticePathsNaive (Int
x,Int
y) = Int -> Int -> [[Step]]
worker Int
x Int
y where
worker :: Int -> Int -> [[Step]]
worker !Int
x !Int
y
| forall a. Integral a => a -> Bool
odd (Int
xforall a. Num a => a -> a -> a
-Int
y) = []
| Int
xforall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
yforall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
yforall a. Eq a => a -> a -> Bool
==Int
0 = Int -> [[Step]]
dyckPaths (forall a. Integral a => a -> a -> a
div Int
x Int
2)
| Int
xforall a. Eq a => a -> a -> Bool
==Int
1 Bool -> Bool -> Bool
&& Int
yforall a. Eq a => a -> a -> Bool
==Int
1 = [[Step
UpStep]]
| Bool
otherwise = [[Step]]
as forall a. [a] -> [a] -> [a]
++ [[Step]]
bs
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
as :: [[Step]]
as = [ Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p | [Step]
p <- Int -> Int -> [[Step]]
worker (Int
xforall a. Num a => a -> a -> a
-Int
1) (Int
yforall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
k <- [Int
1..(forall a. Integral a => a -> a -> a
div Int
x Int
2)] , [Step]
p <- Int -> [[Step]]
dyckPaths (Int
kforall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- Int -> Int -> [[Step]]
worker (Int
xforall a. Num a => a -> a -> a
-Int
2forall a. Num a => a -> a -> a
*Int
k) Int
y ]
countLatticePaths :: (Int,Int) -> Integer
countLatticePaths :: (Int, Int) -> Integer
countLatticePaths (Int
x,Int
y)
| forall a. Integral a => a -> Bool
even (Int
xforall a. Num a => a -> a -> a
+Int
y) = forall a. Integral a => a -> a -> Integer
catalanTriangle (forall a. Integral a => a -> a -> a
div (Int
xforall a. Num a => a -> a -> a
+Int
y) Int
2) (forall a. Integral a => a -> a -> a
div (Int
xforall a. Num a => a -> a -> a
-Int
y) Int
2)
| Bool
otherwise = Integer
0
touchingDyckPaths
:: Int
-> Int
-> [LatticePath]
touchingDyckPaths :: Int -> Int -> [[Step]]
touchingDyckPaths = Int -> Int -> [[Step]]
touchingDyckPathsNaive
touchingDyckPathsNaive
:: Int
-> Int
-> [LatticePath]
touchingDyckPathsNaive :: Int -> Int -> [[Step]]
touchingDyckPathsNaive = forall {t}. (Num t, Ord t) => t -> Int -> [[Step]]
worker where
worker :: t -> Int -> [[Step]]
worker !t
k !Int
m
| Int
m forall a. Eq a => a -> a -> Bool
== Int
0 = if t
kforall a. Eq a => a -> a -> Bool
==t
0 then [[]] else []
| t
k forall a. Ord a => a -> a -> Bool
<= t
0 = []
| Int
m forall a. Ord a => a -> a -> Bool
< Int
0 = []
| t
k forall a. Eq a => a -> a -> Bool
== t
1 = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- Int -> [[Step]]
dyckPaths (Int
mforall a. Num a => a -> a -> a
-Int
1) ]
| Bool
otherwise = [ [Step] -> [Step]
bracket [Step]
p forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
l <- [Int
1..Int
mforall a. Num a => a -> a -> a
-Int
1] , [Step]
p <- Int -> [[Step]]
dyckPaths (Int
lforall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- t -> Int -> [[Step]]
worker (t
kforall a. Num a => a -> a -> a
-t
1) (Int
mforall a. Num a => a -> a -> a
-Int
l) ]
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countTouchingDyckPaths
:: Int
-> Int
-> Integer
countTouchingDyckPaths :: Int -> Int -> Integer
countTouchingDyckPaths Int
t Int
n
| Int
tforall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
&& Int
nforall a. Eq a => a -> a -> Bool
==Int
0 = Integer
1
| Bool
otherwise = (Int, Int) -> Integer
countLatticePaths (Int
2forall a. Num a => a -> a -> a
*Int
nforall a. Num a => a -> a -> a
-Int
tforall a. Num a => a -> a -> a
-Int
1,Int
tforall a. Num a => a -> a -> a
-Int
1)
peakingDyckPaths
:: Int
-> Int
-> [LatticePath]
peakingDyckPaths :: Int -> Int -> [[Step]]
peakingDyckPaths = Int -> Int -> [[Step]]
peakingDyckPathsNaive
peakingDyckPathsNaive
:: Int
-> Int
-> [LatticePath]
peakingDyckPathsNaive :: Int -> Int -> [[Step]]
peakingDyckPathsNaive = forall {a}. (Ord a, Enum a, Num a) => a -> Int -> [[Step]]
worker where
worker :: a -> Int -> [[Step]]
worker !a
k !Int
m
| Int
m forall a. Eq a => a -> a -> Bool
== Int
0 = if a
kforall a. Eq a => a -> a -> Bool
==a
0 then [[]] else []
| a
k forall a. Ord a => a -> a -> Bool
<= a
0 = []
| Int
m forall a. Ord a => a -> a -> Bool
< Int
0 = []
| a
k forall a. Eq a => a -> a -> Bool
== a
1 = [ Int -> [Step]
singlePeak Int
m ]
| Bool
otherwise = [[Step]]
as forall a. [a] -> [a] -> [a]
++ [[Step]]
bs forall a. [a] -> [a] -> [a]
++ [[Step]]
cs
where
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- a -> Int -> [[Step]]
worker a
k (Int
mforall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step]
smallHill forall a. [a] -> [a] -> [a]
++ [Step]
q | [Step]
q <- a -> Int -> [[Step]]
worker (a
kforall a. Num a => a -> a -> a
-a
1) (Int
mforall a. Num a => a -> a -> a
-Int
1) ]
cs :: [[Step]]
cs = [ [Step] -> [Step]
bracket [Step]
p forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
l <- [Int
2..Int
mforall a. Num a => a -> a -> a
-Int
1] , a
a <- [a
1..a
kforall a. Num a => a -> a -> a
-a
1] , [Step]
p <- a -> Int -> [[Step]]
worker a
a (Int
lforall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- a -> Int -> [[Step]]
worker (a
kforall a. Num a => a -> a -> a
-a
a) (Int
mforall a. Num a => a -> a -> a
-Int
l) ]
smallHill :: [Step]
smallHill = [ Step
UpStep , Step
DownStep ]
singlePeak :: Int -> [Step]
singlePeak !Int
m = forall a. Int -> a -> [a]
replicate Int
m Step
UpStep forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
m Step
DownStep
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep forall a. a -> [a] -> [a]
: [Step]
p forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countPeakingDyckPaths
:: Int
-> Int
-> Integer
countPeakingDyckPaths :: Int -> Int -> Integer
countPeakingDyckPaths Int
k Int
m
| Int
m forall a. Eq a => a -> a -> Bool
== Int
0 = if Int
kforall a. Eq a => a -> a -> Bool
==Int
0 then Integer
1 else Integer
0
| Int
k forall a. Ord a => a -> a -> Bool
<= Int
0 = Integer
0
| Int
m forall a. Ord a => a -> a -> Bool
< Int
0 = Integer
0
| Int
k forall a. Eq a => a -> a -> Bool
== Int
1 = Integer
1
| Bool
otherwise = forall a. Integral a => a -> a -> a
div (forall a. Integral a => a -> a -> Integer
binomial Int
m Int
k forall a. Num a => a -> a -> a
* forall a. Integral a => a -> a -> Integer
binomial Int
m (Int
kforall a. Num a => a -> a -> a
-Int
1)) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m)
randomDyckPath :: RandomGen g => Int -> g -> (LatticePath,g)
randomDyckPath :: forall g. RandomGen g => Int -> g -> ([Step], g)
randomDyckPath Int
m g
g0 = ([Paren] -> [Step]
nestedParensToDyckPath [Paren]
parens, g
g1) where
([Paren]
parens,g
g1) = forall g. RandomGen g => Int -> g -> ([Paren], g)
randomNestedParentheses Int
m g
g0