[comp.lang.ada] Dynamic Typing in Ada

linnig@skvax1.csc.ti.com (Mike Linnig) (03/11/89)

John Gateley's (Gateley@tilde.csc.ti.com) challenge was to implement the
following lisp construct in Ada...

 ----------------------------------------------------------------------
-- Scheme                           CL
-- (define print                    (defun print (x)
--   (lambda (x)                      (typecase x
--     (cond                            (integer 1)
--       ((integer? x) 1)               (real 2)
--       ((real? x) 2)                  (complex 3)
--       ((complex? x) 3)               (vector 4)))
--       ((vector? x) 4))))
 ----------------------------------------------------------------------
package dynamic_types is

  TYPE real IS NEW integer;             -- not how we would really do it
  TYPE complex IS NEW integer;          -- but good enuf for an example.
  TYPE vector IS NEW integer;

  Type Dynamic_Type_options is (integer_t,real_t,complex_t,vector_t);

  type Dynamic_Type(which: Dynamic_Type_options := integer_t) IS record
    CASE which IS
      WHEN integer_t    => I: integer;
      WHEN real_t       => R: real;
      WHEN complex_t    => C: complex;
      WHEN vector_t     => V: vector;
    END CASE;
  END RECORD;

 FUNCTION print(x: Dynamic_Type) return integer;
END dynamic_types;
 ----------------------------------------------------------------------
WITH text_io;
PACKAGE BODY  dynamic_types IS
   
   FUNCTION print(x: Dynamic_Type) return integer is
   BEGIN
      CASE x.which IS
         WHEN integer_t   => return (1);
         WHEN real_t      => return (2);
         WHEN complex_t   => return (3);
         WHEN vector_t    => return (4);
      END CASE;
   END print;  
      
END Dynamic_Types;
 ----------------------------------------------------------------------
WITH dynamic_types,text_io;
USE  dynamic_types;

PROCEDURE lisp_test IS
  PACKAGE int_io IS NEW text_io.integer_io(integer); -- I hate this.

  tmp1 : dynamic_type := (which=>real_t, r=> 3); -- init to real type
  
BEGIN
    text_io.put("the type number of tmp1 is :");
    int_io.put(print(tmp1)); 
    text_io.new_line;

    tmp1 := (which=>complex_t, c => 99);

    text_io.put("the NEW type number of tmp1 is :");
    int_io.put(print(tmp1)); 
    text_io.new_line;

END lisp_test;
 ----------------------- SAMPLE OUTPUT ---------------------------
the type number of tmp1 is :          2
the NEW type number of tmp1 is :          3
 ----------------------------------------------------------------------

I think this answers John Gateley's (Gateley@tilde.csc.ti.com) dynamic
typing challenge. I didn't understand the first challenge; if it involved
something similar to The C Language construct allowing pointers to
procedures (and calling them) that can be handled too.

	Mike Linnig,
	Texas Instruments

Geez, we work for the same company!

mark@aoa.UUCP (Som Hane) (03/20/89)

In article <8903101703.AA04518@ti.com> linnig@skvax1.csc.ti.com (Mike Linnig) writes:

>John Gateley's (Gateley@tilde.csc.ti.com) challenge was to implement the
>following lisp construct in Ada...
>
> ----------------------------------------------------------------------
>-- Scheme                           CL
>-- (define print                    (defun print (x)
>--   (lambda (x)                      (typecase x
>--     (cond                            (integer 1)
>--       ((integer? x) 1)               (real 2)
>--       ((real? x) 2)                  (complex 3)
>--       ((complex? x) 3)               (vector 4)))
>--       ((vector? x) 4))))
> ----------------------------------------------------------------------
>package dynamic_types is
>
> [ Ada code deleted ]
>
>I think this answers John Gateley's (Gateley@tilde.csc.ti.com) dynamic
>typing challenge. I didn't understand the first challenge; if it involved
>something similar to The C Language construct allowing pointers to
>procedures (and calling them) that can be handled too.
>

Let me start off by saying that I am a Lisp ( and C ) programmer, and have
only been doing Ada for 1 year, so I may be totally off base, but... I
think you may have missed the point.  In the CL example above the type of
"x" could be set or changed dynamically;  in your Ada example it was ( and
had to be ? ) set at compile time.  In addition, in the CL example the range
of choices in the typecase itself could be modified dynamically to be the
members of an arbitrary set of types ( some of which could themselves have
been dynamically created ).  I do not see at all how your example can be
modified to do any of these things... so I think the challenge has not been
met.

	Mark Reynolds

	...!BBN.COM!bbn!aoa!mark
	mark%aoa.uucp@BBN.COM

scott@shuksan.UUCP (Scott Moody) (03/25/89)

I think the real issue is not to make Ada into something it
isn't. It isn't the greatest dynamic prototyping language,
but can still be used to prototype. 
Aside from the Ada Guru test of providing in Ada similar 
dynamic capabilities that other languages offer, I think we
are missing the real issue of "design" and "requirements". 

Once the prototyper has developed a nifty lisp/smalltalk/ai program, take the
"design/requirements" that the prototype generated 
and code it up in your favorite type safe language (Ada). 
A "design/requirements" shouldn't rely on dynamic typing 
(but an implementation may use these feature especially in a prototype).

-- Scott Moody