❮❮❮ ❮❮❮   ❯❯❯ ❯❯❯
Identity wrappers in ML
Side effects in functional programming.

Tags: F# SML
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:

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.

Fazit

Function signatures in impure languages are sometimes misleading. Hidden side-effects can cause nasty surprises.