comp.lang.ada
 help / color / mirror / Atom feed
* Dynamic_Predicate  with a generic
@ 2012-10-31  3:58 Bill Findlay
  2012-10-31  4:31 ` Shark8
  0 siblings, 1 reply; 2+ messages in thread
From: Bill Findlay @ 2012-10-31  3:58 UTC (permalink / raw)


The following code compiles:
> 
>    type integral          is new Integer;
>    type unsigned_integral is mod 2**32;
> 
>    subtype nat_integral is integral range 0 .. integral'Last;
>    subtype pos_integral is integral range 1 .. integral'Last;
> 
>    -- returns True iff nr is a prime number
>    function is_prime (nr : integral) return Boolean;
> 
>    subtype prime_number is integral
>       with Dynamic_Predicate => is_prime(prime_number);
> 

But if the types integral and unsigned_integral are given by generic type
parameters, thus:

> generic
>    type integral          is range <>;
>    type unsigned_integral is mod <>;
> package prime_numbers is
> 
>     pragma Elaborate_Body;
> 
>    subtype nat_integral is integral range 0 .. integral'Last;
>    subtype pos_integral is integral range 1 .. integral'Last;
> 
>    -- returns True iff nr is a prime number
>    function is_prime (nr : integral) return Boolean;
> 
>    subtype prime_number is integral
>       with Dynamic_Predicate => is_prime(prime_number);

I get the error message:

> 
> ==============Error messages for source file: prime_numbers.ads
>     20.       with Dynamic_Predicate => is_prime(prime_number);
>                                                  |
>>>> invalid use of subtype mark in expression or call

What am I missing?

-- 
Bill Findlay
with blueyonder.co.uk;
use  surname & forename;





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

* Re: Dynamic_Predicate  with a generic
  2012-10-31  3:58 Dynamic_Predicate with a generic Bill Findlay
@ 2012-10-31  4:31 ` Shark8
  0 siblings, 0 replies; 2+ messages in thread
From: Shark8 @ 2012-10-31  4:31 UTC (permalink / raw)


> >
> > ==============Error messages for source file: prime_numbers.ads
> >     20.       with Dynamic_Predicate => is_prime(prime_number);
> >                                                  |
> >>>> invalid use of subtype mark in expression or call
> 
> What am I missing?

Maybe nothing, I recently found two bugs one where 
declare
 item : a_type := ...;
begin
 Operation( New a_type'( item) );
end 
gave results you would expect from using 'unchecked_access.
and another where LRM 6.5 isn't implemented (Aliased extended returns).

I put the package into the declaration area of a procedure and it compiled fine.

    generic
	type integral          is range <>;
	type unsigned_integral is mod <>;
    package prime_numbers with Elaborate_Body is

	subtype nat_integral is integral range 0 .. integral'Last;
	subtype pos_integral is integral range 1 .. integral'Last;

	-- returns True iff nr is a prime number
	function is_prime (nr : integral) return Boolean;

	subtype prime_number is integral
	with Dynamic_Predicate => is_prime(prime_number); 
    end prime_numbers;

    package body prime_numbers is
	function is_prime (nr : integral) return Boolean is
	begin
	    Return Result : Boolean := Integer(nr) in positive do
		if Result then
		    case pos_integral(nr) is
		    when 1 | 2 | 3 => null;
		    when Others =>
			declare
			    subtype Mod_Search is integral range 2..nr-2;
			begin
			    for Index in mod_search loop
				if nr mod Index = 0 then
				    Result := False;
				    return;
				end if;
			    end loop;
			end;
		    end case;
		end if;
	    end return;
	end is_prime;
    end prime_numbers;

    Type mod_13 is mod 13;
    Package P is new Prime_numbers( integral => Integer, unsigned_integral => mod_13);



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

end of thread, other threads:[~2012-10-31  4:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-31  3:58 Dynamic_Predicate with a generic Bill Findlay
2012-10-31  4:31 ` Shark8

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