3 ms·
But isn't this already solved in an OOP world? So long as your "list of actions" is against a target (the "metaphor" in my original post), you can accomplish w
by taeric 13y ago
But isn't this already solved in an OOP world? So long as your "list of actions" is against a target (the "metaphor" in my original post), you can accomplish what you are referring to by manipulating that target.
To go with my dance suggestion:
function dance(dancer) {
dancer.stepForward
dancer.crossStepForward
dancer.crossStepForward
}
Now, before the dancer is passed to this function, it is first passed to a "goOnPoint" function, such that now those directions are taken while on point. Or a "do all steps twice" function, for your direct example.
The cooking metaphor is a little more directly applicable here. The cook is just configured to double (or half, etc) all ingredients used. Heck, the cook can even be configured such that substitute ingredients are known. Consider, it is trivial to use soy milk instead of regular milk for many "recipes".
I actually like that example, because it doesn't always work. In a sense, it doesn't "type check." Some recipes have to basically be flagged as "milk substitutes not applicable." I fail to see how a monad really helps there.
- dllthomas 13y agoSure, you can go around and implement each special handling in every object that accepts actions. That potentially a lot of boiler plate, though. The functional approach is to view the actions as first class objects and operate on them before sequencing them together, whether that sequencing is done by an object in IO. > The cooking metaphor is a little more directly applicable here. The cook is just configured to double (or half, etc) all ingredients used. Heck, the cook can even be configured such that substitute ingredients are known. Consider, it is trivial to use soy milk instead of regular milk for many "recipes". > I actually like that example, because it doesn't always work. In a sense, it doesn't "type check." Some recipes have to basically be flagged as "milk substitutes not applicable." I fail to see how a monad really helps there. Again, the problem there is that you have to write your cook such that it can perform whatever transformations might be needed, rather than having the ability to apply arbitrary transformations. Regarding the type checking, I don't see how OO helps you there. In Haskell, monads (with type inference and typeclasses more generally) are absolutely a solution: instance Monad RecipeCanSubSoymilk where ... instance Monad RecipeNoSubSoymilk where ... instance Recipe RecipeCanSubSoymilk where ... instance Recipe RecipeNoSubSoymilk where ... noSoy :: RecipeNoSubSoymilk () milk :: Recipe r => Quantity -> r () flour :: Recipe r => Quantity -> r () subSoymilk :: Recipe r => RecipeCanSubSoymilk a -> r a This lets us say: subSoymilk (milk 10 >> flour 5) but provides a type error when we say: subSoymilk (milk 10 >> noSoy >> flour 5) and this will propagate through any depth of functions &c that we use to build our recipe. Though monads are overkill in this case: a list will do just fine, since we never need the result of a step to determine what to do next. But it's nice to have the flexibility when needed.
- taeric 13y agoSorry, when I said I fail to see how a monad helps here, I meant more in the "keeping the algorithm clear" sense. Simply put, there is a reason cookbooks do not resort to the idea of monads to convey directions. Thinking more on this, I am beginning to think a multiple phase "type checking" would make the most sense. Essentially, have the declarative ingredients list, the imperative commands, and optionally any declarative constraints. I strongly feel that the use of nominative subtyping is a very bad idea for the "constraints" of what goes into an algorithm leads to hardly readable stuff. I grant, though, that it may be more fruitful to train one's self to read these "hardly readable" things than it is to opine the lack of a better alternative.
- deleted 13y ago[deleted]
- dllthomas 13y agoYou're conflating and confusing a whole ton of things, to the point where I don't really know where to begin... There is "monad as a mathematical concept", which cookbooks don't "resort to" discussion of because a rigorous treatment of their internal logic isn't what cookbooks are about. A logician sitting down to describe and analyze the processes in a cookbook may well make reference to monads. Cookbooks don't care much about such description and analysis, because recipes are tremendously simple (compared to software), they rely on a ton of implicit knowledge, and they can be exhaustively tested. There is "monad as an interface as it is used in Haskell", which cookbooks don't "resort to" because they are one particular DSL and the sole relevant context is implicit by the fact you opened a cookbook and are looking at something laid out like a recipe. It's not terribly difficult to characterize a recipe as a monad, though if you wanted to do so precisely you'd probably also want linear types (since you can't chop the same onion twice). Regarding your later points of division of "imperative commands" and denotational constraints, I actually agree with you that splitting these but allowing some specification of both (I would actually broaden "imperative commands" to operational constraints generally) is going to be a worthwhile avenue in the medium to long term. I think, though, that it will look more like writing things out denotationally based on your actual goals (plus some operational constraints regarding time/space/resource bounds) and then filling in operational details as the compiler is unable to come up with how to do something on its own in a way that meets the requirements, as opposed to detailing how to do things and checking constraints.