axiom-developer
[Top][All Lists]

## RE: [Axiom-developer] Lazy re-evaluation (was: More AxiomUI)

 From: Page, Bill Subject: RE: [Axiom-developer] Lazy re-evaluation (was: More AxiomUI) Date: Thu, 23 Jun 2005 14:20:12 -0400

```On Thursday, June 23, 2005 7:29 AM Ralf Hemmecke wrote:

> Axiom also allows to do something like that...
> What should the line (6) f(n) return after a user modified
> line (5) to n:=3?

(6)  6
Type: PositiveInteger

The way to know for sure that this is correct is just to start
a new Axiom session, or use ')clear completely' and repeat steps
(1) to (4), the revised (5), and then (6).

> Will f(n) return 6 or 7? And what did the user expect?

Since you are using a free (i.e. global) variable in your
function definition and the execution of the function involves
hidden side-effects, this a little more complicated. See especially
"Caching Previously Computed Results" page 249 and 6.16
"Free and Local Variables" in the Axiom book.

But in general it is possible to replace an imperative programming
style construct involving side-effects like this with something
more "functional", making the hidden side-effects explicit.
So that your line (2) could re-written as:

f(k,n)==[k+1,n+k+1]

and the Axiom session looks like this:

(7) -> )clear completely
All user variables and function definitions have been cleared.
All )browse facility databases have been cleared.
Internally cached functions and constructors have been cleared.
)clear completely is finished.
(1) -> k:=1

(1)  1
Type:
PositiveInteger
(2) -> f(k,n)==[k+1,n+k+1]
Type:
Void
(3) -> n:=2

(3)  2
Type:
PositiveInteger
(4) -> [k,x]:=f(k,n)
Compiling function f with type (PositiveInteger,PositiveInteger) ->
List PositiveInteger

(4)  [2,4]
Type: List
PositiveInteger
(5) -> n:=2

(5)  2
Type:
PositiveInteger
(6) -> [k,x]:=f(k,n)

(6)  [3,5]
Type: List
PositiveInteger
(7) ->

Now I think that it is more obvious (at least to a program that
is trying to do lazy re-evaluation!) that changing  line (5)
to n:=3 will return the result 6. In fact calculation only
needs to be rolled-back to the state at line (4) and then (5)
and (6) repeated:

(4') -> [k,x]:=%%(4)

(4')  [2,4]
Type: List PositiveInteger
(5') -> n:=3

(5')  3
Type: PositiveInteger
(6') -> [k,x]:=f(k,n)

(6')  [3,6]
Type: List PositiveInteger

-------

where the primes ' mean that these expressions replace the
output on the page of the same numbered expression unprimed.

Regards,
Bill Page.

> Ralf
>
> -------
>
> (1) -> k:=1
>
>    (1)  1
>                                      Type: PositiveInteger
> (2) -> f(n)==(free k; k:=k+1; n+k);
>
> Type: Void
> (3) -> n:=2
>
>     (3)  2
>                                      Type: PositiveInteger
> (4) -> f(n)
>    Compiling function f with type PositiveInteger -> PositiveInteger
>
>     (4)  4
>                                       Type: PositiveInteger
> (5) -> n:=2
>
>    (5)  2
>                                       Type: PositiveInteger
> (6) -> f(n)
>
>    (6)  5
>                                       Type: PositiveInteger

```