caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Charles Hymans <charles.hymans@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] optimization of sequence of List.map and inlining
Date: Tue, 10 Jun 2008 19:07:47 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0806101901080.21480@localhost> (raw)
In-Reply-To: <676aba050806101201x526f03b1lf1fdbed665ee2e3f@mail.gmail.com>



On Tue, 10 Jun 2008, Charles Hymans wrote:

> Let's say, I have the following code:
>
>  let f l = List.map succ l
>
>  ....
>
>  let l = f l in
>  let l = List.map succ l in
>    do_something_with l
>
>
> Is there a way to tell the compiler to optimize it so that it runs as fast
> as this code:
>  let l = List.map (fun x -> succ (succ x)) l in
>    l
> In the first case, there are two passes where succ is applied to 
> each elements of the list. In the second case, there is only one pass 
> that applies succ twice to each element of the list.
>

The short answer is no- due to the possibility of side effects, it's hard 
(read: in the general case equivelent to the halting problem) to determine 
if that code transformation produces the same result.  Even ignoring 
exceptions and i/o!  As an example of what I mean, consider the code:

let r : ref 1;;

let foo x = r := !r + x; !r;;

let bar x = foo (foo x);;

So now, if we set r to 1, and call List.map foo (List.map foo [1;2;3]), we 
get the return list [9; 13; 20], while List.map bar [1;2;3] returns [4; 
12; 30].  So it's not always the case that you can replace List.map f 
(List.map g lst) with List.map (f . g) lst, and get the same result back.

Brian


  parent reply	other threads:[~2008-06-10 22:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-10 19:01 Charles Hymans
2008-06-10 19:21 ` [Caml-list] " Jon Harrop
2008-06-10 20:55   ` Richard Jones
2008-06-10 22:10 ` Peng Zang
2008-06-10 23:07 ` Brian Hurt [this message]
2008-06-17 14:36 ` Jon Harrop

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=Pine.LNX.4.64.0806101901080.21480@localhost \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=charles.hymans@gmail.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).