[Haskell-cafe] Expressing disjunctions of constraints
foxbenjaminfox at gmail.com
Tue Jul 24 10:32:10 UTC 2018
You could write a type family like this one:
type family (x :: k) `IsOneOf` (y :: (k, k)) :: Bool where
x `IsOneOf` '(x, _) = 'True
x `IsOneOf` '(_, x) = 'True
_ `IsOneOf` _ = 'False
This is a type family that checks if a given type matches at least one of a
given pair of types.
Once you have that, you can easily add it as a constraint to your typeclass:
class c `IsOneOf` '(a, b) ~ 'True => Foo a b c | a b -> c where
foo :: a -> b -> c
This seems to give you what you want: It means that no instance of your
typeclass can be for types a, b, and c such that c that isn't equal to at
least one of a or b.
On Mon, Jul 23, 2018 at 9:55 PM Olaf Klinke <olf at aatal-apotheke.de> wrote:
> Dear cafe,
> today I wrote a type class with three parameters.
> class Foo a b c | a b -> c where
> foo :: a -> b -> c
> instance Foo A R A where ...
> instance Foo R A A where ...
> In addition to the functional dependency I'd like to express that at least
> one of the parameters a and b is c, that is, Foo is the union of the two
> class Foo1 a b where
> foo1 :: a -> b -> a
> class Foo2 a b where
> foo2 :: a -> b -> b
> and furthermore the choice between Foo1 and Foo2 determines one parameter
> to be a fixed type, say R. I understand that logical disjunction poses
> problems for instance resolution  because adding an instance may affect
> the instance dictionary lookup. This should not happen in my case because
> in the end this would be a one-parameter type class. The ultimate aim is to
> use the same symbol for both a function
> foo :: R -> a -> a
> and for flip foo. Of course writing flip foo is not that much of a
> nuisance, I just wonder whether Haskell is expressive enough to do this.
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe