Wednesday, March 3, 2010

polymorphic vs normal variants

Just a quick summary of the pros and cons of each alternative.

Polymorphic Variants

  1. Outrageously polymorphic - use them any way you like, anywhere
  2. No declarations needed.
  3. The runtime can associate certain meanings with the tags on polymorphic variants. This makes them act more like lisp type tags. For example, the runtime can identify a list and correctly print it out, rather than {u0 1 {u0 2 {u0 3 {u1}}}}. [This is probably addressed in ML by treating lists specially, rather than having the 'basis' declare the list type.]
  1. Each variant consumes precious tag space. Since type tags are stored in a byte, and in particular a byte with the lower two bits zeroed, there are only about 59 such tags left.
  2. Lots of work for the type solver, because of the overly detailed types...
  3. ...thus the compiler runs slower
'Normal' Variants.

  1. Cleanly declared datatypes. If we're really planning on doing systems programming with this, then declared datatypes are probably non-negotiable.
  2. Tag values are consumed only within a datatype - so rather than having 59 total different types, we can have an unlimited number of types, each of which can have up to 59 different variants. (To clarify this with an example, the list datatype uses the tags 0 and 1, because it has two variants, 'nil', and 'cons'). A tree datatype will also use tags starting at 0, since the type system guarantees that a tree will never be confused with a list at runtime.
  1. The runtime has no idea what it's printing out, so you can get impenetrable output like this: {u0 16 {u0 1 0} {u0 15 {u0 1 0} {u0 14 {u0 1 0}...
  2. Datatype declarations are going to scare non-ML people off, which is a shame. Here's what the syntax currently looks like:
    (datatype item
    (:nt symbol (list (item 'a)))
    (:t symbol)

    (datatype stack
    (:elem (item 'a) int (stack 'a))
Sadly, I've chosen the same solution as OCaml - to leave both kinds in. If I could collect up the nerve to remove a lot of hard-earned code, I'd probably remove polymorphic variants.

No comments:

Post a Comment