CQCL / text_to_discocirc

Apache License 2.0
4 stars 1 forks source link

`partial_apply` method does not curry properly while performing partial application #2

Closed JosephNathaniel closed 1 year ago

JosephNathaniel commented 1 year ago

The partial_apply performs currying on the final type but this currying does not change the actual expression. This incomplete currying leads to errors in n_expand code. https://github.com/CQCL/text_to_discocirc/blob/670602ecfe803cc95621b680d93c22ed6c471d8e/discocirc/expr/expr.py#L234-L236 A possible fix would be to write a currying function (similar to the uncurry function) that partial_apply calls. We could potentially simplify the partial_apply function like the following:

  1. curry the function
  2. apply the argument
  3. uncurry the result

To reproduce:

x = Expr.literal('x', Ty('n'))
y = Expr.literal('y', Ty('n'))
a = Expr.literal('a', Ty('n'))
likes = Expr.literal('likes', Ty('n') >> (Ty('n') >> (Ty('s'))))
expr = Expr.lmbda(Expr.lst([x,y], interchange=False), likes(y)(x))
expr(a)

gives the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[38], line 1
----> 1 expr(a)

File [c:\Users\New](file:///C:/Users/New) User\Documents\GitHub\text_to_discocirc\discocirc\expr\expr.py:79, in Expr.__call__(self, arg)
     78 def __call__(self, arg: Expr):
---> 79     return Expr.apply(self, arg)

File [c:\Users\New](file:///C:/Users/New) User\Documents\GitHub\text_to_discocirc\discocirc\expr\expr.py:217, in Expr.apply(expr, arg, context, reduce, head)
    215 # NOTE: if no head kwarg is passed in, this function now erases the head
    216 if expr.typ.input != arg.typ:
--> 217     return Expr.partial_apply(expr, arg, context, head=head)
    218 if expr.expr_type == "lambda" and reduce:
    219     if context == None:

File [c:\Users\New](file:///C:/Users/New) User\Documents\GitHub\text_to_discocirc\discocirc\expr\expr.py:243, in Expr.partial_apply(expr, arg, context, head)
    239     raise TypeError(f"Type of:\n{arg}\n is not compatible "
    240                     + f"with the input type of:\n{expr}")
    241 expr.typ = expr.typ.input[-i:] >> \
    242            (expr.typ.input[:-num_inputs] >> expr.typ.output)
--> 243 return Expr.apply(expr, arg, context, reduce=True, head=head)

File [c:\Users\New](file:///C:/Users/New) User\Documents\GitHub\text_to_discocirc\discocirc\expr\expr.py:222, in Expr.apply(expr, arg, context, reduce, head)
    220     context = {}
...
--> 222     for var, val in zip(expr.var.expr_list, arg.expr_list):
    223         context[var] = val
    224 else:

AttributeError: 'Expr' object has no attribute 'expr_list'
JosephNathaniel commented 1 year ago

fixed in 00a02b5f84427da35035c92b857274771c735b57