caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
To: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
Cc: sejourne_kevin <sejourne_kevin@yahoo.fr>, caml-list@inria.fr
Subject: [Caml-list] About Obj (was Recursive lists)
Date: Mon, 11 Oct 2004 15:38:34 +0200	[thread overview]
Message-ID: <416A8CDA.7060407@univ-savoie.fr> (raw)
In-Reply-To: <16746.15832.409677.764564@gargle.gargle.HOWL>

[-- Attachment #1: Type: text/plain, Size: 2865 bytes --]

Jean-Christophe Filliatre wrote:
> sejourne_kevin wrote:
> 
>>(** Take a list and connect the end on the beginning
>>   Copyright : Kévin ;)
>>*)
>>let cycle l =
>>  let rl= ref l in
>>  let rec go_fin = function
>>      [] -> invalid_arg "cycle:[] can't be !"
>>    | [x] as f -> Obj.set_field (Obj.repr f) 1 (Obj.repr !rl);l
>>    | x::reste-> go_fin reste
>>  in go_fin l
>>;;
>>I haven't test GC issu.
> 
> 
> This shouldn't be advised, and not even posted on this list.
> 

And how do you write a tail recursive map doing only one structure 
traversal (which is important with the penalty for memory access) on 
immutable list without the Obj module ?

However, using Obj you can write once for all some functions to 
construct a list starting with its head, for instance with the following 
interface (you can write an implmentation with Stack but the "extract" 
function will not be in constant time):

type 'a prelist (* mutable type of a list under construction *)

val start : unit -> 'a prelist
val extract : 'a prelist -> 'a list
val cons : 'a -> 'a prelist -> unit

Then, map can be rewritten

let map f l =
   let pl = start () in
   let rec fn = function
     [] -> extract pl
   | a::l -> cons (f a) pl; fn l
   in
   fn l

This kind of code is not that intolerable and you can advise people to 
use Obj module to write an implementation of the above signature (if 
they are sure they really need it).
Since use of the Obj module is restricted to a small module, it will be 
easy to adapt to follow the evolution of the language.

The presence of the Obj module in the standard distribution itself tells 
that it is necessary and not only for C interface (this is not its main 
usage anyway, its main usage is to compile code typable only with 
dependent type).

Here is a possible implementation of the above module (I think it could 
be use to improve the actual implementation of map and fold_right in the 
standard library)

type 'a prelist = { mutable start : 'a list; mutable current : 'a list}

let start () = { start = []; current = []}

let cons a pl = match pl.current with
   [] -> let l = [a] in pl.current <- l; pl.start <- l
| l ->
	let l' = [a] in Obj.set_field (Obj.repr l) 1 (Obj.repr l');
         pl.current <- l'

let extract pl =
   let r = pl.start in
   pl.current <- []; pl.start <- [];
   (* to guaranty that we can not mute the list once it has been
   extracted *)
   r

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature
---------------------------------------------

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

  parent reply	other threads:[~2004-10-11 13:37 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-08 13:20 [Caml-list] Recursive lists Luca Pascali
2004-10-08 13:31 ` Keith Wansbrough
2004-10-08 14:32   ` skaller
2004-10-08 14:42   ` Alex Baretta
2004-10-08 15:43     ` David Brown
2004-10-08 17:19       ` Alex Baretta
2004-10-08 23:29         ` skaller
2004-10-09  8:35           ` Keith Wansbrough
2004-10-09  9:07             ` skaller
2004-10-09  8:32         ` Keith Wansbrough
2004-10-08 17:18     ` Wolfgang Lux
2004-10-11  0:44   ` Brian Hurt
2004-10-11  6:32     ` William Lovas
2004-10-11  6:52       ` Ville-Pertti Keinonen
2004-10-13 11:29         ` Alex Baretta
2004-10-13 11:22       ` Alex Baretta
2004-10-11  9:04     ` Keith Wansbrough
2004-10-08 14:05 ` Sébastien Furic
2004-10-08 14:44   ` Alex Baretta
2004-10-08 15:09     ` Jon Harrop
2004-10-08 15:13   ` james woodyatt
2004-10-08 14:26 ` sejourne_kevin
2004-10-08 18:28   ` Alex Baretta
2004-10-11  8:01     ` Jean-Christophe Filliatre
2004-10-11  9:20       ` Diego Olivier Fernandez Pons
2004-10-11 13:38       ` Christophe Raffalli [this message]
2004-10-11 13:49         ` [Caml-list] Re: About Obj (was Recursive lists) Christophe Raffalli
2004-10-11 15:33         ` [Caml-list] " Jon Harrop
2004-10-11 16:09           ` Richard Jones
2004-10-11 16:40           ` [Caml-list] About Obj Yamagata Yoriyuki
2004-10-13 11:59             ` Alex Baretta
2004-10-11 16:24         ` [Caml-list] About Obj (was Recursive lists) james woodyatt
2004-10-11 16:46           ` brogoff
2004-10-11 17:24             ` james woodyatt
2004-10-12  0:19               ` skaller
2004-10-20 22:10             ` Greg K
2004-10-12 15:19           ` Christophe Raffalli
2004-10-13 11:42           ` Alex Baretta
2004-10-13 21:19             ` brogoff
2004-10-14  9:52               ` Andreas Rossberg
2004-10-14 17:38                 ` brogoff
2004-10-15  8:22               ` Alex Baretta
2004-10-15 17:02                 ` brogoff
2004-10-17 13:42                   ` Alex Baretta
2004-10-12  6:17       ` [Caml-list] Recursive lists sejourne_kevin

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=416A8CDA.7060407@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --cc=Jean-Christophe.Filliatre@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=sejourne_kevin@yahoo.fr \
    /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).