10 ms·
Has there been any exploration into an idea where the problem with side effects is not that they make functions impure, but they are often against the metaphor
by taeric 13y ago
Has there been any exploration into an idea where the problem with side effects is not that they make functions impure, but they are often against the metaphor of the instructions being given? That is, if the metaphor that one is trying to model is traditional mathematics, then of course side effects are terrible.
However, consider a program where the main metaphor is controlling something. Logo, for example. Few people argue, I would think, that the traditional imperative styling there hurts and confuses things.
More extreme, consider stack based languages. These are strictly based on the current state of the program, yet my understanding is if you can fit your mind to that metaphor, it works very very well.
Or, my favorite category, cookbooks. Look at traditional baking directions: "Begin heating oven to XXX, mix dry ingredients in bowl, add butter, whip, ..." Doesn't get any more imperative than that, and yet people around the world often have great success replicating the desired results. (Granted, I often think that the difference in programming and teaching is that humans make an effort to understand what you were communicating, computers typically don't.)
Does this make sense?
- billsix 13y agoGuy Lewis Steele, Jr. and Gerald Jay Sussman. "The Art of the Interpreter of, the Modularity Complex (Parts Zero, One, and Two)". MIT AI Lab. AI Lab Memo AIM-453. May 1978 http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-453.pdf http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AIM-...
- taeric 13y agoHoly crap, just from the abstract I can't help but think many today would balk at the claim "More general side effects are also found to be necessary to promote modular style." Thank you very much for this link! I should have known to search for work by these authors. I can only dream of a world where I could have found their works more prominent in something like infoq.
- agentultra 13y agoThanks for the link to that paper! I recommend reading the section on "Top Level vs Referential Transparency" after reading this article. It argues that the trade-offs for absolute transparency require the programmer to define the entire world ahead of time. This trade-off is not acceptable by the authors of the paper for reasons elucidated in earlier sections and in SICP and so they demonstrate a version of EVAL whose environment allows for forward-references to top-level forms which we know is useful to writing dynamically compiled programs and decoupled modules. Every pure-FP language implementation has to make trade-offs with impure, mutable state and data. Haskell had to add the IO monad. Clojure has Var and Ref. Engineering isn't a discipline of absolutes. http://this-plt-life.tumblr.com/post/44462204757/simon-peyton-jones-adding-the-io-monad-to-haskell http://this-plt-life.tumblr.com/post/44462204757/simon-peyto...
- argv_empty 13y agoHas there been any exploration into an idea where the problem with side effects is not that they make functions impure, but they are often against the metaphor of the instructions being given? There are lots of posts on blogs, newsgroups, etc. about the aesthetic value of having one's code be math-like, but the working programmer doesn't necessarily care about that. That programmer probably does care about avoiding spooky action at a distance. Conforming to a "traditional math is [beautiful|divine|the true Dao]" ideology does not matter to that programmer as much as knowing what data a function depends on or affects.
- taeric 13y agoRight, this is essentially my question. Most of these posts start with the assumed position that one's code should be "math like." At least, that is how it seems to me. Yet we often find ourselves working with items such as a DOM which are far removed from how many of us think about "math like" items. My question is essentially have there been any approachable explorations of this topic? In my case, at least, preferably not starting from the perspective of category theory. :)
- argv_empty 13y agoWhich is your question? "Why is this useful?" or "How do I use this with something like the DOM?" or both or something else?
- taeric 13y agoMy question is if there are explorations of where "the aesthetic value of having one's code be math-like" is explored at a more objective level. Aesthetics are nice, but they do little to explore the ultimate goal of getting things done. I confess I really like the exploration in SICP of the value and place of imperative programming versus declarative programming. I do think there is a space for both, but I worry at the gymnastics we go through to never mix our metaphor away from "equation like" and over to "directions like."
- 13y ago
- dllthomas 13y agoObviously at some point you need to get something done. The question is how you organize your data and your actions.
- taeric 13y agoNot sure I follow. I'm essentially asking how this question is addressed with the single word answer of "functionally." I think a lot of this comes from the terrible division of "layers" that we have introduced in most common metaphors. Often we have to go through gymnastics to get all "mutations" back to a base layer where we can view them as "events" and then have pure functions that operate on these events. Consider, the common division of "Storage, Logic, Display" that we try and hammer out in some form or fashion. It becomes all too often that one can not come up with a good way of describing these systems where events only propagate along a single direction in these layers. As such, we often find small silos of "storage" in the logic and display area. (Similarly, we have a small silo of "logic" in both storage and display, quite often.) Coming up with a mechanism where there is a single "storage" location for an entire application often introduces complicated logic. Carmack had an interesting take on this idea for games programming, where the current logic "state" would be held immutable such that all calculations were only allowed to work on the current state. Kept those calculations easier, but introduced a new layer of effort to make that possible. Now, I think his argument was that this layer of effort could be seen as almost a constant factor that has shrunk in cost compared to the scaling cost of the number of actors involved in a modern system.
- dllthomas 13y agoWell, take that Logo procedure. If all I want to do with that list of commands is execute them, then a fully imperative language (like, as best I recall, Logo) is fine. Once I want to do slightly more complicated things (say, repeat) you need to move beyond "simple list of things". Of course, for a lot of things you might want to do to a list of commands (obviously including repeat) there is built in support. But what about when you want to define new ones? "Do each thing in the following list twice"? In Haskell, if you're passing around [IO ()], you can run it through (concatMap (\ x -> [x,x])) before passing it to sequence, and bam - things are doubled.
- jerf 13y ago"Does this make sense?" I've read your post three times, carefully, and no, I actually can not extract a clear question from it. (I do not mean that in a mean way, just as feedback.) It seems to resolve to an observation that functional programming is not the only paradigm that has produced successfully-running programs, which is certainly true, but not terribly information-rich. There are actually a lot of interesting possible directions you might be trying to go, but I'm not sure I want to try to guess them. (I will take one stab with: http://en.wikipedia.org/wiki/Effect_system http://en.wikipedia.org/wiki/Effect_system ) I will observe that baking directions aren't a useful comparison; the difference between programming paradigms do not manifest until you have something that is a real size. Imperative is just fine for a single page program. Anything is just fine for a single page program; I have unashamedly written outright spaghetti code for single page programs.
- taeric 13y agoNot taken in a mean way. Was an honest question in my post. :) Not sure how much I care for effect systems, just yet. Though, I think there is a high chance that is exactly what I want. My main observation from such things as baking directions seems to be that functional (being a highly declarative style) is great for high level, but that you need a way to carefully logic about the lower level details as well. To go with my baking example. The declarative would be, "Dinner should be enough pizza for X people..." This is great and all, especially if you are strictly at the management "high level" view of the task at hand. (Especially since this is likely to be part of a whole other set of declared outcomes. "Party room with X tables, etc...") However, one is not going to be able to actually get pizza for the crowd this way. Now, one might say that at the lowest level one can have relatively "pure" functions such as "dice onions." It certainly seems this is where most blog posts spend there time. At the overall high level declarations and the low level functions. Often pointing out that one could even implement this with the "order from local store" keeping the high level meaningful and completely divorced from the lower level. My problem is most of these posts/papers completely lose track of the area where the actual steps to make the pizza, or arrange the party room, take place. That is, at some level imperative works because it actually is easier to model in your mind. Why does nobody focus on identifying where that level of abstraction may be? Or do I just have a broken mind? :)
- reitzensteinm 13y agoOn the other hand, recipes don't maintain global state, so they expose a functional interface (ingredients->result). A lemon meringue pie for lunch is assumed to not influence the taste of roast pork for dinner. Yes, functional programming is analogous to cleaning your kitchen. You heard it here first, folks.
- taeric 13y agoOnly in a sense of "non-shared" resources do not influence each other. Trying to clean your kitchen while cooking is not the easiest task to consider. Similarly, not realizing you lack the ingredients to make both breakfast and dinner due to few eggs indicates a shared state between them. More directly, if you do not clean your resources, eventually you will not be able to complete a task. Of course, I think this is the main appeal of functional in today's world. We have so much computing resources that it often does seem we have unlimited memory and such. Essentially we move from cooking area to cooking area such that someone can clean up after us, and we only carry with us the items we are interested in.
- eru 13y agoCooking uses linear typing, hence, you can only use your eggs once. Clean is a purely functional language with linear typing.
- taeric 13y agoI've never heard of that language. Amusingly, I had toyed in my head with the idea of modeling this language. Seems you pretty much have to have some notion of "dynamic" typing, since the type of a variable changes on calls to it. Thanks for the pointer, here is a link, for those that will also find this interesting. http://wiki.clean.cs.ru.nl/Clean http://wiki.clean.cs.ru.nl/Clean
- eru 13y agoIt's not really dynamic typing..
- rodelrod 13y agoOr, my favorite category, cookbooks. Look at traditional baking directions: "Begin heating oven to XXX, mix dry ingredients in bowl, add butter, whip, ..." Doesn't get any more imperative than that, and yet people around the world often have great success replicating the desired results. After living with a chef for a couple of years, I came to believe that recipes would be much more effective if less imperative and more focussed on exposing the contribution and relative importance of each ingredient/process for the final result. In cooking there is much variation on the inputs: I want to know if the lemon juice is there for taste or acidity so I can tell if I have a bad lemon and for what I can replace it. It's like GPS navigation: the step by step instructions will often lead you off track. Let me see final destination on the map.
- taeric 13y agoBut aren't you just trying to get recipes to answer a question they weren't made to answer? That is, a recipe is often "how do I make biscuits?" The result is a known place. Not, "why do these ingredients mix together to make biscuits." That is an entirely different question that will require a fairly different answer. Now, a question that can be a bit of a hybrid is "why does this recipe make good tasting biscuits?" Starts with a limited scope and provides ingredient by ingredient level answers.
- rodelrod 13y agoRecipes try to micro-manage you instead of giving you the right information. This works in the same cases where micro-management works. You had plenty of biscuits before, you know how you like them, and if you have experience baking them you know what can go wrong. So for biscuits the recipe model is fine. But for that lobster bisque soufflé you just picked off Joel Robuchon's cookbook? Not so much.
- taeric 13y agoI'm not sure I follow. There are folks out there good enough at following recipes that they pretty much can just pick one up and run with it. Similarly, there are recipes that are so poorly done that they couldn't be followed in any reasonable sense. None of this really addresses the imperative/declarative point, though. If you were to put together a recipe that was nothing but the declarations of all of the interactions that must take place to make the desired food, I'm sure it could be done. At this point, "executing" the program would really just be finding the correct order to make the interactions happen. However, my fundamental challenge is that this program would be no more approachable for other programmers than alternatives. Less so, actually. As an example, SQL is a wonderful language for describing the data that one wants to get out of a database. It practically provides zero indication for how that fetching is actually done. For that, one creates an execution plan to logic about the process of getting the data.
- breckinloggins 13y ago> Does this make sense? Are you asking "Is it possible that purely functional programming is yet another paradigm that, like OOP, is fantastically useful but is still destined to be the square peg that is forced into too many round holes?" If so, then yes, you're making sense. And yes, that's probably what will happen. However, as with OOP, the end result should be a net gain in knowledge and maturity for our very young field and yet another wonderful tool in our tool box. Having said that, I think that with the "functional wave" comes a bit more understanding that this, too, may not be the perfect fit for every problem. Look at Clojure: they try hard to give you mutability in terms of abstractions that seem to encapsulate what it means for mutability to be a valuable thing. In Haskell, monads are a great model of "what it means to be imperative". If you think of imperative programming as: setup(); goLeft(); stop(); lookAround(); pickUp(ball); turnAround(); Then monads try to make precise just what exactly those semi-colons are doing. To me, the fact that this is hard to understand is mainly the result of the language being the playground of mathematicians who have long since forgotten why such concepts are hard. In other words: there is a "meta paradigm" going on here and it's just starting to emerge. That meta-paradigm is to provide a language that gives you the abstractions to build the right abstractions to ultimately build the star-shaped peg that fits into the star-shaped hole. So yes, if you're taking the long view then Clojure might be too opinionated for your tastes (and I have in fact opined on another thread today about what it would take to back slightly away from that lofty perch), then it sometimes can look like yet another oversold silver bullet language. But in the long term, we seem to be converging on something much healthier than the "stop using that paradigm and start using this one because it solves everything!" approach of the past.
- taeric 13y agoClose. I'm more asking if there has been study into ways of plugging the different paradigms together into a whole solution. That is, I firmly believe that each have their strengths. To carry a sports analogy through to this, teams practice different strategies and techniques in isolation so that together they can operate better. Seems most of the reading is about this isolated level of practice. I would love a more holistic view that covers it all. Edit: So, monads help show what happens under the scenes for when you are writing imperatively, but they do little to illuminate when that style is beneficial. My contention is that this is up to the metaphors one has defined for their program.
- samatman 13y ago(serve (fry (add-pan kale (fry (add-pan (dice onions)))))
- taeric 13y agoYou joke, but I've often thought how would one write a traditional recipe in a programming language in such a way that it would make sense. Same for "do it yourself" furniture. Ultimately, I always fall back on programs not having a good concept of "consuming" or otherwise changing a variable. This is clearly doable, and elsewise it was referenced as linear typing (new to me).
- angersock 13y agoC++ with references or C with pointers or Fortran with inout variables would accomplish this, just using normal procedural code: contents = open(&ramen); add_into( &pot, waterfactory() ); add_into( &pot, contents ); add_heat( &pot ); sleep( RAMENCOOKTIME ); tasty_ramen = open(&pot);
- taeric 13y agoRight. I did not think I was stating anything grand here. I was simply pointing out that "imperative" mixed with "mutable" state is not something that is hard for users to grasp. Especially when limited in scope to single threads of action. The hard part comes in making sure this "type checks." That is, if you had it dependently typed such that you could not "add_into( &pot, contents );" until after you had "add_into( &pot, waterfactory() );" than you could conceivably catch more bugs. The trick, to me, is that the "type" of "pot" is not fixed in this. Since I consider the "type" to be dependent on its state. I know this can be done with generic and higher order programming, but usually not with the same reference. That is, each call would create a new reference to pot such that the next one fed off of that. Thing is, that doesn't read well. (I mean, it does for toy examples, but doesn't for long ones.) Make sense?
- angersock 13y ago
- jhaywood 13y agoI think you're hitting up against something that is very subtle and it speaks to the fundamental nature of what it means to compute something. I think you can gain a bit of insight into what you're trying to get at if you expand the discussion to the interplay between mathematics and physics. Let's look at your example of baking. Indeed it seems like an imperative (physical) process. When we mix eggs and flour to form a batter we can't get the eggs and flour back! Indeed even cracking the eggs seems like destructive update to me. Think about why that is. We've performed a non-reversible transformation on our ingredients AND we can't go back in time! In this case time is implicit in our understanding. Now consider trying to model baking mathematically. We want to build an equation that will tell us the state of our cake. This will be function - that is dependent on time - time is now explicit in our model. Now we can say at t0 our eggs are whole. At some time in the future t1 our eggs are cracked. This doesn't negate the fact at t0 the eggs are whole. We can't go back in time and update what the state of the eggs then - that makes no sense! So here we see that when time is an explicit parameter we have time symmetry. The equations don't care if time goes backwards or forwards. And we can always do this. If you have some process that has a state that changes through time you can model that process with a static model that includes time as an explicit parameter. This means that our model is a higher dimensional look at the real thing. And our actual computation (or experiment or baking etc.) is a degenerate case of the more general model where the time parameter is actual time. How does this relate to functional programming versus imperative programming. Well it is the difference between theory and experiment. But in this case we also have to deal with the fact that our program is computed (a physical process happening in time) and so we're using a physical computation which follows the rules of mathematics to model mathematical rules using a degenerate case of those rules. So if it seems like there's an abstraction leaking it's because it kinda is. I like to think about it in this way because it highlights the very tight knit nature of physics and computation. Indeed a general theory of what it means to compute something could lead to a unified theory of physics and vice-versa.
- gruseom 13y agoThat's an insightful comment and an important point. Because the underlying computation is physical, there's no such thing as side-effect-free computation. Any attempt to treat programming as purely mathematical, i.e. pure FP, is going to run up against this physical substrate. You can't abstract it away completely, and it gets complicated if you try. So I don't think pure FP is likely to be an ultimate solution to the core problems of minimizing complexity and building scalable abstractions. It would be, if programming were math, but math is not executable. I believe one can even see this in the OP. Some of those examples seem more complicated than the side-effecty, loopy code that they're replacing. Maybe it is all habit and my mind has been warped by years of exposure to side effects and loops. But what if those things are more "native" to programming than pure FP can easily allow, and we need a more physical model of computation? I've often thought it would be interesting to try teaching programming as a kind of mechanics in which values are physical commodities that get moved around and modified—to see whether it would be more accessible to beginners that way. I also wonder what sort of programming languages one might come up with out of such a model. It would be an interesting experiment. One would use mathematical abstractions to reason about such a system, but would not think of the system itself as a mathematical one.
- joe_the_user 13y agoAn interesting thought. Recipes seem like a great metaphor to explore. The main is that a destructive functions can call a non-destructive functions freely but a non-destructive operation, say "find best egg" should not have any destructive effects whereas "beat egg" can call "find best egg freely". I know SQL separates destructive and non-destructive operations very clearly and it also allows the user to back out of some destructive action sequences if all the conditions aren't satisfied. This makes what to do when you don't have butter more easier solved.
- agumonkey 13y agoWhat's the longest recipe you've seen ? 12 steps ? 32 ? are they nested ? do they guarantee the ability to share stove, shelves, ... ? do they guarantee anything at all ? I wouldn't use the recipe metaphor except to explain what a computer does in less than 60 seconds to someone afraid of electronics.
- taeric 13y agoOf course... how many programs do you have running right now? Do they do anything to guarantee they can each use the main processor in isolation to the others? What about the ram? The keyboard? That is, the recipe for a single dish could be seen as a subroutine in the entire process pool for preparing a meal. Considered this way, what is the largest dining establishment you have seen? What is a menu, but a declarative listing of the strategies that an establishment can use to satisfy a customer's desire for dinner? More directly, all of the metaphors of programming have almost direct analogies into the real world. "Worker" threads. "Queues" of "tasks." As I've said elsewhere, I don't think I'm being particularly grand here. Just find it curious that, though we commonly have recipes and other directions that are mixes of declarative and imperative, the current trend in programming is to be purely declarative/functional. I wish I understood why that was.
- agumonkey 13y agoSimplicity of recipes (they're blurry, they're not chemistry) , and constant human oversight (your brain takes care of many things not written down) makes imperative instructions feasible for small tasks like a meal. It won't scale, but since it doesn't have to it survives.
- taeric 13y agoBut why won't it scale up? This is essentially my question. And, it is not like there are not explorations of cooking in non imperative forms. When your metaphor is "with these ingredients and devices, perform these transformations on them" an imperative form works really well. This is just as true for any algorithm you will want to write. Consider, write down any algorithm for how to do something. I challenge that key to your algorithm will be the idea of transforming something. Often in a "destructive" way. An example, with the concept of a browser that has state, the following algorithm will get you logged in to your bank. browser = new browser browser.location = "www.yourbank.com" browser.focusFieldLabeled("username") browser.type("your name") browser.focusFieldLabeled("password") browser.type("your password") browser.focusButtonLabeled("login").or("submit") browser.click() Implementing something that does this would be trivial, all things told. Now, do so without the metaphor of a "stateful" browser. Just trying to come up with "pure functional" implementations of each of these is not something that sounds pleasant to me. (I look forward to being surprised. :) )
- Dewie 13y agoMost decently popular posts about functional programming seems to have a good deal of meta-comments about functional programming such as "is functional programming even useful?", "it will take over the world!", "My father didn't need it, my brother didn't need it; I don't need it". Sometimes, the discussion thread is almost only meta-discussion of functional programming. Just an observation.
- taeric 13y agoNote, that I am decidedly not saying functional is bad. Just questioning why we do not focus on mixed usage of it and imperative. We used to. We don't any more.