Formula seems odd beyond the Either[Unit, *]. In the “success” case, it will produce another formula that may be fed (the same? another?) environment to produce yet another formula, etc. The only way to break out of this recursion is for a formula to “fail” with Unit. IOW, at best, Formula represents a chain of black-box, side-effecting computations that never yields an explicit result. This may make some sense with a mutable environment, but still - is this the intended semantics?
Forgetting that Formula feels weird. The reason why your code doesn’t work is because you have to return an Either, but you are returning some value and expecting the compiler to magically create the either for you.
// This should compile.
def mon_id(): Formula = (_Env: Environment) => {
Left(debug(“mon_id no match”))
}