caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Chambart <pierre.chambart@ocamlpro.com>
To: Kenneth Adam Miller <kennethadammiller@gmail.com>,
	 caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] <DKIM> Type Encoding Format Control
Date: Tue, 25 Aug 2015 14:09:10 +0200	[thread overview]
Message-ID: <55DC5AE6.9010902@ocamlpro.com> (raw)
In-Reply-To: <CAK7rcp_DfDdW94HWhuPsnKE-eg3ncvLTDfk7vcg9R+toKMkZhg@mail.gmail.com>

On 18/08/2015 19:06, Kenneth Adam Miller wrote:
> I was wondering if cases where format control is possible in typing
> constructs can allow things like restricting the implementation size
> after compilation of a specific variant type. Say, for instance that I
> wanted to have a malloc implementation instead of returning a Some 'a
> | None type that compiles down to a boxed case of first a word and
> then the subsequent 'a instance, down to the 'a instance, where in the
> values of the word enum (or tag) are not present in the possibilities
> of the 'a instance.
>
> Maybe it sounds silly, but in really tight loops you want to squeeze
> for efficiency. So I was wondering if maybe the same actual code be
> used with the same sanity of type checking, but some annotation
> provided at the type declaration to allow such optimization to take place.
Note that there are some ways to avoid allocating options in tight loops
using well chosen GADT and CPS style. With CPS style, it is possible to
'return' multiple values (as they are encoded as arguments to the
continuation) and GADT's it is possible to encode the tag of a value
outside of it, i.e. not needing to box to have different cases.

This is kind of a heavy style, but if it is only for the tight loop, it
may not matter.

For instance, encoding a while loop accumulating on an option can look like:

let f x =
  let r = ref None in
  while 'condition' do
    do_something ...
    if other_condition then
      r := Some (something)
    else
      r := None
  done

Transformed to:

type some_type = ... (the thing that will end up in the option)

type _ arg =
  | None_arg : unit arg
  | Some_arg : some_type arg

let f x =
  let rec loop : type t. t arg -> t -> unit = fun arg_kind r ->
    if condition then ()
    else begin
      do_something ...
      if other_condition then
        loop Some_arg (something)
      else
        loop None_arg ()
   in
   loop None_arg ()

Happy encoding !
-- 
Pierre

      parent reply	other threads:[~2015-08-25 12:09 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-18 17:06 [Caml-list] " Kenneth Adam Miller
2015-08-18 18:57 ` Hendrik Boom
2015-08-18 19:01   ` Kenneth Adam Miller
2015-08-18 19:44     ` Gabriel Scherer
2015-08-18 19:55       ` Kenneth Adam Miller
2015-08-18 19:58         ` Gabriel Scherer
2015-08-20  9:10       ` Goswin von Brederlow
2015-08-20 13:08         ` Kenneth Adam Miller
2015-08-20 14:05           ` David Allsopp
2015-08-20 14:09             ` Kenneth Adam Miller
2015-08-20 14:11               ` Kenneth Adam Miller
2015-08-25 12:09 ` Pierre Chambart [this message]

Reply instructions:

You may reply publicly 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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=55DC5AE6.9010902@ocamlpro.com \
    --to=pierre.chambart@ocamlpro.com \
    --cc=caml-list@inria.fr \
    --cc=kennethadammiller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).