实施“展示”功能 [英] Implementation of "show" for function

查看:90
本文介绍了实施“展示”功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为(二进制)函数实现 show 方法,并使其能够将endofunctions (a - > a)



类似于伪哈斯克尔代码:

  instance显示(a-> b)其中
show fun =<< Endofunction>>如果a == b
show fun =<< Function>>如果a \ = b

如何区分这两种情况?

$ b $您需要启用一些扩展功能:

  { - #LANGUAGE OverlappingInstances,FlexibleInstances# - } 
模块FunShow其中

实例显示(( - >))aa)其中
显示_ =<< Endofunction>> ;

实例显示(( - >)a b)其中
显示_ =<<函数>>

您需要 OverlappingInstances ,因为实例 a - >由于语言标准要求实例声明中的类型变量是不同的,因此也匹配endofunctions,因此存在重叠,并且需要 FlexibleInstances p>

  * FunShow> show not 
<< Endofunction>>
* FunShow>显示fst
<<功能>>
* FunShow> show id
<< Endofunction>>


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?

解决方案

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

这篇关于实施“展示”功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆