Skip to content

Instantly share code, notes, and snippets.

@viercc
Last active December 8, 2023 05:18
Show Gist options
  • Save viercc/32d6c080fc199d8b7985077760896bb6 to your computer and use it in GitHub Desktop.
Save viercc/32d6c080fc199d8b7985077760896bb6 to your computer and use it in GitHub Desktop.
Effect of MonoLocalBinds
{-# LANGUAGE ExplicitForAll #-}
-- With MonomorphismRestriction (which is ON by default,)
-- any constrained type variable (e.g. `m` in `Monad m`)
-- are not generalized
{-# LANGUAGE NoMonomorphismRestriction #-}
-- MonoLocalBinds prevents generalization of non-top-level binding
{-# LANGUAGE MonoLocalBinds #-}
-- Compare for
-- {-# LANGUAGE NoMonoLocalBinds #-}
module Main where
main :: IO ()
main = putStrLn (result 0)
--------
class Monad m => MonadXXX m where
xxx :: Int -> m Int
instance MonadXXX Maybe where
xxx = Just
instance MonadXXX IO where
xxx a = putStrLn "xxx" >> pure a
genericAction :: forall m. MonadXXX m => Int -> Int -> m Int
genericAction x y = xxx (x + y)
foo1 :: Maybe Int -> String
foo1 = show
foo2 :: m Int -> String
foo2 _ = "foo2"
result :: Int -> String
result n = (_foo1 action) ++ (foo2 action)
where
action = genericAction n n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment