Declarative versus Imperative


I responded to a recent discussion Declarative vs imperative programming on the Types mailing list, but my post was rejected because of a bad mail header. By the time I got around to fixing it, the discussion was old. But for what it's worth, here are my thoughts:
------
I have been thinking about the use of the words "declarative" and "imperative" for some time. This is my understanding of how they are commonly used in computer science today:
Declarative: describing "what" is to be computed rather than "how" to compute the result/behavior
Imperative: a description of a computation that involves implicit effects, usually mutable state and input/output.
As others have pointed out, our usage originally came from the distinction between imperative and declarative statements in natural languages. However, I think that our usage has diverged significantly from this origin, so that the words no longer form a dichotomy.
For example, one might argue that a finite state machine is both declarative and imperative in the computer science senses. I think Uday made a similar suggestion. There is also Wadler's classic paper on "How to declare an imperative".
Another hint that "declarative" and "imperative" are not antonyms is that the definitions don't have any significant words in common. The antonym of "imperative" in common usage is "pure functional". I don't know of a widely-used word that acts as an antonym for "declarative", although "operational" might work. It may be that "imperative" has a connotation of "step-by-step", which hints at it being the opposite of "declarative", but this connotation is fairly weak at this point. If we wanted to we could try to force “declarative” and “imperative” to be antonyms, possibly by redefining what we mean by “imperative”, but I'm not sure that would be an improvement.
I agree with those who say that "declarative" is a spectrum. For example, some people say that Haskell is a declarative language, but I my view Haskell programs are very much about *how* to compute a result. It is true that many details about how are left out (memory management, order of operations, etc). But if you compare a Haskell program with a logical specification (pre/post conditions), they are quite different. Thus while I would say Haskell is more declarative than many other programming languages, Haskell is not a declarative language in the strongest sense of the word. Haskell programs are not specifications, they are computations, in the sense that they say how to compute and answer.
Here is a quick shot at a spectrum between "how" and "what". Each level has a quick summary of the "how" that is involved, and it also includes all the "hows" listed below them. I suspect that many of you might disagree with the placement or absence of various languages, so I cannot claim that this list is definitive.
 More "How"
How the machine works
   Assembly
How memory is managed
   C, C++, etc
Order of operations
   Java, Smalltalk, Python, Ruby, etc
How data flows (with issues like nontermination and cut)
   Haskell, Prolog, Lambda Calculus (in various forms)
----split between Programming and Specification Languages---
Restricted Specification Languages
   BNF, SQL, Excel, Statecharts
Logical specification languages
   VDM, Z, B, Alloy
 More "What"
The idea that a specification language (by definition) cannot be executed is widely held but false. I consider BNF to be a simple counter example. BNF is clearly a specification language, and it clearly has efficient execution (parsing) strategies.
As for the objects/type distinction that sparked this discussion, I think the discussion was pretty reasonable, and I would like to thank Jonathan for presenting my views so articulately.
I agree with Uday that we need to get our own house in order. These kinds of discussions are a good start.

Advice for faculty candidates interviewing at UTCS

Everybody has heard the advice that job talks must be "accessible to a broad audience". Some people take this to mean that "all of the talk must be understandable by a broad CS audience". Unfortunately this leads the speaker to remove all the depth from their talk, for fear that somebody in the audience might not understand it. The resulting talks are underwhelming.

Keshav Pingali suggested this recipe based on thirds: 1/3 of your talk should be understandable by everyone. This should include the motivation and the high-level contribution and significance of the work. 1/3 of your talk should be understandable by people in your general area. Finally, 1/3 should be understandable by specialists working on your particular problem.

I'm not sure I would go so far as Keshav. But I do agree that there must be at least part of your talk where you describe the key problem and your solution in detail, and that this is likely to be fully understood by only a few people in the audience. But that's OK.

So PLEASE keep the depth in your talks and don't worry too much that somebody might not be able to follow. Just think about how many job talks you've been to where you haven't understood everything. It's OK. Take a deep breath. Show your depth!

Revised definitions of object and object-oriented

I revised my proposed definition of object based on feedback, to be more general. Please put comments on the original post. I'm very interested in getting more feedback on the definition.

The Day Functional Programming Became Pure

The definition of "functional programming" changed slowly over the last 20 years, as expressed on the Wikipedia page on Functional Programming. Perhaps the process started even earlier with John Backus's famous Turing Award lecture Can programming be liberated from the von Neumann style?: a functional style and its algebra of programs. [Note, I have revised this to put the quotations in chronological order and add a new paragraph on the end]

Before 14 August 2003, the page described functional programming as a style that emphasized the use of functions. For example on 14 October 2001‎ the entire page was 220 words long, and began:
Functional programming is a style of programming that emphasizes the evaluation of functional expressions, rather than execution of commands. The expressions in these languages are formed by using functions to combine basic values.  
A functional programming language is a language that supports and encourages functional programming. The oldest example is LISP. More recent examples include Scheme, ML, Haskell, Erlang, Clean.
Lisp is identified as the first functional language, and Schema, ML and Haskell are on equal footing.

The changes started on On 14 August 2003 when the pages from Nupedia, which had a structured and peer reviewed model for content production, were integrated into Wikipedia by Luxor to create a page with 1,640 words:
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. In contrast to imperative programming, functional programming emphasizes the evaluation of functional expressions, rather than execution of commands. The expressions in these languages are formed by using functions to combine basic values.  

At this point functional programming is not about emphasizing use of functions. Instead it means programming with mathematical functions. Implicit in this definition is that such functions are pure, but because it is not explicit the implications were not clear. This description was elaborated until 29 May 2006, when the page read:
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. Functional programming emphasizes the definition of functions rather than the implementation of state machines, in contrast to procedural programming, which emphasizes the execution of sequential commands. A purely functional program does not modify state to produce values (as is done in imperative programming), it constructs new values from (but does not overwrite) existing values. 
There is no uniform agreement on what constitutes functional programming or a functional programming language. Often considered important are higher-order and first-class functions, closures, and recursion. Other common features of functional programming languages are continuations, Hindley-Milner type inference systems, non-strict evaluation (i.e. "laziness"), and monads.
There is still a distinction between (ordinary) functional programming and the "pure" form of functional programming, which avoids the use of mutable state. At this point there was a small but significant change, in the middle of the night, with no fanfare, by an unknown person. At 01:07 on 29 May 2006 it was changed to read:
Functional programming is a programming style that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional programming emphasizes the definition of functions, in contrast to procedural programming, which emphasizes the execution of sequential commands. 
Functional programming relies on concepts from the lambda calculus, Lisp, and more recently Haskell. Frequently mentioned are the avoidance of state and side-effects (which provides for referential transparency), higher order functions, recursion, and closures. 
At a stroke, the author made explicit the requirement for purity and added it to the definition of functional programming. This requirement had been implicit in the description of functional programming, as computation with mathematical functions. This author also resolved the uncertainty about how functional programming should be defined. Prior pages said there is no agreement, but the new definition was stated without such doubts.

The change was made by ideogram, whose real-world identity is not disclosed. This account was created a few days before the active editing of the Functional Programming page started. Since then the account has been banned numerous times for unacceptable behavior (trolling) but this seems to be urelated to the editing of the Functional Programming page.

They didn't leave a revision comment. On the other hand, there is some discussion about the issues on the Functional Programming Talk page. There have been edits since this point, which have restored some of the previous language, but they are later removed. Currently (July 14, 2012) the page reads:
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.[1] Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.[1] 
In practice, the difference between a mathematical function and the notion of a "function" used in imperative programming is that imperative functions can have side effects, changing the value of program state. Because of this, they lack referential transparency, i.e. the same language expression can result in different values at different times depending on the state of the executing program. Conversely, in functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
[1] Paul Hudak. 1989. Conception, evolution, and application of functional programming languages. ACM Computing Surveys volume 21, issue 3 (September 1989).
What are the implications of this? How did this change come about? Are we in the midst of a (small) scientific revolution? Who is behind this?


According to the new definition ML and Lisp are no longer functional languages. It is possible to write some some in a functional subset of these languages. The first requirement is avoiding the use of "ref" types in ML, or the use of assignment and structural mutation in Lisp languages. However, this is not enough, because many of the standard libraries for ML and Lisp languages have side-effects. This is especially true of any libraries for IO. As a result, the range of program that can be conveniently written in a functional style is fairly limited.

Interesting discussion on DSLs

Discussion of Domain Specific Languages (DSL) on Lambda-the-Ultimate. Originally the discussion was sparked by a Facebook status update by Erik Meijer. But not everybody is friends with Erik, so it was not public. Please put comments on the LtU page.

Why do North Americans not like MODELS?

Why don't North Americans (US+CA) publish work at the MODELS conference? The conference is often outside the US, but even when it is located in the US (Denver), North Americans don't publish there in great numbers. Even then, there is a higher percentage of North American papers at ECOOP, which is never located in North America. We don't like AOSD very much either.

ConferenceLocationNorth American papers
Models 12Austria16%
Models 09US21%
AOSD 12Germany22%
ECOOP 11UK35%
OOPSLA 11US54%

Of course, maybe they do submit papers but they just aren't accepted :-)

Update: The numbers for USA are even worse:


ConferenceLocationUS papers
Models 12Austria6%
Models 09US12%
AOSD 12Germany22%
ECOOP 11UK31%
OOPSLA 11US52%

UML is the worst thing to ever happen to MDE/MDD

I tweeted about the relationship between UML and MDD.
There was also some discussion on StackExchange.

It has since become clear to me that UML is perfectly reasonable for
its purpose: to describe OO designs. But UML is not appropriate for
doing serious Model-Driven Development. It was OMG that tried
to connect the two, in promoting MDA. That was a mistake.

I think that DSL research has many of the same goals as MDD, and
the connection between these two threads of research will eventually
bear fruit.

Skype UI Fail

Does anybody else think the new MacOS Skype 5.x user interface is terrible? The contact list takes up a huge amount of real estate, and if you try to make it smaller, the names of your friends are shortened to 2 characters and the rest of the window is unused space. Grrrrr

Emails with Bob Harper

I ran into Bob Harper at POPL in Philadelphia. We chatted briefly in the hallway. We invariably disagree on most topics when we talk, but the conversation was pleasant enough. I enjoy talking to Bob, since he has a strong world-view and is a very smart guy. When I got home, Bob wrote me a followup note. After a few emails I noticed that he had updated his draft book, Practical Foundations for Programming Languages, to include two chapters that touch on topics related to object-oriented programming. I believe that a previous version of the book only mentioned objects once, in a footnote, so I was curious to read what Bob had to say about objects. I have an special interest in this question given the strong and unsupported criticisms that Bob has made about object-oriented programming in his blog, Existential Type. Over the next few weeks we exchanged almost 50 emails about the book and object-oriented programming in general.

I previously started posting the messages, because it is an interesting story. I viewed our discussion as a continuation of the debate that we began in person at POPL. Bob never manages to support his criticisms of object-oriented programming, and in the end shows that his criticisms are based on misunderstanding of the subject. At least, that is my interpretation of our exchange. He conclusions are completely different, I am sure. I have since decided to remove the posts. Just because Bob is impolite, does not mean that I have the right to be as well. If my actions offended anyone, I apologize. I will write some separate notes on his book, based on the email discussion. He has already modified the book and removed some of the unsupported claims based on our conversation.

I am still concerned with the fundamental internal conflict and lack of understanding between different camps within programming languages. It is unhealthy and reduces our reputation within the larger community. No other sub-discipline within computer science is so fundamentally at odds with itself. I will find another way to address these issues.

Dell service call... slow, confused.. but gets the job done

This is an automated email sent from Dell Chat. The following information
is a log of your session. Please save the log for your records.












































































































































































04:05:15 Session Started with Agent
04:05:22 William: "Hi"
04:05:23 Agent: "Thank you for contacting Dell XPS Premium Support. My name is XXXX and my rep ID number is #####. How may I help you today?"
04:05:43 William: "I was just talking with tech support about my XPL laptop."
04:05:56 William: "They tried to connect me to hardware support, but the connection failed"
04:06:09 William: "we are having trouble communicating, so I thought I'd try chat"
04:06:25 Agent: "I appreciate the opportunity to assist you with this issue."
04:06:31 Agent: "Please allow me 2 to 3 minutes to pull up your account information. In the meantime, May I have an alternate contact number and the best time to reach you if we have to follow up on the case?"
04:06:52 William: "the phone number I gave on the login screen here is good. Do you have it?"
04:07:10 Agent: "Yes."
04:08:55 Agent: "I see that you are contacting us regarding your XPS L401X , which has windows 7 as the Operating System. Am I correct?"
04:09:12 William: "yes"
04:09:44 Agent: "Okay."
04:09:57 Agent: "May i know what exactly is the issue?"
04:11:13 William: "THe computer started being slow... VERY slow. opening a folder window took 5 minutes"
04:11:27 William: "then i reinstalled the operating system.. but it hung during installation and did not complete"
04:11:39 William: "I ran diagnostics and got a disk error"
04:11:59 William: "2000-0146"
04:12:13 William: "it cannot boot at all"
04:12:52 Agent: "Okay."
04:12:52 William: "another diagnostic gave DTS Self-test Read error with number 0F00-065D"
04:13:04 William: "i'm getting tired of repeating this problem over and over again to different people"
04:14:25 Agent: "I will make sure that this would be the last time you the explain the issue , i will resolve it my the end of the chat."
04:15:04 Agent: "If you got 146 error we need to run the extended test on hard drive."
04:15:12 Agent: "Are we chatting from the same computer?"
04:15:27 William: "no (its broken :-)"
04:16:12 William: "how do I run the test?"
04:17:05 Agent: "Turn on the computer and keep on tapping f12 key on the keyboard."
04:17:33 William: "ok"
04:17:42 Agent: "I understand that but we need a code in order to replace the hard drive without which it cannot be processed."
04:17:52 William: "its fine"
04:18:20 William: "i've done the f12 step.. have the "please select boot device" screen. Please go as quickly as you can"
04:20:36 Agent: "Sure."
04:20:42 Agent: "Select diagnostics."
04:21:00 William: "there is no option named "diagnostics""
04:21:17 William: "it has "har drive" "Cd drive" "Network" "Enter PSA+" and "Enter Setup""
04:21:58 William: "the last person asked for "diagnostics" too. It annoys me that you do not know the options in your own product"
04:22:33 Agent: "William by default it should have the option."
04:22:50 William: "well.. it doesn't"
04:22:53 William: "i haven't done anything to the computer"
04:23:08 William: "PSA+ is "pre-boot system assesement" test"
04:23:28 Agent: "But as the hard drive has issue it is not displaying it select Psa++"
04:24:59 William: "if you say so"
04:26:04 Agent: "So once you select that it will run the test and give you the error 146 and after that it will asks you to continue the test or nor select continue."
04:26:07 William: "it is doing tests now.."
04:26:18 William: "yes, that is right"
04:26:50 William: "i've been waiting foryou to tell me what to do then"
04:28:01 Agent: "select continue and it will continue the test and give you some options let me know what are they?"
04:28:37 William: "it is continuing with memory test... it wil take about 5 minutes i think. there is no way to skip that test and get to the advanced options... so we must wait"
04:28:59 Agent: "Yes."
04:34:35 William: "ok.. it is done"
04:34:48 William: "it says "do you want to run the remaining tests?""
04:34:55 William: "it will take 30 minutes or more"
04:35:08 William: "but i think these tests are different from the on-disk diagnostis"
04:35:09 Agent: "Select no."
04:35:14 William: "ok"
04:35:21 Agent: "And the it will give you options."
04:35:25 William: "booting utility partition"
04:35:41 William: "options: test memory/ test system/ exit"
04:36:55 Agent: "Select test system."
04:37:09 William: "ok.. its going"
04:37:56 William: "express test/ extended test/ custom test/ symptom tree"
04:38:18 Agent: "Select custom ."
04:39:41 William: "ok"
04:39:45 William: "(can you go faster?)"
04:40:18 Agent: "Sorry we need to run the tests then only we can see the error."
04:40:30 Agent: "Have you select custom?"
04:40:47 William: "yes. I already said "ok""
04:40:59 Agent: "OK."
04:41:05 William: "I just want you to respond more quickly. I am waiting a long time between each message"
04:41:21 Agent: "Oh i am sorry you felt so."
04:41:24 Agent: "Sure."
04:41:56 Agent: "Now it will start the test and this test will take around 30 min."
04:42:15 Agent: "So if you want i can stay or i will give you a call."
04:42:15 William: "which tests should I select in the custom area?"
04:42:35 Agent: "Hard drive."
04:42:59 William: "hmm.. you did not say that before"
04:43:23 William: "which tests?"
04:43:42 Agent: "Hard drive extended test."
04:43:52 William: "that's not an option"
04:43:55 William: "it has"
04:43:58 William: "confidence"
04:44:04 William: "device quick check"
04:44:09 William: "derive self-test (short)"
04:44:13 William: "drive self-test (long)"
04:44:16 William: "read test"
04:44:18 William: "seek test"
04:44:22 William: "SMART test"
04:44:25 William: "Verify test"
04:44:42 Agent: "Drive self test long is know as extended test."
04:44:46 Agent: "So please select it."
04:44:57 Agent: "Known*"
04:45:07 William: ""known as" doesn't help me, since you are not using the same words as I'm seeing."
04:45:26 William: "just that one? nothing else?"
04:45:26 Agent: "It will take 30 min to complete this test ."
04:45:38 Agent: "Only that one."
04:45:49 William: "ok.. its running"
04:45:56 Agent: "Perfect."
04:46:03 William: "estimted completion time 113 minutes"
04:46:20 William: "ahh.. it failed already"
04:46:33 William: "0F00-075D"
04:46:39 Agent: "Okay what is the error message?"
04:46:44 William: "that is the error code"
04:47:00 William: "Msg: DISK -DST Sefl-test Read error"
04:47:13 William: "continue testing?"
04:47:26 Agent: "Yes."
04:48:31 Agent: "Is it running?"
04:48:40 William: "oh.. it seems to have stopped the test"
04:48:49 William: ""continue testing" seems to mean "select another test to run""
04:49:06 William: "there is an "X" on that test, since it failed"
04:49:34 Agent: "In this case we will replace the hard drive."
04:49:56 Agent: "Are you comfortable in replacing the hard drive?"
04:50:38 Agent: "This is not to rush you, please respond if we are connected."
04:50:47 William: "ye"
04:50:47 William: "yes"
04:51:00 Agent: "Okay."
04:51:21 William: "yes, i can probably replace it, if its not too difficult"
04:51:27 William: "i have done things like this before"
04:51:30 Agent: "So i will send you the hard drive which will reach your place within 2 to3 business days."
04:51:30 William: "on computers"
04:51:40 William: "ok"
04:51:42 Agent: "Okay that really great."
04:53:50 Agent: "Thank you."
04:54:44 Agent: "You will also get a call from dell to check if the issue is resolved or not after 3 days ."
04:54:53 Agent: "And would there be anything else that I may assist you with today?"
04:55:51 Agent: "It was pleasure assisting you ."
04:56:48 William: "thanks"
04:57:12 Agent: "You are welcome."


If you require further assistance, please visit us at support.dell.com

Talk at MSR on Batches

I recently gave a talk on Batches at Microsoft Research. I think them for being nice enough to host my visit and record the talk.

Batches are a new approach to relational database access, remote procedure calls, and web services.

A Remote Batch statement combines remote and local execution: all the remote code is executed in a single round-trip to the server, where all data sent to the server and results from the batch are communicated in bulk. Batches support remote blocks, iteration and conditionals, and local handling of remote exceptions. Batches are efficient even for fine-grained interfaces, eliminating the need for hand-optimized server interfaces.

Batch services also provide a simple and powerful interface to relational databases, with support for arbitrary nested queries and bulk updates. One important property of the system is that a single batch statement always generates a constant number of SQL queries, no matter how many nested loops are used.

A few more messages about Ensō

I have been telling people that we need to work on guided synthesis over verification. I believe that guided synthesis is the only way we are going to be able to create correct programs in practice. Verification is just too hard, because you have to write the actual program, write the specification, and then develop the verification proof. Maintenance is difficult, because if any of these parts change you have to update the other two. I think that guided synthesis would work better, because you can write do the specification, then write down the steps and designs to convert it into efficient code. I can imagine doing maintenance on such a structure. It would feel like programming to me, given the right tool support.

In the short term, I think we need to focus on specialized executable specification languages.

Form-based code and Airport boulevard

I have always felt that zoning regulations are an insidious evil that have caused our communities to be built in ways that are impractical and, well, unpleasant. It doesn't make sense to completely separate uses, so that we end up with huge strip malls and housing developments without shops or sidewalks. That's why I'm excited to learn about a viable alternative, called form-based code. The idea is to regulate the form, rather than the use, of properties. In other words, you can put any kind of use in a building, as long as it visually and functionally fits in with the neighborhood. While it wouldn't necessarily make sense to put a Starbuck's into a house in a suburban neighborhood (hmm... maybe that's not such a bad idea ;-), if you allow a little flexibility in visual design then different uses can be very compatible. Even more important, they can be synergistic. Form-based codes are being proposed for the north Airport boulevard neighborhood in Austin, and I support it. Spread the word!

The Irrationality of the American Home Buyer

Austin recently started conversion of its old Mueller airport into a housing development. There are some things right about it: mixed use, public space. But the actual houses are crazy. Look at this photo:










(Thanks Google!)

Note that it has huge stand-alone houses that almost completely cover the available space. The alleys are a nice touch, but they aren't very efficient in terms of space. The houses are so close together that you could almost reach out one window and touch the other building.

Consider this analogous random chunk of suburban London. The houses are touching! But that leaves more room for a back yard.















Maybe its just me, but I would rather have my house touch my neighbor's house and have some open space. Having the houses touch actually gives more room for windows that you want to look out. The American style might have more windows, but there is less view.

I am not sure exactly who to blame... It could be the American architects, planners, and builders. It is partly their fault. But in the end they build what American buyers want to buy. And those buyers are not very rational, I fear.

Is there anything we can do?

How to Teach, Write, and Deploy a Partial Evaluator to a Million People in 24 Hours

On Tuesday I gave a tutorial on partial evaluation, called "Build your own partial evaluator in 90 minutes" at the working conference on domain specific languages (DSL 2011) in Bordeaux. The lecture notes benefited greatly by help from Ralf Lämmel. I've given this talk a few times before, but have not suggested that the audience actually implement a partial evaluator during the tutorial, as I did this time. Several people complete the task during the talk, and many more completed it in the following few hours. Solutions were done in Haskell, Rascal, and Scheme, and maybe more languages.

Working in Scheme, Ludovic Courtès finished the code that evening. He then checked it in to the code base of the GNU Guile compiler. I suspect that it needs a little more engineering to make it completely robust, but there is nothing that can't be taken care of with a little care.

This is a very fun example of practical impact: lecture to deployment in less than 24 hours. Thanks to everyone who attended, and to Ludovic for his extra efforts!

Facebook Insecurity

I recently found out that Facebook is incredibly easy to hack. It allows you to access the reset password page without any email confirmation, and it also does not let you modify your security question. Facebook had better do something about this, especially given that Google+ is now going after them. Please go to this Facebook FAQ page and tell that that the answer is not helpful.

Facebook FAQs

We want to make sure that your account and the information in it stays safe, so once you set up a security question on your account there’s no way to update it. Sorry for the inconvenience.