[Haskell-cafe] Question about typeclass & constraints
Erik Hesselink
hesselink at gmail.com
Tue Apr 8 08:53:01 UTC 2014
I've run into problems like this as well. Often you can redesign a bit
to fix things, although it depends on the application how to do that.
Two other technical options in addition to your PP':
You can add a constraint to the 'create' function. It depends on the
actual semantics if this makes sense, I guess:
class PP2 m where
create :: CB a => a -> m a
You can use ConstraintKinds [1] to add a constraint to create
depending on the type 'm':
type family C m a :: Constraint
class PP3 m where
create :: C m a => a -> m a
This way you can choose if you want to add a constraint and which one,
depending on the 'm'.
If you can solve it without any of these things, I'd probably prefer
that personally, though. As Tom also said, complicated type class
constructions often make your program more difficult to understand and
maintain.
Erik
[1] http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/constraint-kind.html
On Tue, Apr 8, 2014 at 10:17 AM, jean-christophe mincke
<jeanchristophe.mincke at gmail.com> wrote:
> Tom,
>
> Yes of course it is simplified for clarity.
>
> Here is a modified version where fb does something (a bit more usefull)
>
>
> class PP m where
> create :: a -> m a
>
> data A a = A a
> instance PP A where
> create a = A a
>
> class CB a where
> fb :: a -> a
>
> data B m a = B (m a)
> instance (PP m) => PP (B m) where
> create a = let a' = fb a
> in B (create a')
>
> class PP' m a where
> create' :: a -> m a
>
> instance (PP m) => PP' m a where
> create' = create
>
> instance (PP m, CB a) => PP' (B m) a where
> create' a = let a' = fb a
> in B (create a')
>
> Actually I ran into that problem when trying to add a kind of rule engine
> layer above the Persistent typeclass. Given the complexity of these
> typeclass, I think it is more practical to reason about a simpler form of
> the same problem.
>
> Thanks
>
> J-C
>
> On Tue, Apr 8, 2014 at 9:42 AM, Tom Ellis
> <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:
>>
>> On Tue, Apr 08, 2014 at 09:25:17AM +0200, jean-christophe mincke wrote:
>> > instance (PP m) => PP (B m) where
>> > create a = let _ = fb a
>> > in B (create a)
>>
>> Your use of 'fb' here is baffling. Am I right in thinking you have tried
>> to
>> simplify your problem for clarity? If so I think you have simplified too
>> far!
>>
>> Could you give an example where the use of 'fb' actually matters?
>>
>> Tom
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list