1 1 false Entering factorial. Stack top is with value .

Dispatching recursive actions in XForms

C. M. Sperberg-McQueen, 30 July 2012

This form demonstrates the use of xf:action, xf:dispatch, and user-specified events to provide a kind of subroutine-call mechanism. It may be thought of as yet another illustration of the proposition that XForms is Turing-complete. (It also illustrates that Javascript numbers overflow somewhere between 21! and 22! in some browsers.)


Enter a (small) integer:
Calculate factorial()
factorial() =
Turn tracing on?

Stack

The state of the stack instance document is shown below. (This doesn't currently ever show anything useful, see discussion below.)

     
   
Details and discussion ...
Hide discussion ...

What is going on under the hood?

The model includes an instance which holds a stack.


          
          
      ]]>

The input widget accepts an integer value from the user.

When the input value changes, the widget dispatches a DOMActivate event to a Calculate factorial ... button. (The button is hidden unless tracing is turned on.)


     Enter a (small) integer: 
     
   ]]>

The Calculate factorial ... button places the user-entered value on the stack and dispatches a factorial event to the model.


     
       Calculate factorial()
       

	 
	 
	 

	 
	 

	 
	 
	 
       
     
     
]]>

The model's handler for the factorial event implements the usual recursive definition of the function. It looks for its input parameter on the stack and either returns (if the parameter is less than 1) or performs a recursive call, then multiplies the result of the recursive call by the input value, places the final result on the stack, and cleans its own parameter and temporary values off the stack.


       
       

       
	 Entering factorial.  
	 Stack top is  
	 with value .
       

       
       
       

       
	 
	 
	 
	 
	 

	 
	 
	 
	 
	 
	 
	 
	 
       
     ]]>

The output control just displays the result of the calculation, as a way of making visible it visible.

The display of the stack instance was intended to allow the user to see the state of the stack as it changed, but apparently I haven't yet fully understood the intended usage and effect of the xforms-refresh event. At least, nothing I've tried has made the stack display update to show the changes of the stack during the recursive calls.