Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to an existential type variable

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
like image 282
Heatsink Avatar asked Feb 28 '13 03:02

Heatsink


People also ask

What is an existential type?

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.

What is an existential type in Swift?

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.


1 Answers

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
like image 166
snak Avatar answered Nov 10 '22 02:11

snak