7.7.9 Eager programming

Prev Up Next Page 205 of 800 Search internet

Recall the basic constructs which allows to express all maps (i.e. all raw maps):

array ( left ) \ x . y \\ x ' y \\ true \\ If true then u else v end array

Programming from the four basic constructs is possible, but does not lead to very efficient programs. If one references the base page then one gets acces to e.g. falsehood defined thus:

optimized define value of false as true LazyPair true end define

Once false is recognized as a built in construct, the Logiweb abstract machine does not really use the definition of falsehood given above but, rather, uses a built in version which typically leads to programs running much faster.

To define a new function which also runs fast, use the eager definition construct eager define x as y end define like in the following definition of append:

eager define append ( x , y ) as if x atom then y else x head :: append ( x tail , y ) end define

The eager definition construct eager define x as y end define is macro defined on the base page; it expands into an ordinary value definition which, however, lgwam can recognize as something it can optimize heavily.

In eager definitions you can use most of the built in constructs. Furthermore, you can use other functions which are defined by the eager definition construct. If in doubt if a construct can be used, just try it. The lgc compiler while whine if it sees something that looks like an eager definition but which the lgc compiler nevertheless finds to be unfit for optimization.

When the lgc compiler sees something that looks like an eager definition, it checks whether or not the definition is fit for optimization. It does so used fitness analysis which is similar to more traditional strictness analysis.

Stating a definition which looks eager but which is unfit for optimization is no error, so the lgc compiler just issues a probably mistakenly unfit warning to tell you that lgc thinks you might have done something you did not intend. But when this happens, lgc just compiles the definition without optimizations and continues since you might after all know what you are doing.

All functions defined using eager define x as y end define are eager left-relaying in that they compute their arguments left to right and relay exceptions. If you need to define a construct which is not eager left-relaying but which you still want to be fast, chances are you should macro define your construct rather than giving a value definition.

Prev Up Next Page 205 of 800 Search logiweb.eu

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