[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    [Haskell-cafe] [Fwd: selectively allow alternatives in a sum type]
From:       Olaf Klinke <olf () aatal-apotheke ! de>
Date:       2022-08-30 13:57:27
Message-ID: 98d7722432234bb1fee0c2b4fe03881994354ab2.camel () aatal-apotheke ! de
[Download RAW message or body]

Disregard Const' below, Data.Functor.Const as a newtype is always
strict, as is Identity. 
That also ensures Foo' Identity is isomorphic to the original Foo.
Apologies for the noise. 

Olaf

-------- Forwarded Message --------
From: Olaf Klinke <olf@aatal-apotheke.de>
To: Haskell Café <haskell-cafe@haskell.org>
Subject: selectively allow alternatives in a sum type
Date: Tue, 30 Aug 2022 15:30:39 +0200

Dear Café,

Is there prior art/existing packages for the following? Is it maybefunctional \
programming folklore? Is it a sign of bad program design?  Sometimes I feel the need \
to selectively allow or disallow alternatives in a sum type. That is, suppose we have \
a sum type

data Foo = LeftFoo !A | RightFoo !B

and at some places in the program we want the type system to enforce
that only the constructor LeftFoo can be used. My solution would be to
use a strict version of Data.Functor.Const and make the type higher
rank:

newtype Const' a b = Const' !a
-- Const' Void b ~ Void
-- Const' ()   b ~ ()

data Foo' f = LeftFoo' !A | RightFoo' !(f B)
type Foo = Foo' Identity
type LeftFoo = Foo' (Const' Void) -- can not construct a RightFoo'

The advantage over defining LeftFoo as an entirely different type is
that Foo and LeftFoo can share functions operating entirely on the left
option. 

Olaf


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic