[Mono-list] Making a ruby.net compiler

Michal Moskal malekith@pld-linux.org
Sun, 11 May 2003 19:37:52 +0200


[snip]

Few more thoughts.

1. why should we optimize (special-case) the particular get_type call?

Because it's *very* important in any language that has some kinds of
variant (discriminating union) data structure, which would be compiled
to subclasses. For example list is defined in ML:

type 'a list = Cons of 'a * 'a list | Nil

Which would be compiled to following Generic C#:

class list<A> {}
sealed class Cons<A> : list<A> {
  A field_1;
  list<A> field_2;
}
sealed class Nil<A> : list<A> {}

And functions like (warning: this isn't optimal version :-)

ML:

let length = function
    Cons (x, xs) -> 1 + length (xs)
  | Nil -> 0

C#:

int length<A>(list<A> x)
{
  if (x is Cons<A>) {	// (*)
    return 1 + length(((Cons<A>)x).field_2);
  } else {
    return 0;
  }
}

Here another issue should be raised, wrt Generic C#. It should be
possible to write something like "if (x is Cons) {" at (*), because
there is no need to check if type parameter A is the same.

But this is just a comment.

2. bit patterns Fergus Henderson was talking about could be used to use
   jump table in pattern matching. For example:
 
type t = 
    Case1 of int
  | Case2 of ...
  | Case3 ...
  ....
  | Case23

and then you have function:

let f = function
    Case1 (i) -> ...
  | Case2 (...) -> ...
  ...
  | Case23 -> ...

you would need to have 23 successive equality tests. It would be better
to use jump table, but I'm not quite sure if it's at all possible in
CIL. Anyway it can be done by having compiler-generated "int get_tag()"
method in each of CaseN classes.

Anyway this jump-table stuff doesn't present that-big performance hit
as calling runtime function for each typeof.

3. if you are familiar with ILX, you should remember .classunion type
   there, that just did discriminating unions.

But I believe it isn't very good idea to put it in Mono, since it's
very limited (namely it seems to fit OCaml variants, but not SML
variants (things after of can have names), or OCaml polymorphic 
variants (one polymorphic variant can be in several types)).

-- 
: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::::: Wroclaw University, CS Dept : {E-,w}-- {b++,e}>+++ h