Implementing a show function for a function

I would like to implement the show method for (binary) functions and make it able to distance endofunctions (a -> a) .

Something like pseudo-Haskell code:

 instance Show (a->b) where show fun = "<<Endofunction>>" if a==b show fun = "<<Function>>" if a\=b 

How can I distinguish between two cases?

+7
source share
1 answer

You need to enable some extensions:

 {-# LANGUAGE OverlappingInstances, FlexibleInstances #-} module FunShow where instance Show ((->) aa) where show _ = "<<Endofunction>>" instance Show ((->) ab) where show _ = "<<Function>>" 

You need OverlappingInstances since the a -> b instance also matches the endofunctions functions, so they overlap, and you need FlexibleInstances because the language standard states that type variables in instance declarations are different.

 *FunShow> show not "<<Endofunction>>" *FunShow> show fst "<<Function>>" *FunShow> show id "<<Endofunction>>" 
+15
source

All Articles