Differences between revisions 13 and 14
Revision 13 as of 2007-12-01 19:42:24
Size: 10264
Editor: Ted Herman
Comment:
Revision 14 as of 2014-05-25 18:23:59
Size: 10302
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[TableOfContents]] <<TableOfContents>>
Line 11: Line 11:
 * [http://www.erlang.org/course/course.html Mini-Course on Erlang].
 * [http://www.erlang.org/doc/pdf/getting_started.pdf Getting Started (PDF)].
 * [[http://www.erlang.org/course/course.html|Mini-Course on Erlang]].
 * [[http://www.erlang.org/doc/pdf/getting_started.pdf|Getting Started (PDF)]].
Line 16: Line 16:
 * [http://www.erlang.org/doc/ Index to Online Documents].  * [[http://www.erlang.org/doc/|Index to Online Documents]].
Line 20: Line 20:
Erlang can be installed easily on Windows machines, downloading the latest version from [http://erlang.mirror.libertine.org/download.html here]. For installing on Linux, Unix or Mac, the situation is less clear. I did manage to install it on Linux, but it was far from simple. An executable version of Erlang is available on the department's Linux cluster, following instructions below. Erlang can be installed easily on Windows machines, downloading the latest version from [[http://erlang.mirror.libertine.org/download.html|here]]. For installing on Linux, Unix or Mac, the situation is less clear. I did manage to install it on Linux, but it was far from simple. An executable version of Erlang is available on the department's Linux cluster, following instructions below.
Line 27: Line 27:
  You need to edit your ''path'' environment variable, so that it has {{{C:\Program Files\erl5.5.5\bin}}} in the list of directories. How to do this is not obvious; on Windows XP this can be done via {{{My Computer -> Properties -> Advanced -> path}}}, then edit path to append the Erlang bin directory. For more detail, see [http://vlaurie.com/computers2/Articles/environment.htm this article] for example.   You need to edit your ''path'' environment variable, so that it has {{{C:\Program Files\erl5.5.5\bin}}} in the list of directories. How to do this is not obvious; on Windows XP this can be done via {{{My Computer -> Properties -> Advanced -> path}}}, then edit path to append the Erlang bin directory. For more detail, see [[http://vlaurie.com/computers2/Articles/environment.htm|this article]] for example.
Line 47: Line 47:
 * [http://www.erlang.org/examples/small_examples/index.html small examples]
 * [http://www.erlang.org/doc/programming_examples/part_frame.html more examples]
 * [http://www.erlang.org/examples/examples-2.0.html or these examples] (but you need to download a compressed tar file to get all of the code).
 * [[http://www.erlang.org/examples/small_examples/index.html|small examples]]
 * [[http://www.erlang.org/doc/programming_examples/part_frame.html|more examples]]
 * [[http://www.erlang.org/examples/examples-2.0.html|or these examples]] (but you need to download a compressed tar file to get all of the code).
Line 53: Line 53:
The [http://www.erlang.org/doc/ Erlang Manual] has a standard library [http://www.erlang.org/doc/apps/stdlib/index.html index] where you can browse built-in functions. The [[http://www.erlang.org/doc/|Erlang Manual]] has a standard library [[http://www.erlang.org/doc/apps/stdlib/index.html|index]] where you can browse built-in functions.
Line 70: Line 70:
This is '''''optional''''' and not well documented. The [http://www.erlang.org/doc/apps/gs/part_frame.html users guide] is currently not much more than an index, but with enough patience and experimenting, you probably can get windows, buttons, colors, and so forth to work. This is '''''optional''''' and not well documented. The [[http://www.erlang.org/doc/apps/gs/part_frame.html|users guide]] is currently not much more than an index, but with enough patience and experimenting, you probably can get windows, buttons, colors, and so forth to work.
Line 75: Line 75:
trying [http://www.erlang.org/doc/apps/toolbar/index.html toolbar] : trying [[http://www.erlang.org/doc/apps/toolbar/index.html|toolbar]] :
Line 84: Line 84:
The [http://www.erlang.org/doc/apps/debugger/index.html debugger module] might be helpful to find problems in your programs. The [[http://www.erlang.org/doc/apps/debugger/index.html|debugger module]] might be helpful to find problems in your programs.
Line 88: Line 88:
The [http://www.erlang.org/doc/apps/parsetools/index.html parsetools] module makes it easy to parse with a BNF grammar. The [[http://www.erlang.org/doc/apps/parsetools/index.html|parsetools]] module makes it easy to parse with a BNF grammar.
Line 92: Line 92:
The [http://www.erlang.org/doc/apps/webtool/index.html webtool] module is for building a simple web server. (In the other direction, recall that the [http://www.erlang.org/examples/small_examples/index.html small examples] list contains an example to fetch a page from a webserver.) The [[http://www.erlang.org/doc/apps/webtool/index.html|webtool]] module is for building a simple web server. (In the other direction, recall that the [[http://www.erlang.org/examples/small_examples/index.html|small examples]] list contains an example to fetch a page from a webserver.)
Line 96: Line 96:
I haven't looked through the code available in [http://www.erlang.org/user.html user-contributed examples]; some of them could be useful for advanced applications. I haven't looked through the code available in [[http://www.erlang.org/user.html|user-contributed examples]]; some of them could be useful for advanced applications.
Line 100: Line 100:
You can find some examples in the ["erlang reading assignment"] page. You can find some examples in the [[erlang reading assignment]] page.
Line 104: Line 104:
Here's a simple example, adapted from [http://www.erlang.org/course/concurrent_programming.html the online course], showing how processes can send messages in a ''call/response'' pattern, something like a subroutine call: Here's a simple example, adapted from [[http://www.erlang.org/course/concurrent_programming.html|the online course]], showing how processes can send messages in a ''call/response'' pattern, something like a subroutine call:
Line 180: Line 180:
The first line, c(concurrentFac, debug_info), compiles the program with debugging information added, which enables using the debugger module. Line 2 starts the debugger, which opens a window (to be demonstrated in class). Line 3 then starts concurrentFac:fac(5), under control of the debugger. Then we can step through execution to see what is happening. The Erlang user manual has a chapter about the [http://www.erlang.org/doc/apps/debugger/debugger_chapter.html debugger]. The first line, c(concurrentFac, debug_info), compiles the program with debugging information added, which enables using the debugger module. Line 2 starts the debugger, which opens a window (to be demonstrated in class). Line 3 then starts concurrentFac:fac(5), under control of the debugger. Then we can step through execution to see what is happening. The Erlang user manual has a chapter about the [[http://www.erlang.org/doc/apps/debugger/debugger_chapter.html|debugger]].
Line 184: Line 184:
This ["erlang palindrome example"] was presented in class. This [[erlang palindrome example]] was presented in class.

Erlang

Erlang is a "Concurrency Oriented Programming" language.

Learning the Language

Recommended resources for learning Erlang:

As backup to the above, there are other online documents, even a book on Erlang, but you shouldn't need these for this course.

Using the Language

Erlang can be installed easily on Windows machines, downloading the latest version from here. For installing on Linux, Unix or Mac, the situation is less clear. I did manage to install it on Linux, but it was far from simple. An executable version of Erlang is available on the department's Linux cluster, following instructions below.

Key Idea: the "Path" environment variable

Whether you use a Unix/Linux version or a Windows version, the default behavior (which we don't like) is that Erlang programs would be located in the same directory as the Erlang "program files" (binary executables, etc) -- rather than allowing you to put your own Erlang programs in your own directory. Here is how to get around this default behavior.

Windows: suppose you have installed Erlang, and all the program files have been put into C:\Program Files\erl5.5.5\ -- the "erl" compiler will be in the bin subdirectory of this. Here is how to set up your Windows account so that it will automatically find Erlang program files, even within your own directory:

  • You need to edit your path environment variable, so that it has C:\Program Files\erl5.5.5\bin in the list of directories. How to do this is not obvious; on Windows XP this can be done via My Computer -> Properties -> Advanced -> path, then edit path to append the Erlang bin directory. For more detail, see this article for example.

Once this is done, you can start a Command Prompt, and position the current directory to wherever you'd like to have your Erlang program files. Once there, you execute the erl command and for example "c(test)." should attempt to compile the file test.erl in the current directory (assuming you have made a test.erl file). I created and editted erl programs using Notepad, but other editors would be OK also.

Department Linux Cluster: I'm supposing that you are using the default shell for CS department accounts, tcsh or csh or something like that (the alternative would be bash). What you need to do is change the PATH environment variable. Check to see if you have a file in your home directory called ".cshrc". You should have this file, which has commands and parameters for each time you log in. Add this line to the file:

  • setenv PATH {$PATH}:/space/herman/public/bin

That line appends /space/herman/public/bin to your PATH variable (you may need to log out and log back in for this change to take effect). Check your results with the command

  • echo $PATH

-- you should find the /space/herman/public/bin listed in the reponse output. Then try the command

  • erl

and you should see a prompt from Erlang. You're all set.

Example Erlang Programs

I'll be adding example Erlang programs here; you can also look at

BIFs (Built-In Functions) and Applications

The Erlang Manual has a standard library index where you can browse built-in functions.

The io module

One built-in you'll probably want to use is the io module. Example:

  •  % comment:  the following outputs "abc" to standard output
     io:put_chars("abc").
     % comment:  same as above, but also print a newline character 
     io:put_chars("abc"), io:nl().
     % comment:  you can also read from standard input, line this:
     io:read('enter>').

The io module also has something similar to the printf formatting string with substitutions (also recently added to Java).

The gs module (for widgets)

This is optional and not well documented. The users guide is currently not much more than an index, but with enough patience and experimenting, you probably can get windows, buttons, colors, and so forth to work.

Tools

There are lots of tools for developing Erlang programs. You can get some idea of the possibilities (though it's confusing to use this without reading documentation), by trying toolbar :

  •  toolbar:start().

Try the above after starting Erlang with the erl command, and a window should appear with lots of trace/debugging features.

Debugger Module

The debugger module might be helpful to find problems in your programs.

The Parser Module

The parsetools module makes it easy to parse with a BNF grammar.

Web Tools

The webtool module is for building a simple web server. (In the other direction, recall that the small examples list contains an example to fetch a page from a webserver.)

Other Examples

I haven't looked through the code available in user-contributed examples; some of them could be useful for advanced applications.

The Reading Assignment

You can find some examples in the erlang reading assignment page.

Simple Concurrency

Here's a simple example, adapted from the online course, showing how processes can send messages in a call/response pattern, something like a subroutine call:

  •  -module(subroutine).
     -export([start/0, caller/0, square/0]).
    
     square() ->
      io:put_chars("square started"), io:nl(),
      receive
        { call, Who, Value } ->
           io:put_chars("square got message"), io:nl(),
           Who ! { response, Value++Value }
      end,
      true.
    
     caller() ->
      io:put_chars("caller started"), io:nl(),
      SubProcess = spawn(subroutine, square, []),
      SubProcess ! { call, self(), "abc" },
      receive
        { response, Value } -> io:put_chars(Value), io:nl()
      end,
      true.
    
     start() -> spawn(subroutine, caller, []).

Notes:

  1. Ending a process with the atom true is expected, it seems.

  2. Starting a process has three parameters:
    •      spawn ( <name of module> ,  <name of exported function> , <parameter> )
      Notice that if the exported function has no parameter, use [] for that.
  3. It's a good idea to put output statements when learning.

Exercises Generalize the above.

  • The caller could spawn square just once, and send messages many times to get reponses (so that square will be like a server); in that case, square should be a recursive or iterative function, getting a message in each iteration -- and if the message it gets is the atom stop, then square should terminate.

  • Try changing the calling argument send to square from a string to a number, learning how to do that in Erlang.
  • The caller could spawn multiple square instances, saving the process identifiers in a list; then, each call should randomly choose one in the list to send a message and get a response.

Debugging Concurrency

Here is an example that really is not concurrent, but uses Erlang process sending and receiving messages to compute factorial. Let us suppose that the file concurrentFac.erl contains the following code:

  •  -module(concurrentFac).
     -export([fac/1, facslave/0 ]).
    
     facslave() -> % notice: no parameters to facslave
      receive      % because facslave gets arguments from messages
        {calculate,Source,{X,ProductSoFar}} when X<1 ->  % X<1 is trivial case
           Source ! {result,ProductSoFar};  % send result back to factorial function
        {calculate,Source,{X,ProductSoFar}} ->  % not done, so send message to self!
           self() ! {calculate,Source,{X-1,X*ProductSoFar}}  % message is "smaller" work
      end,
      if X >= 1 -> facslave();   % need recursion only in this case
         X < 1  -> true        % better return "true" or Erlang will complain!
      end.
    
     fac(N) ->   % main function
      io:format("~w fac started with ~w~n",[self(),N]),  % maybe useful for debugging ?
      P = spawn(concurrentFac, facslave, []),   % spawn the slave process to do the work
      io:format("~w facslave was spawned~n",[P]),
      P ! {calculate,self(),{N,1}},  % send slave the problem
      receive
        {result,Z} -> true  % wait for result from slave process
      end,
      Z.  % return whatever Z bound to as result of receive

Now imagine that we're at the keyboard, and enter the Erlang shell (say, by invoking the erl command). We enter the following:

  •  1> c(concurrentFac, debug_info).
       {ok,concurrentFac}
     2> debugger:start().
       {ok,<0.60.0>}
     3> debugger:quick(concurrentFac,fac,[5]).

The first line, c(concurrentFac, debug_info), compiles the program with debugging information added, which enables using the debugger module. Line 2 starts the debugger, which opens a window (to be demonstrated in class). Line 3 then starts concurrentFac:fac(5), under control of the debugger. Then we can step through execution to see what is happening. The Erlang user manual has a chapter about the debugger.

Palindrome Example

This erlang palindrome example was presented in class.

erlang (last edited 2014-05-25 18:23:59 by localhost)