From: Goswin von Brederlow <goswin-v-b@web.de>
To: Diego Olivier Fernandez Pons <dofp.ocaml@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] How to simplify an arithmetic expression ?
Date: Wed, 05 Oct 2011 15:09:23 +0200 [thread overview]
Message-ID: <874nznr3ek.fsf@frosties.localnet> (raw)
In-Reply-To: <CAHqiZ-K9dVQL2QNuQEXHedqZgeWgPRTmt4XKmCUJW7eUtgpy1A@mail.gmail.com> (Diego Olivier Fernandez Pons's message of "Sun, 2 Oct 2011 13:51:13 +0200")
Diego Olivier Fernandez Pons <dofp.ocaml@gmail.com> writes:
> OCaml list,
>
> It's easy to encapsulate a couple of arithmetic simplifications into a function
> that applies them bottom up to an expression represented as a tree
>
> let rec simplify = function
> | Plus (e1, e2) ->
> match (simplify e1, simplify e2) with
> | (Constant 0, _) -> e2
>
> With a couple well known tricks like pushing constants to the left side and so
> on...
>
> How can I however guarantee that
> 1. My final expression reaches a kind of minimal normal form
First you have to define what you mean by that.
For a (good) compiler a minimal normal form would be one that uses the
least number of instructions (optimize size) or least number of cpu
cycles (optimize speed) to execute. And that is quite a complex problem.
One simplification that will probably destroy any idea of a simple
minimal normal form is probably eliminating common subexpressions.
Some optimizations will require complex heuristics or much larger
patterns.
> 2. My set of simplifications is optimal in the sense it doesn't traverse
> subtrees without need
That you have to think through. I don't think you can get the compiler
to tell you when a traversal isn't needed. You could annotate your
syntax tree with traversal counts and check if the count goes up too
much somewhere. If you do extra unneeded traversals then I expect them
to become exponential quite quickly so it should be easy to spot by
running a bunch of expressions through your code.
>
> Here is my current simplifier and I have no way of telling if it really
> simplifies the expressions as much as possible and if it does useless passes or
> not
It doesn't.
1 - 1 -> 0
a + b + 2 * (a + b) -> 3 * a + 3 * b or 3 * (a + b)?
(a + 1) * (a - 1) -> a * a - 1
9 * a -> 8 * a + a?
The last would be something a compiler does to optimize speed since a
muliplication by a power of 2 is a shift and shift + add is faster than
multiplication.
MfG
Goswin
prev parent reply other threads:[~2011-10-05 13:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-02 11:51 Diego Olivier Fernandez Pons
2011-10-02 14:08 ` Gabriel Scherer
2011-10-02 15:09 ` Ernesto Posse
2011-10-02 16:32 ` Xavier Leroy
2011-10-02 16:48 ` Gabriel Scherer
2011-10-02 17:07 ` Gabriel Scherer
2011-10-05 13:17 ` Goswin von Brederlow
2011-10-05 21:31 ` Gabriel Scherer
2011-10-02 16:56 ` Xavier Clerc
2011-10-05 13:09 ` Goswin von Brederlow [this message]
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=874nznr3ek.fsf@frosties.localnet \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
--cc=dofp.ocaml@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).