Parse logic input

14 views
Skip to first unread message

Paul Royik

unread,
Jun 18, 2021, 6:07:37 AM6/18/21
to sympy
I want to parse logic implication `p->q`. What should I add to global_dict, so that parse_expr parsed it?

I know, that SymPy doesn't have and Implication class. I will create it by myself, but `global_dict = {'->': Implication}` doesn't work.

Oscar Benjamin

unread,
Jun 18, 2021, 8:51:23 AM6/18/21
to sympy
On Fri, 18 Jun 2021 at 11:07, Paul Royik <distan...@gmail.com> wrote:
I want to parse logic implication `p->q`. What should I add to global_dict, so that parse_expr parsed it?

I know, that SymPy doesn't have and Implication class. I will create it by myself, but `global_dict = {'->': Implication}` doesn't work.

I don't think there is any way to add operators to `parse_expr`. SymPy does have Implies though:


In [1]: Implies

Out[1]: Implies


In [2]: Implies(x, y)

Out[2]: x → y


The operator used for this is left/right shift:

In [3]: x >> y

Out[3]: x → y


In [4]: x << y

Out[4]: y → x


Oscar

Aaron Meurer

unread,
Jun 18, 2021, 3:35:58 PM6/18/21
to sympy
parse_expr parses Python syntax. global_dict only tells it how to
handle names that are undefined, but a->b isn't valid Python syntax.
So the only way to do this would be to create a transformer function
that parses -> from the tokens and reorganizes it into an Implies
call.

This actually probably won't be that hard, because -> is already a
token recognized by the Python tokenizer (RARROW). Take a look at the
existing transformers in sympy/parsing/sympy_parser.py to see how they
work. I also recommend a site that I made for documentation on Python
tokenize in general
https://www.asmeurer.com/brown-water-python/index.html.

Aaron Meurer

On Fri, Jun 18, 2021 at 4:07 AM Paul Royik <distan...@gmail.com> wrote:
>
> I want to parse logic implication `p->q`. What should I add to global_dict, so that parse_expr parsed it?
>
> I know, that SymPy doesn't have and Implication class. I will create it by myself, but `global_dict = {'->': Implication}` doesn't work.
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/81e866b5-d1c7-4c1a-b762-1ecfc3cd22f0n%40googlegroups.com.

Paul Royik

unread,
Jun 19, 2021, 7:02:45 AM6/19/21
to sympy
Thank you very much!
Reply all
Reply to author
Forward
0 new messages