Why is Haskell fully declarative?

I am not very well versed in the differences between the programming paradigms of imperative and declarative . I read that Haskell is a declarative language. I would say yes, but there is something that bothers me regarding the definition of imperative.

When I have a datastruct and use the Haskell functions to convert, I actually just said WHAT to convert. Therefore, I give datastruct as an argument to the function and am pleased with the result.

But what if there is no function that really satisfies my needs?

I would start writing my own function that expects datastruct as an argument. After that, I will start writing how to handle the datastruct. Since I can only name native Haskell functions, am I still adhering to a declarative paradigm? But what when I start using the if statement. Does this not end up being declarative in nature, since I'm going to tell the program HOW to do things from now on?

+5
source share
5 answers

Perhaps this is a matter of perspective. The way I see it has nothing to do with defining things in terms of other things, because we can always replace something with our definition (so far this definition is clean). That is, if we have fx = x + 1 , then in any place that we see fz , we can replace it with z + 1 . Therefore, pure functions should not be considered instructions; they should be considered definitions.

For this reason, a lot of Haskell code is considered declarative. We simply define things as (pure) functions of other things.

In Haskell, you can write imperative style code. Sometimes we really want to say something like "do A, then do B, and then do C". This adds a new dimension to the simple world of function applications: we need the concept of "happen sooner". Haskell adopted the concept of Monad to describe computations that have an evaluation order. This is very convenient because it can encapsulate effects such as state changes.

+6
source

Haskell doesn't have an if , but an if if a then b else c that is really equivalent to ternary expressions like a ? b : c a ? b : c in languages ​​like C or b if a else c in Python. You cannot leave else , and it can be used wherever an arbitrary expression can be used. In addition, the constraint a must be of type Boolean , and b and c must be of the same type. This is syntactic sugar for

 case a of True -> b False -> c 

Do not go in cycles on a label "declarative"; it's just a programming style that supports a language. Consider, for example, a typical declarative definition of a factorial function:

 fact 0 = 1 fact n = n * fact (n - 1) 

It is also just syntactic sugar for something that you think is more important:

 fact n = case n of 0 -> 1 otherwise -> n * fact (n - 1) 
+6
source

Honestly, the term "declarative programming" is more of a marketing term than anything else. The unofficial definition, which means β€œan indication of what to do, not how to do it,” is vague and open to interpretation and certainly far from the black and white border. In practice, this seems to apply to everything that is broadly related to the categories of functional programming, logical programming, or the use of domain languages ​​(DSL).

Therefore (and I understand that this probably does not qualify as an answer to your question, but still :)), I recommend that you do not spend time trying to find out if something else is declarative or not. The terms of the imperative against functional and logical programming are already a little more meaningful, so it might be more useful to reflect them.

+4
source

Declarative languages ​​are built from expressions, while imperative languages ​​are built from statements.

The usual explanation is what to do and how to do it. You have already found confusion in this. If you do, declarative uses definitions (by name), and imperative uses definitions of definitions. What is a declarative language? One that only calls definitions? If so, then the only Haskell program you could write is for sure main !

There is a declarative way and an imperative way to have branching, and this follows directly from the definition. A declarative branch is an expression, and a mandatory branch is an expression. Haskell has case … of … (expression), and C has if (…) {…} else {…} (instruction).

What is the difference between expressions and statements? Expressions matter, while statements have consequences. What is the difference between values ​​and effects?

For expressions, there is a function ΞΌ that maps any expression e to the value ΞΌ(e) . This is also called semantics, or meaning, and ideally represents a well-defined mathematical object. This method of determining values ​​is called denotational semantics. There are other methods.

For operators, there is state P immediately before operator S and state Q immediately after. The effect S is a delta from P to Q. This method of determining effects is called Chorus logic. There are other methods.

+1
source

You cannot use the if statement in Haskell because it does not exist. You can use "if-expression"

 if c then a else b 

but it's just syntactic sugar for something like

 let f True ab = a; f False ab = b in fcab 

which again is declarative.

0
source

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


All Articles