christhekeele
Hey all!
I’ve been working on an elixir-to-matchspec compiler. Think ex2ms with support for a few more expressions, pattern support, tracing support, helpful errors, and some other niceties.
Right now it’s passing ex2ms’s test suite, and I’ve added many tests of my own, but… I know I’ve seen edge cases where bad matchspecs were generated during development that I forgot to jot down. Also, I’d love to see it build other matchspecs used in the field that people have crafted by hand!
So, it’d be incredibly helpful if anyone cared to share theirs to bolster my test suite! I’m interested in:
- matchspecs passed to
:ets.select...:recon_trace.calls/2Registry.select/2:dbg.tp...:erlang.trace_pattern...
- matchpatterns passed to
:ets.match...Registry.match/4
- especially anything with
- non-trivial destructuring in the match heads
- nested tuple literals in match bodies
All contributions greatly appreciated! ![]()
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
While I am working on the Language Agnostic Code Audit SaaS, which uses MetaAST (spoiler: I am expecting it to be in a good shape for ann...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 8 of 8 Posts
mguilmineau
A few samples from our code - hope it helps.
:ets.select( ets_name( customer ), [ { { { customer, :_ }, { true, :“$1”, :_, :“$2” } },
[ { :andalso,
{ :“==”, {:map_get, :a, {:map_get, :job, :“$2”}}, a},
{ :“==”, {:map_get, :b, {:map_get, :job, :“$2”}}, b}
} ],
[ [:“$1”, :“$2”] ] } ] )
:ets.select_delete( ets_name( customer ), ( for task_id ← task_ids,
do: { { { customer, :_ }, :“$1” }, [ { :“==”, {:map_get, :id, :“$1”}, task_id } ], [:true] } ) )
:ets.select( ets_name( customer ),
[ { { { customer, :_ }, { :, :“$1”, :, :“$2” } },
[ { :andalso,
{ :“>=”, :“$1”, Util.to_dets( date ) },
{ :“<”, :“$1”, Util.to_dets( Util.tomorrow ) }
} ],
[ :“$2” ] } ] )
:ets.select( ets_name( customer ),
[ { { { customer, :_ }, :“$1”, :“$2”, :, : },
[ { :or,
{ :“==”, {:map_get, :reattempt, :“$1”}, @auto },
{ :“==”, {:map_get, :reattempt, :“$1”}, @manual }
} ],
[ :“$1” ] },
{ { { customer, :_ }, :“$1”, @paused, :, : },
,
[ :“$1” ] },
{ { { customer, :_ }, :“$1”, @deleted, :, : },
,
[ :“$1” ] },
( if for_seeding,
do: { false, , [ true ] },
else: { { { customer, :_ }, :“$1”, :, :, true },
,
[ :“$1” ] } )
] )
christhekeele
This is invaluable @mguilmineau! Thank you so much!
It did in fact surface the bug I couldn’t recall the cause of! (In your first example, I have to re-write the
map_gets to map destructuring. Turns out things broke when you both destructured a map to bind a variable from one of its values, 2 maps deep, and assigned the map itself to a variable at the same time.)One fun thing is that since all specs pass through Elixir’s compiler, at compile time, before being converted to the underlying syntax, it issues a friendly familiar warning on the first clause of your 4th example: if you convert the
:"$2"reference into, say, a variable calledarg2in the match head, Elixir complains:This is, of course, because it isn’t used in the body of the matchspec. Changing it to
_or_arg2fixes.So, two of your examples are working well when transliterated, and generate (semantically) identical matchspecs:
Example 1
Generated spec
Example 3
Generated spec
Examples 2 and 4, however, are using
forandifinside of the spec when building clauses. This is something I can totally support, but haven’t decided on the syntax yet. The simplespec do; x -> y; endsyntax mimicking anonymous function definitions obviously don’t support dynamic clause generation.My two ideas are to either expose a lower-level single-clause compiler and a merging syntax, ex:
Or, to try to allow top-level, or even nested, control structures within the spec macro:
The former feels verbose, but very straight-forward. The latter seems convenient, but also violates basic syntax rules of Elixir. Allowing arbitrary
->s in existing control structures that don’t normally support them likeforandifwould be confusing. I’m also not sure how it would need to interact with control structures that already use->, likecaseandcond.What do you think?
mguilmineau
Nice. I did clean up and simplify my examples, hence the rogue :“$2”. Good that you caught this!
If you were not supporting the if within the query spec, we could easily branch that :ets.select into two functions each with their own query spec.
On the other hand, the for is valuable to support as it noticeably speeds up queries.
I do not have a strong preference on your question about syntax. Hopefully actual contributors to the language will chime in
One thought that may lead you to consider one version vs another, the for here is used as an OR clause to match multiple task ids, however it could also be used to match multiple possible attributes (as in [ { :“==”, {:map_get, field, :“$1”}, … i.e. we’d be matching a key instead of a value. I imagine part of the appeal in writing the matchspecs in elixir is having key and value differentiation standing out more clearly.
A final thought: none of these pattern matches are terribly efficient since they require a full scan of the data sets, as opposed to lookup and match. We make up for it by optimizing our :ets cache set structures from flatter :dets stores (denormalized in multiple ways and updated only as needed). From my perspective, this is where the magic and difficulty lies, as opposed to writing the matchspecs. If the intermediate :ets caching and retrieval was done automatically based on our :dets storage structure and based on the way we ultimately query the data, that would be a lot of time and code saved indeed.
christhekeele
I feel as if this is 50/50, personally.
This is my real goal: not to solve these kinds of problems, but to make match specs more accessible, and therefore increase their adoption in general, so that more advanced tooling can be easily built on top of them without needing to understand the underlying syntax. What I’m working on was originally a proposal to the language itself, though I feel like it belongs outside it now.
Of course, targeting Elixir AST as the high-level format should help with this a lot: library authors can just leverage Elixir’s powerful macro system to translate things (ex: mnesia schemas, ecto schemas, ets queries) from Elixir code, to Elixir code. Then my library can handle all the fussy details of whether or not it’s a viable match spec without requiring further knowledge.
Not quite sure I understand here—are you saying that you have a known set of ids on hand you want to retrieve verbatim, and doing a single
:ets.select/2call with a match spec is not as efficient as a series of:ets.lookup/2calls or a single:ets.match/2call with a match pattern?mguilmineau
I support your effort and I hope I wasn’t giving a different impression. match specs are not intuitive, too different from elixir syntax and little discussed on the web. The pre-compilation validation is a welcome addition. The detailed match specs we currently have look unnecessarily intimidating. There is value in this effort.
My comment on full scan: we tend to duplicate values in :ets storage with keys designed to match the queries we run frequently, so as to use lookup or match instead of more complicated and slower select match specs. In other words, while we do use match specs they tend to be a temporary stop gap but eventually get simpler or are removed entirely, primarily for performance reasons.
christhekeele
@mguilmineau Been a couple of years, but happy to report that recent work in the Matcha compiler allows full conversion of Elixir destructuring into ms guards, so the usage test suite based on your helpful examples is getting substantially more Elixir-ish.
mguilmineau
Just saw your note and clicked on the helpful examples link.
One thing that comes to mind is that :andalso (and :orelse) accepts any number of attributes sequentially, for example I have this:
A second thing I’ve learned since then is that >= is handled differently than < if you have nil values, so I must insert an :is_integer when comparing with >=
These weren’t obvious to me - not sure if either of this is useful for you & your library, but I’m sharing
Finally, found that the fastest way to generate an Elixir Map from an :ets output is to double up the brackets, as such:
and I even have this optimized way of collecting counts:
Cheers -
Mathieu
christhekeele
Excellent, I love me some edge cases!
I didn’t know that! I’ll have to play around with this form to see if it is a potential optimization.
I think this is a case of holistic term ordering, a standard caveat of comparison operators on the BEAM. Sadly there’s not much my compiler can do to “correct” this, but I hope that by passing all MS code thru the Elixir compiler first it will benefit from the new typing warnings coming to comparisons. I need to play around with how those intersect…
That’s slick! I’m pretty sure my compiler supports emitting map literals straight out of an MS but definitely should make sure that’s in the test cases.