Send Beginners mailing list submissions to
        beginners@haskell.org

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
        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:  help with FFI: passing char* to dll function + pointer
      freeing (Miro Karpis)
   2. Re:  question about pattern guards (Daniel Trstenjak)
   3. Re:  help with FFI: passing char* to dll  function + pointer
      freeing (Edward Z. Yang)
   4. Re:  question about pattern guards (Graham Gill)
   5. Re:  help with FFI: passing char* to dll function + pointer
      freeing (Miro Karpis)


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

Message: 1
Date: Sat, 21 Sep 2013 14:55:58 +0200
From: Miro Karpis <miroslav.kar...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID:
        <cajnnbxfzpnm4avgu+ierimck6wpsspmmype1wyn+msj1h_w...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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 = "Input_Bit_Nozz"
  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 <allber...@gmail.com>wrote:

> On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis <miroslav.kar...@gmail.com>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
> allber...@gmail.com
> ballb...@sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/40773752/attachment-0001.html>

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

Message: 2
Date: Sat, 21 Sep 2013 15:34:05 +0200
From: Daniel Trstenjak <daniel.trsten...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] question about pattern guards
Message-ID:
        <CAPZ0SW7m2_77XQ9JkCHoTmhjroZAaYs6d1oOSPVNOcEE=yg...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

> What am I missing?

You can combine booleans by '||', but 'Just n <- x' doesn't result to a
boolean.

You can use '<|>' from 'Control.Applicative' to get the desired behavoir.

f x y
   | Just _ <- x <|> y = 1
   | otherwise           = 2

Greetings,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/fae8088b/attachment-0001.html>

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

Message: 3
Date: Sat, 21 Sep 2013 10:21:03 -0400
From: "Edward Z. Yang" <ezy...@mit.edu>
To: Miro Karpis <miroslav.kar...@gmail.com>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID: <1379773167-sup-6378@javelin>
Content-Type: text/plain; charset=UTF-8

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 = "Input_Bit_Nozz"
>   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 <allber...@gmail.com>wrote:
> 
> > On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis 
> > <miroslav.kar...@gmail.com>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
> > allber...@gmail.com
> > ballb...@sinenomine.net
> > unix, openafs, kerberos, infrastructure, xmonad
> > http://sinenomine.net
> >


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

Message: 4
Date: Sat, 21 Sep 2013 11:42:43 -0400
From: Graham Gill <math.simp...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] question about pattern guards
Message-ID: <523dbe73.5030...@gmail.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

That works with Maybe, but not with lists.

If I want at least one of xs or ys to match a two element list then

f xs ys
   | [_,_] <- xs <|> ys = ...

won't behave as expected, since <|> is ++ for lists, so

f [1] [2]

will match the pattern. Undoubtedly there are other type- and 
example-specific ways to encode the alternative for lists and other 
examples. I think what the OP is asking though, is why pattern 
alternatives aren't allowed in pattern guards (or in patterns in 
function definitions, or in case statements). I'm guessing that the 
major problem is binding? Suppose the token "or" introduces pattern 
alternatives:

g xs ys
   | (_:_:xrest) <- xs) or ([y1] <- ys) = ...

The pattern matches if xs has at least two elements or if ys is a one 
element list. But after the match you don't know which names have been 
bound. I guess there are ways to deal with that problem, like requiring 
every pattern alternative to bind exactly the same set of names and 
types, but sounds like that would get pretty hairy. How do other 
languages that allow "or patterns" handle binding?

Graham

On 21/09/2013 9:34 AM, Daniel Trstenjak wrote:
>
> > What am I missing?
>
> You can combine booleans by '||', but 'Just n <- x' doesn't result to 
> a boolean.
>
> You can use '<|>' from 'Control.Applicative' to get the desired behavoir.
>
> f x y
>    | Just _ <- x <|> y = 1
>    | otherwise           = 2
>
> Greetings,
> Daniel
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/15b8e925/attachment-0001.html>

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

Message: 5
Date: Sat, 21 Sep 2013 21:25:58 +0200
From: Miro Karpis <miroslav.kar...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID:
        <cajnnbxfhvukruivlxwugear_1c7-ns105yfku3xgu5xv+y8...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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 <ezy...@mit.edu> 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 <allber...@gmail.com
> >wrote:
> >
> > > On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis <
> miroslav.kar...@gmail.com>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
> > > allber...@gmail.com
> > > ballb...@sinenomine.net
> > > unix, openafs, kerberos, infrastructure, xmonad
> > > http://sinenomine.net
> > >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/343b2a50/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


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

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

Reply via email to