Ocaml A Language Of Opportunity English Language Essay

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

OCaml was originally known Objective Caml. The main implementation of Caml is the OCaml language. OCaml was developed by Xavier Leroy and Jerome Vouillon along with a few others in the year of 1996. The point of OCaml is to extend the core Caml language using object oriented layer (hence Objective Caml or OCaml).

OCaml had evolved from Caml which is a "general-purpose programming language" (OCAML) designed with program safety and reliability. "It is very expressive, yet easy to learn and use. Caml supports functional, imperative, and object-oriented programming styles" (OCAML). Caml was developed and distributed by INRIA. INRIA is a "French research institute in computer science and applied mathematics, since 1985" (OCAML). Other languages that relate with OCaml is "the Caml Light system is a lightweight, portable implementation of the core Caml language. Because of its stable status, it is actively used in education. For most other uses, we recommend switching to its successor OCaml" (OCAML). OCaml has numerous useful functions.

OCaml has numerous useful functions. OCaml has been created in such a way that it puts combines the functional aspect, object-oriented programming object, and imperative programming in a ML-like type system. What this means is that the programming author does not need to be an expert with the language paradigm to effectively use OCaml. Viewing OCaml from afar, "it extends the core Caml language with a fully-fledged object-oriented layer, as well as a powerful module system, all connected by a sound, polymorphic type system featuring type inference" (OCAML). So OCaml is useful in many development related ways in its functions.

For a better understanding of OCaml and its functions think of it this way: "the OCaml system is an industrial-strength implementation of this language, featuring a high-performance native-code compiler (ocamlopt) for 9 processor architectures (IA32, PowerPC, AMD64, Alpha, Sparc, Mips, IA64, HPPA, StrongArm), as well as a bytecode compiler (ocamlc) and an interactive read-eval-print loop (ocaml) for quick development and portability. The OCaml distribution includes a comprehensive standard library, a replay debugger (ocamldebug), lexer (ocamllex) and parser (ocamlyacc) generators, a pre-processor pretty-printer (camlp4) and a documentation generator (ocamldoc)" (OCAML). This is useful for rapid experimentation and development.

OCaml has basic operations as well as some of its own that helps in its own way. The syntax is familiar with other compilers. To define a constant, OCaml uses the form: let pi = 3.1416;; Another noticeable difference is that one will notice the use of the "#" sign which is the input prompt. OCaml function definitions are essentially the same for most compilers. If one had a sum function, you would type: let sum (x, y) = x + y; or let add x y = x + y;; for the OCaml language. There are multiple reasons why one should use OCaml over most languages when compared to ML and lisp.

First reason why is that, "numerous Statistics benchmarks suggest the Ocaml compiler generates the second fastest code of any of the currently available compilers (gcc and the Intel C compilers being first") (Murphy, Kevin). There is also "A detailed comparative study of C++, IDL, Fortran, SML, Ocaml, Dylan, Erlang, Clean, Haskell, Lisp, Mathematica for scientific computing: And most importantly, the CAML version works, and it works properly every time" (Murphy, Kevin). Second, experiments that had taken place report that there were no memory leaks. Thirdly, the "quality of code generated by the OCaml compiler has been analyzed by the Intel VTune system and it show no pipeline stalls, maximum parallelism between integer and floating point units, and machine assembly code that is as good or better than can be achieved by hand coding" (Murphy, Kevin). Ocaml is also seen as equally terse as Lisp or even slightly better. However it needs a fair amount of support routines written to cover certain application needs. "Some of this is in C/C++ (very little) but most has to do with providing things like unwind-protect, generalized string handling, generalized list operations".(McClain, David) It produces very fast runtime code (not needed here) and quite reasonably sized DLL's and OCaml produces smaller binary than lisp. Lastly, OCaml system is widely available.

The OCaml language system is open source software on the OCaml website. The compiler for OCaml is "distributed under the terms of the Q Public License, and its library is under LGPL; please read the license document for more details. A BSD-style license is also available for a fee through the Caml Consortium. The latest release can be freely downloaded on this site, and its user's manual is also available on-line" (OCAML). OCaml is a well written language and is useful in the modern world.

OCaml has several special features and implementations that make it unique. Three particular aspects of OCaml that is important to iterate on is the principal option and principal type functionality, the pattern matching function and the most important; the REPL implementation used in OCaml.

An interesting aspect of OCAML is the principal option/principal type. Given a program, a type for this program is principal if it is the most general type that can be given to the given program. This is in the sense that all other possible types are specialization or instances of this type. For example, the program fun x -> x can be given the types int -> int and bool -> bool. with the given program, the polymorphic 'a -> 'a is a principal type (What Is A Principal Type?). An interesting fact about principal types is that a good number of type systems use it. However some type systems, principal types do not always exist. You have a program P with two possible types T1 and T2, none of them being more general than the other. For example, "in some systems where numeric operators are overloaded, the program fun x -> x + x can be given the type int -> int and the type float -> float, and there is no type that subsumes both" (What Is A Principal Type?). This is a problem for the inference engine. It is a problem for the inference engine because it means that it has to make an arbitrary choice which allows it to pick one of the possible types without knowing if it's the one the user intended for. If you have principal types in your system/code, the inference process does not need to make any choice. Another example of principal option and principal types;

"A class is a model for objects. For instance, a class counter can be defined as follows.

class counter = object val mutable n = 0 method incr = n < - n + 1 method get = n end;;" (What Is A Principal Type?).

Objects of the class counter have a mutable field n and two methods incr and get. The field n in this class is used to record the current value of the counter and is initialized to 0. The two methods are used to increment the counter and read current.

As for any other declaration, the OCaml system infers a principal type for this declaration:

"class counter: object val mutable n: int method get: int method incr : unit end" (What Is A Principal Type?).

In the modern world, a large part of the designers of programming languages have given up on the idea of principality. The reason is because they want to have more ambitious type systems where it is just too hard to seek principality. "Instead non-principal inferences are being used: it's already good if the inference engine can find some type" (What Is A Principal Type?). However the primary maintainer of the OCaml type systems, still cares about it very much, which makes it an interesting aspect of the OCaml programming language.

The third technical aspect of OCAML I thought was interesting is the pattern matching. It is similar to variant types but the ability to pattern match over them. For pattern matching to happen it needs two basic syntaxes which OCAML provides. "The match… with construct and the function… construct is the two basic syntaxes for pattern matching. The match ... with construct evaluates the given expression and then compares the result with a sequence of pattern matches:

match expression with

| pattern -> ...

| pattern -> ...

| pattern -> ...

The expression corresponding to the first pattern that matches is then evaluated and returned" (Introducing OCAML).

The pattern aspect of the match patterns contain multiple parts. Parts that match patterns contain include primitive values as well as tuples. Other things that match patterns would contain are records, variable names and their type constructors. Match patterns also tend to have catchall pattern denoted _ that matches any value. Sub patterns may contain alternatives, denoted pat1 | pat2 (Introducing OCAML). For example, the pattern 1 | 2 matches both 1 and 2. Another example where a function to invert a given state may be written:

"# let invert state =

match state with

| On -> Off

| Off -> On;;

val invert : state -> state = <fun>"( Introducing OCAML).

Here is an example where the context of the variant type representing a symbolic expression is tested where the following pattern match tests whether the expression e is an atom or a sum/product.

"match e with

| Int _ | Var _ -> true

| Add _ | Mul _ -> false

For example, Int 7 is an atomic expression:

# match Int 7 with

| Int _ | Var _ -> true

| Add _ | Mul _ -> false;;

- : bool = true" (Introducing OCAML).

Pattern matching is a vital aspect of the OCAML language. It is vital to the OCAML language because it underpins its expressive power. This expressive power that the pattern matching is what makes the OCAML language nice and user friendly.

OCaml is an interesting language with unique functions, features and implementations. These functions include the principal type and pattern matching which not only makes coding a lot easier but allows OCaml be an enjoyable and user friendly language. But there is an implementation that is unique to OCaml and that is a feedback loop known as REPL, which makes it easier on programmers/developers.

The REPL is a feedback loop which facilitates a unique style of development as well as the code exploration process a computer programmer would take (Church Michael). But it isn't just a normal process; REPL is a far more effective process than just processing the data and then reading back code without the REPL. A REPL is by far more superior to anything available that an IDE could provide. It is said that "A REPL is, in some sense, a simple but highly effective IDE. Properly built, it makes IDEs unnecessary" (Church, Michael). REPL is a useful technical aspect of OCAML because "REPLs make it possible to explore average-case code and comprehend it without dedicating massive amounts of time to the process, and this makes a huge difference. Aging modules can be refactored in the earliest stages of decay, long before they get anywhere near the "legacy horror" state" (Church, Michael). This enables interactions of functions. REPLs are extremely user friendly for it "also allow programmers to explore libraries and get a sense of their interfaces to be used" (Church, Michael). The REPL makes the OCaml language a real nice choice to learn about.

REPL is also useful because programmer's productivity in general is binary. What is meant by this is that, programmers tend to be in either a productive state of flow (1 in binary) or the programmer is in a disorganized and unproductive state resulting in lack of production (0 in binary). "In general it takes programmers about 15 to 30 minutes to enter flow, but once in it, they are immensely productive and moreover, quite happy" (Church, Michael). Usually in the real world, developers are usually the ones having to write the most code and always write the best code when in flow. Developers strongest trait is reading code when in this state as well (Church Michael). However most will quickly agree that a major problem is in reading other people's code. If the code seems complex and obscure to a fellow programmer then instead of doing something productive in the language a question is asked. Normally, "the question the reader is trying to answer often shatters the flow" (Church Michael). OCAML and its REPL function have solved this issue completely.

Programmers will understand and agree that flow is importance from experience. Programmers also know that flow becomes top priority as a programmer increases one's programming skills (Church Michael). Programmers will negotiate deals that are oriented toward a quiet working space for the maximum flow and productivity. Programmers tend to also ask for unconditional rights to turn down meetings instead of higher salaries so they may achieve maximum potential (Church Michael). "An experienced programmer can work at a 1.0 level (cranking out code of nominal additive value) without inspiration, but 1.5+ and especially 2.0+ level contributions require creativity and focus" (Church, Michael). With REPL, programmers could write amazing code and not have to be cut off from the modern world. "Experienced, elite programmers know that a REPL-less language is generally a dead-end. In a "green field" environment where the programmer controls the entire context in which his work exists, engaged writing of code in languages like Java is still possible- but engaged reading of code is out of the question" (Church, Michael). The most productive way to read code is to get the big-picture of it through interaction, and then examination of the code for implementation details after it has been achieved.

The REPL is most commonly used at the command-line. This is great because it allows the programmer to interact with code and evaluate what each piece does. At least half of what programmers must do is comprehend the different assets that other people have created (Church, Michael). REPL allows us to do this without context switch associated with having to read obscure code. "It enables "flowful", that is engaging exploration and later; "lazy reading" of code. (Lazy, in this sense, is a non-pejorative computer science term associated with doing only the work needed to solve a problem.)" (Church, Michael). Everything stated above brings a conclusion that REPL-less languages can't provide due to the lack of code that is seen "as a dead static thing that might be run against some dead static tests"(Church, Michael). This is not something a developer/programmer can interact with as he works.

"For example, in Ocaml, it's possible to get the full type signature of any module:

# module L = List;;

module L :


val length : 'a list -> int

val hd : 'a list -> 'a

val tl : 'a list -> 'a list

val nth : 'a list -> int -> 'a

val rev : 'a list -> 'a list

val append : 'a list -> 'a list -> 'a list

val rev_append : 'a list -> 'a list -> 'a list

val concat : 'a list list -> 'a list

val flatten : 'a list list -> 'a list

val iter : ('a -> unit) -> 'a list -> unit

val map : ('a -> 'b) -> 'a list -> 'b list



A programmer would know that it should look like this…

# List.length [1; 1; 2; 3; 5; 8];;

- : int = 6

# List.map (fun x -> x*x) [1; 1; 2; 3; 5; 8];;

- : int list = [1; 1; 4; 9; 25; 64]" (Church, Michael).

Any person could explore this code and get a sense of the big picture before actually reading and translating the code. This makes everything so much easier for reading code, especially sense reading code "is an order of magnitude which is easier and more engaging when one understands what one is looking at" (Church, Michael). Without the REPL, OCaml not only would make little difference comparatively with C++ and Java but it would make it so many different tasks that OCaml can do not usable.

The REPL implementation is the main piece a programmer should know when starting out on the OCaml language. This is because not only due to all the reason and explanations shown above but having the OCaml language with the REPL implementations allows for code to not have to be compiled. Instead, a programmer can just use #use "<filename.ml> which will tell OCaml to include a specified file when the top-level of the programming is running. It would also be beneficial for a programmer to include the extra modules that are compiled and the .cma files on the command-line. It would also be advised to have the #load "filename.cma" written on the top-level of the program. An example of this is shown below (similar to Michael, Church). A programmer will see that on page 1 example, the declaration of a module with a list is occurring. Then on page 2 example, a programmer will notice the usage of the REPL implementation without having to compile manually.

# module A = Alist;;

module A :


val length : 'a list -> int

val height : 'a list -> int

val width : 'a list -> int

val map : ('a -> 'b) -> 'a list -> 'b list


(Page 1)

A programmer would know that it should look like this…

# Alist.length [1; 1; 2; 3; 5; 8];;

- : int = 6

# Alist.map (fun x -> x*x) [1; 1; 2; 3; 5; 8];;

- : int list = [1; 1; 4; 9; 25; 64]"

# Alist.height [1;2;6;8;9];;

-: int = 5

# Alist.map (fun x -> x*x) [1; 2; 6; 8; 9];;

- : int list = [1; 4; 36; 64; 81]"

# Alist.width [1;2;4;5];;

-: int = 4

# Alist.map (fun x -> x*x) [1; 2; 4; 5];;

- : int list = [1; 4; 16; 25]"

(Page 2)

Example of same but not in REPL in C++

Shown below is an example of a C++ program that does something similar to the example shown above with the OCaml language. A programmer will notice that not only would one have to #include <iostream>, #include <string> but that the programmer will also need to #include <vector>. What is not shown but should be noted is that for C++, all headers and other files need to already be compiled and ready to go before the example below will work. In contrast, when a programmer does a program in OCaml language, the REPL implementation allows for flexibility and does not need to be compiled.

#include <iostream>

#include <vector>

#include <string>


using namespace std;




   vector<string> Test;


   Test.push_back("The # is 25");

   Test.push_back("The # is 85");

   Test.push_back("The # is 150");


   cout << "Index Loop Listed:" << endl;


   int ii;

   for(ii=0; ii < Test.size(); ii++)


      cout << SS[ii] << endl;


Compile: g++ exampleVector.cpp

Run: ./a.out


Index Loop Listed:

The # is 25

The # is 85

The # is 150

Without the REPL implementation in the OCaml language, the language functions and features like the principal type/option and pattern matching will not work how it should. REPL makes the OCaml language easier to read and write code as well as allowed a user-friendly environment for OCaml beginners. OCaml is a nice language to begin with, although obviously different from C++ and Java, but after learning some of the features and new implementations that OCaml possess, there is no doubt that a beginner programmer will become a OCaml expert in a matter of months.