❮❮❮
Interspersing elements into a sequence
❮❮❮
❯❯❯
Data
❯❯❯
Identity wrappers in ML
Side effects in functional programming.
Reading time: 1 min.
Setup
Consider some function
val f : 'a1 -> 'a2 -> 'r
It is somewhere (partially) applied as in
f a1
Wrapping
Consider a function wrapper type
val w : ('a -> 'b) -> ('a -> 'b)
There are two typechecking ways to wrap the above application of f:
(w f) a1w (f a1)
Both typecheck to 'a2 -> 'r.
In a pure language the only possible implementation would be the identity. With w := id, both wraps would give the same result.
In an impure strict language with exceptions (like SML), the application order matters. A standard wrapper would be to catch and transform some exception.
- In
(w f) a1any exception thrown byfgoes throughws logic. - In
w (f a1)exceptions in the first partial application(f a1)bypassws logic.
Fazit
Function signatures in impure languages are sometimes misleading. Hidden side-effects can cause nasty surprises.