caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitri Boulytchev <db@tepkom.ru>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] type abbreviation is cyclic
Date: Thu, 25 Oct 2007 01:34:19 +0000	[thread overview]
Message-ID: <471FF29B.8010409@tepkom.ru> (raw)
In-Reply-To: <200710242140.21143.jon@ffconsultancy.com>

    We intensively use -rectypes option. The "pattern" we've found to be
handy is like that:

    type 'a p =
       | Add    of 'a * ' a
       | Sub     of 'a * 'a
       | Neg    of 'a
       | Const of int
    and t = t p (* we need rectypes here *)

    let map f = function
       | Add (x, y) -> Add (f x, f y)
       | Sub  (x, y) -> Sub  (f x, f y)
       | Neg x        -> Neg (f x)
       | Const x     -> Const x

    let rec toString x =
       match map toString x with
       | Add (a, b) -> a ^ " + " ^ b
       | Sub  (a, b) -> a ^ " - " ^ b
       | Neg  a      ->  "-" ^ a
       | Const x    -> string_of_int x

    let rec eval x =
       match map eval x with
       | Add (a, b) -> a + b
       | Sub  (a, b) -> a - b
       | Neg  a      -> -a
       | Const x    -> x

    etc.

    Although this may look useless at a first glance we have some profit
since we bother about
correct applications of function only when writing map. In this very
example it may not be obvious
but when the number of "phantom" parameters grows (type ('expr, 'stmt,
'process, 'handler) something = ...)
the code becomes much more transparent.

    We use -rectypes together with recursive modules in a rather large
project (>30 000 loc) and
all works well.

    Best regards,
    Dmitry Boulytchev,
    St.Petersburg State University.
   

>On Wednesday 24 October 2007 19:09:13 Till Varoquaux wrote:
>  
>
>>...
>>AFAIK -rectypes doesn't break any of the programs working before but
>>if you attend to use it you will probably run in problems with type
>>inference (not polymorphic enough). I would recommend steering clear
>>of it for anything else than toy programs.
>>    
>>
>
>You used to be able to get the OCaml compiler to segfault by using -rectypes 
>inconsistently while compiling. I believe that -rectypes can be useful. For 
>example, the OCaml implementations of the ray tracer benchmark use it to good 
>effect to implement the scene tree without an extra level of indirection. I 
>have never bothered turning it on for any significant projects or during 
>development though, so I can't say with any certainty whether or not it has a 
>practical impact on error messages.
>
>  
>


  reply	other threads:[~2007-10-24 21:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-24 17:27 William W Smith
2007-10-24 17:40 ` [Caml-list] " Till Varoquaux
2007-10-24 22:01   ` Dmitri Boulytchev
2007-10-24 17:44 ` Brian Hurt
2007-10-24 18:09   ` Till Varoquaux
2007-10-24 20:40     ` Jon Harrop
2007-10-25  1:34       ` Dmitri Boulytchev [this message]
2007-10-24 21:36 ` Dmitri Boulytchev
2007-10-24 21:38 ` Dmitri Boulytchev
2007-10-25 11:22 William Smith

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=471FF29B.8010409@tepkom.ru \
    --to=db@tepkom.ru \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.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).