Implementation of “show” for function

前端 未结 1 1311
天涯浪人
天涯浪人 2021-01-12 11:32

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

Something like t

相关标签:
1条回答
  • 2021-01-12 11:44

    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>>"
    
    0 讨论(0)
提交回复
热议问题