Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Created October 28, 2014 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdepillabout/3b780e469236625c0354 to your computer and use it in GitHub Desktop.
Save cdepillabout/3b780e469236625c0354 to your computer and use it in GitHub Desktop.
short example of a place where rank2types and rankntypes are necessary
{-# LANGUAGE Rank2Types #-}
-- This is a small example of where you need to use Rank2Types (or
-- RankNTypes).
-- The 'a' type is being used polymorphically here, but when it
-- is called, it will already be bound (as either an Int or String),
-- so it cannot be used polymorphically.
--
-- This means that g is a monomorphic function.
--
-- The forall at the outermost level means that f promises to work with
-- any argument g as long as g has the shape a -> a for some type a unknown
-- to f.
--f :: forall a. Show a => (a -> a) -> IO ()
--f g = do
-- print $ g 1
-- print $ g "lala"
-- The 'a' type here is being used polymorphically, however f' get's to
-- pick what to use for a, not the caller. This is why you need
-- higher-rank types. Without higher-rank types, the caller always get to
-- pick the types.
--
-- This means that g' must be a polymorphic function.
--
-- The forall at the inner level means that it's the argument g' who
-- promises to be of shape a -> a for all types a at the same time,
-- and it's f' who makes use of that promise by choosing both a = Int
-- and a = String.
f' :: (forall a. Show a => a -> a) -> IO ()
f' g' = do
print $ g' 1
print $ g' "lala"
main :: IO ()
main = f' id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment