Usenet
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!uunet!rs
From: rs@uunet.UU.NET (Rich Salz)
Newsgroups: comp.sources.unix
Subject: v10i035: Interpreted Functional Programming lanuage, Part 02/07
Message-ID: <572@uunet.UU.NET>
Date: Tue, 7-Jul-87 00:32:09 EDT
Article-I.D.: uunet.572
Posted: Tue Jul 7 00:32:09 1987
Date-Received: Wed, 8-Jul-87 04:01:08 EDT
Organization: UUNET Communications Services, Arlington, VA
Lines: 2192
Approved: rs@uunet.uu.net
Mod.sources: Volume 10, Number 35
Submitted by: robison@b.cs.uiuc.edu (Arch Robison)
Archive-name: ifp/Part02
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh.
# The following files will be created:
# manual
export PATH; PATH=/bin:$PATH
if test -f 'manual'
then
echo shar: over-writing existing file "'manual'"
fi
cat << \SHAR_EOF > 'manual'
Professional Workstation
Research Group Technical Report #7
Illinois FP User's Manual
Arch D. Robison
Department of Computer Science
University of Illinois at Urbana-Champaign
Urbana, Illinois 61801
February 9, 1987
_T_a_b_l_e _o_f _C_o_n_t_e_n_t_s
1. Overview .......................................... 1
2. Prerequisites ..................................... 2
2.1. Organization .................................... 2
2.2. Environment (UNIX) .............................. 3
2.3. Environment (MSDOS) ............................. 3
3. Using IFP ......................................... 4
3.1. Starting IFP .................................... 4
3.2. Creating and Editing Definitions ................ 4
3.3. Applying Functions .............................. 5
3.4. Executing UNIX Commands ......................... 5
3.5. Executing MSDOS Commands ........................ 6
4. Language .......................................... 6
4.1. Objects ......................................... 6
4.2. Functions ....................................... 7
4.2.1. Primitive Functions ........................... 9
4.2.1.1. Structural Functions (/sys) ................. 11
4.2.1.2. Arithmetic (/math/arith) .................... 12
4.2.1.3. Logic (/math/logic) ......................... 14
4.2.1.4. String Functions (/sys) ..................... 16
4.2.1.5. Miscellaneous Functions (/sys) .............. 16
4.2.2. User Defined Functions ........................ 19
4.2.3. Functional Variables .......................... 20
4.3. Functional Forms ................................ 21
4.3.1. Constant ...................................... 21
4.3.2. Selection ..................................... 22
4.3.3. Composition ................................... 22
4.3.4. Construction .................................. 23
4.3.5. Apply to Each ................................. 23
4.3.6. If-Then-Else .................................. 24
4.3.7. Filter ........................................ 24
4.3.8. Right Insert .................................. 25
4.3.9. While ......................................... 26
4.3.10. Fetch[8] ..................................... 26
4.4. Comments ........................................ 27
4.5. Syntax Summary .................................. 27
5. IFP Graphics (optional)[9] ........................ 27
5.1. Coordinate System ............................... 28
5.2. Display List Structure .......................... 28
5.2.1. Polyline ...................................... 28
5.2.2. Color ......................................... 29
5.2.3. Transform ..................................... 29
5.2.4. Text .......................................... 29
6. Debugging ......................................... 30
7. Differences between IFP and Backus' FP ............ 31
7.1. Domain .......................................... 31
7.2. Functions ....................................... 31
7.3. Functional Forms ................................ 31
7.4. Syntax .......................................... 32
8. Functional Programming Techniques ................. 33
8.1. Functional Programming Identities ............... 33
8.2. Common Subfunctions ............................. 34
8.3. State Machines .................................. 34
8.4. Tail Recursion .................................. 35
9. Installation Notes ................................ 35
9.1. Machine Dependence .............................. 35
9.2. Compiling Options ............................... 36
i
_I_l_l_i_n_o_i_s _F_P _0._5 _U_s_e_r_s _M_a_n_u_a_l[_1]
_1. _O_v_e_r_v_i_e_w
Functional Programming (FP) [Bac78a] is a radically new
form of programming. FP programs have neither the control
flow nor variables of Von-Neumann languages. Instead pro-
grams are directly constructed from smaller programs. As a
result, FP offers a new style of programming with numerous
advantages, including:
Modular Programming
Program Verification
Parallel Processing
Optimization
IFP (Illinois Functional Programming) [Rob87a,Rob87b]
is an interactive functional programming implementation for
UNIX and MSDOS systems. The user may interactively create
and execute functional programs. In addition to Backus' FP,
IFP has the following features:
Hierarchical and Modular Function Organization
Block-Structured Syntax
Error Explanations
Graphics Display List Processor[2]
The interpreter is an order of magnitude more compact and
____________________
[1]Any resemblance to the real product is purely coin-
cidental.
[2]Once upon a time it worked. The code has since then
not been maintained. So it is not implemented in most ver-
sions.
February 9, 1987 IFP 0.5 Users Manual 2
faster than previous FP implementations.
_2. _P_r_e_r_e_q_u_i_s_i_t_e_s
The rest of the manual assumes the reader has read
Backus' original paper on FP. [Bac78a] Other references on
FP [Bad83a,Dar82a] may be of help. Additionally, parts of
the manual assume the reader understands UNIX or MSDOS[3]
file structure and paths.
_2._1. _O_r_g_a_n_i_z_a_t_i_o_n
IFP organizes functions in a tree structure analogous
to UNIX/MSDOS files. In fact each function is a file. For
UNIX systems, each user specifies the root (``IFP root'') of
their function tree. Within IFP, paths specify a path rela-
tive to the IFP root. The IFP root is set by a UNIX
environment variable. For MSDOS systems, the IFP root is
identical to the current drive root. (see ``Environment''
below).
Each node on the tree is either a function definition
(corresponding to a file), or a module (corresponding to a
directory). A function may reference another function via a
path.
To avoid having to write out the entire path for a
function every time, IFP has a function identifier importa-
tion feature. Functions from other modules may be imported
into a module. Once imported, a function may be referenced
February 9, 1987 IFP 0.5 Users Manual 3
as though it were defined in the module.
_2._2. _E_n_v_i_r_o_n_m_e_n_t (_U_N_I_X)
Before invoking IFP, two environment variables should
be set. The ``EDITOR'' variable should be set to the name of
your favorite editor. The ``IFProot'' variable should be
set to the absolute path of your ``IFP root''.[3] The
``IFPprompt'' is optional. If set, it changes the IFP
prompt. The default prompt is ``ifp> ''. Normally these
variables will be set by your .login file. Below is an
example of the commands which would appear in your .login
file.
setenv EDITOR = ``/usr/ucb/vi''
setenv IFProot = ``/mnt/bonzo/fproot''
setenv IFPprompt = ``ifp> ''
_2._3. _E_n_v_i_r_o_n_m_e_n_t (_M_S_D_O_S)
Before invoking IFP, two environment variables should
be set. The ``EDITOR'' and ``IFPDIR'' variables should be
set to the names of your favorite editor and directory lis-
ters respectively. Normally these should be set by your
autoexec.bat file, e.g.:
set EDITOR=C:ED.EXE
set IFPDIR=C:SD2.COM
Unlike the UNIX version, there is no IFProot variable. The
____________________
Use the actual path, not a symbolic link. When IFP
starts up, it assumes that the current directory path is a
prefix of the IFP root path.
February 9, 1987 IFP 0.5 Users Manual 4
root of the IFP file system is the root of the current
drive.
_3. _U_s_i_n_g _I_F_P
_3._1. _S_t_a_r_t_i_n_g _I_F_P
To start an IFP session, change your current working
directory to a directory under your IFP root. Then type
"ifp". Your current working directory becomes your IFP
current working module. When IFP is ready, it will respond
with the prompt ``ifp> ''. To end the IFP session, type
control-D or enter the command ``exit''.
_3._2. _C_r_e_a_t_i_n_g _a_n_d _E_d_i_t_i_n_g _D_e_f_i_n_i_t_i_o_n_s
To edit an IFP definition, type the command:
vi[4] foo
where foo is the name of the function to be edited. The
function may be one local to the current working module, or
one that is imported into the current working module. If
the function name is neither defined locally nor imported,
then it is assumed to be a new local function. To delete an
IFP definition, type the command:
rm[5] foo
____________________
[4]If your editor is not ``vi'' (as specified by the last
element of your EDITOR path), replace ``vi'' with your
editor's name. For MS-DOS, the command is always ``ed'', no
matter what the editor is called.
February 9, 1987 IFP 0.5 Users Manual 5
_3._3. _A_p_p_l_y_i_n_g _F_u_n_c_t_i_o_n_s
To apply an FP function, type the command[6]:
show object : function
The interpreter evaluates the result of applying the
function to the object. The result is then pretty-printed
at the terminal. Below are some example inputs and outputs.
show : reverse
show <1 2 3> : sum
6
show <1 2 3> : EACH [id,id]|* END | sum (* sum of squares *)
14
show <1 2 3 4 5> : EACH iota END
<
<1>
<1,2>
<1,2,3>
<1,2,3,4>
<1,2,3,4,5>
>
exit
_3._4. _E_x_e_c_u_t_i_n_g _U_N_I_X _C_o_m_m_a_n_d_s
If a command is not recognized by the IFP interpreter,
then it is passed on to the UNIX shell ``sh''. Commands
such as ``ls'' and ``more'' work as expected. Commands
which change environment do not work properly, as they
____________________
[5]For MS-DOS, the command is ``del''.
[6]Some earlier versions (before 0.4, e.g. the BYTE BIX
release) require a semicolon after the _f_u_n_c_t_i_o_n.
February 9, 1987 IFP 0.5 Users Manual 6
change their environment (within ``sh'') but not your own.
For example, the ``cd'' command does not work.
_3._5. _E_x_e_c_u_t_i_n_g _M_S_D_O_S _C_o_m_m_a_n_d_s
The only two MSDOS command that can be run from within
the interpreter are ``dir'' and ``del''. Some systems seem
to require ``dir/''. I don't know why.
_4. _L_a_n_g_u_a_g_e
IFP semantics are almost identical to Backus' FP,
though the syntax is quite different. The IFP language con-
sists of objects, functions, and functional forms. The sin-
gle operation is _a_p_p_l_y which maps a function and object into
a new object.
_4._1. _O_b_j_e_c_t_s
Objects in FP are either atoms, sequences, or _b_o_t_t_o_m.
The latter is a special object which denotes an undefined
value. Atoms are numbers, strings, or boolean values.
Strings must be quoted when they look like another kind of
atom or contain non-alphanumeric characters. Below is a
table of some typical atoms:
banana string
"The cat in the hat" string (double quotes)
'hello world' string (single quotes)
7 number
3.1415 number
1e6 number (million)
February 9, 1987 IFP 0.5 Users Manual 7
"1.414" string
t boolean true
f boolean false
"t" string
Sequences are lists of zero or more objects surrounded
by angle brackets. Sequences are written as:
1 2 n
Below is table of some typical sequences:
<1 2 3 4 5 6>
<>
<<1 2 3> t>
Either commas or spaces may be used to separate the elements
of a sequence. The elements of the sequence may be any kind
of object except ``?'', and do not have to be of the same
type.
IFP sequences have the _b_o_t_t_o_m _p_r_e_s_e_r_v_i_n_g [_B_a_c_7_8_a] pro-
perty. Any sequence containing ``?'' is itself equal to
``?''.
_4._2. _F_u_n_c_t_i_o_n_s
Functions in FP always have a single argument and a
single result. FP functions are analogous to UNIX programs
which transform ``standard input'' into ``standard output''
without side effects.
February 9, 1987 IFP 0.5 Users Manual 8
The IFP interpreter distinguishs two kinds of func-
tions: primitive functions and user-defined functions.
Primitive functions are built into the FP interpreter;
user-defined functions are created by the user. The only
distinction between the two kinds of functions is that
user-defined functions have definitions in terms of other
IFP functions. All functions may be used in the same
manner, neither primitive nor user-defined functions are
privileged in any way.
IFP functions are arranged in a tree structure analo-
gous to the way UNIX files are arranged. Each node of the
tree is either a module (directory) or function (file). A
function is referenced by its _p_a_t_h_n_a_m_e, which is a sequence
of node names separated by slashes. Pathnames follow the
UNIX conventions. Absolute pathnames begin with a slash,
which indicates that the path starts at the IFP root direc-
tory (as specified by the IFProot variable in your environ-
ment). Relative pathnames do not begin with a slash, which
indicates that the path starts at the current directory.
Within function definitions, the current directory is the
parent node of the function. Pathnames may contain ``..'',
which indicates moving up to the parent node.
For example, consider the node tree in Figure 1. The
root node is ``r''. Below are some ways the function ``b''
can reference the other nodes. Note that the name of the
root node is never explicitly used.
February 9, 1987 IFP 0.5 Users Manual 9
+-----+
| r |
+-----+
/ \
/ \
/ \
/ \
/ \
/ \
+-----+ +-----+
| tmp | | sys |
+-----+ +-----+
/ | \ / \
/ | \ / \
/ | \ / \
/ | \ / \
+-----+ +-----+ +-----+ +-----+ +-----+
| foo | | a | | b | | id | | sum |
+-----+ +-----+ +-----+ +-----+ +-----+
/ \
/ \
/ \
+-----+ +-----+
| p | | q |
+-----+ +-----+
Figure 1
_________________________
|_p_a_t_h_n_a_m_e________t_y_p_e______|
| /sys/sum absolute|
| /tmp/foo/p absolute|
| foo/p relative|
| ../tmp/foo/p relative|
| ../sys/sum relative|
|_________________________|
_4._2._1. _P_r_i_m_i_t_i_v_e _F_u_n_c_t_i_o_n_s
Primitive functions are built into the IFP interpreter.
They have pathnames like any other function, except that
there is no source code file for the function. The function
descriptions are grouped into sections below. The pathname
for the function's module is in parentheses at the top of
February 9, 1987 IFP 0.5 Users Manual 10
each section.[7]
The following sets (types) are used in the definitions
of functions:
A atoms
B boolean values
O objects
R real numbers
Z integers
S strings
T* sequences with element type T
T+ non-empty sequences with element type T
Tn sequences of length n with element type T
T[n,m] sequences of length m with element type Tn
[T ,T ] pair of types T and T
1 2 1 2
A function returns ``?'' if the argument is not in its
domain. The notation x denotes the nth element of a
n
sequence X.
For example, the domain of the addition function is
[X,Y] in [R,R]. That is addition takes a pair of real
numbers as its argument. We could also write this as [X,Y]
in R2, since a pair is a sequence of length two.
The types may be pictured neatly with the Venn diagram
in Figure 2:
____________________
[7] NOTE: The author does not worship backward compati-
bility. Future versions of IFP may put primitive functions
in different subdirectories.
February 9, 1987 IFP 0.5 Users Manual 11
+----------------------------+
| O |
| +--------------------+ |
| | A | |
| | +-----------+ | |
| | | B | | |
| | +-----------+ | |
| | | R | | |
| | | +---+ | | |
| | | | Z | | | |
| | | +---+ | | |
| | | | | |
| | +-----------+ | |
| | | O* | | |
| | +-----------+ | |
| | | |
| +--------------------+ |
| |
| +-+ |
| |?| |
| +-+ |
| |
+----------------------------+
Figure 2
_4._2._1._1. _S_t_r_u_c_t_u_r_a_l _F_u_n_c_t_i_o_n_s (/_s_y_s)
Structural functions are assemble, reorganize, and
select data. The primitive structural functions are listed
below:
February 9, 1987 IFP 0.5 Users Manual 12
__________________________________________________________________________
_|N_a_m_e_______D_o_m_a_i_n_________________D_e_f_i_n_i_t_i_o_n________________________________|
| |
|apndl [X,Y] in [O,On] |
| 1 2 n |
|apndr [X,Y] in [Om,O] |
| 1, 2 m |
| nm |
|cat X in O catenate subsequences, e.g. |
| < <3 5>> -> |
| n |
|distl [X,Y] in [O,O ] <...> |
| m |
|distr [X,Y] in [O ,O] <...> |
| n |
|dropl [X,K] in [O ,0<_Z<_n] |
| n |
|dropr [X,K] in [O ,0<_Z<_n] |
| |
|iota n in Z>_0 <1,2,...n> |
| n |
|length X in O n |
| n |
|pick [X,K] in [O ,0 of length K |
| n |
|reverse X in O |
| n |
|takel [X,K] in [O ,0<_Z<_n] |
| n |
|taker [X,K] in [O ,0<_Z<_n] |
| m>0 |
|tl X in O |
| m>0 |
|tlr X in O |
| [n,m] |
|trans X in O transpose matrix, e.g. |
_||________________________________<_<_a__1_>__<_b__2_>__<_c__3_>_>__-_>__<_<_a__b__c_>__<_1__2__3_>_>
_4._2._1._2. _A_r_i_t_h_m_e_t_i_c (/_m_a_t_h/_a_r_i_t_h)
Most IFP arithmetic functions are found here. Below is
a table of the existing functions. Some function's domain
may be further restricted due to range limitations.
February 9, 1987 IFP 0.5 Users Manual 13
_______________________________________________________________
_|N_a_m_e______D_o_m_a_i_n______________D_e_f_i_n_i_t_i_o_n_________________________|
| |
|+ [X,Y] in [R,R] X+Y |
| |
|- ... X-Y |
| |
|* ... XxY |
| |
|% [X,Y] in [R,R=/0] X/Y |
| |
|add1 X in R X+1 |
| |
|arcsin X in R, -1<_X<_1 arcsinX |
| |
|arccos X in R, -1<_X<_1 arccosX |
| |
|arctan X in R arctanX |
| |
|cos X in R cosX |
| |
|div [X,Y] in [R,R=/0] floor(X/Y) |
| |
|exp X in R eX |
| |
|ln X in R>0 log X |
| e |
|max [X,Y] in [R,R] max(X,Y) |
| |
|min [X,Y] in [R,R] min(X,Y) |
| |
|minus X in R -X |
| |
|mod [X,Y] in [R,R] X-Yfloor(X/Y) if Y=/0, 0 otherwise|
| |
|power [X,Y] in [R>_0,R] XY |
| |
|sin X in R sinX |
| _ |
|sqrt X in R>0 \|X |
| |
|sub1 X in R X-1 |
| |
|sum X in R* X +X +...+X |
| 1 2 n |
|tan X in R tanX |
_|______________________________________________________________|
February 9, 1987 IFP 0.5 Users Manual 14
_4._2._1._3. _L_o_g_i_c (/_m_a_t_h/_l_o_g_i_c)
Most IFP primitive functions returning boolean values
are found here. Below is a table of the existing functions:
February 9, 1987 IFP 0.5 Users Manual 15
______________________________________________
|_N_a_m_e_______D_o_m_a_i_n__________________D_e_f_i_n_i_t_i_o_n___|
| |
| = [X,Y] in [O,O] X=Y |
| |
| ~= ... X=/Y |
| |
| < [X,Y] in [R,R]U[S,S] X= ... X>_Y |
| |
| > ... X>Y |
| |
| ~ X in B ~X |
| |
| and [X,Y] in [B,B] X/\Y |
| |
| all X in B* /\x |
| k k |
| |
| any X in B* \/xk |
| k |
| atom X in O X in A |
| |
| boolean X in O X in B |
| |
| false X in O X=#f |
| |
| imply [X,Y] in [B,B] ~X\/Y |
| |
| longer [X,Y] in [Om,On] m>n |
| |
| member [X,Y] in [O*,O] Y in X |
| |
| numeric X in O X in R |
| |
| null X in O* X=<> |
| |
| odd X in Z X mod 2 = 1|
| |
| or [X,Y] in [B,B] X\/Y |
| |
| pair X in O X in [O,O] |
| |
| shorter [X,Y] in [Om,On] m "123" |
_|___________________________________________________________|
_4._2._1._5. _M_i_s_c_e_l_l_a_n_e_o_u_s _F_u_n_c_t_i_o_n_s (/_s_y_s)
The miscellaneous functions are listed below. Each
function description is preceded by a title line of the
form:
____________________________________________________________
function domain definition
____________________________________________________________
apply [X,F] in [O,S*] apply F to X
F is a sequence of strings representing a pathname
to a defined function. The result is the function
referenced by F applied to X. Example:
<<3 4>