Discussion:
The goal of functional programming?
(too old to reply)
s***@gmail.com
2014-04-23 18:02:27 UTC
Permalink
I asked this question on SO: http://stackoverflow.com/questions/23250027/the-goal-of-functional-programming

Can someone please answer it?
Paul Rubin
2014-04-24 03:13:11 UTC
Permalink
I asked this question on SO: http://stackwhatever..
Why is there suddenly all this usenet spam for stackwhatever? If you've
got a question for Stackwhatever, then ask it over there, don't
cross-post. If to ask on Usenet, ask it here.
s***@gmail.com
2014-04-24 06:45:44 UTC
Permalink
Post by Paul Rubin
I asked this question on SO: http://stackwhatever..
Why is there suddenly all this usenet spam for stackwhatever? If you've
got a question for Stackwhatever, then ask it over there, don't
cross-post. If to ask on Usenet, ask it here.
Since no one answers on SO, I refer to the question for asking it here. I will post the question again:

I has always been thinking this, but I need a confirmation: The purpose of functional programming is to make written program becomes a mathematical proof. Once a program is correctly written, it is "proved" and is expected to work the same in any environment. This is why in FP, immutable is needed, otherwise there's no guarantee for the correctness of the "proof", because state changes can introduce unexpected behaviors.

Am I correct on the basis of Functional Programming?
Paul Rubin
2014-04-24 08:23:23 UTC
Permalink
Post by s***@gmail.com
I has always been thinking this, but I need a confirmation: The
purpose of functional programming is to make written program becomes a
mathematical proof....
Am I correct on the basis of Functional Programming?
No. Functional programming is a somewhat vague term. The "programs as
proofs" formulation (Curry-Howard correspondence) is important
theoretically and is used in some systems like Coq, but functional
programming in general has wider meaning. For example Scheme is
considered a functional programming language.

You might look for John Hughes's article "Why Functional Programming
Matters" for further info.
Jussi Piitulainen
2014-04-24 08:58:16 UTC
Permalink
Post by s***@gmail.com
I has always been thinking this, but I need a confirmation: The
purpose of functional programming is to make written program becomes
a mathematical proof. Once a program is correctly written, it is
"proved" and is expected to work the same in any environment. This
is why in FP, immutable is needed, otherwise there's no guarantee
for the correctness of the "proof", because state changes can
introduce unexpected behaviors.
Am I correct on the basis of Functional Programming?
Yes and no. It's certainly possible to reason about changes of state,
and people like Dijkstra, Gries, Hoare have studied proof patterns for
such programs in the past. One wants to write even the imperative
program in such a way that the changes are not unexpected.

But immutability makes reasoning easier. Consider, absent knowledge
about f except assume that f does return:

{ String s = new String("whatever"); // immutable strings
f(s);
if (s.length() == 0) loop(); else halt(); }

{ String s = new String("whatever");
f(s);
if (s[0] == '?') loop(); else halt(); }

(let ((s (string-copy "whatever"))) ; mutable strings of constant length
(f s)
(if (zero? (string-length s)) (loop) (halt)))

(let ((s (string-copy "whatever")))
(f s)
(if (char=? (string-ref s 0) #\?) (loop) (halt)))

In neither language can f assign a different object to the local
variable s, so that helps some: we are reasoning about a known string
object that was the initial value of s.

In Java, f cannot change the String object at all, so that helps more:
both Java examples take the else branch.

In Scheme, f can change the first character to #\?, so it's not
possible to tell which branch the second Scheme example takes. The
first Scheme example takes the else branch, and a Scheme compiler
could in principle rely on that, assuming standard bindings.

In a functional language, just calling f for effect would make no
sense. There would be some local code to tell what the effect of f
might be, and that would then help in the reasoning. I think.
Stefan Ram
2014-04-26 00:11:35 UTC
Permalink
The goal of functional programming?
The goal of functional programming is the same as the goal
of other kinds of programming. Functional programming just
employs other means to achieve this goal.

Loading...