Search code examples
haskellio-monad

How to use pure functions recursively in types with IOMonad instances?


I get the error:

Main.hs:38:22: error:
    • Couldn't match type ‘WD ()’ with ‘()’
...
    • In the expression:
        setScrollHPos height >> scrollUntilEnd0 height <$> getHeight

where:

setScrollHPos :: Integer -> WD ()
scrollUntilEnd0 :: Integer -> Integer -> WD ()
scrollUntilEnd0 lastHeight height
  | lastHeight < height = setScrollHPos height >> scrollUntilEnd0 

and WD has an IOMonad instance.

I am trying to use pure functions over values of I/O monads so, I tried the solution from the Stack Overflow question Haskell - How can I use pure functions inside IO functions?, but I am still unable to do it recursively.

Dependencies:

dependencies:
- base >= 4.7 && < 5
- http-client
- http-client-tls
- bytestring
- webdriver

Code:

{-# LANGUAGE OverloadedStrings #-}

module Module (main) where

import Test.WebDriver
import qualified Test.WebDriver.JSON as JSON
import System.IO
import Control.Monad.IO.Class (liftIO)

firefoxConfig :: WDConfig
firefoxConfig = hilf $ useBrowser firefox defaultConfig
  where
    hilf m = m { wdHTTPRetryCount = 100 }

main :: IO ()
main = runSession firefoxConfig $
  openPage "https://duckduckgo.com" >>
  
  scrollUntilEnd >>
  liftIO getLine >>

  closeSession
  where
    getHeight :: WD Integer
    getHeight = executeJS [] "return document.documentElement.scrollHeight"
    setScrollHPos :: Integer -> WD ()
    setScrollHPos height = JSON.ignoreReturn $ executeJS [JSArg height]
      "(function(h) { window.scrollTo(0, h); })"

    scrollUntilEnd0 :: Integer -> Integer -> WD ()
    scrollUntilEnd0 lastHeight height
      | lastHeight < height = setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
      | otherwise            = pure ()
    
    scrollUntilEnd :: WD ()
    scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight```

Error out:

Main.hs:34:31: error:
    • Couldn't match type ‘WD ()’ with ‘()’
      Expected type: WD ()
        Actual type: WD (WD ())
    • In the expression:
        setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
      In an equation for ‘scrollUntilEnd0’:
          scrollUntilEnd0 lastHeight height
            | lastHeight < height
            = setScrollHPos height >> scrollUntilEnd0 height <$> getHeight
            | otherwise = pure ()
      In an equation for ‘someFunc’:
          someFunc
            = runSession firefoxConfig
                $ openPage "https://duckduckgo.com" >> scrollUntilEnd
                    >> liftIO getLine
                    >> closeSession
            where
                getHeight :: WD Integer
                getHeight
                  = executeJS [] "return document.documentElement.scrollHeight"
                setScrollHPos :: Integer -> WD ()
                setScrollHPos height
                  = JSON.ignoreReturn
                      $ executeJS
                          [JSArg height] "(function(h) { window.scrollTo(0, h);
                ....
   |
34 |       | lastHeight < height = setScrollHPos height >> scrollUntilEnd0 heig
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Main.hs:38:22: error:
    • Couldn't match type ‘WD ()’ with ‘()’
      Expected type: WD ()
        Actual type: WD (WD ())
    • In the expression: scrollUntilEnd0 0 <$> getHeight
      In an equation for ‘scrollUntilEnd’:
          scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight
      In an equation for ‘someFunc’:
          someFunc
            = runSession firefoxConfig
                $ openPage "https://duckduckgo.com" >> scrollUntilEnd
                    >> liftIO getLine
                    >> closeSession
            where
                getHeight :: WD Integer
                getHeight
                  = executeJS [] "return document.documentElement.scrollHeight"
                setScrollHPos :: Integer -> WD ()
                setScrollHPos height
                  = JSON.ignoreReturn
                      $ executeJS
                          [JSArg height] "(function(h) { window.scrollTo(0, h);
                ....
   |
38 |     scrollUntilEnd = scrollUntilEnd0 0 <$> getHeight
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Solution

  • <$> lets you pass a monadic input to a function with a non-monadic output. In your case, you want to pass a monadic input to a function with a monadic output, so you need =<< instead. For precedence reasons, you also now need parentheses, so do setScrollHPos height >> (scrollUntilEnd0 height =<< getHeight).