From: Pietro Campesato
Subject: Is (let ((pid (fork))) ...) "functional"?
Date: 
Message-ID: <1140058949.670960.118000@g44g2000cwa.googlegroups.com>
It might seem like a stupid question to some, I guess...

At first I dismissed it as not, but later I wondered about
the real meaning of referential transparency in the presence
of an hypothetical "fork" primitive and its unique behaviour.

It almost sounds like a philosophical question.
I would be interested to hear any opinions on that.

Thanks in advance.
From: Pascal Bourguignon
Subject: Re: Is (let ((pid (fork))) ...) "functional"?
Date: 
Message-ID: <87lkwbn7uc.fsf@thalassa.informatimago.com>
"Pietro Campesato" <················@gmail.com> writes:

> It might seem like a stupid question to some, I guess...
>
> At first I dismissed it as not, but later I wondered about
> the real meaning of referential transparency in the presence
> of an hypothetical "fork" primitive and its unique behaviour.
>
> It almost sounds like a philosophical question.
> I would be interested to hear any opinions on that.


Note that fork returns only once per process.

However, fork doesn't return the same result in the parent process
when it calls it twice.  Therefore it's not a function.

We could use instead:

;; Untested code follows:
(let ((hash (make-hash-table)))
  (defun ffork (n)
    (or (gethash n hash) 
         (setf (gethash n hash) (fork)))))


Then you'd call: (ffork 0) (ffork 10) (ffork 1) (ffork 2) (ffork 0),
etc, and when you give an argument already given, it just returns the
pid of the corresponding child, but when you give an argument never
seen before, it forks and returns the the pid of the child or 0.

(list (ffork n) (ffork n)) --> (,some-pid ,some-pid) in the parent
                           --> (0 0) in the child

So now, we have a functional function.  When you consider each
process, you just have a normal function.  Nothing special happens.


Perhaps every time you compute (sin (/ pi 2)) the universe is
duplicated and half a galaxy is disintegrated in one of the copies.
Perhaps it's only when you make an "observation" that the universe is
duplicated or n-plicated. You don't have any way to know it, have you?


Now if you introduced a communication channel between the processes,
that'd be something else...  But quite similar to the problem of I/O
in pure functional; it could be encapsulated in an I/O monad, I guess.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay