Can a function be parametrically polymorphic over a constructor without a null type?

Haskell has many examples of a higher kind of polymorphism when it comes to special polymorphism such as Monad and Functor. However, I cannot come up with any examples of this for parametric polymorphism.

Is this possible, and if so, can I have an example of something useful?

+4
source share
3 answers

If you still allow class restrictions, the answer is a must! For instance. I would still call something like

normalise :: (Foldable f, Functor f, Fractional n) => f n -> f n
normalise v = fmap (/sum v) V

parametric polymorphism. But I suppose that is not what you mean.

, , , , , _ _,

hmap :: (f a -> f b) -> [f a] -> [f b]
hmap = map

, . , phantom:

class LengthyList l where minimumLength :: l a -> Int
instance LengthyList [] where minimumLength _ = 0
instance LengthyList NonEmpty where minimumLength _ = 1

  minimumLength :: p (l a) -> Int

  minimumLength :: proxy (l a) -> Int

, l, , .

, ,

  minimumLength :: Tagged (l a) Int

or

  minimumLength :: proxy (l a) -> Int

, f a , fa, .

+2

. :

type f ~> g = forall a . f a -> g a

, , ( , ) f g. f ~> g f a . ( , , , Ralf Hinze, ), .

:

{-# LANGUAGE RankNTypes, TypeOperators #-}

type f ~> g = forall a . f a -> g a
data Two g a = Two (g a) (g a)
data Foo f a = This (f a)
             | That (Foo (Two f) a)

hello :: (f ~> g) -> Foo f a -> Foo g a
hello t (This fa) = This (t fa)
hello t (That buh) =
  That (hello (\(Two x y) -> Two (t x) (t y)) buh)

hello f g, * -> *. &; , * .

& dagger; , PolyKinds, f g k -> *.

+1

, , foldr , : p Nat -> * , length m, p m.

:

foldr :: forall a (p :: Nat -> *).
         (forall n. a -> p n -> p ('Succ n)) ->
         p 'Zero ->
         forall n. Vec a n -> p n

, , append, foldr , .

append :: forall a m n. Vec a m -> Vec a n -> Vec a (m :+: n)

, , .

+1

All Articles