
Hi, I'm very, very new to Haskell. I am trying to understand the following error, but I'm having a hard time wrapping my head around the whole type system. Stack module Stack (Stack(..)) where import Prelude hiding (head, tail) class Stack s where empty :: s a isEmpty :: s a -> Bool cons :: s a -> a -> s a head :: s a -> a tail :: s a -> s a data ListStack a = LS [a] deriving (Show) instance Stack ListStack where empty = LS [] isEmpty (LS s) = null s cons (LS s) x = LS(x:s) head(LS(x:_)) = x tail(LS(x:xs)) = LS xs Not using Mixmax yet? The problem is that when I load this code in the interpreter and write>emptyI get the following error <interactive>:108:1: No instance for (Show (s0 a0)) arising from a use of ‘print’ The type variables ‘s0’, ‘a0’ are ambiguous I mean when I write "LS []" it works just fine. But something is wrong with empty. Thanks, Uneeb

Hello Uneeb, instances of class Stack are defined for *s*. empty's *return* type is *s a* *empty* does not take any arguments. GHCi is confused about *empty*'s *return* type. option 1: try adding *return* type at prompt e.g.: empty::ListStack Int instead of just empty option 2: add *a* as Stack class parameter i.e.: class Stack s *a* where ... then if you define only 1 instance, interactive will probably assume that you expect *empty* to return that (the only one defined) type see if this works
participants (2)
-
Imants Cekusins
-
Uneeb Adeel Agha