morganstanley / hobbes

A language and an embedded JIT compiler
http://hobbes.readthedocs.io/
Apache License 2.0
1.16k stars 105 forks source link

Why can I define a record with numeric field ids? #354

Open adam-antonik opened 4 years ago

adam-antonik commented 4 years ago

I just discovered that intV's are allowed as labels to records. So I can write

> {0=0, 1=1}
(0,1)

However doing anything else than exactly creating a tuple, e.g. {1=0, 0=1} or {10=1, x=0} makes things break; generic functions over these objects seem to fail, and :t {10=1, x=0} is reported as (int * )

Is there some reason for this construction? As it stands it can only see it introducing a difficult to track down bug.

kthielen commented 4 years ago

I think that the only reason that this is allowed is because both field projection and construction go through the recfieldname grammar term. Tuple indexing looks like p.3 because tuples can have any size and the Haskell naming convention of fst, snd, thd doesn't scale (what's the name for the 9,237th tuple field?).

Maybe the right way to do this is to validate record construction (rejecting constructions like {1=e, 0=e}).

As you probably guessed, the runtime memory layout of tuples and records is the same, so we settled on using records as the canonical form because that way we don't lose field names.