Hans Höglund | 31 Jan 18:56 2014
Picon

Dispatch a type-function on the existence (or not) of instances?

Dear all,

I have been curious about the ability to detect the presence of a certain instance (ClassFoo TypeBar) in the
type system.
Specifically, is it possible to "dispatch" a type on the existence (or not) of such an instance. For example
given two functions:

withInstance :: (ClassFoo TypeBar) => TypeIfInstanceExists
withoutInstance :: TypeIfInstanceDoesNotExists

I would be able to consolidate them into something like this:

withOrWithoutInstance :: 
    (r ~ InstanceExists ClassFoo TypeBar, 
     a ~ If r TypeIfInstanceExists TypeIfInstanceDoesNotExists) => a

I guess what I need is:

1) A type-level "if".
2) The possibility of "converting" a constraint into a type-level bool.

I am sure (1) is possible but have no idea about (2). Anyone?

Best regards,
Hans
Richard Eisenberg | 1 Feb 19:21 2014

Re: Dispatch a type-function on the existence (or not) of instances?

I am sure that "2) The possibility of "converting" a constraint into a type-level bool." is *not* possible.
And, it really shouldn't be possible.

The problem has to do with modules. Suppose we have:

> module A where
> data Foo
> type Magic           -- invented syntax:
>   | Show Foo  = Int
>   | otherwise = Bool

> module B where
> import A
> instance Show Foo
> bar :: Magic
> bar = 3

> module C where
> import A
> quux :: Magic -- no (Show Foo) here!
> quux = False

> module D where
> import B
> import C
> hasSameType :: a -> a -> ()
> hasSameType _ _ = ()
> unit :: ()
> unit = hasSameType bar quux

(Continue reading)

Hans Höglund | 3 Feb 10:13 2014
Picon

Re: Dispatch a type-function on the existence (or not) of instances?

This makes sense, thank you for clearing it up.

My problem arises from the following situation, where I want to overload t depending whether a getter is
available or not.

class HasTGetter a
class HasTSetter a

t :: HasTSetter a -> Setter a T
t :: (HasTGetter a, HasTSetter a) -> Lens a T

I could probably resolve the ambiguity by using a separate type function.

instance HasTSetter Foo
type instance LensType Foo T = Setter Foo T

instance HasTGetter Bar
instance HasTSetter Bar
type instance LensType Bar T = Lens Bar T

t :: (HasGetter a, HasSetter a) -> LensType a T

Regards,
Hans

On 1 feb 2014, at 19:21, Richard Eisenberg wrote:

> I am sure that "2) The possibility of "converting" a constraint into a type-level bool." is *not*
possible. And, it really shouldn't be possible.
> 
(Continue reading)


Gmane