Functional Programming Library for C++
- PDF / 304,825 Bytes
- 11 Pages / 612 x 792 pts (letter) Page_size
- 11 Downloads / 270 Views
nctional Programming Library for C++ M. M. Krasnov Keldysh Institute of Applied Mathematics, Russian Academy of Sciences, Moscow, 125047 Russia e-mail: [email protected] Received July 26, 2019; revised July 30, 2019; accepted November 8, 2019
Abstract—Modern functional programming languages, such as Haskell, Scala, ML, and F#, have properties that make it possible to implement logically complicated algorithms relatively easily. Among such properties is the composition of functions, currying, metafunctions (functions over functions), and some others. This makes it possible to obtain complex functions by combining simpler functions. An example of a complicated algorithm is a parser. In Haskell, there is a library called Parsec, which is a set of elementary parsers; by combining these parsers, one can create more complicated parsers. This fact makes the library relatively simple, yet powerful. The majority of these tools are not explicitly included in C++. However, C++ is sufficiently powerful for implementing many properties of functional programming languages. In this paper, an attempt is made to develop such a library. DOI: 10.1134/S0361768820050047
1. INTRODUCTION The decision to develop the functional programming library funcprog was inspired by studying the language Haskell [1]. Its powerful system of types, functional “purity, laziness,” infinite lists, support of function currying and function composition, monads and monad transformers, and many other features make this language a powerful tool for writing logically complicated algorithms. Other modern functional programming languages (Scala [2], ML [3], and F# [4]) also have similar properties. When I learned Haskell, I tried to understand how it can be used to easily implement a parser of mathematical formulas (calculator). It turned out that a general purpose parser is already included in the language library, and implementing a formula parser was an easy task. However, the main language used by many mathematicians, including the author of this paper, for performing everyday tasks (mainly for implementing numerical methods in fluid dynamics) is C++. In such problems, logically complicated algorithms are also common (e.g., formula parser), and it would be attractive to use the capabilities of functional programming languages for implementing such algorithms. However, it is difficult to combine two languages in the same program. Hence, the desire to remain within C++ and simultaneously write some modules in the style of Haskell using many its features, including function currying, composition of functions, monads, lazy evaluation, etc. In essence, we want to implement a functional domain-specific language (DSL) as a library of classes and functions in C++.
The main feature of all functional languages is that a function in them is a full participant of computations; i.e., a function can be passed as a function parameter and returned as a result of computations. In particular, all functional programming languages include the concept of lambda expression, the result
Data Loading...