This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

type abbreviation is cyclic
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-10-24 (21:33) From: Dmitri Boulytchev Subject: Re: [Caml-list] type abbreviation is cyclic
```    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
| 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
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.
>
>
>

```