esato1981
esato1981

Reputation: 283

Why is this recursive (?) type permitted in Haskell?

I have seen the following definition floating around:

getCC :: t -> ContT r m (t, t -> ContT r m b)
getCC x0 = callCC (\c -> let f x = c (x, f) in return (x0, f))

But if someone hid the type declaration of getCC I would not be able to figure it out.

What is the type of f?

Well, f is a function that takes an t and returns a Cont r m (t, *something*). But something must be of the same type as f!

So f :: Cont r m (t, Cont r m (t, Cont r m (t, Cont r m (t, ...))).

Why/how does the ghc infer that f :: t -> Cont r m b)?

Upvotes: 2

Views: 120

Answers (1)

effectfully
effectfully

Reputation: 12715

There is no recursion in the types:

{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad.Trans.Cont

getCC :: t -> ContT r m (t, t -> ContT r m b)
getCC x0 = callCC (\(c :: (t, t -> ContT r m b) -> ContT r m b) 
                   -> let f :: t -> ContT r m b
                          f x = c (x, f)
                      in return (x0, f))

Upvotes: 2

Related Questions