Version française
Home     About     Download     Resources     Contact us    
Browse thread
optimization of sequence of List.map and inlining
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] optimization of sequence of List.map and inlining
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 = <abstr>
# let b = Seq.map float a;;
val b : (int, float) Seq.t = <abstr>
# let c = Seq.map string_of_float b;;
val c : (int, string) Seq.t = <abstr>
# 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