Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Something like the application `$` operator for "do" notation?

Tags:

haskell

I'm supplying a function to idleCallback This notation works:

idleCallback $= Just (do
    modifyIORef world play
    postRedisplay Nothing)

Why doesn't this (seemingly similar) notation work?

idleCallback $= Just $ do
    modifyIORef world play
    postRedisplay Nothing

To save your hoogling, the types are:

($=) :: HasSetter s => s a -> a -> IO ()
type IdleCallback = IO ()
data SettableStateVar a 
idleCallback :: SettableStateVar (Maybe IdleCallback)
postRedisplay :: Maybe Window -> IO ()
modifyIORef :: IORef a -> (a -> a) -> IO ()

GHC says:

Couldn't match expected type `Maybe IdleCallback'
            with actual type `a0 -> Maybe a0'
In the second argument of `($=)', namely `Just'
In the expression: idleCallback $= Just
In a stmt of a 'do' block:
  idleCallback $= Just
  $ do { modifyIORef world play;
         postRedisplay Nothing }

Can this be written without wrapping the do block in parenthesis?

like image 758
Cuadue Avatar asked Dec 15 '13 23:12

Cuadue


People also ask

What is do notation in Haskell?

Basically, as we discussed, this do notation is used to handle the input and output operation in Haskell; we also have some more ways which provide us to handle these operations, but out of which do notation is very easy to use and implement in Haskell.

What does the bind operator do Haskell?

The bind operations, >> and >>=, combine two monadic values while the return operation injects a value into the monad (container).

What does a period do in Haskell?

In general terms, where f and g are functions, (f . g) x means the same as f (g x). In other words, the period is used to take the result from the function on the right, feed it as a parameter to the function on the left, and return a new function that represents this computation."

What does colon mean in Haskell?

In Haskell, the colon operator is used to create lists (we'll talk more about this soon). This right-hand side says that the value of makeList is the element 1 stuck on to the beginning of the value of makeList .


1 Answers

It is a precedence error.... ($=) is binding more tightly than ($). You can see this in the error message:

Couldn't match expected type `Maybe IdleCallback'
        with actual type `a0 -> Maybe a0'
In the second argument of `($=)', namely `Just'

It thinks that the second argument of ($=) is simply Just (which is a valid Haskell type referencing a function). If you put parenthesis around the whole Just, including the do-block, it should work.

like image 50
jamshidh Avatar answered Oct 15 '22 04:10

jamshidh