I would like to implement the show
method for (binary) functions and make it able to distingish endofunctions (a -> a)
.
Something like the pseudo-haskell code:
instance Show (a->b) where
show fun = "<<Endofunction>>" if a==b
show fun = "<<Function>>" if a\=b
How can I distinguish the two cases?
You need to enable some extensions:
{-# LANGUAGE OverlappingInstances, FlexibleInstances #-}
module FunShow where
instance Show ((->) a a) where
show _ = "<<Endofunction>>"
instance Show ((->) a b) where
show _ = "<<Function>>"
You need OverlappingInstances
since the instance a -> b
also matches endofunctions, so there's overlap, and you need FlexibleInstances
because the language standard mandates that the type variables in instance declarations are distinct.
*FunShow> show not
"<<Endofunction>>"
*FunShow> show fst
"<<Function>>"
*FunShow> show id
"<<Endofunction>>"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With