technotroph

random thoughts about mozilla hacking and the computing world

Collection of FP Articles by Paul Callaghan

Thinking Functionally with Haskell

Dependent Types

OCaml, ocamlfind and camlp4 quirks

I’ve been playing around with OCaml for my final year project. The language is awesome but the ecosystem seems lacking in many ways. Compared to other languages like Ruby, Python and even Haskell, OCaml doesn’t seem to have a lot of people using and talking about it.

The biggest problem I find when using ocaml is the build system. There are a lot of tools to simplify the build process. There’s ocamlbuild and ocamlfind. But I find that the documentations aren’t very helpful. For example, I can’t find any documentations about writing META files for ocamlfind. Neither can I find any guides about writing _tag files for ocamlbuild. I guess the best way around this is for the users of these tools to post their own guides and document their issues. Sp I’m going to write this post so that I can save another user the headaches that I’ve went through.

For my final year project, I’m working with Jeremy Yallop’s deriving library. The library uses camlp4 (original syntax). To install the library to your system, install opam and execute this command:

$> opam install deriving-ocsigen

Opam is a package manager for OCaml libraries. After installing, make sure you have findlib installed. Then test your installation:

$> ocaml
        OCaml version 4.00.1
# #use "topfind";;

You should see a menu with some commands to get you started with findlib. Next execute these commands in your ocaml toploop:

# #camlp4o;;
# #require "deriving-ocsigen.syntax";;

You should see some output showing that the library’s path is added to ocaml’s search path. Next, let’s try to evaluate some code that use the deriving library.

type point = { mutable x : int; mutable y : int }
     deriving (Eq, Show);;

If the above code is successfully evaluated, then you should have the deriving library installed on your system. To compile code that uses the deriving library, you have to use ocamlfind telling it to use the camlp4o (original syntax) and to use the deriving-ocsigen.syntax package.

ocamlfind ocamlc -package deriving-ocsigen.syntax -syntax camlp4o -c point_seq.ml -o point

That’s it. Hope I’ve saved you some headaches when using ocamlfind with libraries that use camlp4. I believe the above commands should work with other libraries that use camlp4 (sexplib) too.

Python Closures and the Python 2.7 nonlocal Solution

In python, functions are first class. A language with first class functions allows functions to be passed as a variable and returned from a function just like other first class data types like integers or floats. Some notable language with first class functions are Python, JavaScript, Scheme and Haskell.

Therefore in python, it’s possible to do things like this:

def raise_to_base(n):
  def my_pow(x):
    return x**n
  return my_pow

>>> pow_base_5 = raise_to_base(5)
>>> pow_base_5(2) # => 32

(above code is taken from Python Closures Explained)

I have created a function that returns another function. And the inner function is known as a closure. A “closure” is an expression (typically a function) that can have free variables together with an environment that binds those variables (that “closes” the expression). With closures, we can do other neat things like this:

def outer():
  y = 0
  def inner():
    nonlocal y
    y+=1
    return y
  return inner


>>> f = outer()

>>> f()
1
>>> f()
2
>>> f()
3

The inner function now becomes something like a method in OOP. It controls the way the variable y is accessed from the outside. But there’s a problem when we want to do this in Python 2.7. The above code works only in Python 3.x. The nonlocal keyword does not exist in Python 2.7. To solve this problem, we can use dictionaries (or we can also create another object) to store the y variable in a namespace that the inner function can access.

def outer():
  d = {'y' : 0}
  def inner():
    d['y'] += 1
    return d['y']
  return inner

Though IMO, it’s kinda weird that we can’t access the non local variable but we can access the dictionary.

Connecting to NUS wifi on Arch

I recently have to move to arch due to some annoying gcc problems on the OSX of my macbook but it seems that configuring the wifi to connect to the NUS network on arch isn’t that easy.

The NUS wifi network is configured with WPA2 enterprise authentication. I referred to Tech That!’s blog post to configure it.


First, you have to get the netcfg and the wpa_supplicant packages.

sudo pacman -S netcfg wpa_supplicant

netcfg allows you to configure your wifi according to profiles and manages the profiles for you. wpa_supplicant is used for configuring the authentication.
Next, copy /etc/network.d/examples/wireless-wpa-config to /etc/network.d/nus. In this example, I shall name it as nus.

sudo cp /etc/network.d/examples/wireless-wpa-config /etc/network.d/nus

Then, make sure your /etc/wpa_supplicant.conf looks like this:

ctrl_interface=/var/run/wpa_supplicant

network={
ssid="NUS"
key_mgmt=WPA-EAP
eap=PEAP
group=CCMP
pairwise=CCMP
identity="NUSSTU\"
password=""
priority=1
ca_path="/etc/ssl/certs"
phase2="auth=MSCHAPV2"
}

Lastly, start the netcfg profile. The profile name should correspond to the file name of the profile configuration file in /etc/network.d/.

sudo netcfg nus

Javascript Shell in Your Terminal

I’m currently working on the Sage Notebook and I’ve been writing code in javascript. To test snippets of js code, I usually open up either firebug or the dev console on Chrome to test it. Which is pretty annoying. I have to wait for the browser to load, then I’ve to wait for firebug to load, then I have to click on the console prompt in firebug. So, I’ve decided to look for a quicker solution to my problem. I’ve previously seen a talk that a web developer gave (it’s really funny, you should watch it!) and he gave live demos of javascript running on his command line. Kinda like a javascript repl. So I’ve decided to look for it and I found rhino.

Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users. It is embedded in J2SE 6 as the default Java scripting engine.

The best thing about rhino is that it has a javascript shell. (IMO, that’s the best thing about it. It comes with other cool things like a debugger by the way.)

Setting Up Rhino (On the OSX)

First, download Rhino. Extract it. Then, create the ~/Library/java/Extensions directory if it’s not created yet. Lastly, copy js.jar from the rhino folder to ~/Library/java/Extensions. Or you can copy and paste the following commands to your Terminal.

mkdir -p ~/Library/java/Extensions
cp ~/Downloads/rhino1_7R3/js.jar ~/Library/java/Extensions

To test it, execute this in the Terminal:

java org.mozilla.javascript.tools.shell.Main

You should see this in your Terminal:

Rhino 1.7 release 3 2011 05 09
js>

And..you’re done. Oh a quick tip, instead of typing that long command to your terminal, you can alias it. You can do so by putting this in your ~/.bash_profile:

alias js="java org.mozilla.javascript.tools.shell.Main"

Enjoy!

Note: If you want to have readline capabilities like line editing, history, etc, you might want to put this in the ~/.bash_profile instead:

alias js="rlwrap java org.mozilla.javascript.tools.shell.Main"

Are You a Git Junkie?

GitHub
Yup! You can still contribute to Mozilla even if you prefer git over mercurial. Here are the repositories for various mozilla projects.

Memoize it, the Python way!

I am currently reading Python Algorithms by Magnus Hetland Lie. It’s an awesome book about coding algorithms in python. You should check it out. One of the things that I thought it was really interesting is the use of decorators when memoizing in python.

In case you don’t know, memoization is the use of caches to retrieve previously computed values to reduce the time complexity of algorithms.

For example, this is the code for to compute the nth fibonacci number:

def fib(n):
    if n == 1: return 1
    return fib(n-1) + fib(n-2)

The running time for this piece of code is exponential. The memoized version of the function goes like this:

f = [-1]*n
def pre_fib(n):
    f[0] = 1
    f[1] = 1
    fib(n)
def fib(n):
    if f[n] != -1: return f[n]
    return f[n-1] + f[n-2]

This version of the fibonacci function is polynomial. The key lies in the use of the array to retrieve previously computed values instead of computing it again.

Decorate Your Python

In Python, we can transform functions using decorators. It is a design pattern that allows the programmer to add behavior to existing objects. To decorate our fibonacci function, we need to first define what behavior we want to add to the function. This is how we define the decorator:

from functools import wraps

def memo(func):
    cache = {}
    @wraps(func)
    def wrap(*args):
        if args not in cache:
            cache[args] = func(*args)
        return cache[args]
    return wrap

This piece of code simply wraps the function that we want to memoize and calls it if the value we are looking for is not in the cache. If it is in the cache, it simply returns the value from the cache. Here comes the beauty of using decorators to memoize the fibonacci function. IMO, it’s really elegant. See it for yourself:

@memo
def fib(n):
    if n<2: return 1
    return fib(n-1) + fib(n-2)

That’s it! It’s really easy. Now simply call the fib function to see it in action!

The Best Developer Job Advertisement Ever?

I stumbled upon this while working on my networking assignment. I made a http connection to my blog and this is what I saw in the http response headers:

X-hacker: If you're reading this, you should vist automattic.com/jobs and apply to join the fun, mention this header.

Cool huh.

Python, The Lazy Mathematician’s Langauge

I was supposed to compute a exponentiation modulo 10 table for my networking homework. The question went like this:

Write a Exponentiation Modulo 10 table for 0 to 9. That is x = 0 to 9, y = 0 to 9 and the table values are x^y mod 10.

After computing the first few values, I got tired and fired up my python shell to get the computer to compute for me. This was my code:


>>> for x in range(10):
... print [x**y % 10 for y in range(10)]

This is the output:


[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 2, 4, 8, 6, 2, 4, 8, 6, 2]
[1, 3, 9, 7, 1, 3, 9, 7, 1, 3]
[1, 4, 6, 4, 6, 4, 6, 4, 6, 4]
[1, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[1, 6, 6, 6, 6, 6, 6, 6, 6, 6]
[1, 7, 9, 3, 1, 7, 9, 3, 1, 7]
[1, 8, 4, 2, 6, 8, 4, 2, 6, 8]
[1, 9, 1, 9, 1, 9, 1, 9, 1, 9]

Not bad huh. I am pretty impressed that python can do it in just two lines of code.

I Wanna Flip Bits, Not Burgers!

After talking to mentors throughout this week, I finally have two proposals for my GSoC application. I will be applying for SageMath and Thunderbird.

The Matlab Alternative

I’m really excited about contributing to Sage. It’s a open source alternative to popular proprietary products such as Matlab and Mathematica. What’s really cool about it is that it’s in my favorite language, python! I will be working on improving the SageNB usability. See my a draft of my proposal here.

Automate, automate, automate

As for Thunderbird, I am going to work on their automated email ISP configuration manager. Specifically, I’m gonna work on the database. They call it the ISPDB. It’s a database that the ISP configuration manager queries for server configurations of email domains. The user simply have to supply their email address and the manager will query for the configuration (server address, port, protocol, etc) of the email domain. Currently, there’s no way for users to submit configurations to ISPDB directly, so that’s what I’m going to implement for this summer. Another feature that I’m gonna implement is to allow users to edit (or propose changes) to the configurations.

Things are kinda hazy now but I think things will get clearer soon as I talk to the mentors. Hope my proposals get approved. ;)

Update: The url for the svn code repo is at
http://svn.mozilla.org/mozillamessaging.com/sites/ispdb.mozillamessaging.com/trunk/ispdb/

Follow

Get every new post delivered to your Inbox.

Join 165 other followers