-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Labeled Tuples #1235
base: dev
Are you sure you want to change the base?
Implement Labeled Tuples #1235
Conversation
…: optimize hacks for cleaner code, fix more bugs
@WondAli playing with this branch, looks like the labels aren't showing up in the types of the tuples (and so you get weird run-time errors when projecting out a label that doesn't exist). is there a specific issue with adding labels to product types? |
…nmatched labels in function parameters
b8e1f37
to
3c24406
Compare
3c24406
to
fe24986
Compare
|
@WondAli let me know if you also have any ideas. If we move all the lifting/rearranging to statics maybe there's some additional data we can put on the Info.exp that can be used more generally. |
// We need to keep the original status of the expression to get error messages on the unelaborated expression | ||
let info = { | ||
...info, | ||
status: original_info.status, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copying the status from the inner expression is probably too clever. We may need to have a more principled status for this expression.
@cyrus- I'm working on the pattern case now and I'm running across an issue I'm curious if you have thoughts on. In the following case the
|
Ah I see, yeah that's awkward. Don't have an immediate solution... |
Maybe only patterns that are not under an ascription have the lifting behavior? That would also allow you to explicitly bind a 1-tuple when defining a function:
and
seems natural enough to me. |
This is implemented in the latest commit. |
let rec strip_casts = (e: Exp.t): Exp.t => { | ||
print_endline("Stripping casts: " ++ Exp.show(e)); | ||
Exp.map_term( | ||
~f_pat= | ||
(fn, t) => | ||
switch (t.term) { | ||
| Cast(e, _, _) => strip_casts_pat(e) | ||
| _ => fn(t) | ||
}, | ||
~f_exp= | ||
(fn: Exp.t => Exp.t, t: Exp.t) => | ||
switch (t.term) { | ||
| Cast(e, _, _) => strip_casts(e) | ||
| _ => fn(t) | ||
}, | ||
e, | ||
); | ||
} | ||
and strip_casts_pat = (p: Pat.t): Pat.t => { | ||
print_endline("Stripping casts: " ++ Pat.show(p)); | ||
Pat.map_term( | ||
~f_pat= | ||
(fn, t) => | ||
switch (t.term) { | ||
| Cast(e, _, _) => fn(e) | ||
| _ => fn(t) | ||
}, | ||
~f_exp= | ||
(fn: Exp.t => Exp.t, t: Exp.t) => | ||
switch (t.term) { | ||
| Cast(e, _, _) => strip_casts(e) | ||
| _ => fn(t) | ||
}, | ||
p, | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has to be a better way to do this.
Prod([ | ||
Unknown(Internal) |> Typ.fresh, | ||
Unknown(Internal) |> Typ.fresh, | ||
]) | ||
|> Typ.fresh, | ||
Prod([Float |> Typ.fresh, Bool |> Typ.fresh]) |> Typ.fresh, | ||
) | ||
|> Exp.fresh, | ||
Cast( | ||
Bool(true) |> Exp.fresh, | ||
Bool |> Typ.fresh, | ||
Prod([Float |> Typ.fresh, Bool |> Typ.fresh]) |> Typ.fresh, | ||
Prod([ | ||
Unknown(Internal) |> Typ.fresh, | ||
) | ||
|> Exp.fresh, | ||
]) | ||
Unknown(Internal) |> Typ.fresh, | ||
]) | ||
|> Typ.fresh, | ||
) | ||
|> Exp.fresh, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something in the elaboration for tuples is adding a bunch more casts here
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 labeledx
andy
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).
(x=1, True, y=6)
is properly typed against(Bool, y=Int, x=Int)
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 to1
Currently undefined for anything except tuple expressions.
Implementation Strategy
Checkpoints:
Known Bugs: