I'm doing so fairly fun stuff with GHC 7.8, but have ran in to a bit of a problem. I have the following:
mkResultF :: Eq k => Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v)
mkResultF Here key = ResultComp (pure . lookup key)
mkResultF q@(There p) key =
case mkResultF p key of
ResultFId a -> pure a
ResultComp c ->
ResultComp $ \foo ->
case c foo of
ResultFId a -> pure a
ResultComp c ->
ResultComp $ \foo ->
case c foo of
ResultFId a -> pure a
Clearly there is something to abstract here, but I can't quite work out how to do it. When I try the following:
mkResultF :: Eq k => Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v)
mkResultF Here key = ResultComp (pure . lookup key)
mkResultF q@(There p) key = magic (mkResultF p key)
magic :: ResultF (Reverse kvs) (Maybe v) -> ResultF (Reverse kvs ++ '[('KV x y)]) (Maybe v)
magic (ResultFId a) = pure a
magic (ResultComp c) = ResultComp (\foo -> magic (c foo))
This feels like an "obvious" solution, but it doesn't type check:
Could not deduce (kvs2 ~ Reverse kvs0)
from the context (Reverse kvs ~ ('KV k v1 : kvs2))
bound by a pattern with constructor
ResultComp :: forall a k v (kvs :: [KV * *]).
([(k, v)] -> ResultF kvs a) -> ResultF ('KV k v : kvs) a,
in an equation for `magic'
at query-kv.hs:202:8-19
`kvs2' is a rigid type variable bound by
a pattern with constructor
ResultComp :: forall a k v (kvs :: [KV * *]).
([(k, v)] -> ResultF kvs a) -> ResultF ('KV k v : kvs) a,
in an equation for `magic'
at query-kv.hs:202:8
Expected type: ResultF (Reverse kvs0) (Maybe v)
Actual type: ResultF kvs2 (Maybe v)
Relevant bindings include
c :: [(k, v1)] -> ResultF kvs2 (Maybe v)
(bound at query-kv.hs:202:19)
In the first argument of `magic', namely `(c foo)'
In the expression: magic (c foo)
I'm really stuck on this. A full code listing with the starting code can be found here: https://gist.github.com/ocharles/669758b762b426a3f930
Why do you have AllowAmbiguousTypes
enabled? That's almost never a good idea. Without the extension, you get a much better error message:
Couldn't match type ‘Reverse kvs0’ with ‘Reverse kvs’
NB: ‘Reverse’ is a type function, and may not be injective
The type variable ‘kvs0’ is ambiguous
Expected type: ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse kvs ++ '['KV x y]) (Maybe v)
Actual type: ResultF (Reverse kvs0) (Maybe v)
-> ResultF (Reverse kvs0 ++ '['KV x0 y0]) (Maybe v)
In the ambiguity check for:
forall (kvs :: [KV * *]) v x y.
ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse kvs ++ '['KV x y]) (Maybe v)
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
In the type signature for ‘magic’:
magic :: ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse kvs ++ '[KV x y]) (Maybe v)
The problem is indeed in the type signature for magic
, where you have
magic :: ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse kvs ++ '[('KV x y)]) (Maybe v)
All the variables kvs
, x
, and y
occur only as arguments to Reverse
and ++
, which are type families and need not be injective. Such as situation is always suspicious.
The easiest fix is to add a proxy. Here's code that compiles for me:
mkResultF :: forall k v kvs. Eq k
=> Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v)
mkResultF Here key = ResultComp (pure . lookup key)
mkResultF (There p) key = magic (Proxy :: Proxy kvs) (mkResultF p key)
magic :: Proxy ('KV x y ': kvs)
-> ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse ('KV x y ': kvs)) (Maybe v)
magic _ r =
case r of
ResultFId a -> pure a
ResultComp c ->
ResultComp $ \foo ->
case c foo of
ResultFId a -> pure a
ResultComp c ->
ResultComp $ \foo ->
case c foo of
ResultFId a -> pure a
I've looked at this again, and here's a version that uses your definition of magic
(as magic2
). It's still not very elegant, but it hopefully suffices as a proof-of-concept:
mkResultF :: forall k v kvs. Eq k
=> Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v)
mkResultF Here key = ResultComp (pure . lookup key)
mkResultF (There p) key = magic1 (Proxy :: Proxy kvs) (mkResultF p key)
magic1 :: forall x y kvs v. Proxy ('KV x y ': kvs)
-> ResultF (Reverse kvs) (Maybe v)
-> ResultF (Reverse ('KV x y ': kvs)) (Maybe v)
magic1 _ = magic2 (Proxy :: Proxy ('KV x y)) (Proxy :: Proxy (Reverse kvs))
magic2 :: Proxy ('KV x y) -> Proxy kvs
-> ResultF kvs (Maybe v)
-> ResultF (kvs ++ '[('KV x y)]) (Maybe v)
magic2 _ _ (ResultFId a) = pure a
magic2 p _ (ResultComp (c :: ([(k, v')] -> ResultF kvs' (Maybe v))))
= ResultComp (\ foo -> magic2 p (Proxy :: Proxy kvs') (c foo))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With