How are all graphics and web libraries implemented in Haskell?

I'm just starting to learn Haskell. I read that it is a pure functional language, and everything in it is unchanged. Thus, things like input, write and read databases cause state variability. I know that in Haskell there is a thing called monads that allow you to use imperative functions in Haskell, like IO Monad . But I wonder if everything mandatory in Haskell is implemented using monads? HackageDB has many packages that allow you to work with 3D graphics, databases, parse HTML, write web servers, etc. Etc.

What is the general idea of ​​all this? What allows Haskell to stay clean and at the same time be applicable to writing all this? Hope someone clarifies this to me. Thanks in advance!

+5
source share
4 answers

I figured this out using the following analogy, which I will express using JavaScript.

How can side expressions be expressed?

1. Function

This is obviously the first thing that comes to mind:

 var launchRockets = function () { prepareRockets( queryDBForPreparationParameters() ) launchAllPreparedRockets() outputResults() } 

You can see an effective function that calls a bunch of other spectacular functions that themselves can create unknown effects with all the ensuing consequences.

2. Instructions

Another way to express this would be to compile a set of instructions describing these efficient calculations for some function for subsequent execution. (Ever made a SQL query?)

 var launchRocketsInstructions = [ { description: "Prepare rockets", parameters: { description: "Query a DB for preparation parameters" } }, { description: "Launch all prepared rockets" }, { description: "Output results" } ] 

So what do we see in our second example? We see an immutable data tree describing the calculation, not its execution immediately. There are no side effects here, and we can use pure functions to compile this data tree. And what are essentially side effects in Haskell. The entire infrastructure provided by the language: monads, IO , do -notation are just tools and abstractions that simplify your task of compiling a single instruction tree.

Of course, in order to really follow these instructions, you will eventually have to go into the wild world of side effects. In the case of JavaScript, it will be something like execute(launchRocketsInstructions) , in the case of Haskell, it is the runtime that executes the root of the command tree, which you create using the main function of the main module, which becomes the only entry point of your program. Thus, the side effects in Haskell actually occur outside the scope of the language, so it is clean.

+12
source

I read that it is a pure functional language, and everything in it is unchanged.

Haskell is only clean / default /. If you declare to the compiler (via the monadic type) that you want certain effects, then they are included.

By default, they are not included.

+8
source

In Haskell, you never execute anything. You simply create a description of what you want to do by combining the I / O actions, and then assign this description to the main one. Then the compiler translates any description of the program that it finds in the main variable into executable code.

I recommend that you read this introduction to Haskell IO. I wrote that describes the situation in more detail.

However, this only explains how we combine IO activities, and not how to introduce new ones. Haskell has two ways to add new I / O:

  • Built-in compilers
  • External Function Interface (FFI)

Then, all IO monads combine these primitive I / O actions into larger I / O operations.

+3
source

Yes, everything you need in Haskell is written using monads. Monads is a general idea that allows Haskell to be clean and usable for writing practical programs that perform real-world I / O.

I recommend reading the famous article "Shooting a Clumsy Squad" by Simon Peyton Jones, which explains how the IO monad is used for real - programming in a purely functional language.

0
source

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


All Articles