{-# 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
(Step -> Step -> Bool) -> (Step -> Step -> Bool) -> Eq Step
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
Eq Step
-> (Step -> Step -> Ordering)
-> (Step -> Step -> Bool)
-> (Step -> Step -> Bool)
-> (Step -> Step -> Bool)
-> (Step -> Step -> Bool)
-> (Step -> Step -> Step)
-> (Step -> Step -> Step)
-> Ord 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
$cp1Ord :: Eq Step
Ord,Int -> Step -> ShowS
[Step] -> ShowS
Step -> String
(Int -> Step -> ShowS)
-> (Step -> String) -> ([Step] -> ShowS) -> Show Step
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 ([String] -> ASCII) -> [String] -> ASCII
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
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 String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Int -> [Step] -> [String]
go (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Step]
xs
Step
DownStep -> Int -> Step -> String
ee (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Step
x String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Int -> [Step] -> [String]
go (Int
hInt -> Int -> Int
forall 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 = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
maxhInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Step -> Char
ch Step
x] String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
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
yInt -> Int -> Bool
forall 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
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 }
in if Int
y'Int -> Int -> Bool
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
yInt -> Int -> Bool
forall 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
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 }
in if Int
y'Int -> Int -> Bool
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 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
h (Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)) (Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Step]
ts
Step
DownStep -> Int -> Int -> [Step] -> Int
go Int
h (Int
yInt -> Int -> Int
forall 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 = [Step] -> Int
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
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Step]
ts
Step
DownStep -> Int -> Int -> [Step] -> (Int, Int)
go (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
yInt -> Int -> Int
forall 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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
y' :: Int
y' = case Step
t of { Step
UpStep -> Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 }
in (Int
x',Int
y') (Int, Int) -> [(Int, Int)] -> [(Int, Int)]
forall a. a -> [a] -> [a]
: Int -> Int -> [Step] -> [(Int, Int)]
go Int
x' Int
y' [Step]
ts
pathNumberOfUpSteps :: LatticePath -> Int
pathNumberOfUpSteps :: [Step] -> Int
pathNumberOfUpSteps = (Int, Int) -> Int
forall a b. (a, b) -> a
fst ((Int, Int) -> Int) -> ([Step] -> (Int, Int)) -> [Step] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Step] -> (Int, Int)
pathNumberOfUpDownSteps
pathNumberOfDownSteps :: LatticePath -> Int
pathNumberOfDownSteps :: [Step] -> Int
pathNumberOfDownSteps = (Int, Int) -> Int
forall a b. (a, b) -> b
snd ((Int, Int) -> Int) -> ([Step] -> (Int, Int)) -> [Step] -> Int
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
uInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
d [Step]
ps
Step
DownStep -> Int -> Int -> [Step] -> (Int, Int)
go Int
u (Int
dInt -> Int -> Int
forall 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
xStep -> Step -> Bool
forall a. Eq a => a -> a -> Bool
==Step
UpStep Bool -> Bool -> Bool
&& Step
yStep -> Step -> Bool
forall a. Eq a => a -> a -> Bool
==Step
DownStep then Int
kInt -> Int -> Int
forall 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
yInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 ; Step
DownStep -> Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1 }
cnt' :: Int
cnt' = if Int
y'Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
h then Int
cntInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 else Int
cnt
in Int -> Int -> Int -> [Step] -> Int
go Int
cnt' (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int
y' [Step]
ts
dyckPaths :: Int -> [LatticePath]
dyckPaths :: Int -> [[Step]]
dyckPaths = ([Paren] -> [Step]) -> [[Paren]] -> [[Step]]
forall a b. (a -> b) -> [a] -> [b]
map [Paren] -> [Step]
nestedParensToDyckPath ([[Paren]] -> [[Step]]) -> (Int -> [[Paren]]) -> Int -> [[Step]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Paren]]
nestedParentheses
dyckPathsNaive :: Int -> [LatticePath]
dyckPathsNaive :: Int -> [[Step]]
dyckPathsNaive = Int -> [[Step]]
forall a. (Eq a, Enum a, Num a) => a -> [[Step]]
worker where
worker :: a -> [[Step]]
worker a
0 = [[]]
worker a
m = [[Step]]
as [[Step]] -> [[Step]] -> [[Step]]
forall a. [a] -> [a] -> [a]
++ [[Step]]
bs where
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- a -> [[Step]]
worker (a
ma -> a -> a
forall a. Num a => a -> a -> a
-a
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | a
k <- [a
1..a
ma -> a -> a
forall a. Num a => a -> a -> a
-a
1] , [Step]
p <- a -> [[Step]]
worker (a
ka -> a -> a
forall a. Num a => a -> a -> a
-a
1) , [Step]
q <- a -> [[Step]]
worker (a
ma -> a -> a
forall a. Num a => a -> a -> a
-a
k) ]
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countDyckPaths :: Int -> Integer
countDyckPaths :: Int -> Integer
countDyckPaths Int
m = Int -> Integer
forall a. Integral a => a -> Integer
catalan Int
m
nestedParensToDyckPath :: [Paren] -> LatticePath
nestedParensToDyckPath :: [Paren] -> [Step]
nestedParensToDyckPath = (Paren -> Step) -> [Paren] -> [Step]
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 = (Step -> Paren) -> [Step] -> [Paren]
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
hInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
mInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
mInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 = [[]]
| Int
hInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
0 = []
| Bool
otherwise = [[Step]]
as [[Step]] -> [[Step]] -> [[Step]]
forall a. [a] -> [a] -> [a]
++ [[Step]]
bs
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- Int -> Int -> [[Step]]
boundedDyckPaths (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
k <- [Int
1..Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] , [Step]
p <- Int -> Int -> [[Step]]
boundedDyckPaths (Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- Int -> Int -> [[Step]]
boundedDyckPaths Int
h (Int
mInt -> Int -> Int
forall 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
| Int -> Bool
forall a. Integral a => a -> Bool
odd (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
y) = []
| Int
xInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
yInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
0 = []
| Int
yInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 = Int -> [[Step]]
dyckPaths (Int -> Int -> Int
forall a. Integral a => a -> a -> a
div Int
x Int
2)
| Int
xInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
1 Bool -> Bool -> Bool
&& Int
yInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
1 = [[Step
UpStep]]
| Bool
otherwise = [[Step]]
as [[Step]] -> [[Step]] -> [[Step]]
forall a. [a] -> [a] -> [a]
++ [[Step]]
bs
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
as :: [[Step]]
as = [ Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p | [Step]
p <- Int -> Int -> [[Step]]
worker (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Int
yInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step] -> [Step]
bracket [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
k <- [Int
1..(Int -> Int -> Int
forall a. Integral a => a -> a -> a
div Int
x Int
2)] , [Step]
p <- Int -> [[Step]]
dyckPaths (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- Int -> Int -> [[Step]]
worker (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
k) Int
y ]
countLatticePaths :: (Int,Int) -> Integer
countLatticePaths :: (Int, Int) -> Integer
countLatticePaths (Int
x,Int
y)
| Int -> Bool
forall a. Integral a => a -> Bool
even (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
y) = Int -> Int -> Integer
forall a. Integral a => a -> a -> Integer
catalanTriangle (Int -> Int -> Int
forall a. Integral a => a -> a -> a
div (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
y) Int
2) (Int -> Int -> Int
forall a. Integral a => a -> a -> a
div (Int
xInt -> Int -> Int
forall 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 = Int -> Int -> [[Step]]
forall t. (Num t, Ord t) => t -> Int -> [[Step]]
worker where
worker :: t -> Int -> [[Step]]
worker !t
k !Int
m
| Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = if t
kt -> t -> Bool
forall a. Eq a => a -> a -> Bool
==t
0 then [[]] else []
| t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = []
| Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = []
| t
k t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
1 = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- Int -> [[Step]]
dyckPaths (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ]
| Bool
otherwise = [ [Step] -> [Step]
bracket [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
l <- [Int
1..Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] , [Step]
p <- Int -> [[Step]]
dyckPaths (Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- t -> Int -> [[Step]]
worker (t
kt -> t -> t
forall a. Num a => a -> a -> a
-t
1) (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
l) ]
where
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countTouchingDyckPaths
:: Int
-> Int
-> Integer
countTouchingDyckPaths :: Int -> Int -> Integer
countTouchingDyckPaths Int
t Int
n
| Int
tInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
&& Int
nInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 = Integer
1
| Bool
otherwise = (Int, Int) -> Integer
countLatticePaths (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
tInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1,Int
tInt -> Int -> Int
forall 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 = Int -> Int -> [[Step]]
forall t. (Ord t, Enum t, Num t) => t -> Int -> [[Step]]
worker where
worker :: t -> Int -> [[Step]]
worker !t
k !Int
m
| Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = if t
kt -> t -> Bool
forall a. Eq a => a -> a -> Bool
==t
0 then [[]] else []
| t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = []
| Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = []
| t
k t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
1 = [ Int -> [Step]
singlePeak Int
m ]
| Bool
otherwise = [[Step]]
as [[Step]] -> [[Step]] -> [[Step]]
forall a. [a] -> [a] -> [a]
++ [[Step]]
bs [[Step]] -> [[Step]] -> [[Step]]
forall a. [a] -> [a] -> [a]
++ [[Step]]
cs
where
as :: [[Step]]
as = [ [Step] -> [Step]
bracket [Step]
p | [Step]
p <- t -> Int -> [[Step]]
worker t
k (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ]
bs :: [[Step]]
bs = [ [Step]
smallHill [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | [Step]
q <- t -> Int -> [[Step]]
worker (t
kt -> t -> t
forall a. Num a => a -> a -> a
-t
1) (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) ]
cs :: [[Step]]
cs = [ [Step] -> [Step]
bracket [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step]
q | Int
l <- [Int
2..Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] , t
a <- [t
1..t
kt -> t -> t
forall a. Num a => a -> a -> a
-t
1] , [Step]
p <- t -> Int -> [[Step]]
worker t
a (Int
lInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) , [Step]
q <- t -> Int -> [[Step]]
worker (t
kt -> t -> t
forall a. Num a => a -> a -> a
-t
a) (Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
l) ]
smallHill :: [Step]
smallHill = [ Step
UpStep , Step
DownStep ]
singlePeak :: Int -> [Step]
singlePeak !Int
m = Int -> Step -> [Step]
forall a. Int -> a -> [a]
replicate Int
m Step
UpStep [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ Int -> Step -> [Step]
forall a. Int -> a -> [a]
replicate Int
m Step
DownStep
bracket :: [Step] -> [Step]
bracket [Step]
p = Step
UpStep Step -> [Step] -> [Step]
forall a. a -> [a] -> [a]
: [Step]
p [Step] -> [Step] -> [Step]
forall a. [a] -> [a] -> [a]
++ [Step
DownStep]
countPeakingDyckPaths
:: Int
-> Int
-> Integer
countPeakingDyckPaths :: Int -> Int -> Integer
countPeakingDyckPaths Int
k Int
m
| Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = if Int
kInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 then Integer
1 else Integer
0
| Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Integer
0
| Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Integer
0
| Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Integer
1
| Bool
otherwise = Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
div (Int -> Int -> Integer
forall a. Integral a => a -> a -> Integer
binomial Int
m Int
k Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Int -> Integer
forall a. Integral a => a -> a -> Integer
binomial Int
m (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)) (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m)
randomDyckPath :: RandomGen g => Int -> g -> (LatticePath,g)
randomDyckPath :: Int -> g -> ([Step], g)
randomDyckPath Int
m g
g0 = ([Paren] -> [Step]
nestedParensToDyckPath [Paren]
parens, g
g1) where
([Paren]
parens,g
g1) = Int -> g -> ([Paren], g)
forall g. RandomGen g => Int -> g -> ([Paren], g)
randomNestedParentheses Int
m g
g0