It seems I'm using over and over a pattern that I would like to abstract as a function. The idea behind the pattern is that maybe I have something and if not I can try to produce it. Here is some OCaml code for the function I'm interested in naming, but the problem isn't OCaml specific. I looked for a Haskell precedent but I haven't seen such a function in the Data.Maybe module and hoogle didn't help: http://www.haskell.org/hoogle/?hoogle=Maybe+b+-%3E+%28a+-%3E+Maybe+b%29+-%3E+a+-%3E+Maybe+b.
let my_function a f arg = match a with
| None -> f arg
| Some _ -> a
This is almost like having a default potential value, but it avoid the need for generating the default if we have a value already.
Edit:
The reason I need this type is that I have a combinatorial problem to solve and a set of heuristics to solve it (say h1 and h2). h1 is faster than h2. None of these heuristics is guaranteed to find a solution, though. So I chain them and try them in order. Something like
match my_function (h1 problem) h2 problem with
| None -> "bad luck"
| Some solution -> "hurray"