comp.lang.ada
 help / color / mirror / Atom feed
* generic with function procedure
@ 2020-06-05 14:35 Gilbert Gosseyn
  2020-06-05 14:52 ` gautier_niouzes
  2020-06-09 16:44 ` Shark8
  0 siblings, 2 replies; 12+ messages in thread
From: Gilbert Gosseyn @ 2020-06-05 14:35 UTC (permalink / raw)


Hi,
I would like to code the nelder-mead algorithm in Ada to solve the following problem:
-- minimize f(x)
-- subject to:  g(j,x) <= 0.0 for j in 1..q
--                     h(j,x) = 0.0   for j in q+1..n

My definition for the solving procedure is:

generic
with function f(v : Real_Vector) return Real;
with function g(j : Integer; v : Real_Vector) return Real;
with function h(j : Integer; v : Real_Vector) return Real;
procedure denm (np,q,n,m : Integer);

However when I try to instantiate after having defined f1,g1,h1 like

procedure nm is new denm(f=>f1,g=>g1,h=>h1);

the message is: no visible subprogram matches the specification for "g"
                           no visible subprogram matches the specification for "h"

It therefor seems to be OK for "f=>f1" only.

How to make it working?


         

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

* Re: generic with function procedure
  2020-06-05 14:35 generic with function procedure Gilbert Gosseyn
@ 2020-06-05 14:52 ` gautier_niouzes
  2020-06-05 15:45   ` Gilbert Gosseyn
  2020-06-09 16:44 ` Shark8
  1 sibling, 1 reply; 12+ messages in thread
From: gautier_niouzes @ 2020-06-05 14:52 UTC (permalink / raw)


It depends on how you have defined g1 and h1...
You may also want to pass Real as a generic parameter to make your "denm" package work for any precision. Perhaps doing so will solve your problem by the way.
Here is a package from Mathpaqs, as an inspiration.

with Ada.Numerics.Generic_Real_Arrays;

generic
  type Real is digits <>;
  with package Real_Arrays is new Ada.Numerics.Generic_Real_Arrays (Real);
  type Integer_Vector is array (Integer range <>) of Integer;

package Generic_Real_Linear_Equations is

  use Real_Arrays;

  function Linear_Equations ( A : Real_Matrix ;
                              Y : Real_Vector ) return Real_Vector ;
...

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

* Re: generic with function procedure
  2020-06-05 14:52 ` gautier_niouzes
@ 2020-06-05 15:45   ` Gilbert Gosseyn
  2020-06-05 15:59     ` Dmitry A. Kazakov
  2020-06-05 20:28     ` Jeffrey R. Carter
  0 siblings, 2 replies; 12+ messages in thread
From: Gilbert Gosseyn @ 2020-06-05 15:45 UTC (permalink / raw)


On Friday, June 5, 2020 at 4:52:13 PM UTC+2, gautier...@hotmail.com wrote:
> It depends on how you have defined g1 and h1...
> You may also want to pass Real as a generic parameter to make your "denm" package work for any precision. Perhaps doing so will solve your problem by the way.
> Here is a package from Mathpaqs, as an inspiration.
> 
> with Ada.Numerics.Generic_Real_Arrays;
> 
> generic
>   type Real is digits <>;
>   with package Real_Arrays is new Ada.Numerics.Generic_Real_Arrays (Real);
>   type Integer_Vector is array (Integer range <>) of Integer;
> 
> package Generic_Real_Linear_Equations is
> 
>   use Real_Arrays;
> 
>   function Linear_Equations ( A : Real_Matrix ;
>                               Y : Real_Vector ) return Real_Vector ;
> ...

I used already:
type Real is digits 18;
package Real_Arrays is instantiated with type Real from above.

Please explain: "It depends on how you have defined g1 and h1...". The functions g1 and h1 are defined in the same manner a f1. If I restrict the numer of with-clauses to one, then there is no error-message.

Thank you for your example. I am still studying it and I can see that it uses different methods in seperate programs with usual parameters. However, there are no functions as parameters involved. As in Ada you cannot use  functions as parameters eg in a procedure, the with clause may be used. My problem is still how to use the with clause correctly?

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

* Re: generic with function procedure
  2020-06-05 15:45   ` Gilbert Gosseyn
@ 2020-06-05 15:59     ` Dmitry A. Kazakov
  2020-06-05 16:09       ` Anh Vo
  2020-06-05 20:28     ` Jeffrey R. Carter
  1 sibling, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2020-06-05 15:59 UTC (permalink / raw)


On 05/06/2020 17:45, Gilbert Gosseyn wrote:

> Please explain: "It depends on how you have defined g1 and h1...". The functions g1 and h1 are defined in the same manner a f1.

Surely

   function f (v : Real_Vector) return Real;

will never match

   function g (j : Integer; v : Real_Vector) return Real;

P.S. Always post compile-ready code.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: generic with function procedure
  2020-06-05 15:59     ` Dmitry A. Kazakov
@ 2020-06-05 16:09       ` Anh Vo
  0 siblings, 0 replies; 12+ messages in thread
From: Anh Vo @ 2020-06-05 16:09 UTC (permalink / raw)


On Friday, June 5, 2020 at 8:59:14 AM UTC-7, Dmitry A. Kazakov wrote:
> On 05/06/2020 17:45, Gilbert Gosseyn wrote:
> 
> > Please explain: "It depends on how you have defined g1 and h1...". The functions g1 and h1 are defined in the same manner a f1.
> 
> Surely
> 
>    function f (v : Real_Vector) return Real;
> 
> will never match
> 
>    function g (j : Integer; v : Real_Vector) return Real;
> 
> P.S. Always post compile-ready code.

Agree totally. Usually, there will be a faster reply when compile-ready code is posted.

Anh Vo

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

* Re: generic with function procedure
  2020-06-05 15:45   ` Gilbert Gosseyn
  2020-06-05 15:59     ` Dmitry A. Kazakov
@ 2020-06-05 20:28     ` Jeffrey R. Carter
  1 sibling, 0 replies; 12+ messages in thread
From: Jeffrey R. Carter @ 2020-06-05 20:28 UTC (permalink / raw)


On 6/5/20 5:45 PM, Gilbert Gosseyn wrote:
> 
> Please explain: "It depends on how you have defined g1 and h1...".

It means that unless you show us your complete attempt to perform the 
instantiation, we can't tell why you get these msgs.

The most common reasons for the error msg you got are

* G1 is not directly visible
* G1 does not match G

-- 
Jeff Carter
"Sheriff murdered, crops burned, stores looted,
people stampeded, and cattle raped."
Blazing Saddles
35

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

* Re: generic with function procedure
  2020-06-05 14:35 generic with function procedure Gilbert Gosseyn
  2020-06-05 14:52 ` gautier_niouzes
@ 2020-06-09 16:44 ` Shark8
  2020-06-10  8:21   ` Gilbert Gosseyn
  1 sibling, 1 reply; 12+ messages in thread
From: Shark8 @ 2020-06-09 16:44 UTC (permalink / raw)


On Friday, June 5, 2020 at 8:35:34 AM UTC-6, Gilbert Gosseyn wrote:
> 
> the message is: no visible subprogram matches the specification for "g"
>                            no visible subprogram matches the specification for "h"

Well, first, let's look at some generic code:
 Generic
   Type Element is private;
   Type Index   is (<>);
   Type Vector  is Array( Index range <> ) of Element;
   Zero : Element;
   with Function "+"( Left, Right : Element ) return Element is <>;
 Function Generic_Sum( Input : Vector ) return Element;
 Function Generic_Sum( Input : Vector ) return Element is
   Return Result : Element := Zero do
     For Item of Input Loop
       Result:= Result + Item;
     End Loop;
   End Return;
 End Generic_Sum;

Now, if we say something like:
  Package Example_1 is
     Type X is private;
     Type Y is range 1..7;
     Type Vector is Array(Y range <>) of X;

     Function "+"( Right : Vector ) return Element;
     Default : Constant X;
  Private
     Function Sum is new Generic_Sum(X,Y,Vector, Zero => Default, others => <>);
     Type X is range 0..15; -- 4-bits.
     Function "+"( Right : Vector ) return Element
        renames Sum;
     Default : Constant X := 0;
  End Example_1;

...doesn't work. Why?
Well, for one, the point where you're trying to instantiate Generic_Sum into Sum, even though the parameter (X) matches the formal parameter, at this point in the code the only thing we know is that it's a private type... it doesn't have a "+" function that it can see, and so it will tell you that there's nothing to match with function "+".

Swapping the "function Sum is" and "type X is" lines would solve that problem, but there *might* be another, similar one: the constant (Default) hasn't yet been assigned a value and so the instantiation might fail. (I'd have to re-read the ARM,)

In your post the line "procedure nm is new denm(f=>f1,g=>g1,h=>h1);" is analogous to the situation above, albeit slightly different: you're explicitly specifying that subprograms "g1" and "h1" are to be respectively associated with parameters "g" and "h" -- this is all well and good, and the compiler has caught two mistakes for you:
— no visible subprogram matches the specification for "g"
— no visible subprogram matches the specification for "h" 

So it's telling you that of all the functions "g1" that it sees at the point of instantiation, none match the generic's specification of "function g(j : Integer; v : Real_Vector) return Real;"
(and the same for "h".)

Now, at this point the compiler cannot do anything more: this is the realm where only the human programmer can tell what to do:
— Is it a copy and paste-error creating the functions "g" and "h"? (Were they declared in the form of "f" which is "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"?)
— Did you perhaps mean to make the formal parameters of denm, "g" and "h", to be "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"? (Perhaps being interrupted while refactoring it, and it's stuck in a form intermediate between what you had and what you want.)
— Did you forget to make a "g1" and "h1"?

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

* Re: generic with function procedure
  2020-06-09 16:44 ` Shark8
@ 2020-06-10  8:21   ` Gilbert Gosseyn
  2020-06-10  8:45     ` AdaMagica
                       ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Gilbert Gosseyn @ 2020-06-10  8:21 UTC (permalink / raw)


On Tuesday, June 9, 2020 at 6:44:13 PM UTC+2, Shark8 wrote:
> On Friday, June 5, 2020 at 8:35:34 AM UTC-6, Gilbert Gosseyn wrote:
> > 
> > the message is: no visible subprogram matches the specification for "g"
> >                            no visible subprogram matches the specification for "h"
> 
> Well, first, let's look at some generic code:
>  Generic
>    Type Element is private;
>    Type Index   is (<>);
>    Type Vector  is Array( Index range <> ) of Element;
>    Zero : Element;
>    with Function "+"( Left, Right : Element ) return Element is <>;
>  Function Generic_Sum( Input : Vector ) return Element;
>  Function Generic_Sum( Input : Vector ) return Element is
>    Return Result : Element := Zero do
>      For Item of Input Loop
>        Result:= Result + Item;
>      End Loop;
>    End Return;
>  End Generic_Sum;
> 
> Now, if we say something like:
>   Package Example_1 is
>      Type X is private;
>      Type Y is range 1..7;
>      Type Vector is Array(Y range <>) of X;
> 
>      Function "+"( Right : Vector ) return Element;
>      Default : Constant X;
>   Private
>      Function Sum is new Generic_Sum(X,Y,Vector, Zero => Default, others => <>);
>      Type X is range 0..15; -- 4-bits.
>      Function "+"( Right : Vector ) return Element
>         renames Sum;
>      Default : Constant X := 0;
>   End Example_1;
> 
> ...doesn't work. Why?
> Well, for one, the point where you're trying to instantiate Generic_Sum into Sum, even though the parameter (X) matches the formal parameter, at this point in the code the only thing we know is that it's a private type... it doesn't have a "+" function that it can see, and so it will tell you that there's nothing to match with function "+".
> 
> Swapping the "function Sum is" and "type X is" lines would solve that problem, but there *might* be another, similar one: the constant (Default) hasn't yet been assigned a value and so the instantiation might fail. (I'd have to re-read the ARM,)
> 
> In your post the line "procedure nm is new denm(f=>f1,g=>g1,h=>h1);" is analogous to the situation above, albeit slightly different: you're explicitly specifying that subprograms "g1" and "h1" are to be respectively associated with parameters "g" and "h" -- this is all well and good, and the compiler has caught two mistakes for you:
> — no visible subprogram matches the specification for "g"
> — no visible subprogram matches the specification for "h" 
> 
> So it's telling you that of all the functions "g1" that it sees at the point of instantiation, none match the generic's specification of "function g(j : Integer; v : Real_Vector) return Real;"
> (and the same for "h".)
> 
> Now, at this point the compiler cannot do anything more: this is the realm where only the human programmer can tell what to do:
> — Is it a copy and paste-error creating the functions "g" and "h"? (Were they declared in the form of "f" which is "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"?)
> — Did you perhaps mean to make the formal parameters of denm, "g" and "h", to be "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"? (Perhaps being interrupted while refactoring it, and it's stuck in a form intermediate between what you had and what you want.)
> — Did you forget to make a "g1" and "h1"?

Apparently the problem is solved when I make the parameter list for f, g, and h the same. (it must be a well known fact when using more than one with. However, I missed it). Thanks to all contributors.

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

* Re: generic with function procedure
  2020-06-10  8:21   ` Gilbert Gosseyn
@ 2020-06-10  8:45     ` AdaMagica
  2020-06-12 14:29     ` Shark8
  2020-06-12 16:45     ` Simon Wright
  2 siblings, 0 replies; 12+ messages in thread
From: AdaMagica @ 2020-06-10  8:45 UTC (permalink / raw)


Am Mittwoch, 10. Juni 2020 10:21:13 UTC+2 schrieb Gilbert Gosseyn:
> Apparently the problem is solved when I make the parameter list for f, g, and h the same. (it must be a well known fact when using more than one with. However, I missed it). Thanks to all contributors.

What do want to say with this? Of course the parameter and return profiles of generic parameter subprograms need not be the same.

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

* Re: generic with function procedure
  2020-06-10  8:21   ` Gilbert Gosseyn
  2020-06-10  8:45     ` AdaMagica
@ 2020-06-12 14:29     ` Shark8
  2020-06-12 16:45     ` Simon Wright
  2 siblings, 0 replies; 12+ messages in thread
From: Shark8 @ 2020-06-12 14:29 UTC (permalink / raw)


On Wednesday, June 10, 2020 at 2:21:13 AM UTC-6, Gilbert Gosseyn wrote:
> On Tuesday, June 9, 2020 at 6:44:13 PM UTC+2, Shark8 wrote:
> > Now, at this point the compiler cannot do anything more: this is the realm where only the human programmer can tell what to do:
> > — Is it a copy and paste-error creating the functions "g" and "h"? (Were they declared in the form of "f" which is "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"?)
> > — Did you perhaps mean to make the formal parameters of denm, "g" and "h", to be "(v : Real_Vector) return Real;" instead of "(j : Integer; v : Real_Vector) return Real;"? (Perhaps being interrupted while refactoring it, and it's stuck in a form intermediate between what you had and what you want.)
> > — Did you forget to make a "g1" and "h1"?
> 
> Apparently the problem is solved when I make the parameter list for f, g, and h the same.
But is it?
Take a look at the first point I raised:
* Is it a copy and paste-error creating the functions "g" and "h"?

IOW, which is appropriate? A function with signature "(v : Real_Vector) return Real;", or a function with signature "(j : Integer; v : Real_Vector) return Real;"?

> It must be a well known fact when using more than one with. However, I missed it.
It doesn't really have to do with WITH, but with (a) the generic's formal declaration, and (b) the functions visible at instantiation... WITH is only relevant here when using it to pull compilation units into visibility, but even that can be blocked/overriden (e.g.) by locally declaring a function of the same name.

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

* Re: generic with function procedure
  2020-06-10  8:21   ` Gilbert Gosseyn
  2020-06-10  8:45     ` AdaMagica
  2020-06-12 14:29     ` Shark8
@ 2020-06-12 16:45     ` Simon Wright
  2020-06-12 18:49       ` Simon Wright
  2 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2020-06-12 16:45 UTC (permalink / raw)


Gilbert Gosseyn <hnptz@yahoo.de> writes:

> Apparently the problem is solved when I make the parameter list for f,
> g, and h the same. (it must be a well known fact when using more than
> one with. However, I missed it). Thanks to all contributors.

If you'd posted this question on the [ada] tag on StackOverflow, we'd
have probably closed the question because you haven't shown us the code
that you couldn't get to work!

Show us the original failing declarations of F, G, and H and then we can
tell you more.

It is most definitely *not* a "well known fact" that the parameter lists
of all subprogram arguments to a generic have to be the same. In fact,
that suggestion is completely false.

Simple compiling demo:

procedure Vogt is
   subtype Real is Float;
   type Real_Vector is array (1 .. 10) of Real;

   generic
      with function F(V : Real_Vector) return Real;
      with function G(J : Integer; V : Real_Vector) return Real;
      with function H(J : Integer; V : Real_Vector) return Real;
   procedure Denm (Np,Q,N,M : Integer);

   procedure Denm (Np,Q,N,M : Integer) is null;

   function F1 (V : Real_Vector) return Real is (42.0);
   function G1 (J : Integer; V : Real_Vector) return Real is (42.0);
   function H1 (J : Integer; V : Real_Vector) return Real is (42.0);

   procedure Nm is new Denm (F => F1, G => G1, H => H1);
begin
   null;
end Vogt;

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

* Re: generic with function procedure
  2020-06-12 16:45     ` Simon Wright
@ 2020-06-12 18:49       ` Simon Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Simon Wright @ 2020-06-12 18:49 UTC (permalink / raw)


Simon Wright <simon@pushface.org> writes:

> Show us the original failing declarations of F, G, and H and then we
> can tell you more.

Of course I mean F1, G1, H1.

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

end of thread, other threads:[~2020-06-12 18:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 14:35 generic with function procedure Gilbert Gosseyn
2020-06-05 14:52 ` gautier_niouzes
2020-06-05 15:45   ` Gilbert Gosseyn
2020-06-05 15:59     ` Dmitry A. Kazakov
2020-06-05 16:09       ` Anh Vo
2020-06-05 20:28     ` Jeffrey R. Carter
2020-06-09 16:44 ` Shark8
2020-06-10  8:21   ` Gilbert Gosseyn
2020-06-10  8:45     ` AdaMagica
2020-06-12 14:29     ` Shark8
2020-06-12 16:45     ` Simon Wright
2020-06-12 18:49       ` Simon Wright

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