Bind [0019]
The basic rule for bind (aka sequencing) in the \mathsf {Cmd} monad is as follows:
Note that this rule corresponds to a particular evaluation strategy, namely, evaluating c_1 and c_2 lazily, at the time c_1;\; c_2 is executed. If we instead wanted to evaluate c_1 and c_2 eagerly, we would need a different rule:
def f : Int -> Cmd Unit = ... end def g : Dir -> Cmd Unit = ... end def example = f 3; g north endWhen are
f 3 and g north evaluated? Under the first rule above, they would be re-evaluated every time example is executed. Under the second rule, they must be evaluated eagerly, when the definition of example is evaluated.As another example, consider the standard definition
def doN : Int -> Cmd a -> Cmd Unit = \n. \c.
if (n == 0) {} {c ; doN (n-1) c}
end
Under a lazy evaluation semantics for sequencing, a call to doN 10 move expands lazily, expanding one step each time the next move is executed. Under an eager semantics, a call to doN 10 move expands immediately into a fully unrolled sequence of 10 move instructions. There are certainly scenarios where we would want this eager unrolling (for efficiency, or in order to pass a fully unrolled program to a child robot that therefore does not need any complex devices to run it), but lazy evaluation is probably the better default, perhaps with a special "eager sequence" operation provided as well. Consider the example of a forever function that calls the same command in an infinite loop: under eager evaluation, this would simply go into an infinite loop generating an infinite program without ever executing anything!
However, we can also bind the result of c_1 in c_2, in which case we do not even have the option to evaluate c_2 eagerly (unless we wanted to implement partial evaluation of open terms). The rule for x \leftarrow c_1;\; c_2 is therefore: