Skip to content
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

Draft
wants to merge 120 commits into
base: dev
Choose a base branch
from
Draft

Implement Labeled Tuples #1235

wants to merge 120 commits into from

Conversation

WondAli
Copy link
Contributor

@WondAli WondAli commented Mar 5, 2024

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 bind 4 to a and 2 to b

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

  1. Implement a "label" term to express the labeled tuple element.
  2. Modify tuple semantics to consider labeled term contained within the tuple.
  3. Implement the dot operator as a new expression that takes an expression and a pattern (var only)
  4. Modify labeled tuple semantic interactions with let-binding, casting, function application, etc.

Checkpoints:

  • finish writing the draft pull request
  • Implement the Label term that simply wraps an element with a string.
  • Tuples type-check assuming all elements are in order.
  • Tuples type-check considering rearrangements of labeled elements.
  • Tuple type-checking when there is an unequal number of labeled elements
  • Dot operation on Tuple expressions
  • Labeled tuples as function arguments
  • Code Cleaning and Optimization
  • Add documentation buffer for labeled tuples
    Known Bugs:
  • Label uniqueness in a tuple needs to be checked during statics (currently dynamic check is broken too?)

@WondAli WondAli added the in-development for PRs that remain in development label Mar 5, 2024
@WondAli WondAli self-assigned this Mar 5, 2024
@WondAli WondAli mentioned this pull request Mar 5, 2024
@cyrus-
Copy link
Member

cyrus- commented May 1, 2024

@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?

@WondAli WondAli force-pushed the labeled-tuple-rewrite branch 2 times, most recently from b8e1f37 to 3c24406 Compare August 24, 2024 21:16
@WondAli WondAli force-pushed the labeled-tuple-rewrite branch from 3c24406 to fe24986 Compare August 24, 2024 21:22
@cyrus-
Copy link
Member

cyrus- commented Nov 5, 2024

  • singleton analytic case for patterns should insert labels
let f : (b=Float) -> Float = 
  fun b -> b
in ...
  • better messages in the cursor inspector for when lifting occurs

@7h3kk1d
Copy link
Contributor

7h3kk1d commented Nov 8, 2024

Screenshot from 2024-11-08 10-40-51
Here's a first pass on the "better messages in the cursor inspector for when lifting occurs". We need to figure out how we want to represent this all in statics in a more disciplined way.

@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,
Copy link
Contributor

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.

@7h3kk1d
Copy link
Contributor

7h3kk1d commented Nov 8, 2024

  • singleton analytic case for patterns should insert labels
let f : (b=Float) -> Float = 
  fun b -> b
in ...

@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 x pattern is getting expanded to (a=x) as it would in the function case so x ends up just being 3. So I'm kind of unclear how you actually would bind a variable to the singleton labeled tuple.

let x : (a=Int) = (a=3)

@cyrus-
Copy link
Member

cyrus- commented Nov 8, 2024

Ah I see, yeah that's awkward. Don't have an immediate solution...

@cyrus-
Copy link
Member

cyrus- commented Nov 8, 2024

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:

let f : (a=Int) -> Int = 
  fun x : (a = Int) -> x.a

and

let f : (a=Int) -> Int = 
  fun x -> x

seems natural enough to me.

@7h3kk1d
Copy link
Contributor

7h3kk1d commented Nov 15, 2024

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:

This is implemented in the latest commit.

Comment on lines +16 to +51
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,
);
};
Copy link
Contributor

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.

Comment on lines +342 to 357
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,
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-development for PRs that remain in development
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants