Integrating Functional Programming Into C++
Total Page:16
File Type:pdf, Size:1020Kb
_________________________________________________________________________Swansea University E-Theses Integrating functional programming into C++. Abdul Rauf, Rose Hafsah Binti How to cite: _________________________________________________________________________ Abdul Rauf, Rose Hafsah Binti (2008) Integrating functional programming into C++.. thesis, Swansea University. http://cronfa.swan.ac.uk/Record/cronfa43069 Use policy: _________________________________________________________________________ This item is brought to you by Swansea University. Any person downloading material is agreeing to abide by the terms of the repository licence: copies of full text items may be used or reproduced in any format or medium, without prior permission for personal research or study, educational or non-commercial purposes only. The copyright for any work remains with the original author unless otherwise specified. The full-text must not be sold in any format or medium without the formal permission of the copyright holder. Permission for multiple reproductions should be obtained from the original author. Authors are personally responsible for adhering to copyright and publisher restrictions when uploading content to the repository. Please link to the metadata record in the Swansea University repository, Cronfa (link given in the citation reference above.) http://www.swansea.ac.uk/library/researchsupport/ris-support/ Integrating Functional Programming Into C++ Rose Hafsah Binti Ab. Rauf BSc. (Malaysia) MSc. (Malaysia) A thesis submitted to the University of Wales in candidature for the degree of Philosophiae Doctor Department of Computer Science University of Wales, Swansea July 2008 ProQuest Number: 10821461 All rights reserved INFORMATION TO ALL USERS The quality of this reproduction is dependent upon the quality of the copy submitted. In the unlikely event that the author did not send a com plete manuscript and there are missing pages, these will be noted. Also, if material had to be removed, a note will indicate the deletion. uest ProQuest 10821461 Published by ProQuest LLC(2018). Copyright of the Dissertation is held by the Author. All rights reserved. This work is protected against unauthorized copying under Title 17, United States C ode Microform Edition © ProQuest LLC. ProQuest LLC. 789 East Eisenhower Parkway P.O. Box 1346 Ann Arbor, Ml 48106- 1346 Summary C++ is a general purpose language that supports object-oriented programming as well as procedural and generic programming, but unfortunately not functional programming. We have developed a parser-translator program that translates simply typed A-term to equiva lent C++ statements so as to integrate functional programming. The program parses A-terms and translate them into full language of C++. Our intention is to upgrade this to an exten sion of the language of C++ by A-types and -terms together with a parser program which translates this extended language into native C++. For this purpose we introduce a syntax for representing A-types and -terms in C++. We use functional style notation rather than overloading existing C++ notation, since we believe that this will improve readability and acceptability of our approach among functional programmers. The translated code generated by the parser-translator program uses the object-oriented ap proach of programming that involves the creation of classes for the A-term. By using inher itance, we achieve that the translation of a A-abstraction is an element of a function type. The most important advantage of our thesis is that we give a mathematical proof of the correctness of the translation, and to our knowledge the verification of the implementation of A-calculus in C++ using a logical relation is new. We introduce a suitable fragment of C++ with a precise denotational semantics. We give a formal translation of A-terms into this fragment and show that it preserves this semantics. We show as well completeness, i.e. essentially all programs in this fragment of C++ can be obtained by translating terms of the A-calculus. We develop a mathematical model for the evaluation of programs in this model, and show that this evaluation is correct with respect to the denotational semantics. We hope that our model of a fragment of C++ which includes a formal model of the heap, will have applications which go beyond the translation of the typed A-calculus. We expect that extensions of this model can be used to verify formally the correctness of more complex C++ programs, including programs with side effects. We believe that if our approach is extended to cover full C++, we obtain a language in which the worlds of functional and object-oriented programming are merged, and that we will see many examples where the combination of both language concepts (such as the use of A-terms with side effects), will result in interesting new programming techniques. Declaration This work has not been previously accepted in substance for any degree and is not being concurrently submitted in candidature for any degree. Signed .... ... (candidate) Date .........L . ^ / 7 / ^ Statement 1 This thesis is the result of my own investigations, except where otherwise stated. Other sources are acknowledged by footnotes giving explicit references. A bibliography is ap pended. Signed (candidate) Date .............................................................. Statement 2 I hereby give my consent for my thesis, if accepted, to be available for photocopying and for inter-library loan, and for the title and summary to be made available to outside organi sations. — Signed (candidate) (o/y / o f Acknowledgements This thesis would not have been possible without the support of many people. I would like to express my heartiest gratitude to both of my supervisors, Dr. Ulrich Berger and Dr. Anton Setzer. They were abundantly helpful and offered invaluable assistance, support and guidance. Without their guidance and knowledge, this thesis will not have been successful. Special thanks also to my colleagues and friends who gave their support and assistance throughout my duration of study. I would also like to convey my thanks to the Department of Computer Science for their cooperation and facilities, and not forgetting to my sponsor, Department of Training and Scholarship, University of Technology MARA, Malaysia for their financial means. I wish to express my love and infinite gratitude to my beloved family especially my husband, Abd. Ghafar and my children, Najwa, Nawal, Nadia, Nazira, Najat, Najla and Abd. Hafiz for their support and sacrifices. Also, my love to my parents for their endless support and love. Finally, I would like to thank every individual that was involved directly and indirectly throughout the process of my thesis. Contents 1 Introduction 1 1.1 A Brief History of Programming Languages ................................................... 2 1.1.1 Development of Low-Level L anguages .............................................. 2 1.1.1.1 First G eneration ................................................................... 3 1.1.1.2 Second Generation ................................................................ 3 1.1.2 Evolution of High Level Languages ................................................... 3 1.1.2.1 Third Generation ................................................................ 3 1.1.2.2 Fourth Generation ................................................................ 5 1.1.2.3 Fifth Generation ................................................................... 6 1.2 Languages Evolved from other Programming L anguages ............................. 7 1.3 Outline of T h e s is ............................................................................................... 8 2 From Imperative Programming to Object-oriented and Generic Programming 10 2.1 Imperative Program m ing .................................................................................. 11 2.1.1 Structured Programming ...................................................................... 13 2.1.2 Sequential C om position ...................................................................... 15 2.1.3 Selection or Alternation ...................................................................... 15 2.1.4 Iteration .................................................................................................. 15 2.1.5 Side Effects ............................................................................................ 16 2.1.6 Aliasing .................................................................................................. 16 2.2 Procedural Programm ing .................................................................................. 18 2.3 Generic Programming ........................................................................................ 20 2.3.1 Templates............................................................................................... 21 2.3.2 C++C oncepts ..................................................................................... 22 2:3.3 Generic Programming Features in Other Languages ......................... 23 2.4 Object-oriented Programming ......................................................................... 24 2.4.1 History of Object-oriented Program m ing ......................................... 24 2.4.2 Concepts in Object-oriented Programming ........................................ 25 2.5 C++ as an Object Oriented Programming Language ...................................... 32 3 A-Calculus and Functional Programming