Skip to content

Deriving quantified methods

Motivation

I was trying to derive Data.Functor.Alt.Alt

class Functor f => Alt f where
  (<!>) :: f a -> f a -> f a

  some :: Applicative f => f a -> f [a]
  many :: Applicative f => f a -> f [a]

It's really some and many that get in the way, so let's work with Alt'

Now this is a bit tricky, should this be expected to compile?

{-# Language ConstrainedClassMethods #-}
{-# Language DerivingVia             #-}

import Data.Functor.Identity

class Alt' f where
  some :: Applicative f => f a -> f [a]

instance Alt' Identity where
  some :: Identity a -> Identity [a]
  some (Identity a) = Identity [a,a,a] -- doesn't matter

newtype ID a = ID a
  deriving Alt'
  via Identity


--     • Couldn't match type ‘Identity’ with ‘ID’
--         arising from the coercion of the method ‘some’
--           from type ‘forall a.
--                      Applicative Identity =>
--                      Identity a -> Identity [a]’
--             to type ‘forall a. Applicative ID => ID a -> ID [a]’
--     • When deriving the instance for (Alt' ID)
Edited by Icelandjack
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information