If you want to defer the choice of 's' you've to make it appear in the type signature of test1, so you've to introduce an artificial parameter even if we're interested only in its type. e.g.:<br>data Proxy (s :: * -> * -> *) -- useful because we can't have an argument of type 's' directly, since it's higher-kinded, <br>
-- and to document that we're using a phantom argument<br>proxy :: Proxy s<br>proxy = undefined<br><br>test1 :: Stack s => Proxy s -> Integer<br>test1 pr = first . p 2 . p 3 $ empty `asTypeOf` toStack pr<br>
where toStack :: Proxy s -> s a b<br>testTuple = test1 (proxy :: Proxy (,))<br><br>enabling LANGUAGE ScopedTypeVars you can rewrite test1 in a more direct fashion:<br><br>test1 :: forall s. Stack s => Proxy s -> Integer<br>
test1 _ = fist . p 2 . p 3 $ (empty :: s () Void)<br><br><br><div class="gmail_quote">On Mon, Nov 24, 2008 at 5:09 AM, Jacques Carette <span dir="ltr"><<a href="mailto:carette@mcmaster.ca">carette@mcmaster.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I was trying to create a typeclass for an abstract Stack class, and ran into some problems. The following 'works' fine:<br>
<br>
{-# OPTIONS_GHC -XEmptyDataDecls -XFlexibleContexts -fno-monomorphism-restriction #-}<br>
module Stack where<br>
<br>
data Void<br>
<br>
class Stack s where<br>
push_ :: s a r -> b -> s b (s a r)<br>
empty :: s () Void<br>
top :: s a (s b r) -> (a, s b r)<br>
first :: s a r -> a<br>
<br>
instance Stack (,) where<br>
push_ s a = (a,s)<br>
empty = ((),undefined::Void)<br>
top = id<br>
first = fst<br>
<br>
p = flip push_<br>
test0 = top . p 2 . p 3 $ empty<br>
<br>
-- But the following doesn't - I get an "Ambiguous type variable `s' in the contraint `Stack s' arising from the use of `first':<br>
test1 = first . p 2 . p 3 $ empty<br>
-- sure, that makes sense, it somehow needs to know what flavour of Stack to use even though (or perhaps because) the answer is independent of it.<br>
-- So I listen to the "probable fix" and add a type signature:<br>
test1 :: Stack (,) => Integer<br>
<br>
-- This does not however help at all! The only way I have found of 'fixing' this requires annotating the code itself, which I most definitely do not want to do because I specifically want the code to be polymorphic in that way. But GHC 6.8.2 does not want to let me do this.<br>
<br>
What are my options?<br>
<br>
Jacques<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>