Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of "show" for function

Tags:

haskell

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?

like image 973
Aslan986 Avatar asked Jun 03 '12 13:06

Aslan986


1 Answers

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>>"
like image 57
Daniel Fischer Avatar answered Sep 30 '22 03:09

Daniel Fischer