[MLton] Extending the SML Basis library (Library project)

Stephen Weeks sweeks at sweeks.com
Tue Oct 10 12:47:59 PDT 2006

> > type ('a, 'b) iso = ('a -> 'b) * ('b -> 'a)
> > type ('a, 'b) emb = ('a -> 'b) * ('b -> 'a option)
> Honestly, I'm not a huge fan of the iso/emb types and functions.  But, as 
> a minor suggestion, I would revise the types to:
>   type ('a, 'b) iso = {to: 'a -> 'b, from: 'b -> 'a}
>   type ('a, 'b) emb = {to: 'a -> 'b, from: 'b -> 'a option}
> Then I can write:
>    #to Char.int

Those who know my SML coding style will be unsurprised to hear my
opinion on the above.  If a type is worth naming, it's probably worth
putting in its own structure and putting all the functions that
operate on the type in that structure.  So, I'd recommend

  structure Iso:> ISO

Here, for example, is the ISO signature that I quickly hacked up for
use in my recent stuff on type-indexed values.

  signature ISO = sig
     type ('a, 'b) t
     val arrow: ('a1, 'a2) t * ('b1, 'b2) t -> ('a1 -> 'b1, 'a2 -> 'b2) t
     val compose: ('a, 'b) t * ('b, 'c) t -> ('a, 'c) t
     val flip: ('a, 'b) t -> ('b, 'a) t
     val id: ('a, 'a) t
     val iso: ('a1, 'a2) t * ('b1, 'b2) t -> (('a1, 'b1) t, ('a2, 'b2) t) t
     val inject: ('a, 'b) t * 'a -> 'b
     val make: ('a -> 'b) * ('b -> 'a) -> ('a, 'b) t
     val project: ('a, 'b) t * 'b -> 'a
     val tuple2: ('a1, 'b1) t * ('a2, 'b2) t -> ('a1 * 'a2, 'b1 * 'b2) t

I do like Matthew's names better, so I'd replace inject and project in
the above with

  val from: ('a, 'b) t * 'b -> 'a
  val to: ('a, 'b) t * 'a -> 'b

If one is in a situation where one is using isomorphisms a lot, then
one could even bind from/to at the toplevel, and use nice syntax like

  from (Char.int, i)
  to (Char.int, c)

More information about the MLton mailing list