Advantages of Erlang
Advantages of Erlang
The previous examples have shown how functions The 3rd equations in both lookup and lookup1
can be defined using sets of recursion equations. This for- specifies the recursive tree walking case where we succes-
malism, while simple, has considerable power. We can show sively follow the branches of the tree, which branch depend-
some of the expressive power of Erlang by examining a ing upon the next digit in the sequence.
simplified telephony example. Manipulation of a 10-way tree shows some of the
The example is a dialled number analyser. We define power of Erlang for a typical telephony programming
a function lookup(Seq,Struct) which analyses some number problem. Tree insertion is equally simple and is left as an
sequence Seq to see if it represents a valid subscriber exercise for the reader.
number. lookup can be defined as follows: 3. EXAMPLES WITH COMMUNICATION
/* Erlang has primitives for creating a parallel process
* lookup(Seq, Struct) returns (spawn), and for sending and receiving messages (send and
* subscriber(Who) if a valid sequence receive). To illustrate this we take the example of a simple
* getimoreidigits if an ambiguous sequence process that could be created to count charging pulses in a
* invalid if an invalid sequence telephony metering system.
*/ Firstly a process that records debiting pulses.
lookup(Seq, nil) -> -module(counter).
ˆinvalid. -export([start/2]).
lookup([], Struct) ->
ˆgetimoreidigits. start(Sys, Proc) ->
lookup([DigiteMoreidigits], Struct) -> counter(Sys, Proc, 0).
Arg = index(Digit),
X = getiarg(Arg, Stuct), counter(Sys, Proc, Tot) ->
lookup1(X, Moreidigits). receive {
Proc ? bump =>
lookup1(nil, i) -> Tot1 = Tot + 1,
ˆinvalid. counter(Sys, Proc, Tot1);
lookup1(subscriber(Who), i) -> Sys ? report =>
ˆsubscriber(Who). Sys ! Tot,
lookup1(Struct, Moreidigits) -> counter(Sys, Proc, Tot);
lookup(Moreidigits, Struct). Sys ? reset =>
counter(Sys, Proc, 0).
How does this work? Struct is a data structure defined
recursively as follows: The function start is evaluated to start a counter process.
The counter process receives either a bump message from
the process Proc or one of the messages report or reset from
the process Sys. ringingib(A, B) ->
Note that in this example we have included module receive {
declarations. The module declaration in the example causes B ? offihook =>
the function start to be exported from the module, whereas B ! stopiring,
the function counter is purely local to the module. Exported A ! answered,
functions are the only functions which can be accessed from conversationib(A, B);
outside the module. A ? terminate =>
B ! stopiring,
A set of such a counter process could be started as idle(B)
follows: }.
-module(sysistart).
-export([init/2]). The A side of the call evaluates the function
ringingia, the B side ringingib . From the A sides point of
init(Sys,0). view three things can happen. The A side can stop the call
init(Sys,Max) -> attempt by going on hook (the message onihook is received
Id = spawn(lineihandler:start(port(Max))), from the process A), the B partner can answer (the message
spawn(counter:start(Sys, Id)), answered is received from the process B) or a timeout can
Max1 = Max - 1, occur (ie. after 10 seconds the event timeout occurs).
init(Sys, Max1). The Erlang syntax used to express this is:
The function sysistart:init(Sys, Max) when evaluated spawns receive 10 {
Max line handler processes and connects to each line handler A ? onihook =>
process a process to count the number of charging pulses ...
sent by the line handler process. B ? answered =>
Note that inter-module references require fully ...
qualified names - thus we refer to counter:start within the timeout =>
module sysistart but simply start within the module counter. ...
}.
A flexible message passing and process spawning
mechanism provides a convenient framework in which sim- where ... represents the code to be executed if the
ple fragments of a telephony program can be expressed. For condition is true.
example we can illustrates part of the code for a conven- If an onihook message is received the ring tone that
tional POTS like application. the calling subscriber hears is stopped by sending the mes-
The code describes the situation which occurs when a sage stopitone to the process A ( A ! stopitone), and a mes-
call is being set up. We assume that the calling (or A) sub- sage is sent to the called partner to abandon the call attempt
scriber has dialled a valid number and the called (or B) (B ! terminate). Finally, the function idle(A) is evaluated.
number was free. If the called partner answers, the ringing tone is
In such circumstances the caller will hear a ring tone, removed and the switch started (startiswitch(A,B)).
and the called subscribers phone will be ringing. The call is now in progress and the functions
This situation can be described by a pair of communi- conversationia and conversationib will control future pro-
cating functions ringingia/2 and ringingib/2. gress in the call.
ringingia(A, B) -> One further point to note about the receive primitive
receive 10 { is that it is selective, that is to say it takes the first message
A ? onihook => that matches from a queue of messages that are waiting for
A ! stopitone, the attention of the receiving process. It also queues any
B ! terminate, unmatched messages.
idle(A); In our previous example the process which was
B ? answered => evaluating the function ringingia was expecting one of the
A ! stopitone, messages onihook or, answered. Any other messages which
startiswitch(A, B), arrive at the process while it is waiting will be automatically
conversationia(A, B); queued for future processing.
timeout =>
A ! stopitone, For example: Suppose the following messages have
B ! terminate, been sent (but not yet received) to a process, eventually they
waiticlear(A) end up in an input queue to the process:
}. p1,onihook
p2,offihook
p3,digit(3)
p3,digit(4)
...
REFERENCES
..
[1] Dacker B., Elshiewy N., Hedeland P., Welin C-W. and
Williams M. C., Experiments with Programming Languages
and Techniques for Telecommunication Applications Proc.
6th Int Conf. on Software Engineering for Telecommunica-
tion Switching Systems, Eindhoven, The Netherlands, 14-18
April 1986.
[2] Armstrong, J. L., Elshiewy, N. A. and Virding, S. R.,
The Phoning Philosopher’s Problem or Logic Programming
for Telecommunications Applications Third IEEE Sympo-
sium on Logic Programming, September 23-26, 1986, Salt
Lake City, Utah
[3] Armstrong, J.L., Williams, M.C. Using Prolog for Rapid
Prototyping of Telecommunications Systems. 7th Int Conf.
on Software Engineering for Telecommunication Switching
Systems, Bournemouth 3 - 7 July 1989.
[4] Clocksin W. F. and Mellish, C. S. Programming in Pro-
log. Springer Verlag 1981.
[5] CCITT Recommendation Z.100 Specification Description
Language. (SDL)
[6] Reprint of Ericsson Review articles on MD 110 from nos
1 and 2 1982
[7] STRAND: New Concepts in Parallel Processing. Ian
Foster and Steven Taylor. Prentice Hall, 1989.