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
Existential types, or 'existentials' for short, are a way of 'squashing' a group of types into one, single type. Existentials are part of GHC's type system extensions.
Existentials in Swift allow defining a dynamic value conforming to a specific protocol. Using primary associated types, we can constrain existentials to certain boundaries. The Swift team introduced the any keyword to let developers explicitly opt-in to a performance impact that might otherwise not be visible.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With