FpinS : Referential transparency in Parallel code Chapter 7

82 views
Skip to first unread message

phata...@gmail.com

unread,
Oct 15, 2014, 1:27:27 AM10/15/14
to scala-fu...@googlegroups.com
In Chapter 7, author says the map2 should take arguments lazily ( before introducing fork) to get parallelism and referential transparency . 
  
  The map 2 signature will be 
      def map2[A, B, C](a: => Par[A], b: => Par[B])(fn: (A, B) => C): Par[C] 
  and sum function will have
     Par.map2(sum(l), sum(r))(_ + _) . 

This version runs in parallel. But if I rewrite the same sum function as below
   
      val leftSum =  sum(l)
      val rightSum = sum(r)
      map2(leftSum,rightSum)(_+_)

which is no longer parallel. Is it the case of breaking referential transparency? 
  

Arya Irani

unread,
Oct 15, 2014, 7:46:51 AM10/15/14
to scala-fu...@googlegroups.com
<phata...@gmail.com> wrote:
Is it the case of breaking referential transparency?

No, the program is still RT and still produces the same output.  It's just no longer lazy, and no longer parallel.

runarorama

unread,
Nov 25, 2014, 7:59:13 AM11/25/14
to scala-fu...@googlegroups.com
Yes, this is sort of hinting at the more sophisticated notion of RT in chapter 14. There are certain cases (such as this one) where the evaluation strategy factors into the correct execution of our program. So starting evaluation immediately (instead of in a later phase, like we do later in the chapter) can be considered a side effect. Note that the evaluated result doesn't change, so our more simple definition of RT from chapter 1 isn't violated. But your intuition is correct in that something like RT is being violated in that the rewritten program is not equivalent in some sense to the original, even though it evaluates to the same result.

What we have to do is refine our definition of RT to consider some notion of equivalence. A good standard answer is "moral equivalence", which is discussed in a paper called "Fast and Loose Reasoning is Morally Correct". http://www.cs.ox.ac.uk/jeremy.gibbons/publications/fast+loose.pdf

Rúnar
Reply all
Reply to author
Forward
0 new messages