File: bind.tex

package info (click to toggle)
sollya 8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,592 kB
  • sloc: ansic: 124,655; yacc: 7,543; lex: 2,440; makefile: 888; cpp: 77
file content (124 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
\subsection{bind}
\label{labbind}
\noindent Name: \textbf{bind}\\
\phantom{aaa}partially applies a procedure to an argument, returning a procedure with one argument less\\[0.2cm]
\noindent Usage: 
\begin{center}
\textbf{bind}(\emph{proc}, \emph{ident}, \emph{obj}) : (\textsf{procedure}, \textsf{identifier type}, \textsf{any type}) $\rightarrow$ \textsf{procedure}\\
\end{center}
Parameters: 
\begin{itemize}
\item \emph{proc} is a procedure to be partially applied to an argument
\item \emph{ident} is one of the formal arguments of \emph{proc}
\item \emph{obj} is any \sollya object \emph{ident} is to be bound to
\end{itemize}
\noindent Description: \begin{itemize}

\item \textbf{bind} allows a formal parameter \emph{ident} of a procedure \emph{proc} to 
   be bound to an object \emph{obj}, hence \emph{proc} to be partially applied.
   The result of this curryfied application, returned by \textbf{bind}, is 
   a procedure with one argument less. This way, \textbf{bind} permits 
   specialization of a generic procedure, parameterized e.g. by a function
   or range.

\item In the case when \emph{proc} does not have a formal parameter named 
   \emph{ident}, \textbf{bind} prints a warning and returns the procedure 
   \emph{proc} unmodified. 

\item \textbf{bind} always returns a procedure, even if \emph{proc} only has one 
   argument, which gets bound to \emph{ident}. In this case, \textbf{bind}
   returns a procedure which does not take any argument. Hence 
   evaluation, which might provoke side effects, is only performed
   once the procedure gets used.

\item If \textbf{bind} is used on a procedure with an arbitrary number of
   arguments, no check is performed whether the procedure actually
   has a parameter of name \emph{ident}. \textbf{bind} returns a procedure
   with an arbitrary number of arguments whose argument has the
   same name as the original procedure \emph{proc}. That returned procedure
   binds \emph{ident} to \emph{obj} and then runs \emph{proc} within this context,
   applying it to its original argument. The execution of \textbf{bind}
   fails for procedures with an arbitrary number of arguments if
   the original procedure's argument has the same name as \emph{ident}.
   In this case, the procedure is returned unmodified.
\end{itemize}
\noindent Example 1: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure add(X,Y) { return X + Y; };
> succ = bind(add,X,1);
> succ(5);
6
> succ;
proc(Y)
{
nop;
return (proc(X, Y)
{
nop;
return (X) + (Y);
})(1, Y);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 2: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure add(X,Y) { return X + Y; };
> succ = bind(add,X,1);
> five = bind(succ,Y,4);
> five();
5
> five;
proc()
{
nop;
return (proc(Y)
{
nop;
return (proc(X, Y)
{
nop;
return (X) + (Y);
})(1, Y);
})(4);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 3: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> verbosity = 1!;
> procedure add(X,Y) { return X + Y; };
> foo = bind(add,R,1);
Warning: the given procedure has no argument named "R". The procedure is returne
d unchanged.
> foo;
proc(X, Y)
{
nop;
return (X) + (Y);
}
\end{Verbatim}
\end{minipage}\end{center}
\noindent Example 4: 
\begin{center}\begin{minipage}{15cm}\begin{Verbatim}[frame=single]
> procedure sum_times(l=...) { var s, i; s = 0; for i in l do s = s + i; return 
c * s; };
> foo = bind(sum_times, c, 17);
> foo;
proc(l = ...)
{
var c;
c = 17;
return (proc(l = ...)
{
var s, i;
s = 0;
for i in l do
s = (s) + (i);
return (c) * (s);
}) @ (l);
}
> foo(1, 2, 3);
102
\end{Verbatim}
\end{minipage}\end{center}
See also: \textbf{procedure} (\ref{labprocedure}), \textbf{proc} (\ref{labproc}), \textbf{function} (\ref{labfunction}), \textbf{@} (\ref{labconcat})