comp.lang.ada
 help / color / mirror / Atom feed
* Ada as glue ? logical approach ?
@ 2012-01-29  4:53 Patrick
  2012-01-29  8:26 ` Simon Wright
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Patrick @ 2012-01-29  4:53 UTC (permalink / raw)


Hi Everyone

I have been studying ada for sometime now but I have no experience
coding with it.

 It doesn't look like there are all the library bindings i want but I
am wondering if this really matters.

Someone could write a full binding for a library but could someone
also not just use the import pragma to "grab onto" the existing C
library APIs?

This would be a bit like ctypes in Python correct ? Are other list
members writing their own 'as needed' partial bindings? Is this a
logical approach? -Patrick



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

* Re: Ada as glue ? logical approach ?
  2012-01-29  4:53 Ada as glue ? logical approach ? Patrick
@ 2012-01-29  8:26 ` Simon Wright
  2012-01-29  9:46 ` mockturtle
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Simon Wright @ 2012-01-29  8:26 UTC (permalink / raw)


Patrick <patrick@spellingbeewinnars.org> writes:

> I have been studying ada for sometime now but I have no experience
> coding with it.

Get coding then! It won't hurt (much).

> Someone could write a full binding for a library but could someone
> also not just use the import pragma to "grab onto" the existing C
> library APIs?
>
> This would be a bit like ctypes in Python correct ? Are other list
> members writing their own 'as needed' partial bindings? Is this a
> logical approach? -Patrick

Quite a few of the "full bindings" are in fact partial; there's often a
lot of the full API that isn't immediately necessary. For example, in my
Ada 2005 Math Extensions[1], I didn't bother with the parts of LAPACK
and BLAS that deal with matrices of special forms, for example an "upper
Hessenberg", whatever that is (see [2] Table 2.1 for the full set).

[1] https://sourceforge.net/projects/gnat-math-extn/
[2] http://www.netlib.org/lapack/lug/node24.html



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

* Re: Ada as glue ? logical approach ?
  2012-01-29  4:53 Ada as glue ? logical approach ? Patrick
  2012-01-29  8:26 ` Simon Wright
@ 2012-01-29  9:46 ` mockturtle
  2012-01-29 13:10 ` Brian Drummond
  2012-02-01  2:19 ` BrianG
  3 siblings, 0 replies; 10+ messages in thread
From: mockturtle @ 2012-01-29  9:46 UTC (permalink / raw)


On Sunday, January 29, 2012 5:53:21 AM UTC+1, Patrick wrote:
> Hi Everyone
> 
> I have been studying ada for sometime now but I have no experience
> coding with it.

<OT-remark class="old-guy-suggestion"> :-)
I +1 what Simon told you: start coding.  In my (humble) opinion, computer languages and human languages share at least one characteristic: if you really want to learn them, you must use them.  Studying the grammar is useful and it gives you an idea of the language, but if you want to master it you need to use it.
</OT-remark>

> 
>  It doesn't look like there are all the library bindings i want but I
> am wondering if this really matters.
> 
> Someone could write a full binding for a library but could someone
> also not just use the import pragma to "grab onto" the existing C
> library APIs?
> 

Let me see if I understand your question.  Let us consider, for example, the GNAT.Sockets library that refers to the socket C API and consider the procedure to connect a socket.  You are asking if you can do something like
 
    pragma Import(C, BSD_Connect, "connect");

and then calling BSD_Connect directly instead of writing a procedure GNAT.Sockets.Connect_Socket that in turn calls the connect() (imported with a pragma similar to the above one).

If this is your question, the answer is that, yes, you can, but the resulting code would not be very convenient to use.  For example, depending on the parameters of the C function, you need to convert from Ada parameter to C parameter (e.g., Ada String to C char*) and this would be quite unconvenient to do every time you need to call the function.  For me, I prefer to write a tiny procedure that takes normal Ada parameters and hides the conversions problem.  
Note also that sometimes conversion could be a little tricky, so you do not want to spread your code with tricky conversion stuff that could introduce bugs; it is better to have the tricky part only in one place, write it and debug it just once and forget about it.

The only reason that I see for "grabbing" the "naked" C API is when you are in a rush (e.g., you need the final version tomorrow) and you need to call the C API in just few places.  Of course, your mileage can vary.

> This would be a bit like ctypes in Python correct ? Are other list
> members writing their own 'as needed' partial bindings? 

Yes, me too.  As Simon told you, many bindings are just partial, no need to interface with the whole C library.  

> Is this a logical approach? -Patrick


Riccardo



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

* Re: Ada as glue ? logical approach ?
  2012-01-29  4:53 Ada as glue ? logical approach ? Patrick
  2012-01-29  8:26 ` Simon Wright
  2012-01-29  9:46 ` mockturtle
@ 2012-01-29 13:10 ` Brian Drummond
  2012-01-29 13:57   ` Patrick
  2012-02-01  2:19 ` BrianG
  3 siblings, 1 reply; 10+ messages in thread
From: Brian Drummond @ 2012-01-29 13:10 UTC (permalink / raw)


On Sat, 28 Jan 2012 20:53:21 -0800, Patrick wrote:

> Hi Everyone
> 
> I have been studying ada for sometime now but I have no experience
> coding with it.
> 
>  It doesn't look like there are all the library bindings i want but I
> am wondering if this really matters.
> 
> Someone could write a full binding for a library but could someone also
> not just use the import pragma to "grab onto" the existing C library
> APIs?
> 
> This would be a bit like ctypes in Python correct ? Are other list
> members writing their own 'as needed' partial bindings? Is this a
> logical approach? -Patrick

In addition to agreeing with the other comments - especially the 
recommendation to make a "thick binding" to give you an Ada-like 
interface, rather than exposing the C-like details any more widely than 
necessary...

if you use a recent enough version of GCC, you can compile a C or C++ 
header file with the -fdump-ada-spec or -fdump-ada-spec-slim options, and 
it will automatically generate the thin binding (with import pragmas) for 
you. The -slim version just compiles the header you give it; the fat 
version compiles the transitive closure (i.e. all the header files it 
imports too, recursively)

The result is not perfect; very good on plain C but it doesn't handle C++ 
templates very well (last time I looked) but even then, it saves a lot of 
time.

I would implement the Ada binding as a package that uses this, exposing 
only Ada types and functions to the rest of your code.

A very simple example : 
http://wiki.ada-dk.org/c_bindings_example

If you are importing C rather than C++, ignore the bit about saving the 
header as .hpp. (There is another way to force C vs C++, not covered in 
the article. Compile a .h file with g++ rather than gcc)

See also 
http://www.adacore.com/2009/02/23/gem-59/

- Brian




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

* Re: Ada as glue ? logical approach ?
  2012-01-29 13:10 ` Brian Drummond
@ 2012-01-29 13:57   ` Patrick
  2012-01-29 16:51     ` mockturtle
  0 siblings, 1 reply; 10+ messages in thread
From: Patrick @ 2012-01-29 13:57 UTC (permalink / raw)


Hi Simon, Hi Riccardo, Hi Brian

Thank you so much for the valuable feedback, I did not expect to see
this Sunday morning.

I will follow all this advice. I have been studying for about 150-200
hours and haven't written a thing. That's going to change today !

All the best-Patrick




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

* Re: Ada as glue ? logical approach ?
  2012-01-29 13:57   ` Patrick
@ 2012-01-29 16:51     ` mockturtle
  2012-01-30  0:26       ` Patrick
  0 siblings, 1 reply; 10+ messages in thread
From: mockturtle @ 2012-01-29 16:51 UTC (permalink / raw)


Il giorno domenica 29 gennaio 2012 14:57:18 UTC+1, Patrick ha scritto:
> Hi Simon, Hi Riccardo, Hi Brian
> 
> Thank you so much for the valuable feedback, 

You are more than welcome.

> I did not expect to see this Sunday morning.

You know, on Sunday you do not work, so you have time to write answers on newsgroups :-)

> 
> I will follow all this advice. I have been studying for about 150-200
> hours and haven't written a thing. That's going to change today !
> 

Still with my old-guy hat on, I can tell what I do in these cases: after a couple of "hello world"s, I take a project that is of medium complexity and fun :-) and I develop it.  For example, when I learnt Ada I wrote a package for computation with Galois fields and a generic package for computation with matrices with entries in a ring (if this is fun it is a matter of tastes, but never mind...).  Because of the inexperience, the resulting code was not the best code in the world, but nevertheless helped me in learning.

> All the best-Patrick




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

* Re: Ada as glue ? logical approach ?
  2012-01-29 16:51     ` mockturtle
@ 2012-01-30  0:26       ` Patrick
  0 siblings, 0 replies; 10+ messages in thread
From: Patrick @ 2012-01-30  0:26 UTC (permalink / raw)


Hi Riccardo

In February I am planning on writing an application to help Autistic
kids visualize how words should be spoken in English. Later on this
year I have an ada on bare metal project planned.

It looks like such a neat language and I think it's going to be a lot
of fun.

Thanks again :)



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

* Re: Ada as glue ? logical approach ?
  2012-01-29  4:53 Ada as glue ? logical approach ? Patrick
                   ` (2 preceding siblings ...)
  2012-01-29 13:10 ` Brian Drummond
@ 2012-02-01  2:19 ` BrianG
  2012-02-01  4:07   ` Patrick
  3 siblings, 1 reply; 10+ messages in thread
From: BrianG @ 2012-02-01  2:19 UTC (permalink / raw)


On 01/28/2012 11:53 PM, Patrick wrote:
> Hi Everyone
>
> I have been studying ada for sometime now but I have no experience
> coding with it.
>
>   It doesn't look like there are all the library bindings i want but I
> am wondering if this really matters.
>
> Someone could write a full binding for a library but could someone
> also not just use the import pragma to "grab onto" the existing C
> library APIs?
>
> This would be a bit like ctypes in Python correct ? Are other list
> members writing their own 'as needed' partial bindings? Is this a
> logical approach? -Patrick

I'd like to add one caution to the other responses.  If you combine the 
two descriptions above (little experience in Ada and writing/using thin 
bindings), you may fall into the trap of writing C (or whatever) in Ada. 
  This is especially true if you are familiar with that language.

On the other hand, if you do manage to write Ada in Ada, you'll soon 
find yourself writing that thick binding, because it makes your 
application code so much easier to manage.

-- 
---
BrianG
000
@Google's Mail name



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

* Re: Ada as glue ? logical approach ?
  2012-02-01  2:19 ` BrianG
@ 2012-02-01  4:07   ` Patrick
  2012-02-01  6:05     ` tmoran
  0 siblings, 1 reply; 10+ messages in thread
From: Patrick @ 2012-02-01  4:07 UTC (permalink / raw)


Thanks Brian

It would be a lot easier for me to follow the crowd and code in C but
I really don't like the language much. I definitely don't want to
bring it's problems into ada.

I will try to follow along with the principles of ada. Thanks for the
feedback and steering this newbie away from dangers



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

* Re: Ada as glue ? logical approach ?
  2012-02-01  4:07   ` Patrick
@ 2012-02-01  6:05     ` tmoran
  0 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2012-02-01  6:05 UTC (permalink / raw)


For an example "thick" binding you can take a look at Claw at
www.rrsoftware.com
It's an Ada 95 binding to Windows, which is probably a bit large to digest ;),
but you can look at some pieces to see the flavor and techniques.



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

end of thread, other threads:[~2012-02-01  6:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-29  4:53 Ada as glue ? logical approach ? Patrick
2012-01-29  8:26 ` Simon Wright
2012-01-29  9:46 ` mockturtle
2012-01-29 13:10 ` Brian Drummond
2012-01-29 13:57   ` Patrick
2012-01-29 16:51     ` mockturtle
2012-01-30  0:26       ` Patrick
2012-02-01  2:19 ` BrianG
2012-02-01  4:07   ` Patrick
2012-02-01  6:05     ` tmoran

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