30 Aug 21:52 2012

## why do I need class context in declaring data constructor?

Paul Liu <ninegua <at> gmail.com>

2012-08-30 19:52:21 GMT

2012-08-30 19:52:21 GMT

I had a toy program that encodes simply typed lambda in types. It used to work fine with GHC prior to 7.2. But now it no longer compiles. Here is a minimal fragment that demonstrates this problem. > {-# LANGUAGE GADTs, > MultiParamTypeClasses, > FlexibleInstances, > FlexibleContexts #-} > > data Abs env t v where > Abs :: g (a, env) h v -> Abs env (g (a, env) h v) (a -> v) > > class Eval g env t v where > eval :: env -> g env t v -> v > > instance Eval g (a, env) h v => > Eval Abs env (g (a, env) h v) (a -> v) where > eval env (Abs e) = \x -> eval (x, env) e The type Abs has 3 parameters: its environment, sub term (encoded in types), and type. The constructor Abs has 1 parameter: its sub term. The code loads fine in GHC 7.0.3. Here is the error reported by GHC 7.2.2 (and later): test.lhs:14:30: Could not deduce (Eval g1 (a1, env) h1 v1) arising from a use of `eval' from the context (Eval g (a, env) h v) bound by the instance declaration at test.lhs:(12,12)-(13,49)(Continue reading)