Skip to content

Commit

Permalink
Merge pull request #66 from wismill/wip/hlint
Browse files Browse the repository at this point in the history
hlint
  • Loading branch information
robstewart57 authored May 23, 2019
2 parents 4fd2edf + fbf8431 commit 896e21e
Show file tree
Hide file tree
Showing 30 changed files with 237 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# - group: {name: dollar, enabled: true}
#
# Generalise map to fmap, ++ to <>
# - group: {name: generalise, enabled: true}
- group: {name: generalise, enabled: true}


# Ignore some builtin hints
Expand Down
43 changes: 22 additions & 21 deletions bench/MainCriterion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Main where

import Prelude hiding (readFile)
import Data.Semigroup ((<>))
import Criterion
import Criterion.Types
import Criterion.Main
Expand Down Expand Up @@ -85,20 +86,20 @@ main = defaultMainWith
,
bgroup
"query"
(queryBench "TList" triplesList ++
(queryBench "TList" triplesList <>
queryBench "AdjHashMap" adjMap
-- queryBench "SP" mapSP ++ queryBench "HashSP" hashMapSP
-- queryBench "SP" mapSP <> queryBench "HashSP" hashMapSP
)
, bgroup
"select"
(selectBench "TList" triplesList ++
(selectBench "TList" triplesList <>
selectBench "AdjHashMap" adjMap
-- selectBench "SP" mapSP ++ selectBench "HashSP" hashMapSP
-- selectBench "SP" mapSP <> selectBench "HashSP" hashMapSP
)
, bgroup
"add-remove-triples"
(addRemoveTriples "TList" triples (empty :: RDF TList) triplesList
++ addRemoveTriples "AdjHashMap" triples (empty :: RDF AdjHashMap) adjMap
<> addRemoveTriples "AdjHashMap" triples (empty :: RDF AdjHashMap) adjMap
)
, bgroup
"count_triples"
Expand All @@ -110,13 +111,13 @@ main = defaultMainWith

selectBench :: Rdf a => String -> RDF a -> [Benchmark]
selectBench label gr =
[ bench (label ++ " SPO") $ nf selectGr (subjSelect,predSelect,objSelect,gr)
, bench (label ++ " SP") $ nf selectGr (subjSelect,predSelect,selectNothing,gr)
, bench (label ++ " S") $ nf selectGr (subjSelect,selectNothing,selectNothing,gr)
, bench (label ++ " PO") $ nf selectGr (selectNothing,predSelect,objSelect,gr)
, bench (label ++ " SO") $ nf selectGr (subjSelect,selectNothing,objSelect,gr)
, bench (label ++ " P") $ nf selectGr (selectNothing,predSelect,selectNothing,gr)
, bench (label ++ " O") $ nf selectGr (selectNothing,selectNothing,objSelect,gr)
[ bench (label <> " SPO") $ nf selectGr (subjSelect,predSelect,objSelect,gr)
, bench (label <> " SP") $ nf selectGr (subjSelect,predSelect,selectNothing,gr)
, bench (label <> " S") $ nf selectGr (subjSelect,selectNothing,selectNothing,gr)
, bench (label <> " PO") $ nf selectGr (selectNothing,predSelect,objSelect,gr)
, bench (label <> " SO") $ nf selectGr (subjSelect,selectNothing,objSelect,gr)
, bench (label <> " P") $ nf selectGr (selectNothing,predSelect,selectNothing,gr)
, bench (label <> " O") $ nf selectGr (selectNothing,selectNothing,objSelect,gr)
]

subjSelect, predSelect, objSelect, selectNothing :: Maybe (Node -> Bool)
Expand All @@ -133,19 +134,19 @@ queryNothing = Nothing

queryBench :: Rdf a => String -> RDF a -> [Benchmark]
queryBench label gr =
[ bench (label ++ " SPO") $ nf queryGr (subjQuery,predQuery,objQuery,gr)
, bench (label ++ " SP") $ nf queryGr (subjQuery,predQuery,queryNothing,gr)
, bench (label ++ " S") $ nf queryGr (subjQuery,queryNothing,queryNothing,gr)
, bench (label ++ " PO") $ nf queryGr (queryNothing,predQuery,objQuery,gr)
, bench (label ++ " SO") $ nf queryGr (subjQuery,queryNothing,objQuery,gr)
, bench (label ++ " P") $ nf queryGr (queryNothing,predQuery,queryNothing,gr)
, bench (label ++ " O") $ nf queryGr (queryNothing,queryNothing,objQuery,gr)
[ bench (label <> " SPO") $ nf queryGr (subjQuery,predQuery,objQuery,gr)
, bench (label <> " SP") $ nf queryGr (subjQuery,predQuery,queryNothing,gr)
, bench (label <> " S") $ nf queryGr (subjQuery,queryNothing,queryNothing,gr)
, bench (label <> " PO") $ nf queryGr (queryNothing,predQuery,objQuery,gr)
, bench (label <> " SO") $ nf queryGr (subjQuery,queryNothing,objQuery,gr)
, bench (label <> " P") $ nf queryGr (queryNothing,predQuery,queryNothing,gr)
, bench (label <> " O") $ nf queryGr (queryNothing,queryNothing,objQuery,gr)
]

addRemoveTriples :: (NFData a,NFData (RDF a), Rdf a) => String -> Triples -> RDF a -> RDF a -> [Benchmark]
addRemoveTriples lbl triples emptyGr populatedGr =
[ bench (lbl ++ "-add-triples") $ nf addTriples (triples,emptyGr)
, bench (lbl ++ "-remove-triples") $ nf removeTriples (triples,populatedGr)
[ bench (lbl <> "-add-triples") $ nf addTriples (triples,emptyGr)
, bench (lbl <> "-remove-triples") $ nf removeTriples (triples,populatedGr)
]

addTriples :: Rdf a => (Triples,RDF a) -> RDF a
Expand Down
6 changes: 3 additions & 3 deletions examples/ESWC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ heldByProp = "swc:heldBy"
eswcCommitteeMembers :: RDF TList -> [T.Text]
eswcCommitteeMembers graph =
let triples = query graph (Just (unode eswcCommitteeURI)) (Just (unode heldByProp)) Nothing
memberURIs = map objectOf triples
in map
memberURIs = fmap objectOf triples
in fmap
(\memberURI ->
let (LNode (PlainL (firstName::T.Text))) =
objectOf $ head $ query graph (Just memberURI) (Just (unode "foaf:firstName")) Nothing
(LNode (PlainL lastName)) =
objectOf $ head $ query graph (Just memberURI) (Just (unode "foaf:lastName")) Nothing
in (T.append firstName (T.append (T.pack " ") lastName)))
memberURIs

main :: IO ()
main = do
result <- parseURL
Expand Down
2 changes: 1 addition & 1 deletion examples/ParseURLs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ timBernersLee :: IO ()
timBernersLee = do
Right (rdf::RDF TList) <- parseURL (XmlParser Nothing Nothing) "http://www.w3.org/People/Berners-Lee/card.rdf"
let ts = query rdf (Just (UNode "http://www.w3.org/2011/Talks/0331-hyderabad-tbl/data#talk")) (Just (UNode "dct:title")) Nothing
let talks = map (\(Triple _ _ (LNode (PlainL s))) -> s) ts
let talks = fmap (\(Triple _ _ (LNode (PlainL s))) -> s) ts
print talks

main :: IO ()
Expand Down
2 changes: 2 additions & 0 deletions rdf4h.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ test-suite test-rdf4h

if impl(ghc < 7.6)
build-depends: ghc-prim
if !impl(ghc >= 8.0)
build-depends: semigroups == 0.18.*

other-modules: W3C.TurtleTest
hs-source-dirs: testsuite/tests
Expand Down
8 changes: 4 additions & 4 deletions src/Data/RDF/Graph/AdjHashMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TupleSections, GeneralizedNewtypeDeriving #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE ScopedTypeVariables #-}

Expand All @@ -13,6 +12,7 @@
module Data.RDF.Graph.AdjHashMap (AdjHashMap) where

import Prelude hiding (pred)
import Data.Semigroup ((<>))
import Data.List
import Data.Binary (Binary)
import Data.RDF.Types
Expand Down Expand Up @@ -103,16 +103,16 @@ instance Rdf AdjHashMap where
-- show (AdjHashMap ((spoMap, _), _, _)) =
-- let ts = concatMap (uncurry tripsSubj) subjPredMaps
-- where subjPredMaps = HashMap.toList spoMap
-- in concatMap (\t -> show t ++ "\n") ts
-- in concatMap (\t -> show t <> "\n") ts

showGraph' :: RDF AdjHashMap -> String
showGraph' ((AdjHashMap ((spoMap, _), _, _))) =
let ts = concatMap (uncurry tripsSubj) subjPredMaps
where subjPredMaps = HashMap.toList spoMap
in concatMap (\t -> show t ++ "\n") ts
in concatMap (\t -> show t <> "\n") ts

-- instance Show (RDF AdjHashMap) where
-- show gr = concatMap (\t -> show t ++ "\n") (triplesOf gr)
-- show gr = concatMap (\t -> show t <> "\n") (triplesOf gr)

-- some convenience type alias for readability

Expand Down
44 changes: 22 additions & 22 deletions src/Data/RDF/Graph/HashMapSP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TupleSections, GeneralizedNewtypeDeriving #-}
{-# LANGUAGE EmptyDataDecls #-}

-- |A graph implementation mapping (S,P) pairs to O, backed by 'Data.Map'.

module Data.RDF.Graph.HashMapSP (HashSP) where

import Prelude hiding (pred)
import Data.Semigroup ((<>))
import Control.DeepSeq (NFData)
import Data.RDF.Types
import Data.RDF.Query
Expand Down Expand Up @@ -47,15 +47,15 @@ instance Rdf HashSP where
-- instance Show (HashSP) where
-- show (HashSP (tsMap,_,_)) =
-- let ts = (concatMap (\((s,p),oList) -> map (Triple s p) oList) . HashMap.toList) tsMap
-- in concatMap (\t -> show t ++ "\n") ts
-- in concatMap (\t -> show t <> "\n") ts

showGraph' :: RDF HashSP -> String
showGraph' (HashSP (tsMap,_,_)) =
let ts = (concatMap (\((s,p),oList) -> map (Triple s p) oList) . HashMap.toList) tsMap
in concatMap (\t -> show t ++ "\n") ts
let ts = (concatMap (\((s,p),oList) -> fmap (Triple s p) oList) . HashMap.toList) tsMap
in concatMap (\t -> show t <> "\n") ts

-- instance Show (HashSP) where
-- show gr = concatMap (\t -> show t ++ "\n") (triplesOf gr)
-- show gr = concatMap (\t -> show t <> "\n") (triplesOf gr)

type SPMap = HashMap (Subject,Predicate) [Object]

Expand All @@ -77,10 +77,10 @@ mkRdf' :: Triples -> Maybe BaseUrl -> PrefixMappings -> RDF HashSP
mkRdf' triples baseURL pms = HashSP (tsMap, baseURL, pms)
where
tsMap = sortAndGroup triples
sortAndGroup xs = HashMap.fromListWith (++) [((s,p), [o]) | Triple s p o <- xs]
sortAndGroup xs = HashMap.fromListWith (<>) [((s,p), [o]) | Triple s p o <- xs]

triplesOf' :: RDF HashSP -> Triples
triplesOf' (HashSP (tsMap,_,_)) = (concatMap (\((s,p),oList) -> map (Triple s p) oList) . HashMap.toList) tsMap
triplesOf' (HashSP (tsMap,_,_)) = (concatMap (\((s,p),oList) -> fmap (Triple s p) oList) . HashMap.toList) tsMap

uniqTriplesOf' :: RDF HashSP -> Triples
uniqTriplesOf' = nub . expandTriples
Expand All @@ -93,47 +93,47 @@ select' (HashSP (tsMap,_,_)) Nothing (Just pSelector) Nothing =
HashMap.foldrWithKey findTripleWithP [] tsMap
where
findTripleWithP (s,p) oList ts = if pSelector p
then map (Triple s p) oList ++ ts
then fmap (Triple s p) oList <> ts
else ts

select' (HashSP (tsMap,_,_)) Nothing Nothing (Just oSelector) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = map (Triple s p) (filter oSelector oList) ++ ts
findTripleWithS (s,p) oList ts = fmap (Triple s p) (filter oSelector oList) <> ts

select' (HashSP (tsMap,_,_)) Nothing (Just pSelector) (Just oSelector) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = if pSelector p
then map (Triple s p) (filter oSelector oList) ++ ts
then fmap (Triple s p) (filter oSelector oList) <> ts
else ts

select' (HashSP (tsMap,_,_)) (Just sSelector) Nothing Nothing =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = if sSelector s
then map (Triple s p) oList ++ ts
then fmap (Triple s p) oList <> ts
else ts

select' (HashSP (tsMap,_,_)) (Just sSelector) (Just pSelector) Nothing =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = if sSelector s && pSelector p
then map (Triple s p) oList ++ ts
then fmap (Triple s p) oList <> ts
else ts

select' (HashSP (tsMap,_,_)) (Just sSelector) Nothing (Just oSelector) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = if sSelector s
then map (Triple s p) (filter oSelector oList) ++ ts
then fmap (Triple s p) (filter oSelector oList) <> ts
else ts

select' (HashSP (tsMap,_,_)) (Just sSelector) (Just pSelector) (Just oSelector) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = if sSelector s && pSelector p
then map (Triple s p) (filter oSelector oList) ++ ts
then fmap (Triple s p) (filter oSelector oList) <> ts
else ts

query' :: RDF HashSP -> Maybe Subject -> Maybe Predicate -> Maybe Object -> Triples
Expand All @@ -144,42 +144,42 @@ query' (HashSP (tsMap,_,_)) Nothing (Just p) Nothing =
HashMap.foldrWithKey findTripleWithP [] tsMap
where
findTripleWithP (s,p') oList ts = if p == p'
then map (Triple s p) oList ++ ts
then fmap (Triple s p) oList <> ts
else ts

query' (HashSP (tsMap,_,_)) Nothing Nothing (Just o) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p) oList ts = map (Triple s p) (filter (== o) oList) ++ ts
findTripleWithS (s,p) oList ts = fmap (Triple s p) (filter (== o) oList) <> ts

query' (HashSP (tsMap,_,_)) Nothing (Just p) (Just o) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s,p') oList ts = if p == p'
then map (Triple s p) (filter (== o) oList) ++ ts
then fmap (Triple s p) (filter (== o) oList) <> ts
else ts

query' (HashSP (tsMap,_,_)) (Just s) Nothing Nothing =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s',p) oList ts = if s == s'
then map (Triple s p) oList ++ ts
then fmap (Triple s p) oList <> ts
else ts

-- optimal pattern for this RDF HashSP instance
query' (HashSP (tsMap,_,_)) (Just s) (Just p) Nothing =
(map (Triple s p) . HashMap.lookupDefault [] (s,p)) tsMap
(fmap (Triple s p) . HashMap.lookupDefault [] (s,p)) tsMap

query' (HashSP (tsMap,_,_)) (Just s) Nothing (Just o) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s',p) oList ts = if s == s'
then map (Triple s p) (filter (== o) oList) ++ ts
then fmap (Triple s p) (filter (== o) oList) <> ts
else ts

query' (HashSP (tsMap,_,_)) (Just s) (Just p) (Just o) =
HashMap.foldrWithKey findTripleWithS [] tsMap
where
findTripleWithS (s',p') oList ts = if s == s' && p == p'
then map (Triple s p) (filter (== o) oList) ++ ts
then fmap (Triple s p) (filter (== o) oList) <> ts
else ts
Loading

0 comments on commit 896e21e

Please sign in to comment.