9
votes

I would like to add type signatures to some local functions in my code. However, their types involve anonymous, existentially bound type variables. Since they're anonymous, I don't know how to write a type signature. How can I refer to such type variables?

In the following example, go has type [b] -> Int -> Int, where b is the type bound by the pattern match T (x_shared:xs) g. What type signature can I write for it?

data T = forall a. T [a] (a -> a -> Int)

f :: T -> Int
f (T (x_shared:xs) g) = go xs 0
  where
    -- go :: what type?
    go (x:xs) n = go xs $! n + g x_shared x
    go []     n = n
1

1 Answers

15
votes

With ScopedTypeVariables extension, you can add a type annotation to g and introduce type variable a to the scope.

f (T (x_shared:xs) (g :: a -> a -> Int)) = go xs 0

Then you can write a type signature for go with a.

go :: [a] -> Int -> Int