companion_cube changed the topic of #ocaml to: Discussion about the OCaml programming language | http://www.ocaml.org | OCaml 4.12 released: https://ocaml.org/releases/4.12.0.html | Try OCaml in your browser: https://try.ocamlpro.com | Public channel logs at https://libera.irclog.whitequark.org/ocaml/
jcob has quit [Quit: Client closed]
infinity0 has quit [Remote host closed the connection]
infinity0 has joined #ocaml
average has quit [Quit: Connection closed for inactivity]
[itchyjunk] has joined #ocaml
favonia has joined #ocaml
average has joined #ocaml
xd1le has joined #ocaml
mbuf has joined #ocaml
waleee has quit [Ping timeout: 260 seconds]
[itchyjunk] has quit [Remote host closed the connection]
zebrag has quit [Remote host closed the connection]
Corbin has quit [Quit: zzz]
mro has joined #ocaml
average has quit []
average has joined #ocaml
gravicappa has joined #ocaml
mro has quit [Remote host closed the connection]
mro has joined #ocaml
mro has quit [Quit: Leaving...]
Haudegen has joined #ocaml
Serpent7776 has quit [Quit: leaving]
abraham has quit [Quit: The Lounge - https://thelounge.chat]
abraham has joined #ocaml
glassofethanol has joined #ocaml
mbuf has quit [Quit: Leaving]
hendursa1 has joined #ocaml
olle has joined #ocaml
hendursaga has quit [Ping timeout: 276 seconds]
Everything has joined #ocaml
bartholin has joined #ocaml
waleee has joined #ocaml
sagax has quit [Read error: Connection reset by peer]
adanwan has joined #ocaml
mbuf has joined #ocaml
<d_bot> <rizo> Is there an equivalent of `Lwt.fail` in Async? It it just `failwith`?
<Leonidas> Essentially, since failures will be collected by the monitor, since unline Lwt.t Deferred.t does not sideload a `result` (but there's Deferred.Result.t in Async)
<d_bot> <rizo> That's helpful, thanks!
Haudegen has quit [Quit: Bin weg.]
daachi has quit [Ping timeout: 268 seconds]
mbuf has quit [Quit: Leaving]
Haudegen has joined #ocaml
favonia has quit [Ping timeout: 252 seconds]
Ronan48 has joined #ocaml
Ronan48 has quit [Ping timeout: 256 seconds]
[itchyjunk] has joined #ocaml
favonia has joined #ocaml
favonia has quit [Ping timeout: 268 seconds]
hackinghorn has joined #ocaml
xd1le has quit [Quit: xd1le]
daachi has joined #ocaml
adanwan has quit [Ping timeout: 276 seconds]
favonia has joined #ocaml
daachi has quit [Read error: Connection reset by peer]
Guest80 has joined #ocaml
<d_bot> <RegularSpatula> Is there a way to globally cap the level of "jobs" run at the same time using Async?
<d_bot> <rgrinberg> Throttle
Corbin has joined #ocaml
<d_bot> <wombat> Hi, I would like to write a shared library (dynamic .so / .dll / .dylib) in OCaml that exports 2 functions, to be called by native C code. I see in the manual that this is possible, but I was wondering if there is an example repo out there that showcases how it's done?
<d_bot> <wombat>
<d_bot> <wombat> I was thinking to write a "wrapper" shared lib in C, which in its DllMain / .init function calls caml_main, exports the 2 functions, whichin turn use the callback mechanism to pass on the call to ocaml? if so, would it be complicated to do the marshalling of pointers, etc.?
Serpent7776 has joined #ocaml
Guest80 has quit [Quit: Ping timeout (120 seconds)]
yoctocell has joined #ocaml
<d_bot> <wombat> actually, I see in Ch 7.5 that I can compile straight to .so with `ocamlc -output-obj -o shlib.so shlib.ml`. However, this gives me a linker error:
<d_bot> <wombat>
<d_bot> <wombat> `/usr/bin/ld: /usr/lib/ocaml/libcamlrun.a(printexc.b.o): relocation R_X86_64_PC32 against symbol `caml_debugger_in_use' can not be used when making a shared object; recompile with -fPIC`
<d_bot> <octachron> If I remember correctly, you need to link with the `_pic` runtime. Either with `-runtime-variant _pic` or with `-no-runtime` (and linking the runtime later).
<d_bot> <wombat> Thanks! Now it's compiling 🙂 Now I just need to figure out how to export the functions. With ctypes?
glassofethanol has quit [Quit: leaving]
hendursa1 has quit [Quit: hendursa1]
hendursaga has joined #ocaml
[itchyjunk] has quit [Read error: Connection reset by peer]
Haudegen has quit [Quit: Bin weg.]
<d_bot> <wombat> Hmm, how do I build with Ctypes? I get unbound module error
<d_bot> <octachron> `Ctypes` is mostly for writing C-to-OCaml binding. You are in the reverse case, aren't you? Then you should call `Callback.register` on the OCaml side to register the OCaml function, and start the OCaml runtime from the C side before calling the OCaml function from the C.
<d_bot> <wombat> It's the reverse case, but once I'm inside OCaml code, I will call back into the C code, via a function pointer given with one of the calls to from the C code
<d_bot> <wombat> It looks something like `void dllEntry( void* func_ptr )` which is exported by the library
favonia has quit [Ping timeout: 265 seconds]
<d_bot> <wombat> then the C code will call the other exported function `vmMain` which should implement the logic that I'd like to write in OCaml, sometimes using calls to that function given by func_ptr
<d_bot> <octachron> Since your main application is written in C, it sounds like it would be easier to write the C binding function yourself and handle all function pointer business on the C side.
<d_bot> <wombat> I see. Good point
<d_bot> <wombat> Then the only thing I haven't understood is how to handle the conversion of data types such as `struct` or strings.
favonia has joined #ocaml
bartholin has quit [Quit: Leaving]
adanwan has joined #ocaml
mro has joined #ocaml
kakadu has quit [Quit: Konversation terminated!]
mro has quit [Remote host closed the connection]
jess has joined #ocaml
Haudegen has joined #ocaml
favonia has quit [Ping timeout: 260 seconds]
adanwan has quit [Ping timeout: 276 seconds]
daachi has joined #ocaml
mro has joined #ocaml
mro has quit [Remote host closed the connection]
daachi has quit [Read error: Connection reset by peer]
olle has quit [Ping timeout: 268 seconds]
adanwan has joined #ocaml
daachi has joined #ocaml
mro has joined #ocaml
favonia has joined #ocaml
Everything has quit [Quit: leaving]
adanwan has quit [Ping timeout: 276 seconds]
olle has joined #ocaml
average has quit [Quit: Connection closed for inactivity]
Tuplanolla has joined #ocaml
mro has quit [Remote host closed the connection]
olle has quit [Ping timeout: 268 seconds]
mro has joined #ocaml
<d_bot> <carmysilna> I'm looking to make use of the Rust crate https://github.com/egraphs-good/egg in an OCaml project... Can I interface/ffi/create bindings directly, or would anyone be interested in an OCaml translation?
Stumpfenstiel has joined #ocaml
<d_bot> <cemerick> oh, that looks pretty hot
zebrag has joined #ocaml
<d_bot> <crackcomm> I use https://github.com/zshipko/ocaml-rs for Rust extensions
<d_bot> <Lortex> https://github.com/Gopiandcode/ego might be useful
gravicappa has quit [Ping timeout: 268 seconds]
<d_bot> <crackcomm> I'm using ocaml platform extension in vscode and I have a struct functor which takes a long time to format on each save and it randomly keeps blinking with error ```The value `sexp_of_t' is required but not provided```, is there any fix for it (the code is ok)?
adanwan has joined #ocaml
<Leonidas> I just ran into a silly issue. I have an .mli with essentially `sig type t module I : sig type t val oops : t -> t end end` and I want to refer in `oops` to be from `I.t` -> `t` where the latter `t` is the "outer" `t`. But I shadowed it so I can't access it. Is there a way to do so anyway?
<cemerick> I thought I saw a rust->ocaml transpiler before...or did I dream it?
<cemerick> Source-to-source, that is
<Leonidas> (well, apart from the "don't do that" and also "stop hitting yourself in the face")
olle has joined #ocaml
<cemerick> Hum, this interesting project claims java->ocaml; the toy example seems to work, even 🤔 https://github.com/jarble/transpiler
mro has quit [Remote host closed the connection]
mro has joined #ocaml
Haudegen has quit [Quit: No Ping reply in 180 seconds.]
Haudegen has joined #ocaml
yoctocell has quit [Ping timeout: 240 seconds]
mro has quit [Quit: Leaving...]
shawnw has joined #ocaml
motherfsck has quit [Quit: quit]
adanwan has quit [Ping timeout: 276 seconds]
daachi has quit [Ping timeout: 268 seconds]
favonia has quit [Ping timeout: 265 seconds]
olle has quit [Ping timeout: 265 seconds]
<d_bot> <Skid> You can use https://github.com/yallop/ocaml-ctypes-inverted-stubs-example for supposedly easy conversions (i havent use it but it is popular) other than that similar to int_val to pass an int you can do this with other types it is just not defined this way for some reason, for example, you can use caml_copy_string to pass strings ```c
<d_bot> <Skid> const char* message(const char* str){
<d_bot> <Skid> static value * message_closure = NULL;
<d_bot> <Skid> if(message_closure == NULL) message_closure = caml_named_value("message");
<d_bot> <Skid> return String_val(caml_callback(*message_closure, caml_copy_string(str)));
<d_bot> <Skid> }
<d_bot> <Skid> ```
<d_bot> <Skid> The documentation is pretty non-existent for OCaml to C so expect to have an absolutely terrible experience 😆
favonia has joined #ocaml
<d_bot> <leviroth> @leonidas do `type outer := t` before defining the inner `t`
<d_bot> <wombat> Ha! Thanks! I'll give it a try and see how far I get. It's a pity though, because I thought that interop would be quite useful for transitioning legacy code.
<d_bot> <Skid> C to OCaml is great just not the other way around 🤷‍♂️
<d_bot> <Skid> Granted, if youre using C most of the time it makes sense to write the whole thing in C
<d_bot> <Skid> I use it to build OCaml libs for Python
<d_bot> <Skid> Other than that it isnt worth it to me
<d_bot> <wombat> you mean calling OCaml from C?
hackinghorn has quit [Quit: Leaving]
<d_bot> <Skid> In regards to what
<d_bot> <wombat> "C to OCaml is great"
<d_bot> <Skid> Calling C from OCaml
Stumpfenstiel has quit [Ping timeout: 252 seconds]
<d_bot> <wombat> oh I see. Indeed the next thing I need to do is calling OCaml from C, and that's where I'm stuck now 🙂
<d_bot> <wombat> I managed to compile my project with mixed C and OCaml code, I call caml_startup at the beginning, but now I'm trying to figure out how to call the OCaml function
<d_bot> <Skid> R u calling the mixed C and Ocaml from C
<d_bot> <Skid> If so u just have to extern the function at the top
Haudegen has quit [Ping timeout: 268 seconds]
<d_bot> <Skid> Call the start up and then call the function
<d_bot> <Skid> and compile the main.c and .so together
<d_bot> <Skid> Depending on how u compiled the .so u may need to include an ocaml runtime
<d_bot> <wombat> yeah, let's say I have `let foobar x y = x + y` in the .ml file. Now can I use `int foobar( int x, int y )` or something like that in C?
<d_bot> <Skid> Correct
<d_bot> <wombat> it crashes: Symbol not found: _foobar
<d_bot> <wombat> it's anyway funny because the types of x and y are generic in the .ml code, but I have to set them to int in the C definition
<d_bot> <Skid> send your code
[itchyjunk] has joined #ocaml
<d_bot> <wombat> sure: please have a look! github.com/miried/ocaml-q3ui
hendursaga has quit [Remote host closed the connection]
hendursaga has joined #ocaml
<d_bot> <Skid> ```ocaml
favonia has quit [Ping timeout: 268 seconds]
<d_bot> <Skid> let _ = Callback.register "foobar" foobar
<d_bot> <Skid> ``` add this to the bottom of the .ml
<d_bot> <Skid> and you need a wrapper C file did you just not include this in the github?
Tuplanolla has quit [Quit: Leaving.]
<d_bot> <Skid> also with this function, you said the types were generic but they arent, you will only be able to pass integers to it both from C and in OCaml due to the + operator
<d_bot> <Skid> if you want multiple type operators you can make custom types and define custom operators
<d_bot> <Skid> or override the existing ones but probably shouldnt do that
<d_bot> <Skid> i define ( +! ) to handle both floats and ints if i dont know what the input will be
<d_bot> <wombat> right, it's int.
<d_bot> <Skid> Anyway
<d_bot> <wombat> still crashing
<d_bot> <Skid> What now
<d_bot> <wombat> what wrapper C file do I need?
<d_bot> <Skid> How did u compile the .so
<d_bot> <Skid> Shouldve been a c program
<d_bot> <Skid> And the ocaml
<d_bot> <wombat> there's a main.c, no?
<d_bot> <wombat> `ocamlc -output-obj -o shlib.so main.c shlib.ml`
<d_bot> <wombat> you mean the code that calls the .so?
<d_bot> <Skid> to make the .so you have to compile a C wrapper script with the .ml
<d_bot> <Skid> no main.c
<d_bot> <Skid> ```c
<d_bot> <Skid> #include <stdio.h>
<d_bot> <Skid> #include <string.h>
<d_bot> <Skid> #include <caml/mlvalues.h>
<d_bot> <Skid> #include <caml/callback.h>
<d_bot> <Skid>
<d_bot> <Skid> double caml_sqr(double x)
<d_bot> <Skid> {
<d_bot> <Skid> static value * caml_sqr_closure = NULL;
<d_bot> <Skid> if (caml_sqr_closure == NULL) caml_sqr_closure = caml_named_value("caml_sqr");
<d_bot> <Skid> return Double_val(caml_callback(*caml_sqr_closure, caml_copy_double(x)));
<d_bot> <Skid> }
<d_bot> <Skid> ```
<d_bot> <Skid> something like this
<d_bot> <wombat> aha
<d_bot> <Skid> If youre return ints the return will be Int_val(..)
<d_bot> <Skid> And instead of caml_copy_double(x) i believe it is Val_int(x)
<d_bot> <Skid> May be lowercase V i forget id check the docs
<d_bot> <wombat> OK, i think i need some sleep 😅
<d_bot> <wombat> that's chapter 18.7.2 in the manual?
<d_bot> <wombat> i'm getting too tired
<d_bot> <wombat> OK, got it
<d_bot> <wombat> it works 😄