The problem is that show :: (t -> t -> t) -> String
should work for any function working on any type t
. You are assuming that t
is Boolean which is illegal, because (according to GHC) "t
is a rigid type variable" and cannot be unified with specialized type.
One possible solution would be to specialize your instance by Bool
(FlexibleInstances
are necessary)
{-#LANGUAGE FlexibleInstances #-}
instance Show (Bool -> Bool -> Bool) where
show b = show $ b True False
But this will reduce generality of your Church's boolean.
It is impossible to define flexible solution that would work on any type, because you will need to have two representatives of that type that would describe true and false cases, and there are types such like Void
which have no (defined) values.
An idea that comes to my mind and is quite universal is to add few more class constraints to t
:
{-#LANGUAGE FlexibleInstances #-}
import Data.Boolean
instance (Show t, Boolean t) => Show (t -> t -> t) where
show b = show $ b true false
The Boolean
class collects types that can be understood as logic values in some terms. For example for Bool
:
instance Boolean Bool where
true = True
false = False
notB = not
(||*) = (||)
(&&*) = (&&)
And now we can ensure that
t
is a thing that you can actually show
- There are at least two valid and different values of type
t
that appear as true
and false
Which are the required circumstances to actually be able to show
a function of such signature in this way.
IMPORTANT
Following example won't work:
show (true :: (Show t, Boolean t) => t -> t -> t)
The problem is that typechecker won't guess which t
are you going to have here. This solution provides valid and working instance, but only for fully instantiated types. If you get ambiguity error, you will need to specify what is t
:
show (true :: Bool -> Bool -> Bool)
>>> "True"
show (true :: Int -> Int -> Int) -- assuming Boolean instance
>>> "1"
EDIT:
Yet another idea was mentioned in the comments. The solution would be to wrap your Church boolean with Rank2Type
:
{-# LANGUAGE Rank2Types #-}
newtype ChBool = ChBool (forall t. t -> t -> t)
Which will let t
be any type independent of the context. Then you may define casual instance like this:
instance Show ChBool where
show (ChBool f) = show $ f True False
t -> t -> t
, but you're specifically usingBool
s in the definition ofshow
, which prevents it being a valid definition for arbitraryt
. – Bobettenewtype ChBool = ChBool (forall x. x -> x -> x)
if you want instances – PilsudskiRank2Types
orRankNTypes
extension) then it works. (You also have to changeshow b =
toshow (ChBool b =
– Mccaskillinstance Show (forall t. t -> t -> t) where
even withRank2Types
enabled, I get an error about an "illegal polymorphic type" which I don't really understand, particularly as using anewtype
wrapper for the type seems to be absolutely fine.] – MccaskillShow
and instead just had something likeshowBoolean :: (forall t. t -> t -> t) -> String; showBoolean b = b "True" "False"
– Bula