caml-list - the Caml user's mailing list
 help / Atom feed
From: Oleg <>
Subject: [Caml-list] Implicits for the masses
Date: Thu, 5 Sep 2019 00:25:17 +0900
Message-ID: <20190904152517.GA2014@Melchior.localnet> (raw)

This is to announce a simple, plain OCaml library to experiment with
type-class/implicits resolution, which can be thought of as evaluating
a Prolog-like program. One may allow `overlapping instances' -- or
prohibit them, insisting on uniqueness. One may make the search fully
deterministic, fully non-deterministic, or something in-between.
There is an immediate, albeit modest practical benefit: the facility
like "#install_printer", which was restricted to top-level, is now
available for all -- as a small, self-contained, plain OCaml library
with no knowledge or dependencies on the compiler internals. We show
an example at the end of the message.

This message has been inspired by the remarkable paper
        Canonical Structures for the working Coq user
        Assia Mahboubi, Enrico Tassi
        DOI: 10.1007/978-3-642-39634-2_5
Its introduction is particularly insightful: the power of
(mathematical) notation is in eliding distracting details. Yet to
formally check a proof, or to run a program, the omitted has to be
found. When pressed to fill in details, people `skillful in the art'
look in the database of the `state of the art', with the context as
the key. Computers can be programmed similarly; types well represent
the needed context to guide the search.

Mahboubi and Tassi's paper explains very well how this eliding and
filling-in is realized, as a programmable unification, and used in
Coq. Yet their insights go beyond Coq and deserve to be known better.
This message and the accompanying code is to explain them in
plain OCaml and encourage experimentation. It could have been titled
`Canonical Structures for the working OCaml (meta) programmer'.

The rudiment of canonical structures is already present in OCaml, in
the form of the registry of printers for user-defined types. This
facility is available only at the top-level, however, and deeply
intertwined with it. As a modest practical benefit, this facility is
now available for all programs, as a plain, small, self-contained
library, with no compiler or other magic. The full potential of the
method is realized however in (multi-) staged programming. In fact, I'm
planning to use it in the upcoming version of MetaOCaml to implement
`lifting' from a value to the code that produces it -- letting the
users register lifting functions for their own data types.
        The implementation and the examples, some of which are noted below.
        A generic library of type representations: something like 
        Typeable in Haskell. Some day it may be built-in into the compiler
        A well-commented code that records the progressive development of It is not part of the library, but may serve as
        its explanation.

Here are a few examples, starting with the most trivial one
   module Show = MakeResolve(struct type 'a dict = 'a -> string end)
   let () = Show.register Int string_of_int    (* Define `instances' *)
   let () = Show.register Bool string_of_bool
   Show.find Int 1;;

However contrived and flawed, it is instructive. Here (Int : int trep)
is the value representing the type int. The type checker can certainly
figure out that 1 is of the type int, and could potentially save us
trouble writing Int explicitly. What the type checker cannot do by
itself is to find out which function to use to convert an int to a
string. After all, there are many of them. Show.register lets us
register the *canonical* int->string function. Show.find is to search
the database of such canonical functions: in effect, finding *the*
evidence that the type int->string is populated. Keeping Curry-Howard
in mind, Show.find does a _proof search_.

The type of Show.find is 'a trep -> ('a -> string). Compare with
Haskell's show : Show a => a -> String (or, desuraging => and Show)
show : ('a -> string) -> ('a -> string).  Haskell's show indeed does
not actually do anything: it is the identity function. All the hard
work -- finding out the right dictionary (the string producing
function) -- is done by the compiler. If one does not like the way the
compiler goes about it -- tough luck. There is little one may do save
complaining on reddit. In contrast, the first argument of Show.find is
trivial: it is a mere reflection of the type int, with no further
information. Hence Show.find has to do a non-trivial work.  In the
case of int, this work is the straightforward database search --
or, if you prefer, running the query ?- dict(int,R) against a logic
The program becomes more interesting when it comes to pairs:
     dict(T,R) :- T = pair(X,Y), !,  
         dict(X,DX), dict(Y,DY), R=make_pair_dict(DX,DY).
Here is how it is expressed in OCaml:
let () = 
  let open Show in
  let pat : type b. b trep -> b rule_body option = function
    | Pair (x,y) -> 
        Some (Arg (x, fun dx -> Arg (y, fun dy -> 
          Fact (fun (x,y) -> "(" ^ dx x ^ "," ^ dy y ^ ")"))))
    | _      -> None
  in register_rule {pat}

let () = Show.register (Pair(Bool,Bool)) 
           (fun (x,y) -> string_of_bool x ^ string_of_bool y)

Our library permits `overlapping instances'. We hence registered the
printer for generic pairs, and a particular printer just for pairs of

The library is extensible with user-defined data types, for example:
   type my_fancy_datatype = Fancy of int * string * (int -> string)

After registering the type with trep library, and the printer
   type _ trep += MyFancy : my_fancy_datatype trep
   let () = Show.register MyFancy (function Fancy(x,y,_) ->
     string_of_int x ^ "/" ^ y ^ "/" ^ "<fun>")

one can print rather complex data with fancy, with no further ado:
   Show.find (List(List(Pair(MyFancy,Int)))) [[(Fancy ...,5)];[]]

As Mahboubi and Tassi would say, proof synthesis at work!

We should stress that what we have described is not a type-class
facility for OCaml. It is *meta* type-class facility.  Show.find has
many drawbacks: we have to explicitly pass the trep argument like
Int. The resolution happens at run time, and hence the failure of the
resolution is a run-time exception. But the canonical instance
resolution was intended to be a part of a type checker. There, the
resolution failure is a type checking error. The trep argument,
representing the type in the object program, is also at
hand. Likewise, the drawbacks of Show.find disappear when we use the
library in a meta-program (code generator). The library then becomes a
type-class/implicits facility, for the generated code -- the facility,
we can easily (re)program.

             reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-04 15:23 Oleg [this message]
2019-09-04 20:40 ` Ivan Gotovchits
2019-09-10 14:38   ` [Caml-list] Type-indexed heterogeneous collections (Was: Implicits for the masses) Oleg
2019-09-10 19:01     ` Ivan Gotovchits

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190904152517.GA2014@Melchior.localnet \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror

AGPL code for this site: git clone public-inbox