简体   繁体   English

是否有默认的多态单元类型haskell

[英]Is there a default polymorphic unit type haskell

in Haskell there is a default unit type, namely (). 在Haskell中有一个默认的单元类型,即()。 I'm looking for a polymorphic one (preferably in Hackage), for instance: 我正在寻找一个多态的(最好是在Hackage中),例如:

data PUT a = PUT

or perhaps a polymorphic zero type: 或者可能是多态零类型:

data PZT a = PZT (PZT a)

So yes, I could write one myself, by either of the above statements. 所以是的,我可以通过以上任何一种陈述自己写一个。 I'm looking for one in hackage. 我正在寻找一个hackage。

The reason I need it, is because I have a class with multiple type parameters, which contains a function that does not use one of them: 我需要它的原因是因为我有一个具有多个类型参数的类,其中包含一个不使用其中一个的函数:

class MyClass a b where
  someFunction :: a
  -- and some other functions

when using this function "someFunction", GHC cannot find the right instance, so I changed my definition: 当使用这个函数“someFunction”时,GHC找不到合适的实例,所以我改变了我的定义:

class MyClass a b where
  someFunction :: (PUT b) -> a

Now when I call someFunction, I can use (PUT::SomeType) as its first argument, and Haskell can derive which instance I meant. 现在当我调用someFunction时,我可以使用(PUT :: SomeType)作为它的第一个参数,而Haskell可以导出我的意思。 Every time I use this trick, I write a new polymorphic unit type (it's just one line of codes), which gives me a bit of extra work when combining different libraries (because preferably, I'd use the same constructor everywhere). 每次我使用这个技巧时,我都会编写一个新的多态单元类型(它只是一行代码),这在组合不同的库时给了我一些额外的工作(因为我最好在各处使用相同的构造函数)。 I'm sure that other people ran into this problem, so maybe one of them put a solution in hackage (ghc's packet manager)? 我确信其他人遇到了这个问题,所以也许其中一个人提出了hackage解决方案(ghc的数据包管理器)? I'd like to import it. 我想导入它。 Am I searching for the wrong thing, or does it not exist in hackage? 我在寻找错误的东西,还是在hackage中不存在?

I think what you are looking for is usually called Proxy . 我认为你所寻找的通常被称为Proxy Since base-4.7, it is available in Data.Proxy 从base-4.7开始,它在Data.Proxy中可用

A similar thing, which is available in base since longer than Proxy , is Const , eg you can use Const () . 一个类似的东西,它在基础上可用,因为比Proxy ,是Const ,例如你可以使用Const ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM