comp.lang.ada
 help / color / mirror / Atom feed
* ? Is ok return a  type derived from ada.finalization.controlled from a "Pure_Function" ? thanks.
@ 2019-01-24 23:56 danielcheagle
  2019-01-25 21:20 ` Randy Brukardt
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: danielcheagle @ 2019-01-24 23:56 UTC (permalink / raw)


  Hi!

  Is ok return a type derived from ada.finalization.controlled 
from a function declared "Pure_Function" ?

  Or yet, is ok declare a fuction returning a 
controlled type as "pure_function" ?

  Thanks in Advance!!!
  
[]'s Dani. :-)

 note1 : the type has a access value.
 note2 : initialize, adjust and finalize overrided and working :-)

 fragment example code:

---------------------------------
pragma Ada_2012;
pragma Detect_Blocking;

with Ada.Finalization;

package Arbitrary
  with preelaborate
is

   type Arbitrary_Type (size : Positive) is
     new Ada.Finalization.Controlled with private;

   function To_Arbitrary (value : Integer; precision : Integer)
     return Arbitrary_Type
      with inline; -- Can I add "pure_function" ?

private

  type Mantissa_Type is array (Positive range <>) of Integer;
  type Mantissa_Pointer is access Mantissa_Type;

  type Arbitrary_Type (size : Positive) is
    new Ada.Finalization.Controlled with record
      mantissa    : Mantissa_Pointer;
      exponent    : Integer;
      sign        : Integer range -1 .. 1;
      precision   : Positive := size;
   end record;

end arbitrary;

-----------------------------------------------


pragma Ada_2012;
pragma Detect_Blocking;

with Ada.Unchecked_Deallocation;

package body Arbitrary is

  procedure Delete is new Ada.Unchecked_Deallocation (Mantissa_Type,
    Mantissa_Pointer);

  -----------------------------------------------------------------------
  -- Initialize an Arbitrary_Type
  -----------------------------------------------------------------------
  procedure Initialize (Object : in out Arbitrary_Type) is
  begin
    Object.mantissa     := new Mantissa_Type (1 .. Object.precision);
    Object.exponent     := 0;
    Object.sign         := 1;
    -- "here" for diminish race condition from OS' s
    Object.mantissa.all := (others => 0);
  end Initialize;

  -----------------------------------------------------------------------
  -- Fix an Arbitrary_Type after being assigned a value
  -----------------------------------------------------------------------
  procedure Adjust (Object : in out Arbitrary_Type) is
  begin
    Object.mantissa := new Mantissa_Type'(Object.mantissa.all);
  end Adjust;

  -----------------------------------------------------------------------
  -- Release an Arbitrary_Type;
  -----------------------------------------------------------------------
  procedure Finalize (Object : in out Arbitrary_Type) is
  begin
    if Object.mantissa /= null then
      Delete (Object.mantissa);
    end if;
    Object.mantissa := null;
  end Finalize;
  
  -----------------------------------------------------------------------
  -- Convert an Integer type to an Arbitrary_Type
  -----------------------------------------------------------------------
  function To_Arbitrary (value : Integer; precision : Integer)
    return Arbitrary_Type is
    result    : Arbitrary_Type (precision);
  begin
    result.mantissa (result.exponent + 1) := value;
    Normalize (result);
    return result;
  end To_Arbitrary;

end arbitrary;
-------------------------------------------------------------------

 Really Thanks! :-)
 []'s Dani. :-)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ? Is ok return a  type derived from ada.finalization.controlled from a "Pure_Function" ? thanks.
  2019-01-24 23:56 ? Is ok return a type derived from ada.finalization.controlled from a "Pure_Function" ? thanks danielcheagle
@ 2019-01-25 21:20 ` Randy Brukardt
  2019-01-26  0:22 ` Shark8
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2019-01-25 21:20 UTC (permalink / raw)


Of course it's OK, "Pure_Function" is some GNAT-specific nonsense. :-)

My recollection is that GNAT does not check if Pure_Function makes sense, so 
the only question is whether you can live with the possible implications. 
(And I don't know why you would want to use Pure_Function anyway.)

Note that in Ada 2020, you would use the Global aspect to declare the usage 
of globals by your subprogram, and those are checked, so either the aspect 
is legal or your program won't compile. But GNAT hasn't implemented that 
yet, so far as I know.

                         Randy.

<danielcheagle@gmail.com> wrote in message 
news:db66050e-9606-4c79-bd21-6c0164913181@googlegroups.com...
>  Hi!
>
>  Is ok return a type derived from ada.finalization.controlled
> from a function declared "Pure_Function" ?
>
>  Or yet, is ok declare a fuction returning a
> controlled type as "pure_function" ?
>
>  Thanks in Advance!!!
>
> []'s Dani. :-)
>
> note1 : the type has a access value.
> note2 : initialize, adjust and finalize overrided and working :-)
>
> fragment example code:
>
> ---------------------------------
> pragma Ada_2012;
> pragma Detect_Blocking;
>
> with Ada.Finalization;
>
> package Arbitrary
>  with preelaborate
> is
>
>   type Arbitrary_Type (size : Positive) is
>     new Ada.Finalization.Controlled with private;
>
>   function To_Arbitrary (value : Integer; precision : Integer)
>     return Arbitrary_Type
>      with inline; -- Can I add "pure_function" ?
>
> private
>
>  type Mantissa_Type is array (Positive range <>) of Integer;
>  type Mantissa_Pointer is access Mantissa_Type;
>
>  type Arbitrary_Type (size : Positive) is
>    new Ada.Finalization.Controlled with record
>      mantissa    : Mantissa_Pointer;
>      exponent    : Integer;
>      sign        : Integer range -1 .. 1;
>      precision   : Positive := size;
>   end record;
>
> end arbitrary;
>
> -----------------------------------------------
>
>
> pragma Ada_2012;
> pragma Detect_Blocking;
>
> with Ada.Unchecked_Deallocation;
>
> package body Arbitrary is
>
>  procedure Delete is new Ada.Unchecked_Deallocation (Mantissa_Type,
>    Mantissa_Pointer);
>
>  -----------------------------------------------------------------------
>  -- Initialize an Arbitrary_Type
>  -----------------------------------------------------------------------
>  procedure Initialize (Object : in out Arbitrary_Type) is
>  begin
>    Object.mantissa     := new Mantissa_Type (1 .. Object.precision);
>    Object.exponent     := 0;
>    Object.sign         := 1;
>    -- "here" for diminish race condition from OS' s
>    Object.mantissa.all := (others => 0);
>  end Initialize;
>
>  -----------------------------------------------------------------------
>  -- Fix an Arbitrary_Type after being assigned a value
>  -----------------------------------------------------------------------
>  procedure Adjust (Object : in out Arbitrary_Type) is
>  begin
>    Object.mantissa := new Mantissa_Type'(Object.mantissa.all);
>  end Adjust;
>
>  -----------------------------------------------------------------------
>  -- Release an Arbitrary_Type;
>  -----------------------------------------------------------------------
>  procedure Finalize (Object : in out Arbitrary_Type) is
>  begin
>    if Object.mantissa /= null then
>      Delete (Object.mantissa);
>    end if;
>    Object.mantissa := null;
>  end Finalize;
>
>  -----------------------------------------------------------------------
>  -- Convert an Integer type to an Arbitrary_Type
>  -----------------------------------------------------------------------
>  function To_Arbitrary (value : Integer; precision : Integer)
>    return Arbitrary_Type is
>    result    : Arbitrary_Type (precision);
>  begin
>    result.mantissa (result.exponent + 1) := value;
>    Normalize (result);
>    return result;
>  end To_Arbitrary;
>
> end arbitrary;
> -------------------------------------------------------------------
>
> Really Thanks! :-)
> []'s Dani. :-)
>
>
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ? Is ok return a  type derived from ada.finalization.controlled from a "Pure_Function" ? thanks.
  2019-01-24 23:56 ? Is ok return a type derived from ada.finalization.controlled from a "Pure_Function" ? thanks danielcheagle
  2019-01-25 21:20 ` Randy Brukardt
@ 2019-01-26  0:22 ` Shark8
  2019-01-26 11:48 ` Simon Wright
  2019-01-26 17:02 ` Daniel Norte Moraes
  3 siblings, 0 replies; 5+ messages in thread
From: Shark8 @ 2019-01-26  0:22 UTC (permalink / raw)


On Thursday, January 24, 2019 at 4:56:12 PM UTC-7, Daniel Norte Moraes wrote:
> Hi!
> 
>   Is ok return a type derived from ada.finalization.controlled 
> from a function declared "Pure_Function" ?

IIRC, Pure_Function doesn't need to be in a Pure unit to be tagged as such, and the GNAT-specific meaning is: given a call with a particular set of parameter-values always returns the same result.

As I recall GNAT doesn't actually check this is case, but rather uses it for optimization purposes.

> 
>   Or yet, is ok declare a fuction returning a controlled type as "pure_function" ?

See above: "Pure_Function" has nothing to do with categorization or restrictions and is just an attribute denoting allowance for certain optimizations. (Again, IIRC.)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ? Is ok return a  type derived from ada.finalization.controlled from a "Pure_Function" ? thanks.
  2019-01-24 23:56 ? Is ok return a type derived from ada.finalization.controlled from a "Pure_Function" ? thanks danielcheagle
  2019-01-25 21:20 ` Randy Brukardt
  2019-01-26  0:22 ` Shark8
@ 2019-01-26 11:48 ` Simon Wright
  2019-01-26 17:02 ` Daniel Norte Moraes
  3 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2019-01-26 11:48 UTC (permalink / raw)


danielcheagle@gmail.com writes:

>   Is ok return a type derived from ada.finalization.controlled 
> from a function declared "Pure_Function" ?
>
>   Or yet, is ok declare a fuction returning a 
> controlled type as "pure_function" ?

Given that the documentation of Pure_Function[1] says

   ... the compiler can assume that there are no side effects, and in
   particular that two calls with identical arguments produce the same
   result

and that

   ... there are no static checks to try to ensure that this promise is
   met

it would be a Bad Idea to apply it to your function.

[1] https://gcc.gnu.org/onlinedocs/gnat_rm/Pragma-Pure_005fFunction.html


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ? Is ok return a  type derived from ada.finalization.controlled from a "Pure_Function" ? thanks.
  2019-01-24 23:56 ? Is ok return a type derived from ada.finalization.controlled from a "Pure_Function" ? thanks danielcheagle
                   ` (2 preceding siblings ...)
  2019-01-26 11:48 ` Simon Wright
@ 2019-01-26 17:02 ` Daniel Norte Moraes
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Norte Moraes @ 2019-01-26 17:02 UTC (permalink / raw)


Em quinta-feira, 24 de janeiro de 2019 21:56:12 UTC-2, Daniel Norte Moraes  escreveu:
> Hi!
> 
>   Is ok return a type derived from ada.finalization.controlled 
> from a function declared "Pure_Function" ?
> 
>   Or yet, is ok declare a fuction returning a 
> controlled type as "pure_function" ?
> 
>   Thanks in Advance!!!
>   
> []'s Dani. :-)
> 
>  note1 : the type has a access value.
>  note2 : initialize, adjust and finalize overrided and working :-)
> 
>  fragment example code:
> 
> ---------------------------------
> pragma Ada_2012;
> pragma Detect_Blocking;
> 
> with Ada.Finalization;
> 
> package Arbitrary
>   with preelaborate
> is
> 
>    type Arbitrary_Type (size : Positive) is
>      new Ada.Finalization.Controlled with private;
> 
>    function To_Arbitrary (value : Integer; precision : Integer)
>      return Arbitrary_Type
>       with inline; -- Can I add "pure_function" ?
> 
> private
> 
>   type Mantissa_Type is array (Positive range <>) of Integer;
>   type Mantissa_Pointer is access Mantissa_Type;
> 
>   type Arbitrary_Type (size : Positive) is
>     new Ada.Finalization.Controlled with record
>       mantissa    : Mantissa_Pointer;
>       exponent    : Integer;
>       sign        : Integer range -1 .. 1;
>       precision   : Positive := size;
>    end record;
> 
> end arbitrary;
> 
> -----------------------------------------------
> 
> 
> pragma Ada_2012;
> pragma Detect_Blocking;
> 
> with Ada.Unchecked_Deallocation;
> 
> package body Arbitrary is
> 
>   procedure Delete is new Ada.Unchecked_Deallocation (Mantissa_Type,
>     Mantissa_Pointer);
> 
>   -----------------------------------------------------------------------
>   -- Initialize an Arbitrary_Type
>   -----------------------------------------------------------------------
>   procedure Initialize (Object : in out Arbitrary_Type) is
>   begin
>     Object.mantissa     := new Mantissa_Type (1 .. Object.precision);
>     Object.exponent     := 0;
>     Object.sign         := 1;
>     -- "here" for diminish race condition from OS' s
>     Object.mantissa.all := (others => 0);
>   end Initialize;
> 
>   -----------------------------------------------------------------------
>   -- Fix an Arbitrary_Type after being assigned a value
>   -----------------------------------------------------------------------
>   procedure Adjust (Object : in out Arbitrary_Type) is
>   begin
>     Object.mantissa := new Mantissa_Type'(Object.mantissa.all);
>   end Adjust;
> 
>   -----------------------------------------------------------------------
>   -- Release an Arbitrary_Type;
>   -----------------------------------------------------------------------
>   procedure Finalize (Object : in out Arbitrary_Type) is
>   begin
>     if Object.mantissa /= null then
>       Delete (Object.mantissa);
>     end if;
>     Object.mantissa := null;
>   end Finalize;
>   
>   -----------------------------------------------------------------------
>   -- Convert an Integer type to an Arbitrary_Type
>   -----------------------------------------------------------------------
>   function To_Arbitrary (value : Integer; precision : Integer)
>     return Arbitrary_Type is
>     result    : Arbitrary_Type (precision);
>   begin
>     result.mantissa (result.exponent + 1) := value;
>     Normalize (result);
>     return result;
>   end To_Arbitrary;
> 
> end arbitrary;
> -------------------------------------------------------------------
> 
>  Really Thanks! :-)
>  []'s Dani. :-)

Thanks Randy,Shark8 and Simon! :-)

Continuing the work, I tested including pure_function and the craziest mistakes popped up.
gdb showed that pure_function interfered with "adjust" and "initialize" horribly. (I.e.
Thanks for the advice! at least in "to_arbitrary" add "Pure_Function" is a bad idea.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-01-26 17:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-24 23:56 ? Is ok return a type derived from ada.finalization.controlled from a "Pure_Function" ? thanks danielcheagle
2019-01-25 21:20 ` Randy Brukardt
2019-01-26  0:22 ` Shark8
2019-01-26 11:48 ` Simon Wright
2019-01-26 17:02 ` Daniel Norte Moraes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox