Skip to content

Lists #39

@qsctr

Description

@qsctr

We are adding lists parameterized by the type of the contents and the maximum length.

For the syntax for the list type, given that the syntax for the int type is written like a function call, should the list type then be something like list(n, T) or list(T, n)? Or maybe we should have different kinds of type arguments be separate, so type-level numbers go in parentheses but types go somewhere else, so something along the lines of list(n) T or [T](n) or T list(n).

For the list literal syntax I was thinking we could just have something like [expr, expr, ..., expr], but if we are parameterizing by max length then we would also need the max length in there if we want to avoid type inference. So it would have to be like [a, b, c](n) or (n)[a, b, c], or if we follow the existing int value syntax, list(n, [a, b, c]) or maybe if we flatten it a bit list(n, a, b, c)? This would be kind of annoying to type out every time though. Maybe we can assume the max size is the length of the list literal by default if it is not explicitly specified, although I have no idea how common that will be the case in actual code.

As for semantics I thought about how to desugar lists to existing constructs but I'm not sure if it fully works out. We could desugar [a, b, c](3) to something like (3, (a, (b, c))) where the first thing is the length and the rest are the elements, or maybe (3, (a, (b, (c, false)))) to be more uniform. And if we have something where the actual length is less than the max length such as [a, b, c](5) then it could be like (3, (a, (b, (c, (def, (def, false)))))) where def is some default value for the type and doesn't really matter. But then the problem with doing it this way is that if we do a cons operation then it's not just a simple O(1) thing but rather we would have to shift everything inwards. And if we put the elements on the inside and the default values on the outside then we would have to go through the default values to get to the elements as well. So I'm not sure if there's a good way to represent things with tuples.

One thing I was thinking was if we don't parameterize by the maximum length but rather just by the actual length. So it would be similar to the vector types you have in dependently typed languages. Then lists could really just be simple syntactic sugar over nested tuples. But then you would probably really need polymorphism over length of lists to do anything useful that you couldn't currently do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions