djspiewak / parseback

A Scala implementation of parsing with derivatives
http://parseback.io
Apache License 2.0
197 stars 22 forks source link

Find a way to avoid writing O(n^2) conversions #27

Open djspiewak opened 7 years ago

djspiewak commented 7 years ago

This is the current state of the ^^ syntax in parseback. It is done in this way to achieve two goals:

The reason there is a quadratic number of cases stems from the fact that we need to provide a separate, specific syntax class for each possible reassociation of the ~ type constructor. Obviously it would be much nicer to just write a linear number of cases (one for each arity, presumably) and then build some implicit machinery which would convince implicit search to generate the quadratic portion, but my efforts in that direction have thus far been unsuccessful.

Paging @milessabin and @travisbrown for thoughts and assistance if they feel so inclined!

milessabin commented 7 years ago

Possibly related SO Q&A.

djspiewak commented 7 years ago

@milessabin Similar, but not quite the same. This would all actually be quite easy if the sequential combinator produced a Parser[A :: B :: HNil] rather than a Parser[A ~ B], but I want to avoid the dependency if I can. It would also be very easy if I didn't have to worry about associativity, which is where things get especially tricky. That particular SO question deals with pattern extraction, which has explicit associativity. Because I have to infer associativity and types at the same time, guided solely by the Parser[E] (where E has some form) and the arity of the FunctionN type given, it gets a little more annoying.

Oh, tiny bit of background: type ~[+A, +B] = (A, B)

tl;dr: I tried what you suggest, and it doesn't leave enough structure in place for scalac to infer both associativity and specific types. I still think that the answer is probably somewhere along that line, but the direct approach didn't work for me.