Componolit/libsparkcrypto

Make library pure

senier opened this issue · 2 comments

The library should not have any internal state - add pragma Pure to all packages.

Seems like only debug output prevents us from making all packages pure. We should look into hacks to get debug output in a pure package.

Here is a hack to enable non-pure debug functions:

  1. Create an output package which is non-pure and exports and output operation
package Output                                                                                                                                 
is
   procedure Write (Message : String)
   with Export;
end Output;

The implementation may use whatever output facility is appropriate:

with Ada.Text_IO;

package body Output
is
   procedure Write (Message : String)
   is
   begin
      Ada.Text_IO.Put_Line (Message);
   end Write;
end Output;

The Output package is not referenced within the (pure) packages that are being debugged. It must, however, be included somewhere in a non-pure part of the program to provide a symbol to the Write operation.

  1. Create a "pure" debug package
package Debug                                                                                                                                  
is
   pragma Pure;
   procedure Print (Message : String);
end Debug;

The Print procedure is used to print debug output in pure packages. The body imports the output function:

package body Debug                                                                                                                             
is
   procedure Write (Message : String) with Import;

   procedure Print (Message : String) is
   begin
      Write ("DEBUG: " & Message);
   end Print;
end Debug;
  1. Use the debug package in a pure package
package Pure                                                                                                                                   
is
   pragma Pure;
   function Foo (Arg : Integer) return Integer;
end Pure;
with Debug;                                                                                                                                    
package body Pure
is
   function Foo (Arg : Integer) return Integer
   is
   begin
      Debug.Print ("(Debug) Arg: " & Arg'Image);
      return Arg + 1;
   end Foo;
end Pure;