2
$\begingroup$

Wikipedia has several languages listed as declarative languages. One of these languages listed is XSLT, which is a language for applying transformations to XML.

https://en.wikipedia.org/wiki/XSLT

As I understand it though, imperative languages say HOW to do something, while declarative languages only say WHAT to do and leave it up to the eg. compiler to figure out the imperative details.

In this light, since when writing XSLT we still say "Do transform A, then do transform B", it seems like we are still writing imperative code.

Furthermore, taking a language like C++ which is arguably imperative, it actually seems semi declarative because the optimizer can re-order instructions or, change algorithms.

In fact, a switch statement seems to be a declarative associative structure, with containing values known at compile time, letting the optimizer make the imperative details based on the type of data it stores. It can do binary searching, jump tables, if/else if chains and more. http://www.codeproject.com/Articles/100473/Something-You-May-Not-Know-About-the-Switch-Statem

This leads to my question...

Is it possible to have a purely declarative language? If so, what would it look like, and could an interpreter/compiler actually be made to convert anything into imperative machine code, beyond a no-op?

It also seems impossible to have a purely imperative language in modern days, as the CPU can do instruction re-ordering, speculative branch predictions, and similar, making your instructions more of hints as to what to do, as opposed to exact instructions of what to do.

Or am I completely off base here?

$\endgroup$
  • 2
    $\begingroup$ Can you define what you mean by a "purely declarative language"? What would that amount to, in your view? If I had a language that I thought was purely declarative, what criteria would you want to use to determine whether it qualifies? $\endgroup$ – D.W. Nov 8 '16 at 23:29
  • $\begingroup$ Good question, and maybe that's where my confusion comes from. Wikipedia also says that declarative languages describe what must happen in the problem domain but not how to do it. Maybe what makes a language declarative is if you can describe only the side effects you care about, and the rest that is unspecified is fair game for the compiler/interpreter to decide about? $\endgroup$ – Alan Wolfe Nov 8 '16 at 23:35
  • $\begingroup$ I disagree with the "transform A then transform B" being already imperative one, it is just telling what is the goal with given interface. $\endgroup$ – Evil Nov 9 '16 at 0:31
  • 1
    $\begingroup$ I agree with you in light of your answer, DW's leading question, and my further education. Thanks for that! $\endgroup$ – Alan Wolfe Nov 9 '16 at 1:00
  • $\begingroup$ Are you only interested in Turing-complete languages? In other realms there are plenty of declarative languages, e.g. HTML. $\endgroup$ – Raphael Nov 9 '16 at 1:33
2
$\begingroup$

Declarative programming defined the goal, as in your example transform A to B, where steps made are not present in this layer of abstraction, and simply it is not enforced.

Imperative programming defines steps to achieve the goal. With optimizations it is a bit of mixture of both - steps are defined but this is translated to goal and then changed to make it faster.

To achieve exactly imperative in this pure form all optimisations need to be suppressed, branching does not change the order. Interpreter executing one instruction at the time does that already.

As far as I know we call languages like C, Fortran, Pascal, assembler imperative languages and do not go into details of execution. One example is read from disk 1bit, 1 byte, several bytes and so on - disk reads the whole batch at once so it did not exactly what we asked for, the same with access to memory.

The side effects are not declarative, but imperative. Purely functional language is declarative already. The problem with purely functional language is e.g. I/O, which is mittigated by monads, to set states, but it does not matter to declarative one.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.