OCamel, Oh what?

Started by
122 comments, last by GameDev.net 18 years ago
Quote:Original post by jsgcdude
So, there is no formal specification for ocaml and no official standardization of the language. No unicode support. Very few libraries available for it. Hmmm... This could be a problem.


If you mean "standarized" as in ANSI/ISO/ECMA, then you're right. A lot of languages have no specifications because they have a single implementation. Unlike some of them, OCaml is free software. You're free to fork it if you don't like the direction it's taking. As for a formal specification, I'm sure that the dev. team has one, but it is an evolving language. The grammar is documented.

You are right about Unicode: there is no support for it in vanilla OCaml. Various libraries provide "Unicode" (from just string support to conversions between encodings). Extlib and Camomile come to mind.

Very few libraries available for it? I guess it depends on what you call "very few". Next to Java, it's certainly the case. Places like the Caml Humps, the O'Caml Links Database, and tools like Godi usually provide everything I need. Debian also has a very extensive selection of packages right in APT. When none of them has what I need, wrapping an existing C library is usually fairly easy because OCaml has a decent FFI.


Hope this helps.
Advertisement
Quote:Original post by jsgcdude
So, there is no formal specification for ocaml and no official standardization of the language.


Yes. Note that very few languages have a formal specification (SML is one), most languages have an informal specification. The flip side of "no official standardisation" is that OCaml continues to evolve as new research work is added to it (e.g. objects, polymorphic variants, recursive modules, recursive types, streams, MetaOCaml and other variants). So OCaml provides many state-of-the-art features not found in most other languages (including SML) but some of them are unstable.

Quote:No unicode support.


http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=85

Quote:Very few libraries available for it.


This isn't specific enough to debate but I have never needed a library that wasn't already available. The only arguable exception that I can think of is the bindings to the GLU tesselator in LablGL. I had to correct them and the author adopted my changes almost immediately. Nothing new there though - just open source software at its best...

Cheers,
Jon.
On a related note that many may find interesting and useful is that there are programs which can take SML code and turn it into java bytecode. I've personally used MLJ with a pretty good degree of success -- personally I could not figure out how to make the callbacks work for doing some java library calls, such as GUI stuff with Swing, but you can also run a disassembler on it to see how your functional algorithm could potentially be written in java. It's weird but you can learn a lot from doing that.
Quote:Original post by jregan
bool AnalyzeReferenceSpending(MoneyTransaction trans) {ReferenceChain refChain = trans.GetReferenceChain();int totalRefSpending;int totalRefAvailable;for (iterator on refChain obj){totalRefSpending += (*iterator).GetSpent();totalRefAvailable += (*iterator).GetAvailable();}if (trans + (totalRefSpending - totalRefAvailable) > 0 )    return true;else return false;}



An OCaml equivalent might be:

let sum = fold_left ( + ) 0let analyzeReferenceSpending trans =  let chain = getReferenceChain trans in  let totalRefSpending = sum (map getSpent chain) in  let totalRefAvailable = sum (map getAvailable chain) in  trans + (totalRefSpending - totalRefAvailable) > 0


Cheers,
Jon.
Quote:Original post by wahoodra
My challenge for OCamlers.


Here's a MetaOCaml implementation (including the responses written by the MetaOCaml top-level):

# let isZ = ( = ) 0;;val isZ : int -> bool = <fun># let inc = ( + ) 1;;val inc : int -> int = <fun># let expr = .< isZ 0, if isZ (inc 10) then 1337 else 999 >.;;val expr : ('a, bool * int) code =  .<((((* cross-stage persistent value (as id: isZ) *)) 0),   if (((* cross-stage persistent value (as id: isZ) *))        (((* cross-stage persistent value (as id: inc) *)) 10)) then    1337   else 999)>.# .! expr;;- : bool * int = (true, 999)


Note that the type of the expression to be evaluated is correctly inferred to be "bool * int". However, this isn't a fair comparison because the above code provides a much richer type system to the target language and compiles the given expression to high-performance native code.

Here's a challenge: let's develop this interpreter to include support for functions. An OCaml implementation of such an interpreter is given on one of our web pages:

http://www.ffconsultancy.com/free/ocaml/examples.html

Cheers,
Jon.
BTW, how significant is whitespace in OCaml?
Could I "compress" the above code like this:

let sum=fold_left(+)0

Or

let totalRefSpending=sum(map getSpent chain)in

?
Or would it then recognize "sum=fold_left(+)0" as a single token?
Quote:Original post by Anonymous Poster
BTW, how significant is whitespace in OCaml?
Could I "compress" the above code like this:

let sum=fold_left(+)0

Or

let totalRefSpending=sum(map getSpent chain)in

?
Or would it then recognize "sum=fold_left(+)0" as a single token?


Both of those are valid OCaml, i.e. they are equivalent to the originals.

By convention, OCaml programs tend to use more whitespace than C/C++ programs because sequences of consecutive operators are themselves valid operator names (with associativity and precedence of that of the built-in operator given by the first character).

Also, you can remove the spaces in ( + ) but not in ( * ) because (* is the beginning of a comment. Note that there are similar caveats in C and C++.

For example, the Num module provides arbitrary-precision integer and rational arithmetic and uses operators with the suffix "/", i.e. +/, -/, */ etc.:

$ ocaml nums.cma        Objective Caml version 3.09.1# open Num;;# let rec fact n =    if n =/ Int 0 then Int 1 else n */ fact(n -/ Int 1);;val fact : Num.num -> Num.num = <fun># string_of_num(fact(Int 120));;- : string ="6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000"


I gave this example on Wikipedia's OCaml page:

http://en.wikipedia.org/wiki/Ocaml

Cheers,
Jon.
Didn't American ocamlers once vowed to change the name of the language to freedom-caml? Or is that just a myth?


:P
I wish the Americans really knew how much of their culture comes from the French. It's not just the Statue of Liberty; there is a lot of french influence on their history and culture.

Anyways, back on topic. First, the reason there is such a small standard library is that the language itself has so much functionality in it. Having tuples of arbitrary length, lists, and arrays as basic types makes many algorithms very easy to author. Type declarations, when they must be literal, are phenomenally easy.

I wish I had a good example for variant types on hand, though... I'm amazed no-one has made particular mention of them. C people, imagine having type-safe unions! These things are phenomenally useful in compiler construction, especially, where I've made the most use of them.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Quote:Original post by Wyrframe
I wish I had a good example for variant types on hand, though... I'm amazed no-one has made particular mention of them. C people, imagine having type-safe unions! These things are phenomenally useful in compiler construction, especially, where I've made the most use of them.


The two interpreters written in OCaml that I've posted on this thread both use variant types (albeit polymorphic variants). The factorial example uses the variant type constructor Int defined in the stdlib's Num module.

Here's a quick introduction to variant types in OCaml:

http://www.ffconsultancy.com/free/ocaml/variants.html

Cheers,
Jon.

This topic is closed to new replies.

Advertisement