asedt - Ada


Page menu:

Posted by asedt
2011-11-15

Changed by asedt
2012-01-06

Ada programming

My first encounter with the Ada programming language was in 2004 when I was taking the first programming class at Linköpings University. From the begining I started to like this programming language a lot. I had some previous programming experience in C++ from hi-school but not much. There was much that was new to me, previous I only had experience from IDE's and Windows but now we worked in the terminal on Unix machines. In the same class we also learned Lisp that I also liked very much.

Ada compared to other languages

Ada first apperd in 1980 compared to Fortran (1957), Lisp (1958), Pascal (1970), C (1972), C++ (1983), Python (1991) and Java (1995).

In November 2011 Ada was on position 19 on the TIOBE index which is an indicator of the popularity of programming languages.
In December 2011 Ada was on position 17 on the TIOBE index.

Ada is well suited for safe and secure software, a quote from John Barnes comparing it with C is below:

In terms of software, the languages Ada and C have very different attitudes to freedom. Ada introduces restrictions and checks, with the goal of providing freedom from errors. On the other hand C gives the programmer more freedom, making it easier to make errors.
~ John Barnes, Safe and Secure Software - An invitation to Ada 2005

Anothere quote is from Richard Riehle where hi gives his observation and opinion about the Ada language:

The Ada language is designed to maximize the possibilities for error detection early in the development process. This reduces the overall cost of software development since it is known that errors corrected early, will cost less than those detected late. No other language, not C++, not Java, not Eiffel, provides the level of support for safety-critical software found in Ada. Developers can and do make mistakes, even using Ada, but the probability of such mistakes is substantially less when using Ada than it is when using other languages. Therefore, when failure is not acceptable, during development or during software execution, the wise developer will select Ada. Although Ada is not as popular as some of the more well-known languages, it has substantial benefits over most of them when software dependability is a primary objective. In addition, contemporary Ada is as appropriate for developing GUI-based applications, information systems applications, and computationally intensive systems as any existing language. It is truly a general-purpose language, well-suited to any kind of software problem you might encounter.
~ Richard Riehle, Ada Distilled

Ada verisons/standards

Standards documents: Ada Standards - Ada Resource Association.

See this comparison chart from AdaCore that shows an overview of evolution of the major features of the Ada programming language.

Ada Links

The IRC channel for Ada is #ada on irc.freenode.net

ADA Implementations

GNAT Variants (2011)

VariantPageCostLicence
GNAT FSF (Part of GCC)GCC, the GNU Compiler CollectionCost freeVersion 4.4 and later: GPL version 3 with GCC Runtime Library Exception*
GNAT (Part of GCC/MinGW)Ada compiler with the MinGW environment.Cost freeGPL
GNAT AUXDragonLaceCost freeSame as GNAT FSF
GNAT GPLAdaCore > LibreCost freeGPL
GNAT ProAdaCore > GNAT ProNot cost free**GNAT-Modified GPL*

* Permits the distribution of proprietary software.

** You have to contact AdaCore for a Quote. For a hint on the price; this was what I got in a mail December 2011: ".. the supported combination would include both the GNAT Pro Ada development environment and the GtkAda library and tools. The lowest level of subscription for this technology supports a team for up to 5 developers, and is sold 22 950 euros per year."

Explanation of the variants of GNAT in the Debian policy for Ada

Posted by asedt
2012-01-06

Ada basics

Basic data types

Kewords

abort, abs, abstract (Ada 95), accept, access, aliased (Ada 95), all, and, array, at,
begin, body,
case, constant,
declare, delay, delta, digits, do,
else, elsif, end, entry, exception, exit,
for, function,
generic, goto,
if, in, interface (Ada 2005), is,
limited, loop,
mod,
new, not, null,
of, or, others, out, overriding (Ada 2005),
package, pragma, private, procedure, protected (Ada 95),
raise, range, record, rem, renames, requeue (Ada 95), return, reverse,
select, separate, subtype, synchronized (Ada 2005),
tagged (Ada 05), task, terminate, then, type,
until (Ada 95), use,
when, while, with,
xor.

Posted by asedt
2011-11-18

Ada books

You can find a list of Ada materials at: Learning Materials - Ada Resource Association

Ada books from Amazon.com

Free books

Swedish books

Posted by asedt
2011-12-01

Changed by asedt
2011-12-13

Math in Ada

Operators

OperatorDescriptionExampleX
+AdditionX := 3 + 4;7
-SubtractionX := 3 - 4;-1
*MultiplicationX := 3 * 4;12
/Integer DivisionX := 12 / 5;2
/Float DivisionX := 12.0 / 5.0;2.4
**Power ofX := 2 ** 3;2*2*2 = 8
remRemainderX := 12 rem 5;2
modModuloX := 12 mod 5;2
absAbsolute valueX := abs (-5);5

Note that types can't be mixed. Using a float and a integer in the same expression: "1.0 + 1" is not allowed. In that example you can fix it by changing it to: "1.0 + 1.0".

This is because Ada don't have implicit type conversion (coercion, automatic type conversion). Implicit type conversion is available in many other languages, it can be convenient, but also a source of hard to detect bugs and errors.

Explicit type conversion can be used when needed: "1.0 + Float(1)".

Note the difference on float and integer division

rem and mod have the same results when both operands have the same sign if not they differ.

Elementary Functions

Elementary Functions such as sin, cos, log, sqrt etc is available in the Ada.Numerics.Generic_Elementary_Functions package.

If you are using floats you can use the non generic package Ada.Numerics.Elementary_Functions.

Posted by asedt
2011-11-18

How to create programs.

This instructions explains how to compile and run programs in the terminal. You can find a Youtube video here: Ada programming - first time with gnatmake!.

  1. Create the empty text file "hello.adb".
  2. Type or copy the code below to the textfile.
  3. Open terminal.
  4. Make sure you are in the same directory as the "hello.adb" file.
  5. Run "gnatmake hello.adb"
  6. Run "./hello" to run the program.

Code for Hello World! example:

-- hello.adb
-- Prints "Hello world!" to terminal.

with Ada.Text_IO;         use Ada.Text_IO;

procedure hello is

begin
    Put_Line("Hello world!");
        
end hello;
    	

When you run gnatmake it will look like this:

$ gnatmake hello.adb 
gcc-4.4 -c hello.adb
gnatbind -x hello.ali
gnatlink hello.ali
        

When you run the program:

$ ./hello
Hello world!
        

If your using GNAT with MinGW on Windows you can use gnatmake the same way but you get a program with the .exe extension.

> hello.exe
Hello world!        
        

Posted by asedt
2011-11-18

Code Examples

All code examples was tested in GNAT from GCC. You can find manuals and guides for GNAT and GCC at: GCC online documentation

All code on this page is public domain and free to use.

Variables - Integers

-- helloint.adb
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure helloint is
    
    number : Integer := 42;
        
begin
    number := number + 22;
    Put(number, width=>1);

end helloint;
        

Variables - Floats

-- hellofloat.adb
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

procedure hellofloat is
        
    procent : Float := 10.0;
    
begin
    procent := 50.0;
    Put(procent, fore=>1, aft=>2, exp=>0);

end hellofloat;
        

Looping

-- helloloop.adb
with Ada.Text_IO;           use Ada.Text_IO;
with Ada.Integer_Text_IO;   use Ada.Integer_Text_IO;

procedure helloloop is
    
    X : Integer := 1; 
    
begin
    for i in 1..3 loop
        Put("For looping");
        Put(i);
        New_Line;
    end loop;
    
    while X <= 11 loop
        Put("While looping");
        Put(X);
        New_Line;
        X := X + 2;
    end loop;
end helloloop;
        

Command line arguments

-- helloarguments.adb

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line;    use Ada.Command_Line;

procedure helloarguments is

begin
    Put("You are running: ");
    Put(Command_Name);
    New_Line;
    
    if Argument_Count /= 1 then
        Put_Line("Use with one argument.");
    else
        Put(Argument(1));
        New_Line;
        Put(Argument(1)'Length, width=>1);
        New_Line;
    end if;
    
end helloarguments;
        

Functions

-- hellofunction.adb
with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

procedure Hellofunction is
        
    Price    : Float;
    Change   : Float;
    
    NewPrice : Float;
    
    function Change_Price (Price, Change : Float) return Float is
    begin
        return Price * (1.0 + Change);
    end Change_Price; 
    
begin
    Put("Enter price: ");
    Get(Price);
    Put("Enter change: ");
    Get(Change);
    NewPrice := Change_Price(Price, Change);
    Put("The new price is: ");
    Put(NewPrice, fore=>1, aft=>2, exp=>0);

end Hellofunction;
        

Keystrokes

You can use Get_Immediate to get keystrokes. Can be used if you don't whant the input printed in the terminal.

-- keystroke.adb
with Ada.Text_IO; use Ada.Text_IO;

procedure keystroke is
    c : Character;
begin
    loop
        Get_Immediate(c);
        Put(c); -- remove if you don'ẗ whant to see it at the terminal.
        
        if Character'Pos(c) = 27 then -- ESC to exit the loop!
            exit;
        end if;
        
        if c = 'a' then
            Put_Line("You did enter: a");
        end if;    
    end loop;
end keystroke;        
        

Procedures, Elementary functions

I made this program during the online AI class in 2011. It solwes this problem: Homework 8 13 Motion Model Question

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

with Ada.Numerics.Elementary_Functions; 
use Ada.Numerics.Elementary_Functions;

procedure motionmodel is
    X     : Float := 0.0;
    Y     : Float := 0.0;
    Angle : Float := 0.0;
    
    dTime : Float := 4.0;
    V     : Float := 10.0;
    W     : Float := 22.5; -- pi/8
    
    procedure Print (X     : in     Float;
                     Y     : in     Float;
                     Angle : in     Float) is
    begin
        Put("X: ");
        Put(X, fore=>5, aft=>12, exp=>0);
        Put(";  Y: ");
        Put(Y, fore=>5, aft=>12, exp=>0);
        Put(";  Angle: ");
        Put(Angle, fore=>5, aft=>12, exp=>0);
        New_Line;    
    end Print;
    
    procedure Update (X     : in out Float;
                      Y     : in out Float;
                      Angle : in out Float;
                      dTime : in     Float;
                      V     : in     Float;
                      W     : in     Float) is
    begin
        X     := X + V * dTime * Cos(Angle, 360.0);
        Y     := Y + V * dTime * Sin(Angle, 360.0);
        Angle := Angle + W * dTime;    
    end Update;

begin
    Put_Line("Motion model.");
    
    Print(X, Y, Angle);
    
    for i in 1..4 loop
        Update(X, Y, Angle, dTime, V, W);
        Print(X, Y, Angle);
    end loop;
    
end motionmodel;    
    
        

Linear Regression

This program is also inspired from the AI-Class, you can check out unit 5 as a guest on the AI-Class page.

This is the "simple" formula, if you have a more advanced problem I can recommend you to use GNU Octave instead.

-- LinearRegression.adb
-- Aron Knifström
-- Date: 20120424

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;


procedure LinearRegression is

    -- 1 is X value, 2 is Y value.
    type dot is 
        array (Integer range 1..2) of Float;

    type setofdots is
        array (Integer range <>) of dot;

    -- Change input on row below
    Dataset : setofdots(1..4) := ((3.0, 0.0), (6.0, -3.0),
                                  (4.0, -1.0), (5.0, -2.0));
    --Dataset : setofdots(1..3) := ((1.1, 2.1), (2.5, 5.7), (2.9, 5.3));
    
    xsum  : Float := 0.0;
    ysum  : Float := 0.0;
    xysum : Float := 0.0; -- sum of x*y
    xxsum : Float := 0.0; -- sum of x*x
    
    N  : Integer := Dataset'Length;
    Nf : Float := Float(N);
    k  : Float;
    m  : Float;

begin
    Put_Line("Linear Regression");
    Put_Line("Point data is edited directly in source file.");

    for I in Dataset'Range loop
        xsum  := xsum + Dataset(I)(1);
        ysum  := ysum + Dataset(I)(2);
        xysum := xysum + Dataset(I)(1) * Dataset(I)(2);
        xxsum := xxsum + Dataset(I)(1) * Dataset(I)(1);    
    end loop;
    
    k := (Nf * xysum - xsum * ysum) / (Nf * xxsum - xsum * xsum);
    m := 1.0/Nf * ysum - k/Nf * xsum;
    
    Put_Line("Line: y = kx + m");
    Put("Number of dots: ");
    Put(N, width=>1);
    New_Line;
    
    Put("k = ");
    Put(k, fore=>1, aft=>2, exp=>0);
    New_Line;
    
    Put("m = ");
    Put(m, fore=>1, aft=>2, exp=>0);
    New_Line;

end LinearRegression;
        

Posted by asedt
2013-02-06

Problems

1. Input + Product

The program should ask the user to enter a Integer A, and then ask for one other Integer B. It should then print the product of A and B. (the product is A*B).

2. Area of a triangel

Make a program to calculate the Area of a triangle with the Heron's formula. The user should enter the length of each side of the triangle A, B and C as Floats or Integers.

The Heron's formula:
S = 0.5 (A + B + C)
Area = Square root of (S * (S-A) * (S-B) * (S-C))

Optional:
Put in checks to see if the sides are valid and can create a triangle. That is that the values must be positive (larger than 0) and that:
A < B + C
B < A + C
C < A + B

Note to Ada programmers, you can't mix types (Floats and Integers) so use the same type or explicit type conversion if required, I recommend just using Floats. Also to use the sqrt (Square root) function for Floats you need to use Ada.Numerics.Elementary_Functions.

3. Array

Now make a program with an array or list with more than 3 entry's with numbers. Then make the program count how many of the numbers are below zero, zero and above zero.

A array like (1, 0, -2, 12) would give the result one below zero, one zero and two more than zero.

4. PQ

Now the task is to make a program to solve the equation x^2 + px + q = 0, given that we know p and q.

I say that T = (p/2)^2-q
If T is less then zero we want the program to say that we have no reel roots.
If T is zero we want the program to say that we have one double root. That is x = -p/2.
If T is above zero then we should have the both roots as answer. That is first root is x = -p/2 + Square root of T and the second one x = -p/2 - Square root of T.

Posted by asedt
2011-12-16

Graphics with TASH

TASH is an Ada binding to Tcl/Tk. TclAdaShell @ Sourceforge

Latest release:
tash-8.6-0-20110925.zip 2011-09-25 263.2 kB

License is GPL with linking exception* (Simon Wright confirmd that to me 2011-12-16).

* Permits the distribution of proprietary software.

Other tools and librarys for Ada