Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> comp.databases.theory -> Re: Declarative constraints in practical terms

Re: Declarative constraints in practical terms

From: <ralphbecket_at_gmail.com>
Date: 27 Feb 2006 20:23:59 -0800
Message-ID: <1141100639.238080.258460@j33g2000cwa.googlegroups.com>


dawn wrote:
> Mikito Harakiri wrote:
> > ralphbecket_at_gmail.com wrote:
> > > Rougly speaking, a declarative logic language should
> > > - not have side effects;
> > > - be order independent (i.e., respect the commutativity
> > > of conjunction and disjunction and be sound w.r.t.
> > > negation, if supported); and
>
> I'm not sure I fully understand this -- can you provide an example of
> how an imperative language would not meet this requirement?

Sure (examples are in C):
- destructive assignment is a side effect, as in

main(int argc, char **argv)
{
  int x = 0; printf("%d", x);
  x = x + 1; printf("%d", x);
}

will print "01", so the assignment statement definitely has a side effect.

int x;

int p(void) { return x == 0; }

int q(void) { x = 1; return 1; }

main(int argc, char **argv) {
  x = 0; if(p() && q()) printf("a");
  x = 0; if(q() && p()) printf("b");
}

This program will print "a", but not "b", so the order of conjunction matters, therefore the commutativity of conjunction is not preserved.

>
> > > - functions (if any) should be referentially transparent.
>
> So you cannot write functions that take keyboard input, right? How
> would a declarative language take user input?

Ah, I meant `function' in the mathematical sense of a many-to-one relation (with no side effects).

It is quite possible to handle IO (and things that are essentially imperative in a pure language) without destroying the declarative semantics of the language. Haskell uses monads while Mercury uses uniqueness. How those techniques work would be a post in itself, but the gist of both ideas is that functions that "do" IO are represented as functions from states-of-the-world to states-of-the-world (i.e., the state-of-the-world appears as an explicit argument).

> > > Have a look at Mercury for an example of a pragmatic,
> > > general purpose, high performance declarative
> > > programming language.
> >
> > In a truly declarative language predicates shouldn't have the "mode"
> > declarations

Mikito is wrong here: the modes in Mercury are orthogonal to the declarative semantics. Modes constrain the operational semantics of the compiled program, but the denotational semantics remain unaffected - you will never get a result from a Mercury program that is not also a model of its declarative reading.

> Now I'm trying to sort out the difference between declarative and
> imperative as well as the reason I would want to use one over the
> other.

Declarative = "what".
Imperative = "how".

A spreadsheet is a declarative language: you tell it what to compute, not how to compute.

C/Java/etc. are imperative languages: you tell the computer how it should compute, but not what it is computing.

At the risk of making a weak argument, a declarative program is a mathematical statement, and mathematics is the most successful means of modelling the world yet invented! On the other hand, there is no useful mathematical intepretation of an imperative program.

> All declarative languages have these features that are not found in
> imperative languages:
>
> Would the list completing this sentence be Ralph's list above?

In a nutshell, if your language is just a special syntax for (some) mathematics, then it's declarative. In mathematics there are no side effects and the order in which you evaluate things doesn't matter. (Order of evaluation is something compilers should worry about; programmers should worry about writing programs that work and meet the spec.)

Received on Mon Feb 27 2006 - 22:23:59 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US