Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Arnaud Spiwack <aspiwack@lix.polytechnique.fr>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Purity in ocaml
Date: Sat, 1 Feb 2014 15:52:30 +0100	[thread overview]
Message-ID: <20140201145230.GB1783@frosties> (raw)
In-Reply-To: <CAMoPVjcbiG=b2D+q2LV5LRjaV2c17VOOocFkOQ4w2Df+v-zoKg@mail.gmail.com>

On Tue, Jan 28, 2014 at 10:21:20AM +0100, Arnaud Spiwack wrote:
> > Do you mean something like this?
> >
> > let g = function 0 -> raise G | n -> n
> > let f = function 1 -> raise F | n -> n
> >
> > map f (map g [1;0]) ===> raise G
> > map (f %> g) [1;0]  ===> raise F
> >
> > It is true, the exception prevents any optimization that would reorder
> > two functions that both throw an excepition.
> 
> 
> Precisely
> 
> 
> > But many other
> > optimizations are still possible, e.g. common subexpression
> > elimination:
> >
> > let n =
> >   let x = f (g 1) in
> >   let y = g 1 in
> >   x + y
> >
> > ==>
> >
> > let n =
> >   let t = g 1 in
> >   let x = f t in
> >   let y = t in
> >   x + y
> >
> 
> To some degree:
> 
> let n =
>   let x = f (g 1) (h ()) in
>   let y = g 1 in
>   x+y
> 
> is a different program than
> 
> let n =
>   let t = g 1 in
>   let x = f t (h ()) in
>   let y = t in
>   x+y
> 
> As it reorders (g 1) and (h ()). I think the rule is something like: when
> the program is in monadic form, and you have a call to e, you can factor in
> every subsequent calls to e. Which I guess is good enough. But I would tend
> to believe deforestation would be tremendously more useful than common
> expression elimination.

Does it reorder? I would expect these two to be equivalent

    let x = f (g 1) (h ()) in

and

    let f2 = f (g 1) in
    let x = f (h ()) in

and in the second case the order is clearly g before h.

If the order is indeed to evaluate the last argument first then

    let t1 = h () in
    let t2 = g 1 in
    let x = f t2 t1 in
    let y = t2 in
    x + y

It's not hard to preserve the order whatever it may be.

MfG
	Goswin

  reply	other threads:[~2014-02-01 14:52 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-20 20:45 Yotam Barnoy
2014-01-20 21:08 ` Siraaj Khandkar
2014-01-20 21:16   ` Yotam Barnoy
2014-01-20 21:31     ` Siraaj Khandkar
2014-01-20 21:43       ` Yotam Barnoy
2014-01-20 22:55         ` Siraaj Khandkar
2014-01-21  1:37           ` Yotam Barnoy
2014-01-21  9:49 ` Goswin von Brederlow
2014-01-21 15:27   ` Yotam Barnoy
2014-01-23  9:20     ` Goswin von Brederlow
2014-01-23  9:35       ` Arnaud Spiwack
2014-01-27  9:32         ` Goswin von Brederlow
2014-01-28  9:21           ` Arnaud Spiwack
2014-02-01 14:52             ` Goswin von Brederlow [this message]
2014-02-03  9:20               ` Arnaud Spiwack
2014-01-23 18:18       ` Yotam Barnoy
2014-01-27  9:46         ` Goswin von Brederlow
2014-01-29 17:16           ` Yotam Barnoy
2014-02-01 15:03             ` Goswin von Brederlow

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=20140201145230.GB1783@frosties \
    --to=goswin-v-b@web.de \
    --cc=aspiwack@lix.polytechnique.fr \
    --cc=caml-list@inria.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