Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is F#'s implementation of monads unique with respect to the amount of keywords available to it?

I only know F#. I haven't learned the other functional programming languages. All the examples that I have seen for monads only describe the bind and unit methods. F# has lots of keywords (e.g. let!, do!, etc.) that allow you to do different things within the same computational expression. This seemingly gives you more power than your basic bind and unit methods. Is this unique to F# or is it common across functional programming languages?

like image 388
Charles Lambert Avatar asked May 13 '11 18:05

Charles Lambert


People also ask

What IS F in horsepower?

The IS F packs a 416-hp 5.0-liter V-8 mated to an eight-speed gearbox.

Is the F 5 a V8?

The vehicle features a 5.0 L direct-injected V8 producing 416 SAE hp (423 PS, 311 kW) at 6,600 rpm, while peak torque is 371 ft⋅lbf (503 N⋅m) at 5,200 rpm. The engine also features a two-stage intake system, engine oil and automatic transmission fluid coolers and an oil pump designed for high-speed cornering.

How Fast is Lexus IS F?

Lexus' official top speed for the IS F is 170 mph (273.6 kph), so this one is kissing the model's Vmax.


2 Answers

Yes, I think that the F# syntax for computation expressions is unique in that it provides direct syntactic support for different types of computations. It can be used for working with monoids, usual monads and also MonadPlus computations from Haskell.

I wrote about these in the introduction of my Master thesis. I believe it is quite readable part, so you can go to page 27 to read it. Anyway, I'll copy the examples here:

Monoid is used just for concatenating values using some "+" operation (Combine). You can use it for example for building strings (this is inefficient, but it demonstrates the idea):

type StringMonoid() =
  member x.Combine(s1, s2) = String.Concat(s1, s2)
  member x.Zero() = ""
  member x.Yield(s) = s

let str = new StringMonoid()

let hello = str { yield "Hello "
                  yield "world!" };;

Monads are the familiar example that uses bind and return operations of comptuation expressions. For example maybe monad represents computations that can fail at any point:

type MaybeMonad() =
  member x.Bind(m, f) =
    match m with Some(v) -> f v | None -> None
  member x.Return(v) = Some(v)

let maybe = new MaybeMonad()

let rec productNameByID() = maybe {
  let! id = tryReadNumber()
  let! prod = db.TryFindProduct(id)
  return prod.Name }

Additive monads (aka MonadPlus in Haskell) is a combination of the two. It is a bit like monadic computation that can produce multiple values. A common example is list (or sequence), which can implement both bind and combine:

type ListMonadPlus() =
  member x.Zero() = []
  member x.Yield(v) = [v]
  member x.Combine(a, b) = a @ b
  member x.Bind(l, f) = l |> List.map f |> List.concat

let list = new ListMonadPlus()

let cities = list {
  yield "York"
  yield "Orleans" }
let moreCities = list {
  let! n = cities
  yield n
  yield "New " + n }

// Creates: [ "York"; "New York"; "Orleans"; "New Orleans" ]

There are some additional keywords that do not directly correspond to any theoretical idea. The use keyword deals with resources and for and while can be used to implement looping. The sequence/list comprehension actually use for instead of let!, because that makes much more sense from the syntactic point of view (and for usually takes some sequence - although it may be e.g. asynchronous).

like image 52
Tomas Petricek Avatar answered Oct 01 '22 03:10

Tomas Petricek


Monads are defined in terms of bind and unit operations (only). There are other structures which are defined by other operations (e.g. in Haskell, the MonadPlus typeclass has zero and plus operations - these correspond to Zero and Combine in F# computation expressions). As far as I know, F#'s computation builders are unique in terms of providing nice syntax for the wide range of operations that they support, but most of the operations are unrelated to monads.

like image 20
kvb Avatar answered Oct 01 '22 02:10

kvb