Open gelisam opened 1 year ago
Here is one possibility.
Once we implement #105, we could scrap which-problem
and instead define separate syntax parameters indicating whether the current context is X or not. So the (current-expression-type)
syntax parameter would provide a (Maybe Type)
, the (current-pattern-context)
syntax parameter would return a (Maybe Unit)
, and the library could define a (current-fancy-pattern-context)
syntax parameter of type (Maybe (Syntax, Macro Syntax, Macro Syntax))
, to explicitly provide scrutinee and the continuations to the user's macro.
One difficulty with that approach is that setting the other syntax parameters to Nothing
only a convention, and one which is hard to follow if different libraries can define different syntax parameters and don't know about each other. So maybe the kernel should provide a facility for registering a syntax parameter and setting all the registered syntax parameters to Nothing
?
Currently, the kernel language defines a Problem datatype which lists a small number of problems which the macro might currently need to solve, such as an expression, a type, or a pattern. It would be nice to allow libraries to extend this list.
For example, imagine an extensible library which provides pattern guards and allows the user to define variants, such as view patterns. The library must define a convention, which the user must follow when writing macros which cooperate with the library. The convention could be:
scrutinee
,on-success
, andon-failure
let
which binds that variable and callon-success
within the body of thelet
on-failure
Thanks to this convention, it is now possible for the user to write a macro which cooperates with this library... But consider a macro like
list
, which makes sense in an expression context as well as a pattern context. Wouldn't it make sense to also allowlist
to cooperate with the pattern guard library? Alas, it cannot, becausewhich-problem
will return the same value,(expression _)
, in two of those three contexts. If the library could extend the list of possible problems, e.g. to addfancy-pattern
, thenlist
could distinguish between those two contexts.