simulating dependent types; ghc/ghci discrepancy
Hello all- I'm still playing with simulating dependent types using rank-2 polymorphism, and I've run into a small stumbling block on unwrapping existential datatypes: they can't be let-bound (monomorphic restriction?), but can be unwrapped in a case or lambda. Actually, I find that assignment in do-notation works nicely (since I'm already writing monadic code). As an example: module DepTest where data Show' = forall a . Show a => Show' a print' :: Show' -> IO () print' x = do (Show' x') <- return x print x' -- here, the type of x' is known test :: IO () test = print' (Show' True) This works fine. My problem is that I'd like to do this interactively, in ghci. *DepTest> :t Show' True Show' True :: Show' *DepTest> Show' x <- return $ Show' True *DepTest> :t x x :: forall a. a *DepTest> x getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} Interrupted. First, the type of x seems like it should be either Bool or (forall a. Show a => a), but not (forall a.a). Second, egads, what did I do? Is it possible to unwrap an existential type in ghci? Thanks- John
data Show' = forall a . Show a => Show' a
The "forall a. Show a =>" context here has no effect on the representation; it merely constrains applications of the data constructor Show'. Since you have to say
print' :: Show' -> IO ()
anyway (thus providing the dictionary) there is no point in storing it. Thus
*DepTest> :t x x :: forall a. a
is indeed the type of x. There was a thread about this in the café recently; see http://www.haskell.org//pipermail/haskell-cafe/2004-March/005985.html http://www.haskell.org//pipermail/haskell-cafe/2004-March/005999.html http://www.haskell.org//pipermail/haskell-cafe/2004-March/005998.html --KW 8-)
On Wed, Apr 14, 2004 at 04:58:55PM +0100, Keith Wansbrough wrote:
data Show' = forall a . Show a => Show' a
The "forall a. Show a =>" context here has no effect on the representation; it merely constrains applications of the data constructor Show'. Since you have to say
print' :: Show' -> IO ()
anyway (thus providing the dictionary) there is no point in storing it.
Aren't you talking about a different declaration? data Show a => Show' a = Show' a Best regards, Tom -- .signature: Too many levels of symbolic links
anyway (thus providing the dictionary) there is no point in storing it.
Aren't you talking about a different declaration?
data Show a => Show' a = Show' a
Yes, I am. Oops, sorry. --KW 8-) -- Keith Wansbrough <kw217@cl.cam.ac.uk> http://www.cl.cam.ac.uk/users/kw217/ University of Cambridge Computer Laboratory.
On Wed, Apr 14, 2004 at 10:13:51AM -0400, John D. Barnett wrote:
*DepTest> :t Show' True Show' True :: Show' *DepTest> Show' x <- return $ Show' True *DepTest> :t x x :: forall a. a *DepTest> x getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} getTcTyVar a {- tv aXJ -} Interrupted.
First, the type of x seems like it should be either Bool or (forall a. Show a => a), but not (forall a.a). Second, egads, what did I do?
Is it possible to unwrap an existential type in ghci?
For me this seems to be a bug in GHCi. BTW, this works: *Exist> do Show' x <- return (Show' True); print x True I wonder if there a type in Haskell-with-extensions that could be assigned to x? Best regards, Tom -- .signature: Too many levels of symbolic links
On Wed, Apr 14, 2004 at 06:29:38PM +0200, Tomasz Zielonka wrote:
Is it possible to unwrap an existential type in ghci?
For me this seems to be a bug in GHCi. I wonder if there a type in Haskell-with-extensions that could be assigned to x?
Probably GHCi could do the same as in this situation and report an error: *Exist> do Show' y <- return (Show' "QWrwer"); return y <interactive>:1: Inferred type is less polymorphic than expected Quantified type variable `a' escapes When checking an existential match that binds y :: a The pattern(s) have type(s): Show' The body has type: m a In a 'do' expression: Show' y <- return (Show' "QWrwer") In the definition of `it': it = do Show' y <- return (Show' "QWrwer") return y Best regards, Tom -- .signature: Too many levels of symbolic links
participants (3)
-
John D. Barnett -
Keith Wansbrough -
Tomasz Zielonka