-- | Braids. See eg. <https://en.wikipedia.org/wiki/Braid_group>
--
--
-- Based on: 
--
--  * Joan S. Birman, Tara E. Brendle: BRAIDS - A SURVEY
--    <https://www.math.columbia.edu/~jb/Handbook-21.pdf>
--
--
-- Note: This module GHC 7.8, since we use type-level naturals
-- to parametrize the 'Braid' type.
--


{-# LANGUAGE 
      CPP, BangPatterns, 
      ScopedTypeVariables, ExistentialQuantification,
      DataKinds, KindSignatures, Rank2Types,
      TypeOperators, TypeFamilies,
      StandaloneDeriving #-}

module Math.Combinat.Groups.Braid where

--------------------------------------------------------------------------------

import Data.Proxy
import GHC.TypeLits

import Control.Monad

import Data.List ( mapAccumL , foldl' )

import Data.Array.Unboxed
import Data.Array.ST
import Data.Array.IArray
import Data.Array.MArray
import Data.Array.Unsafe
import Data.Array.Base

import Control.Monad.ST

import System.Random

import Math.Combinat.ASCII
import Math.Combinat.Sign
import Math.Combinat.Helper
import Math.Combinat.TypeLevel
import Math.Combinat.Numbers.Series

import Math.Combinat.Permutations ( Permutation(..) , (!!!) )
import qualified Math.Combinat.Permutations as P

--------------------------------------------------------------------------------
-- * Artin generators

-- | A standard Artin generator of a braid: @Sigma i@ represents twisting 
-- the neighbour strands @i@ and @(i+1)@, such that strand @i@ goes /under/ strand @(i+1)@.
--
-- Note: The strands are numbered @1..n@.
data BrGen
  = Sigma    !Int         -- ^ @i@ goes under @(i+1)@
  | SigmaInv !Int         -- ^ @i@ goes above @(i+1)@
  deriving (BrGen -> BrGen -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BrGen -> BrGen -> Bool
$c/= :: BrGen -> BrGen -> Bool
== :: BrGen -> BrGen -> Bool
$c== :: BrGen -> BrGen -> Bool
Eq,Eq BrGen
BrGen -> BrGen -> Bool
BrGen -> BrGen -> Ordering
BrGen -> BrGen -> BrGen
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 :: BrGen -> BrGen -> BrGen
$cmin :: BrGen -> BrGen -> BrGen
max :: BrGen -> BrGen -> BrGen
$cmax :: BrGen -> BrGen -> BrGen
>= :: BrGen -> BrGen -> Bool
$c>= :: BrGen -> BrGen -> Bool
> :: BrGen -> BrGen -> Bool
$c> :: BrGen -> BrGen -> Bool
<= :: BrGen -> BrGen -> Bool
$c<= :: BrGen -> BrGen -> Bool
< :: BrGen -> BrGen -> Bool
$c< :: BrGen -> BrGen -> Bool
compare :: BrGen -> BrGen -> Ordering
$ccompare :: BrGen -> BrGen -> Ordering
Ord,Int -> BrGen -> ShowS
[BrGen] -> ShowS
BrGen -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BrGen] -> ShowS
$cshowList :: [BrGen] -> ShowS
show :: BrGen -> String
$cshow :: BrGen -> String
showsPrec :: Int -> BrGen -> ShowS
$cshowsPrec :: Int -> BrGen -> ShowS
Show)
 
-- | The strand (more precisely, the first of the two strands) the generator twistes
brGenIdx :: BrGen -> Int
brGenIdx :: BrGen -> Int
brGenIdx BrGen
g = case BrGen
g of
  Sigma    Int
i -> Int
i
  SigmaInv Int
i -> Int
i

brGenSign :: BrGen -> Sign
brGenSign :: BrGen -> Sign
brGenSign BrGen
g = case BrGen
g of
  Sigma    Int
_ -> Sign
Plus
  SigmaInv Int
_ -> Sign
Minus

brGenSignIdx :: BrGen -> (Sign,Int)        
brGenSignIdx :: BrGen -> (Sign, Int)
brGenSignIdx BrGen
g = case BrGen
g of
  Sigma    Int
i -> (Sign
Plus ,Int
i)
  SigmaInv Int
i -> (Sign
Minus,Int
i) 

-- | The inverse of a braid generator
invBrGen :: BrGen -> BrGen
invBrGen :: BrGen -> BrGen
invBrGen  BrGen
g = case BrGen
g of
  Sigma    Int
i -> Int -> BrGen
SigmaInv Int
i
  SigmaInv Int
i -> Int -> BrGen
Sigma    Int
i

--------------------------------------------------------------------------------
-- * The braid type
  
-- | The braid group @B_n@ on @n@ strands.
-- The number @n@ is encoded as a type level natural in the type parameter.
--
-- Braids are represented as words in the standard generators and their
-- inverses.
newtype Braid (n :: Nat) = Braid [BrGen] deriving (Int -> Braid n -> ShowS
forall (n :: Nat). Int -> Braid n -> ShowS
forall (n :: Nat). [Braid n] -> ShowS
forall (n :: Nat). Braid n -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Braid n] -> ShowS
$cshowList :: forall (n :: Nat). [Braid n] -> ShowS
show :: Braid n -> String
$cshow :: forall (n :: Nat). Braid n -> String
showsPrec :: Int -> Braid n -> ShowS
$cshowsPrec :: forall (n :: Nat). Int -> Braid n -> ShowS
Show)

-- | The number of strands in the braid
numberOfStrands :: KnownNat n => Braid n -> Int
numberOfStrands :: forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands = forall a. Num a => Integer -> a
fromInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat). Braid n -> Proxy n
braidProxy where                                                         
  braidProxy :: Braid n -> Proxy n
  braidProxy :: forall (n :: Nat). Braid n -> Proxy n
braidProxy Braid n
_ = forall {k} (t :: k). Proxy t
Proxy

-- | Sometimes we want to hide the type-level parameter @n@, for example when
-- dynamically creating braids whose size is known only at runtime.
data SomeBraid = forall n. KnownNat n => SomeBraid (Braid n)

someBraid :: Int -> (forall (n :: Nat). KnownNat n => Braid n) -> SomeBraid
someBraid :: Int -> (forall (n :: Nat). KnownNat n => Braid n) -> SomeBraid
someBraid Int
n forall (n :: Nat). KnownNat n => Braid n
polyBraid = 
  case SomeNat
snat of    
    SomeNat Proxy n
pxy -> forall (n :: Nat). KnownNat n => Braid n -> SomeBraid
SomeBraid (forall {k} (f :: k -> *) (a :: k). f a -> Proxy a -> f a
asProxyTypeOf1 forall (n :: Nat). KnownNat n => Braid n
polyBraid Proxy n
pxy)
  where
    snat :: SomeNat
snat = case Integer -> Maybe SomeNat
someNatVal (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n :: Integer) of
      Just SomeNat
sn -> SomeNat
sn
      Maybe SomeNat
Nothing -> forall a. HasCallStack => String -> a
error String
"someBraid: input is not a natural number"

withSomeBraid :: SomeBraid -> (forall n. KnownNat n => Braid n -> a) -> a
withSomeBraid :: forall a.
SomeBraid -> (forall (n :: Nat). KnownNat n => Braid n -> a) -> a
withSomeBraid SomeBraid
sbraid forall (n :: Nat). KnownNat n => Braid n -> a
f = case SomeBraid
sbraid of SomeBraid Braid n
braid -> forall (n :: Nat). KnownNat n => Braid n -> a
f Braid n
braid

mkBraid :: (forall n. KnownNat n => Braid n -> a) -> Int -> [BrGen] -> a
mkBraid :: forall a.
(forall (n :: Nat). KnownNat n => Braid n -> a)
-> Int -> [BrGen] -> a
mkBraid forall (n :: Nat). KnownNat n => Braid n -> a
f Int
n [BrGen]
w = a
y where
  sb :: SomeBraid
sb = Int -> (forall (n :: Nat). KnownNat n => Braid n) -> SomeBraid
someBraid Int
n (forall (n :: Nat). [BrGen] -> Braid n
Braid [BrGen]
w)
  y :: a
y  = forall a.
SomeBraid -> (forall (n :: Nat). KnownNat n => Braid n -> a) -> a
withSomeBraid SomeBraid
sb forall (n :: Nat). KnownNat n => Braid n -> a
f

withBraid 
  :: Int
  -> (forall (n :: Nat). KnownNat n => Braid n)
  -> (forall (n :: Nat). KnownNat n => Braid n -> a) 
  -> a
withBraid :: forall a.
Int
-> (forall (n :: Nat). KnownNat n => Braid n)
-> (forall (n :: Nat). KnownNat n => Braid n -> a)
-> a
withBraid Int
n forall (n :: Nat). KnownNat n => Braid n
polyBraid forall (n :: Nat). KnownNat n => Braid n -> a
f = 
  case SomeNat
snat of    
    SomeNat Proxy n
pxy -> forall (n :: Nat). KnownNat n => Braid n -> a
f (forall {k} (f :: k -> *) (a :: k). f a -> Proxy a -> f a
asProxyTypeOf1 forall (n :: Nat). KnownNat n => Braid n
polyBraid Proxy n
pxy)
  where
    snat :: SomeNat
snat = case Integer -> Maybe SomeNat
someNatVal (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n :: Integer) of
      Just SomeNat
sn -> SomeNat
sn
      Maybe SomeNat
Nothing -> forall a. HasCallStack => String -> a
error String
"withBraid: input is not a natural number"

--------------------------------------------------------------------------------

braidWord :: Braid n -> [BrGen]
braidWord :: forall (n :: Nat). Braid n -> [BrGen]
braidWord (Braid [BrGen]
gs) = [BrGen]
gs

braidWordLength :: Braid n -> Int
braidWordLength :: forall (n :: Nat). Braid n -> Int
braidWordLength (Braid [BrGen]
gs) = forall (t :: * -> *) a. Foldable t => t a -> Int
length [BrGen]
gs

-- | Embeds a smaller braid group into a bigger braid group    
extend :: (n1 <= n2) => Braid n1 -> Braid n2
extend :: forall (n1 :: Nat) (n2 :: Nat). (n1 <= n2) => Braid n1 -> Braid n2
extend (Braid [BrGen]
gs) = forall (n :: Nat). [BrGen] -> Braid n
Braid [BrGen]
gs

-- | Apply \"free reduction\" to the word, that is, iteratively remove @sigma_i sigma_i^-1@ pairs.
-- The resulting braid is clearly equivalent to the original.
freeReduceBraidWord :: Braid n -> Braid n
freeReduceBraidWord :: forall (n :: Nat). Braid n -> Braid n
freeReduceBraidWord (Braid [BrGen]
orig) = forall (n :: Nat). [BrGen] -> Braid n
Braid ([BrGen] -> [BrGen]
loop [BrGen]
orig) where

  loop :: [BrGen] -> [BrGen]
loop [BrGen]
w = case [BrGen] -> Maybe [BrGen]
reduceStep [BrGen]
w of
    Maybe [BrGen]
Nothing -> [BrGen]
w
    Just [BrGen]
w' -> [BrGen] -> [BrGen]
loop [BrGen]
w'
  
  reduceStep :: [BrGen] -> Maybe [BrGen]
  reduceStep :: [BrGen] -> Maybe [BrGen]
reduceStep = Bool -> [BrGen] -> Maybe [BrGen]
go Bool
False where    
    go :: Bool -> [BrGen] -> Maybe [BrGen]
go !Bool
changed [BrGen]
w = case [BrGen]
w of
      (Sigma    Int
x : SigmaInv Int
y : [BrGen]
rest) | Int
xforall a. Eq a => a -> a -> Bool
==Int
y   -> Bool -> [BrGen] -> Maybe [BrGen]
go Bool
True [BrGen]
rest
      (SigmaInv Int
x : Sigma    Int
y : [BrGen]
rest) | Int
xforall a. Eq a => a -> a -> Bool
==Int
y   -> Bool -> [BrGen] -> Maybe [BrGen]
go Bool
True [BrGen]
rest
      (BrGen
this : [BrGen]
rest)                             -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (BrGen
thisforall a. a -> [a] -> [a]
:) forall a b. (a -> b) -> a -> b
$ Bool -> [BrGen] -> Maybe [BrGen]
go Bool
changed [BrGen]
rest
      [BrGen]
_                                         -> if Bool
changed then forall a. a -> Maybe a
Just [BrGen]
w else forall a. Maybe a
Nothing

--------------------------------------------------------------------------------
-- * Some specific braids

-- | The braid generator @sigma_i@ as a braid
sigma :: KnownNat n => Int -> Braid (n :: Nat)
sigma :: forall (n :: Nat). KnownNat n => Int -> Braid n
sigma Int
k = Braid n
braid where
  braid :: Braid n
braid = if Int
k forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
k forall a. Ord a => a -> a -> Bool
< forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
    then forall (n :: Nat). [BrGen] -> Braid n
Braid [Int -> BrGen
Sigma Int
k]
    else forall a. HasCallStack => String -> a
error String
"sigma: braid generator index out of range"

-- | The braid generator @sigma_i^(-1)@ as a braid
sigmaInv :: KnownNat n => Int -> Braid (n :: Nat)
sigmaInv :: forall (n :: Nat). KnownNat n => Int -> Braid n
sigmaInv Int
k = Braid n
braid where
  braid :: Braid n
braid = if Int
k forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
k forall a. Ord a => a -> a -> Bool
< forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
    then forall (n :: Nat). [BrGen] -> Braid n
Braid [Int -> BrGen
SigmaInv Int
k]
    else forall a. HasCallStack => String -> a
error String
"sigma: braid generator index out of range"

-- | @doubleSigma s t@ (for s<t)is the generator @sigma_{s,t}@ in Birman-Ko-Lee's
-- \"new presentation\". It twistes the strands @s@ and @t@ while going over all
-- other strands. For @t==s+1@ we get back @sigma s@
-- 
doubleSigma :: KnownNat n => Int -> Int -> Braid (n :: Nat)
doubleSigma :: forall (n :: Nat). KnownNat n => Int -> Int -> Braid n
doubleSigma Int
s Int
t = Braid n
braid where
  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  braid :: Braid n
braid
    | Int
s forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
s forall a. Ord a => a -> a -> Bool
> Int
n   = forall a. HasCallStack => String -> a
error String
"doubleSigma: s index out of range"
    | Int
t forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
t forall a. Ord a => a -> a -> Bool
> Int
n   = forall a. HasCallStack => String -> a
error String
"doubleSigma: t index out of range"
    | Int
s forall a. Ord a => a -> a -> Bool
>= Int
t           = forall a. HasCallStack => String -> a
error String
"doubleSigma: s >= t"
    | Bool
otherwise        = forall (n :: Nat). [BrGen] -> Braid n
Braid forall a b. (a -> b) -> a -> b
$
       [ Int -> BrGen
Sigma Int
i | Int
i<-[Int
tforall a. Num a => a -> a -> a
-Int
1,Int
tforall a. Num a => a -> a -> a
-Int
2..Int
s] ] forall a. [a] -> [a] -> [a]
++ [ Int -> BrGen
SigmaInv Int
i | Int
i<-[Int
sforall a. Num a => a -> a -> a
+Int
1..Int
tforall a. Num a => a -> a -> a
-Int
1] ]

-- | @positiveWord [2,5,1]@ is shorthand for the word @sigma_2*sigma_5*sigma_1@.
positiveWord :: KnownNat n => [Int] -> Braid (n :: Nat)
positiveWord :: forall (n :: Nat). KnownNat n => [Int] -> Braid n
positiveWord [Int]
idxs = Braid n
braid where
  braid :: Braid n
braid = forall (n :: Nat). [BrGen] -> Braid n
Braid (forall a b. (a -> b) -> [a] -> [b]
map Int -> BrGen
gen [Int]
idxs) 
  n :: Int
n     = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  gen :: Int -> BrGen
gen Int
i = if Int
iforall a. Ord a => a -> a -> Bool
>Int
0 Bool -> Bool -> Bool
&& Int
iforall a. Ord a => a -> a -> Bool
<Int
n then Int -> BrGen
Sigma Int
i else forall a. HasCallStack => String -> a
error String
"positiveWord: index out of range"
       
-- | The (positive) half-twist of all the braid strands, usually denoted by @Delta@.
halfTwist :: KnownNat n => Braid n
halfTwist :: forall (n :: Nat). KnownNat n => Braid n
halfTwist = Braid n
braid where
  braid :: Braid n
braid = forall (n :: Nat). [BrGen] -> Braid n
Braid forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Int -> BrGen
Sigma forall a b. (a -> b) -> a -> b
$ Int -> [Int]
_halfTwist Int
n 
  n :: Int
n     = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid

-- | The untyped version of 'halfTwist'
_halfTwist :: Int -> [Int]
_halfTwist :: Int -> [Int]
_halfTwist Int
n = [Int]
gens where
  gens :: [Int]
gens  = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ Int -> [Int]
sub Int
k | Int
k<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] ]
  sub :: Int -> [Int]
sub Int
k = [ Int
j | Int
j<-[Int
nforall a. Num a => a -> a -> a
-Int
1,Int
nforall a. Num a => a -> a -> a
-Int
2..Int
k] ]
  
-- | Synonym for 'halfTwist'
theGarsideBraid :: KnownNat n => Braid n
theGarsideBraid :: forall (n :: Nat). KnownNat n => Braid n
theGarsideBraid = forall (n :: Nat). KnownNat n => Braid n
halfTwist 

-- | The inner automorphism defined by @tau(X) = Delta^-1 X Delta@, 
-- where @Delta@ is the positive half-twist.
-- 
-- This sends each generator @sigma_j@ to @sigma_(n-j)@.
--
tau :: KnownNat n => Braid n -> Braid n
tau :: forall (n :: Nat). KnownNat n => Braid n -> Braid n
tau braid :: Braid n
braid@(Braid [BrGen]
gens) = forall (n :: Nat). [BrGen] -> Braid n
Braid (forall a b. (a -> b) -> [a] -> [b]
map BrGen -> BrGen
f [BrGen]
gens) where
  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  f :: BrGen -> BrGen
f (Sigma    Int
i) = Int -> BrGen
Sigma    (Int
nforall a. Num a => a -> a -> a
-Int
i)
  f (SigmaInv Int
i) = Int -> BrGen
SigmaInv (Int
nforall a. Num a => a -> a -> a
-Int
i)


-- | The involution @tau@ on permutations (permutation braids)
--
tauPerm :: Permutation -> Permutation
tauPerm :: Permutation -> Permutation
tauPerm Permutation
perm = Int -> [Int] -> Permutation
P.toPermutationUnsafeN Int
n [ (Int
nforall a. Num a => a -> a -> a
+Int
1) forall a. Num a => a -> a -> a
- Permutation
perm Permutation -> Int -> Int
!!! (Int
nforall a. Num a => a -> a -> a
-Int
i) | Int
i<-[Int
0..Int
nforall a. Num a => a -> a -> a
-Int
1] ] where
  n :: Int
n = Permutation -> Int
P.permutationSize Permutation
perm

--------------------------------------------------------------------------------
-- * Group operations

-- | The trivial braid
identity :: Braid n
identity :: forall (n :: Nat). Braid n
identity = forall (n :: Nat). [BrGen] -> Braid n
Braid []

-- | The inverse of a braid. Note: we do not perform reduction here,
-- as a word is reduced if and only if its inverse is reduced.
inverse :: Braid n -> Braid n
inverse :: forall (n :: Nat). Braid n -> Braid n
inverse = forall (n :: Nat). [BrGen] -> Braid n
Braid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map BrGen -> BrGen
invBrGen forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat). Braid n -> [BrGen]
braidWord

-- | Composes two braids, doing free reduction on the result 
-- (that is, removing @(sigma_k * sigma_k^-1)@ pairs@)
compose :: Braid n -> Braid n -> Braid n
compose :: forall (n :: Nat). Braid n -> Braid n -> Braid n
compose (Braid [BrGen]
gs) (Braid [BrGen]
hs) = forall (n :: Nat). Braid n -> Braid n
freeReduceBraidWord forall a b. (a -> b) -> a -> b
$ forall (n :: Nat). [BrGen] -> Braid n
Braid ([BrGen]
gsforall a. [a] -> [a] -> [a]
++[BrGen]
hs)

composeMany :: [Braid n] -> Braid n
composeMany :: forall (n :: Nat). [Braid n] -> Braid n
composeMany = forall (n :: Nat). Braid n -> Braid n
freeReduceBraidWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nat). [BrGen] -> Braid n
Braid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall (n :: Nat). Braid n -> [BrGen]
braidWord 

-- | Composes two braids without doing any reduction.
composeDontReduce :: Braid n -> Braid n -> Braid n
composeDontReduce :: forall (n :: Nat). Braid n -> Braid n -> Braid n
composeDontReduce (Braid [BrGen]
gs) (Braid [BrGen]
hs) = forall (n :: Nat). [BrGen] -> Braid n
Braid ([BrGen]
gsforall a. [a] -> [a] -> [a]
++[BrGen]
hs)

--------------------------------------------------------------------------------
-- * Braid permutations

-- | A braid is pure if its permutation is trivial
isPureBraid :: KnownNat n => Braid n -> Bool
isPureBraid :: forall (n :: Nat). KnownNat n => Braid n -> Bool
isPureBraid Braid n
braid = (forall (n :: Nat). KnownNat n => Braid n -> Permutation
braidPermutation Braid n
braid forall a. Eq a => a -> a -> Bool
== Int -> Permutation
P.identityPermutation Int
n) where
  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid

-- | Returns the left-to-right permutation associated to the braid. 
-- We follow the strands /from the left to the right/ (or from the top to the 
-- bottom), and return the permutation taking the left side to the right side.
--
-- This is compatible with /right/ (standard) action of the permutations:
-- @permuteRight (braidPermutationRight b1)@ corresponds to the left-to-right
-- permutation of the strands; also:
--
-- > (braidPermutation b1) `multiply` (braidPermutation b2) == braidPermutation (b1 `compose` b2)
--
-- Writing the right numbering of the strands below the left numbering,
-- we got the two-line notation of the permutation.
--
braidPermutation :: KnownNat n => Braid n -> Permutation
braidPermutation :: forall (n :: Nat). KnownNat n => Braid n -> Permutation
braidPermutation braid :: Braid n
braid@(Braid [BrGen]
gens) = Permutation
perm where
  n :: Int
n    = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  perm :: Permutation
perm = Int -> [Int] -> Permutation
_braidPermutation Int
n (forall a b. (a -> b) -> [a] -> [b]
map BrGen -> Int
brGenIdx [BrGen]
gens)

-- | This is an untyped version of 'braidPermutation'
_braidPermutation :: Int -> [Int] -> Permutation
_braidPermutation :: Int -> [Int] -> Permutation
_braidPermutation Int
n [Int]
idxs = UArray Int Int -> Permutation
P.uarrayToPermutationUnsafe (forall i e. (forall s. ST s (STUArray s i e)) -> UArray i e
runSTUArray forall s. ST s (STUArray s Int Int)
action) where

  action :: forall s. ST s (STUArray s Int Int) 
  action :: forall s. ST s (STUArray s Int Int)
action = do 
    STUArray s Int Int
arr <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) 
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
i -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
arr Int
i Int
i
    forall {m :: * -> *} {a :: * -> * -> *} {e} {i}.
(MArray a e m, Ix i, Num i) =>
a i e -> [i] -> m (a i e)
worker STUArray s Int Int
arr [Int]
idxs
    forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Int Int
arr
    
  worker :: a i e -> [i] -> m (a i e)
worker a i e
arr = [i] -> m (a i e)
go where
    go :: [i] -> m (a i e)
go []     = forall (m :: * -> *) a. Monad m => a -> m a
return a i e
arr 
    go (i
i:[i]
is) = do
      e
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray a i e
arr  i
i
      e
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray a i e
arr (i
iforall a. Num a => a -> a -> a
+i
1)
      forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray a i e
arr  i
i    e
b
      forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray a i e
arr (i
iforall a. Num a => a -> a -> a
+i
1) e
a
      [i] -> m (a i e)
go [i]
is

--------------------------------------------------------------------------------
-- * Permutation braids

-- | A positive braid word contains only positive (@Sigma@) generators.
isPositiveBraidWord :: KnownNat n => Braid n -> Bool
isPositiveBraidWord :: forall (n :: Nat). KnownNat n => Braid n -> Bool
isPositiveBraidWord (Braid [BrGen]
gs) = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Sign -> Bool
isPlus forall b c a. (b -> c) -> (a -> b) -> a -> c
. BrGen -> Sign
brGenSign) [BrGen]
gs 

-- | A /permutation braid/ is a positive braid where any two strands cross
-- at most one, and /positively/. 
--
isPermutationBraid :: KnownNat n => Braid n -> Bool
isPermutationBraid :: forall (n :: Nat). KnownNat n => Braid n -> Bool
isPermutationBraid Braid n
braid = forall (n :: Nat). KnownNat n => Braid n -> Bool
isPositiveBraidWord Braid n
braid Bool -> Bool -> Bool
&& Bool
crosses where
  crosses :: Bool
crosses     = forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ Int -> Int -> Bool
check Int
i Int
j | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1], Int
j<-[Int
iforall a. Num a => a -> a -> a
+Int
1..Int
n] ] 
  check :: Int -> Int -> Bool
check Int
i Int
j   = forall {a}. (Eq a, Num a) => a -> Bool
zeroOrOne (UArray (Int, Int) Int
lkMatrix forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! (Int
i,Int
j)) 
  zeroOrOne :: a -> Bool
zeroOrOne a
a = (a
aforall a. Eq a => a -> a -> Bool
==a
1 Bool -> Bool -> Bool
|| a
aforall a. Eq a => a -> a -> Bool
==a
0)
  lkMatrix :: UArray (Int, Int) Int
lkMatrix    = forall (n :: Nat). KnownNat n => Braid n -> UArray (Int, Int) Int
linkingMatrix   Braid n
braid
  n :: Int
n           = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid

-- | Untyped version of 'isPermutationBraid' for positive words.
_isPermutationBraid :: Int -> [Int] -> Bool
_isPermutationBraid :: Int -> [Int] -> Bool
_isPermutationBraid Int
n [Int]
gens = Bool
crosses where
  crosses :: Bool
crosses     = forall (t :: * -> *). Foldable t => t Bool -> Bool
and [ Int -> Int -> Bool
check Int
i Int
j | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1], Int
j<-[Int
iforall a. Num a => a -> a -> a
+Int
1..Int
n] ] 
  check :: Int -> Int -> Bool
check Int
i Int
j   = forall {a}. (Eq a, Num a) => a -> Bool
zeroOrOne (UArray (Int, Int) Int
lkMatrix forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! (Int
i,Int
j)) 
  zeroOrOne :: a -> Bool
zeroOrOne a
a = (a
aforall a. Eq a => a -> a -> Bool
==a
1 Bool -> Bool -> Bool
|| a
aforall a. Eq a => a -> a -> Bool
==a
0)
  lkMatrix :: UArray (Int, Int) Int
lkMatrix    = Int -> [BrGen] -> UArray (Int, Int) Int
_linkingMatrix Int
n forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Int -> BrGen
Sigma [Int]
gens

-- | For any permutation this functions returns a /permutation braid/ realizing
-- that permutation. Note that this is not unique, so we make an arbitrary choice
-- (except for the permutation @[n,n-1..1]@ reversing the order, in which case 
-- the result must be the half-twist braid).
-- 
-- The resulting braid word will have a length at most @choose n 2@ (and will have
-- that length only for the permutation @[n,n-1..1]@)
--
-- > braidPermutationRight (permutationBraid perm) == perm
-- > isPermutationBraid    (permutationBraid perm) == True
--
permutationBraid :: KnownNat n => Permutation -> Braid n
permutationBraid :: forall (n :: Nat). KnownNat n => Permutation -> Braid n
permutationBraid Permutation
perm = Braid n
braid where
  n1 :: Int
n1 = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  n2 :: Int
n2 = Permutation -> Int
P.permutationSize Permutation
perm
  braid :: Braid n
braid = if Int
n1 forall a. Eq a => a -> a -> Bool
== Int
n2
    then forall (n :: Nat). [BrGen] -> Braid n
Braid (forall a b. (a -> b) -> [a] -> [b]
map Int -> BrGen
Sigma forall a b. (a -> b) -> a -> b
$ Permutation -> [Int]
_permutationBraid Permutation
perm)
    else forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"permutationBraid: incompatible n: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n1 forall a. [a] -> [a] -> [a]
++ String
" vs. " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n2

-- | Untyped version of 'permutationBraid'
_permutationBraid :: Permutation -> [Int]
_permutationBraid :: Permutation -> [Int]
_permutationBraid = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Permutation -> [[Int]]
_permutationBraid'

-- | Returns the individual \"phases\" of the a permutation braid realizing the
-- given permutation.
_permutationBraid' :: Permutation -> [[Int]]
_permutationBraid' :: Permutation -> [[Int]]
_permutationBraid' Permutation
perm = forall a. (forall s. ST s a) -> a
runST forall s. ST s [[Int]]
action where
  n :: Int
n = Permutation -> Int
P.permutationSize Permutation
perm

  action :: forall s. ST s [[Int]]
  action :: forall s. ST s [[Int]]
action = do

    -- cfwd = the current state of strands    : cfwd!j = where is strand #j now?
    -- cinv = the inverse of that permutation : cinv!i = which strand is on the #i position now?

    STUArray s Int Int
cfwd <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) :: ST s (STUArray s Int Int)
    STUArray s Int Int
cinv <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) :: ST s (STUArray s Int Int)
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
j -> do
      forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cfwd Int
j Int
j
      forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cinv Int
j Int
j

    let doSwap :: Int -> ST s ()
doSwap Int
i = do     
          Int
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
cinv  Int
i
          Int
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
cinv (Int
iforall a. Num a => a -> a -> a
+Int
1)
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cinv  Int
i    Int
b
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cinv (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
a

          Int
u <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
cfwd Int
a
          Int
v <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
cfwd Int
b
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cfwd Int
a Int
v
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
cfwd Int
b Int
u

    -- at the k-th phase, we move the (inv!k)-th strand, which is the k-th strand /on the RHS/, to correct position.
    let worker :: Int -> ST s [[Int]]
worker Int
phase
          | Int
phase forall a. Ord a => a -> a -> Bool
>= Int
n  = forall (m :: * -> *) a. Monad m => a -> m a
return []
          | Bool
otherwise   = do
              let tgt :: Int
tgt = Permutation -> Int -> Int
P.lookupPermutation Permutation
perm Int
phase  -- (arr ! phase)
              Int
src <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
cfwd Int
tgt
              let this :: [Int]
this = [Int
srcforall a. Num a => a -> a -> a
-Int
1,Int
srcforall a. Num a => a -> a -> a
-Int
2..Int
phase]
              forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Int -> ST s ()
doSwap forall a b. (a -> b) -> a -> b
$ [Int]
this 
              [[Int]]
rest <- Int -> ST s [[Int]]
worker (Int
phaseforall a. Num a => a -> a -> a
+Int
1)
              forall (m :: * -> *) a. Monad m => a -> m a
return ([Int]
thisforall a. a -> [a] -> [a]
:[[Int]]
rest)

    Int -> ST s [[Int]]
worker Int
1
 

-- | We compute the linking numbers between all pairs of strands:
--
-- > linkingMatrix braid ! (i,j) == strandLinking braid i j 
--
linkingMatrix :: KnownNat n => Braid n -> UArray (Int,Int) Int
linkingMatrix :: forall (n :: Nat). KnownNat n => Braid n -> UArray (Int, Int) Int
linkingMatrix braid :: Braid n
braid@(Braid [BrGen]
gens) = Int -> [BrGen] -> UArray (Int, Int) Int
_linkingMatrix (forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid) [BrGen]
gens where

-- | Untyped version of 'linkingMatrix'
_linkingMatrix :: Int -> [BrGen] -> UArray (Int,Int) Int
_linkingMatrix :: Int -> [BrGen] -> UArray (Int, Int) Int
_linkingMatrix Int
n [BrGen]
gens = forall i e. (forall s. ST s (STUArray s i e)) -> UArray i e
runSTUArray forall s. ST s (STUArray s (Int, Int) Int)
action where

  action :: forall s. ST s (STUArray s (Int,Int) Int)
  action :: forall s. ST s (STUArray s (Int, Int) Int)
action = do
    STUArray s Int Int
perm <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int
1,Int
n) :: ST s (STUArray s Int Int)
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1..Int
n] forall a b. (a -> b) -> a -> b
$ \Int
i -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm Int
i Int
i
    let doSwap :: Int -> ST s ()
        doSwap :: Int -> ST s ()
doSwap Int
i = do
          Int
a <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm  Int
i
          Int
b <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1)
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm  Int
i    Int
b
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Int
perm (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
a
               
    STUArray s (Int, Int) Int
mat <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray ((Int
1,Int
1),(Int
n,Int
n)) Int
0 :: ST s (STUArray s (Int,Int) Int)
    let doAdd :: Int -> Int -> Int -> ST s ()
        doAdd :: Int -> Int -> Int -> ST s ()
doAdd Int
i Int
j Int
pm1 = do
          Int
x <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s (Int, Int) Int
mat (Int
i,Int
j)
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s (Int, Int) Int
mat (Int
i,Int
j) (Int
xforall a. Num a => a -> a -> a
+Int
pm1) 
          forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s (Int, Int) Int
mat (Int
j,Int
i) (Int
xforall a. Num a => a -> a -> a
+Int
pm1)
       
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [BrGen]
gens forall a b. (a -> b) -> a -> b
$ \BrGen
g -> do
      let (Sign
sgn,Int
k) = BrGen -> (Sign, Int)
brGenSignIdx BrGen
g
      Int
u <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm  Int
k 
      Int
v <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Int
perm (Int
kforall a. Num a => a -> a -> a
+Int
1)
      Int -> Int -> Int -> ST s ()
doAdd Int
u Int
v (forall a. Num a => Sign -> a
signValue Sign
sgn)
      Int -> ST s ()
doSwap Int
k 
        
    forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s (Int, Int) Int
mat
    
    
-- | The linking number between two strands numbered @i@ and @j@ 
-- (numbered such on the /left/ side).
strandLinking :: KnownNat n => Braid n -> Int -> Int -> Int
strandLinking :: forall (n :: Nat). KnownNat n => Braid n -> Int -> Int -> Int
strandLinking braid :: Braid n
braid@(Braid [BrGen]
gens) Int
i0 Int
j0 
  | Int
i0 forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
i0 forall a. Ord a => a -> a -> Bool
> Int
n  = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"strandLinkingNumber: invalid strand index i: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i0
  | Int
j0 forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
j0 forall a. Ord a => a -> a -> Bool
> Int
n  = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"strandLinkingNumber: invalid strand index j: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
j0
  | Int
i0 forall a. Eq a => a -> a -> Bool
== Int
j0          = Int
0
  | Bool
otherwise         = forall {a}. Num a => Int -> Int -> [BrGen] -> a
go Int
i0 Int
j0 [BrGen]
gens
  where
    n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
    
    go :: Int -> Int -> [BrGen] -> a
go !Int
i !Int
j []     = a
0
    go !Int
i !Int
j (BrGen
g:[BrGen]
gs)  
      | Int
i forall a. Eq a => a -> a -> Bool
== Int
k   Bool -> Bool -> Bool
&& Int
j forall a. Eq a => a -> a -> Bool
== Int
kforall a. Num a => a -> a -> a
+Int
1  = a
s forall a. Num a => a -> a -> a
+ Int -> Int -> [BrGen] -> a
go (Int
iforall a. Num a => a -> a -> a
+Int
1) (Int
jforall a. Num a => a -> a -> a
-Int
1) [BrGen]
gs
      | Int
j forall a. Eq a => a -> a -> Bool
== Int
k   Bool -> Bool -> Bool
&& Int
i forall a. Eq a => a -> a -> Bool
== Int
kforall a. Num a => a -> a -> a
+Int
1  = a
s forall a. Num a => a -> a -> a
+ Int -> Int -> [BrGen] -> a
go (Int
iforall a. Num a => a -> a -> a
-Int
1) (Int
jforall a. Num a => a -> a -> a
+Int
1) [BrGen]
gs
      | Int
i forall a. Eq a => a -> a -> Bool
== Int
k                =     Int -> Int -> [BrGen] -> a
go (Int
iforall a. Num a => a -> a -> a
+Int
1)  Int
j    [BrGen]
gs
      |             Int
i forall a. Eq a => a -> a -> Bool
== Int
kforall a. Num a => a -> a -> a
+Int
1  =     Int -> Int -> [BrGen] -> a
go (Int
iforall a. Num a => a -> a -> a
-Int
1)  Int
j    [BrGen]
gs
      | Int
j forall a. Eq a => a -> a -> Bool
== Int
k                =     Int -> Int -> [BrGen] -> a
go  Int
i    (Int
jforall a. Num a => a -> a -> a
+Int
1) [BrGen]
gs
      |             Int
j forall a. Eq a => a -> a -> Bool
== Int
kforall a. Num a => a -> a -> a
+Int
1  =     Int -> Int -> [BrGen] -> a
go  Int
i    (Int
jforall a. Num a => a -> a -> a
-Int
1) [BrGen]
gs
      | Bool
otherwise             =     Int -> Int -> [BrGen] -> a
go  Int
i     Int
j    [BrGen]
gs
      where
        (Sign
sgn,Int
k) = BrGen -> (Sign, Int)
brGenSignIdx BrGen
g
        s :: a
s = forall a. Num a => Sign -> a
signValue Sign
sgn

--------------------------------------------------------------------------------
-- * Growth 

-- | Bronfman's recursive formula for the reciprocial of the growth function 
-- of /positive/ braids. It was already known (by Deligne) that these generating functions 
-- are reciprocials of polynomials; Bronfman [1] gave a recursive formula for them.
--
-- > let count n l = length $ nub $ [ braidNormalForm w | w <- allPositiveBraidWords n l ]
-- > let convertPoly (1:cs) = zip (map negate cs) [1..]
-- > pseries' (convertPoly $ bronfmanH n) == expandBronfmanH n == [ count n l | l <- [0..] ] 
--
-- * [1] Aaron Bronfman: Growth functions of a class of monoids. Preprint, 2001
--
bronfmanH :: Int -> [Int]
bronfmanH :: Int -> [Int]
bronfmanH Int
n = [[Int]]
bronfmanHsList forall a. [a] -> Int -> a
!! Int
n

-- | An infinite list containing the Bronfman polynomials:
--
-- > bronfmanH n = bronfmanHsList !! n
--
bronfmanHsList :: [[Int]]
bronfmanHsList :: [[Int]]
bronfmanHsList = [[Int]]
list where
  list :: [[Int]]
list = forall a b. (a -> b) -> [a] -> [b]
map Int -> [Int]
go [Int
0..]
  go :: Int -> [Int]
go Int
0 = [Int
1]
  go Int
n = forall a. Num a => [[a]] -> [a]
sumSeries [ forall {a} {a}. (Integral a, Num a) => a -> [a] -> [a]
sgn Int
i forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate (forall {a}. Integral a => a -> a
choose2 Int
i) Int
0 forall a. [a] -> [a] -> [a]
++ [[Int]]
list forall a. [a] -> Int -> a
!! (Int
nforall a. Num a => a -> a -> a
-Int
i) | Int
i<-[Int
1..Int
n] ]
  sgn :: a -> [a] -> [a]
sgn a
i = if forall a. Integral a => a -> Bool
odd a
i then forall a. a -> a
id else forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
negate
  choose2 :: a -> a
choose2 a
k = forall a. Integral a => a -> a -> a
div (a
kforall a. Num a => a -> a -> a
*(a
kforall a. Num a => a -> a -> a
-a
1)) a
2

-- | Expands the reciprocial of @H(n)@ into an infinite power series,
-- giving the growth function of the positive braids on @n@ strands.
expandBronfmanH :: Int -> [Int]
expandBronfmanH :: Int -> [Int]
expandBronfmanH Int
n = forall a. Num a => [(a, Int)] -> [a]
pseries' (forall {a} {b}. (Eq a, Num a, Num b, Enum b) => [a] -> [(a, b)]
convertPoly forall a b. (a -> b) -> a -> b
$ Int -> [Int]
bronfmanH Int
n) where
  convertPoly :: [a] -> [(a, b)]
convertPoly (a
1:[a]
cs) = forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
negate [a]
cs) [b
1..]
   
--------------------------------------------------------------------------------
-- * ASCII diagram

instance KnownNat n => DrawASCII (Braid n) where
  ascii :: Braid n -> ASCII
ascii = forall (n :: Nat). KnownNat n => Braid n -> ASCII
horizBraidASCII

-- | Horizontal braid diagram, drawn from left to right,
-- with strands numbered from the bottom to the top
horizBraidASCII :: KnownNat n => Braid n -> ASCII
horizBraidASCII :: forall (n :: Nat). KnownNat n => Braid n -> ASCII
horizBraidASCII = forall (n :: Nat). KnownNat n => Bool -> Braid n -> ASCII
horizBraidASCII' Bool
True

-- | Horizontal braid diagram, drawn from left to right.
-- The boolean flag indicates whether to flip the strands
-- vertically ('True' means bottom-to-top, 'False' means top-to-bottom) 
horizBraidASCII' :: KnownNat n => Bool -> Braid n -> ASCII
horizBraidASCII' :: forall (n :: Nat). KnownNat n => Bool -> Braid n -> ASCII
horizBraidASCII' Bool
flipped braid :: Braid n
braid@(Braid [BrGen]
gens) = ASCII
final where

  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
 
  final :: ASCII
final        = VAlign -> Int -> ASCII -> ASCII
vExtendWith VAlign
VTop Int
1 forall a b. (a -> b) -> a -> b
$ [ASCII] -> ASCII
hCatTop [ASCII]
allBlocks
  allBlocks :: [ASCII]
allBlocks    = [ASCII]
prelude forall a. [a] -> [a] -> [a]
++ [ASCII]
middleBlocks forall a. [a] -> [a] -> [a]
++ [ASCII]
epilogue
  prelude :: [ASCII]
prelude      = [ ASCII
numberBlock   , ASCII
spaceBlock , ASCII
beginEndBlock ] 
  epilogue :: [ASCII]
epilogue     = [ ASCII
beginEndBlock , ASCII
spaceBlock , ASCII
numberBlock'  ]
  middleBlocks :: [ASCII]
middleBlocks = forall a b. (a -> b) -> [a] -> [b]
map BrGen -> ASCII
block [BrGen]
gens 
  
  block :: BrGen -> ASCII
block BrGen
g = case BrGen
g of
    Sigma    Int
i -> Int -> [String] -> ASCII
block' Int
i forall a b. (a -> b) -> a -> b
$ if Bool
flipped then [String]
over  else [String]
under
    SigmaInv Int
i -> Int -> [String] -> ASCII
block' Int
i forall a b. (a -> b) -> a -> b
$ if Bool
flipped then [String]
under else [String]
over

  block' :: Int -> [String] -> ASCII
block' Int
i [String]
middle = [String] -> ASCII
asciiFromLines forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop Int
2 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat 
                  forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate Int
a [String]
horiz forall a. [a] -> [a] -> [a]
++ [[String]
space3, [String]
middle] forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate Int
b [String]
horiz
    where 
      (Int
a,Int
b) = if Bool
flipped then (Int
nforall a. Num a => a -> a -> a
-Int
iforall a. Num a => a -> a -> a
-Int
1,Int
iforall a. Num a => a -> a -> a
-Int
1) else (Int
iforall a. Num a => a -> a -> a
-Int
1,Int
nforall a. Num a => a -> a -> a
-Int
iforall a. Num a => a -> a -> a
-Int
1)

  -- cycleN :: Int -> [a] -> [a]
  -- cycleN n = concat . replicate n

  spaceBlock :: ASCII
spaceBlock    = (Int, Int) -> ASCII
transparentBox (Int
1,Int
nforall a. Num a => a -> a -> a
*Int
3forall a. Num a => a -> a -> a
-Int
2)
  beginEndBlock :: ASCII
beginEndBlock = [String] -> ASCII
asciiFromLines forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop Int
2 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate Int
n [String]
horiz
  numberBlock :: ASCII
numberBlock   = [Int] -> ASCII
mkNumbers [Int
1..Int
n]
  numberBlock' :: ASCII
numberBlock'  = [Int] -> ASCII
mkNumbers forall a b. (a -> b) -> a -> b
$ Permutation -> [Int]
P.fromPermutation forall a b. (a -> b) -> a -> b
$ forall (n :: Nat). KnownNat n => Braid n -> Permutation
braidPermutation Braid n
braid

  mkNumbers :: [Int] -> ASCII
  mkNumbers :: [Int] -> ASCII
mkNumbers [Int]
list = HAlign -> VSep -> [ASCII] -> ASCII
vCatWith HAlign
HRight (Int -> VSep
VSepSpaces Int
2) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> ASCII
asciiShow 
                 forall a b. (a -> b) -> a -> b
$ (if Bool
flipped then forall a. [a] -> [a]
reverse else forall a. a -> a
id) forall a b. (a -> b) -> a -> b
$ [Int]
list

  under :: [String]
under  = [ String
"\\ /" , String
" / "  , String
"/ \\" ]
  over :: [String]
over   = [ String
"\\ /" , String
" \\ " , String
"/ \\" ]
  horiz :: [String]
horiz  = [ String
"   "  , String
"   "  , String
"___"  ]
  space3 :: [String]
space3 = [ String
"   "  , String
"   "  , String
"   "  ]

--------------------------------------------------------------------------------

{- this is unusably ugly and vertically loooong

-- | Vertical braid diagram, drawn from the top to the bottom.
-- Strands are numbered from the left to the right.
--
-- Writing down the strand numbers from the top and and the bottom
-- gives the two-line notation of the permutation realized by the braid.
--
verticalBraidASCII :: KnownNat n => Braid n -> ASCII
verticalBraidASCII braid@(Braid gens) = final where

  n = numberOfStrands braid
 
  final        = hExtendWith HLeft 1 $ vCatLeft allBlocks
  allBlocks    = prelude ++ middleBlocks ++ epilogue
  prelude      = [ numberBlock   , spaceBlock , beginEndBlock ] 
  epilogue     = [ beginEndBlock , spaceBlock , numberBlock'  ]
  middleBlocks = map block gens 
  
  block g = case g of
    Sigma    i -> block' i under
    SigmaInv i -> block' i over

  block' i middle = asciiFromLines (map f middle) where
    f xs = drop 1 $ concat $ h (i-1) ++ ["   ",xs] ++ h (n-i-1)
    h k  = replicate k "  |"

  spaceBlock    = transparentBox (n*3-2,1)
  beginEndBlock = asciiFromLines $ replicate 3 $ drop 1 $ concat (replicate n "  |")
  numberBlock   = mkNumbers [1..n]
  numberBlock'  = mkNumbers $ P.fromPermutation $ braidPermutation braid

  mkNumbers :: [Int] -> ASCII
  mkNumbers list = asciiFromString (drop 1 $ concatMap show3 list)
  show3 k = let s = show k 
            in  replicate (3-length s) ' ' ++ s

  under  = [ "\\ /" , " / "  , "/ \\" ]
  over   = [ "\\ /" , " \\ " , "/ \\" ]

-}

--------------------------------------------------------------------------------
-- * List of all words

-- | All positive braid words of the given length
allPositiveBraidWords :: KnownNat n => Int -> [Braid n]
allPositiveBraidWords :: forall (n :: Nat). KnownNat n => Int -> [Braid n]
allPositiveBraidWords Int
l = [Braid n]
braids where
  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands (forall a. [a] -> a
head [Braid n]
braids)
  braids :: [Braid n]
braids = forall a b. (a -> b) -> [a] -> [b]
map forall (n :: Nat). [BrGen] -> Braid n
Braid forall a b. (a -> b) -> a -> b
$ Int -> Int -> [[BrGen]]
_allPositiveBraidWords Int
n Int
l 

-- | All braid words of the given length
allBraidWords :: KnownNat n => Int -> [Braid n]
allBraidWords :: forall (n :: Nat). KnownNat n => Int -> [Braid n]
allBraidWords Int
l = [Braid n]
braids where
  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands (forall a. [a] -> a
head [Braid n]
braids)
  braids :: [Braid n]
braids = forall a b. (a -> b) -> [a] -> [b]
map forall (n :: Nat). [BrGen] -> Braid n
Braid forall a b. (a -> b) -> a -> b
$ Int -> Int -> [[BrGen]]
_allBraidWords Int
n Int
l 

-- | Untyped version of 'allPositiveBraidWords'
_allPositiveBraidWords :: Int -> Int -> [[BrGen]]
_allPositiveBraidWords :: Int -> Int -> [[BrGen]]
_allPositiveBraidWords Int
n = Int -> [[BrGen]]
go where
  go :: Int -> [[BrGen]]
go Int
0 = [[]]
  go Int
k = [ Int -> BrGen
Sigma Int
i forall a. a -> [a] -> [a]
: [BrGen]
rest | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] , [BrGen]
rest <- Int -> [[BrGen]]
go (Int
kforall a. Num a => a -> a -> a
-Int
1) ]

-- | Untyped version of 'allBraidWords'
_allBraidWords :: Int -> Int -> [[BrGen]]
_allBraidWords :: Int -> Int -> [[BrGen]]
_allBraidWords Int
n = Int -> [[BrGen]]
go where
  go :: Int -> [[BrGen]]
go Int
0 = [[]]
  go Int
k = [ BrGen
gen forall a. a -> [a] -> [a]
: [BrGen]
rest | BrGen
gen <- [BrGen]
gens , [BrGen]
rest <- Int -> [[BrGen]]
go (Int
kforall a. Num a => a -> a -> a
-Int
1) ]
  gens :: [BrGen]
gens = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [ Int -> BrGen
Sigma Int
i , Int -> BrGen
SigmaInv Int
i ] | Int
i<-[Int
1..Int
nforall a. Num a => a -> a -> a
-Int
1] ]

--------------------------------------------------------------------------------
-- * Random braids  

-- | Random braid word of the given length
randomBraidWord :: (RandomGen g, KnownNat n) => Int -> g -> (Braid n, g)
randomBraidWord :: forall g (n :: Nat).
(RandomGen g, KnownNat n) =>
Int -> g -> (Braid n, g)
randomBraidWord Int
len g
g = (Braid n
braid, g
g') where
  braid :: Braid n
braid  = forall (n :: Nat). [BrGen] -> Braid n
Braid [BrGen]
w
  n :: Int
n      = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  ([BrGen]
w,g
g') = forall g. RandomGen g => Int -> Int -> g -> ([BrGen], g)
_randomBraidWord Int
n Int
len g
g

-- | Random /positive/ braid word of the given length
randomPositiveBraidWord :: (RandomGen g, KnownNat n) => Int -> g -> (Braid n, g)
randomPositiveBraidWord :: forall g (n :: Nat).
(RandomGen g, KnownNat n) =>
Int -> g -> (Braid n, g)
randomPositiveBraidWord Int
len g
g = (Braid n
braid, g
g') where
  braid :: Braid n
braid  = forall (n :: Nat). [BrGen] -> Braid n
Braid [BrGen]
w
  n :: Int
n      = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid
  ([BrGen]
w,g
g') = forall g. RandomGen g => Int -> Int -> g -> ([BrGen], g)
_randomPositiveBraidWord Int
n Int
len g
g

--------------------------------------------------------------------------------

-- | Given a braid word, we perturb it randomly @m@ times using the braid relations,
-- so that the resulting new braid word is equivalent to the original.
--
-- Useful for testing.
--
randomPerturbBraidWord :: forall n g. (RandomGen g, KnownNat n) => Int -> Braid n -> g -> (Braid n, g)
randomPerturbBraidWord :: forall (n :: Nat) g.
(RandomGen g, KnownNat n) =>
Int -> Braid n -> g -> (Braid n, g)
randomPerturbBraidWord Int
m braid :: Braid n
braid@(Braid [BrGen]
xs) g
g = (forall (n :: Nat). [BrGen] -> Braid n
Braid [BrGen]
word' , g
g') where

  ([BrGen]
word',g
g') = Int -> Int -> [BrGen] -> g -> ([BrGen], g)
go Int
m (forall (t :: * -> *) a. Foldable t => t a -> Int
length [BrGen]
xs) [BrGen]
xs g
g 

  n :: Int
n = forall (n :: Nat). KnownNat n => Braid n -> Int
numberOfStrands Braid n
braid

  -- | A random pair cancelling each other
  rndE :: g -> ([BrGen],g)
  rndE :: g -> ([BrGen], g)
rndE g
g = ([BrGen]
e1,g
g'') where
    (Int
i , g
g'  ) = forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (Int
1,Int
nforall a. Num a => a -> a -> a
-Int
1) g
g 
    (Bool
b , g
g'' ) = forall a g. (Random a, RandomGen g) => g -> (a, g)
random          g
g'
    e0 :: [BrGen]
e0 = [Int -> BrGen
SigmaInv Int
i, Int -> BrGen
Sigma Int
i] 
    e1 :: [BrGen]
e1 = if Bool
b then forall a. [a] -> [a]
reverse [BrGen]
e0 else [BrGen]
e0

  brg :: Sign -> Int -> BrGen
brg    Sign
s Int
i = case Sign
s of { Sign
Plus -> Int -> BrGen
Sigma    Int
i ; Sign
Minus -> Int -> BrGen
SigmaInv Int
i }
  brginv :: Sign -> Int -> BrGen
brginv Sign
s Int
i = case Sign
s of { Sign
Plus -> Int -> BrGen
SigmaInv Int
i ; Sign
Minus -> Int -> BrGen
Sigma    Int
i }

  go :: Int -> Int -> [BrGen] -> g -> ([BrGen], g)
  go :: Int -> Int -> [BrGen] -> g -> ([BrGen], g)
go !Int
cnt !Int
len ![BrGen]
word !g
g 

    | Int
cnt forall a. Ord a => a -> a -> Bool
<= Int
0   = ([BrGen]
word, g
g)

    | Int
len forall a. Ord a => a -> a -> Bool
<  Int
2   = let w' :: [BrGen]
w' = if Bool
b1 then ([BrGen]
eforall a. [a] -> [a] -> [a]
++[BrGen]
word) else ([BrGen]
wordforall a. [a] -> [a] -> [a]
++[BrGen]
e)        -- if it is short, we just add a trivial pair somewhere
                   in  g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g4 (Int
lenforall a. Num a => a -> a -> a
+Int
2) [BrGen]
w'

    | forall a. Num a => a -> a
abs (Int
iforall a. Num a => a -> a -> a
-Int
j) forall a. Ord a => a -> a -> Bool
>= Int
2            = g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g4  Int
len    ([BrGen]
as forall a. [a] -> [a] -> [a]
++ BrGen
vforall a. a -> [a] -> [a]
:BrGen
uforall a. a -> [a] -> [a]
:[BrGen]
bs)         -- they commute, so we just commute them

    | Int
i forall a. Eq a => a -> a -> Bool
== Int
j Bool -> Bool -> Bool
&& Sign
sforall a. Eq a => a -> a -> Bool
/=Sign
t            = g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g4 (Int
lenforall a. Num a => a -> a -> a
-Int
2) ([BrGen]
as forall a. [a] -> [a] -> [a]
++ [BrGen]
bs    )         -- they are inverse of each other, so we kill them

    | forall a. Num a => a -> a
abs (Int
iforall a. Num a => a -> a -> a
-Int
j) forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
&& Sign
s forall a. Eq a => a -> a -> Bool
== Sign
t  = let mid :: [BrGen]
mid = if Bool
b1 
                                        then [ Sign -> Int -> BrGen
brg Sign
s Int
j , Sign -> Int -> BrGen
brg Sign
s Int
i , Sign -> Int -> BrGen
brg Sign
s Int
j , Sign -> Int -> BrGen
brginv Sign
s Int
i ]   -- insert pair and
                                        else [ Sign -> Int -> BrGen
brginv Sign
s Int
j , Sign -> Int -> BrGen
brg Sign
s Int
i , Sign -> Int -> BrGen
brg Sign
s Int
j , Sign -> Int -> BrGen
brg Sign
s Int
i ]   -- apply ternary relation 
                                  in  g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g4 (Int
lenforall a. Num a => a -> a -> a
+Int
2) ([BrGen]
as forall a. [a] -> [a] -> [a]
++ [BrGen]
mid forall a. [a] -> [a] -> [a]
++ [BrGen]
bs)

    | Bool
otherwise                 = let mid :: [BrGen]
mid = if Bool
b1
                                        then (BrGen
u forall a. a -> [a] -> [a]
: [BrGen]
e forall a. [a] -> [a] -> [a]
++ [BrGen
v])
                                        else if Bool
b2
                                          then [BrGen
u,BrGen
v] forall a. [a] -> [a] -> [a]
++ [BrGen]
e
                                          else [BrGen]
e forall a. [a] -> [a] -> [a]
++ [BrGen
u,BrGen
v]
                                  in g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g4 (Int
lenforall a. Num a => a -> a -> a
+Int
2) ([BrGen]
asforall a. [a] -> [a] -> [a]
++(BrGen
uforall a. a -> [a] -> [a]
:[BrGen]
e)forall a. [a] -> [a] -> [a]
++[BrGen
v]forall a. [a] -> [a] -> [a]
++[BrGen]
bs)          -- otherwise we just insert an trivial pair         

    where

      (Int
pos         , g
g1 ) = forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (Int
0,Int
lenforall a. Num a => a -> a -> a
-Int
2) g
g
      (Bool
b1 :: Bool  , g
g2 ) = forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g1
      (Bool
b2 :: Bool  , g
g3 ) = forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g2
      ([BrGen]
e           , g
g4 ) = g -> ([BrGen], g)
rndE   g
g3
      ([BrGen]
as,BrGen
u:BrGen
v:[BrGen]
bs) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
pos [BrGen]
word
      (Sign
s,Int
i) = BrGen -> (Sign, Int)
brGenSignIdx BrGen
u
      (Sign
t,Int
j) = BrGen -> (Sign, Int)
brGenSignIdx BrGen
v
  
      continue :: g -> Int -> [BrGen] -> ([BrGen], g)
continue g
g' Int
len' [BrGen]
word' = Int -> Int -> [BrGen] -> g -> ([BrGen], g)
go (Int
cntforall a. Num a => a -> a -> a
-Int
1) Int
len' [BrGen]
word' g
g'

--------------------------------------------------------------------------------

-- | This version of 'randomBraidWord' may be convenient to avoid the type level stuff
withRandomBraidWord 
  :: RandomGen g 
  => (forall n. KnownNat n => Braid n -> a) 
  -> Int                -- ^ number of strands
  -> Int                -- ^ length of the random word
  -> g -> (a, g)
withRandomBraidWord :: forall g a.
RandomGen g =>
(forall (n :: Nat). KnownNat n => Braid n -> a)
-> Int -> Int -> g -> (a, g)
withRandomBraidWord forall (n :: Nat). KnownNat n => Braid n -> a
f Int
n Int
len = forall g a. Rand g a -> g -> (a, g)
runRand forall a b. (a -> b) -> a -> b
$ do
  forall (m :: * -> *) (f :: Nat -> *) int a.
(Integral int, Monad m) =>
(forall (n :: Nat). KnownNat n => f n -> a)
-> (forall (n :: Nat). KnownNat n => m (f n)) -> int -> m a
withSelectedM forall (n :: Nat). KnownNat n => Braid n -> a
f (forall g a. (g -> (a, g)) -> Rand g a
rand forall a b. (a -> b) -> a -> b
$ forall g (n :: Nat).
(RandomGen g, KnownNat n) =>
Int -> g -> (Braid n, g)
randomBraidWord Int
len) Int
n

-- | This version of 'randomPositiveBraidWord' may be convenient to avoid the type level stuff
withRandomPositiveBraidWord 
  :: RandomGen g 
  => (forall n. KnownNat n => Braid n -> a) 
  -> Int                -- ^ number of strands
  -> Int                -- ^ length of the random word
  -> g -> (a, g)
withRandomPositiveBraidWord :: forall g a.
RandomGen g =>
(forall (n :: Nat). KnownNat n => Braid n -> a)
-> Int -> Int -> g -> (a, g)
withRandomPositiveBraidWord forall (n :: Nat). KnownNat n => Braid n -> a
f Int
n Int
len = forall g a. Rand g a -> g -> (a, g)
runRand forall a b. (a -> b) -> a -> b
$ do
  forall (m :: * -> *) (f :: Nat -> *) int a.
(Integral int, Monad m) =>
(forall (n :: Nat). KnownNat n => f n -> a)
-> (forall (n :: Nat). KnownNat n => m (f n)) -> int -> m a
withSelectedM forall (n :: Nat). KnownNat n => Braid n -> a
f (forall g a. (g -> (a, g)) -> Rand g a
rand forall a b. (a -> b) -> a -> b
$ forall g (n :: Nat).
(RandomGen g, KnownNat n) =>
Int -> g -> (Braid n, g)
randomPositiveBraidWord Int
len) Int
n

-- | Untyped version of 'randomBraidWord'
_randomBraidWord 
  :: (RandomGen g) 
  => Int                -- ^ number of strands
  -> Int                -- ^ length of the random word
  -> g -> ([BrGen], g)
_randomBraidWord :: forall g. RandomGen g => Int -> Int -> g -> ([BrGen], g)
_randomBraidWord Int
n Int
len = forall g a. Rand g a -> g -> (a, g)
runRand forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
len forall a b. (a -> b) -> a -> b
$ do
  Int
k <- forall g a. (RandomGen g, Random a) => (a, a) -> Rand g a
randChoose (Int
1,Int
nforall a. Num a => a -> a -> a
-Int
1)
  Sign
s <- forall g a. (RandomGen g, Random a) => Rand g a
randRoll
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Sign
s of
    Sign
Plus  -> Int -> BrGen
Sigma Int
k
    Sign
Minus -> Int -> BrGen
SigmaInv Int
k

-- | Untyped version of 'randomPositiveBraidWord'
_randomPositiveBraidWord 
  :: (RandomGen g) 
  => Int             -- ^ number of strands
  -> Int             -- ^ length of the random word
  -> g -> ([BrGen], g)
_randomPositiveBraidWord :: forall g. RandomGen g => Int -> Int -> g -> ([BrGen], g)
_randomPositiveBraidWord Int
n Int
len = forall g a. Rand g a -> g -> (a, g)
runRand forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
len forall a b. (a -> b) -> a -> b
$ do
  forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Int -> BrGen
Sigma forall a b. (a -> b) -> a -> b
$ forall g a. (RandomGen g, Random a) => (a, a) -> Rand g a
randChoose (Int
1,Int
nforall a. Num a => a -> a -> a
-Int
1)

--------------------------------------------------------------------------------