xmonad / xmonad-contrib

Contributed modules for xmonad
https://xmonad.org
BSD 3-Clause "New" or "Revised" License
584 stars 274 forks source link

XMonad.Layout.IndependentScreens -> countScreens results in Ambiguous type variable ‘m0’ #789

Closed bag-man closed 1 year ago

bag-man commented 1 year ago

Problem Description

I'm trying to add this function to my xmonad.hs:

togglevga = do
  screencount <- LIS.countScreens
  if screencount > 1
   then spawn "xrandr ...."
   else spawn "xrandr ...."

However I get the following error:

~ λ  xmonad --recompile
XMonad will use ghc to recompile, because neither "/home/owg1/.xmonad/build" nor "/home/owg1/.xmonad/stack.yaml" exists.
XMonad recompiling (forced).
Errors detected while compiling xmonad config: /home/owg1/.xmonad/xmonad.hs
$ ghc --make xmonad.hs -i -ilib -fforce-recomp -main-is main -dynamic -v0 -outputdir /home/owg1/.xmonad/build-x86_64-linux -o /home/owg1/.xmonad/xmonad-x86_64-linux

xmonad.hs:74:3: error:
    • Ambiguous type variable ‘m0’ arising from a do statement
      prevents the constraint ‘(Monad m0)’ from being solved.
      Relevant bindings include
        togglevga :: m0 () (bound at xmonad.hs:73:1)
      Probable fix: use a type annotation to specify what ‘m0’ should be.
      These potential instances exist:
        instance Monad IO -- Defined in ‘GHC.Base’
        instance Monad Query -- Defined in ‘XMonad.Core’
        instance Monad X -- Defined in ‘XMonad.Core’
        ...plus six others
        ...plus 9 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of a 'do' block: screencount <- LIS.countScreens
      In the expression:
        do screencount <- LIS.countScreens
           if screencount > 1 then
               spawn
                 "xrandr ..."
           else
               spawn
                 "xrandr ..."
      In an equation for ‘togglevga’:
          togglevga
            = do screencount <- LIS.countScreens
                 if screencount > 1 then
                     spawn
                       "xrandr ..."
                 else
                     spawn
                       "xrandr ..."
   |
74 |   screencount <- LIS.countScreens
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

xmonad.hs:74:18: error:
    • Ambiguous type variable ‘m0’ arising from a use of ‘LIS.countScreens’
      prevents the constraint ‘(MonadIO m0)’ from being solved.
      Relevant bindings include
        togglevga :: m0 () (bound at xmonad.hs:73:1)
      Probable fix: use a type annotation to specify what ‘m0’ should be.
      These potential instances exist:
        instance [safe] MonadIO IO -- Defined in ‘Control.Monad.IO.Class’
        instance MonadIO Query -- Defined in ‘XMonad.Core’
        instance MonadIO X -- Defined in ‘XMonad.Core’
        ...plus one instance involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In a stmt of a 'do' block: screencount <- LIS.countScreens
      In the expression:
        do screencount <- LIS.countScreens
           if screencount > 1 then
               spawn
                 "xrandr -..."
           else
               spawn
                 "xrandr ..."
      In an equation for ‘togglevga’:
          togglevga
            = do screencount <- LIS.countScreens
                 if screencount > 1 then
                     spawn
                       "xrandr ..."
                 else
                     spawn
                       "xrandr ..."
   |
74 |   screencount <- LIS.countScreens
   |                  ^^^^^^^^^^^^^^^^

Please check the file for errors.

Configuration File

Please include the smallest full configuration file that reproduces the problem you are experiencing:

import XMonad
import qualified XMonad.Layout.IndependentScreens as LIS

main :: IO ()
main = xmonad def

togglevga = do
  screencount <- LIS.countScreens
  if screencount > 1
   then spawn "xrandr ..."
   else spawn "xrandr ..."

Thanks for your help, I'm quite confident this is just a small mistake on my part, I'm quite new to Haskell!

Checklist

geekosaur commented 1 year ago

It looks like you don't use togglevga, so Haskell can't determine its type. The simplest fix for it is to give it a type signature:

togglevga :: MonadIO m => m ()
slotThe commented 1 year ago

@bag-man did this solve your issue?

slotThe commented 1 year ago

This should be fixed, so I'm closing.