{-# 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
data BrGen
= Sigma !Int
| SigmaInv !Int
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)
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)
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
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)
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
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
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
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
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"
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 :: 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 :: 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"
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
_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] ]
theGarsideBraid :: KnownNat n => Braid n
theGarsideBraid :: forall (n :: Nat). KnownNat n => Braid n
theGarsideBraid = forall (n :: Nat). KnownNat n => Braid n
halfTwist
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)
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
identity :: Braid n
identity :: forall (n :: Nat). Braid n
identity = forall (n :: Nat). [BrGen] -> Braid n
Braid []
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
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
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)
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
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)
_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
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
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
_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
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
_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'
_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
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
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
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
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
_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
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
bronfmanH :: Int -> [Int]
bronfmanH :: Int -> [Int]
bronfmanH Int
n = [[Int]]
bronfmanHsList forall a. [a] -> Int -> a
!! Int
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
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..]
instance KnownNat n => DrawASCII (Braid n) where
ascii :: Braid n -> ASCII
ascii = forall (n :: Nat). KnownNat n => Braid n -> ASCII
horizBraidASCII
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
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)
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
" " ]
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
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
_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) ]
_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] ]
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
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
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
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)
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)
| 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 )
| 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 ]
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 ]
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)
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'
withRandomBraidWord
:: RandomGen g
=> (forall n. KnownNat n => Braid n -> a)
-> Int
-> Int
-> 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
withRandomPositiveBraidWord
:: RandomGen g
=> (forall n. KnownNat n => Braid n -> a)
-> Int
-> Int
-> 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
_randomBraidWord
:: (RandomGen g)
=> Int
-> Int
-> 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
_randomPositiveBraidWord
:: (RandomGen g)
=> Int
-> Int
-> 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)