comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@shell5.ba.best.com>
Subject: Re: Array of Variant Records Question...
Date: 1999/09/09
Date: 1999-09-09T00:00:00+00:00	[thread overview]
Message-ID: <Pine.BSF.4.10.9909091910250.12989-100000@shell5.ba.best.com> (raw)
In-Reply-To: 7r8t21$ov5$1@nnrp1.deja.com

On Thu, 9 Sep 1999, Robert Dewar wrote:
> In article <37d7c116@news1.prserv.net>,
>   "Matthew Heaney" <matthew_heaney@acm.org> wrote:
> > 1) access constant params
> 
> no comment, not exciting ...>

That's new, only exciting features may be proposed!

> > 4) implicit conversion of an access param to a named access
> type
> >
> > 5) implicit conversion of a specific access type to an access
> type that
> > designates an ancestor that is class-wide

Matt, can you give examples where you find that explicit conversions are 
painful, or damage readability?

> > 7) downward closures
> 
> discussed to death already, not worth discussing further ...

Well, if we want to be precise, we should call them "downward funargs",
since Ada already supports a restricted form of downward closure via 
generics. To violate Robert's sentiments even further, I'll append a
little GNAT program which 

(1) Uses downward funargs
(2) Has a fair bit of subprogram nesting
(3) Solves the N Queen problem

so that even if downward funargs aren't dicussable I hit two other threads :-)

Happy 9/9/99 

-- Brian

-- N Queens Problem
--
-- see L.Allison. Continuations implement generators and streams.
--     Computer Journal 33(5) 460-465 1990
--
-- Cont = State -> Answer      where State=List,  Answer=Std_output
-- Generator = Cont -> State -> Answer = Cont -> Cont

with Ada.Text_IO; use  Ada.Text_IO;

procedure Generate is
    package Int_IO is new Ada.Text_IO.Integer_IO(Integer);
    type Node_Type;
    type List_Type is access Node_Type;
    type Node_Type is
        record
            Head : Integer;
            Tail : List_Type;
        end record;

    type Cont_Proc_Type is access procedure (L : in List_Type);
    type Gen_Proc_Type  is access procedure (Cont : Cont_Proc_Type;
                                            L : in List_Type);
    type Pred_Func_Type is access function (L : in List_Type) return Boolean;

    N : Integer;

    function Cons(I : Integer; L : List_Type) return List_Type is
        P:List_Type;
    begin
      P := new Node_Type;
      P.Head := I; P.Tail := L; return P;
    end Cons;

    -- generator "library"
    -- success : Cont

    procedure Success(L : List_Type) is

        procedure Print(L : List_Type) is
        begin
            if L /= null then
                Int_IO.Put(L.Head);
                Print(L.Tail);
            end if;
        end Print;

    begin
        if L /= null then
            New_Line;
            Put(" :");
            Print(L);
        end if;
    end Success;

    -- Choose :Int -> Cont -> Cont = Int -> Generator
    procedure Choose(N : Integer; Cont : Cont_Proc_Type; L : List_Type) is
    begin
        for I in 1..n loop
            Cont( Cons(I, L) );
        end loop;
      -- for each i       continue with i++L
    end Choose;

    -- Filter : (State -> boolean) -> Generator
    procedure Filter(P    : Pred_Func_Type;
                     Cont : Cont_Proc_Type;
                     L    : List_Type) is
    begin
       if P(L) then Cont(L); end if;      -- else fail
       -- if L ok then continue with L else do nothing
    end Filter;

    -- doo = gen**n :Int -> Generator -> Generator
    procedure Doo(N    : Integer;
                  Gen  : Gen_Proc_Type;
                  Cont : Cont_Proc_Type;
                  L    : List_Type ) is

        procedure Gen_Cont(L : List_Type) is
        begin
            Gen(Cont,L);
            -- gen and then cont, to L
        end Gen_Cont;

    begin
        if N = 0 then
            Cont(L);
        else
            Doo(N-1, Gen, Gen_Cont'Unrestricted_Access, L);
        end if;
         -- do (n-1) gen and then [gen and then cont], to L
    end Doo;

    -- n queens proper
    procedure Queen(N : Integer) is
        function Valid(L : List_Type) return Boolean is
            function V(Col : Integer; L2 : List_Type) return Boolean is
            begin
              if L2 = null then
                  return True;                    -- safe
              elsif
                (L.Head = L2.Head)    or      -- check rows
                (L2.Head+Col = L.Head) or      -- & diagonals
                (L2.Head-Col = L.Head)         -- other diags
              then
                  return False;                   -- threat
              else
                  return V(col+1, L2.Tail);
              end if;
            end V;
        begin
            if L = null then
                return True;
            else
                return V(1, L.Tail);
            end if;
        end Valid;

        -- choosevalid :Generator
        procedure Choose_Valid(Cont : Cont_Proc_Type; L : List_Type) is

            procedure Valid_Cont (L : List_Type) is
            begin
                Filter(Valid'Unrestricted_Access, Cont, L);
                -- check valid and if so continue, with L
            end Valid_Cont;

        begin
            Choose(N, Valid_Cont'Unrestricted_Access, L);
            -- choose row and then [check valid and if so continue], with L
        end Choose_Valid;

    begin
        Doo(N,
            Choose_Valid'Unrestricted_Access,
            Success'Unrestricted_Access,
            Null);
          --  [do  n times: choose a valid row] and if so succeed
    end Queen;

begin
    Put_Line("Enter a number and hit return");
    Int_IO.Get(N); Queen(N); New_Line;
    Put_Line("and that's it!");
end;







  reply	other threads:[~1999-09-09  0:00 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-08  0:00 Array of Variant Records Question Bruce Detter
1999-09-08  0:00 ` Matthew Heaney
1999-09-08  0:00   ` Mike Silva
1999-09-08  0:00     ` Matthew Heaney
1999-09-09  0:00       ` Robert Dewar
1999-09-09  0:00         ` Matthew Heaney
1999-09-09  0:00           ` Matthew Heaney
1999-09-09  0:00             ` Mark Lundquist
1999-09-09  0:00             ` Robert Dewar
1999-09-09  0:00           ` Robert Dewar
1999-09-09  0:00             ` Brian Rogoff [this message]
1999-09-13  0:00               ` Matthew Heaney
1999-09-13  0:00                 ` Robert A Duff
1999-09-13  0:00                   ` Matthew Heaney
1999-09-13  0:00                 ` Brian Rogoff
1999-09-14  0:00                   ` Robert Dewar
1999-09-14  0:00                     ` Brian Rogoff
1999-09-14  0:00                   ` Robert Dewar
1999-09-09  0:00             ` Matthew Heaney
1999-09-10  0:00               ` Robert Dewar
1999-09-10  0:00                 ` Mark Lundquist
1999-09-10  0:00                   ` Matthew Heaney
1999-09-11  0:00                     ` Jean-Pierre Rosen
1999-09-14  0:00                     ` "cast away const" (was Re: Array of Variant Records Question...) Mark Lundquist
     [not found]                     ` <wccd7viiv59.fsf@world.std.com>
1999-09-22  0:00                       ` Array of Variant Records Question Robert I. Eachus
     [not found]                       ` <7rrmqd$l89@drn.newsguy.com>
     [not found]                         ` <wcciu59n2uf.fsf@world.std.com>
1999-09-22  0:00                           ` Robert I. Eachus
1999-09-23  0:00                             ` Robert Dewar
1999-09-23  0:00                               ` Robert I. Eachus
1999-09-10  0:00               ` Mark Lundquist
1999-09-10  0:00                 ` Matthew Heaney
1999-09-11  0:00                 ` Robert Dewar
1999-09-11  0:00               ` Richard D Riehle
1999-09-13  0:00                 ` Hyman Rosen
1999-09-14  0:00                 ` Mark Lundquist
     [not found]                   ` <7roohh$s6r@dfw-ixnews7.ix.netcom.com>
     [not found]                     ` <37e01168@news1.prserv.net>
     [not found]                       ` <7rp86o$c6h@dfw-ixnews3.ix.netcom.com>
     [not found]                         ` <37E18CC6.C8D431B@rational.com>
     [not found]                           ` <7rs8bn$s6@dfw-ixnews4.ix.netcom.com>
     [not found]                             ` <wccemfxn15s.fsf@world.std.com>
1999-09-22  0:00                               ` 'constant functions' and access constant params (was Re: Array of Variant Records Question...) Richard D Riehle
     [not found]                             ` <37e2e58c@news1.prserv.net>
1999-09-22  0:00                               ` Richard D Riehle
1999-09-22  0:00                                 ` Mark Lundquist
1999-09-22  0:00                                   ` Mark Lundquist
1999-09-22  0:00                                 ` Matthew Heaney
1999-09-22  0:00                                   ` Richard D Riehle
1999-09-22  0:00                                     ` Matthew Heaney
1999-09-22  0:00                                     ` Matthew Heaney
1999-09-23  0:00                                       ` Vincent Marciante
1999-09-23  0:00                                         ` Matthew Heaney
1999-09-24  0:00                                       ` Robert A Duff
1999-09-25  0:00                                         ` Matthew Heaney
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-27  0:00                                         ` David Kristola
1999-09-23  0:00                                     ` Robert Dewar
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-28  0:00                                         ` Robert Dewar
1999-09-28  0:00                                           ` Richard D Riehle
1999-09-29  0:00                                             ` Robert A Duff
1999-09-29  0:00                                             ` Robert Dewar
1999-09-28  0:00                                         ` Robert Dewar
1999-09-28  0:00                                           ` "Competence" (was: 'constant functions' and access constant params) Ted Dennison
1999-09-28  0:00                                             ` Robert Dewar
1999-09-10  0:00             ` Proposed Ada features (was Re: Array of Variant Records Question...) Mark Lundquist
1999-09-10  0:00               ` Matthew Heaney
1999-09-10  0:00                 ` tmoran
1999-09-09  0:00     ` Array of Variant Records Question Nick Roberts
1999-09-09  0:00       ` Tucker Taft
1999-09-10  0:00         ` Nick Roberts
1999-09-09  0:00       ` Robert Dewar
1999-09-08  0:00 ` Ted Dennison
1999-09-08  0:00 ` Thank you Bruce Detter
1999-09-08  0:00   ` Martin C. Carlisle
1999-09-08  0:00 ` Array of Variant Records Question Martin C. Carlisle
replies disabled

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