执行"show"功能

Asl*_*986 7 haskell

我想实现show(二进制)函数的方法,并使其能够distingish内功能(a -> a).

像伪haskell代码的东西:

instance Show (a->b) where
    show fun = "<<Endofunction>>" if a==b
    show fun = "<<Function>>" if a\=b
Run Code Online (Sandbox Code Playgroud)

我如何区分这两种情况?

Dan*_*her 15

您需要启用一些扩展:

{-# LANGUAGE OverlappingInstances, FlexibleInstances #-}
module FunShow where

instance Show ((->) a a) where
    show _ = "<<Endofunction>>"

instance Show ((->) a b) where
    show _ = "<<Function>>"
Run Code Online (Sandbox Code Playgroud)

您需要,OverlappingInstances因为实例a -> b也匹配内部函数,因此存在重叠,您需要,FlexibleInstances因为语言标准要求实例声明中的类型变量是不同的.

*FunShow> show not
"<<Endofunction>>"
*FunShow> show fst
"<<Function>>"
*FunShow> show id
"<<Endofunction>>"
Run Code Online (Sandbox Code Playgroud)