Polymorphic Variants
Pros:
- Outrageously polymorphic - use them any way you like, anywhere
- No declarations needed.
- 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.]
- 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.
- Lots of work for the type solver, because of the overly detailed types...
- ...thus the compiler runs slower
Pros:
- Cleanly declared datatypes. If we're really planning on doing systems programming with this, then declared datatypes are probably non-negotiable.
- 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.
- 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}...
- 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
(:empty)
(:elem (item 'a) int (stack 'a))
)
No comments:
Post a Comment