简体   繁体   中英

Is there a default polymorphic unit type haskell

in Haskell there is a default unit type, namely (). I'm looking for a polymorphic one (preferably in Hackage), for instance:

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.

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:

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. 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)? I'd like to import it. Am I searching for the wrong thing, or does it not exist in hackage?

I think what you are looking for is usually called Proxy . Since base-4.7, it is available in Data.Proxy

A similar thing, which is available in base since longer than Proxy , is Const , eg you can use Const () .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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