Advertisement
Guest User

Untitled

a guest
May 4th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. {-# LANGUAGE ConstraintKinds #-}
  2. {-# LANGUAGE FlexibleContexts #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4. {-# LANGUAGE MultiParamTypeClasses #-}
  5. {-# LANGUAGE PolyKinds #-}
  6. {-# LANGUAGE Rank2Types #-}
  7. {-# LANGUAGE TypeOperators #-}
  8.  
  9. import Prelude hiding (mod)
  10.  
  11. import XMonad hiding (XConfig (..), get, modify,
  12. put, xmonad)
  13. import XMonad (XConfig (XConfig))
  14. import qualified XMonad as X (XConfig (..), xmonad)
  15. import qualified XMonad.StackSet as W
  16.  
  17.  
  18. import Control.Monad.State
  19.  
  20. import Control.Arrow (first, second)
  21. import Data.Constraint
  22. import XMonad.Layout.LayoutModifier (LayoutModifier, ModifiedLayout)
  23.  
  24.  
  25. type IsLayout l a = (Read (l a), LayoutClass l a)
  26.  
  27. class CC m a where
  28. dict :: forall l. IsLayout l a => m l a -> Dict (IsLayout (m l) a)
  29. cc :: forall l. m l a -> IsLayout l a :- IsLayout (m l) a
  30. cc x = Sub $ dict x
  31. {-# MINIMAL dict #-}
  32.  
  33. wrapLT :: CC m a
  34. => (forall l. (LayoutClass l a) => l a -> m l a)
  35. -> Layout a -> Layout a
  36. wrapLT m (Layout la) = Layout (m la) \\ cc (m la)
  37.  
  38. newtype (f :. g) l a = O (f (g l) a)
  39. infixr 9 :.
  40.  
  41. unO :: (f :. g) l a -> f (g l) a
  42. unO (O fgla) = fgla
  43.  
  44. instance Read (m1 (m2 l) a) => Read ((m1 :. m2) l a) where
  45. readsPrec i = map (first O) . readsPrec i
  46.  
  47. instance Show (m1 (m2 l) a) => Show ((m1 :. m2) l a) where
  48. show = show . unO
  49.  
  50. instance LayoutClass (m1 (m2 l)) a => LayoutClass ((m1 :. m2) l) a where
  51. runLayout ws@W.Workspace{W.layout = lay} =
  52. (second (O <$>) <$>) . runLayout (ws{W.layout = unO lay})
  53. handleMessage lay =
  54. fmap (fmap O) . handleMessage (unO lay)
  55. description = description . unO
  56.  
  57. instance (CC m1 a, CC m2 a) => CC (m1 :. m2) a where
  58. dict x = Dict \\ trans (cc (unO x)) (cc undefined)
  59.  
  60. instance CC Mirror a where dict _ = Dict
  61. instance IsLayout m a => CC (Choose m) a where dict _ = Dict
  62. instance LayoutModifier m a => CC (ModifiedLayout m) a where dict _ = Dict
  63.  
  64. type Prime = Arr (XConfig Layout)
  65. type Arr a = StateT a IO ()
  66.  
  67. xmonad :: Prime -> IO ()
  68. xmonad prime = xmonad' =<< execStateT prime (def{X.layoutHook = Layout $ X.layoutHook def})
  69. where xmonad' :: XConfig Layout -> IO ()
  70. xmonad' cf@XConfig{ X.layoutHook = Layout l } = X.xmonad cf{ X.layoutHook = l }
  71.  
  72.  
  73. modifyLayout :: (CC m Window)
  74. => (forall l. (LayoutClass l Window) => l Window -> m l Window)
  75. -> Prime
  76. modifyLayout f = modify $ \c -> c { X.layoutHook = wrapLT f $ X.layoutHook c }
  77.  
  78. -- imagine there is a function f out there somewhere:
  79. f :: LayoutClass l a => l a -> Mirror (Choose Full l) a
  80. f = Mirror . (Full |||)
  81.  
  82. main :: IO ()
  83. main = xmonad $ do
  84. -- this works OK
  85. modifyLayout (Full ||| )
  86. modifyLayout Mirror
  87. -- this doesn't
  88. modifyLayout $ Mirror . (Full |||)
  89. -- this does, but is annoying
  90. modifyLayout $ O . Mirror . (Full |||)
  91. -- this is the real problem:
  92. modifyLayout f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement