Haskell Language Proxies Using Proxy


Example

The Proxy :: k -> * type, found in Data.Proxy, is used when you need to give the compiler some type information - eg, to pick a type class instance - which is nonetheless irrelevant at runtime.

{-# LANGUAGE PolyKinds #-}

data Proxy a = Proxy

Functions which use a Proxy typically use ScopedTypeVariables to pick a type class instance based on the a type.

For example, the classic example of an ambiguous function,

showread :: String -> String
showread = show . read

which results in a type error because the elaborator doesn't know which instance of Show or Read to use, can be resolved using Proxy:

{-# LANGUAGE ScopedTypeVariables #-}

import Data.Proxy

showread :: forall a. (Show a, Read a) => Proxy a -> String -> String
showread _ = (show :: a -> String) . read

When calling a function with Proxy, you need to use a type annotation to declare which a you meant.

ghci> showread (Proxy :: Proxy Int) "3"
"3"
ghci> showread (Proxy :: Proxy Bool) "'m'"  -- attempt to parse a char literal as a Bool
"*** Exception: Prelude.read: no parse