Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  FFI foreignPtr construction (Sylvain Henry)
   2.  Is it possible to use a <> constructor in        analogy to [] ?
      (martin)
   3. Re:  FFI foreignPtr construction (PICCA Frederic-Emmanuel)
   4. Re:  Increasing capabilities dramatically increases execution
      time (Thomas Koster)


----------------------------------------------------------------------

Message: 1
Date: Wed, 27 Jan 2016 19:37:00 +0100
From: Sylvain Henry <hsy...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] FFI foreignPtr construction
Message-ID:
        <capmptcw1rayvdgyt7dgdbhpaoa+rko3zxzs26c3-uybcceq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

You can try something like this (not tested):

data Geo = Geo -- this represents the HklGeometry C structure (for clarity)

newtype Geometry = Geometry (ForeignPtr Geo) deriving (Show, Storable)

newGeometry :: Factory -> IO Geometry
newGeometry f = do -- avoid unsafePerformIO
                  geometry <- c_hkl_factory_create_new_geometry f
                  Geometry <$> newForeignPtr c_hkl_geometry_free geometry

foreign import ccall safe "hkl.h hkl_factory_create_new_geometry"
  c_hkl_factory_create_new_geometry :: Factory
                                    -> IO (Ptr Geo)

foreign import ccall safe "hkl.h &hkl_geometry_free"
  c_hkl_geometry_free :: FunPtr (Ptr Geo -> IO ())

2016-01-27 13:50 GMT+01:00 PICCA Frederic-Emmanuel <
frederic-emmanuel.pi...@synchrotron-soleil.fr>:

> Hello,
>
> since the last time I think that I understand how to manage Ptr, now I
> woud like to masterize the ForeignPtr in order to let haskell managed the
> life of my C objects.
> So I try to create a Geoemtry object like this.
>
> -- data Geometry
> newtype Geometry = Geometry (Ptr Geometry) deriving (Show, Storable)
>
> newGeometry :: Factory -> ForeignPtr Geometry
> newGeometry f = unsafePerformIO $ do
>                   geometry <- c_hkl_factory_create_new_geometry f
>                   newForeignPtr c_hkl_geometry_free geometry
>
> foreign import ccall safe "hkl.h hkl_factory_create_new_geometry"
>   c_hkl_factory_create_new_geometry :: Factory
>                                     -> IO (Geometry)
>
> foreign import ccall safe "hkl.h &hkl_geometry_free"
>   c_hkl_geometry_free :: FunPtr (Geometry -> IO ())
>
>
>
> the C signature are
>
> HKLAPI HklGeometry *hkl_factory_create_new_geometry(const HklFactory
> *self) HKL_ARG_NONNULL(1);
>
> HKLAPI void hkl_geometry_free(HklGeometry *self) HKL_ARG_NONNULL(1);
>
>
> But when I try to compile this code, I get this error message
>
>
> 1 of 1] Compiling Hkl.C            ( src/Hkl/C.hs, dist/build/Hkl/C.o )
>
> src/Hkl/C.hs:51:33:
>     Couldn't match type ?Geometry? with ?Ptr Geometry?
>     Expected type: GHC.ForeignPtr.FinalizerPtr Geometry
>       Actual type: FunPtr (Geometry -> IO ())
>     In the first argument of ?newForeignPtr?, namely
>       ?c_hkl_geometry_free?
>     In a stmt of a 'do' block:
>       newForeignPtr c_hkl_geometry_free geometry
>
> src/Hkl/C.hs:51:53:
>     Couldn't match expected type ?Ptr Geometry?
>                 with actual type ?Geometry?
>     In the second argument of ?newForeignPtr?, namely ?geometry?
>     In a stmt of a 'do' block:
>       newForeignPtr c_hkl_geometry_free geometry
>
> I do not understand what is wrong in my code
>
> thanks if you can help
>
> Frederic
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160127/2bbe2fe7/attachment-0001.html>

------------------------------

Message: 2
Date: Wed, 27 Jan 2016 21:40:18 +0100
From: martin <martin.drautzb...@web.de>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Is it possible to use a <> constructor in
        analogy to [] ?
Message-ID: <56a92b32.4090...@web.de>
Content-Type: text/plain; charset=utf-8

Hello all,

I've written my own Read and Show instances for a type, which is essentially

data Crust a = Open [a] | Closed [a]

"Show" does something like

*Main> Closed [1,2,3]
<1,2,3>
*Main> Open [1,2,3]
<1,2,3..>

And Read faithully parses each stings and turns it into a Crust again.

Problem is: when I just type <1,2,3..> in GHCi I get a "parse error on input 
?<?". However when I spell out the
custructor as above it parses it okay.

Is it possible at all to write my own <> constructor, or something like it=


------------------------------

Message: 3
Date: Wed, 27 Jan 2016 20:49:22 +0000
From: PICCA Frederic-Emmanuel
        <frederic-emmanuel.pi...@synchrotron-soleil.fr>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <beginners@haskell.org>
Subject: Re: [Haskell-beginners] FFI foreignPtr construction
Message-ID:
        
<a2a20ec3b8560d408356cac2fc148e53b3028...@sun-dag3.synchrotron-soleil.fr>
        
Content-Type: text/plain; charset="us-ascii"

Thanks for your help

I end up with this code which I find quite elegant :)

-- data Geometry
data HklGeometry
newtype Geometry = Geometry (ForeignPtr HklGeometry) deriving (Show)

newGeometry :: Factory -> IO Geometry
newGeometry f =
  Geometry <$> (c_hkl_factory_create_new_geometry f >>= newForeignPtr 
c_hkl_geometry_free)

foreign import ccall unsafe "hkl.h hkl_factory_create_new_geometry"
  c_hkl_factory_create_new_geometry :: Factory -> IO (Ptr HklGeometry)

foreign import ccall unsafe "hkl.h &hkl_geometry_free"
  c_hkl_geometry_free :: FunPtr (Ptr HklGeometry -> IO ())



Cheers

Frederic

------------------------------

Message: 4
Date: Thu, 28 Jan 2016 11:06:55 +1100
From: Thomas Koster <tkos...@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Increasing capabilities dramatically
        increases execution time
Message-ID:
        <cag1wh7coaj4hwom377rj42ryt-cmj4om191fphs+q10ltau...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

> On 2016-01-22, Thomas Koster wrote:
> I have encountered a situation in a concurrent program where I see an
> unexpected, dramatic increase in execution time when I add additional
> capabilities. On a multi-core CPU, "-N1" is fastest by an order of
> magnitude and the program increasingly slows for an increasing number
> of capabilities (still fewer than the number of real cores, of
> course).
>
> <snip/>

On 27 January 2016 at 18:07, Elias Diem <li...@webconect.ch> wrote:
> Maybe post this to the caf? list at:
>
> https://mail.haskell.org/mailman/listinfo/haskell-cafe

Thanks, Elias. This is being discussed there in the thread "When are
MVars better than STM?".

https://mail.haskell.org/pipermail/haskell-cafe/2016-January/122785.html

--
Thomas Koster


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 91, Issue 35
*****************************************

Reply via email to