Giter VIP home page Giter VIP logo

Comments (5)

ewestern avatar ewestern commented on July 22, 2024

Hi there,
Thanks for pointing this out.
This haskell library is a wrapper around the geos library, and as such has to work with the memory management characteristics of the underlying library.
The reason the insert function looks as it does is because the underlying geos data structure is mutable and -- unlike other data structures in geos -- the semantics of the STRTree kind of require it to be mutable.
I have an idea for how to fix your issue -- give me a few days and I'll push something.

from geos.

ewestern avatar ewestern commented on July 22, 2024

On reflection,
I think the best move is to remove STRTreeBuilder from the interface (along with its associated functions).
Then, the only function for creating STRTree will be fromList and fromFoldable, which will completely avoid the mutability problem. I'm open to creating other convenient functions for constructing STRTrees. I was considering an unfoldr function, which would have the type signature:
unfoldr :: (b -> Maybe ((Geometry, a), b)) -> b -> STRTree a
Thoughts, @andreabedini ??

from geos.

andreabedini avatar andreabedini commented on July 22, 2024

Hi @ewestern,

I think the simplest approach is to own up to the mutability. How about the following types:

empty :: IO STRTreeBuilder
build :: STRTreeBuilder a -> IO STRTree a 
insert :: Storable a => Geometry b -> a -> STRTreeBuilder a -> IO () 

They might be a way to use ST rather than IO but I haven't done it with FFI yet. I'll investigate.

Edit: this is what I was thinking https://stackoverflow.com/questions/20938819/how-do-i-perform-ffi-calls-inside-st-monad

Edit 2: thinking more about it, perhaps it's a good idea to leave anything in IO and let the user decide whether to use unsafeIOToST. There might be some considerations to make around thread-safety. Admittedly I don't know geos very well.

from geos.

ewestern avatar ewestern commented on July 22, 2024

I went ahead with the decision to abandon STRTreeBuilder.
https://hackage.haskell.org/package/geos-0.4.1

I think this approach is best in line with what users will expect when using this library.
Regarding your edit 2, note that I've exposed the "Raw" module, which exposes functions that are much closer to the underlying GEOS api -- these functions run in a Geos monad and can be used if you're looking for a more "honest" abstraction.
That said, the main reason the low-level FFI functions run in IO is because of the effectful logging that geos does. Assuming that this logging and the memory management is handled correctly (which it may not be!), I don't feel too bad about calling unsafePerformIO to exit the Geos monad -- which is what the functions outside of the Raw module end up doing.

Regarding your suggestion to use ST. I'm definitely open to something like that for the Raw module. To be honest, I haven't used ST directly before, so I don't have a good sense for the advantages there.

With this recent patch, I'm going to close this issue. Feel free to open another if you have ideas for functions in the Raw module -- which appears to be closer to what you're looking for.

from geos.

andreabedini avatar andreabedini commented on July 22, 2024

Thanks @ewestern, it sounds like a good step forward. Exposing the Raw module will allow us to experiment with different approaches. Cheers!

from geos.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.