* Give back a Pair from C to OCaml? @ 2008-01-17 15:32 Oliver Bandel 2008-01-17 15:37 ` [Caml-list] " Berke Durak ` (2 more replies) 0 siblings, 3 replies; 12+ messages in thread From: Oliver Bandel @ 2008-01-17 15:32 UTC (permalink / raw) To: caml-list Hello, how can I give back a pair of values (in my case both are int's) from C to OCaml? Didn't found something in the docs so far. TIA, Oliver ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 15:32 Give back a Pair from C to OCaml? Oliver Bandel @ 2008-01-17 15:37 ` Berke Durak 2008-01-17 15:43 ` Till Varoquaux 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier 2 siblings, 0 replies; 12+ messages in thread From: Berke Durak @ 2008-01-17 15:37 UTC (permalink / raw) To: Oliver Bandel; +Cc: caml-list Oliver Bandel a écrit : > Hello, > > how can I give back a pair of values (in my case both are > int's) from C to OCaml? > > Didn't found something in the docs so far. > Just allocate a block of size two and store your integers in the two fields of the block. pairv = caml_alloc(2, 0); Store_field(pairv, 0, Val_int(x)); Store_field(pairv, 1, Val_int(y)); -- Berke DURAK ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 15:32 Give back a Pair from C to OCaml? Oliver Bandel 2008-01-17 15:37 ` [Caml-list] " Berke Durak @ 2008-01-17 15:43 ` Till Varoquaux 2008-01-17 18:32 ` Oliver Bandel 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier 2 siblings, 1 reply; 12+ messages in thread From: Till Varoquaux @ 2008-01-17 15:43 UTC (permalink / raw) To: Oliver Bandel; +Cc: caml-list from the manual: >>Tuples are represented by pointers to blocks, with tag 0. You have to create a block with two fields and put each integer in one of these fields (ocaml integers that is). I can't tell you exactly from the top of my head but I do rememenber it isn't really hard. Cheers, Till On Jan 17, 2008 3:32 PM, Oliver Bandel <oliver@first.in-berlin.de> wrote: > Hello, > > how can I give back a pair of values (in my case both are > int's) from C to OCaml? > > Didn't found something in the docs so far. > > TIA, > Oliver > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- http://till-varoquaux.blogspot.com/ ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 15:43 ` Till Varoquaux @ 2008-01-17 18:32 ` Oliver Bandel 2008-01-17 18:41 ` Richard Jones 0 siblings, 1 reply; 12+ messages in thread From: Oliver Bandel @ 2008-01-17 18:32 UTC (permalink / raw) To: caml-list Zitat von Till Varoquaux <till.varoquaux@gmail.com>: > from the manual: > > >>Tuples are represented by pointers to blocks, with tag 0. > > You have to create a block with two fields and put each integer in > one > of these fields (ocaml integers that is). I can't tell you exactly > from the top of my head but I do rememenber it isn't really hard. [...] I only have seen so much different docs about that topic. For example OCaml-book from OReilly writes other things then I found at other places. But it doesn't matter what the OCaml-book says, only the reference manual does count. But the reference manual is not an introductional text, and so I may have to reread it to understand it. For example I can use "CAMLprim value <functionname> (.....){...}" or I can throw out "CAMLprim" (which is, what I found in the OReilly-book). Also "value x" or "int x" are working as C-parameters of a function. But an OCaml-int is not the same like a C-int, so I would expect gcc throw out at least a warning. I have added "-Wall", but no warning there. Possibly that's because what is mentioned in "18.2", that int's are "value". But there is no distinction between the OCaml-ints and the machine's C-ints in that text. Is "an unboxed integer" meant to be a machine's native int, 32 Bits or 64 Bits, depending on the machine?. Can it be given as parameter and return value as it is? At least the missing compiler-warnings seem to say me that. Possibly I should read the reference manual in more detail but possibly someone sees, what I have overseen...?! Ciao, Oliver ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 18:32 ` Oliver Bandel @ 2008-01-17 18:41 ` Richard Jones 2008-01-17 19:57 ` Oliver Bandel 2008-01-17 21:43 ` CAMLprim and co (was: Give back a Pair from C to OCaml?) Alain Frisch 0 siblings, 2 replies; 12+ messages in thread From: Richard Jones @ 2008-01-17 18:41 UTC (permalink / raw) To: Oliver Bandel; +Cc: caml-list On Thu, Jan 17, 2008 at 07:32:19PM +0100, Oliver Bandel wrote: > For example I can use "CAMLprim value <functionname> (.....){...}" > or I can throw out "CAMLprim" (which is, what I found in the > OReilly-book). It's not really a good idea to "throw out" CAMLprim. It expands to something useful on Windows. > Also "value x" or "int x" are working as C-parameters > of a function. But an OCaml-int is not the same like a > C-int, so I would expect gcc throw out at least a warning. > I have added "-Wall", but no warning there. value <> int. On normal 64 bit architectures it's defined as a long, but basically they are not interchangable and you should always use 'value' when you mean an OCaml value. > Possibly that's because what is mentioned in "18.2", > that int's are "value". > But there is no distinction between the OCaml-ints and the > machine's C-ints in that text. Is "an unboxed integer" meant > to be a machine's native int, 32 Bits or 64 Bits, depending on the > machine?. Can it be given as parameter and return value as it is? No. A Caml int is not represented the same way as a C int. This doesn't matter because you should be using the macros supplied to convert between the two representations, Val_int, Int_val, Long_val, Unsigned_long_val etc. Rich. -- Richard Jones Red Hat ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 18:41 ` Richard Jones @ 2008-01-17 19:57 ` Oliver Bandel 2008-01-17 21:43 ` CAMLprim and co (was: Give back a Pair from C to OCaml?) Alain Frisch 1 sibling, 0 replies; 12+ messages in thread From: Oliver Bandel @ 2008-01-17 19:57 UTC (permalink / raw) To: caml-list Zitat von Richard Jones <rich@annexia.org>: > On Thu, Jan 17, 2008 at 07:32:19PM +0100, Oliver Bandel wrote: > > For example I can use "CAMLprim value <functionname> (.....){...}" > > or I can throw out "CAMLprim" (which is, what I found in the > > OReilly-book). > > It's not really a good idea to "throw out" CAMLprim. It expands to > something useful on Windows. [...] OK, I will let it inside. > > > Also "value x" or "int x" are working as C-parameters > > of a function. But an OCaml-int is not the same like a > > C-int, so I would expect gcc throw out at least a warning. > > I have added "-Wall", but no warning there. > > value <> int. On normal 64 bit architectures it's defined as a long, > but basically they are not interchangable and you should always use > 'value' when you mean an OCaml value. Yes, and that was the reason why I asked ;-) So, when sseing at the C-function, that it gets parameters from OCaml, I have to use the parameters as "value".... at least this way remembering it makes sense. So I will look at "value" like on a typedef'd something. > > > Possibly that's because what is mentioned in "18.2", > > that int's are "value". > > But there is no distinction between the OCaml-ints and the > > machine's C-ints in that text. Is "an unboxed integer" meant > > to be a machine's native int, 32 Bits or 64 Bits, depending on the > > machine?. Can it be given as parameter and return value as it is? > > No. A Caml int is not represented the same way as a C int. [...] Yes, I know, but I didn't know, which "int" is meant ther in the first text-passage of 18.2. Following all the hints here from that list (and the manual and some OReilly's book reading), I have done this now: ======================================== CAMLprim value both_quad( value a, value b ) { /* declarations, CAML-allocations and conversions */ long a_quad; long b_quad; long a_c; long b_c; CAMLparam2( a, b ); CAMLlocal1( result ); result = caml_alloc (2, 0); a_c = Long_val( a ); b_c = Long_val( b ); /* now start working */ /* do the quad */ /* ----------- */ a_quad = a_c * a_c; b_quad = b_c * b_c; Store_field (result, 0, Val_long(a_quad)); Store_field (result, 1, Val_long(b_quad)); CAMLreturn( result ); } ======================================== This is only learncode, so don't await that it would make sense to implement this in realworld ;-) I only want to know if this is bugfree now, and can be used without problems. Or can I make it better somehow? TIA, Oliver ^ permalink raw reply [flat|nested] 12+ messages in thread
* CAMLprim and co (was: Give back a Pair from C to OCaml?) 2008-01-17 18:41 ` Richard Jones 2008-01-17 19:57 ` Oliver Bandel @ 2008-01-17 21:43 ` Alain Frisch 1 sibling, 0 replies; 12+ messages in thread From: Alain Frisch @ 2008-01-17 21:43 UTC (permalink / raw) To: Richard Jones, caml-list Richard Jones wrote: > It's not really a good idea to "throw out" CAMLprim. It expands to > something useful on Windows. CAMLexport was more appropriate. CAMLprim is used by the OCaml Makefiles to build to table of C primitives available by default in an OCaml runtime. I said "was" because in the forthcoming OCaml 3.11, CAMLexport will be useless even on Windows (but it might be a good idea to keep it, just as an annotation). -- Alain ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 15:32 Give back a Pair from C to OCaml? Oliver Bandel 2008-01-17 15:37 ` [Caml-list] " Berke Durak 2008-01-17 15:43 ` Till Varoquaux @ 2008-01-17 20:43 ` Florent Monnier 2008-01-17 21:05 ` Oliver Bandel ` (2 more replies) 2 siblings, 3 replies; 12+ messages in thread From: Florent Monnier @ 2008-01-17 20:43 UTC (permalink / raw) To: caml-list; +Cc: Oliver Bandel > how can I give back a pair of values (in my case both are > int's) from C to OCaml? > > Didn't found something in the docs so far. Hi, Recently I've written a small tutorial about wrapping ocaml to C, maybe you could find it usefull: http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php and if someone wish to contribute to this document (correct the english or enhance it) it's wellcome. With regards ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier @ 2008-01-17 21:05 ` Oliver Bandel 2008-01-17 22:26 ` Oliver Bandel 2008-01-18 11:05 ` Richard Jones 2 siblings, 0 replies; 12+ messages in thread From: Oliver Bandel @ 2008-01-17 21:05 UTC (permalink / raw) To: caml-list Zitat von Florent Monnier <fmonnier@linux-nantes.fr.eu.org>: > > how can I give back a pair of values (in my case both are > > int's) from C to OCaml? > > > > Didn't found something in the docs so far. > Hi, > Recently I've written a small tutorial about wrapping ocaml to C, > maybe you could find it usefull: > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php [...] OK, thank you, I will look at it! :-) Ciao, Oliver ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier 2008-01-17 21:05 ` Oliver Bandel @ 2008-01-17 22:26 ` Oliver Bandel 2008-01-17 23:58 ` Florent Monnier 2008-01-18 11:05 ` Richard Jones 2 siblings, 1 reply; 12+ messages in thread From: Oliver Bandel @ 2008-01-17 22:26 UTC (permalink / raw) To: caml-list hello Florent, Zitat von Florent Monnier <fmonnier@linux-nantes.fr.eu.org>: > > how can I give back a pair of values (in my case both are > > int's) from C to OCaml? > > > > Didn't found something in the docs so far. > Hi, > Recently I've written a small tutorial about wrapping ocaml to C, > maybe you could find it usefull: > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php [...] You mentioned a "Double_wosize" in your tutorial, but I can't find such a thing in the reference manual. So, there seems to be something different.... Possibly you mean something like Wosize_val(Double_field(ml_array, 0)) ??? Would that work? Does int- and floats really need such a distinction?! Ciao, Oliver ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 22:26 ` Oliver Bandel @ 2008-01-17 23:58 ` Florent Monnier 0 siblings, 0 replies; 12+ messages in thread From: Florent Monnier @ 2008-01-17 23:58 UTC (permalink / raw) To: caml-list; +Cc: Oliver Bandel Hi, > > Recently I've written a small tutorial about wrapping ocaml to C, > > maybe you could find it usefull: > > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php > > [...] > > You mentioned a "Double_wosize" in your tutorial, > but I can't find such a thing in the reference manual. Yes indeed, the macro Val_emptylist is not either in the manual, but if you read the caml header files, you will find both of these macro. You can safely use both of them in your project. > Possibly you mean something like > Wosize_val(Double_field(ml_array, 0)) > ??? > Would that work? I don't know, I haven't tryed. But for the "Double_wosize" macro, you can trust, I have asked on the other ocaml list, so there's no problem there. And a lot of ocaml wrappers use it. > Does int- and floats really need such a distinction?! Yes, float arrays are not handle in the same way than the other values. (it's explained in the manual.) When I had read this part of the ocaml manual, I haven't found it very easy to understand, so that's why I thought perhaps it is the same for other people, and why I have tryed to make this little tutorial. Regards ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Give back a Pair from C to OCaml? 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier 2008-01-17 21:05 ` Oliver Bandel 2008-01-17 22:26 ` Oliver Bandel @ 2008-01-18 11:05 ` Richard Jones 2 siblings, 0 replies; 12+ messages in thread From: Richard Jones @ 2008-01-18 11:05 UTC (permalink / raw) To: Florent Monnier; +Cc: caml-list, Oliver Bandel On Thu, Jan 17, 2008 at 09:43:55PM +0100, Florent Monnier wrote: > > how can I give back a pair of values (in my case both are > > int's) from C to OCaml? > > > > Didn't found something in the docs so far. > Hi, > Recently I've written a small tutorial about wrapping ocaml to C, > maybe you could find it usefull: > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php > > and if someone wish to contribute to this document (correct the english or > enhance it) it's wellcome. Thanks ... Added to the list of external tutorials. http://www.ocaml-tutorial.org/#External_tutorials I found the grey-on-grey colour scheme a little bit tiring to read though. Rich. -- Richard Jones Red Hat ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2008-01-18 11:05 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-01-17 15:32 Give back a Pair from C to OCaml? Oliver Bandel 2008-01-17 15:37 ` [Caml-list] " Berke Durak 2008-01-17 15:43 ` Till Varoquaux 2008-01-17 18:32 ` Oliver Bandel 2008-01-17 18:41 ` Richard Jones 2008-01-17 19:57 ` Oliver Bandel 2008-01-17 21:43 ` CAMLprim and co (was: Give back a Pair from C to OCaml?) Alain Frisch 2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier 2008-01-17 21:05 ` Oliver Bandel 2008-01-17 22:26 ` Oliver Bandel 2008-01-17 23:58 ` Florent Monnier 2008-01-18 11:05 ` Richard Jones
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox