4. goals of the IAL
mathematical notation
printable in publications
“should be able to be translated to
programs”
5. great strides
agreed upon the BNF
renamed from IAL to Algol
spanned 50 centres and 74 persons
formalized pass by value/reference
agreed to meet in 1960
7. clashes
to use “,” or a “.” in numbers ?
US industrial programmers had real-world
inputs and recommendations
pass by reference was confusing
“Add recursion?” Lisp had just added
recursion few months before release
8. Dijkstra saw the potential of recursion.
With editor Naur, added it secretly to
the draft over phone from Amsterdam.
Hence called the “Amsterdam Plot”
Recursion was a major step. It was
introduced in a sneaky way.
9. 1986
Erlang was designed for writing
concurrent programs that
“run forever.”
Joe Armstrong at Ericsson Labs.
#telephony
star #1 on erlang team
11. why concurrency was #1
Idea came from existing telephone exchanges
Lets say there are two separate phone calls
One call going down should not affect the other
They should not share anything, no sharing pointers
So copy data over. beginnings of message passing.
Be able to work on different registers
simultaneously.
12. thought process
with concurrency as the first goal,
everything thing else was worked out
or a consequences around decisions
around concurrency
13. concurrency existed before
erlang
• Ada (with tasks)
• EriPascal (an Ericsson dialect of Pascal
with concurrent processes)
• Chill (with processes)
• PLEX (processes implemented in
hardware. Also inspired linking errors)
• and Euclid
14. #emp2 to #emp1
“3 properties of a programming language
were central to the efficient operation of a
concurrent language or operating system.“
1) the time to create a process.
2) the time to perform a context switch
between two different processes
3) the time to copy a message between two
processes
15. 3 emps
“We rapidly adopted a philosophy of
message passing by copying and no
sharing of data resources. Again, this was
counter to the mood of the time, where
threads and shared resources protected
by semaphores were the dominant way
of programming concurrent systems.”
16. funny incident
• the 3 employees working on erlang
went to a conference just like this
• asked speakers what would happen if
one of the many machines crashes
• reply was “no failures” or “can’t do
anything” . This was still 1985.
17. • Joe armstrong first wrote this in the
algebra (hurray for algol 60 )
• a prolog engineer saw the algebra
and said, i can program this
• Joe was amazed. Learned prolog.
18. • prior to prolog, the implementation in smalltalk
• dial a number, send that as a message to
exchange.
• dial a valid number, other phone should ring
• if its busy, get back busy
• if its not busy, get back not busy
19. state machines
“the software for call control was best
modeled using finite state machines
that undergo state transitions in
response to protocol messages.”
20. What started as an experiment in
adding concurrency to Prolog,
became more of a language in its own
right and this language acquired a
name Erlang in 1987.
21. with concurrency as goal
“we didn’t realise at the time, was that
copying data between processes
increases process isolation, increases
concurrency and simplifies the
construction of distributed systems.”
27. • usually a function gets executed and returns to
where it was called
return 1 + foo(a)
• but in a tail optimised function, it effectively
does not have to remember where to go back
to
return tail(1,a)
• #gajini
28. int r(int x) {
if (x == 1) {
return 1;
}
return x*r(x-1);
}
r(3)
!
!
recursive
40. 3 * r(2)
3 * (2 * r(1))
3 * (2 * 1)
3 * 2
6
tr(3-1, 3 * 1)
tr(2-1, 2 * 3)
tr(1-1, 1 * 6)
6
are these both recursive?
which of these are easier to spawn off ?
which of these are easier to stop midway?
which of these are easier to debug?
41. 3 * r(2)
3 * (2 * r(1))
3 * (2 * 1)
3 * 2
6
tr(3-1, 3 * 1)
tr(2-1, 2 * 3)
tr(1-1, 1 * 6)
6
are these both recursive?
which of these are easier to spawn off ?
which of these are easier to stop midway?
which of these are easier to debug?
recursive
good backtrace
cant change order
recursive
easier to parallelise
42. Amdahl’s Law
“The speedup of a program using
multiple processors in parallel computing
is limited by the time needed
for the sequential fraction of the program.”
43. which brings us to
spawning/forking
PART III
let’s look at some code
44. what is a daemon
• fair to say a daemon is a long running
process?
• fair to say a long running process is a tail-
recursive function?
46. a long running process is
in fact a tail-recursive
function
a daemon in erlang
47. Does this block the scheduler? is this an infinite loop?
!
Let’s start foo and bar on new processes and see what
happens?!
48. runs a function as a new process. here
we ask it to spawn both foo, then bar.
spawn
49. erlang’s scheduler splits CPU time
between multiple concurrent processes
based on the “reductions” ( # times a
function is called)
use 8 cores,
so 8 schedulers
51. as long as the heart is beating, you are alive
same way: as long as a process is
a) tail-recursive or
b) waiting to receive ( on its inbox )
…it is alive
57. Leslie Lamport , “father of distributed
computing” recent Turing Awardee
said that if you are not representing
your distributed program as a state
machine you’re doing something
wrong.
PART III conclusion
58. tail resursive functions are the foundation for
processes
processes are the foundation for state
machines
tail recursive functions / processes with sockets
are the foundation for network programming
networking programming is the foundation of
distributed computing.
PART III conclusion
59. erlang & distributed systems
so in other words, erlang’s strength in
distributed computing can be
indirectly be attributed to supporting
tail recursion
61. parallelism and ordering, another great debate
and tale of priorities and compromises
found in any producer/consumer scenario
1,2,3,4,5,6
1,2
3
4
5
6
5,4,1,2,3,6
eg: ekaf_partition_strategy
}
63. socket already closed, re-open it
socket already open
data being received currently
data being sent currently
socket closed before sending
socket closed before receiving
timeouts
and lastly, not friendly towards batched sending
65. “Because if they do, it’ll be the G8”
recursion and erlang #ftw
Bhasker Kode
Helpshift
@bhaskerkode
http://github.com/helpshift
git clone https://github.com/bosky101/fuconf
66. BONUS PART V
( if we have time)
are language designers sadists?
72. language designer: “i want to be concurrent”
compiler designer: “ok, lets try not bottle necking on resources. that way
it’ll be easier to garbage collect.”
language: ”ok, i’ll favour immutability. this may mean recreating entire
Objects/trees/structures and passing them around.
OO folks wont like this”
compiler: “btw since variables won’t change, can i re-use the call frame.”
language:”ok. as I need to add the sum of 100 numbers, i’ll create A1=1,
A2=increment(A1), …A100 = increment(A99)”
compiler: that looks too repetitive. use A = increment(100, previous) and
write a recursive fn that calls itself until 0.
76. • Guido, was in favour of not loosing stack
traces, and the fact that tail-call optimised
functions would be tougher to debug
• Trivia Alert! An early contributor to
python was an anonymous lisp hacker
“Amit Prem" who added
map,reduce,lambda to python in 1.0.0
TCO/recursion in other languages
77. • java compiler doesnt tco
• but scala allows tail recursive functions
• infact if you mention @tailrec it wont
compile until you make the function tail
recursive
• lua compiler does tail call optimisation
TCO/recursion in other languages
78. • The ECMAScript 4 spec was originally going to add
support for TCO, but it was dropped.
• But ECMAScript 6 does, but it only becomes a
standard mid 2015 ( PS: it also has destructuring
which is cool)
• in C compilers , do tail call optimisation but not
necessarily support tail recursion ( where the last
function is intact the same function )
TCO/recursion in other languages
79. pseudo-code notes
# not tail call optimised but is recursive
a(n){
….
return 1 + a(n-1)
}
# is tail call optimised but not recursive
a(n){
….
return b(n-1)
}
# is tail call optimised and is recursive
a(n){
….
return a(n-1)
}
80. • in erlang, if a function a calls b, and a function b
calls a, its memory foot print will never increase.
• infact all process that receive messages need to
be tail-recursive
• this is the fundamental construct behind long
lived processes and building state machines in
erlang
TCO/recursion in erlang
81. recursion and erlang #ftw
Bhasker Kode
Helpshift
@bhaskerkode
http://github.com/helpshift
git clone https://github.com/bosky101/fuconf