Skip to content

Commit

Permalink
Refactor, and major restructuring of package layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
akc committed Jul 10, 2014
1 parent d40ea93 commit 7a09e8a
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 187 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ cabal-dev
*.chi
*.chs.h
tests/Properties
tests/Test
tests/Test.*
TODO.txt
18 changes: 0 additions & 18 deletions Math/Perm.hs

This file was deleted.

12 changes: 6 additions & 6 deletions Math/Sym.hs → Sym.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- Maintainer : Anders Claesson <[email protected]>
--

module Math.Sym
module Sym
(
Permutation(..)
, perms
Expand All @@ -14,12 +14,12 @@ module Math.Sym
) where

import Data.Ord
import Data.SSYT (SSYTPair (..))
import qualified Data.SSYT as Y
import Sym.Perm.SSYT (SSYTPair (..))
import qualified Sym.Perm.SSYT as Y
import Data.List
import Math.Perm (Perm)
import qualified Math.Perm as P
import qualified Math.Perm.D8 as D8
import Sym.Perm.Meta (Perm)
import qualified Sym.Perm.Meta as P
import qualified Sym.Perm.D8 as D8


-- The permutation typeclass
Expand Down
10 changes: 5 additions & 5 deletions Data/CLongArray.hs → Sym/Internal/CLongArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
--
-- Convenience functions for dealing with arrays of 'CLong's.

module Data.CLongArray
module Sym.Internal.CLongArray
(
-- * Data type
CLongArray
Expand All @@ -32,7 +32,7 @@ module Data.CLongArray
) where

import Data.Ord
import Data.Size
import Sym.Internal.Size
import Foreign
import Foreign.C.Types
import GHC.Base
Expand Down Expand Up @@ -89,8 +89,8 @@ toList w = map fromIntegral . inlinePerformIO . unsafeWith w $ peekArray (size w
-- size of the array.
slice :: [Int] -> CLongArray -> [CLongArray]
slice ks w
| any (<=0) ks = error "Data.CLongArray.slice: zero or negative parts"
| sum ks /= size w = error "Data.CLongArray.slice: parts doesn't sum to size of array"
| any (<=0) ks = error "Sym.Internal.CLongArray.slice: zero or negative parts"
| sum ks /= size w = error "Sym.Internal.CLongArray.slice: parts doesn't sum to size of array"
| otherwise = unsafeSlice ks w

-- | Like 'slice' but without range checking.
Expand All @@ -112,7 +112,7 @@ at :: CLongArray -> Int -> Int
at w i =
let n = size w
in if i < 0 || i >= n
then error $ "Data.CLongArray.at: " ++ show i ++ " not in [0.." ++ show (n-1) ++ "]"
then error $ "Sym.Internal.CLongArray.at: " ++ show i ++ " not in [0.." ++ show (n-1) ++ "]"
else unsafeAt w i
{-# INLINE at #-}

Expand Down
2 changes: 1 addition & 1 deletion Data/Size.hs → Sym/Internal/Size.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Data.Size (Size (..)) where
module Sym.Internal.Size (Size (..)) where

import qualified Data.Set as Set

Expand Down
70 changes: 13 additions & 57 deletions Data/Perm/Internal.hs → Sym/Internal/SubSeq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,21 @@
-- Maintainer : Anders Claesson <[email protected]>
--

module Data.Perm.Internal
module Sym.Internal.SubSeq
(
Set
, normalize
, subsets
, minima
, maxima
, powerset
, kSubsets
module Sym.Internal.CLongArray
, SubSeq
, choose
) where

import Data.List
import Data.Ord
import qualified Data.Set as S
import Data.CLongArray
import Sym.Internal.CLongArray
import Foreign
import Foreign.C.Types
import System.IO.Unsafe


-- | A set is represented by an increasing array of non-negative
-- | A SubSeq is represented by an increasing array of non-negative
-- integers.
type Set = CLongArray


-- Utils
-- -----

-- | Sort and remove duplicates.
normalize :: Ord a => [a] -> [a]
normalize = map head . group . sort

-- | The set of minimal elements with respect to inclusion.
minima :: Ord a => [S.Set a] -> [S.Set a]
minima = minima' . sortBy (comparing S.size)
where
minima' [] = []
minima' (x:xs) = x : minima' [ y | y<-xs, not (S.isSubsetOf x y) ]

-- | The set of maximal elements with respect to the given order.
maxima :: Ord a => [S.Set a] -> [S.Set a]
maxima = maxima' . sortBy (comparing $ \x -> -S.size x)
where
maxima' [] = []
maxima' (x:xs) = x : maxima' [ y | y<-xs, not (S.isSubsetOf y x) ]

kSubsets :: Ord a => Int -> S.Set a -> [S.Set a]
kSubsets 0 _ = [ S.empty ]
kSubsets k s = if S.null s
then []
else let (x, t) = S.deleteFindMin s
in kSubsets k t ++ map (S.insert x) (kSubsets (k-1) t)

powerset :: Ord a => S.Set a -> [S.Set a]
powerset s = if S.null s
then [s]
else let (x, t) = S.deleteFindMin s
ts = powerset t
in ts ++ map (S.insert x) ts
type SubSeq = CLongArray

-- Bitmasks
-- --------
Expand All @@ -73,12 +29,12 @@ class (Bits a, Integral a) => Bitmask a where
-- | Lexicographically, the next bitmask with the same Hamming weight.
next :: a -> a

-- | @ones k m@ is the set of indices whose bits are set in
-- @m@. Default implementation:
-- | @ones k m@ is the set / subsequence of indices whose bits are
-- set in @m@. Default implementation:
--
-- > ones m = fromListN (popCount m) $ filter (testBit m) [0..]
--
ones :: a -> CLongArray
ones :: a -> SubSeq
ones m = fromList . take (popCount m) $ filter (testBit m) [0..]

instance Bitmask CLong where
Expand All @@ -97,10 +53,10 @@ bitmasks n k = take binomial (iterate next ((1 `shiftL` k) - 1))
k' = toInteger k
binomial = fromIntegral $ product [n', n'-1 .. n'-k'+1] `div` product [1..k']

-- | @subsets n k@ is the list of subsets of @[0..n-1]@ with @k@
-- | @n \`choose\` k@ is the list of subsequences of @[0..n-1]@ with @k@
-- elements.
subsets :: Int -> Int -> [Set]
subsets n k
choose :: Int -> Int -> [SubSeq]
choose n k
| n <= 32 = map ones (bitmasks n k :: [CLong])
| otherwise = map ones (bitmasks n k :: [Integer])

Expand Down
53 changes: 53 additions & 0 deletions Sym/Internal/Util.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-- |
-- Copyright : Anders Claesson 2014
-- Maintainer : Anders Claesson <[email protected]>
--

module Sym.Internal.Util
(
minima
, maxima
, kSubsets
, powerset
, nubSort
) where

import Data.List
import Data.Ord
import Data.Set (Set)
import qualified Data.Set as S

-- | The set of minimal elements with respect to inclusion.
minima :: Ord a => [Set a] -> [Set a]
minima = minima' . sortBy (comparing S.size)
where
minima' [] = []
minima' (x:xs) = x : minima' [ y | y<-xs, not (x `S.isSubsetOf` y) ]

-- | The set of maximal elements with respect to the given order.
maxima :: Ord a => [Set a] -> [Set a]
maxima = maxima' . sortBy (comparing $ \x -> -S.size x)
where
maxima' [] = []
maxima' (x:xs) = x : maxima' [ y | y<-xs, not (y `S.isSubsetOf` x) ]

-- | A list of all k element subsets of the given set.
kSubsets :: Ord a => Int -> Set a -> [Set a]
kSubsets 0 _ = [ S.empty ]
kSubsets k s
| S.null s = []
| otherwise = kSubsets k t ++ map (S.insert x) (kSubsets (k-1) t)
where
(x,t) = S.deleteFindMin s

-- | A list of all subsets of the given set.
powerset :: Ord a => Set a -> [Set a]
powerset s
| S.null s = [s]
| otherwise = ts ++ map (S.insert x) ts
where
(x,t) = S.deleteFindMin s; ts = powerset t

-- | Sort and remove duplicates.
nubSort :: Ord a => [a] -> [a]
nubSort = map head . group . sort
6 changes: 3 additions & 3 deletions Data/Perm.hs → Sym/Perm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
--
-- Generating permutations: rank and unrank

module Data.Perm
module Sym.Perm
(
module Data.CLongArray
module Sym.Internal.CLongArray
, Perm
, emptyperm
, one
Expand All @@ -21,7 +21,7 @@ module Data.Perm
) where

import Data.List
import Data.CLongArray
import Sym.Internal.CLongArray
import Foreign
import Foreign.C.Types
import System.IO.Unsafe
Expand Down
4 changes: 2 additions & 2 deletions Math/Perm/Bijection.hs → Sym/Perm/Bijection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
-- Maintainer : Anders Claesson <[email protected]>
--

module Math.Perm.Bijection
module Sym.Perm.Bijection
(
simionSchmidt
, simionSchmidt'
) where

import Data.Perm
import Sym.Perm
import Foreign
import Foreign.C.Types
import System.IO.Unsafe
Expand Down
16 changes: 8 additions & 8 deletions Math/Perm/Class.hs → Sym/Perm/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Maintainer : Anders Claesson <[email protected]>
--

module Math.Perm.Class
module Sym.Perm.Class
(
inc
, dec
Expand Down Expand Up @@ -32,12 +32,12 @@ module Math.Perm.Class
, fibonacci
) where

import Data.Perm
import Math.Perm.Bijection
import Math.Perm.Constructions
import Data.Perm.Internal
import Math.Perm.Pattern
import qualified Math.Perm.D8 as D8
import Sym.Internal.Util
import Sym.Perm
import Sym.Perm.Bijection
import Sym.Perm.Constructions
import Sym.Perm.Pattern
import qualified Sym.Perm.D8 as D8

-- | The class of increasing permutations.
inc :: Int -> [Perm]
Expand Down Expand Up @@ -137,7 +137,7 @@ lt :: Int -> [Perm]
lt = map D8.reverse . gt

union :: [Int -> [Perm]] -> Int -> [Perm]
union cs n = normalize $ concat [ c n | c <- cs ]
union cs n = nubSort $ concat [ c n | c <- cs ]

-- | The union of 'vee', 'caret', 'gt' and 'lt'.
wedges :: Int -> [Perm]
Expand Down
6 changes: 3 additions & 3 deletions Math/Perm/Component.hs → Sym/Perm/Component.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- Components of permutations.
--

module Math.Perm.Component
module Sym.Perm.Component
(
components
, skewComponents
Expand All @@ -17,8 +17,8 @@ module Math.Perm.Component

import Foreign
import System.IO.Unsafe
import Data.Perm
import qualified Math.Perm.D8 as D8
import Sym.Perm
import qualified Sym.Perm.D8 as D8

-- Positions /i/ such that /max{ w[j] : j <= i } = i/. These positions
-- mark the boundaries of components.
Expand Down
8 changes: 4 additions & 4 deletions Math/Perm/Constructions.hs → Sym/Perm/Constructions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- Sum, skew sum, etc
--

module Math.Perm.Constructions
module Sym.Perm.Constructions
(
(/+/)
, (\-\)
Expand All @@ -17,9 +17,9 @@ module Math.Perm.Constructions
import Foreign
import System.IO.Unsafe
import Control.Monad
import Data.Perm
import qualified Data.Permgram as G
import qualified Math.Perm.D8 as D8
import Sym.Perm
import qualified Sym.Permgram as G
import qualified Sym.Perm.D8 as D8

infixl 6 /+/
infixl 6 \-\
Expand Down
Loading

0 comments on commit 7a09e8a

Please sign in to comment.