Calculations for the Haskell programmer

I am looking to learn F #, but one thing that baffles me is the do-notation? Syntax of the syntax and desugaring.

In haskell, you have a very simple Monad style and rules for desugaring do-notation in bind and return. There is no magic associated with adding keywords; the only thing that should match is the types.

F # has a bunch of builders, keywords, and complexity.

Is there a good explanation of how to compare one concept with another?

Mostly I want to know how I draw

do x <- monadicComputation foo x someOtherMonadicComputation let y = somePureComputation x return $ bar y 

in F #.

The only keywords in haskell are do (<-) and let.

+6
source share
3 answers

You cannot write generic monadic code in F #; instead, you must specify the monad you are working on by naming the builder associated with the expression. Your sample code would look like this:

 let example = async { let! a = someAsyncComputation foo a do! someOtherAsyncComputation let y = somePureComputation a return (bar y) } 

for the expression type of the async calculation. The kick pattern (do !, let! Etc.) is used when binding monadic values, while regular keywords are used for non-monodic values.

let! corresponds to the bind (>>=) , and let corresponds let designation do . return corresponds to return , and return! used to get the existing monadic value. do! similar to (>>) , which performs a monadic meaning for its effects, and do for non-monodic effects, which has no parallel in Haskell.

+13
source

If you came from the Haskell background, you might be interested in an academic article about which I recently wrote about F # calculations.

It associates the syntax of a calculation expression (which is pretty flexible) with the standard class types that are used in Haskell. As already mentioned, F # not only allows you to write general code on top of the monad (this can be done, but it is not idiomatic), but, on the other hand, it allows you to choose the most suitable syntax, and you can even get good syntax for MonadPlus or for monad transformers.

In addition to the async monad mentioned by Lee, here is an example of MonadPlus (using the sequence expression - list monad - as an example):

 let duplicate list = seq { for n in list do yield n yield n βˆ— 10 } 

Or a calculation expression for parsers:

 let rec zeroOrMore p = parse { return! oneOrMore p return [] } 
+4
source

The haskell notation has only one special syntax, i.e. <- , which maps to the bind function, everything else inside do is just a normal function application, for which the result is a monad type, for example: return , putStr , etc.

Similarly in F # you have let! to represent the bind and return operation, the syntactic sugar of the keyword (not an ordinary function call, as in haskell, but this keyword is mapped to the return function you define). Now there are many other keywords that can support your calculation expressions (you can easily omit them if not required), all of them are documented here . These extra operations give you syntactic sugar for using the F # keywords instead of the usual functions that return a monadic value. You can see that all keywords that you can overload in F # evaluation expressions have a monodic return value.

So, basically, you don’t have to worry about all these keywords, just think of them as a normal monad return function (with a specific signature of the type that you can find in the documentation) that you can call using the F # keywords inside syntax for expressing calculations.

+3
source

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


All Articles