[Aldor-l] Aldor and Pattern Matching
Peter Broadbery
p.broadbery at gmail.com
Thu Aug 7 18:19:33 EDT 2025
Since the topic of pattern matching was mentioned on the fricas-l
list, I thought I'd share some less than fully cooked ideas that I'm
working on in aldor. It may be possible to implement them in
Spad/Fricas terms as well - I'd be interested in knowing how feasible
this sort of thing is in that environment (*).
First, a proposed matching syntax
foo(l: List T): X == select l in
nil() => no things
cons(?a, nil()) => one thing
cons(?a, cons(?b, ?l)) => two or more things..
The idea is that functions can be used as patterns, at least
syntactically, and they compose as a normal function would. Note that
[a, b] could work as a pattern, but don't illustrate the nesting
element.
The glue here is that the operations are defined as partial functions
going from output to input rather than the traditional left to right
direction. Maybe a sort of dual category thing.
Carrying on, definitions could look like:
List(T: with): ... ==
cons: (T, %) <- %
nil: () <- %
== add
....
(<- cons)(l: %): Partial(T, %) == if cons? l then success(first l,
rest l) else failed
(<- nil)(l: %): Partial(T, %) == if empty? l then success() else failed
....
This abuses the leftward arrow to indicate that the declared name
works in pattern matching, and is distinct from a normal mapping.
It has been surprisingly easy to get a fair part of this working.
Lots to do yet though.
Some questions: should it work, and is anyone aware of similar
constructs in other languages?
Peter
(*) I might look at it there at some stage, depending on relative
ease and time.
More information about the Aldor-l
mailing list