[Haskell-cafe] Re: GATD and pattern matching
Dupont Corentin
corentin.dupont at gmail.com
Thu Jun 10 17:30:50 EDT 2010
On Thu, Jun 10, 2010 at 11:14 PM, Daniel Fischer
<daniel.is.fischer at web.de>wrote:
> On Thursday 10 June 2010 22:01:38, Dupont Corentin wrote:
> > Hello Maciej,
> > i tried this out, but it didn't worked.
> >
> > Daniel,
> >
> > I added a (Show a) constraint to Equal:
> > > data Obs a where
> > > Player :: Obs Integer
> > > Turn :: Obs Integer
> > > Official :: Obs Bool
> > > Equ :: (Show a, Eq a) => Obs a -> Obs a -> Obs Bool
> >
> > --woops!!
> >
> > > Plus :: (Num a) => Obs a -> Obs a -> Obs a
> > > Time :: (Num a) => Obs a -> Obs a -> Obs a
> > > Minus :: (Num a) => Obs a -> Obs a -> Obs a
> > > Konst :: a -> Obs a
> > > And :: Obs Bool -> Obs Bool -> Obs Bool
> > > Or :: Obs Bool -> Obs Bool -> Obs Bool
> >
> > It works for the Show instance, but not Eq.
> > By the way, shouldn't the Show constraint be on the instance and not on
> > the datatype declaration?
>
> Can't be here, because of
> Equ :: Obs a -> Obs a -> Obs Bool
>
> You forget the parameter a, and you can't recover it in the instance
> declaration. So you have to provide the Show instance for a on
> construction, i.e. put the constraint on the data constructor.
>
>
Anyway, is my Obs construction revelant at all? What i want to do is to
provide an EDSL to the user to test things about the state of the game (for
the Nomic game i'm making). Obs will be then embedded in another EDSL to
construct Nomic's rules.
> > I'd prefer to keep the datatype as generic as possible...
> >
> > There is really no way to make my Obs datatype an instance of Eq and
> > Show??
>
> Show can work (should with the constraint on Equ), Eq is hairy.
>
> instance Show t => Show (Obs t) where
> show (Equ a b) = show a ++ " `Equal` " ++ show b
> show (Plus a b) = ...
> show (Konst x) = "Konst " ++ show x
> ...
>
> For an Eq instance, you have the problem that
>
> Equ (Konst True) (Konst False)
> and
> Equ Player Turn
>
> both have the type Obs Bool, but have been constructed from different
> types, so you can't compare (Konst True) and Player.
> I don't see a nice way to work around that.
>
>
These is a dirty way: compare the string representation of the rules. They
should be unique.
Corentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100610/ac28045b/attachment.html
More information about the Haskell-Cafe
mailing list