Implementation of "show" for function

haskell

Solution

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>>"

Problem

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?

Original source