6.6.3 Eager definitions

Prev Up Next Page 81 of 800 Search internet

To make your append function faster, you can change 'value define' to 'eager define' in Line 11:

   ""P append
   ""R base
   ""D 17
   " ++ "
   page ( ""N , ""C )
   title "Append"
   bib "@MISC{dummy}"
   main text "
   \item "[[ eager define x ++ y as
             if x atom then y else x head :: x tail ++ y end define ]]"
   \item "[[ etst << 1 ,, 2 >> ++ << 3 >> ; << 1 ,, 2 ,, 3 >> end test ]]"
   appendix "
   \item This space intentionally left blank
   end page

In general, Logiweb is a lazy programming language. Thus, the arguments of a function are evaluated first time they are needed. And not at all if they are not needed.

You may, however, ask for eager evaluation explicitly on a function by function basis. For eager functions, arguments are computed one by one in the order they appear (left to right if the source text runs left to right) before the body of the function is evaluated. If an argument throws an exception, evaluation stops and the exception is returned as the result of the computation.

Eager definitions may only refer to other functions which are also eager, such as the pair construct x :: y and many other functions defined on the base page. If an eager definition refers to a non-eager function anyway you get a warning and the definition will be 'unfit for optimization'. That is legal but not very useful.

In addition to other eager functions, however, eager definitions may also refer to a few other constructs:

   if x then y else z
   x .and. y
   x .or. y

The constructs above do not mindlessly evaluate all their arguments.

   x catch
   x catching maptag

The constructs above do not throw an exception even if x throws an exception.

   map ( x )

The construct above does not evaluate x at all. Rather, it constructs a 'tagged map' which contains x unevaluated. One can test if a data structure is a tagged map using mapp, one can get the value of x using untag, one can compute x partially using root, and one can do various other things as explained elsewhere.

The right hand side of eager definitions are only allowed to contain constructs which are 'fit for optimization' such as other eager functions and the constructs mentioned above. Inside map ( x ) one can include arbitrary functions, however, so the map construct allows to embed lazy code inside eager code. On the other hand, free variables are not permitted inside map ( x ): If x contains free variables, you get a warning that it is not fit for optimization.

Prev Up Next Page 81 of 800 Search logiweb.eu

Copyright © 2010 Klaus Grue, GRD-2010-01-05