Sun Dec 18 12:45:19 EST 2011

Structure get/set and _lambda / _app

Maybe it really is simpler to do this reusing _lambda and _app.  There
is a very nice type correspondence between:

    _lambda <->  _structGet  (binding in)
    _app    <->  _structSet  (binding out)

Both have quite heavy Code implementations already.  Can they just be
placed on the same plan so this code is reused?

Maybe the thing is to extend the current way of passing arguments into
using structs directly?

Wait..  What can be done is to cheat and make flat C struct objects
into primitive types.  In fact, it really doesn't matter to the
Term/Code system as long as assignment works for both statement and
declaration.  Yep it's allready just there, what is needed is support
as TMLprim and a pack/unpack (de)structuring operation part of Loop,
probably simplest to do this in one go.

It looks like what is necessary is to 

   1. define Array instances of Struct: (,) and Atom
   2. Allow struct elements to reside in variables.

The first one seems simple if TMLword (was TMLprim) is extended to
struct types, which doesn't seem like a good idea.

Stuck... Let's make some examples.  Haskell and C type correspondence
using the canonical tuple naming from before:

  TMLprim: Tfloat                              // float 
           Tint -> Tfloat                      // float *

  Struct:  Atom Tfloat                         // struct tuple_f0
           (Atom Tfloat, Atom Tfloat)          // struct tuple_f0_f0
           Tint -> (Atom Tfloat, Atom Tfloat)  // struct tuple_f0_f0 *

What about making struct ref/deref fit in array access (ignoring
representation r) ?

  (Tint -> (Atom Tfloat, Atom Tfloat))   -- pointer to struct
  -> Tint                                -- member index
  -> Tfloat                              -- member
  -> m ()

  (Tint -> (Atom Tfloat, Atom Tfloat))   -- pointer to struct
  -> Tint                                -- member index
  -> m Tfloat                            -- member

Almost but not quite..  It really should be done all at once:

  (Tint -> r as)       -- representation of (array of struct)
  -> Tint              -- index
  -> r_as              -- struct of representation
  -> m ()

  (Tint -> r as)       -- representation of (array of struct)
  -> Tint              -- index
  -> m r_as            -- struct of representation

So maybe it really can just be an instance?  Let's try.

Nope that will overlap in a nasty way with the scalar instance.  Make
the only array instance to be one of Struct, and wrap all scalar
arrays in Atom?

Pfff..  This doesn't work because pointers (to pointers ...) to
structs are words, while the structs themselves are not words

Meaning: the base type is different than the pointer type.

Really, this needs a TMLword instance for a struct, but that will
probably mess up many other things.

It's inconsistent..

Unless the Struct used for grouping machine word struct that is a
machine word are treated as different things.

They are, really...

But how to represent them?
And how to bridge them (their recursion) and the Struct recursion?

Something really doesn't fit..  Ha, I'm confusing types and data
again.  So, let's try: make a primitive type (C struct) that resembles
at least the structure of the Struct recursive class instances (binary

Here we go:

  {- Structs are also words.  Is this bad?  The main reason this is here
  is to support C structs and maybe later unions.  These can behave as a
  real atomic type (a single variable), but support destructuring. -}
  instance (TMLword t1, TMLword t2) => TMLword (t1,t2) where
    primType _ = APair (primType (undefined :: t1)) (primType (undefined :: t2))

  data TypeName   = AFloat | AInt | ABool | AVoid   -- atomic
                  | APair TypeName TypeName         -- composite
                  deriving (Eq)

Next: pack/unpack.  Can these be made the same as Struct?  Should
these be implemented as structs?  I.e. pack/unpack an "atomic" struct
into a representation struct.  This then uses the type

   unpack :: r as -> r_as
   pack   :: r_as -> r as

Note that in the Code interpretation of Loop/Effect, r as is never
visible.  This is currently only used in Value whare there is a struct