Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a default polymorphic unit type haskell

Tags:

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?

like image 369
Sebastiaan Avatar asked Sep 29 '14 10:09

Sebastiaan


1 Answers

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, e.g. you can use Const ().

like image 54
Joachim Breitner Avatar answered Sep 24 '22 00:09

Joachim Breitner