[Haskell-beginners] Restrict type in phantom data-type

Baa aquagnu at gmail.com
Fri Sep 1 14:18:02 UTC 2017


Hello, List!

For example, I have specialized (right nameis phantom?) type:

  data Day a = Day { ... no `a` here }
  data Sunny
  data Rainy

  joyToday :: Day Sunny -> IO ()
  joyToday day = ...

  melancholyToday :: Day Rainy -> IO ()
  melancholyToday day = ...

And I can create (in spite of that it's phantom) some day:

  let day1 = Day {...} :: Day Sunny
  joyToday day1

but no problem to create `Day Int`, `Day Char`, etc which is
pointless actually (sure "creator"-function can be exported from the
module only, but I'm talking about type-level solution).

I know that constraints (`... =>`) on data types are redundant/removed
from the language. And I'm not sure how it's possible to restrict that
parameter `a` (I know that it's possible to Java/C++/Perl6 (not sure),
some other languages but how to add such restriction in Haskell? IMHO
type families can help but I'm not sure how it will look (Sunny, Rainy
are "nullary" type, so...).

Is it possible for Haskell too?

===
Best regards, Paul


More information about the Beginners mailing list