Referential Transparency is Overrated Didier Verna Referential Transparency is Overrated Introduction But let’s keep this between us. . . Scoping Syntax Extension Symbol Didier Verna Macros Macros didier@lrde.epita.fr Conclusion http://www.lrde.epita.fr/˜didier http://www.facebook.com/didierverna @didierverna ACCU 2015 – Thursday, April 23rd 1/54
Table of contents Referential Transparency is Overrated Introduction 1 Didier Verna Introduction Scoping 2 Scoping Syntax Extension Syntax Extension (RTMP) 3 Symbol Macros Symbol Macros / Generalized Variables Macros 4 Conclusion Macros (CTMP) 5 Conclusion 6 2/54
Natural Languages (Analytical Philosophy) Origins Referential Transparency Quine is Overrated Didier Verna reference ≈ meaning replacing an expression by another one which refers to Introduction Views the same thing doesn’t alter the meaning Confusion Benefits Scoping Example: “Wallace’s dog” ≡ “Gromit” Syntax Extension ✔ Tomorrow, I’ll go feed Wallace’s dog. Symbol Macros ✖ Gromit isn’t Wallace’s dog anymore. Macros Conclusion 4/54
Programming Languages (Semantics) Inspired from Quine Referential Transparency Strachey 1 is Overrated Didier Verna If we wish to find the value of an expression which contains a sub-expression, the only thing we need Introduction Views to know about the sub-expression is its value. Confusion Benefits Scoping Syntax Reade 2 Extension Symbol Only the meaning of immediate sub-expressions is Macros significant in determining the meaning of a Macros compound expression. Since expressions are Conclusion equal if and only if they have the same meaning, [it] means that substitutivity of equality holds. 1 Fundamental Concept of Programming Languages, 1967 2 Elements of Functional Programming, 1989 5/54
Purely Functional Languages A more extremist view Referential Transparency Take your pick is Overrated Didier Verna An expression which can be replaced with its value without changing the behavior of a program. Introduction Views Confusion Benefits Evaluation of the expression simply changes the Scoping form of the expression but never its value. Syntax Extension All references to a value are equivalent to the value Symbol Macros itself. Macros Conclusion There are no other effects in any procedure for obtaining the value. 6/54
The original points Quine and Strachey agreed Referential Transparency Quine’s point is Overrated Didier Verna Natural languages are complicated because they need to be practical. Introduction Views Confusion Benefits Strachey’s point Scoping The same! And BTW, he was talking about imperative Syntax Extension languages. Symbol Macros A sound denotational semantics would render even Macros imperative languages referentially transparent (by telling you Conclusion when two expressions are equal). 7/54
Purity vs. Referential Transparency Are we talking about the same thing ? Referential Transparency What purely functional programmers talk about is Overrated Didier Verna values instead of meaning Introduction evaluation process becomes relevant Views Confusion side effects (or lack thereof) Benefits Scoping Syntax Extension Symbol Macros Macros Conclusion 8/54
The Typical PFP’s argument Which is refutable Referential Transparency This is referentially transparent is Overrated Didier Verna int plus_one ( x ) { return x + 1; } / ∗ plus_one (1) i s always 2 ∗ / Introduction Views Confusion Benefits This is not Scoping int foo = 10; Syntax int plus_foo ( x ) { return x + foo ; } / ∗ plus_foo (1) depends on foo ∗ / Extension Symbol Macros Really? What about this? Macros Conclusion foo : : Int foo = 10 plus_foo : : Int − > Int plus_foo x = x + foo −− plus_foo 1 i s always 1 1 . . . l e t foo = 20 in l e t plus_foo x = x + foo in plus_foo 1 −− 21. Woops! 14/54
One final definition Gotta stop somewhere Referential Transparency Gelernter & Jagannathan is Overrated Didier Verna A language is referentially transparent if (a) every subexpression can be replaced by any other Introduction Views that’s equal to it in value and (b) all occurrences of Confusion Benefits an expression within a given context yield the Scoping same value. Syntax Extension Symbol Macros Applies to languages , not expressions Macros Mostly rules mutation out Conclusion 15/54
Intermediate Conclusion Where to go from here Referential Transparency There is always some form of context dependency is Overrated (lexical / dynamic definitions, free / bound variables, Didier Verna side effects etc ) Introduction Views PFPs disregard their own contexts (lexical and purity) Confusion Benefits PFPs reduce the notion of “meaning” to that of “value” Scoping (result of a λ -calculus evaluation process) Syntax Extension Symbol Macros Consequently, I hereby claim that the expression “referential Macros transparency” is not referentially transparent :-). Conclusion 16/54
Optimization, Safety, Expressiveness. . . Why referential transparency is profitable Referential Transparency Optimization: is Overrated ◮ Memoization Didier Verna ◮ Parallelism (Cf. Erlang) Introduction Safety: Views Confusion ◮ Localized semantics (hence localized bugs) Benefits ◮ Program reasoning and proof Scoping Syntax Expressiveness: Extension ◮ Lazy evaluation Symbol Macros Macros Conclusion 17/54
Safety with Program Proof Formal reasoning Referential Transparency is Overrated Demonstrate (please) that ∀ n , ssq ( n ) > 0 Didier Verna Introduction Views Confusion Imperative Purely functional Benefits Scoping ssq : : Int − > Int int ssq ( int n ) Syntax ssq 1 = 1 { Extension ssq n = n ∗ n + ssq (n − 1) int i = 1 , a = 0; Symbol while ( i <= n ) Macros { True for N = 1 a += i ∗ i ; Macros i += 1; } Conclusion Assuming it holds for return a ; N − 1 . . . } Ahem. . . 18/54
Expressiveness with Lazy Evaluation Thank you Church-Rosser Referential Transparency is Overrated Explicit representation of infinite data structures Didier Verna Introduction Views Confusion Haskell Lisp Benefits Scoping i n t l i s t : : Int − > [ Int ] ( defun i n t l i s t ( s ) i n t l i s t s = s : i n t l i s t ( s + 1) Syntax ( cons s ( i n t l i s t (1+ s ) ) ) ) Extension −− ( i n t l i s t 0) ! ! 3 − > 3. ; ; ( e l t ( i n t l i s t 0) 3) − > ^C^C Symbol Macros Macros Conclusion 20/54
Where to Look for Bindings? Scoping Referential Transparency Lexical Scoping: in the defining context is Overrated Didier Verna Dynamic Scoping: in the calling context Introduction Scoping Definitions Lexical Scope Dynamic Scope Lexical Scope Dynamic Scope Interlude ( l e t ( ( x 10)) ( l e t ( ( x 10)) ( defun foo ( ) ( defun foo ( ) Syntax ( declare ( special x ) ) Extension x ) ) x ) ) Symbol ( l e t ( ( x 20)) ( l e t ( ( x 20)) Macros ( foo ) ) ; ; − > 10 ( foo ) ) ; ; − > 20 Macros Conclusion First Lisp was dynamically scoped Lexical scope since Scheme (except Emacs Lisp!) Common Lisp still offers both (Emacs Lisp now does) 22/54
Lexical Closures Brought to you by lexical scope Referential Transparency Definition: is Overrated Combination of function definitions and their defining Didier Verna environment (free variables values at define-time) Introduction Benefits: Scoping Definitions ◮ 1st order functional (anonymous) arguments Lexical Scope ◮ 1st order functional (anonymous) return values Dynamic Scope Interlude ◮ . . . ( e.g. encapsulation) Syntax Extension Lisp note: lexical state is mutable Symbol Macros Macros Conclusion 23/54
Why lexical closures are crucial They’re everywhere! Referential Transparency 1st order functional (anonymous) arguments is Overrated Didier Verna ( defun l i s t + ( l s t n ) ( mapcar ( lambda ( x ) (+ x n ) ) Introduction l s t ) ) Scoping Definitions Lexical Scope 1st order functional (anonymous) return values Dynamic Scope Interlude ( defun make − adder ( n ) Syntax ( lambda ( x ) (+ x n ) ) ) Extension Symbol Macros Mutable lexical state Macros Conclusion ( l e t ( ( cnt 0)) ( defun newtag ( ) ( incf cnt ) ) ( defun resettag ( ) ( setq cnt 0 ) ) ) 24/54
Why is dynamic scoping dangerous ? But also useful Referential Transparency Problems: is Overrated ◮ Name clashes on free variables Didier Verna ◮ Very difficult to debug Introduction ◮ Mc Carthy’s first example of higher order function Scoping (1958) was wrong! Definitions Lexical Scope Advantages: Dynamic Scope Interlude ◮ Dynamic paradigms ( e.g. COP) Syntax ◮ Global variables! Extension As per defvar and defparameter Symbol Macros E.g. Emacs user options Macros Conclusion 29/54
Mc Carthy’s bugged example Transcribed in Common Lisp Referential Transparency The first mapping function is Overrated Didier Verna ( defmacro while ( t e s t & rest body ) ‘ ( do ( ) ( ( not , t e s t ) ) Introduction ,@body) ) Scoping ( defun my − mapcar ( func l s t ) Definitions ( l e t ( e l t n ) Lexical Scope ( while ( setq e l t ( pop l s t ) ) Dynamic Scope ( push ( funcall func e l t ) n ) ) Interlude ( nreverse n ) ) ) Syntax ( defun l i s t + ( l s t n ) Extension (my − mapcar ( lambda ( x ) Symbol ( declare ( special n ) ) Macros (+ x n ) ) ; ; Barf ! ! l s t ) ) Macros Conclusion 30/54
Recommend
More recommend