[Haskell-cafe] Types of partially instantiated polymorphic helper functions

Todd Wilson twilson at csufresno.edu
Tue Nov 24 22:58:33 UTC 2020


I see, thanks! And I guess that if the type variable c in your type of g
were changed to b, it will still work, but now g would have a monomorphic
instead of polymorphic type. I hope the combination of keywords in my
subject line can serve to direct someone else with this problem to your
answer!

Todd Wilson

On Tue, Nov 24, 2020 at 2:37 PM Adam Gundry <adam at well-typed.com> wrote:

> Dear Todd,
>
> On 24/11/2020 22:26, Todd Wilson wrote:
> > This has got to be a trivial question, but I don't see a solution, nor
> > could I find anything through searching: If I want to write a type
> > declaration for the helper function g here, what do I write?
> >
> >     f :: a -> [b] -> [(a,b)]
> >     f a bs = g bs where
> >       g :: ????
> >       g [] = []
> >       g (x:xs) = (a,x) : g xs
>
> This has a disappointingly not-entirely-trivial answer:
>
>     {-# LANGUAGE ExplicitForAll, ScopedTypeVariables #-}
>
>     f :: forall a b . a -> [b] -> [(a,b)]
>     f a bs = g bs where
>       g :: [c] -> [(a, c)]
>       g [] = []
>       g (x:xs) = (a,x) : g xs
>
> We need the type variable `a` in the type of `g` to be the same as the
> one bound in the type of `f`, but Haskell2010 doesn't have a way to
> express this. The ScopedTypeVariables extension, when used with an
> explicit forall, makes the type variables scope over type signatures in
> the body of the function.
>
> Hope this helps,
>
> Adam
>
> --
> Adam Gundry, Haskell Consultant
> Well-Typed LLP, https://www.well-typed.com/
>
> Registered in England & Wales, OC335890
> 118 Wymering Mansions, Wymering Road, London W9 2NF, England
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20201124/cab4337d/attachment.html>


More information about the Haskell-Cafe mailing list