Jaewoo Kim wrote:
> 
> > Many people have said this, but I still haven't found an instance where
> > existential types are *needed*. For example, if you declare a data type:
> 
> How about this? Is the following example possibly accepted in any extended
> Haskell system?
> 
> class Sequence seq a where
>     empty :: seq a
>     cons :: a -> seq a -> seq a
>     ...
> 
> data Stack a = forall seq.(Sequence seq) => MkStack (seq a)
> ...
> push a (MkStack seq) = MkStack (cons a seq)
> ...
> 
> I'd like to use Stack a, not one of (seq a) - possible representations for
> Stack a.
> Without existential type, could the level of abstraction like (Stack a) be
> reached at?

I think I understand what you're looking for. Correct me if I'm
mistaken, but you're trying to define a Stack data type that can be
implemented using any arbitrary Sequence type, right? That is, one Stack
value can be using a standard list to sequence items, whereas another
Stack value can be using a queue data structure. In order to do that
without involving existential types, I would do something like the
following:

data Sequence a = MkSeq {
  cons :: a -> Sequence a,
  ... }
class SequenceClass seq a where
  mkSequence :: seq a -> Sequence a
  empty :: seq a
data Stack a = MkStack (Sequence a)
...
push a (MkStack seq) = MkStack (cons seq a)
...
instance SequenceClass [] a where
  mkSequence list = let
    cons a = mkSequence (a:list)
    ...
    in MkSeq cons ...
  empty = []


- Michael Hobbs


Reply via email to