Haskell:`show`的变体,它不会在引号中包装String和Char(Haskell: variant of `show` that doesn't wrap String and

编程入门 行业动态 更新时间:2024-10-14 10:40:04
Haskell:`show`的变体,它不会在引号中包装String和Char(Haskell: variant of `show` that doesn't wrap String and Char in quotes)

我想要一个show的变体(让我们称之为label ),就像show一样,除了它不会将String包装在" "或Char中' ' s ' ' 。 例子:

> label 5 "5" > label "hello" "hello" > label 'c' "c"

我尝试手动实现这个,但我碰到了一些墙。 这是我尝试过的:

{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} module Label where class (Show a) => Label a where label :: a -> String instance Label [Char] where label str = str instance Label Char where label c = [c] -- Default case instance Show a => Label a where label x = show x

但是,由于默认大小写的类与instance Label [Char]和instance Label Char重叠,因此这些类型不适用于label函数。

是否有提供此功能的库函数? 如果没有,是否有解决方法来使上述代码工作?

I'd like a variant of show (let's call it label) that acts just like show, except that it doesn't wrap Strings in " " or Chars in ' '. Examples:

> label 5 "5" > label "hello" "hello" > label 'c' "c"

I tried implementing this manually, but I ran into some walls. Here is what I tried:

{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} module Label where class (Show a) => Label a where label :: a -> String instance Label [Char] where label str = str instance Label Char where label c = [c] -- Default case instance Show a => Label a where label x = show x

However, because the default case's class overlaps instance Label [Char] and instance Label Char, those types don't work with the label function.

Is there a library function that provides this functionality? If not, is there a workaround to get the above code to work?

最满意答案

上面的代码不起作用,因为只根据“head”选择实例 ,即类名后面的部分。 “上下文”, =>之前的东西,如“显示a”,只能在之后进行检查。 上下文可以消除实例并产生编译器错误,但不会导致编译器选择不同的实例 。 由于这种行为,重叠的实例可能存在歧义。

有一些编译器扩展可以让你编写更复杂的实例,但我怀疑你最好只编写Label类的单个实例。 你有什么目的吗? 根据你想要完成的任务,可能还有更特殊的用途。

您的示例代码非常简单 - 如果您愿意,只需添加OverlappingInstances扩展即可使其无需进一步修改即可使用。 使用OverlappingInstances会导致GHC容忍一些歧义,只要有一个明显的“最具体”的实例。 在您的代码中,具有特定类型的两个实例是特定的,因此不应该有任何问题。

为了更好的可读性,也可以在你使用时添加TypeSynonymInstances :

{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Label where class (Show a) => Label a where label :: a -> String instance Label String where label x = x instance Label Char where label x = [x] instance (Show a) => Label a where label = show

The code above isn't going to work because instances are chosen only based on the "head", that is, the part after the class name. The "context", the stuff before the => such as `Show a' is only examined afterwards. The context can eliminate an instance and produce a compiler error, but not cause the compiler to pick a different instance. Because of this behavior, overlapping instances are a potential ambiguity.

There are compiler extensions that can let you write more complicated instances, but I suspect you're probably best off just writing individual instances of your Label class. What purpose do you have in mind for this? Depending on what you're trying to accomplish, there might be something more special-purpose already out there.

Your example code is pretty simple, though--if you want, simply adding the OverlappingInstances extension should make it work with no further modifications. Using OverlappingInstances causes GHC to tolerate some ambiguity, so long as there's an obvious "most specific" instance. In your code, the two instances with concrete types are as specific as it gets, so there shouldn't be any problems.

Might as well add TypeSynonymInstances while you're at it, for better readability:

{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Label where class (Show a) => Label a where label :: a -> String instance Label String where label x = x instance Label Char where label x = [x] instance (Show a) => Label a where label = show

更多推荐

本文发布于:2023-08-04 09:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变体   会在   引号   它不   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!