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

[Relay] Add ADTs to text format #3863

Merged
merged 29 commits into from
Sep 6, 2019
Merged

Conversation

weberlo
Copy link
Contributor

@weberlo weberlo commented Aug 30, 2019

This PR tackles some of item 0 from this issue, using this RFC as a reference.

One notable deviation is that match expressions can be specified as incomplete (i.e., not requiring all cases to be covered) by writing match?.

The tests in test_ir_parser.py currently do not pass if I don't inline every expression in the pretty printer. This is because when graph vars are created, scoping becomes more complicated. For example, the following program

def @length[A](%xs: List[A]) -> int32 {
    match? (%xs) {
    | Cons(_, %rest) => 1 + @length(%rest)
    | Nil => 0
    }
}

would be pretty-printed like so

def @length[A](%xs: List[A]) -> int32 {
    %0 = 1 + @length(%rest)
    match? (%xs) {
    | Cons(_, %rest) => %0
    | Nil => 0
    }
}

. While this can still be parsed (by delaying evaluation of the graph expr until the context of its usage is available), it seems to add additional complexity without any benefits that I'm aware of. It also makes testing equality with the original program more difficult.

Hopefully, this brings us closer, if not all the way there, to writing Relay's prelude in the text format.

CC @jroesch @MarisaKirisame @joshpoll @slyubomirsky @junrushao1994

@slyubomirsky
Copy link
Contributor

Thank you for adding tests for the additional features.

Higher-level point of discussion: What syntax do we want to settle on for text format type definitions? I like the style @weberlo used here (similar to OCaml's), but it may contrast a bit with other syntax we have (e.g., it doesn't use curly braces).

Also on the subject of syntax, I think it may be a little redundant to include the line separators | between match clauses when we use braces to delimit a match block and each pattern must match exactly one expression. Do we want braces around the expression in the match clauses? As in, pattern => { expr }? Since there can only be one expression, the braces might be redundant, though.

One reason I am raising these questions is so we can update our documentation to be consistent with the syntax we actually implement.

@weberlo
Copy link
Contributor Author

weberlo commented Aug 31, 2019

@slyubomirsky I agree on the vertical bars in match exprs. I was originally just implementing the syntax from the RFC and hoping to reopen the discussion in this PR.

If we remove the bars from match, then it falls more in line with Rust syntax. We could then use Rustish syntax for ADT definitions. An example Rust enum is

enum WebEvent {
    // An `enum` may either be `unit-like`,
    PageLoad,
    PageUnload,
    // like tuple structs,
    KeyPress(char),
    Paste(String),
}

and we could just replace enum with type.

We could also allow optional braces in match arms, which would be useful for sequencing.

match %ayy_lmao {
  Ayy => 0
  Lmao => {
    @mutate();;
    1
  }
}

The last minor point is that if we're going with Rust syntax, should we also require commas after match arms?

Copy link
Contributor

@MarisaKirisame MarisaKirisame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please extend graph_equal for this.

src/relay/ir/alpha_equal.cc Outdated Show resolved Hide resolved
src/relay/ir/alpha_equal.cc Outdated Show resolved Hide resolved
@MarisaKirisame
Copy link
Contributor

also:maybe change the name of graph_equal to something better (freevars_eq e.x.)

@weberlo
Copy link
Contributor Author

weberlo commented Sep 2, 2019

@MarisaKirisame Is there not a more standard PL name for the type of equality that graph_equal encodes?

@MarisaKirisame
Copy link
Contributor

MarisaKirisame commented Sep 2, 2019

@weberlo alpha_equal_up_to_free_vars. unifiable is also good.

* NOTE: The `USE_ANTLR` option in `config.cmake` must be enabled in order for
* changes in this file to be reflected by the parser.
* NOTE: All upper-case rules are *lexer* rules and all camel-case rules are *parser* rules.
*/

grammar Relay;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the version number to 0.0.4.

@slyubomirsky
Copy link
Contributor

On syntax, I like the idea of optional braces but we should be certain that this will not cause problems for round-tripping. I think commas in type definitions and match definitions in the Rust style might be good in that they more closely resemble C-like syntax

@weberlo
Copy link
Contributor Author

weberlo commented Sep 3, 2019

@slyubomirsky @MarisaKirisame I took a crack at implementing Rust-style syntax in the most recent commits. Take a look when you get chance.


Doc adt_body;
adt_body << PrintSep(constructor_docs, separator);
// add trailing comma if there are any constructors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why trailing comma?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk. it's not necessary. just a matter of style.

@weberlo
Copy link
Contributor Author

weberlo commented Sep 4, 2019

I've just added support for external type declarations (example syntax is extern Type[A]), per @jroesch's suggestion. It will allow for importing opaque types which can be interacted with via operators.

}

// TODO(weberlo): Consolidate this method and `IsAtomic` in `pass_util.h`?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea if we can avoid repeating ourselves, though are we certain that these definitions (something we will always inline and atomic expressions) will always coincide?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I was originally thinking it might be a good idea, but I'm not convinced they should be merged anymore.



def test_extern_adt_defn():
# TODO(weberlo): update this test once extern is implemented
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shouldn't include the syntax while we don't have the feature implemented (e.g., leave it for a different PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jroesch is planning on updating it very soon for the VM. He just wanted me to add all of the necessary boilerplate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need this change ASAP, I just want the parsing changes to be shipped in a unit to keep things clean.

@weberlo weberlo force-pushed the add-adt-to-text-format branch from 8f63f9d to 0ea6870 Compare September 6, 2019 00:50
@jroesch jroesch merged commit ca0292d into apache:master Sep 6, 2019
MarisaKirisame pushed a commit to MarisaKirisame/tvm that referenced this pull request Sep 7, 2019
* Getting closer to having ADT defs

* ADT defs working probly

* Match parsing basipally done

* came to earth in a silver chrome UFO

* match finished?

* All tests but newest are passing

* ADT constructors work

now cleanup?

* Cleanup round 1

* Cleanup round 2

* Cleanup round 3

* Cleanup round 4

* Cleanup round 6

* Cleanup round 7

* Lil grammar fix

* Remove ANTLR Java files

* Lint roller

* Lint roller

* Address feedback

* Test completeness in match test

* Remove unused imports

* Lint roller

* Switch to Rust-style ADT syntax

* Lil fix

* Add dummy `extern type` handler

* Add type arg to test

* Update prelude semantic version

* Repair test

* Fix graph var handling in match

* Revert 's/graph_equal/is_unifiable' change
wweic pushed a commit to wweic/tvm that referenced this pull request Sep 16, 2019
* Getting closer to having ADT defs

* ADT defs working probly

* Match parsing basipally done

* came to earth in a silver chrome UFO

* match finished?

* All tests but newest are passing

* ADT constructors work

now cleanup?

* Cleanup round 1

* Cleanup round 2

* Cleanup round 3

* Cleanup round 4

* Cleanup round 6

* Cleanup round 7

* Lil grammar fix

* Remove ANTLR Java files

* Lint roller

* Lint roller

* Address feedback

* Test completeness in match test

* Remove unused imports

* Lint roller

* Switch to Rust-style ADT syntax

* Lil fix

* Add dummy `extern type` handler

* Add type arg to test

* Update prelude semantic version

* Repair test

* Fix graph var handling in match

* Revert 's/graph_equal/is_unifiable' change
wweic pushed a commit to wweic/tvm that referenced this pull request Sep 16, 2019
* Getting closer to having ADT defs

* ADT defs working probly

* Match parsing basipally done

* came to earth in a silver chrome UFO

* match finished?

* All tests but newest are passing

* ADT constructors work

now cleanup?

* Cleanup round 1

* Cleanup round 2

* Cleanup round 3

* Cleanup round 4

* Cleanup round 6

* Cleanup round 7

* Lil grammar fix

* Remove ANTLR Java files

* Lint roller

* Lint roller

* Address feedback

* Test completeness in match test

* Remove unused imports

* Lint roller

* Switch to Rust-style ADT syntax

* Lil fix

* Add dummy `extern type` handler

* Add type arg to test

* Update prelude semantic version

* Repair test

* Fix graph var handling in match

* Revert 's/graph_equal/is_unifiable' change
wweic pushed a commit to neo-ai/tvm that referenced this pull request Sep 16, 2019
* Getting closer to having ADT defs

* ADT defs working probly

* Match parsing basipally done

* came to earth in a silver chrome UFO

* match finished?

* All tests but newest are passing

* ADT constructors work

now cleanup?

* Cleanup round 1

* Cleanup round 2

* Cleanup round 3

* Cleanup round 4

* Cleanup round 6

* Cleanup round 7

* Lil grammar fix

* Remove ANTLR Java files

* Lint roller

* Lint roller

* Address feedback

* Test completeness in match test

* Remove unused imports

* Lint roller

* Switch to Rust-style ADT syntax

* Lil fix

* Add dummy `extern type` handler

* Add type arg to test

* Update prelude semantic version

* Repair test

* Fix graph var handling in match

* Revert 's/graph_equal/is_unifiable' change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants