Monday, 23 February 2009

OpenSolaris, libc problem upgrading to b107

Just a quick note, upgrading from b106 to 107 left me with a foobared installation. It turns out that libc.so.1 ends up being an incompatible version for the Kernel.

Luckily the fix / work around is fairly trivial :)

On my machine it was roughly as follows:

beadm mount opensolaris-19 /a
rm /a/lib/libc.so.1
rm /a/lib/amd64/libc.so.1
pkg -R /a fix SUNWcsl
beadm unmount opensolaris-19

Reboot into opensolaris-19

Problem fixed!

The bug if filed here: http://defect.opensolaris.org/bz/show_bug.cgi?id=3562

But that's not all, I was then faced with a second problem - which turned out to be unsupported "stuff" left over in the /etc/X11/xorg.conf, in particular an "RgbPath" related config line - so this needed to be removed.

gdm then restarted

svcadm disable gdm
svcadm enable gdm

X11 logs can also be checked for further errors:

/var/log/Xorg.0.log

and viola, a graphical login appears at last...

For further details on b107 issues, see http://blogs.sun.com/alanc/entry/xorg_1_5_3_in


Update: Since these problems with 107, 108 has come out and it seems to address these issues, upgrading from 107 to 108 was completely painless and problem free!


Additional note:

Recently trying to install OpenSolaris 109 under VirtualBox 2.1.4 resulted in a frozen white screen before the graphical login. After some digging around and debugging the problem appeared to be an incompatibility with VirtualBox guest additions (GA).

I believe the problem is captured here (as a major fault).

http://www.virtualbox.org/ticket/3408

The work around / temp solution is to:

Boot into single user command line login (edit boot grub)
Remove VirtualBox GA installation

>pkginfo | grep -i box

>pkgrm SUNWvboxguest

Delete xorg.conf (/etc/X11/)

>rm /etc/X11/xorg.conf

Restart GDM (svcadm restart gdm)

Check logs:

>tail -f /var/log/Xorg.0.log

--

To install VirtualBox Guest Additions, select "Devices -> Install Guest Additions..."

Navigate to the installer resources (usually visible on the guest OS desktop)

>pkgadd -d ./VBoxSolarisAdditions.pkg


.

Sunday, 22 February 2009

Scala, implicit conversions

Today, a small posting on the implicit conversion feature of Scala.

Implicit conversions provide a powerful way to extend classes with new functionality, as if already built into the library or even the language itself. There are similarities with C# extension methods, but Scala's implicit conversion mechanism is more concise, flexible and maintainable.

To define an implicit conversion in Scala, the syntax is:

impict def myImplicitConversionName(n: type) = new MyConversionType(n)

The scenario is that you want to add some new method(s) or functionality to instances of an existing class for which may be final/sealed or where sub-classing is inappropriate or not possible / effective. Implicit conversions provide a very natural way to add functionality and to users it will appear as natural features of the language/library, no special syntax is required.

A good example is provided on the Scala site: http://www.scala-lang.org/node/226

Which is a good example because it shows exactly the reasons why you might want to extend the language or library. In this case, the type int provides a good candidate for extension - to add the ! factorial function as if it was natural function of the type int.

I wanted to expand on this example by providing a couple of examples of the same thing with further explanation.

Example 1: explicitly defining a class that provides the extensions and using it in the implicit def


object extendBuiltins2 {

// the important bit, it says there's an implicit conversion from int to Factorizer(n : int)
implicit def int2fact(n: Int) = new Factorizer(n)

def main(args : Array[String]) = {

// when the compiler sees this, it will first 'look for' ! method in int,
// when it doesn't find it, it will look for an implicit conversion on int that provides !
// which is of course exactly what we've just provided in the Factorizer class
println("10! = " + (10!))
}

// define ! as a method on a class that can be constructed from int - this class will
// provide the implicit conversion that supplied an implementation of ! on int
class Factorizer(n: Int) {

def ! = fact(n)
}

// the plain old factorial function
def fact(n: Int): BigInt =
if (n == 0) 1 else fact(n-1) * n
}


The key line that defines the implicit conversion is this:

implicit def int2fact(n: Int) = new Factorizer(n)

what we are saying is that there is an implicit conversion from type int to type Factorizer(int) - so where we have an int, if ! is called the compiler will look for ! on int and fail to find it. It will then seek an implicit conversion that can convert from int in order to provide this method ! - where it finds our implicit conversion definition that uses the Factorizer class to provide the implementation of !

One reason this is more powerful than extension methods is that a whole class of functionality can be added to an existing class without explicitly extending all the methods individually. This has important benefits when it comes to maintenance, if the definition of the extending class changes, you get those changes automatically, you do not have to modify all the extension methods to match.


Example 2: equivalent to the above example but using an anonymous class for a more concise solution where the explicit definition of a class is of no other use.


/* More concise way of adding ! as a method on int's - no explicit class definition */
object extendBuiltins {

implicit def int2fact(n: Int) = new {

def fact(n: Int): BigInt =
if (n == 0) 1 else fact(n-1) * n

def ! = fact(n);
}

def main(args : Array[String]) = {

println("10! = " + (10!))
}
}


This achieves the same this as example 1 but does so more concisely with the anonymous class, constructed using the convenient = new {} syntax.


.

Friday, 20 February 2009

Some interesting reading material and resources...

Some interesting reading material and resources...

Nothing much to see here, I just wanted to capture some links to interesting resources and reading material...

Papers and articles on Lambdas http://library.readscheme.org/page1.html

Functional Java library - http://functionaljava.org/

This library brings some functional programming constructs and library functions to Java, using either Java bgga prototype compiler based on JDK 7 or a more traditional anonymous class approach using standard Java 5. Both are JRE 5 compatible.

The Java Closures BGGA proposal http://javac.info/

.Net functional extension method library http://www.mono-project.com/Rocks

Apple iPhone Dev centre - http://developer.apple.com/iphone/index.action

http://www.iolanguage.com/


.

Wednesday, 18 February 2009

Currying, some examples and comparisons between C# and Scala

I'm back again! It's been some 3 weeks since my last blog - I've been in New York for 5 days and busy either side sorting out commitments and work and home.

So, in this posting I'd like to continue on the theme of Lamda and functional programming with some simple examples of Currying in both C# .Net (3.0+) and Scala.

I've tried to keep the examples equivalent as closely as possible but Scala has a few different options when it comes to syntax, so the Scala version is a little longer as it delves into these extra possibilities.

What is currying?

In a nutshell currying is "the process of reducing a function that takes multiple arguments into a series of functions that each take one argument"

Named after Haskell Curry - http://en.wikipedia.org/wiki/Haskell_Curry

A good explanation and definition can be found here http://www.haskell.org/haskellwiki/Currying

C# .Net example using C# 3.0+ syntax for Lamdas:


using System;

static class Program
{
static void Main()
{
Func<int, int, int> add = (x, y) => x + y;

var curriedAdd = add.Curry();

var curriedIncrement = add.Curry()(1);
var curriedDecrement = add.Curry()(-1);

Console.WriteLine("curriedAdd(10)(11) = " + curriedAdd(10)(11));
Console.WriteLine("curriedIncrement(41) = " + curriedIncrement(41));
Console.WriteLine("curriedDecrement(41) = " + curriedDecrement(41));
}

// a simple 2 arg curry template function
static Func<TArg1, Func<TArg2, TResult>> Curry<TArg1, TArg2, TResult>(this Func<TArg1, TArg2, TResult> f)
{
return a1 => a2 => f(a1, a2);
}
}

Results:

curriedAdd(10)(11) = 21
curriedIncrement(41) = 42
curriedDecrement(41) = 40

Notes:

  • The use of the var keyword for implicit typing
  • C# does not provide implicit functions/ partial application so a Curry template function for 2 arguments has been provided explicitly.
  • The Curry method is defined as an extension method so it appears as-if defined on add

Extension Method:

static Func<TArg1, Func<TArg2, TResult>> Curry<TArg1, TArg2, TResult>(this Func<TArg1, TArg2, TResult> f)

Take the above definition of a method, the this keyword in the (this Func...) parameter definition makes the function an extension method, so it can be used as if it were declared on the add function - to illustrate the point, without the extension syntax the Curry function could only be invoked in the regular way as follows:

var curriedAdd = Curry(add);

As an extension method, it's valid to use the original add.Curry() or Curry(add) syntax interchangeably.


Scala example:

package test;

object Currying {

def main(args : Array[String]) {

// regular syntax function add
def add(x : int, y : int) = {

x + y
}

// create curried add using 1 and -1 for increment and decrement by invoking partial function (a:int, _:int):int
var curriedIncrement = add(1, _ : int)
var curriedDecrement = add(-1, _ : int)

// test funtions
println("add(10, 11) = " + add(10, 11))
println("curriedIncrement(41) = " + curriedIncrement(41));
println("curriedDecrement(41) = " + curriedDecrement(41));

// curried syntax function add
def add2(x : int)(y : int) = {

x + y
}

// create curried functions using partial functions
var curriedIncrement2 = add2(1)(_ : int)
var curriedDecrement2 = add2(-1)(_ : int)

println("add2(10)(11) = " + add2(10)(11))
println("curriedIncrement2(41) = " + curriedIncrement2(41));
println("curriedDecrement2(41) = " + curriedDecrement2(41));

// special {} syntax
val res1 = add2(10) {
11
}

println("res1 = " + res1)

// more complex example
val x = 1
val y = 2

val z = add2(10) {

add2(20) {

x + y
}
}

println("z = " + z)
}
}


Results:
add(10, 11) = 21
curriedIncrement(41) = 42
curriedDecrement(41) = 40
add2(10)(11) = 21
curriedIncrement2(41) = 42
curriedDecrement2(41) = 40
res1 = 21
z = 33

Notes:
  • Scala supports f(x:int)(y:int) syntax for explicit curried functions
  • Scala supports implicit currying using partial application, using the placeholder _ syntax
  • Scala supports curried parameter in {} syntax for functions that appear to extend the language by taking parameter from the body of the call.



Update:


I realised that the way I'd defined the curried add functions in the Scala example is perhaps not as verbose or explicit as it could have been, and therefore perhaps less easy to understand or compare to the C# example. So here is another code snipped that shows a more verbose currying:


object Currying {

def main(args : Array[String]) {

def add3(x : int) = {

(y : int) => {
x + y
}
}

println("add3(1)(2) = " + add3(1)(2));

val res = add3(5){

3
}

println("res = " + res)
}
}


Results:


add3(1)(2) = 3
res = 8


Resourses:

Good explanation of currying in C#.Net http://diditwith.net/2007/08/15/TheArtOfCurrying.aspx


.