6 ms·
What Is Declarative Programming (2013)
- aikah 12y ago> Declarativeness is a property of code which subsumes what professional developers typically refer to as functional and purely functional code. While real world applications don't allow us to solely rely on declarative code to solve all of our problems, we can embrace its positive qualities and learn to overcome its shortcomings by following two basic approaches: embedding and extending. I like the balanced conclusion. So as I understand it,languages like Haskell(which I dont know) wrap states into a type that can be passed to pure functions right?is that what monads are about? A bit like promises in javascript where the developpers wraps an eventual outcome into an object and can reason about his code without worrying about what the value will be or even if the value will be at all? More generally,how one does remove side-effects from OO code?since objects are statefull,or is the solution not using OO at all?
- sqrt17 12y agoA more paradigm-neutral notion of declarativity is that you separate intent (the function or relation that your program is to compute) from execution (the shoveling of bits that goes on in the actual execution of the program). For example, SQL or LinQ are declarative fragments embedded in an otherwise imperative/OO language, since you specify the intent, and behind the scenes, the database's query optimizer or the LinQ API can do all the optimizations they like because you've specified your intent and not a concrete plan for execution. In Haskell, treating state as data to be passed around has the advantage that you can also fork the state if you want to try out alternatives (i.e., search). Which means, you don't remove side-effects from OO code. You institute a contract between some provider of a declarative framework (that allows you to specify intents) and the consumer of said framework (who is supposed not to poke around in any state that the framework works on). I.e. state is seen in functional programming as undocumented API calls in imperative programming. In rare cases you need them, but you want to only use them where you actually get an advantage out of it, because it's bound to get in your way at some point if you're not careful.
- mp8 12y ago> So as I understand it,languages like Haskell(which I dont know) wrap states into a type that can be passed to pure functions right? I would restate this as: "In Haskell you write a description of a stateful computation." The "State Monad" is really just the illusion of mutable state, provided by passing the underlying state to each described computation. Actually-mutable state is possible with things like IORef, but these are impure.
- mbrock 12y agoYou could also say it's just an implementation of mutable state. The "State" type and its operations are just a few lines of simple Haskell code. The implementation uses parameters and return values to thread state between functions. It's just a simple abstraction of a simple pattern of programming. It uses a monadic interface, which is convenient and gives you some syntactic sugar, but there's nothing magical about it. From the Haskell point of view, so called "actually-mutable" state is on the other hand deeply magical, because it lets you do exotic things like having one thread modify a value in another thread. And that's possible through things like IORefs. But it should be avoided as much as possible.
- mp8 12y agoReally I just wanted to point out that the State Monad is not actually hiding any "real" mutation anywhere, nor is it related to the IO Monad- I was confused on this point for some time when learning Haskell.
- seanmcdirmid 12y agoHaskell actually lacks named state (in its safe world), meaning state is just a value to be passed around and returned. You can then index off of this state, but these names are then qualified. OOP is all about naming: objects encapsulate state, have identities if they are aliased, are thought of by humans in terms of nouns, have ontological is-a and has-a relationships, and so on. If it is just referential transparency you are after...then there are solutions for OOP (that also don't involve monads), but it requires a different programming model: http://research.microsoft.com/apps/pubs/default.aspx?id=211297 http://research.microsoft.com/apps/pubs/default.aspx?id=2112...
- deleted 12y ago[deleted]
- cwhy 12y agoCSS should be a very good example...
- adamnemecek 12y agoCSS is a markup though and not a Turing complete language. EDIT: Turns out that HTML5 + CSS3 are in fact accidentally Turing complete http://beza1e1.tuxen.de/articles/accidentally_turing_complete.html http://beza1e1.tuxen.de/articles/accidentally_turing_complet.... Regardless, it's still only accidentally Turing complete.
- ww520 12y agoSpreadsheet is a declarative programming system with a simple programming model and an amazingly intuitive UI, that has enabled a generation of non-programmers to do very complex computation and analysis.
- lliwta 12y agoSpreadsheets meet none of the three criteria given in the article: * Independence: Spreadsheets explicitly rely upon mutable state for <strike>all</strike> some commonly-used operations (i.e., the value of cells). * Statelessness: Non-independence implies non-statelessness. * Deterministic: Non-independence implies non-determinism. The reasoning for the last two point to an underlying problem with the criteria (removing the word "computation" from independence makes the three inter-reducible in any real-world machine). but yeah. edit: all functions->some commonly-used operations edit wrt downvotes: perhaps reserve judgement unless you've maintained a very large spreadsheet-based application written by non-programmers. Spreadsheets -- as most commonly used in practice -- do not meet the criteria above, except for the smallest/most trivial of applications. Period. Now, perhaps the problem is the definition. Or perhaps the problem is that spreadsheets are awful at-scale (even assembly can be quite elegant for small problems or when usage is well-constrained). Or maybe both.
- lmm 12y ago> Spreadsheets explicitly rely upon mutable state for all functions (i.e., the value of cells) How so? The values in the cells don't change as a spreadsheet "runs". You put the formulae in and the output is there, conceptually instantaneously. Some spreadsheets have some cells that are seen as "input" and expected to be edited, but that corresponds rather well to monadic I/O.
- lliwta 12y agoTo name a few: 1. Macros. And you don't get to weasel, because any non-trivial spreadsheet application contains at least a few. 2. relative references, which are basically heap pointers. 3. simulating for loops/iteration is a fairly common thing in spread-sheet programming. But also see my edit to the original comment. As an aside, it's absolutely astounding to me that my original reply is down-voted. edit: most obvious -> a few edit2: Could someone please explain what's so offensive about these posts? Is there that much love for spreadsheet programming (last I checked everyone understood the downsides of at-scale spreadsheets and how these problems arise from subtle non-declarative features)? Am I being unintentionally abrasive? Should I post example files and links to empirical studies demonstrating how people commonly program spreadsheets in non-declarative ways?
- lispm 12y ago"We say the operation is declarative if, whenever called with the same arguments, it returns the same results independent of any other computation state." That's the use of the term 'declarative programming' by the functional programming community. There are some other uses of it. Usually in computer science 'declarative programming' means: the problem is described and way to the solution is determined from the problem description. A declarative program describes 'what' and not 'how'. The detailed control flow is not described. • specify static properties of problems * solutions are computed * no explicit dynamic behaviour see also: http://en.wikipedia.org/wiki/Declarative_programming http://en.wikipedia.org/wiki/Declarative_programming
- seanmcdirmid 12y agoI've seen "declarative" languages that included if statements (explicit dynamic behavior!) still passed off as declarative. Some even add sequencing, and what not. However, its ok because they aren't part of the language core, just escape hatches on expressiveness :) "Declarative" always exists in a spectrum: one's specification is another's implementation. It used to be that Fortran was "declarative" because you no longer had to write machine code and schedule instructions by hand! In truth, it is about how many, not just the kind, details can be omitted. E.g. the list order element is probably important, and if HTML couldn't guarantee that this order would be preserved at run-time, there would be some big problems.
- leoc 12y ago> "Declarative" always exists in a spectrum: one's specification is another's implementation. I'll go further: statelessness and applicativity obviously have an intimate and important relationship with declarativity ("what rather than how") (just as non-Turing-equivalence does), but they're not the same, and trying to simply define the latter as the former is an annoying PR wheeze that spreads obfuscation.
- innguest 12y ago> It used to be that Fortran was "declarative" because you no longer had to write machine code and schedule instructions by hand! Maybe, but even at that time, pure mathematics was still more declarative than Fortran, so it's not a relative term. > In truth, it is about how many, not just the kind, details can be omitted. I think it's about the kind of details that can be omitted. If I can omit implementation details (how to execute my code on a given machine) from my function declaration, then that is a declarative function.
- renarenasaini 12y agomy co-worker's sister-in-law got paid $12019 the prior week. she gets paid on the laptop and bought a $391300 house. All she did was get fortunate and put to work the steps uncovered on this web site ,,,,,,,, ,,,,,,,, WWW.MAX43.COM
- lliwta 12y agoThis blog post points out problems with the three criteria given in the article for what "declarative" means: http://existentialtype.wordpress.com/2013/07/18/what-if-anything-is-a-declarative-language/ http://existentialtype.wordpress.com/2013/07/18/what-if-anyt... edit: addresses -> points out problems with. edit2: the blog post also addresses the "referential transparency" characterization.