tags:

views:

639

answers:

3

Could you, please, give a code snippet showing how to use Lua embedded in OCaml?

A simple example could be a "Hello, World" variant. Have OCaml prompt the user for a name. Then pass that name to a Lua function. Have Lua print a greeting and return the length of the name. Then have OCaml print a message about the length of the name.

Example:

user@desktop:~$ ./hello.opt

Name? User

Hello, User.

Your name is 4 letters long.

user@desktop:~$

[Edit]

As a non-C programmer, could I implement this without having to write an intermediary C program to pass the data between Lua and OCaml?

Following is a theoretical idea of what I would like to try. Unfortunately, line 3 of ocaml_hello.ml would need to know how to call the function defined in lua_hello.lua in order for the code to be valid.

lua_hello.lua Defines lua_hello, which prints an argument and returns its length.

1  function lua_hello (name)
2    print ("Hello, "..name..".")
3    return (string.len (name))
4  end

ocaml_hello.ml OCaml prompts for a name, calls the Lua function, and prints the return value.

1  let () = print_string "Name? "; flush stdout in
2  let name = input_line stdin in
3  let len  = Lua_hello.lua_hello name in
4    Printf.printf "Your name is %d letters long." len; flush stdout;;
+4  A: 

I'm not aware of a mature set of bindings for embedding the C implementation of Lua into OCaml. An immature set of bindings was posted on the Caml mailing list in 2004.

If you want to use the ML implementation you can find some examples in a paper called ML Module Mania. The ML implementation, unlike the C implementation, guarantees type safety, but to do so it uses some very scurvy tricks in the ML module system. If you are asking basic questions, you probably want to avoid this.

In your example it's a little hard to guess where you want the function to come from. I suggest you either ask for a C example or give people a C example and ask how it could be realized in OCaml (though I think bindings are going to be a problem).


Edit

In response to the revised question, it's pretty complicated. The usual model is that you would put Lua in charge, and you would call Objective Caml code from Lua. You're putting Caml in charge, which makes things more complicated. Here's a rough sketch of what things might look like:

let lua = Lua.new()  (* create Lua interpreter *)
let chunk = LuaL.loadfile lua "hello.lua" (* load and compile the file hello.lua *)
let _ = Lua.call lua 0 0 (* run the code to create the hello function *)
let lua_len s =
  (* push the function; push the arg; call; grab the result; pop it; return *)
  let _ = Lua.getglobal lua "lua_hello" in
  let _ = Lua.pushstring lua s in
  let _ = Lua.call lua 1 1 in
  let len = Lua.tointeger lua (-1) in
  let _ = Lua.pop lua 1 in
  len

let () = print_string "Name? "; flush stdout
let name = input_line stdin
let len  = lua_len name
Printf.printf "Your name is %d letters long." len; flush stdout;;

Again, I don't know where you'll get the bindings for the Lua and LuaL modules.

Norman Ramsey
I will definitely read your paper this weekend.Also, I have tried to clarify my question a bit more.Unfortunately, I do not know C. I had been trying to avoid learning it. However, if I must learn it to improve my programming, I guess I can bite the bullet and look for some C docs.
ldfritz
A: 

norman, would you mind giving us an example for the other way around: Putting Lua in charge and calling Ocaml. The lack of C would not be a requirement ... thanks a lot ben

A: 

Ben,

On further reflection, I'm not sure if you can do this with the official C implementation of Lua, because I think OCaml believes it owns main(). You'd have to find out if OCaml could be packaged as a library from a C main program.

For an example of putting Lua-ML in charge, you can get Lua-ML standalone from Cminusminus.org, and you can also check out the examples in the paper on Lua-ML as well as the source code to the QC-- compiler itself.

Norman Ramsey