| 1 |
= InfixingOperators |
| 2 |
|
| 3 |
Fixity specifications are not part of signatures in |
| 4 |
<<StandardML#,Standard ML>>. When one wants to use a module that |
| 5 |
provides functions designed to be used as infix operators there are |
| 6 |
several obvious alternatives: |
| 7 |
|
| 8 |
* Use only prefix applications. Unfortunately there are situations |
| 9 |
where infix applications lead to considerably more readable code. |
| 10 |
|
| 11 |
* Make the fixity declarations at the top-level. This may lead to |
| 12 |
collisions and may be unsustainable in a large project. Pollution of |
| 13 |
the top-level should be avoided. |
| 14 |
|
| 15 |
* Make the fixity declarations at each scope where you want to use |
| 16 |
infix applications. The duplication becomes inconvenient if the |
| 17 |
operators are widely used. Duplication of code should be avoided. |
| 18 |
|
| 19 |
* Use non-standard extensions, such as the <<MLBasis#,ML Basis system>> |
| 20 |
to control the scope of fixity declarations. This has the obvious |
| 21 |
drawback of reduced portability. |
| 22 |
|
| 23 |
* Reuse existing infix operator symbols (`^`, `+`, `-`, ...). This |
| 24 |
can be convenient when the standard operators aren't needed in the |
| 25 |
same scope with the new operators. On the other hand, one is limited |
| 26 |
to the standard operator symbols and the code may appear confusing. |
| 27 |
|
| 28 |
None of the obvious alternatives is best in every case. The following |
| 29 |
describes a slightly less obvious alternative that can sometimes be |
| 30 |
useful. The idea is to approximate Haskell's special syntax for |
| 31 |
treating any identifier enclosed in grave accents (backquotes) as an |
| 32 |
infix operator. In Haskell, instead of writing the prefix application |
| 33 |
`f x y` one can write the infix application `x {backtick}f{backtick} y`. |
| 34 |
|
| 35 |
|
| 36 |
== Infixing operators |
| 37 |
|
| 38 |
Let's first take a look at the definitions of the operators: |
| 39 |
|
| 40 |
[source,sml] |
| 41 |
---- |
| 42 |
infix 3 <\ fun x <\ f = fn y => f (x, y) (* Left section *) |
| 43 |
infix 3 \> fun f \> y = f y (* Left application *) |
| 44 |
infixr 3 /> fun f /> y = fn x => f (x, y) (* Right section *) |
| 45 |
infixr 3 </ fun x </ f = f x (* Right application *) |
| 46 |
|
| 47 |
infix 2 o (* See motivation below *) |
| 48 |
infix 0 := |
| 49 |
---- |
| 50 |
|
| 51 |
The left and right sectioning operators, `<\` and `/>`, are useful in |
| 52 |
SML for partial application of infix operators. |
| 53 |
<<References#Paulson96,ML For the Working Programmer>> describes curried |
| 54 |
functions `secl` and `secr` for the same purpose on pages 179-181. |
| 55 |
For example, |
| 56 |
|
| 57 |
[source,sml] |
| 58 |
---- |
| 59 |
List.map (op- /> y) |
| 60 |
---- |
| 61 |
|
| 62 |
is a function for subtracting `y` from a list of integers and |
| 63 |
|
| 64 |
[source,sml] |
| 65 |
---- |
| 66 |
List.exists (x <\ op=) |
| 67 |
---- |
| 68 |
|
| 69 |
is a function for testing whether a list contains an `x`. |
| 70 |
|
| 71 |
Together with the left and right application operators, `\>` and `</`, |
| 72 |
the sectioning operators provide a way to treat any binary function |
| 73 |
(i.e. a function whose domain is a pair) as an infix operator. In |
| 74 |
general, |
| 75 |
|
| 76 |
---- |
| 77 |
x0 <\f1\> x1 <\f2\> x2 ... <\fN\> xN = fN (... f2 (f1 (x0, x1), x2) ..., xN) |
| 78 |
---- |
| 79 |
|
| 80 |
and |
| 81 |
|
| 82 |
---- |
| 83 |
xN </fN/> ... x2 </f2/> x1 </f1/> x0 = fN (xN, ... f2 (x2, f1 (x1, x0)) ...) |
| 84 |
---- |
| 85 |
|
| 86 |
|
| 87 |
=== Examples |
| 88 |
|
| 89 |
As a fairly realistic example, consider providing a function for sequencing |
| 90 |
comparisons: |
| 91 |
|
| 92 |
[source,sml] |
| 93 |
---- |
| 94 |
structure Order (* ... *) = |
| 95 |
struct |
| 96 |
(* ... *) |
| 97 |
val orWhenEq = fn (EQUAL, th) => th () |
| 98 |
| (other, _) => other |
| 99 |
(* ... *) |
| 100 |
end |
| 101 |
---- |
| 102 |
Using `orWhenEq` and the infixing operators, one can write a |
| 103 |
`compare` function for triples as |
| 104 |
|
| 105 |
[source,sml] |
| 106 |
---- |
| 107 |
fun compare (fad, fbe, fcf) ((a, b, c), (d, e, f)) = |
| 108 |
fad (a, d) <\Order.orWhenEq\> `fbe (b, e) <\Order.orWhenEq\> `fcf (c, f) |
| 109 |
---- |
| 110 |
|
| 111 |
where `{backtick}` is defined as |
| 112 |
|
| 113 |
[source,sml] |
| 114 |
---- |
| 115 |
fun `f x = fn () => f x |
| 116 |
---- |
| 117 |
|
| 118 |
Although `orWhenEq` can be convenient (try rewriting the above without |
| 119 |
it), it is probably not useful enough to be defined at the top level |
| 120 |
as an infix operator. Fortunately we can use the infixing operators |
| 121 |
and don't have to. |
| 122 |
|
| 123 |
Another fairly realistic example would be to use the infixing operators with |
| 124 |
the technique described on the <<Printf#>> page. Assuming that you would have |
| 125 |
a `Printf` module binding `printf`, `{backtick}`, and formatting combinators |
| 126 |
named `int` and `string`, you could write |
| 127 |
|
| 128 |
[source,sml] |
| 129 |
---- |
| 130 |
let open Printf in |
| 131 |
printf (`"Here's an int "<\int\>" and a string "<\string\>".") 13 "foo" end |
| 132 |
---- |
| 133 |
|
| 134 |
without having to duplicate the fixity declarations. Alternatively, you could |
| 135 |
write |
| 136 |
|
| 137 |
[source,sml] |
| 138 |
---- |
| 139 |
P.printf (P.`"Here's an int "<\P.int\>" and a string "<\P.string\>".") 13 "foo" |
| 140 |
---- |
| 141 |
|
| 142 |
assuming you have the made the binding |
| 143 |
|
| 144 |
[source,sml] |
| 145 |
---- |
| 146 |
structure P = Printf |
| 147 |
---- |
| 148 |
|
| 149 |
|
| 150 |
== Application and piping operators |
| 151 |
|
| 152 |
The left and right application operators may also provide some notational |
| 153 |
convenience on their own. In general, |
| 154 |
|
| 155 |
---- |
| 156 |
f \> x1 \> ... \> xN = f x1 ... xN |
| 157 |
---- |
| 158 |
|
| 159 |
and |
| 160 |
|
| 161 |
---- |
| 162 |
xN </ ... </ x1 </ f = f x1 ... xN |
| 163 |
---- |
| 164 |
|
| 165 |
If nothing else, both of them can eliminate parentheses. For example, |
| 166 |
|
| 167 |
[source,sml] |
| 168 |
---- |
| 169 |
foo (1 + 2) = foo \> 1 + 2 |
| 170 |
---- |
| 171 |
|
| 172 |
The left and right application operators are related to operators |
| 173 |
that could be described as the right and left piping operators: |
| 174 |
|
| 175 |
[source,sml] |
| 176 |
---- |
| 177 |
infix 1 >| val op>| = op</ (* Left pipe *) |
| 178 |
infixr 1 |< val op|< = op\> (* Right pipe *) |
| 179 |
---- |
| 180 |
|
| 181 |
As you can see, the left and right piping operators, `>|` and `|<`, |
| 182 |
are the same as the right and left application operators, |
| 183 |
respectively, except the associativities are reversed and the binding |
| 184 |
strength is lower. They are useful for piping data through a sequence |
| 185 |
of operations. In general, |
| 186 |
|
| 187 |
---- |
| 188 |
x >| f1 >| ... >| fN = fN (... (f1 x) ...) = (fN o ... o f1) x |
| 189 |
---- |
| 190 |
|
| 191 |
and |
| 192 |
|
| 193 |
---- |
| 194 |
fN |< ... |< f1 |< x = fN (... (f1 x) ...) = (fN o ... o f1) x |
| 195 |
---- |
| 196 |
|
| 197 |
The right piping operator, `|<`, is provided by the Haskell prelude as |
| 198 |
`$`. It can be convenient in CPS or continuation passing style. |
| 199 |
|
| 200 |
A use for the left piping operator is with parsing combinators. In a |
| 201 |
strict language, like SML, eta-reduction is generally unsafe. Using |
| 202 |
the left piping operator, parsing functions can be formatted |
| 203 |
conveniently as |
| 204 |
|
| 205 |
[source,sml] |
| 206 |
---- |
| 207 |
fun parsingFunc input = |
| 208 |
input >| (* ... *) |
| 209 |
|| (* ... *) |
| 210 |
|| (* ... *) |
| 211 |
---- |
| 212 |
|
| 213 |
where `||` is supposed to be a combinator provided by the parsing combinator |
| 214 |
library. |
| 215 |
|
| 216 |
|
| 217 |
== About precedences |
| 218 |
|
| 219 |
You probably noticed that we redefined the |
| 220 |
<<OperatorPrecedence#,precedences>> of the function composition operator |
| 221 |
`o` and the assignment operator `:=`. Doing so is not strictly |
| 222 |
necessary, but can be convenient and should be relatively |
| 223 |
safe. Consider the following motivating examples from |
| 224 |
<<WesleyTerpstra#,Wesley W. Terpstra>> relying on the redefined |
| 225 |
precedences: |
| 226 |
|
| 227 |
[source,sml] |
| 228 |
---- |
| 229 |
Word8.fromInt o Char.ord o s <\String.sub |
| 230 |
(* Combining sectioning and composition *) |
| 231 |
|
| 232 |
x := s <\String.sub\> i |
| 233 |
(* Assigning the result of an infixed application *) |
| 234 |
---- |
| 235 |
|
| 236 |
In imperative languages, assignment usually has the lowest precedence |
| 237 |
(ignoring statement separators). The precedence of `:=` in the |
| 238 |
<<BasisLibrary#,Basis Library>> is perhaps unnecessarily high, because |
| 239 |
an expression of the form `r := x` always returns a unit, which makes |
| 240 |
little sense to combine with anything. Dropping `:=` to the lowest |
| 241 |
precedence level makes it behave more like in other imperative |
| 242 |
languages. |
| 243 |
|
| 244 |
The case for `o` is different. With the exception of `before` and |
| 245 |
`:=`, it doesn't seem to make much sense to use `o` with any of the |
| 246 |
operators defined by the <<BasisLibrary#,Basis Library>> in an |
| 247 |
unparenthesized expression. This is simply because none of the other |
| 248 |
operators deal with functions. It would seem that the precedence of |
| 249 |
`o` could be chosen completely arbitrarily from the set `{1, ..., 9}` |
| 250 |
without having any adverse effects with respect to other infix |
| 251 |
operators defined by the <<BasisLibrary#,Basis Library>>. |
| 252 |
|
| 253 |
|
| 254 |
== Design of the symbols |
| 255 |
|
| 256 |
The closest approximation of Haskell's `x {backtick}f{backtick} y` syntax |
| 257 |
achievable in Standard ML would probably be something like |
| 258 |
`x {backtick}f^ y`, but `^` is already used for string |
| 259 |
concatenation by the <<BasisLibrary#,Basis Library>>. Other |
| 260 |
combinations of the characters `{backtick}` and `^` would be |
| 261 |
possible, but none seems clearly the best visually. The symbols `<\`, |
| 262 |
`\>`, `</`, and `/>` are reasonably concise and have a certain |
| 263 |
self-documenting appearance and symmetry, which can help to remember |
| 264 |
them. As the names suggest, the symbols of the piping operators `>|` |
| 265 |
and `|<` are inspired by Unix shell pipelines. |
| 266 |
|
| 267 |
|
| 268 |
== Also see |
| 269 |
|
| 270 |
* <<Utilities#>> |