Is functional programming more "mathematical"? If so, why?

From time to time, I hear someone say things like "functional programming languages ​​are more mathematical." This is true? If so, why and how? Is, for example, a schema more mathematical than Java or C? Or Haskell?

I can’t determine exactly what “math” is, but I believe that you can feel it.

Thanks!

+4
source share
5 answers

There are two general (*) calculation models : the Lambda Calculus (LC) and the Turing Model (TM) model.

Lambda Calculus approaches computation by representing it using a mathematical formalism in which the results are produced through the composition of functions over the type domain. LC is also associated with Combinatory Logic , which is considered a more generalized approach to the same topic.

The Turing Machine model approaches computation, representing it as a manipulation of characters stored in an idealized repository using a number of basic operations (such as addition, mutation, etc.).

These various computational models are the basis for different families of programming languages. Lambda calculus led to the emergence of languages ​​such as ML , Scheme, and Haskell . The Turing model led to C , C ++ , Pascal and others. As a generalization, most functional programming has a theoretical basis in lambda calculus.

Due to the nature of the Lambda calculus, some evidence of the behavior of systems based on its principles is possible. In fact, provability (i.e., correctness ) is an important concept in LC and makes certain kinds of reasoning and conclusions about LC systems possible. LC is also associated with type theory and category theory (and relies).

In contrast, Turing models rely less on type theory and more on structuring computations as a series of state transitions in the base model. Turing Machine calculation models are more difficult to make statements and not succumb to similar mathematical proofs and manipulations that make programs based on LC. However, this does not mean that such an analysis is impossible - some important aspects of TM models are used in the study of virtualization and static analysis of programs.

Since functional programming is based on careful selection of types and conversion between types, FP can be thought of as more “mathematical."

(*) There are other calculation models, but they are less relevant for this discussion.

+9
source

Pure functional programming languages ​​are examples of functional calculus, and therefore theoretically programs written in a functional language can be justified in a mathematical sense. Ideally, you would like to "prove" that the program is correct.

In practice, this reasoning is very difficult, except in trivial cases, but it is still possible to some extent. You may be able to prove certain properties of the program, for example, you could prove that, given all the numerical inputs to the program, the output is always limited in a certain range.

In non-functional languages ​​with a volatile state and side effects, attempts to talk about the program and “prove” the correctness are practically impossible, at least at the moment. With non-functional programs, you can think through a program and convince yourself that parts of it are correct, and you can run unit tests that test specific inputs, but it is usually impossible to build rigorous mathematical evidence about the program’s behavior.

+8
source

I think that one of the main reasons is that pure functional languages ​​do not have side effects, that is, there is no mutable state, they only display input parameters for the result values, which is a mathematical function.

+4
source

The logical structures of functional programming are largely based on lambda calculus . Although it may not be mathematical, based solely on algebraic forms of mathematics, he easily writes from discrete mathematics .

Unlike imperative programming, it does not establish exactly how to do something, but what needs to be done. This reflects the topology.

0
source

The mathematical perception of functional programming languages ​​comes from several different functions. The most obvious is the name; "functional", that is, using functions that are fundamental to mathematics. Another significant reason is that functional programming involves determining the totality of things that will always be true, that by their interaction achieve the desired calculation — this is similar to how mathematical proofs are performed.

0
source

Source: https://habr.com/ru/post/1333109/


All Articles