module fibonacci

   graph class: DirectedGraph

   exports
      transformation unit fib_slow;
      transformation unit fib_fast;
      transformation unit fib_tree_slow;
      transformation unit fib_tree_fast

   realized by

      % Calculated the fib tree of the transformation unit fib_slow
      transformation unit fib_tree_slow
         initial:
            (vertices labeled leaf)
         body:
            apply as long as possible fn
         terminal:
      end;

      % Calculates the fib tree of the transformation unit fib_fast
      transformation unit fib_tree_fast
         initial:
            (vertices labeled leaf)
         body:
            apply as long as possible collapse < fn
         terminal:
      end;

      % Calculates the fibonacci number the classic way
      % fib(n = fib(n-1) + fib(n-2)
      transformation unit fib_slow
         initial:
            (vertices labeled leaf)
         body:
            apply as long as possible fn;
            apply as long as possible sum
         terminal:
            (vertices labeled leaf)
      end;
      
      % Speeds up calculation due to shared nodes
      transformation unit fib_fast
         initial:
            (vertices labeled leaf)
         body:
            apply as long as possible collapse < fn;
            apply as long as possible sum < expand
         terminal:
            (vertices labeled leaf)
      end
end.