Slashdot Mirror


mod_caml Comes Of Age

Richard W.M. Jones writes "mod_caml is a set of bindings between Objective Caml and the full Apache API. mod_caml 0.6 has bindings for the Apache API and a full Perl-like CGI and templating library. There's only two things you need to know about Objective Caml: it's a modern, fully-featured and highly-optimised language, and it has a good tutorial so Perl/Java/C/C++ programmers can join in the fun."

12 of 43 comments (clear)

  1. Old caml page by notfancy · · Score: 2, Informative

    I've noticed that the home page is kind of slow. There's the original, old one at INRIA.

    HTH

  2. Improved linkage by fm6 · · Score: 4, Informative
    The link to Bagley.org gets diverted because Doug Bagley is a Slashdot-hater. You can read his summary of CAML by cutting and pasting
    http://www.bagley.org/~doug/shootout/
    into your address bar.

    Since CAML is a functional language, it's probably more productive to compare it with other functional languages than with more familiar procedural languages. Good stuff here and here. In this context, it makes sense to have a particular look at Guile, which is like mod_caml in that it implements a functional language as a means of writing application extensions.

    1. Re:Improved linkage by zangdesign · · Score: 2, Interesting

      I can see his point about hating /. - I read his reasons and they're all reasonable opinions. I pray god I never post anything that is even remotely interesting to /. as a whole.

      So far, no worries on that point.

      --
      To celebrate the occasion of my 1000th post, I will post no more forever on Slashdot. Goodbye.
    2. Re:Improved linkage by scrytch · · Score: 2, Insightful

      > Since CAML is a functional language, it's probably more productive to compare it with other functional languages than with more familiar procedural languages.

      Actually no, it's worth comparing against imperative languages, because caml has those features as well, like fully mutable variables (believe it or not, it's not something you take for granted in the FP world -- erlang's variables are set-once, and haskell has only bindings aside from monad 'do' blocks).

      Ocaml does have several annoying features though:

      * No machine types. You cannot express an unsigned 32 bit int in ocaml. This would make writing an IP4 stack in ocaml somewhat onerous. Similarly, there's no "raw" struct types you can finely pack.

      * No overloading at all. To implement the actual nuts and bolts of polymorphism, where the types diverge, you either have to use separate classes or pass along type information explicitly, something like a C++ traits object. Somewhat more palatable with closures, but the monomorphism restriction will then bite you in the ass. Modules are suggested as another mechanism, but I've yet to see actual code that makes them look anything like ad hoc polymorphism.

      * Supremely stupid compiler feedback. My favorite is "the expression is of type foo but is used here with type foo". Perhaps a warning about redefined types would have been handy?

      * Syntax gets uuuuuuugly when using polymorphic types and labels. Objective Labl has been in ocaml for years, but still feels bolted on.

      Personally, I want haskell and ocaml to get together and have a baby, but that's probably asking too much...

      --
      I've finally had it: until slashdot gets article moderation, I am not coming back.
  3. Re:O'Caml for Scripting? by notfancy · · Score: 5, Informative

    Actually, the procedural elements of OCaml are so strong than, other than syntax (which is more convenient for functional composition than for imperative sequencing), it is an absolute joy to program UN*X-type filters and transformers in it.

    OCaml has a very complete interface to UN*X (including sockets), supports native (POSIX) threading, lots of publicly available libraries exist from regexps to XML parsing, etcetera; so it can be (and is!) used for systems-level programming, much as you would use C or Java.

    The only thing that may put down programmers accustomed to the more forgiving, dynamic nature of scripting languages like Perl is that OCaml's type system is very strict, much more so than Java's. On the other hand, it has type inference, whereas you rarely if ever need to type-declare things (other than for documenting, that is).

    For the sake of example, this is how you would program wc in OCaml:

    type counts = {
    mutable lines: int;
    mutable words: int;
    mutable chars: int;
    }

    let wc fname =
    let inch = open_in fname
    and stat = { lines = 0; words = 0; chars = 0 }
    in begin
    try while true do
    begin
    match input_char inch with
    '\n' -> stat.lines <- stat.lines + 1;
    stat.words <- stat.words + 1
    | '\t'
    | ' ' -> stat.words <- stat.words + 1
    | _ -> ()
    end;
    stat.chars <- stat.chars + 1
    done with End_of_file -> ()
    end;
    close_in inch;
    stat

    Sorry for the ugly formatting, but Slash is unforgiving

  4. Re:O'Caml for Scripting? by wcbarksdale · · Score: 2, Interesting
    Only in a very limited sense could Python be considered strong typing. It's stronger than C, but most anything is, and you can do things like
    obj.feild = 4
    and in general, functions tend to care about whether their arguments have certain properties (you can apply something to them) rather than what type they are.

    And yes, type inference is absolutely necessary to have a workable strong-typing system - otherwise you end up with something as verbose as Java.

  5. Re:O'Caml for Scripting? by Richard+W.M.+Jones · · Score: 4, Informative
    I've tried to make mod_caml "scripting"[1] as simple as possible. There's a few example scripts in the manual here:

    http://www.merjis.com/developers/mod_caml/docs.sht ml

    Where possible I've gone for reducing the amount of code that you have to write in the common cases, based on a large amount of experience writing CGI scripts in Perl.

    At the moment we're missing a fully integrated database layer, but that's coming soon (the code is already out there, I just need to pull it in and do the persistent database connection stuff).

    Unfortunately I only get to work on this at weekends, but hope to have a Savannah page up soon so others can more easily contribute.

    Rich.

    [1] It's not really "scripting" as such. OCaml programs are bytecode compiled and dynamically linked into the bytecode interpreter which runs inside Apache. We also hope to get natively compiled loading working at some point.

  6. Not tail-recursive by r6144 · · Score: 3, Funny
    Here is a tail-recursive version:
    let process_line () =
    try let s = read_line () in
    (true, 1,
    List.length (Str.split (Str.regexp "[ \t]+") s),
    (String.length s) + 1)
    with End_of_file -> (false, 0, 0, 0)
    ;;

    let rec wc (lines, words, chars) =
    match process_line () with
    (true, l, w, c) -> wc (lines + l, words + w, chars + c)
    | (false, _, _, _) -> (lines, words, chars)
    ;;
    let lines, words, chars = wc (0, 0, 0) in
    Printf.printf "%d %d %d\n" lines words chars
    ;;
  7. Re:O'Caml for Scripting? by Richard+W.M.+Jones · · Score: 2, Informative
    Persistent cached data? So ocaml values can be instantiated once and then shared for the whole application like with a Java appserver?

    Yes. There's two ways to do this - either just create the value at the top level of the script. Top level functions in a script get called once when the script is first loaded. The run function (which you have to register) gets called on each invocation.

    Thus:

    (* foo is evaluated just once: *)

    let foo = expensive_function ()

    let run r = ... (* generate the page each time - see examples in manual*)

    let () = Registry.register_script run

    How far along is it? Does it have any kind of session support or is that up to the application programmer?

    It's still in beta, but please play with it! Without people playing with it it will be perpetually beta software.

    Rich.

    PS. You might want to continue this conversation by email because Slashcode sucks for posting code snippets ....

  8. Re:O'Caml for Scripting? by Richard+W.M.+Jones · · Score: 2, Interesting
    Erm, and the other way to do it is by loading a shared module. In httpd.conf:

    CamlLoad /path/to/my/module.cmo

    Then all scripts can access functions/values in Module.

    Rich.

  9. Re:O'Caml for Scripting? by Anonymous Coward · · Score: 2, Informative

    One can really do better in OCaml for scripting purposes than in the initial version:

    let lines, words, chars = ref 0, ref 0, ref 0 in
    Pcre.foreach_line (fun line ->
    incr lines;
    words := !words + List.length (Pcre.split line);
    chars := !chars + String.length line + 1);
    Printf.printf "%d %d %d\n" !lines !words !chars

    Pcre for OCaml is a very high-level and efficient string processing library that can do just about anything that Perl offers for string manipulation - only much faster :)

  10. Re:O'Caml for Scripting? by smallpaul · · Score: 4, Informative

    Only in a very limited sense could Python be considered strong typing. It's stronger than C, but most anything is

    There are a variety of languages that allow you to cast raw memory addresses to whatever you like: C, C++, Forth, Assembly language, many basics, etc. Those are weakly typed languages as the term was originally applied. Python is a strongly, dynamically typed language. And object has exactly one type and you cannot convince it to behave as another type merely by asserting it is that other type. When you try to get a Python object to do something disallowed by its type, you get an exception whereas in the languages described earlier the results are undefined.