Menhir: type variable name '_a is not allowed in programs

I have the following in menhir.

%start <('a t -> 'a)> doc

I am getting the following error:

File "parser1.mli", line 12, characters 55-58:
12 | val doc: (Lexing.lexbuf -> token) -> Lexing.lexbuf -> ('_a t -> '_a)
Error: The type variable name '_a is not allowed in programs

Does menhir not work with productions that produce value with type parameters?

My guess would be that in your grammar, the type for term that corresponds to doc couldn’t be generalized (not proven to be polymorphic).

What does the rule for doc look like? A common fix for that is to eta-expand it (transform it so that it looks like a fun).

Another issue is that a parser (or any computation) can never produce a value of polymorphic type 'a t -> 'a due to the (relaxed) value restriction. Being more explicit and producing the type

type u = { f: 'a. 'a t -> 'a }

might be an option.