You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Todd Jonker <tv...@pobox.com> on 2003/02/12 05:51:20 UTC

[Jelly] Functional Jelly Redux, was Re: Jelly and X++

On 2/11/03 7:15 PM, mdiggory@latte.harvard.edu wrote:

> Can you briefly clarify "Functional Model" (even with just a link to
> somewhere else) since it can be easily confused with other definitions
> of functional programming?
> 
> -Mark

Good question, Mark.  I'll do my best without digging too deep into the
COMP411 archive...

Technically, a functional programming model is one in which everything is
expressed in terms of pure functions in the mathematical sense.... The old
algebra and calculus kind of function: f(x) = y.  That is, you have
functions, you apply them to parameters, you get results, and you compose
results in various ways.

The functional paradigm, where you compute results by composing functional
expressions, is usually contrasted with the so-called "imperative" paradigm,
where you compute results by sequencing statements and performing
side-effects (generally speaking, assignments to variables).
 
A "Pure Functional" language can be somewhat usefully defined as those
languages that do not have "imperative" features.  From this you can observe
that a pure functional language doesn't have variables (though it will often
have constants).  Another distinguishing characteristic is that the
functional model says that Functions Are Values.   This feature is commonly
called "first-class functions", and lets you define functions that accept
functions as arguments, and return functions as values, and construct
functions on the fly.  (Java's anonymous inner classes is attempt at
approximating the value of first-class functions, but it's terribly clumsy,
verbose, and limited when compared to the real thing.)

Many people are surprised by the fact that you can do any real work that
way, but as it turns out, you can perform _any_ computation under such a
model, you just express things differently.  (That is, the functional model
is Turing-complete.)  If this blows your mind, google for "Lambda Calculus"
and be prepared for some really mind-warping stuff.


In the real world, however, imperative languages reign, and there are
relatively few purely-functional languages, and not a one that's commonly
used outside of academia.  They _are_ used quite a bit in academia, since
they allow for very complete and rigorous mathematical models and analyses,
useful for things like proving correctness of programs.  The Lambda Calculus
is in fact very widely used means of defining the semantics of programming
languages, whether they by functional or imperative or "other".

There are, however, quite a few "mostly functional" languages that provide
imperative features, but where the bulk of the  programming is typically
done in a functional way.  Lisp, Scheme, and ML all fall into this
category... In those languges the use of an assignment is a pretty rare
sighting.

[ Most OO languages are pretty aggressively imperative.  It's damn near
impossible to write a useful Java program without using an assignment. ]

By "functional programming", language-weenies like me usually mean
"programming within the functional paradigm", and sometimes mean
"programming with a functional language".  It's sometimes possible, but
usually frustrating, to do functional programming in an imperative
langugage.

For my money, the "big win" in the functional model is actually very simple:
eradicate the distinction between statements and expressions.  If you've
ever used Lisp or Scheme (or, more recently, Dylan) you understand what this
means and how powerful it is.  Basically, every programming language
construct has "a value".

The IF statement is a particularly nice example.  If you write in Java,

   if (test) { doSomething(); } else { doSomethingElse(); }

there is no notion of the IF as a whole having "a value".  You can't use the
whole thing on the right-hand-side of an assignment, for instance.

However, the parallel construct in Scheme,

    (if test (doSomething) (doSomethingElse))

does in fact have a well-defined value: if the value of test is true, the
value of the if-expression is the value of it's then-clause; otherwise it
evaluates to it's else-clause.  So in the example above, the value would be
the result of calling one of the functions doSomething or doSomethingElse.

Before somebody notes that C/C++/Java do in fact have this notion in the
form of the conditional expression

    (test ? doSomething() : doSomethingElse())

the point is that it's extremely useful to design your language so that
*every* construct has a value... So there's no difference between statements
and expressions (ie, imperative constructs and functional constructs).

So what does this have to do with Jelly?  I first observe that the whole
structure of XML leads itself perfectly towards a functional paradigm, which
is very big on nesting, syntactic consistency, and substitutability.  But
more convincing (to me at least) is the fact that a functional model would
enable a great deal more flexibility in our Tag Libraries, at much lower
effort.  I guess what I'm really shooting for is to extend Jelly's
script/tag model so that it is easy to define tag libraries that work in a
purely-functional manner, an imperative manner, or anything in between.  As
some who's worked with quite a few languages in my time, I recognize the
value of using the right model for the job.  I'd like to see Jelly enable
that model so that it can be used all over the place!

Rather than repeat my earlier proposals, I'd encourage the interested reader
to search this forum's archive for the thread "Towards Functional
programming in Jelly scripts" that started 12/12/02.  I would of course be
happy to renew those discussions again.


.T.

-- 
                 War is NOT a necessity!

  http://UnitedForPeace.org/    http://MoveOn.org/
  http://NotInOurName.net/      http://CWG.org/


Re: (OT): Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Todd Jonker <tv...@pobox.com>.
On 2/12/03 11:52 AM, eprice@ptc.com wrote:

> Hey Todd, thanks for writing this up.  I've heard of FP before and
> others have explained it but this one really cinched it for me.  (Or
> maybe I was just ready to understand it this time.)

I'm glad to have been useful!  :-)


> I don't know if it qualifies as a "functional programming language", but
> I've just recently learned that Python treats functions as first class
> objects which can be declared anonymously and passed around as any other
> value.  It is also a respectable OO scripting language in its own right.
> 
> I think it lets you take a FP paradigm approach but does not require it
> of you.

Yeah, Python is one of those mixed-paradigm languages.  I'm sure Python
lovers would be able to write in detail about how first-class functions are
useful.  Thanks for bringing up a great example.


.T.

-- 
                 War is NOT a necessity!

  http://UnitedForPeace.org/    http://MoveOn.org/
  http://NotInOurName.net/      http://CWG.org/


(OT): Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Erik Price <ep...@ptc.com>.

Todd Jonker wrote:
> On 2/11/03 7:15 PM, mdiggory@latte.harvard.edu wrote:
> 
> 
>>Can you briefly clarify "Functional Model" (even with just a link to
>>somewhere else) since it can be easily confused with other definitions
>>of functional programming?
>>
>>-Mark
> 
> 
> Good question, Mark.  I'll do my best without digging too deep into the
> COMP411 archive...

Hey Todd, thanks for writing this up.  I've heard of FP before and 
others have explained it but this one really cinched it for me.  (Or 
maybe I was just ready to understand it this time.)

> In the real world, however, imperative languages reign, and there are
> relatively few purely-functional languages, and not a one that's commonly
> used outside of academia.  They _are_ used quite a bit in academia, since
> they allow for very complete and rigorous mathematical models and analyses,
> useful for things like proving correctness of programs.  The Lambda Calculus
> is in fact very widely used means of defining the semantics of programming
> languages, whether they by functional or imperative or "other".
> 
> There are, however, quite a few "mostly functional" languages that provide
> imperative features, but where the bulk of the  programming is typically
> done in a functional way.  Lisp, Scheme, and ML all fall into this
> category... In those languges the use of an assignment is a pretty rare
> sighting.
> 
> [ Most OO languages are pretty aggressively imperative.  It's damn near
> impossible to write a useful Java program without using an assignment. ]

I don't know if it qualifies as a "functional programming language", but 
I've just recently learned that Python treats functions as first class 
objects which can be declared anonymously and passed around as any other 
value.  It is also a respectable OO scripting language in its own right.

I think it lets you take a FP paradigm approach but does not require it 
of you.



Erik


Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Todd Jonker <tv...@pobox.com>.
On 2/12/03 11:23 AM, paul@activemath.org wrote:

> All my Lisp friends couldn't deny it: an Iterator is not doable in a
> functional language. They don't seem to be missing it but I would
> terribly.

You can't do iterators, but you can do other things that work just as well.


> Do you really intend to make variables immutable in Jelly ?

NO!  I never suggested any such thing.  I only got into the whole theory and
background of functional languages because somebody asked me to explain it
more thoroughly.

All I am really advocating at this point is that tags and scripts, when
evaluated, return a value.  I just want to make it easier to write
functional-style tag libraries, if that's what the author wants.  See the
earlier thread for details.

I have absolutely no desire to change Jelly into a purely functional system.
That would pretty much negate it's entire purpose.


.T.

-- 
                 War is NOT a necessity!

  http://UnitedForPeace.org/    http://MoveOn.org/
  http://NotInOurName.net/      http://CWG.org/


Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Paul Libbrecht <pa...@activemath.org>.
All my Lisp friends couldn't deny it: an Iterator is not doable in a 
functional language. They don't seem to be missing it but I would 
terribly.

Do you really intend to make variables immutable in Jelly ?

Paul


On Mercredi, févr 12, 2003, at 16:44 Europe/Paris, Todd Jonker wrote:

> If you've not used a language with true closures and first-class 
> functions,
> you're missing out on a really cool experience.


Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Todd Jonker <tv...@pobox.com>.
Paul,

The functional model means far more than "you call functions a lot".  It
means, in particular, that values do not change state.  The entire OO model
is based on imperative notion that an object "has state" and that that state
tends to change over time.  Anything with a setter-method is imperative and
not functional.  By definition, functional objects cannot change state after
they are constructed.  In Java terms, the only functional classes would be
those in which every member variable is declared "final" so it can't change
after construction.

In a (pure) functional language you have the guarantee that for any function
called twice with the same parameters, the result will be the same.  In
other words, f(x) == y at all times.  This is why Java doesn't call its
methods "functions": the latter term implies something much more specific.

Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Paul Libbrecht <pa...@activemath.org>.
On Mercredi, févr 12, 2003, at 05:51 Europe/Paris, Todd Jonker wrote:
> [ Most OO languages are pretty aggressively imperative.  It's damn near
> impossible to write a useful Java program without using an assignment. 
> ]
>

On this I don't agree. I do think you can write lots of useful GUI 
things that almost purely functional (e.g. a Swing model class based on 
some predefined storage). It's just rarely seen.

Moreover there are thing that really suck in functional programming: 
how do you want to make, without a state, something like an iterator 
that walks a tree ? And iterators are really close to functional, are 
immensely useful!

As to the cumbersomeness of the inner-class syntax, I think this really 
only true if you respect the coding conventions which, for most of what 
I have seen, have never talked about these hence require five lines for 
a one line inner-class (e.g. a parameter to 
SwingUtilities.invokeLater() parameter).

Paul


Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Todd Jonker <tv...@pobox.com>.

On 2/12/03 3:58 AM, paul@activemath.org wrote:

> From what I remember of this thread, you had suggested tags to have a
> result.
> But tags DO have a result: namely, an XML output.
> So for the processing of XML objects, jelly IS functional.
> Trouble of course start if you want your tags to return anything else
> than XML. And this is the place where I suggested an extension for
> being functional is to be inserted.

Agreed: the XMLOutput is an output stream, not a functional result.  It's
only really useful if I want to return text, not if I want to return an
Action to whatever happens to be enclosing the <action> tag. (Which is in
fact exactly what I want to do.)


> There's one seducing feature seen in some functional languages that I
> feel you forgot: lazy evaluation: e.g. that an expression is to be
> evaluated only when it's needed. This is missing just about everywhere
> but I have the impression that making something such in Jelly would be
> do-able.

Good point!  Jelly has this already, at least at the tag level. Just look at
the Swing <action> tag. It is, indeed, wonderful (though I worry about the
memory overhead).  One could argue that attributes might also benefit from
lazy evaluation, but I don't currently feel the need to push for that.  :-)
Maybe you can get that effect through clever use of the Expression
interface, but I don't know.

.T.

-- 
                 War is NOT a necessity!

  http://UnitedForPeace.org/    http://MoveOn.org/
  http://NotInOurName.net/      http://CWG.org/


Re: [Jelly] Functional Jelly Redux, was Re: Jelly and X++

Posted by Paul Libbrecht <pa...@activemath.org>.
 From what I remember of this thread, you had suggested tags to have a 
result.
But tags DO have a result: namely, an XML output.
So for the processing of XML objects, jelly IS functional.
Trouble of course start if you want your tags to return anything else 
than XML. And this is the place where I suggested an extension for 
being functional is to be inserted.

There's one seducing feature seen in some functional languages that I 
feel you forgot: lazy evaluation: e.g. that an expression is to be 
evaluated only when it's needed. This is missing just about everywhere 
but I have the impression that making something such in Jelly would be 
do-able.

Paul


On Mercredi, févr 12, 2003, at 05:51 Europe/Paris, Todd Jonker wrote:
> Rather than repeat my earlier proposals, I'd encourage the interested 
> reader
> to search this forum's archive for the thread "Towards Functional
> programming in Jelly scripts" that started 12/12/02.  I would of 
> course be
> happy to renew those discussions again.