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

Francesco Ariis fa-ml at ariis.it
Fri Sep 1 14:55:32 UTC 2017


On Fri, Sep 01, 2017 at 05:18:02PM +0300, Baa wrote:
> 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

Hello Paul,

the usual way is create a module and export `smart` constructors:

    module MyType (rainy, Day, Sunny) -- *not* Day

    rainy :: Day Rainy
    rainy = undefined -- something here

That way you can use Day for time signatures but *not* `Day` the
constructor.

Does this solve your problem?


More information about the Beginners mailing list