comp.lang.ada
 help / color / mirror / Atom feed
* From Pascal to Ada
@ 2007-11-01 22:44 j.khaldi
  2007-11-01 23:38 ` Ludovic Brenta
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: j.khaldi @ 2007-11-01 22:44 UTC (permalink / raw)


Hi,
how do you translate this Pascal code in Ada?

type
  realFunction = function(x: double): double;

Thanks!




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

* Re: From Pascal to Ada
  2007-11-01 22:44 From Pascal to Ada j.khaldi
@ 2007-11-01 23:38 ` Ludovic Brenta
  2007-11-03 13:29   ` j.khaldi
  2007-11-01 23:44 ` anon
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Ludovic Brenta @ 2007-11-01 23:38 UTC (permalink / raw)


j.khaldi@oltrelinux.com writes:
> Hi,
> how do you translate this Pascal code in Ada?
>
> type
>   realFunction = function(x: double): double;
>
> Thanks!

In Ada you cannot declare subprogram types, only access-to-subprogram
(aka "pointer to subprogram") types.  In this case, you would say

declare
   type Real_Function is access function (X : Long_Float) return Long_Float;

   function F (X : Long_Float) return Long_Float is
   begin
      return 42.0 * X;
   end F;

   type Vector is array (Positive range <>) of Long_Float;
   procedure Apply (Func : in Real_Function; To : in out Vector) is
   begin
      for J in To'Range loop
         To (J) := Func (To (J)); -- call the pointed-to function
      end loop;
   end Apply;

   V : Vector (1 .. 100);
begin
   Apply (Func => F'Access, To => V);
end;

However there are other ways.  Ada 2005 introduces anonymous
access-to-subprogram types:

procedure Apply (Func : access function (X : Long_Float) return Long_Float;
                 To : in out Vector);

And it has been possible to use generics since Ada 83:

generic
   with function Func (X : Long_Float) return Long_Float;
procedure Apply (To : in out Vector);

Why do you ask?

-- 
Ludovic Brenta.



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

* Re: From Pascal to Ada
  2007-11-01 22:44 From Pascal to Ada j.khaldi
  2007-11-01 23:38 ` Ludovic Brenta
@ 2007-11-01 23:44 ` anon
  2007-11-01 23:45 ` Adam Beneschan
  2007-11-02  0:01 ` (see below)
  3 siblings, 0 replies; 9+ messages in thread
From: anon @ 2007-11-01 23:44 UTC (permalink / raw)



Try this:

  type realFunction is access function ( x : float ) return float ;



In <1193957079.981952.101030@d55g2000hsg.googlegroups.com>,  j.khaldi@oltrelinux.com writes:
>Hi,
>how do you translate this Pascal code in Ada?
>
>type
>  realFunction = function(x: double): double;
>
>Thanks!
>




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

* Re: From Pascal to Ada
  2007-11-01 22:44 From Pascal to Ada j.khaldi
  2007-11-01 23:38 ` Ludovic Brenta
  2007-11-01 23:44 ` anon
@ 2007-11-01 23:45 ` Adam Beneschan
  2007-11-03 13:04   ` j.khaldi
  2007-11-02  0:01 ` (see below)
  3 siblings, 1 reply; 9+ messages in thread
From: Adam Beneschan @ 2007-11-01 23:45 UTC (permalink / raw)


On Nov 1, 3:44 pm, j.kha...@oltrelinux.com wrote:
> Hi,
> how do you translate this Pascal code in Ada?
>
> type
>   realFunction = function(x: double): double;

Ada doesn't really have types that denote *functions*, but instead
they have types that are *access* to function (sort of like a
pointer).  Assuming you have a type named Double (in Ada, the standard
floating-point types are named Float and Long_Float):

   type Real_Function is access function (X : Double) return Double;

(Note: Ada programmers tend not to go for pseudo-Germanic or Hungarian
notation when naming things.)  If you have a function like Cos that
takes a Double and returns a Double, you'd say Cos'Access to get a
"pointer" to the function.

Now, you can not only pass this as a procedure or function parameter,
you can also declare variables of this type and store function
pointers in them.  Because of this ability, there are restrictions on
what kinds of functions you can take the 'Access of.  Basically, if
Func is nested inside some other procedure Proc, you can't use
Func'Access where a Real_Function is needed, unless the definition of
Real_Function is also nested inside that procedure Proc.  (That's an
oversimplification of the rule.)  Otherwise you could call Func while
you're not even inside Proc and really screw yourself up.

But if all you want to do is pass this function to a procedure, you
can use an *anonymous* access type:

   procedure Do_Integrate (Func : access function (X : Double) return
Double);

and now you can use Do_Integrate(Cos'Access) or whatever other
function you want, even if it's nested.  But you can't give a name to
the access-function type in that case.

Hope this helps,

                           -- Adam







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

* Re: From Pascal to Ada
  2007-11-01 22:44 From Pascal to Ada j.khaldi
                   ` (2 preceding siblings ...)
  2007-11-01 23:45 ` Adam Beneschan
@ 2007-11-02  0:01 ` (see below)
  2007-11-02  1:02   ` Adam Beneschan
  3 siblings, 1 reply; 9+ messages in thread
From: (see below) @ 2007-11-02  0:01 UTC (permalink / raw)


On 1/11/07 22:44, in article
1193957079.981952.101030@d55g2000hsg.googlegroups.com,
"j.khaldi@oltrelinux.com" <j.khaldi@oltrelinux.com> wrote:

> Hi,
> how do you translate this Pascal code in Ada?
> 
> type
>   realFunction = function(x: double): double;

That is NOT Pascal code.

Did you copy it from something, or make it up?
If the first, how was it used?
If the second, how do you want to use it?

-- 
Bill Findlay
<surname><forename> chez blueyonder.co.uk





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

* Re: From Pascal to Ada
  2007-11-02  0:01 ` (see below)
@ 2007-11-02  1:02   ` Adam Beneschan
  0 siblings, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2007-11-02  1:02 UTC (permalink / raw)


On Nov 1, 5:01 pm, "(see below)" <yaldni...@blueyonder.co.uk> wrote:
> On 1/11/07 22:44, in article
> 1193957079.981952.101...@d55g2000hsg.googlegroups.com,
>
> "j.kha...@oltrelinux.com" <j.kha...@oltrelinux.com> wrote:
> > Hi,
> > how do you translate this Pascal code in Ada?
>
> > type
> >   realFunction = function(x: double): double;
>
> That is NOT Pascal code.

It's not standard Pascal code, perhaps (sorry, I haven't kept up to
date with the latest Pascal standards).  But I know of at least one
Pascal compiler that accepts it (and lots of other extensions).

                        -- Adam




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

* Re: From Pascal to Ada
  2007-11-01 23:45 ` Adam Beneschan
@ 2007-11-03 13:04   ` j.khaldi
  0 siblings, 0 replies; 9+ messages in thread
From: j.khaldi @ 2007-11-03 13:04 UTC (permalink / raw)


> Ada doesn't really have types that denote *functions*, but instead
> they have types that are *access* to function (sort of like a
> pointer).  Assuming you have a type named Double (in Ada, the standard
> floating-point types are named Float and Long_Float):
>
>    type Real_Function is access function (X : Double) return Double;
>
> (Note: Ada programmers tend not to go for pseudo-Germanic or Hungarian
> notation when naming things.)  If you have a function like Cos that
> takes a Double and returns a Double, you'd say Cos'Access to get a
> "pointer" to the function.
>
> Now, you can not only pass this as a procedure or function parameter,
> you can also declare variables of this type and store function
> pointers in them.  Because of this ability, there are restrictions on
> what kinds of functions you can take the 'Access of.  Basically, if
> Func is nested inside some other procedure Proc, you can't use
> Func'Access where a Real_Function is needed, unless the definition of
> Real_Function is also nested inside that procedure Proc.  (That's an
> oversimplification of the rule.)  Otherwise you could call Func while
> you're not even inside Proc and really screw yourself up.
>
> But if all you want to do is pass this function to a procedure, you
> can use an *anonymous* access type:
>
>    procedure Do_Integrate (Func : access function (X : Double) return
> Double);
>
> and now you can use Do_Integrate(Cos'Access) or whatever other
> function you want, even if it's nested.  But you can't give a name to
> the access-function type in that case.
Very clear and interesting.
Thank you.

Jilani




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

* Re: From Pascal to Ada
  2007-11-01 23:38 ` Ludovic Brenta
@ 2007-11-03 13:29   ` j.khaldi
  2007-11-03 13:51     ` Ludovic Brenta
  0 siblings, 1 reply; 9+ messages in thread
From: j.khaldi @ 2007-11-03 13:29 UTC (permalink / raw)


On Nov 2, 12:38 am, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> j.kha...@oltrelinux.com writes:
> > Hi,
> > how do you translate this Pascal code in Ada?
>
> > type
> >   realFunction = function(x: double): double;
>
> generic
>    with function Func (X : Long_Float) return Long_Float;
> procedure Apply (To : in out Vector);
>
Well, I am porting a math library from Object Pascal (Delphi and Free
Pascal) to Ada.
The original library was written in C and I translated it to Object
Pascal many
years ago. As other C libraries it uses a lot of pointer to functions
and the
translation between C and Object pascal code was a straitful word to
word.
The library has a lot of functions, very fast and I am using it for a
long time in
many projects without arising a single bug.
Lastly I am very interested in Ada and as first "serious" exercice, I
am trying a
fast "mechanical" translation, almost word to word, from Object Pascal
to Ada of this
library.

Jilani




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

* Re: From Pascal to Ada
  2007-11-03 13:29   ` j.khaldi
@ 2007-11-03 13:51     ` Ludovic Brenta
  0 siblings, 0 replies; 9+ messages in thread
From: Ludovic Brenta @ 2007-11-03 13:51 UTC (permalink / raw)


j.khaldi@oltrelinux.com writes:
> Lastly I am very interested in Ada and as first "serious" exercice,
> I am trying a fast "mechanical" translation, almost word to word,
> from Object Pascal to Ada of this library.

There is a tool called p2ada (Pascal to Ada) for this.  I have never
tried it, so I don't know if it handles Object Pascal.  See
http://homepage.sunrise.ch/mysunrise/gdm/gsoft.htm

-- 
Ludovic Brenta.




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

end of thread, other threads:[~2007-11-03 13:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-01 22:44 From Pascal to Ada j.khaldi
2007-11-01 23:38 ` Ludovic Brenta
2007-11-03 13:29   ` j.khaldi
2007-11-03 13:51     ` Ludovic Brenta
2007-11-01 23:44 ` anon
2007-11-01 23:45 ` Adam Beneschan
2007-11-03 13:04   ` j.khaldi
2007-11-02  0:01 ` (see below)
2007-11-02  1:02   ` Adam Beneschan

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