#1965: Allow unconstrained existential contexts in newtypes
-------------------------------+--------------------------------------------
Reporter: guest | Owner:
Type: feature request | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 6.8.1
Resolution: | Keywords:
Os: Unknown/Multiple | Architecture: Unknown/Multiple
Failure: None/Unknown | Difficulty: Unknown
Testcase: | Blockedby:
Blocking: | Related:
-------------------------------+--------------------------------------------
Changes (by Irene):
* cc: ireney.knapp@… (added)
Comment:
Okay, so this ticket-revival stemmed from a question I asked on IRC, so
here's what I wanted to use this for.
{{{
class (Monad (m context)) => MonadContext m context where
getContext
:: m context context
withContext
:: MonadContext m context'
=> context'
-> m context' a
-> m context a
data FallibleContextualOperation failure context a =
FallibleContextualOperation {
fallibleContextualOperationAction :: context -> IO (Either failure
a)
}
instance Monad (FallibleContextualOperation failure context) where
return a = FallibleContextualOperation {
fallibleContextualOperationAction = \_ -> return $ Right
a
}
x >>= f =
FallibleContextualOperation {
fallibleContextualOperationAction = \context -> do
v <- fallibleContextualOperationAction x context
case v of
failure@(Left _) -> return failure
Right y -> fallibleContextualOperationAction (f y) context
}
instance MonadContext (FallibleContextualOperation failure) context where
getContext =
FallibleContextualOperation {
fallibleContextualOperationAction =
\context -> return $ Right context
}
withContext context' x =
FallibleContextualOperation {
fallibleContextualOperationAction =
\context -> fallibleContextualOperationAction x context'
}
}}}
I can provide more details of what this is motivated by upon request, but
basically, I want to have my "master implementation" of this class based
on FallibleContextualOperation, and then I want to make two types that get
that behavior for free:
{{{
newtype Serialization context a =
forall backend
. Serialization {
serializationOperation :: FallibleContextualOperation
(SerializationFailure backend)
context
a
}
deriving instance Monad (Serialization context)
deriving instance MonadContext Serialization context
newtype Deserialization context a =
forall backend
. Deserialization {
deserializationOperation :: FallibleContextualOperation
(DeserializationFailure backend)
context
a
}
deriving instance Monad (Deserialization context)
deriving instance MonadContext Deserialization context
}}}
The point is so that I can then do:
{{{
class Serializable context a where
serialize :: Serialization context a
deserialize :: Deserialization context a
}}}
... and write many instances of this, elsewhere in my program, which don't
need to know or care about the fact that there are two backend types
(ByteStrings and open files), or any of this other nasty plumbing detail.
I don't know if this is sufficient motivation to justify the work the
ticket is requesting, since I don't know just how much work it is. But
it's presented here for everyone's edification. :)
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/1965#comment:6>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs