ifcxt-0.1.0.0: put if statements within type constraints

Safe HaskellNone
LanguageHaskell2010

IfCxt

Synopsis

Documentation

class IfCxt cxt where Source

This class lets a polymorphic function behave differently depending on the constraints of the polymorphic variable. For example, we can use it to write the following improved version of the "show" function:

ifShow :: forall a. IfCxt (Show a) => a -> String
ifShow = ifCxt (Proxy::Proxy (Show a))
  show
  (const "<<unshowable>>")

In ghci, we can run:

>>> ifShow (1 :: Int)
"1"
>>> ifShow (id :: a -> a)
"<<unshowable>>"

The IfCxt.Examples module contains more examples.

Methods

ifCxt :: proxy cxt -> (cxt => a) -> a -> a Source

Instances

mkIfCxtInstances :: Name -> Q [Dec] Source

Derives all possible instances of IfCxt for the given one parameter type class.