Use of Ppxlib: how to convert an expression into a pattern?

let exp = [%expr (a, b)]
let pat = ? exp

There is no general way to convert an expression into a pattern (eg what would be the pattern corresponding to 1 + 1 ?). Of course, some expression do correspond to patterns; you can try writing a function Parsetree.expression -> Parsetree.pattern that fails on those expressions that do not correspond to a pattern, but as far as I know there isn’t anything already available in ppxlib.

1 Like

I bet I need a Parsetree.expression -> Parsetree.pattern option

What are you trying to do? There might be a way to do this without converting an arbitrary pattern into an expression.

Thanks a lot for this concern!
However, it’s exactly what I want… I mean it’s almost my direct use.
I want to convert a binop b; c into

proceed b @@ function
| a -> c
| x -> fail x

where binop is an infix operator.

Monad is an instance(when proceed = bind), and in this case what I want to do will get specialized to do-notations:

b := a
c := f b
h g
bind a @@ fun b ->
bind (f b) @@ fun c ->
h g