Daily Archives: November 28, 2012

Ocaml 2 HTML

As I’m often adding OCaml code to blog posts I really wanted to find an automatic way to add syntax highlighting. The first solution that I have found is caml2html but I’m still looking for something better … suggestions welcome

System programming in OCaml – Part 1

This series of articles will follow my journey through “Unix system programming in OCaml”, available as a pdf here. After this series, I hope to move onto a series on Mirage system programming in OCaml and working with Signposts (a framework for managing end-to-end connectivity between devices using DNS infrastructure for signalling)

Introducing the Sys and Unix

The modules that give access to the system in OCaml are:

  • Sys – functions common to all OS’s that run OCaml
  • Unix – functions specific to unix

To make use of these modules in some OCaml code, we can add open Sys or/and open Unix to the top of the OCaml program.

NOTE that Unix and Sys can overwrite some of the functions in the Pervasive module, so to access a function from the Pervasive module you need to add Pervasives. in front of the function call.

When using ocamlc to compile OCaml programs that made use of  Unix, you need to pass unix.cma to the compiler e.g.

$ ocamlc unix.cma hello.ml -o hello

The same is not true of sys.cma

PASSING INFORMATION TO YOUR PROGRAM

Now we are going to consider 2 different ways of passing information from your terminal to your ocaml program

1) Passing the information as arguments to the executable. For this we are going to use Sys.argv which will return a string array of arguments passed to the program. The first index of the argument array that we use here is 1, this is because the 0th element in the array is the command used to call the program. Consider the following simple example:

open Sys;;
print_string argv.(1)
Now provided that at least one argument is passed to the program, this argument will be printed. We can now extend this program, to print the number of arguments and then exactly what the user will have just entered
open Sys;;

let args = Array.length argv;;
print_int args ;;
print_newline ();;
for i=0 to args-1 do
print_string argv.(i);
print_char ‘ ‘;
done;

2a ) Passing information using the environment variables, this can be fetched from within the OCaml programing using Sys.enviroment, this will return as array of environment variables

open Unix;;

let env = environment();;
for i=0 to (Array.length env) -1  do
print_string env.(i);
print_newline);
done;

Don’t forget then when you compile this, you need to add unix.cma to the command as we are making use of the Unix library

2b ) We can also lookup the values of environment variables using the Sys.getenv (or Unix.getenv) functions that takes a variable name and returns the value associated with it

open Sys;;

let var = argv.(1);;
print_string (getenv var)

Exceptions

Unless otherwise stated in the documentation, all function in the Unix module raise the exception Unix_error.

Exception Unix_error of error * string * string

The first argument is the error code, the second argument is the name of the system call that raised the error and third argument identifies the object on which the error was called.

If an exception reaches the top-level then the program will halt. The Unix module provides a function for clearly describing exceptions. We can make 2a use this function as follows:

open Unix;;

let prog () = 

  let env = environment();;
for i=0 to (Array.length env) -1  do
print_string env.(i);
print_newline);
done;

 done;

 handle_unix_error prog ();;

LaTeX – The IEEE Surveys & Tutorials Way (Pt 1)

This article aims to give an overview of setting out a survey for submission to an IEEE Journal.

For this article, I will be using ubuntu 12.10, 32 Bit and Vim as my text editor.

IEEEtran is the offical LaTeX class for authors of IEEE transaction journals and coferences

My primary source of information for this article is the latest version of the IEEEtran package IEEEtrans director. Other helpful information is available here.

INSTALLATION & SETUP

The essential book on LaTeX

The IEEEtrans directory (as linked above) includes a “bare bones example of a IEEE journal, called bare_jrnl.tex. We will now use the IEEEtran LaTeX class to compile this .tex file into a pdf file.

To find out the location that we need to place IEEEtran.cls in, use:

$ locate article.cls

For me, the first location returned is /usr/share/texlive/texmf-dist/tex/latex/base/article.cls

I now need to move the IEEEtran.cls that I download into the same directory as article.cls. So move the the IEEEtrans directory and execute:

$ sudo cp  IEEEtran.cls

You can now tell Tex about this new LaTeX class using:

$ sudo texthash

Latex is already installed on my system but to allow us of the IEEEtran class, you may also need:

$ sudo apt-get install texlive-fonts-recommended

If you get an error of the form:

Font OT1/ptm/m/n/9=ptmr7t at 9.0pt not loadable: Metric (TFM) file not found
Then it is likely that you need to install the texlive-fonts-recommended package.
Now we are going to compile the bare_jrnl.tex, by moving to its directory and excauting:
$ latex bare_jrnl.tex
You can now view the outputted document using:
$ xdvi bare_jrnl
NEXT STEPS
Now you have a bare bones copy of your paper, you can customize it to meet your specific requirements and add the content of your paper. I will look at this in more detail tomorrow. is the Information for Authors