Pattern Matching
Jean-Baptiste Giraudeau
jb at giraudeau.info
Thu Mar 30 19:06:57 UTC 2017
Hi everyone,
implementing algebraic data types and structural pattern matching using
object algebra (akka inner-visitor) is relatively straight-forward. I
took sometimes to automate the process (including equals() generation)
via a jsr269 annotation processor, see
https://github.com/derive4j/derive4j for some nice examples of how far
the visitor can be pushed with current java.
Currently visitor is the only way to implement ADT without resorting to
reflection api/instanceof/etc (it as also the nice side-effect of
allowing to implement transparent lazy thunk).
But as mentioned visitor has the problem of forcing boxing of return
type. Another one is that, if the visitor need to capture many
variables, it can be costlier to instantiate (vs unapply).
But one potential problem I'm most concerned about is that implementing
/tail recursion optimization/ is probably quite harder using visitor
than using 'unapply'. IMO any implementation of (structural) pattern
matching that would found itself to not be tail call optimizable would
have failed big time, so I hope that concern will be taken into account
in the design.
my 2cts, and thanks for your work!
- jb
On 30. 03. 17 00:01, john.r.rose at oracle.com (John Rose) wrote:
> On Mar 28, 2017, at 1:04 AM, Andrey Breslav <andrey.breslav at jetbrains.com> wrote:
>> For completeness, since Remi aimed at listing all alternatives, I think we should not forget Object algebras: https://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf <https://urldefense.proofpoint.com/v2/url?u=https-3A__www.cs.utexas.edu_-7Ewcook_Drafts_2012_ecoop2012.pdf&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PQcxBKCX5YTpkKY057SbK10&r=lHQQP_21o1x25cljBzJfny3WhZfis9i1l__EQId-YS8&m=S66HlLkIAVyLjybndnX757FqKMVc09aizAFtTfO3bYk&s=VFuju0jCypu8uUv-_3IsX8DrsJuMu-TdIAWGnpE5dMU&e=>
>>
>> My experience has been that this pattern is good for observing data structures (transforming, pretty-printing, etc), but it's usually hard to implement, say, equals() through it
> Thanks for the reference. Cook's work on batches is probably relevant.
> There is something deep going on here, and that paper shines some light on it.
> I hope we can connect Java deeply to the sorts of patterns they talk about.
>
> Since matching is not (yet, officially) on the table for Amber, it would be
> premature to use this venue to say much more. One teaser: I hope
> to get some time to write about what I think of as "metamorphic programming",
> or coordinated hooks for the "tear down" and "build up" of data.
>
> — John
More information about the amber-spec-observers
mailing list