Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax of ST monad declaration

I recently began looking at core libraries on Hackage, and there's a recurring idiom I don't understand. Here's an example from the ST module:

instance Monad (ST s) where
    {-# INLINE (>>=)  #-}
    (>>) = (*>)
    (ST m) >>= k
      = ST (\ s ->
        case (m s) of { (# new_s, r #) ->
        case (k r) of { ST k2 ->
        (k2 new_s) }})

In particular, I don't understand (# new_s, r #). I assume the second hash refers to an unboxed value, but the rest is a mystery to me (something to do with "new state", presumably).

like image 984
planarian Avatar asked Apr 07 '17 22:04

planarian


People also ask

Which three rules are strictly applicable over a monad declaration?

All the three rules are strictly applicable over a Monad declaration which is as follows − The three basic laws that are applicable over a Monad declaration are − Left Identity Law − The return function does not change the value and it should not change anything in the Monad. It can be expressed as "return >=> mf = mf".

How to express return as a function in monad?

It can be expressed as "return >=> mf = mf". Right Identity Law − The return function does not change the value and it should not change anything in the Monad. It can be expressed as "mf >=> return = mf". Associativity − According to this law, both Functors and Monad instance should work in the same manner.

What is the syntax of variable declaration?

Syntax of Variable Declaration The word syntax means the grammar of a programming language. We can talk about the syntax of just a small part of a program, such as the syntax of variable declaration. This declares a variable, declares its data type, and reserves memory for it. It says nothing about what value is put in memory.

What is a monad in JavaScript?

One generic concept that I love expressing in JS is the functional keystone known as the Monad. A Monad is an encapsulation of an associative binary operation. In other words, you can call map on it with an appropriate function parameter to change the inner value — even its type.


1 Answers

(# x, y, z #) is an unboxed tuple with three elements. See "8.2.2. Unboxed Tuples" at https://downloads.haskell.org/~ghc/6.8.3/docs/html/users_guide/primitives.html.

The rest is basically just an implementation of State.

like image 92
amalloy Avatar answered Oct 18 '22 10:10

amalloy