Is there a simple explanation as to why ko1
and ko2
fail ?
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module SOQuestionExistential where
import Data.Proxy
------------------------------------------------------------------------------
class C s where
important_stuff :: proxy s -> a
compute_ok :: forall r. (forall s. C s => Proxy s -> IO r) -> IO r
compute_ok k = undefined
unref_ok :: Proxy s -> Proxy s -> IO ()
unref_ok _ _ = return ()
----
ok :: (forall s. C s => t -> Proxy s) -> t -> IO ()
ok calc t = compute_ok (unref_ok (calc t))
ko1 :: (forall s. C s => t -> Proxy s) -> t -> IO ()
ko1 calc t = (compute_ok . unref_ok) (calc t)
-- • Couldn't match type ‘s’ with ‘s0’
-- ‘s’ is a rigid type variable bound by
-- a type expected by the context:
-- forall s. C s => Proxy s -> IO ()
ok' :: (forall s. C s => t -> Proxy s) -> t -> IO ()
ok' calc t = compute_ok (unref_ok (let proxy_secret_s = calc t in proxy_secret_s))
ko2 :: (forall s. C s => t -> Proxy s) -> t -> IO ()
ko2 calc t = let proxy_secret_s = calc t in compute_ok (unref_ok (proxy_secret_s))
-- • No instance for (C s1) arising from a use of ‘calc’
-- • In the expression: calc t
-- In an equation for ‘proxy_secret_s’: proxy_secret_s = calc t
-- In the expression:
-- let proxy_secret_s = calc t
-- in compute_ok (unref_ok (proxy_secret_s))typec
edit : added the error message