Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: ffi array and peekArray (Sylvain Henry)
2. Re: ffi array and peekArray (PICCA Frederic-Emmanuel)
3. Re: ffi array and peekArray (Sylvain Henry)
4. Calling "zipWith" using "^" function. (Venu Chakravorty)
5. Re: Calling "zipWith" using "^" function. (yi lu)
----------------------------------------------------------------------
Message: 1
Date: Sun, 8 Nov 2015 18:34:52 +0100
From: Sylvain Henry <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ffi array and peekArray
Message-ID:
<CAPmptcW52afFsMdPGa8wp8kfk6hvKAjc+Wjs6To+b=4vnsx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
you have to peek the pointed value. Something like:
hklFactoryGetAll :: IO [HklFactory]
hklFactoryGetAll = alloca $ \ptrn -> do
factories <- c_hkl_factory_get_all ptrn
n <- peek ptrn
peekArray n factories
2015-11-08 10:55 GMT+01:00 PICCA Frederic-Emmanuel <
[email protected]>:
> Hello,
>
> Here the signature of one of my C function
>
> -- hkl.h --
>
> typedef struct _HklFactory HklFactory;
>
> HKLAPI HklFactory **hkl_factory_get_all(size_t *n) HKL_ARG_NONNULL(1);
>
> HKLAPI HklFactory *hkl_factory_get_by_name(const char *name,
> GError **error)
> HKL_ARG_NONNULL(1) HKL_WARN_UNUSED_RESULT;
>
> HKLAPI const char *hkl_factory_name_get(const HklFactory *self)
> HKL_ARG_NONNULL(1);
>
> HKLAPI HklGeometry *hkl_factory_create_new_geometry(const HklFactory
> *self) HKL_ARG_NONNULL(1);
>
> HKLAPI HklEngineList *hkl_factory_create_new_engine_list(const HklFactory
> *self) HKL_ARG_NONNULL(1);
>
>
> I will focuss for now only the get_all method. As you can see even for the
> C client of my API HklFactory is an opac struct
> This get_all method return an array of (HklFactory *) and get the size of
> the array via the n parameter.
>
> So I am trying to use this API from haskell with the foreign system and
> return a [HklFactory]
>
> import Foreign (Ptr, peek)
> import Foreign.Marshal.Alloc (alloca)
> import Foreign.Marshal.Array (peekArray)
>
> data HklFactory
>
> foreign import ccall safe "hkl.h hkl_factory_get_all"
> c_hkl_factory_get_all :: Ptr Int -> IO (Ptr HklFactory)
>
> hklFactoryGetAll :: IO [HklFactory]
> hklFactoryGetAll = alloca $ \n -> do
> factories <- c_hkl_factory_get_all n
> peekArray n factories
>
>
> but indeed it doesn not work, peekArray complain that n is Ptr Int instead
> of Int.
>
> what should I do in order to get my array of HklFactory
>
> thanks for your help.
>
>
> Frederic
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151108/44c49570/attachment-0001.html>
------------------------------
Message: 2
Date: Sun, 8 Nov 2015 20:30:13 +0000
From: PICCA Frederic-Emmanuel
<[email protected]>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell" <[email protected]>
Subject: Re: [Haskell-beginners] ffi array and peekArray
Message-ID:
<a2a20ec3b8560d408356cac2fc148e53b2fae...@sun-dag3.synchrotron-soleil.fr>
Content-Type: text/plain; charset="Windows-1252"
Ok, so now I get this error
ghkl.hs:20:18:
No instance for (Foreign.Storable.Storable HklFactory)
arising from a use of ?peekArray?
In a stmt of a 'do' block: peekArray n factories
In the expression:
do { factories <- c_hkl_factory_get_all ptr;
n <- peek ptr;
peekArray n factories }
In the second argument of ?($)?, namely
?\ ptr
-> do { factories <- c_hkl_factory_get_all ptr;
n <- peek ptr;
.... }?
What should I do to create a Storable for HklFactory which is a simple pointer.
Thanks
Frederic
------------------------------
Message: 3
Date: Sun, 8 Nov 2015 21:45:39 +0100
From: Sylvain Henry <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ffi array and peekArray
Message-ID:
<capmptcw9nexlj3qqglzuc9f-tajpovesrw8+lucf1c5pxpr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
You can alias HklFactory:
type HklFactory = Ptr ()
Or if you want to avoid mixing HklFactory and other pointers, you can use:
newtype HklFactory = HklFactory (Ptr ()) deriving (Storable)
The latter requires the GeneralizedNewtypeDeriving extension, see:
https://wiki.haskell.org/Foreign_Function_Interface#Renaming_and_Storable_instances
Sylvain
2015-11-08 21:30 GMT+01:00 PICCA Frederic-Emmanuel <
[email protected]>:
> Ok, so now I get this error
>
>
> ghkl.hs:20:18:
> No instance for (Foreign.Storable.Storable HklFactory)
> arising from a use of ?peekArray?
> In a stmt of a 'do' block: peekArray n factories
> In the expression:
> do { factories <- c_hkl_factory_get_all ptr;
> n <- peek ptr;
> peekArray n factories }
> In the second argument of ?($)?, namely
> ?\ ptr
> -> do { factories <- c_hkl_factory_get_all ptr;
> n <- peek ptr;
> .... }?
>
>
> What should I do to create a Storable for HklFactory which is a simple
> pointer.
>
> Thanks
>
> Frederic
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151108/8f93ff4f/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 8 Nov 2015 23:14:11 -0500
From: Venu Chakravorty <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Calling "zipWith" using "^" function.
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hello there,
This is just some toy program I was trying. I fail to understand why this works:
=============================
Prelude> zipWith (\ x y -> ((x ^ y) / (product [1..x]))) [1..3] [2,2,2]
[1.0,2.0,1.5]
=============================
and this does not:
=============================
Prelude> zipWith (\ x y -> ((y ^ x) / (product [1..x]))) [1..3] [2,2,2]
<interactive>:1:19:
Ambiguous type variable `a' in the constraints:
`Fractional a' arising from a use of `/' at <interactive>:1:19-44
`Integral a' arising from a use of `^' at <interactive>:1:20-24
Probable fix: add a type signature that fixes these type variable(s)
============================
Note that the "^" function has "x" and "y" flipped.
:t (^) says:
============================
(^) :: (Num a, Integral b) => a -> b -> a
============================
Could somebody please throw some light?
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151108/11227543/attachment-0001.html>
------------------------------
Message: 5
Date: Mon, 9 Nov 2015 12:28:14 +0800
From: yi lu <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Calling "zipWith" using "^" function.
Message-ID:
<CAKcmqqwo2BxOvWxJO0iS+JK5+JiyqRov+VdeeW=t=3rcz27...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Prelude> :t (**)
(**) :: Floating a => a -> a -> a
On Mon, Nov 9, 2015 at 12:14 PM, Venu Chakravorty <[email protected]> wrote:
> Hello there,
> This is just some toy program I was trying. I fail to understand why this
> works:
>
> =============================
> Prelude> zipWith (\ x y -> ((x ^ y) / (product [1..x]))) [1..3] [2,2,2]
> [1.0,2.0,1.5]
> =============================
>
> and this does not:
>
> =============================
> Prelude> zipWith (\ x y -> ((y ^ x) / (product [1..x]))) [1..3] [2,2,2]
>
> <interactive>:1:19:
> Ambiguous type variable `a' in the constraints:
> `Fractional a' arising from a use of `/' at <interactive>:1:19-44
> `Integral a' arising from a use of `^' at <interactive>:1:20-24
> Probable fix: add a type signature that fixes these type variable(s)
> ============================
> Note that the "^" function has "x" and "y" flipped.
>
> :t (^) says:
> ============================
> (^) :: (Num a, Integral b) => a -> b -> a
> ============================
>
> Could somebody please throw some light?
>
> Thanks.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151109/b87a6ef1/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 89, Issue 12
*****************************************