work-around to avoid duplicate instances?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

work-around to avoid duplicate instances?

Johannes Waldmann-2
Dear Cafe,


"When matching, GHC takes no account
of the context of the instance declaration"
https://downloads.haskell.org/ghc/latest/docs/html/users_guide/glasgow_exts.html#instance-resolution


for example, these two instances are deemed "duplicate":

{-# language TypeFamilies, FlexibleInstances, UndecidableInstances #-}

data T1 ; data T2
class C a where type F a

class D a
instance (C a, F a ~ T1) => D a
instance (C a, F a ~ T2) => D a


Is there a work-around?

I can do it manually, by instantiation (ha):
for each  T  that I might want to use,
create a copy of `instance D a` (chosen according to  `F T`)
with `a` replaced with `T`.

But that's ugly! A mechanical process that creates
duplicated code (in reality, class D has methods).
(Also, to actually write these instantiations,
I'd have to scatter them over the program, or use awkward imports.)

Can GHC do something for me here?
I guess it depends on full-program knowledge of instances,
and that conflicts with separate compilation.


- J.W.

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: work-around to avoid duplicate instances?

Georgi Lyubenov
Hi!

Can you give a bit more context on what your end goal is? What kinds of things are a, F a, T1, T2, D a, etc?

======
Georgi

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: work-around to avoid duplicate instances?

Henning Thielemann
In reply to this post by Johannes Waldmann-2

On Mon, 5 Oct 2020, Johannes Waldmann wrote:

> Dear Cafe,
>
>
> "When matching, GHC takes no account
> of the context of the instance declaration"
> https://downloads.haskell.org/ghc/latest/docs/html/users_guide/glasgow_exts.html#instance-resolution
>
>
> for example, these two instances are deemed "duplicate":
>
> {-# language TypeFamilies, FlexibleInstances, UndecidableInstances #-}
>
> data T1 ; data T2
> class C a where type F a
>
> class D a
> instance (C a, F a ~ T1) => D a
> instance (C a, F a ~ T2) => D a

Why not:

class D b
instance D T1
instance D T2

and have context (C a, D (F a)) or (C a, F a ~ b, D b) ?
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: work-around to avoid duplicate instances?

David Feuer
In reply to this post by Johannes Waldmann-2
If you want instance chains, you can fake them very well with closed type families, auxiliary classes, etc. Useful, but awkward and verbose.

On Mon, Oct 5, 2020, 3:59 PM Johannes Waldmann <[hidden email]> wrote:
Dear Cafe,


"When matching, GHC takes no account
of the context of the instance declaration"
https://downloads.haskell.org/ghc/latest/docs/html/users_guide/glasgow_exts.html#instance-resolution


for example, these two instances are deemed "duplicate":

{-# language TypeFamilies, FlexibleInstances, UndecidableInstances #-}

data T1 ; data T2
class C a where type F a

class D a
instance (C a, F a ~ T1) => D a
instance (C a, F a ~ T2) => D a


Is there a work-around?

I can do it manually, by instantiation (ha):
for each  T  that I might want to use,
create a copy of `instance D a` (chosen according to  `F T`)
with `a` replaced with `T`.

But that's ugly! A mechanical process that creates
duplicated code (in reality, class D has methods).
(Also, to actually write these instantiations,
I'd have to scatter them over the program, or use awkward imports.)

Can GHC do something for me here?
I guess it depends on full-program knowledge of instances,
and that conflicts with separate compilation.


- J.W.

_______________________________________________
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.

_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: work-around to avoid duplicate instances?

Johannes Waldmann-2
In reply to this post by Henning Thielemann
Thanks for hints and pointers.


> .. more context ..

my use case is: SAT encoding of data structures (matrices)


> .. closed type families

in this case, no? the type family (Decoded a)
is in an external library
https://hackage.haskell.org/package/ersatz-0.4.8/docs/Ersatz-Codec.html
so I cannot make it "closed"?


I have a work-around now, following Henning's suggestion (?) I changed

   Matrix  (dim :: Nat) (a::Type)

to

   Matrix (dim :: Nat) (a :: Type) (d :: Type)

and I use it with (Decoded a ~ d)

The point is that now `d` can be used for instance resolution.



Thanks - J.


NB 1 - source:
https://gitlab.imn.htwk-leipzig.de/waldmann/pure-matchbox/-/blob/a4251052084baf51d8ed34abaa2c4a7d2b273672/src/Matchbox/Matrix/Data.hs#L247

NB 2 - if you have a better solution for
https://gitlab.imn.htwk-leipzig.de/waldmann/pure-matchbox/-/blob/better-matrix/src/Matchbox/Matrix/Remove.hs#L145
...

NB 3 - mailing list archive update is stalled?
The current thread does not appear at
https://mail.haskell.org/pipermail/haskell-cafe/2020-October/date.html
_______________________________________________
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.