skip to main content
article
Open Access

Dynamic typing in a statically typed language

Published:01 April 1991Publication History
Skip Abstract Section

Abstract

Statically typed programming languages allow earlier error checking, better enforcement of diciplined programming styles, and the generation of more efficient object code than languages where all type consistency checks are performed at run time. However, even in statically typed languages, there is often the need to deal with datawhose type cannot be determined at compile time. To handle such situations safely, we propose to add a type Dynamic whose values are pairs of a value v and a type tag T where v has the type denoted by T. Instances of Dynamic are built with an explicit tagging construct and inspected with a type safe typecase construct.

This paper explores the syntax, operational semantics, and denotational semantics of a simple language that includes the type Dynamic. We give examples of how dynamically typed values can be used in programming. Then we discuss an operational semantics for our language and obtain a soundness theorem. We present two formulations of the denotational semantics of this language and relate them to the operational semantics. Finally, we consider the implications of polymorphism and some implementation issues.

References

  1. 1 ATKINSON, M. P., AND BUNEMAN, O. P. Types and persistence in database programming languages. Comput. Surv. 19, 2 (June 1987), 105-190. Google ScholarGoogle Scholar
  2. 2 ATKINSON, M. P., AND MORRISON, R. Polymorphic names and iterations. Draft article, Sept. 1987.Google ScholarGoogle Scholar
  3. 3 BARENDREGT, H.P. The Lambda Calculus. Rev. ed. North-Holland, Amsterdam, 1984.Google ScholarGoogle Scholar
  4. 4 BmR~LL, A. D., AND NELSON, B. J. Implementing remote procedure calls. ACM Trans. Comput. Syst. 2, i (Feb. 1984), 39-59. Google ScholarGoogle Scholar
  5. 5 BmTWISTLE, G. M., DAHL, 0. J., MYHRHAUG, B., AND NYGAARD, K. S~mula Begin. Studentlitteratur, Lund, Sweden, 1979.Google ScholarGoogle Scholar
  6. 6 BORRAS, P., CLEMENT, D., DESPEYROUX, T., INCERPI, J., KAHN, G., LANG, B., AND PASCUAL, V. CENTAUR: The system. In Proceedings of the 3rd Annual Symposium on Software Development Environments (SIGSOFT88) (Boston, Mass., Nov.). ACM, New York, 1988. Google ScholarGoogle Scholar
  7. 7 CARDELLI, L. Amber. In Combinators and Functional Programming Languages, G. Cousineau, P. L. Curien, and B. Robinet, Eds. Lecture Notes in Computer Science, vol. 242. Springer-Verlag, New York, 1986. Google ScholarGoogle Scholar
  8. 8 CARDELLI, L., AND MACQUEEN, D. Persistence and type abstraction. In Proceedings of the Persistence and Datatypes Workshop (St. Andrews, Scotland, Aug. 1985). Dept. of Computational Science, Univ. of St. Andrews, 1985 (Persistent Program. Res. Rep. 16).Google ScholarGoogle Scholar
  9. 9 CARDELLI, L., AND WEGNER, P. On understanding types, data abstraction, and polymorphism. Comput. Surv. 17, 4 (Dee. 1985), 471-522. Google ScholarGoogle Scholar
  10. 10 CARDELLI, L., DONAHUE, J., JORDAN, M., KALSOW, B., AND NELSON, G. The Modula-3 type system. In Proceedings of the 16th Annual ACM Symposium on Princ~ples of Programming Languages (Austin, Tex., Jan. 11-13, 1989). ACM, New York, pp. 202-212. Google ScholarGoogle Scholar
  11. 11 CARDELLI, L., DONAHUE, J., GLASSMAN, L., JORDAN, M., KALSOW, B., AND NELSON, G. Modula-3 report (revised). Res. rep. 52, DEC Systems Research Center, Palo Alto, Calif., Nov. 1989.Google ScholarGoogle Scholar
  12. 12 CHURCH, A. A formulation ofthe simple theory of types. J Symbohc Logic 5 (1940), 56-68.Google ScholarGoogle Scholar
  13. 13 CL~MENT, D., DESPEYROUX, J., DESPEYROUX, T., HASCOET, L., AND KAHN, G. Natural semantics on the computer. Tech. Rep. RR 416, INRIA, Sophia-Antipolis June 1985.Google ScholarGoogle Scholar
  14. 14 DESPEYROUX, T. Typoh A formalism to implement natural semanties. Teeh. Rep. 94, INRIA, Mar. 1988.Google ScholarGoogle Scholar
  15. 15 HARPER, R. Introduction fo Standard ML. Teeh. Rep. ECS-LFCS-86-14, Lab. for the Foundations of Computer Science, Edinburgh Univ., Edinburgh, Scotland, Sept. 1986.Google ScholarGoogle Scholar
  16. 16 HINDLEY, J. R., AND SELDIN, J. P. Introduction to Combinators and x-Calculus. London Mathematical Society Student Texts, vol. 1. Cambridge University Press, New York, 1986. Google ScholarGoogle Scholar
  17. 17 KAH~, G. Natural Semantics. Programming of Future Generation Computers, K. Fuchi and M. Nivat, Eds., Elsevier, (North-Holland), Amsterdam, 1988, pp. 237-258. Google ScholarGoogle Scholar
  18. 18 LAMPSON, B. A description of the Cedar language. Tech. Rep. CSL-8345, Xerox Pale Alto Research Center, Palo Alto, Calif., 1983.Google ScholarGoogle Scholar
  19. 19 LISKOV, B., ATKINSON, R., BLOOM, T, MOSS, E., SCHAFFERT, J. C., SCItEIFLER, R. AND SNYDER, A. CLU Reference Manual. Springer-Verlag, New York, 1981. Google ScholarGoogle Scholar
  20. 20 MACQUEEN, D., PLOTKIN, G., AND SETHI, e. An idea} model for recursive polymorphic types. Inf. Control, 71 (1986), 95-130. Google ScholarGoogle Scholar
  21. 21 MARTIN-L"F, P. Intitutionistic Type Theory. Bibliopolis, Naples, 1984.Google ScholarGoogle Scholar
  22. 22 McDONALD, D. B., FAHLMAN, S. E., AND WHOLEY, S. Internal design of CMU Common Lisp un the IBM RT PC. Tech. Rep. CMU-CS-87-157, Carnegie-Mellon Univ., School of Computer Science, Pittsburgh, Pa., Apr. 1988.Google ScholarGoogle Scholar
  23. 23 MILNER, R. A theory of type polymorphism in programming. J. Comput. Syst. Sci. 17 (Aug. 1978), 348-375.Google ScholarGoogle Scholar
  24. 24 MILNER, R., TOFTE, M., AND HARPER, R. The Definition of Standard ML. MIT Press, Cambridge, Mass., 1990. Google ScholarGoogle Scholar
  25. 25 MITCHELL, J., AND PLOTKIN, G. Abstract types have existential type. ACM Trans. Prograin. Lang. Syst. 10, 3 (July 1988), 470-502. Google ScholarGoogle Scholar
  26. 26 MYCROFT, A. Dynamic types in ML. Draft article, 1983.Google ScholarGoogle Scholar
  27. 27 NEWCOMER, J.M. Efficient binary I/O of IDL objects. SIGPLAN Not. (ACM) 22, 11 (Nov. 1987), 35-42. Google ScholarGoogle Scholar
  28. 28 PLOTKIN, G. Call-by-name, call-by-value, and the X-calculus. Theor. Comput. Sci. I (1975), 125-159.Google ScholarGoogle Scholar
  29. 29 PLOTKIN, G.D. A structural approach to operational semantics. Tech. Rep. DAIMI FN-19, Computer Science Dept., Aarhus Univ., Aarhus, Denmark, 1981.Google ScholarGoogle Scholar
  30. 30 RE~NOLDS, J. Three approaches to type structure. In Mathemat~cal Foundatlons of Software Development, H. Ehrig, C Florol, M. Nivat, and J. Thatcher, Eds. Lecture Notes in Computer Science, vol. 185. Springer-Verlag, New York, 1985. Google ScholarGoogle Scholar
  31. 31 ROVNER, P. On extending Modula-2 to build large, integrated systems IEEE Softw. 3, 6 (Nov. 1986), 46-57.Google ScholarGoogle Scholar
  32. 32 SCHAFFERT, J. C. A formal definition of CLU. Master's thesls, MIT, Dept. of Computer Science, Cambridge, Mass., Jan. 1978 (Tech. Rep. MIT/LCS/TR-193).Google ScholarGoogle Scholar
  33. 33 SCHEIFLER, R. W. A denotational semantics of CLU. Master's thesis, MIT, Cambridge, Mass., May 1978 (Tech. Rep. MIT/LCS/TR-201). Google ScholarGoogle Scholar
  34. 34 T~A~TE, S. R. Quasi-static typing (preliminary report), In Proceed~ngs of the 17th ACM Symposium on Principles ofProgramm~ng Languages. ACM, New York, 1990, pp. 367-381. Google ScholarGoogle Scholar
  35. 35 TOFTE, M. Operational semantics and polymorphic type inference. Ph.D. thesis, Computer Science Dept., Edinburgh Univ., Edinburgh, Scotland, 1988 (Tech. Rep. CST-52-88).Google ScholarGoogle Scholar
  36. 36 WEm, P., APO~TE, M.-V., LAWLLE, A., MAUXY, M., A~D SU~REZ, A. The CAML reference manual, Version 2.6. Tech. Rep., Projet Formel, INRIA-ENS, Paris, 1989.Google ScholarGoogle Scholar

Index Terms

  1. Dynamic typing in a statically typed language

      Recommendations

      Reviews

      Ramaswamy Ramanujam

      Checking type correctness of operations often becomes ne cessary at runtime: consider, for example, the read operation. In this paper, the authors propose the use of a data type called Dynamic. Values of this type are pairs—each pair consists of an object and its type. Thus one keeps track of representations of data objects as well as those of their types. The authors introduce a language construct dynamic for packaging a value together with its type into a Dynamic object, and a typecase construct for inspecting the type tag of a given Dynamic object. The language used is basically lambda calculus (simply typed). The paper gives a clear presentation of transition system–style operational semantics, and of ideal model-based denotational semantics. Suggestions are included for extending the theory to polymorphic lambda calculus, and implementation issues are discussed somewhat sketchily. The paper also includes a short history of various attempts to introduce dynamic typing mechanisms. The most important contribution of this paper lies in providing a rigorous and thorough study of an essential programming language feature that has had an informal existence for a long time. This paper should also serve as a guideline to programming language designers on the right questions to ask when introducing a new language construct.

      Access critical reviews of Computing literature here

      Become a reviewer for Computing Reviews.

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      • Published in

        cover image ACM Transactions on Programming Languages and Systems
        ACM Transactions on Programming Languages and Systems  Volume 13, Issue 2
        April 1991
        114 pages
        ISSN:0164-0925
        EISSN:1558-4593
        DOI:10.1145/103135
        Issue’s Table of Contents

        Copyright © 1991 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 1 April 1991
        Published in toplas Volume 13, Issue 2

        Permissions

        Request permissions about this article.

        Request Permissions

        Check for updates

        Author Tags

        Qualifiers

        • article

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader