Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/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:  help with FFI: passing char* to dll function + pointer
      freeing (Brandon Allbery)
   2. Re:  help with FFI: passing char* to dll  function + pointer
      freeing (Edward Z. Yang)
   3. Re:  help with FFI: passing char* to dll function + pointer
      freeing (Miro Karpis)
   4.  Why is it so slow to solve "10^(10^10)"? (yi lu)


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

Message: 1
Date: Sat, 21 Sep 2013 15:48:15 -0400
From: Brandon Allbery <[email protected]>
To: Miro Karpis <[email protected]>,  The Haskell-Beginners
        Mailing List - Discussion of primarily beginner-level topics related
        to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID:
        <CAKFCL4X6TSoq3S2sA4LYbw6PQr_=brnhxoaaibjt91qjqr_...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sat, Sep 21, 2013 at 3:25 PM, Miro Karpis <[email protected]>wrote:

> Thanks so far, but I'm still having troubles with converting String to
> CString.... The error I'm getting is:
>
>  Couldn't match type `IO CString' with `Ptr CChar'
>  Expected type: CString
>    Actual type: IO CString
>
(...)

>   let param = newCString "someString"
>

Don't use let there; newCString is in IO.

    param <- newCString "someString"

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/d2086d9d/attachment-0001.html>

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

Message: 2
Date: Sat, 21 Sep 2013 16:10:56 -0400
From: "Edward Z. Yang" <[email protected]>
To: Miro Karpis <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID: <1379794173-sup-3409@javelin>
Content-Type: text/plain; charset=UTF-8

Hello Miro,

The immediate problem is that you need to use the bind
notation (param <- newCString "foo") rather than let
notation, but if the string will not be used beyond the
function call, it is preferable to use withCString

Edward

Excerpts from Miro Karpis's message of Sat Sep 21 15:25:58 -0400 2013:
> Thanks so far, but I'm still having troubles with converting String to
> CString.... The error I'm getting is:
> 
>  Couldn't match type `IO CString' with `Ptr CChar'
>  Expected type: CString
>    Actual type: IO CString
> 
> 
> code:
> 
> -------
> import Foreign
> import Foreign.C
> 
> --int somemethod(char *param, int length, double array[], int UB1, int UB2,
> bool isValid);
> foreign import stdcall unsafe "setmoduletable"  c_setmoduletable ::
>    CString -> CInt -> Ptr CDouble -> CInt -> CInt -> CInt -> IO CInt
> 
> main = do
>   let param = newCString "someString"
>   --paramLength = length param
>   let realTable = [ 0.0111125, 0.0111125, 0.009525] :: [CDouble]
>       ub1 = 0::CInt
>       ub2 = 2::CInt
>       --isValid = False
>   realTablePtr <- newArray realTable
>   x <- c_setmoduletable param 14 realTablePtr ub1 ub2 0
>   free realTablePtr
>   putStrLn $ "c_setmoduletable output: " ++ show x
>   putStrLn "Done"
> 
> 
> cheers,
> m.
> 
> On Sat, Sep 21, 2013 at 4:21 PM, Edward Z. Yang <[email protected]> wrote:
> 
> > You should use the C variants of all the types, so
> >
> > type CBool = CInt
> > foreign import stdcall unsafe "setmoduletable"  c_setmoduletable ::
> >     CString -> CInt -> Ptr CDouble -> CInt -> CInt -> CBool -> IO CInt
> >
> > Edward
> >
> > Excerpts from Miro Karpis's message of Sat Sep 21 08:55:58 -0400 2013:
> > > thanks,.. so far I came to this. I can compile it but not getting the
> > right
> > > values from the method. Most probably some problem with the pointers...
> > >
> > >
> > > ----------------
> > > --int setmoduletable(char *param, int length, double array[], int UB1,
> > int
> > > UB2, bool isValid);
> > >
> > >
> > > foreign import stdcall unsafe "setmoduletable"  c_setmoduletable :: Ptr
> > > Char
> > >
> > >                           -> Int
> > >
> > >                           -> Ptr (Double)
> > >
> > >                           -> Int
> > >
> > >                           -> Int
> > >
> > >                           -> Bool
> > >
> > >                           -> IO Int
> > >
> > > main = do
> > >   let param = "someString"
> > >   let paramLength = length param
> > >       realTable = [ 0.0111125, 0.0111125, 0.009525] :: [Double]
> > >       ub1 = 0
> > >       ub2 = 2
> > >       isValid = False
> > >   realTablePtr <- newArray realTable
> > >   paramPtr <- newArray param
> > >   x <- c_setmoduletable paramPtr paramLength realTablePtr ub1 ub2 isValid
> > >   free realTablePtr
> > >   free paramPtr
> > >   putStrLn $ "c_setmoduletable output: " ++ show x
> > >   putStrLn "Done"
> > >
> > > On Sat, Sep 21, 2013 at 1:06 AM, Brandon Allbery <[email protected]
> > >wrote:
> > >
> > > > On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis <
> > [email protected]>wrote:
> > > >
> > > >> Please, I just can not figure this out:
> > > >>
> > > >> I have a method in my C dll:
> > > >> int somemethod(char *param, int length, double array[], int UB1, int
> > UB2,
> > > >> bool isValid);
> > > >>
> > > >> I would like to use this method in Haskell, so I have defined it like:
> > > >>
> > > >> foreign import stdcall unsafe "somemethod"  c_somemethod :: Ptr
> > CString
> > > >>
> > > >
> > > > You don't want Ptr CString.  See:
> > > >
> > > >     Prelude> :m +Foreign.C.String
> > > >     Prelude Foreign.C.String> :i CString
> > > >     type CString = GHC.Ptr.Ptr Foreign.C.Types.CChar
> > > >   -- Defined in `Foreign.C.String'
> > > >
> > > > In other words, CString is an alias for Ptr CChar. Ptr CString
> > corresponds
> > > > to (char **), not (char *).
> > > >
> > > > --
> > > > brandon s allbery kf8nh                               sine nomine
> > > > associates
> > > > [email protected]
> > > > [email protected]
> > > > unix, openafs, kerberos, infrastructure, xmonad
> > > > http://sinenomine.net
> > > >
> >


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

Message: 3
Date: Sat, 21 Sep 2013 23:40:27 +0200
From: Miro Karpis <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID:
        <cajnnbxgwprl7o6s1p+4urk+++ccc2-kasvkejdocvckxsca...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

yes, that solved it. millions of thanks everybody ;-)

m.


On Sat, Sep 21, 2013 at 10:10 PM, Edward Z. Yang <[email protected]> wrote:

> Hello Miro,
>
> The immediate problem is that you need to use the bind
> notation (param <- newCString "foo") rather than let
> notation, but if the string will not be used beyond the
> function call, it is preferable to use withCString
>
> Edward
>
> Excerpts from Miro Karpis's message of Sat Sep 21 15:25:58 -0400 2013:
> > Thanks so far, but I'm still having troubles with converting String to
> > CString.... The error I'm getting is:
> >
> >  Couldn't match type `IO CString' with `Ptr CChar'
> >  Expected type: CString
> >    Actual type: IO CString
> >
> >
> > code:
> >
> > -------
> > import Foreign
> > import Foreign.C
> >
> > --int somemethod(char *param, int length, double array[], int UB1, int
> UB2,
> > bool isValid);
> > foreign import stdcall unsafe "setmoduletable"  c_setmoduletable ::
> >    CString -> CInt -> Ptr CDouble -> CInt -> CInt -> CInt -> IO CInt
> >
> > main = do
> >   let param = newCString "someString"
> >   --paramLength = length param
> >   let realTable = [ 0.0111125, 0.0111125, 0.009525] :: [CDouble]
> >       ub1 = 0::CInt
> >       ub2 = 2::CInt
> >       --isValid = False
> >   realTablePtr <- newArray realTable
> >   x <- c_setmoduletable param 14 realTablePtr ub1 ub2 0
> >   free realTablePtr
> >   putStrLn $ "c_setmoduletable output: " ++ show x
> >   putStrLn "Done"
> >
> >
> > cheers,
> > m.
> >
> > On Sat, Sep 21, 2013 at 4:21 PM, Edward Z. Yang <[email protected]> wrote:
> >
> > > You should use the C variants of all the types, so
> > >
> > > type CBool = CInt
> > > foreign import stdcall unsafe "setmoduletable"  c_setmoduletable ::
> > >     CString -> CInt -> Ptr CDouble -> CInt -> CInt -> CBool -> IO CInt
> > >
> > > Edward
> > >
> > > Excerpts from Miro Karpis's message of Sat Sep 21 08:55:58 -0400 2013:
> > > > thanks,.. so far I came to this. I can compile it but not getting the
> > > right
> > > > values from the method. Most probably some problem with the
> pointers...
> > > >
> > > >
> > > > ----------------
> > > > --int setmoduletable(char *param, int length, double array[], int
> UB1,
> > > int
> > > > UB2, bool isValid);
> > > >
> > > >
> > > > foreign import stdcall unsafe "setmoduletable"  c_setmoduletable ::
> Ptr
> > > > Char
> > > >
> > > >                           -> Int
> > > >
> > > >                           -> Ptr (Double)
> > > >
> > > >                           -> Int
> > > >
> > > >                           -> Int
> > > >
> > > >                           -> Bool
> > > >
> > > >                           -> IO Int
> > > >
> > > > main = do
> > > >   let param = "someString"
> > > >   let paramLength = length param
> > > >       realTable = [ 0.0111125, 0.0111125, 0.009525] :: [Double]
> > > >       ub1 = 0
> > > >       ub2 = 2
> > > >       isValid = False
> > > >   realTablePtr <- newArray realTable
> > > >   paramPtr <- newArray param
> > > >   x <- c_setmoduletable paramPtr paramLength realTablePtr ub1 ub2
> isValid
> > > >   free realTablePtr
> > > >   free paramPtr
> > > >   putStrLn $ "c_setmoduletable output: " ++ show x
> > > >   putStrLn "Done"
> > > >
> > > > On Sat, Sep 21, 2013 at 1:06 AM, Brandon Allbery <
> [email protected]
> > > >wrote:
> > > >
> > > > > On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis <
> > > [email protected]>wrote:
> > > > >
> > > > >> Please, I just can not figure this out:
> > > > >>
> > > > >> I have a method in my C dll:
> > > > >> int somemethod(char *param, int length, double array[], int UB1,
> int
> > > UB2,
> > > > >> bool isValid);
> > > > >>
> > > > >> I would like to use this method in Haskell, so I have defined it
> like:
> > > > >>
> > > > >> foreign import stdcall unsafe "somemethod"  c_somemethod :: Ptr
> > > CString
> > > > >>
> > > > >
> > > > > You don't want Ptr CString.  See:
> > > > >
> > > > >     Prelude> :m +Foreign.C.String
> > > > >     Prelude Foreign.C.String> :i CString
> > > > >     type CString = GHC.Ptr.Ptr Foreign.C.Types.CChar
> > > > >   -- Defined in `Foreign.C.String'
> > > > >
> > > > > In other words, CString is an alias for Ptr CChar. Ptr CString
> > > corresponds
> > > > > to (char **), not (char *).
> > > > >
> > > > > --
> > > > > brandon s allbery kf8nh                               sine nomine
> > > > > associates
> > > > > [email protected]
> > > > > [email protected]
> > > > > unix, openafs, kerberos, infrastructure, xmonad
> > > > > http://sinenomine.net
> > > > >
> > >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/07dabebd/attachment-0001.html>

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

Message: 4
Date: Sun, 22 Sep 2013 06:32:10 +0800
From: yi lu <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Why is it so slow to solve "10^(10^10)"?
Message-ID:
        <cakcmqqwr_hc7fed0urxs2ertvhvrs_n2ps9suexknx1ovpj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Why is it so slow to solve "10^(10^10)" in Haskell?

Yi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130922/1d61512d/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 63, Issue 32
*****************************************

Reply via email to