hazel
hazel copied to clipboard
Implement Labeled Tuples
Goal: Implement Labeled Tuple functionality into Hazel. Note: I don't think I can change the source branch on the old pull request. This redesign of the implementation was started on a new branch, so the old pull request (and branch) is outdated.
TODO: Add more detail to the pull request; add more detail about syntax and semantics
What are Labeled Tuples?
An element in a tuple can be "labeled" or "named" with the =
operator. For example, (x=1, y=2)
is a tuple expression in which its elements are labeled x
and y
respectively. These labels are relevant only within their nearest enclosing tuple.
The =
operator can be thought of as a binary operator that takes in a string and an expression, and returns the expression.
(Uncertain) A labeled element is type-consistent with the type of the element it is labeling.
Labeled Tuple Features When analyzed against a labeled tuple type, the labeled items can be placed anywhere in the tuple expression and tuple type (the unlabeled items must be in relative order to each other).
- Ex:
(x=1, True, y=6)
is properly typed against(Bool, y=Int, x=Int)
- Ex:
Let x=a, b = (y=2, x=4)
will bind4
toa
and2
tob
Dot Operators
The dot operator .
can be applied to a tuple to extract a labeled element.
For example, (x=1, 2, 3).x
is equivalent to 1
Currently undefined for anything except tuple expressions.
Implementation Strategy
- Implement a "label" term to express the labeled tuple element.
- Modify tuple semantics to consider labeled term contained within the tuple.
- Implement the dot operator as a new expression that takes an expression and a pattern (var only)
- Modify labeled tuple semantic interactions with let-binding, casting, function application, etc.
Checkpoints:
- [ ] finish writing the draft pull request
- [x] Implement the Label term that simply wraps an element with a string.
- [x] Tuples type-check assuming all elements are in order.
- [x] Tuples type-check considering rearrangements of labeled elements.
- [x] Tuple type-checking when there is an unequal number of labeled elements
- [x] Dot operation on Tuple expressions
- [x] Labeled tuples as function arguments
- [ ] Code Cleaning and Optimization
Known Bugs:
- Label uniqueness in a tuple needs to be checked during statics.
- Need to enforce labels can only be in tuples, and standalones will become a tuple of size 1 (update: completed statically for expressions. TODO: confirm this is sufficient)
- Dot operator not checking statically (is it possible?)
- Casting with involving the Dot operator blocks the stepper in some circumstances, specifically when applying the dot operator to a variable. Also dot operator possibly not implemented in a correct way.
- Not rearranging when directly supplying labeled arguments to a function (e.g. function(x=1, y=2))