Tuesday, February 17, 2015

Fixing an exception while installing ghostscript on MacOS X using macports

If you ever encountered the following exception while installing ghostscript using macports:

--->  Verifying checksums for ghostscript
Error: org.macports.checksum for port ghostscript returned: ghostscript-fonts-other-6.0.tar.gz does not exist in /opt/local/var/macports/distfiles/ghostscript
Error: Failed to install ghostscript

 The solution is very simple.

Simply download the missing file from here and place it manually in

/opt/local/var/macports/distfiles/ghostscript

This should fix it.

It took me awhile to find it and I would like to thanks this thread to pointing me to the right answer.


Friday, January 23, 2015

Installing OCaml for MacOS Noobie!

After successfully installing OCaml and Ocaide plugin for Eclipse on a  Mac, I now think it's straightforward, however, before I completely forget the pain involved in figuring out what goes where. I wanted to put down some pointers for noobies like me, in hope that it will save people some valuable time.

In general, ocaml.org is a great spot for finding any information related to OCaml. Installing OCaml is no different, you can read all about it here. There is tons of very useful and detailed information that I highly recommend anyone who is interested in OCaml to check out. In this post I just summarize the steps it took me to get from zero to OCaml installed (hero!).

Feel free to correct me or share your own personal successes and experiences.

The following steps worked for me:

xcode-select --install
  • brew install ocaml 
  • brew install opam - I encountered a problem and apparently I am not the only one - see details and solutions here.
    • Basically if after installing opam and trying to run it, you get "Illegal instruction: 4"  Try running:
 brew reinstall --build-from-source opam
      you can thank @avsm for the workaround.
Now that you have OCaml installed, let's discuss IDEs.

IDEs

Eclipse 


My IDE of choice is Eclipse1 ocaide plugin very useful. I used it on Ubuntu and thought that installing it on Mac might be a good way to go.

There are multiple guides (from Upenn, JSU) on the web on how to do just that, just make sure that you get all the prerequesite components such as Java 7 installed and ready to go.

Upenn CS120 provides a nice list for you to follow, make sure that you do! - thanks guys!

  1. Mac Users Only Install X11 libraries (needed for the OCaml graphics libraries to work):
    • Install Apple's XQuartz version, which is available here: http://xquartz.macosforge.org/landing/
    • Before moving on to the next step, make sure you LOG OUT of your user account so that the X11 install can complete.
    • Check to see that the directory /usr/X11/lib exists and that it contains the file libX11.6.dylib (this should have been created when you installed XQuartz). If this isn't the case, try reinstalling XQuartz and then contact the course staff for help.
    • [MM] Removed these instructions, because ~/lib isn't on the default PATH for Macs, and because with most recent SW updates this step isn't necessary.
    • From the terminal, in your home directory (e.g. /Users/stevez) create a directory called lib (if one does not already exist):
       > cd > mkdir lib 
    • Create a symbolic link in that lib directory to the libX11.6.dylib file:
       > cd ~/lib > ln -s /opt/X11/lib/libX11.6.dylib libX11.6.dylib 
    • Next, install the Java Development Kit (JDK) 7. You can download it from this this site (after accepting the terms & conditions). Make sure you do this step before installing Eclipse or OCaIDE.


[Update] As was pointed out by numerous stackoverflow posts like this one , Eclipse IDE tend not to recognise environment variables. The solution is to wrap the execution into a bash script. It works, however, I couldn't manage to make it work with the MacOS launch database. Even after the update of the Info.plist, Eclipse still won't recognise environment variables. At this stage, I run the script manually and it works.

Vim 

[Update] VIM grows on me. If you are interest to configure VIM to use OCaml, please follow these instructions by @avsm.

Here is how my ~/.vimrc looks like:
execute pathogen#infect()
let s:ocamlmerlin=substitute(system('opam config var share'),'\n$','','''') .  "/ocamlmerlin"
let g:syntastic_ocaml_checkers = ['merlin']
au BufRead,BufNewFile *.ml,*.mli compiler ocaml
execute "set rtp+=".s:ocamlmerlin."/vim"
execute "set rtp+=".s:ocamlmerlin."/vimbufsync"
execute ":source " . "/Users/Yan/.opam/system/share/vim/syntax/ocp-indent.vim"

You can then do some cool stuff as kindly shared here by Thomas Leonard:

Then you get some nice shortcuts:
  • \s switches between the .ml and .mli file
  • \c comments the current line / selection (\C to uncomment)
  • % jumps to matching let/in, if/then, etc (see :h matchit-install)
  • \t tells you the type of the thing under the cursor (if you compiled with -annot)
Thomas also adds
"Also, Vim can then parse the output of the compiler and jump to the correct location."
However, he doesn't say how to do that. I searched the web and eventually figured it out. So here is what you need:

  1. Makefile to compile you code
  2. Vim 7.4+ (earlier versions don't seem to support it.
  3. and the knowledge how to use vim's quickfix
Let's tackle these one by one:

1. Here is a sample of a Makefile:


NAME =
OCAMLBUILD = ocamlbuild -use-ocamlfind  -classic-display
PACKAGES = -package -tag thread

all:
                $(OCAMLBUILD) $(PACKAGES) filename.byte 

clean:
                rm *.byte

test:   
                $(OCAMLBUILD) $(PACKAGES) filename.byte 




2. Make sure you have an updated version of Vim.

3.  You can read the full documentation on it here, or check this quick and what I thought I useful, summary provided by this stack overflow answer - run: make and then:

:copen " Open the quickfix window
:ccl   " Close it
:cw    " Open it if there are "errors", close it otherwise (some people prefer this)
:cn    " Go to the next error in the window
:cnf   " Go to the first error in the next file

That's all folks! Enjoy.

Please let me know if above helped you or you whether you encountered other issues so I can update the post accordingly.

After some issues with PATH variables in Eclipse, I am seriously considering converting to Vim.

Tuesday, January 13, 2015

Work summary @ OCaml labs

In this blog I would like to summarise my contribution to some of the on going projects as well as highlight some of the open source development that I encourage the wider community to adopt and provide feedback.

First, I would like to acknowledge the incredible support and welcome I received from the OCaml labs  - lead by @avsm. Thank you for facilitating great and fun working environment.

Executive summary

My main contribution involved

1. Research and development of a graph-based service abstraction for PIH as part the UCN project.

Deliverables: 
  • Co-authored a whitepaper (draft) on how Moana-based service abstraction can be used to facilitate access control

2. Contributing to Ocaml.org - creating a teaching resource page  for the community which provides useful resources for people who would like to keep up to date with the relevant material and resources for teaching OCaml to masses, mainly at the Uni level.


3. Pubłished a position paper (Moana - A Case for Redefining the Internet Service Abstraction) Middleware for Next Generation Internet Computing (MW4NG).

Besides stated above contribution, I was able to participate and help organise in many interesting initiatives such as help set up with @amirmc start-up class meet ups for people to follow online Stanford course on "How to start a startup" as well participated in (initial) discussions with the Office of Postdoctoral Affairs to setup the first Cambridge postdoc's entrepreneurial club. I also learnt a lot about very exciting technologies being developed around the lab such as Mirage OS, Irmin, Jitsu and many others (here is the 2014 review)

Now, in more details...

While in OCaml labs my main role involved contributing to research and development of a personal information hub (PIH) as part of the UCN project. The main objective of the project is:

"... to develop and use the PIH platform and architecture to install the user as the owner and controller of his/her personal data, allowing personalization and immersion while preserving a user’s privacy."

This is an on going project with very promising outcomes and I believe that the technology developed as part of it can serve as the foundation for future information-centric systems. ''
The current two main building blocks are Mirrage OS for deployment and Irmin for Git-like persistent storage. Both very interesting system which I recommend to check them out.

After learning more about the project, I felt that it's very important to provide a very useful and efficient service abstraction for application and system developers to use in order to simplify the development on top of such a systems.

Enter Moana. Moana, an information-centric middleware for distributed services and applications. Moana offers a shared graph-based storage abstraction through which applications communicate with each other by appending and observing the shared graph. Arguably, this higher level of abstraction is much closer  to the developer world, which "allows developers to focus their efforts on functionality and spares their time fiddling with low level tasks, such as information synchronisation or storage". Applications can also specify access control policies which can be then enforced by the system. It would be great to see Moana like abstraction on top of Irmin which I believe will move the overall design  closer towards the overall goals of the UCN project.

So the main challenge was to develop and deploy Moana on top of Irmin. Both Mirage OS and Irmin (for many good reasons ) are written in OCaml so naturally I had to learn OCaml. This is how the work on MoanaML started. MoanaML is essentially implementation of Moana in OCaml. During the process I learnt a lot about type safe, functional languages and at this stage, this paradigm aligns extremely well with  Moana's AC mechanisms (I am working on the blog about it).

The current MoanaML has a couple of implementations including one for Irmin. I previously blogged about it.

I was also working on showcasing the the power behind MoanaML  and the ideas behind a PIH-type applications by developing a contacts app prototype. I will have a separate blog about it soon. In a nutshell, I developed contacts app which uses MoamaML code to store all contacts in the Moana graph, which also contains various access control policies for various requesters. Using js_of_ocaml I converted the contacts app (backend) code into JS which allowed me to reuse it in the JavaScript app development framework such as AngujarJS. I then used Ionic framework which translates JS apps to native mobile apps to create the front end. The current implementation requires further testing and development, nevertheless, the current ability to deploy OCaml code on the phone with nice native app like user interface, I believe, serves as a nice substrate for further for it.

2. I helped out to setup the Ocaml teaching resource page for the OCaml.org website.  The page contains resources for teaching OCaml. It also gathers information on where OCaml is currently being taught.


3. Read the paper :)


Thursday, November 20, 2014

Menhir



This blog post about  -  Menhir.


According to Wikipedia:

A menhir (French, from Middle Breton: men, "stone" and hir, "long"[1]), standing stone, orthostat, or lith is a large upright standing stone.
Coincidently, Menhir, is also the name for LR(1) parser generator for OCaml.
 
I followed the recommendation in the Real World OCaml to use it, rather than ocamlyacc:
"Menhir is an alternative parser generator that is generally superior to the venerable ocamlyacc, which dates back quite a few years. Menhir is mostly compatible with ocamlyacc grammars, and so you can usually just switch to Menhir and expect older code to work (with some minor differences described in the Menhir manual)

I found a few sources online that help you understand Menhir but it took me some time to get my head around it.

This blog (and this post in particular) is mainly for me to record my activities and a way to understand things better. Nevertheless, I hope that by going through and discussing the code I've written, will shorten the learning curve for some of you -  or the very least entertain you :)

On y vas! For my purposes, I started by parsing a simple n-tuple string, for the MoanaML code.
(?x,hasColor,?y,context)
Following the instructions in the Real World OCaml I knew that I had to create two files, a namely, Parser.mly and Lexer.mll

The parser file is used to construct and parse the grammar. You can define tokens and describe their required sequences.

For example, for the Moana tuple, I defined the following tokens:
%token STRING
%token VAR
%token LEFT_BRACE
%token RIGHT_BRACE
%token START
%token END
%token COMMA
%token EOF
 I the used them to define the required parsing sequence:
LEFT_BRACE; s = elem; COMMA; p = elem; COMMA; o = elem; COMMA; c  = elem; RIGHT_BRACE 
elem:
 | v = VAR {Variable v}
 | c = STRING {Constant c} 

The elem is there to differentiate constants and variables and consequently pass the parsed value into a relevant type constructor.

You also define the parsing function and its return type
 start < config .tuple > parse
Now once we have the parser, we can move to the lexer file.  Here we define rules using regular expressions in order to match, capture and convert strings into the previously defined tokens.

In my case:

rule lex = parse
  | [' ' '\t' '\n']      { lex lexbuf }
  | newline         { next_line lexbuf; lex lexbuf }
  | ","                 { COMMA }
  | "("                 { LEFT_BRACE }
  | "{"                {START}
  | eof                {EOF }

To do the actual parsing, we use:
Parser.parse_tuple Lexer.lex (Lexing.from_string s)
the Real World OCaml: helps us understand what's happening
[Lexing.from_string] function is used to construct a lexbuf [from a string], which is passed with the lexing function [Lexer.lex] to the [Parser.parse] functions.
That's all, folks!

Pitfalls.

I was expecting Menhir to provide me with nice exceptions to debug my code, as was promised in the Real World OCaml:
The biggest advantage of Menhir is that its error messages are generally more human-comprehensible, ...
but it didn't. At least, I couldn't find the way to invoke it.
In any case, the book does provide you with some code to make the debugging a bit easier.

Anyway, online regular expression editor is your friend - I used http://www.regexr.com. Use it to test your regular expression and adjust the lexer accordingly.

One more thing, the order of your lexer rules matters!

Finally, in addition to the the Real World OCaml book chapter, I also found very useful this example and this Mini Ocaml tutorial.

That's about it, very simple and elegant once you get your head around it.

Disclaimer: As I mentioned in the beginning, I am a newbie, so please let me know if I got some of the stuff wrong or there is a more efficient way of doing things. I am more than happy to hear from you guys!

Thursday, November 13, 2014

"Good news, everyone!" - OCaml.org teaching page is live!


I am helping out with a great initiative to create a Ocaml teaching page containing resources for anyone who would like to teach Ocaml.
 
The initial page is now up, however the work is far from over.

We would like to open it to the community to contribute any additional resources that we might have missed as well as providing feedback on the currently listed resources.

The best way to do that is by editing this wiki page or editing the page itself and creating a pull request.

Mailing list for teachers

We also created a mailing list for the teachers to exchange their experiences and common practices.

Look forward to hearing from you!


Friday, October 31, 2014

Irmin Irmin on the wall, who is the pretiest of them all - Moana

I am currently working on MoanaML.

My recent challenge was to code a backend based on Mirage/Irmin.

Ok, first thing first, what is Moana:

Moana, an information-centric middleware for distributed services and applications. Moana offers a shared graph-based storage abstraction through which applications communicate with each other by appending and observing the shared graph.
Moana supports two basic operations of ADD and MAP; add allows an application to persistently extend the global graph, and map provides a dynamically maintained mapping of the global graph to an application specific, internal sub-graph.
MoanaML is an implementation of the Moana primitives in OCaml.

Now, what is Irmin:

Irmin is a distributed database with built-in snapshot, branch and revert mechanisms. It is designed to use a large variety of backends, although it is optimized for append-only store.
 
Just  to recap, at the moment, MoanaML contains:

Here I wanted to summaries some of my initial progress on developing the Irmin backend for Moana.

To code Irmin-based backed we need to implement two Moana signatures, namely STORE and GRAPH

I am a noobie when it comes to Irmin, I am still trying to figure out all of its functionalities so proceed with caution :)

I recommend reading this blog post to get a better understanding on Irmin, also I found this short paper very insightful. For better code understanding, make sure you familiarise yourself with the lwt library.

So to use Irmin, I chose to use blocks of type String, just because it is provided; to have different types of values you need to implement your own IrminContents signature. This, from my understanding, involves letting Irmin know how to merge your values.

Ultimately, I want to use Irmin to store Moana tuples, not strings, so I will have to implement the Contents signature. For now, I convert Moana tuples into JSON string and store them in Irmin.

Now to the code...


Following Irmin examples, I first implemented t_of_view  and view_to_t functions to convert list of tuples to view and take list of tuples from the view, respectively. I later use these functions to store the views inside Irmin.

The cool thing about Irmin that all  my data is stored in git.

Thanks to this:

module Git =
  IrminGit.FS(struct let root = Some "/tmp/irmin/moana"
                        let bare = true
                           end)
 
(* Values are Strings * *)
module Store = Git.Make(IrminKey.SHA1)(IrminContents.String)(IrminTag.String)

You can also visualise it!