Existential types, object-orientation.

Marco Pil marcop@cs.kun.nl
Fri, 7 Nov 1997 16:26:22 +0100


Ovidiu Podisor wrote:
 > 'Dynamic' could be thus regarded as type with an unbounded number of
 > contructors (one for every 'real' type).
 >
 > This resembles the treatement of the 'Exception' type in ML, which is
 > again a type with an unbounded number of constructors *and* allowing
 > pattern matching.
 >
 > The way this is implemented in the Standard ML compiler is that the
 > tag is not a (small) integer but the memory address of a string
 > (the exception name).
 >
 > I wonder how the 'Dynamic' construct is implemented in Clean.
 >


The dynamic construct in Clean is implemented by a tuple of an value
and an encoded version of its type.

The types are not coded as integers, for two reasons:
1. There is more than just pattern matching: actually a full-blown
   type unification algorithm is executed at run-time. For this the
   type code has to have some structure.
   One can write the following functions:

    dynamicApply :: Dynamic Dynamic -> Dynamic
    dynamicApply (f :: a -> b) (x :: a) = dynamic (f x) :: b
    dynamicApply  _             _       = dynamic "Type Error" :: String

    dynI  = dynamic I :: (A.a: a -> a)
    dyn37 = dynamic 37 :: Int

   so that  'dynamicApply dynI dyn37'  results in  'dynamic 37 :: Int'.

2. Dynamic values are designed to persist beyond the borders of one
   application. And, as different programs can define their own
   datatypes (sometimes using the same name for different types),
   is necessary to encode the entire definition of all datatypes.


Regards,

Marco Pil