sized-types 0.2.7.20101112 → 0.3.4.0
raw patch · 6 files changed
+82/−68 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Sized.Ix: seeIn2D :: Size ix => (Row ix, Column ix) -> ix
- Data.Sized.Matrix: instance (Show a, Size ix, Size (Row ix), Size (Column ix)) => Show (Matrix ix a)
- Data.Sized.Sparse.Matrix: instance (Show a, Size ix, Size (Row ix), Size (Column ix)) => Show (Matrix ix a)
+ Data.Sized.Matrix: instance (Show a, Size ix) => Show (Matrix ix a)
+ Data.Sized.Sparse.Matrix: instance (Show a, Size ix) => Show (Matrix ix a)
- Data.Sized.Arith: X0_ :: Int -> X0_ a
+ Data.Sized.Arith: X0_ :: Integer -> X0_ a
- Data.Sized.Arith: X1_ :: Int -> X1_ a
+ Data.Sized.Arith: X1_ :: Integer -> X1_ a
Files
- Data/Sized/Arith.hs +11/−8
- Data/Sized/Ix.hs +50/−37
- Data/Sized/Matrix.hs +4/−2
- Data/Sized/Sparse/Matrix.hs +12/−16
- sized-types.cabal +4/−4
- test/Example1.hs +1/−1
Data/Sized/Arith.hs view
@@ -18,26 +18,29 @@ data X0 = X0 deriving (Eq,Ord) -data X0_ a = X0_ Int -- times 2 plus 0-data X1_ a = X1_ Int -- times 2 plus 1+data X0_ a = X0_ Integer -- times 2 plus 0+data X1_ a = X1_ Integer -- times 2 plus 1 type family ADD a b type instance ADD N1 N1 = APP0 N1-type instance ADD N1 X0 = N1+--type instance ADD N1 X0 = N1 type instance ADD N1 (X0_ b) = APP1 (ADD N1 b) type instance ADD N1 (X1_ b) = APP0 b type instance ADD X0 N1 = N1 -- MIR-type instance ADD X0 X0 = X0-type instance ADD X0 (X0_ b) = X0_ b-type instance ADD X0 (X1_ b) = APP1 b+--type instance ADD X0 X0 = X0+--type instance ADD X0 (X0_ b) = X0_ b+--type instance ADD X0 (X1_ b) = APP1 b type instance ADD (X0_ a) N1 = APP1 (ADD a N1) -- MIR-type instance ADD (X0_ a) X0 = APP0 a -- MIR+--type instance ADD (X0_ a) X0 = APP0 a -- MIR type instance ADD (X0_ a) (X0_ b) = APP0 (ADD a b) type instance ADD (X0_ a) (X1_ b) = APP1 (ADD a b) type instance ADD (X1_ a) N1 = APP0 a -- MIR-type instance ADD (X1_ a) X0 = APP1 a -- MIR+--type instance ADD (X1_ a) X0 = APP1 a -- MIR type instance ADD (X1_ a) (X0_ b) = APP1 (ADD a b) -- MIR type instance ADD (X1_ a) (X1_ b) = APP0 (SUCC (ADD a b))++type instance ADD a X0 = a+type instance ADD X0 a = a type family NOT a
Data/Sized/Ix.hs view
@@ -269,8 +269,8 @@ , Size(..) , all , Index- , Row- , Column+-- , Row+-- , Column , coerceSize , ADD , SUB@@ -288,8 +288,8 @@ --- because of TH's lack of type families, will be added later. type family Index a-type family Row a-type family Column a+--type family Row a+--type family Column a class (Eq ix, Ord ix, Show ix, Ix ix, Bounded ix) => Size ix where -- | return the size (number of possible elements) in type 'ix'.@@ -298,21 +298,16 @@ addIndex :: ix -> Index ix -> ix -- | look at an 'ix' as an 'Index', typically just an 'Int'. toIndex :: ix -> Index ix- -- | project any 2D array position onto any array. Helper method for 'show'.- seeIn2D :: (Row ix, Column ix) -> ix - -- TO CONSIDER: ADDing a zero method? This will allow coerseSize to - -- work in 2D, and drop the Enum requrement.- type instance Index (a,b) = (Index a,Index b)-type instance Row (a,b) = a-type instance Column (a,b) = b+--type instance Row (a,b) = a+--type instance Column (a,b) = b instance (Size x, Size y) => Size (x,y) where size ~(a,b) = size a * size b addIndex (a,b) (a',b') = (addIndex a a',addIndex b b') toIndex (a,b) = (toIndex a, toIndex b)- seeIn2D (x,y) = (x,y)+-- seeIn2D (x,y) = (x,y) type instance Index (a,b,c) = (Index a,Index b,Index c) -- type instance Row (a,b,c) = a@@ -322,7 +317,7 @@ size (a,b,c) = size a * size b * size c addIndex (a,b,c) (a',b',c') = (addIndex a a',addIndex b b',addIndex c c') toIndex (a,b,c) = (toIndex a, toIndex b,toIndex c)- seeIn2D (_a,_b) = error "Can not display 3D matrix in 2D"+-- seeIn2D (_a,_b) = error "Can not display 3D matrix in 2D" type instance Index (a,b,c,d) = (Index a,Index b,Index c,Index d) @@ -330,21 +325,21 @@ size (a,b,c,d) = size a * size b * size c * size d addIndex (a,b,c,d) (a',b',c',d') = (addIndex a a',addIndex b b',addIndex c c',addIndex d d') toIndex (a,b,c,d) = (toIndex a, toIndex b,toIndex c,toIndex d)- seeIn2D (_a,_b) = error "Can not display 4D matrix in 2D"+-- seeIn2D (_a,_b) = error "Can not display 4D matrix in 2D" -- | A good way of converting from one index type to another index type, typically in another base. coerceSize :: (Index ix1 ~ Index ix2, Size ix1, Size ix2, Num ix2) => ix1 -> ix2 coerceSize ix = addIndex 0 (toIndex ix) type instance Index X0 = Int-type instance Row X0 = X1-type instance Column X0 = X0+--type instance Row X0 = X1+--type instance Column X0 = X0 instance Size X0 where size _ = 0 addIndex X0 _n = X0 toIndex X0 = 0- seeIn2D (_,y) = y+-- seeIn2D (_,y) = y instance Integral X0 where toInteger a = toInteger (size a)@@ -354,41 +349,45 @@ instance Size a => Bounded (X1_ a) where minBound = X1_ 0- maxBound = let a = X1_ (size a - 1) in a+ maxBound = let a = X1_ (fromIntegral (size a) - 1) in a instance (Size a) => Real (X1_ a) where instance (Size a, Size (X1_ a), Integral a) => Integral (X1_ a) where + quotRem (X1_ a) (X1_ b) = (X1_ (a `quot` b),X1_ (a `rem` b))+ divMod (X1_ a) (X1_ b) = (X1_ (a `div` b),X1_ (a `mod` b)) toInteger (X1_ a) = toInteger a type instance Index (X1_ a) = Int-type instance Row (X1_ a) = X1-type instance Column (X1_ a) = X1_ a+--type instance Row (X1_ a) = X1+--type instance Column (X1_ a) = X1_ a instance Size a => Size (X1_ a) where size = const s where s = 2 * size (undefined :: a) + 1- addIndex (X1_ v) n = mkX1_ (v + n) -- fix bounds issues- toIndex (X1_ v) = v- seeIn2D (_,y) = y+ addIndex (X1_ v) n = mkX1_ (v + fromIntegral n) -- fix bounds issues+ toIndex (X1_ v) = fromIntegral v+-- seeIn2D (_,y) = y type instance Index (X0_ a) = Int-type instance Row (X0_ a) = X1-type instance Column (X0_ a) = X0_ a+--type instance Row (X0_ a) = X1+--type instance Column (X0_ a) = X0_ a instance Size a => Bounded (X0_ a) where minBound = X0_ 0- maxBound = let a = X0_ (size a - 1) in a+ maxBound = let a = X0_ (fromIntegral (size a) - 1) in a instance Size a => Size (X0_ a) where size = const s where s = 2 * size (undefined :: a) - addIndex (X0_ v) n = mkX0_ (v + n) -- fix bounds issues- toIndex (X0_ v) = v- seeIn2D (_,y) = y+ addIndex (X0_ v) n = mkX0_ (v + fromIntegral n) -- fix bounds issues+ toIndex (X0_ v) = fromIntegral v+-- seeIn2D (_,y) = y instance (Size a) => Real (X0_ a) where instance (Size a, Size (X0_ a), Integral a) => Integral (X0_ a) where - toInteger (X0_ a) = toInteger a+ quotRem (X0_ a) (X0_ b) = (X0_ (a `quot` b),X0_ (a `rem` b))+ divMod (X0_ a) (X0_ b) = (X0_ (a `div` b),X0_ (a `mod` b))+ toInteger (X0_ a) = a --- instances@@ -405,8 +404,8 @@ inRange (X0_ a,X0_ b) (X0_ i) = inRange (a,b) i instance (Size a) => Enum (X0_ a) where- toEnum n = mkX0_ n- fromEnum (X0_ n) = n+ toEnum n = mkX0_ (fromIntegral n)+ fromEnum (X0_ n) = fromIntegral n instance (Size a) => Num (X0_ a) where fromInteger n = mkX0_ (fromInteger n) -- bounds checking needed!@@ -427,13 +426,13 @@ (X1_ a) `compare` (X1_ b) = a `compare` b instance (Size a) => Ix (X1_ a) where- range (X1_ a,X1_ b) = map mkX1_ (range (a,b))+ range (X1_ a,X1_ b) = map (mkX1_ . fromIntegral) (range (a,b)) index (X1_ a,X1_ b) (X1_ i) = index (a,b) i inRange (X1_ a,X1_ b) (X1_ i) = inRange (a,b) i instance (Size a) => Enum (X1_ a) where- toEnum n = mkX1_ n- fromEnum (X1_ n) = n+ toEnum n = mkX1_ (fromIntegral n)+ fromEnum (X1_ n) = fromIntegral n instance (Size a) => Num (X1_ a) where fromInteger n = mkX1_ (fromInteger n) -- bounds checking needed!@@ -457,9 +456,23 @@ instance Show X0 where show X0 = "-" -mkX0_ n = let r = X0_ (n `mod` size r) in r-mkX1_ n = let r = X1_ (n `mod` size r) in r+mkX0_ :: forall a . Size a => Integer -> X0_ a+mkX0_ n | n < 0 = error $ "out of range: ((" ++ show n ++ ") :: X" ++ show s ++ ") < 0"+ | n >= s = error $ "out of range: (" ++ show n ++ " :: X" ++ show s ++ ") >= " ++ show s+ | otherwise = r+ where+ r :: X0_ a+ r = X0_ n+ s = fromIntegral (size r) +mkX1_ :: forall a . Size a => Integer -> X1_ a+mkX1_ n | n < 0 = error $ "out of range: ((" ++ show n ++ ") :: X" ++ show s ++ ") < 0"+ | n >= s = error $ "out of range: (" ++ show n ++ " :: X" ++ show s ++ ") >= " ++ show s+ | otherwise = r+ where+ r :: X1_ a+ r = X1_ n+ s = fromIntegral (size r) ------ type X1 = X1_ X0
Data/Sized/Matrix.hs view
@@ -76,9 +76,11 @@ -- | 'assocs' extracts the index/value pairs. assocs :: (Size i) => Matrix i a -> [(i,a)] assocs (Matrix a) = A.assocs a+assocs NullMatrix = [] (//) :: (Size i) => Matrix i e -> [(i, e)] -> Matrix i e (//) (Matrix arr) ixs = Matrix (arr A.// ixs)+(//) (NullMatrix) _ = NullMatrix accum :: (Size i) => (e -> a -> e) -> Matrix i e -> [(i, a)] -> Matrix i e accum f (Matrix arr) ixs = Matrix (A.accum f arr ixs)@@ -231,8 +233,8 @@ m_cols_size = fmap (maximum . map L.length . toList) m_cols -instance (Show a, Size ix,Size (Row ix), Size (Column ix)) => Show (Matrix ix a) where- show = showMatrix . fmap show . ixmap seeIn2D +instance (Show a, Size ix) => Show (Matrix ix a) where+ show = showMatrix . fmap show . unitRow -- | 'S' is shown as the contents, without the quotes. -- One use is a matrix of S, so that you can do show-style functions
Data/Sized/Sparse/Matrix.hs view
@@ -33,7 +33,7 @@ -- | '!' looks up an element in the sparse matrix. If the element is not found -- in the sparse matrix, '!' returns the default value. (!) :: (Ord ix) => Matrix ix a -> ix -> a-(!) (Matrix d sm) id = Map.findWithDefault d id sm +(!) (Matrix d sm) ix = Map.findWithDefault d ix sm fill :: (Size ix) => Matrix ix a -> M.Matrix ix a fill sm = M.forAll $ \ i -> sm ! i@@ -47,11 +47,6 @@ sparse :: (Size ix, Eq a) => a -> M.Matrix ix a -> Matrix ix a sparse d other = Matrix d (Map.fromList [ (i,v) | (i,v) <- M.assocs other, v /= d ]) -foldb1 f [x] = x-foldb1 f xs = foldb1 f (take len_before xs) `f` foldb1 f (drop len_before xs)- where len = length xs- len_before = len `div` 2- mm :: (Size m, Size n, Size m', Size n', n ~ m', Num a) => Matrix (m,n) a -> Matrix (m',n') a -> Matrix (m,n') a mm s1 s2 = Matrix 0 mp where@@ -62,15 +57,22 @@ , let v = foldb1 (+) [ s1 ! (x,k) * s2 ! (k,y) | k <- Set.toList s ] , v /= 0 ] - sm1@(Matrix _ mp1) = prune 0 s1- sm2@(Matrix _ mp2) = prune 0 s2+ (Matrix _ mp1) = prune 0 s1+ (Matrix _ mp2) = prune 0 s2 rs = rowSets (Map.keysSet mp1) cs = columnSets (Map.keysSet mp2) + foldb1 _ [x] = x+ foldb1 f xs = foldb1 f (take len_before xs) `f` foldb1 f (drop len_before xs)+ where len = length xs+ len_before = len `div` 2+++ rowSets :: (Size a, Ord b) => Set (a,b) -> M.Matrix a (Set b) rowSets set = M.accum f (pure Set.empty) (Set.toList set) where- f set e = Set.insert e set+ f set' e = Set.insert e set' columnSets :: (Size b, Ord a) => Set (a,b) -> M.Matrix b (Set a) columnSets = rowSets . Set.map (\ (a,b) -> (b,a))@@ -81,17 +83,11 @@ = Matrix (d1 d2) (Map.fromList [ (k,(sm1 ! k) (sm2 ! k)) | k <- Set.toList keys ]) where keys = Map.keysSet m1 `Set.union` Map.keysSet m2 -instance (Show a, Size ix,Size (Row ix), Size (Column ix)) => Show (Matrix ix a) where+instance (Show a, Size ix) => Show (Matrix ix a) where show m = show (fill m) transpose :: (Size x, Size y, Eq a) => Matrix (x,y) a -> Matrix (y,x) a transpose (Matrix d m) = Matrix d (Map.fromList [ ((y,x),a) | ((x,y),a) <- Map.assocs m ])--m1 = M.matrix [1..6] :: M.Matrix (X2,X3) Int-m2 = M.matrix [1..12] :: M.Matrix (X3,X4) Int-m3 = m1 `M.mm` m2-m4 = M.identity :: M.Matrix (X200,X200) Int- zipWith :: (Size x) => (a -> b -> c) -> Matrix x a -> Matrix x b -> Matrix x c zipWith f m1 m2 = pure f <*> m1 <*> m2
sized-types.cabal view
@@ -1,5 +1,5 @@ Name: sized-types-Version: 0.2.7.20101112+Version: 0.3.4.0 Synopsis: Sized types in Haskell. Description: Providing indices, matrixes, sparse matrixes, and signed and unsigned bit vectors. Category: Language@@ -8,14 +8,14 @@ Author: Andy Gill, Tristan Bull Maintainer: Andy Gill <andygill@ku.edu> Copyright: (c) 2009 The University of Kansas-Homepage: http://www.ittc.ku.edu/csdl/fpg/Tools/SizedTypes-Stability: alpha+Homepage: http://www.ittc.ku.edu/csdl/fpg/Tools+Stability: beta build-type: Simple Cabal-Version: >= 1.6 Flag all Description: Enable full development tree- Default: True+ Default: False Library Build-Depends: base >= 4 && < 5, containers, array
test/Example1.hs view
@@ -51,7 +51,7 @@ -- also works in 2D example5 :: Matrix X6 Bool-example5 = forAll $ \ i -> i > 6+example5 = forAll $ \ i -> i > 3 example6 :: Matrix (X3,X4) Int example6 = forEach example2 $ \ (i,j) a ->