That's called common subexpression elimination (CSE) and is a common feature
of compilers. It's a time/space tradeoff - in the general case it may take a
great deal of space to store the intermediate solutions. So by not making
this tradeoff, the compiler leaves it in the developer's hands. I think most
of the time it would be a good tradeoff to make, but you have to be
consistent about it lest you surprise the developer.

On Mon, Jun 27, 2011 at 12:15 PM, Jean-Marc Vanel
<[email protected]>wrote:

> Hi
>
> I'm new to Oz, coming from Java and other OO languages, and also Prolog.
>
>
> In page 9 in paragraph "1.5 Functions over lists" in book "Concepts,
> Techniques, and Models of Computer Programming,
> by PETER VAN ROY and SEIF HARIDI , there is this nice example:
>
> declare
> fun {Pascal N}
>    if N==1 then [1]
>    else
>       {AddList
>        {ShiftLeft {  Pascal N-1}}
>        {ShiftRight {Pascal N-1}}}
>    end
> end
>
> Later, in paragraph "1.7 Complexity" the authors point out that the
> complexity of this is exponential, and propose a linear complexity variant :
>
> fun {FastPascal N}
> if N==1 then [1]
> else L in
>   L={FastPascal N-1}
>   {AddList {ShiftLeft L} {ShiftRight L}}
> end
> end
>
> Now I come to my question. Since the compiler can know that the function
> Pascal is declarative and without side-effects, why doesn't it do one of
> these :
>
> - 1. warn about the duplicated call {Pascal N-1}
> - 2. actually inline the duplicated call in the generate binary output
>
> I would prefer 1. , to encourage a clean code .
>
> I tried to run :
> ozc --verbose pascal_triangle.oz
> but no warnings were produced .
>
> --
> Jean-Marc Vanel
> Déductions SARL - Consulting, services, training,
> Rule-based programming, Semantic Web
> http://jmvanel.free.fr/ - EulerGUI, a turntable GUI for Semantic Web +
> rules, XML, UML, eCore, Java bytecode
> +33 (0)6 89 16 29 52 -- +33 (0)1 39 55 58 16
>
>
> _________________________________________________________________________________
> mozart-users mailing list
> [email protected]
> http://www.mozart-oz.org/mailman/listinfo/mozart-users
>
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to