In a functional language, an unspecified (variable) type you get back from a function basically has to appear somewhere in the type that you pass in. Otherwise, where would the value come from? So there are no (useful) functions of type 'a -> 'b.
It might help to think about the types that you're expecting to pass to your function, and how the extractor function would relate to them. It should be possible to have a fairly straightforward relation. Fighting with the type system usually means you're trying to do something that is likely to fail unexpectedly, which is what the type system is trying to prevent.
Edit:
Maybe what I'm trying to say is that 'a
and 'b
, the input and output types of your extractor function, aren't going to be arbitrary types. The input type 'a
is probably related somehow to the type 'c
. Similarly the result type 'b
is probably going to be related to the return type of the function xxx
as a whole. Without a little more info about these relationships, it's hard to make suggestions.
If you try to think of them as independent, arbitrary types you run into the parametric impossibility that I was talking about above. (It also requires advanced typing, rank 2 polymorphism I think it is.)
As a simple example, say that 'a
and 'c
are the same type and that 'b
is the return type of the function. Then your function would pretty much have to look like this:
let xxx ?extractor s =
match extractor with
| Some f -> f s
| None -> s
This has the behavior you wanted: the default extractor function is the identity function. But this also forces the return type of f
to be the same as its input type. So the type of xxx is:
val xxx : ?extractor:('a -> 'a) -> 'a -> 'a
Unless you want to get extremely fancy, there's really no way around this, and I suspect a fancy solution would introduce complexities that would outweigh the convenience of having the optional parameter.
Maybe it would work to have two functions. To continue the simplified example they would look like this:
# let xxx extractor s = extractor s;;
val xxx : ('a -> 'b) -> 'a -> 'b = <fun>
# let xxx_id s = xxx (fun x -> x) s;;
val xxx_id : 'a -> 'a = <fun>
I think these have the types you want, and the only inconvenience is that you have two different names.