Re: Re (2): Turing completeness and syntactic elegance;

On Mon, 3 Dec 2012 13:52:02 -0700, you wrote:

>> ... complex relationships
>> that are expressed declaratively rather than imperatively, ...
>
>An example or two would be interesting.

1) Perhaps the most commonly encountered example is SQL, especially the
WHERE clause. A WHERE clause declaratively describes the relationship
between sets of columns.

2) Some of the layout managers used in Java are effectively declarative,
although they are embedded in an imperative framework, and so it gets a
bit fuzzy. But this example from a tutorial on the Group layout manager
could easily be represented as XML:

 layout.setHorizontalGroup(
   layout.createSequentialGroup()
     .addComponent(c1)
     .addComponent(c2)
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
       .addComponent(c3)
       .addComponent(c4)));
 layout.setVerticalGroup(
   layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
       .addComponent(c1)
       .addComponent(c2)
       .addComponent(c3))
     .addComponent(c4));

The XML equivalent might be something like this:

 <layout>
   <horizontal-group>
     <group style="sequential">
       <component id="c1"/>
       <component id="c2"/>
       <group style="parallel" alignnment="LEADING">
         <component id="c3"/>
         <component id="c4"/>
       </group>
     </group>
   </horizontal-group>
   <vertical-group>
     <group style="sequential">
       <group style="parallel" alignnment="BASELINE">
         <component id="c1"/>
         <component id="c2"/>
         <component id="c3"/>
       </group>
       <component id="c4"/>
     </group>
   </vertical-group>
 </layout>

3) Functional languages are mostly declarative. This example program (in
Haskell) defines a function that accepts an argument n and returns the
nth twin prime pair:

 sieve []     = []
 sieve (x:xs) = x : (sieve(filter(\ y -> (rem y x) /= 0) xs ))

 pairs []         = []
 pairs (x1:[])    = []
 pairs (x1:x2:xs) = (x1, x2) : pairs(x2:xs)

 twins (xs) = filter(\ x -> ((snd x) - (fst x)) == 2) xs

 nthTwinPrimes n = head(drop (n - 1) (twins(pairs(sieve [2..]))))

While I suppose it looks like gibberish if you're not familiar with the
language, trust me that it's declarative. Each line of the file is an
equation that represents an equivalence (the expression on the right is
equivalent to the expression on the left). At run time, if I were to
type in something like this:

 nthTwinPrimes 333

the interpreter would examine the equivalences and perform substitutions
as required to resolve the expression, eventually printing

 (19421,19423)

thus letting me know that those are the two members of the 333rd twin
prime pair.

-Steve Schafer

Received on Tuesday, 4 December 2012 02:43:33 UTC