Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #2, and small changes #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Data/Packed/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ vecExp s = case listExp s of
buildVectorST es =
[| runSTVector (do
v <- newUndefinedVector $( lift (length es) )
$( let buildWrites _i [] = [| return () |]
$( let buildWrites :: Int -> [Exp] -> ExpQ
buildWrites _i [] = [| return () |]
buildWrites i (exp:exps) = [| unsafeWriteVector v i $(return exp) >> $(buildWrites (i+1) exps) |]
in buildWrites 0 es)
return v) |]
Expand Down
10 changes: 8 additions & 2 deletions Data/Packed/Syntax/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ unList (TH.ListE l) = l

-- | Parser for matrix expressions. Returns (outer length, inner length, matrix)
matListExp :: String -> Either String (Int, Int, [[TH.Exp]])
matListExp s = case breakOnSemis HSE.parseExp MT.parseExp s of
matListExp s = case breakOnSemis HSE.parseExp MT.parseExp (expandTabs s) of
Right rows@(r:_) ->
let
rowLen = length (unList r)
Expand All @@ -78,7 +78,7 @@ unPList (TH.ListP l) = l

-- | Parser for matrix patterns. Returns (outer length, inner length, matrix)
matListPat :: String -> Either String (Int, Int, [[TH.Pat]])
matListPat s = case breakOnSemis HSE.parsePat MT.parsePat s of
matListPat s = case breakOnSemis HSE.parsePat MT.parsePat (expandTabs s) of
Right rows@(r:_) ->
let
rowLen = length (unPList r)
Expand All @@ -88,3 +88,9 @@ matListPat s = case breakOnSemis HSE.parsePat MT.parsePat s of
then return (rowLen, colLen, map unPList rows)
else fail "Not all rows have the same length"
Left msg -> fail msg


expandTabs :: String -> String
expandTabs ('\t' : xs) = " " ++ expandTabs xs
expandTabs (x : xs) = x : expandTabs xs
expandTabs [] = []
10 changes: 7 additions & 3 deletions hmatrix-syntax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ Library
Data.Packed.Syntax.Internal
Build-depends:
base < 5,
haskell-src-exts >= 1.8 && < 1.15,
haskell-src-exts >= 1.8 && < 1.16,
haskell-src-meta >= 0.5.1 && < 0.7,
hmatrix >= 0.13.1 && < 0.16,
template-haskell >= 2.4 && < 2.9
hmatrix >= 0.13.1 && < 0.17,
template-haskell >= 2.4 && < 3.0
-- Other-modules:
-- Build-tools:
Test-Suite hspec
Main-Is: hspec.hs
Build-Depends: base, hspec, QuickCheck >= 2
Type: exitcode-stdio-1.0
35 changes: 35 additions & 0 deletions hspec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import Control.Exception (evaluate)

import Data.Packed.Syntax.Internal
import Language.Haskell.TH
import Text.Printf
import Data.List

main = hspec $ do
describe "matListExp" $ do
it "tabs before semicolons" $
matListExp "a\t;b" `shouldBe`
Right (1,2,[[VarE (mkName "a")],
[VarE (mkName "b")]])
it "tabs after semicolons" $
matListExp "a;\tb;\tc" `shouldBe`
Right (1,3,[[VarE (mkName "a")],
[VarE (mkName "b")],
[VarE (mkName "c")]])
modifyMaxSize (\n -> n `div` 10) $
it "inverts a pretty-printer" $ property $ \(Positive nrow) (Positive ncol) ->
matListExp (ppMatListExp nrow ncol) `shouldBe`
Right(ncol,nrow,expectedMatList nrow ncol)

ppMatListExp :: Int -> Int -> String
ppMatListExp nrow ncol = intercalate ";" [ intercalate "," xs |
i <- [1 .. nrow],
let xs :: [String]
xs = map (printf "x%d_%d" i) [1 .. ncol] ]

expectedMatList :: Int -> Int -> [[Exp]]
expectedMatList nrow ncol = [ map (VarE . mkName . printf "x%d_%d" i) [1 .. ncol]
| i <- [1 .. nrow]]