From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id DC69DBB84 for ; Tue, 17 Jun 2008 16:40:16 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AugEAKBpV0jUnw6DfGdsb2JhbACCOY9uAQELBQIECREDnBc X-IronPort-AV: E=Sophos;i="4.27,658,1204498800"; d="scan'208";a="12175780" Received: from pih-relay04.plus.net ([212.159.14.131]) by mail2-smtp-roc.national.inria.fr with ESMTP; 17 Jun 2008 16:40:16 +0200 Received: from [80.229.56.224] (helo=beast.local) by pih-relay04.plus.net with esmtp (Exim) id 1K8cM3-0007Rl-EH for caml-list@yquem.inria.fr; Tue, 17 Jun 2008 15:40:15 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] optimization of sequence of List.map and inlining Date: Tue, 17 Jun 2008 15:36:31 +0100 User-Agent: KMail/1.9.9 References: <676aba050806101201x526f03b1lf1fdbed665ee2e3f@mail.gmail.com> In-Reply-To: <676aba050806101201x526f03b1lf1fdbed665ee2e3f@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200806171536.31220.jon@ffconsultancy.com> X-Plusnet-Relay: d102069457dcd7e00aea67125fb623a5 X-Spam: no; 0.00; inlining:01 mls:01 inlining:01 seq:01 sig:01 val:01 val:01 struct:01 seq:01 abstr:01 abstr:01 compiler:01 showstopper:01 uglier:01 camlp:01 On Tuesday 10 June 2008 20:01:12 Charles Hymans wrote: > Thank you for your answer. > >> In MLs, you deforest by hand. You might like to use a function >> composition > > Unfortunately, in the case from which my example is extracted, I > believe I can't do that: > The several List.maps, even though they are executed in sequence, are > not in the same module. In order to deforest by hand, I have to break > the module interface, do some function inlining and then do the > function composition. > Something I don't want to do, because it would make the code difficult > to read and to maintain. Perhaps you could use something like the following abstract lazy sequence implementation: module Seq : sig type ('a, 'b) t val of_list : 'a list -> ('a, 'a) t val map : ('a -> 'b) -> ('c, 'a) t -> ('c, 'b) t val to_list : ('a, 'b) t -> 'b list end = struct type ('a, 'b) t = ('a -> 'b) * 'a list let of_list list = (fun x -> x), list let map f (g, list) = (fun x -> f(g x)), list let to_list (f, list) = List.map f list end Where your existing implementation exposes an 'a list you now expose an ('b, 'a) Seq.t instead. # let a = Seq.of_list [1];; val a : (int, int) Seq.t = # let b = Seq.map float a;; val b : (int, float) Seq.t = # let c = Seq.map string_of_float b;; val c : (int, string) Seq.t = # Seq.to_list c;; - : string list = ["1."] > That's why I wondered if there was a way to tell the compiler to do it > for me. Or maybe some incantation with ocamlp4? I would not recommend pursuing that line of thought under any circumstances. Even if this problem is a critical showstopper for you, I would always recommend uglier code over camlp4 hacks. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e