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

Support Match #93

Open
odashi opened this issue Nov 12, 2022 · 19 comments
Open

Support Match #93

odashi opened this issue Nov 12, 2022 · 19 comments
Assignees
Labels
Milestone

Comments

@odashi
Copy link
Collaborator

odashi commented Nov 12, 2022

It would be great if we support the match statement added in Python 3.10, but the entire syntax is somewhat complex.

https://docs.python.org/3/library/ast.html#pattern-matching

@odashi odashi added the feature label Nov 12, 2022
@odashi odashi added this to the v0.3 milestone Nov 12, 2022
@odashi odashi self-assigned this Nov 12, 2022
@erica-w-fu
Copy link
Contributor

Could I get assigned to this issue? I would love to contribute to this repo!

@odashi
Copy link
Collaborator Author

odashi commented Nov 22, 2022

@erica-w-fu Hi, feel free to work on this!
Since the overall syntax around Match is very huge, I'd like to decompose it to small developments (pull requests) mainly for ease of reviewing.

@erica-w-fu
Copy link
Contributor

Could I get some clarification on what is expected? Are we trying to convert match statements to latex? What does support mean exactly?

@odashi
Copy link
Collaborator Author

odashi commented Nov 29, 2022

Basically implementing some codegen rules in function_codegen.py is all you need.

As for "small development", you can start to implement only a specific portion in the whole syntax (e.g., I think supporting only MatchValue is the easiest one).
For all other cases, we should raise LatexifyNotSupportedError, which marks "not supported, but we'll support it in the future". For example:

class FunctionCodegen(NodeVisitor):
    ...
    def visit_Match(self, node: ast.Match) -> str:
        if isinstance(node.pattern, ast.MatchValue):
            return ...something...
        
        raise exceptions.LatexifyNotSupportedError("Unsupported match syntax.")

@odashi
Copy link
Collaborator Author

odashi commented Nov 29, 2022

I think the whole match case can't be supported in latexify.function because there are capture clauses that there aren't any corresponding syntax in math. It would be better to support only syntaxes with:

  • Constants
  • sequence/dict with only constants

@odashi
Copy link
Collaborator Author

odashi commented Nov 29, 2022

The resulting LaTeX could be expressed semilarly to that of the if clause:

match x:
    case 0:
        return 1
    case _:
        return math.sin(x)/x

$$\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \frac{\sin(x)}{x}, & \mathrm{otherwise} \end{array} \right.$$

@erica-w-fu
Copy link
Contributor

That makes a lot of sense! Thanks for the clarification

@erica-w-fu
Copy link
Contributor

How can we build the project for development? Are there any detailed docs that explain how to build and test latexify/our implementation?

@odashi
Copy link
Collaborator Author

odashi commented Nov 30, 2022

@erica-w-fu I will add some documentation. For now you can install the development version as follows:

cd /path/to/latexify_py
python -m venv venv  # Create a virtual environment
python -m pip install -e '.[dev]' # Install latexify as the editable mode with some libraries

jupyter notebook is also installed by the command above.

@erica-w-fu
Copy link
Contributor

Thanks! I was able to run all of the tests. Are there any build tools and commands that you use to test within jupyter notebook?

This was referenced Dec 4, 2022
@erica-w-fu
Copy link
Contributor

Are there any other cases that would be relevant for latex? If you could provide examples that would be extremely helpful!

@odashi
Copy link
Collaborator Author

odashi commented Dec 5, 2022

Are there any build tools and commands that you use to test within jupyter notebook?

This library doesn't run tests within jupyter. If you need to run tests on jupyter, you need either (1) run CI libraries programmatically, or (2) run shell command from the notebook. If you know some other tools to run tests on jupyter, it is still fine to use it on your local environment.

Are there any other cases that would be relevant for latex?

I'm not sure for now. If you have some ideas, please share on this thread.

@erica-w-fu
Copy link
Contributor

Here are two ideas for things to support:

1. MatchOr

In conditional equations, multiple conditions can lead to one of the cases, thus we should implement MatchOr to support this.

Code example

match x:
  case 0 | 1:
    1
  case _:
    2

Latex example
Screen Shot 2022-12-06 at 10 39 05 PM

2. MatchAs (nonempty/not the wildcard)

In conditional equations, ranges are often used to define certain cases, thus we should implement when MatchAs is not empty so the guard attribute can be evaluated and comparisons through <, >, <=, >= can be made

Code example

match x:
  case x if x > 0:
    1
  case _:
    2

Latex example
Screen Shot 2022-12-06 at 10 47 08 PM

For this case, we might also want to implement a fixed range like this

Code example

match x:
  case x if (x > 0 and x <= 5):
    1
  case _:
    2

Latex example
Screen Shot 2022-12-06 at 10 54 01 PM

Would we want to combine these ranges (so instead of x>0 and x<=5 we have 0<x<=5? That would require more testing but could be a valuable feature.

@odashi
Copy link
Collaborator Author

odashi commented Dec 7, 2022

Thanks! if each case doesn't require additional identifiers (capture), it would be good to be implemented. I guess they could be proposed by separate pull requests (to keep minimality of the change).

MatchOr

It looks we need to use boolean operation: x = 0 \lor x = 1. Multiple ifs for a single expression look weird IMO.

Would we want to combine these ranges

The library has to keep the original expression unless users specified appropriate options. In this case, if the user wanted to get $0 &lt; x \leq 5$, the user basically needs to write 0 < x <= 5.

@Yuqi07
Copy link
Contributor

Yuqi07 commented Dec 11, 2022

@odashi Hi, we cannot get the match statement in the jupyter notebook running. For now, we are getting this error:

Screen Shot 2022-12-11 at 14 57 23

This is what we did:

  • Cloned the repo
  • Ran the jupyter notebook

Is there any step we missed? Thanks ahead!

@odashi
Copy link
Collaborator Author

odashi commented Dec 12, 2022

@Yuqi07 Not sure how you installed the library. Could you provide the complete step you tried?

@Yuqi07
Copy link
Contributor

Yuqi07 commented Dec 12, 2022

@Yuqi07 Not sure how you installed the library. Could you provide the complete step you tried?

  1. pip install latexify-py
  2. jupyter notebook

I think these were the only steps I ran. Did I miss anything? Thxx!

@odashi
Copy link
Collaborator Author

odashi commented Dec 12, 2022

pip install from PyPI doesn't work for development. You need to install the main branch directly: pip install -e '.[dev]'

@Yuqi07
Copy link
Contributor

Yuqi07 commented Dec 12, 2022

Thanks! Now it works!

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

No branches or pull requests

3 participants