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

Easily compose operations such as addTriple, removeTriple, etc. #81

Open
koslambrou opened this issue Mar 31, 2020 · 3 comments
Open

Easily compose operations such as addTriple, removeTriple, etc. #81

koslambrou opened this issue Mar 31, 2020 · 3 comments

Comments

@koslambrou
Copy link
Contributor

Question here.

In the rdf4h tutorial, adding triples to the graph is done the following way:

main :: IO ()
main = do
  -- empty list based RDF graph
  let myEmptyGraph = empty :: RDF TList

  triple1 = triple (unode "...") (unode "...") (unode "...")
  graph1 = addTriple myEmptyGraph triple1

  triple2 = triple (unode "...") (unode "...") (unode "...")
  graph2 = addTriple graph1 triple2

  graph3 = removeTriple graph2 triple1

  putStrLn (showGraph graph3)

Is it currently possible to simply combine those operations (addTriple, removeTriple) without creating temporary variables (graph1, graph2) ?

Something in monadic style would be nice. For example:

createGraph = do
  let triple1 = triple (unode "...") (unode "...") (unode "...")
  addTriple $ triple1
  addTriple $ triple (unode "...") (unode "...") (unode "...")
  removeTriple $ triple1
@koslambrou
Copy link
Contributor Author

Here's the idea (the following code does not compile):

type RdfState rdfImpl m a = StateT (RDF rdfImpl) m a

addTriple :: (RDF.Rdf rdfImpl, Monad m)
              => RDF.Triple
               -> RdfState rdfImpl m ()
addTriple triple = do
  graph <- get
  put $ RDF.addTriple graph triple

main :: IO ()
main = do
  let emptyRdf = empty :: RDF TList
  graph <- execStateT createRdf emptyRdf
  withFile "myfile.ttl" WriteMode (\h -> hWriteRdf (TurtleSerializer Nothing mappings) h graph)

createRdf :: (MonadIO m) => RdfState TList m ()
createRdf = do
    addTriple $ RDF.triple (RDF.unode $ RDF.mkUri dbp "Movie1")
                                       (RDF.unode $ RDF.mkUri RDF.rdf "type")
                                       (RDF.unode $ RDF.mkUri dbo "Movie")
    addTriple $ RDF.triple (RDF.unode $ RDF.mkUri dbp "Movie2")
                                       (RDF.unode $ RDF.mkUri RDF.rdf "type")
                                       (RDF.unode $ RDF.mkUri dbo "Movie")

Is that something that could be integrated in the library ?

@koslambrou
Copy link
Contributor Author

@robstewart57 Would you accept a PR with the RdfState data type in the module Data.RDF.State ? I've already implemented most of it in my projects.

@robstewart57
Copy link
Owner

@koslambrou Yes, I would accept a PR. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants